trace.c 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183
  1. /*
  2. * ring buffer based function tracer
  3. *
  4. * Copyright (C) 2007-2008 Steven Rostedt <srostedt@redhat.com>
  5. * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
  6. *
  7. * Originally taken from the RT patch by:
  8. * Arnaldo Carvalho de Melo <acme@redhat.com>
  9. *
  10. * Based on code from the latency_tracer, that is:
  11. * Copyright (C) 2004-2006 Ingo Molnar
  12. * Copyright (C) 2004 William Lee Irwin III
  13. */
  14. #include <linux/ring_buffer.h>
  15. #include <generated/utsrelease.h>
  16. #include <linux/stacktrace.h>
  17. #include <linux/writeback.h>
  18. #include <linux/kallsyms.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/notifier.h>
  21. #include <linux/irqflags.h>
  22. #include <linux/debugfs.h>
  23. #include <linux/pagemap.h>
  24. #include <linux/hardirq.h>
  25. #include <linux/linkage.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/kprobes.h>
  28. #include <linux/ftrace.h>
  29. #include <linux/module.h>
  30. #include <linux/percpu.h>
  31. #include <linux/splice.h>
  32. #include <linux/kdebug.h>
  33. #include <linux/string.h>
  34. #include <linux/rwsem.h>
  35. #include <linux/slab.h>
  36. #include <linux/ctype.h>
  37. #include <linux/init.h>
  38. #include <linux/poll.h>
  39. #include <linux/nmi.h>
  40. #include <linux/fs.h>
  41. #include <linux/coresight-stm.h>
  42. #include "trace.h"
  43. #include "trace_output.h"
  44. /*
  45. * On boot up, the ring buffer is set to the minimum size, so that
  46. * we do not waste memory on systems that are not using tracing.
  47. */
  48. int ring_buffer_expanded;
  49. /*
  50. * We need to change this state when a selftest is running.
  51. * A selftest will lurk into the ring-buffer to count the
  52. * entries inserted during the selftest although some concurrent
  53. * insertions into the ring-buffer such as trace_printk could occurred
  54. * at the same time, giving false positive or negative results.
  55. */
  56. static bool __read_mostly tracing_selftest_running;
  57. /*
  58. * If a tracer is running, we do not want to run SELFTEST.
  59. */
  60. bool __read_mostly tracing_selftest_disabled;
  61. /* For tracers that don't implement custom flags */
  62. static struct tracer_opt dummy_tracer_opt[] = {
  63. { }
  64. };
  65. static struct tracer_flags dummy_tracer_flags = {
  66. .val = 0,
  67. .opts = dummy_tracer_opt
  68. };
  69. static int dummy_set_flag(u32 old_flags, u32 bit, int set)
  70. {
  71. return 0;
  72. }
  73. /*
  74. * Kill all tracing for good (never come back).
  75. * It is initialized to 1 but will turn to zero if the initialization
  76. * of the tracer is successful. But that is the only place that sets
  77. * this back to zero.
  78. */
  79. static int tracing_disabled = 1;
  80. DEFINE_PER_CPU(int, ftrace_cpu_disabled);
  81. static inline void ftrace_disable_cpu(void)
  82. {
  83. preempt_disable();
  84. __this_cpu_inc(ftrace_cpu_disabled);
  85. }
  86. static inline void ftrace_enable_cpu(void)
  87. {
  88. __this_cpu_dec(ftrace_cpu_disabled);
  89. preempt_enable();
  90. }
  91. cpumask_var_t __read_mostly tracing_buffer_mask;
  92. /*
  93. * ftrace_dump_on_oops - variable to dump ftrace buffer on oops
  94. *
  95. * If there is an oops (or kernel panic) and the ftrace_dump_on_oops
  96. * is set, then ftrace_dump is called. This will output the contents
  97. * of the ftrace buffers to the console. This is very useful for
  98. * capturing traces that lead to crashes and outputing it to a
  99. * serial console.
  100. *
  101. * It is default off, but you can enable it with either specifying
  102. * "ftrace_dump_on_oops" in the kernel command line, or setting
  103. * /proc/sys/kernel/ftrace_dump_on_oops
  104. * Set 1 if you want to dump buffers of all CPUs
  105. * Set 2 if you want to dump the buffer of the CPU that triggered oops
  106. */
  107. enum ftrace_dump_mode ftrace_dump_on_oops;
  108. static int tracing_set_tracer(const char *buf);
  109. #define MAX_TRACER_SIZE 100
  110. static char bootup_tracer_buf[MAX_TRACER_SIZE] __initdata;
  111. static char *default_bootup_tracer;
  112. static int __init set_cmdline_ftrace(char *str)
  113. {
  114. strncpy(bootup_tracer_buf, str, MAX_TRACER_SIZE);
  115. default_bootup_tracer = bootup_tracer_buf;
  116. /* We are using ftrace early, expand it */
  117. ring_buffer_expanded = 1;
  118. return 1;
  119. }
  120. __setup("ftrace=", set_cmdline_ftrace);
  121. static int __init set_ftrace_dump_on_oops(char *str)
  122. {
  123. if (*str++ != '=' || !*str) {
  124. ftrace_dump_on_oops = DUMP_ALL;
  125. return 1;
  126. }
  127. if (!strcmp("orig_cpu", str)) {
  128. ftrace_dump_on_oops = DUMP_ORIG;
  129. return 1;
  130. }
  131. return 0;
  132. }
  133. __setup("ftrace_dump_on_oops", set_ftrace_dump_on_oops);
  134. unsigned long long ns2usecs(cycle_t nsec)
  135. {
  136. nsec += 500;
  137. do_div(nsec, 1000);
  138. return nsec;
  139. }
  140. /*
  141. * The global_trace is the descriptor that holds the tracing
  142. * buffers for the live tracing. For each CPU, it contains
  143. * a link list of pages that will store trace entries. The
  144. * page descriptor of the pages in the memory is used to hold
  145. * the link list by linking the lru item in the page descriptor
  146. * to each of the pages in the buffer per CPU.
  147. *
  148. * For each active CPU there is a data field that holds the
  149. * pages for the buffer for that CPU. Each CPU has the same number
  150. * of pages allocated for its buffer.
  151. */
  152. static struct trace_array global_trace;
  153. static DEFINE_PER_CPU(struct trace_array_cpu, global_trace_cpu);
  154. int filter_current_check_discard(struct ring_buffer *buffer,
  155. struct ftrace_event_call *call, void *rec,
  156. struct ring_buffer_event *event)
  157. {
  158. return filter_check_discard(call, rec, buffer, event);
  159. }
  160. EXPORT_SYMBOL_GPL(filter_current_check_discard);
  161. cycle_t ftrace_now(int cpu)
  162. {
  163. u64 ts;
  164. /* Early boot up does not have a buffer yet */
  165. if (!global_trace.buffer)
  166. return trace_clock_local();
  167. ts = ring_buffer_time_stamp(global_trace.buffer, cpu);
  168. ring_buffer_normalize_time_stamp(global_trace.buffer, cpu, &ts);
  169. return ts;
  170. }
  171. /*
  172. * The max_tr is used to snapshot the global_trace when a maximum
  173. * latency is reached. Some tracers will use this to store a maximum
  174. * trace while it continues examining live traces.
  175. *
  176. * The buffers for the max_tr are set up the same as the global_trace.
  177. * When a snapshot is taken, the link list of the max_tr is swapped
  178. * with the link list of the global_trace and the buffers are reset for
  179. * the global_trace so the tracing can continue.
  180. */
  181. static struct trace_array max_tr;
  182. static DEFINE_PER_CPU(struct trace_array_cpu, max_tr_data);
  183. /* tracer_enabled is used to toggle activation of a tracer */
  184. static int tracer_enabled = 1;
  185. /**
  186. * tracing_is_enabled - return tracer_enabled status
  187. *
  188. * This function is used by other tracers to know the status
  189. * of the tracer_enabled flag. Tracers may use this function
  190. * to know if it should enable their features when starting
  191. * up. See irqsoff tracer for an example (start_irqsoff_tracer).
  192. */
  193. int tracing_is_enabled(void)
  194. {
  195. return tracer_enabled;
  196. }
  197. /*
  198. * trace_buf_size is the size in bytes that is allocated
  199. * for a buffer. Note, the number of bytes is always rounded
  200. * to page size.
  201. *
  202. * This number is purposely set to a low number of 16384.
  203. * If the dump on oops happens, it will be much appreciated
  204. * to not have to wait for all that output. Anyway this can be
  205. * boot time and run time configurable.
  206. */
  207. #define TRACE_BUF_SIZE_DEFAULT 1441792UL /* 16384 * 88 (sizeof(entry)) */
  208. static unsigned long trace_buf_size = TRACE_BUF_SIZE_DEFAULT;
  209. /* trace_types holds a link list of available tracers. */
  210. static struct tracer *trace_types __read_mostly;
  211. /* current_trace points to the tracer that is currently active */
  212. static struct tracer *current_trace __read_mostly;
  213. /*
  214. * trace_types_lock is used to protect the trace_types list.
  215. */
  216. static DEFINE_MUTEX(trace_types_lock);
  217. /*
  218. * serialize the access of the ring buffer
  219. *
  220. * ring buffer serializes readers, but it is low level protection.
  221. * The validity of the events (which returns by ring_buffer_peek() ..etc)
  222. * are not protected by ring buffer.
  223. *
  224. * The content of events may become garbage if we allow other process consumes
  225. * these events concurrently:
  226. * A) the page of the consumed events may become a normal page
  227. * (not reader page) in ring buffer, and this page will be rewrited
  228. * by events producer.
  229. * B) The page of the consumed events may become a page for splice_read,
  230. * and this page will be returned to system.
  231. *
  232. * These primitives allow multi process access to different cpu ring buffer
  233. * concurrently.
  234. *
  235. * These primitives don't distinguish read-only and read-consume access.
  236. * Multi read-only access are also serialized.
  237. */
  238. #ifdef CONFIG_SMP
  239. static DECLARE_RWSEM(all_cpu_access_lock);
  240. static DEFINE_PER_CPU(struct mutex, cpu_access_lock);
  241. static inline void trace_access_lock(int cpu)
  242. {
  243. if (cpu == TRACE_PIPE_ALL_CPU) {
  244. /* gain it for accessing the whole ring buffer. */
  245. down_write(&all_cpu_access_lock);
  246. } else {
  247. /* gain it for accessing a cpu ring buffer. */
  248. /* Firstly block other trace_access_lock(TRACE_PIPE_ALL_CPU). */
  249. down_read(&all_cpu_access_lock);
  250. /* Secondly block other access to this @cpu ring buffer. */
  251. mutex_lock(&per_cpu(cpu_access_lock, cpu));
  252. }
  253. }
  254. static inline void trace_access_unlock(int cpu)
  255. {
  256. if (cpu == TRACE_PIPE_ALL_CPU) {
  257. up_write(&all_cpu_access_lock);
  258. } else {
  259. mutex_unlock(&per_cpu(cpu_access_lock, cpu));
  260. up_read(&all_cpu_access_lock);
  261. }
  262. }
  263. static inline void trace_access_lock_init(void)
  264. {
  265. int cpu;
  266. for_each_possible_cpu(cpu)
  267. mutex_init(&per_cpu(cpu_access_lock, cpu));
  268. }
  269. #else
  270. static DEFINE_MUTEX(access_lock);
  271. static inline void trace_access_lock(int cpu)
  272. {
  273. (void)cpu;
  274. mutex_lock(&access_lock);
  275. }
  276. static inline void trace_access_unlock(int cpu)
  277. {
  278. (void)cpu;
  279. mutex_unlock(&access_lock);
  280. }
  281. static inline void trace_access_lock_init(void)
  282. {
  283. }
  284. #endif
  285. /* trace_wait is a waitqueue for tasks blocked on trace_poll */
  286. static DECLARE_WAIT_QUEUE_HEAD(trace_wait);
  287. /* trace_flags holds trace_options default values */
  288. unsigned long trace_flags = TRACE_ITER_PRINT_PARENT | TRACE_ITER_PRINTK |
  289. TRACE_ITER_ANNOTATE | TRACE_ITER_CONTEXT_INFO | TRACE_ITER_SLEEP_TIME |
  290. TRACE_ITER_GRAPH_TIME | TRACE_ITER_RECORD_CMD | TRACE_ITER_OVERWRITE |
  291. TRACE_ITER_IRQ_INFO;
  292. static int trace_stop_count;
  293. static DEFINE_RAW_SPINLOCK(tracing_start_lock);
  294. static void wakeup_work_handler(struct work_struct *work)
  295. {
  296. wake_up(&trace_wait);
  297. }
  298. static DECLARE_DELAYED_WORK(wakeup_work, wakeup_work_handler);
  299. /**
  300. * tracing_on - enable tracing buffers
  301. *
  302. * This function enables tracing buffers that may have been
  303. * disabled with tracing_off.
  304. */
  305. void tracing_on(void)
  306. {
  307. if (global_trace.buffer)
  308. ring_buffer_record_on(global_trace.buffer);
  309. /*
  310. * This flag is only looked at when buffers haven't been
  311. * allocated yet. We don't really care about the race
  312. * between setting this flag and actually turning
  313. * on the buffer.
  314. */
  315. global_trace.buffer_disabled = 0;
  316. }
  317. EXPORT_SYMBOL_GPL(tracing_on);
  318. /**
  319. * tracing_off - turn off tracing buffers
  320. *
  321. * This function stops the tracing buffers from recording data.
  322. * It does not disable any overhead the tracers themselves may
  323. * be causing. This function simply causes all recording to
  324. * the ring buffers to fail.
  325. */
  326. void tracing_off(void)
  327. {
  328. if (global_trace.buffer)
  329. ring_buffer_record_off(global_trace.buffer);
  330. /*
  331. * This flag is only looked at when buffers haven't been
  332. * allocated yet. We don't really care about the race
  333. * between setting this flag and actually turning
  334. * on the buffer.
  335. */
  336. global_trace.buffer_disabled = 1;
  337. }
  338. EXPORT_SYMBOL_GPL(tracing_off);
  339. /**
  340. * tracing_is_on - show state of ring buffers enabled
  341. */
  342. int tracing_is_on(void)
  343. {
  344. if (global_trace.buffer)
  345. return ring_buffer_record_is_on(global_trace.buffer);
  346. return !global_trace.buffer_disabled;
  347. }
  348. EXPORT_SYMBOL_GPL(tracing_is_on);
  349. /**
  350. * trace_wake_up - wake up tasks waiting for trace input
  351. *
  352. * Schedules a delayed work to wake up any task that is blocked on the
  353. * trace_wait queue. These is used with trace_poll for tasks polling the
  354. * trace.
  355. */
  356. void trace_wake_up(void)
  357. {
  358. const unsigned long delay = msecs_to_jiffies(2);
  359. if (trace_flags & TRACE_ITER_BLOCK)
  360. return;
  361. schedule_delayed_work(&wakeup_work, delay);
  362. }
  363. static int __init set_buf_size(char *str)
  364. {
  365. unsigned long buf_size;
  366. if (!str)
  367. return 0;
  368. buf_size = memparse(str, &str);
  369. /* nr_entries can not be zero */
  370. if (buf_size == 0)
  371. return 0;
  372. trace_buf_size = buf_size;
  373. return 1;
  374. }
  375. __setup("trace_buf_size=", set_buf_size);
  376. static int __init set_tracing_thresh(char *str)
  377. {
  378. unsigned long threshhold;
  379. int ret;
  380. if (!str)
  381. return 0;
  382. ret = strict_strtoul(str, 0, &threshhold);
  383. if (ret < 0)
  384. return 0;
  385. tracing_thresh = threshhold * 1000;
  386. return 1;
  387. }
  388. __setup("tracing_thresh=", set_tracing_thresh);
  389. unsigned long nsecs_to_usecs(unsigned long nsecs)
  390. {
  391. return nsecs / 1000;
  392. }
  393. /* These must match the bit postions in trace_iterator_flags */
  394. static const char *trace_options[] = {
  395. "print-parent",
  396. "sym-offset",
  397. "sym-addr",
  398. "verbose",
  399. "raw",
  400. "hex",
  401. "bin",
  402. "block",
  403. "stacktrace",
  404. "trace_printk",
  405. "ftrace_preempt",
  406. "branch",
  407. "annotate",
  408. "userstacktrace",
  409. "sym-userobj",
  410. "printk-msg-only",
  411. "context-info",
  412. "latency-format",
  413. "sleep-time",
  414. "graph-time",
  415. "record-cmd",
  416. "overwrite",
  417. "disable_on_free",
  418. "irq-info",
  419. "print-tgid",
  420. NULL
  421. };
  422. static struct {
  423. u64 (*func)(void);
  424. const char *name;
  425. } trace_clocks[] = {
  426. { trace_clock_local, "local" },
  427. { trace_clock_global, "global" },
  428. { trace_clock_counter, "counter" },
  429. };
  430. int trace_clock_id;
  431. /*
  432. * trace_parser_get_init - gets the buffer for trace parser
  433. */
  434. int trace_parser_get_init(struct trace_parser *parser, int size)
  435. {
  436. memset(parser, 0, sizeof(*parser));
  437. parser->buffer = kmalloc(size, GFP_KERNEL);
  438. if (!parser->buffer)
  439. return 1;
  440. parser->size = size;
  441. return 0;
  442. }
  443. /*
  444. * trace_parser_put - frees the buffer for trace parser
  445. */
  446. void trace_parser_put(struct trace_parser *parser)
  447. {
  448. kfree(parser->buffer);
  449. }
  450. /*
  451. * trace_get_user - reads the user input string separated by space
  452. * (matched by isspace(ch))
  453. *
  454. * For each string found the 'struct trace_parser' is updated,
  455. * and the function returns.
  456. *
  457. * Returns number of bytes read.
  458. *
  459. * See kernel/trace/trace.h for 'struct trace_parser' details.
  460. */
  461. int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  462. size_t cnt, loff_t *ppos)
  463. {
  464. char ch;
  465. size_t read = 0;
  466. ssize_t ret;
  467. if (!*ppos)
  468. trace_parser_clear(parser);
  469. ret = get_user(ch, ubuf++);
  470. if (ret)
  471. goto out;
  472. read++;
  473. cnt--;
  474. /*
  475. * The parser is not finished with the last write,
  476. * continue reading the user input without skipping spaces.
  477. */
  478. if (!parser->cont) {
  479. /* skip white space */
  480. while (cnt && isspace(ch)) {
  481. ret = get_user(ch, ubuf++);
  482. if (ret)
  483. goto out;
  484. read++;
  485. cnt--;
  486. }
  487. /* only spaces were written */
  488. if (isspace(ch)) {
  489. *ppos += read;
  490. ret = read;
  491. goto out;
  492. }
  493. parser->idx = 0;
  494. }
  495. /* read the non-space input */
  496. while (cnt && !isspace(ch)) {
  497. if (parser->idx < parser->size - 1)
  498. parser->buffer[parser->idx++] = ch;
  499. else {
  500. ret = -EINVAL;
  501. goto out;
  502. }
  503. ret = get_user(ch, ubuf++);
  504. if (ret)
  505. goto out;
  506. read++;
  507. cnt--;
  508. }
  509. /* We either got finished input or we have to wait for another call. */
  510. if (isspace(ch)) {
  511. parser->buffer[parser->idx] = 0;
  512. parser->cont = false;
  513. } else if (parser->idx < parser->size - 1) {
  514. parser->cont = true;
  515. parser->buffer[parser->idx++] = ch;
  516. } else {
  517. ret = -EINVAL;
  518. goto out;
  519. }
  520. *ppos += read;
  521. ret = read;
  522. out:
  523. return ret;
  524. }
  525. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  526. {
  527. int len;
  528. int ret;
  529. if (!cnt)
  530. return 0;
  531. if (s->len <= s->readpos)
  532. return -EBUSY;
  533. len = s->len - s->readpos;
  534. if (cnt > len)
  535. cnt = len;
  536. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  537. if (ret == cnt)
  538. return -EFAULT;
  539. cnt -= ret;
  540. s->readpos += cnt;
  541. return cnt;
  542. }
  543. static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  544. {
  545. int len;
  546. if (s->len <= s->readpos)
  547. return -EBUSY;
  548. len = s->len - s->readpos;
  549. if (cnt > len)
  550. cnt = len;
  551. memcpy(buf, s->buffer + s->readpos, cnt);
  552. s->readpos += cnt;
  553. return cnt;
  554. }
  555. /*
  556. * ftrace_max_lock is used to protect the swapping of buffers
  557. * when taking a max snapshot. The buffers themselves are
  558. * protected by per_cpu spinlocks. But the action of the swap
  559. * needs its own lock.
  560. *
  561. * This is defined as a arch_spinlock_t in order to help
  562. * with performance when lockdep debugging is enabled.
  563. *
  564. * It is also used in other places outside the update_max_tr
  565. * so it needs to be defined outside of the
  566. * CONFIG_TRACER_MAX_TRACE.
  567. */
  568. static arch_spinlock_t ftrace_max_lock =
  569. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  570. unsigned long __read_mostly tracing_thresh;
  571. #ifdef CONFIG_TRACER_MAX_TRACE
  572. unsigned long __read_mostly tracing_max_latency;
  573. /*
  574. * Copy the new maximum trace into the separate maximum-trace
  575. * structure. (this way the maximum trace is permanently saved,
  576. * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
  577. */
  578. static void
  579. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  580. {
  581. struct trace_array_cpu *data = tr->data[cpu];
  582. struct trace_array_cpu *max_data;
  583. max_tr.cpu = cpu;
  584. max_tr.time_start = data->preempt_timestamp;
  585. max_data = max_tr.data[cpu];
  586. max_data->saved_latency = tracing_max_latency;
  587. max_data->critical_start = data->critical_start;
  588. max_data->critical_end = data->critical_end;
  589. memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
  590. max_data->pid = tsk->pid;
  591. /*
  592. * If tsk == current, then use current_uid(), as that does not use
  593. * RCU. The irq tracer can be called out of RCU scope.
  594. */
  595. if (tsk == current)
  596. max_data->uid = current_uid();
  597. else
  598. max_data->uid = task_uid(tsk);
  599. max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  600. max_data->policy = tsk->policy;
  601. max_data->rt_priority = tsk->rt_priority;
  602. /* record this tasks comm */
  603. tracing_record_cmdline(tsk);
  604. }
  605. /**
  606. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  607. * @tr: tracer
  608. * @tsk: the task with the latency
  609. * @cpu: The cpu that initiated the trace.
  610. *
  611. * Flip the buffers between the @tr and the max_tr and record information
  612. * about which task was the cause of this latency.
  613. */
  614. void
  615. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  616. {
  617. struct ring_buffer *buf;
  618. if (trace_stop_count)
  619. return;
  620. WARN_ON_ONCE(!irqs_disabled());
  621. if (!current_trace->use_max_tr) {
  622. WARN_ON_ONCE(1);
  623. return;
  624. }
  625. arch_spin_lock(&ftrace_max_lock);
  626. buf = tr->buffer;
  627. tr->buffer = max_tr.buffer;
  628. max_tr.buffer = buf;
  629. __update_max_tr(tr, tsk, cpu);
  630. arch_spin_unlock(&ftrace_max_lock);
  631. }
  632. /**
  633. * update_max_tr_single - only copy one trace over, and reset the rest
  634. * @tr - tracer
  635. * @tsk - task with the latency
  636. * @cpu - the cpu of the buffer to copy.
  637. *
  638. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  639. */
  640. void
  641. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  642. {
  643. int ret;
  644. if (trace_stop_count)
  645. return;
  646. WARN_ON_ONCE(!irqs_disabled());
  647. if (!current_trace->use_max_tr) {
  648. WARN_ON_ONCE(1);
  649. return;
  650. }
  651. arch_spin_lock(&ftrace_max_lock);
  652. ftrace_disable_cpu();
  653. ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
  654. if (ret == -EBUSY) {
  655. /*
  656. * We failed to swap the buffer due to a commit taking
  657. * place on this CPU. We fail to record, but we reset
  658. * the max trace buffer (no one writes directly to it)
  659. * and flag that it failed.
  660. */
  661. trace_array_printk(&max_tr, _THIS_IP_,
  662. "Failed to swap buffers due to commit in progress\n");
  663. }
  664. ftrace_enable_cpu();
  665. WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
  666. __update_max_tr(tr, tsk, cpu);
  667. arch_spin_unlock(&ftrace_max_lock);
  668. }
  669. #endif /* CONFIG_TRACER_MAX_TRACE */
  670. /**
  671. * register_tracer - register a tracer with the ftrace system.
  672. * @type - the plugin for the tracer
  673. *
  674. * Register a new plugin tracer.
  675. */
  676. int register_tracer(struct tracer *type)
  677. __releases(kernel_lock)
  678. __acquires(kernel_lock)
  679. {
  680. struct tracer *t;
  681. int ret = 0;
  682. if (!type->name) {
  683. pr_info("Tracer must have a name\n");
  684. return -1;
  685. }
  686. if (strlen(type->name) >= MAX_TRACER_SIZE) {
  687. pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
  688. return -1;
  689. }
  690. mutex_lock(&trace_types_lock);
  691. tracing_selftest_running = true;
  692. for (t = trace_types; t; t = t->next) {
  693. if (strcmp(type->name, t->name) == 0) {
  694. /* already found */
  695. pr_info("Tracer %s already registered\n",
  696. type->name);
  697. ret = -1;
  698. goto out;
  699. }
  700. }
  701. if (!type->set_flag)
  702. type->set_flag = &dummy_set_flag;
  703. if (!type->flags)
  704. type->flags = &dummy_tracer_flags;
  705. else
  706. if (!type->flags->opts)
  707. type->flags->opts = dummy_tracer_opt;
  708. if (!type->wait_pipe)
  709. type->wait_pipe = default_wait_pipe;
  710. #ifdef CONFIG_FTRACE_STARTUP_TEST
  711. if (type->selftest && !tracing_selftest_disabled) {
  712. struct tracer *saved_tracer = current_trace;
  713. struct trace_array *tr = &global_trace;
  714. /*
  715. * Run a selftest on this tracer.
  716. * Here we reset the trace buffer, and set the current
  717. * tracer to be this tracer. The tracer can then run some
  718. * internal tracing to verify that everything is in order.
  719. * If we fail, we do not register this tracer.
  720. */
  721. tracing_reset_online_cpus(tr);
  722. current_trace = type;
  723. /* If we expanded the buffers, make sure the max is expanded too */
  724. if (ring_buffer_expanded && type->use_max_tr)
  725. ring_buffer_resize(max_tr.buffer, trace_buf_size);
  726. /* the test is responsible for initializing and enabling */
  727. pr_info("Testing tracer %s: ", type->name);
  728. ret = type->selftest(type, tr);
  729. /* the test is responsible for resetting too */
  730. current_trace = saved_tracer;
  731. if (ret) {
  732. printk(KERN_CONT "FAILED!\n");
  733. goto out;
  734. }
  735. /* Only reset on passing, to avoid touching corrupted buffers */
  736. tracing_reset_online_cpus(tr);
  737. /* Shrink the max buffer again */
  738. if (ring_buffer_expanded && type->use_max_tr)
  739. ring_buffer_resize(max_tr.buffer, 1);
  740. printk(KERN_CONT "PASSED\n");
  741. }
  742. #endif
  743. type->next = trace_types;
  744. trace_types = type;
  745. out:
  746. tracing_selftest_running = false;
  747. mutex_unlock(&trace_types_lock);
  748. if (ret || !default_bootup_tracer)
  749. goto out_unlock;
  750. if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
  751. goto out_unlock;
  752. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  753. /* Do we want this tracer to start on bootup? */
  754. tracing_set_tracer(type->name);
  755. default_bootup_tracer = NULL;
  756. /* disable other selftests, since this will break it. */
  757. tracing_selftest_disabled = 1;
  758. #ifdef CONFIG_FTRACE_STARTUP_TEST
  759. printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
  760. type->name);
  761. #endif
  762. out_unlock:
  763. return ret;
  764. }
  765. void unregister_tracer(struct tracer *type)
  766. {
  767. struct tracer **t;
  768. mutex_lock(&trace_types_lock);
  769. for (t = &trace_types; *t; t = &(*t)->next) {
  770. if (*t == type)
  771. goto found;
  772. }
  773. pr_info("Tracer %s not registered\n", type->name);
  774. goto out;
  775. found:
  776. *t = (*t)->next;
  777. if (type == current_trace && tracer_enabled) {
  778. tracer_enabled = 0;
  779. tracing_stop();
  780. if (current_trace->stop)
  781. current_trace->stop(&global_trace);
  782. current_trace = &nop_trace;
  783. }
  784. out:
  785. mutex_unlock(&trace_types_lock);
  786. }
  787. static void __tracing_reset(struct ring_buffer *buffer, int cpu)
  788. {
  789. ftrace_disable_cpu();
  790. ring_buffer_reset_cpu(buffer, cpu);
  791. ftrace_enable_cpu();
  792. }
  793. void tracing_reset(struct trace_array *tr, int cpu)
  794. {
  795. struct ring_buffer *buffer = tr->buffer;
  796. if (!buffer)
  797. return;
  798. ring_buffer_record_disable(buffer);
  799. /* Make sure all commits have finished */
  800. synchronize_sched();
  801. __tracing_reset(buffer, cpu);
  802. ring_buffer_record_enable(buffer);
  803. }
  804. void tracing_reset_online_cpus(struct trace_array *tr)
  805. {
  806. struct ring_buffer *buffer = tr->buffer;
  807. int cpu;
  808. if (!buffer)
  809. return;
  810. ring_buffer_record_disable(buffer);
  811. /* Make sure all commits have finished */
  812. synchronize_sched();
  813. tr->time_start = ftrace_now(tr->cpu);
  814. for_each_online_cpu(cpu)
  815. __tracing_reset(buffer, cpu);
  816. ring_buffer_record_enable(buffer);
  817. }
  818. void tracing_reset_current(int cpu)
  819. {
  820. tracing_reset(&global_trace, cpu);
  821. }
  822. void tracing_reset_current_online_cpus(void)
  823. {
  824. tracing_reset_online_cpus(&global_trace);
  825. }
  826. #define SAVED_CMDLINES 128
  827. #define NO_CMDLINE_MAP UINT_MAX
  828. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  829. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  830. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  831. static unsigned saved_tgids[SAVED_CMDLINES];
  832. static int cmdline_idx;
  833. static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  834. /* temporary disable recording */
  835. static atomic_t trace_record_cmdline_disabled __read_mostly;
  836. static void trace_init_cmdlines(void)
  837. {
  838. memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
  839. memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
  840. cmdline_idx = 0;
  841. }
  842. int is_tracing_stopped(void)
  843. {
  844. return trace_stop_count;
  845. }
  846. /**
  847. * ftrace_off_permanent - disable all ftrace code permanently
  848. *
  849. * This should only be called when a serious anomally has
  850. * been detected. This will turn off the function tracing,
  851. * ring buffers, and other tracing utilites. It takes no
  852. * locks and can be called from any context.
  853. */
  854. void ftrace_off_permanent(void)
  855. {
  856. tracing_disabled = 1;
  857. ftrace_stop();
  858. tracing_off_permanent();
  859. }
  860. /**
  861. * tracing_start - quick start of the tracer
  862. *
  863. * If tracing is enabled but was stopped by tracing_stop,
  864. * this will start the tracer back up.
  865. */
  866. void tracing_start(void)
  867. {
  868. struct ring_buffer *buffer;
  869. unsigned long flags;
  870. if (tracing_disabled)
  871. return;
  872. raw_spin_lock_irqsave(&tracing_start_lock, flags);
  873. if (--trace_stop_count) {
  874. if (trace_stop_count < 0) {
  875. /* Someone screwed up their debugging */
  876. WARN_ON_ONCE(1);
  877. trace_stop_count = 0;
  878. }
  879. goto out;
  880. }
  881. /* Prevent the buffers from switching */
  882. arch_spin_lock(&ftrace_max_lock);
  883. buffer = global_trace.buffer;
  884. if (buffer)
  885. ring_buffer_record_enable(buffer);
  886. buffer = max_tr.buffer;
  887. if (buffer)
  888. ring_buffer_record_enable(buffer);
  889. arch_spin_unlock(&ftrace_max_lock);
  890. out:
  891. raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
  892. }
  893. /**
  894. * tracing_stop - quick stop of the tracer
  895. *
  896. * Light weight way to stop tracing. Use in conjunction with
  897. * tracing_start.
  898. */
  899. void tracing_stop(void)
  900. {
  901. struct ring_buffer *buffer;
  902. unsigned long flags;
  903. raw_spin_lock_irqsave(&tracing_start_lock, flags);
  904. if (trace_stop_count++)
  905. goto out;
  906. /* Prevent the buffers from switching */
  907. arch_spin_lock(&ftrace_max_lock);
  908. buffer = global_trace.buffer;
  909. if (buffer)
  910. ring_buffer_record_disable(buffer);
  911. buffer = max_tr.buffer;
  912. if (buffer)
  913. ring_buffer_record_disable(buffer);
  914. arch_spin_unlock(&ftrace_max_lock);
  915. out:
  916. raw_spin_unlock_irqrestore(&tracing_start_lock, flags);
  917. }
  918. void trace_stop_cmdline_recording(void);
  919. static void trace_save_cmdline(struct task_struct *tsk)
  920. {
  921. unsigned pid, idx;
  922. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  923. return;
  924. /*
  925. * It's not the end of the world if we don't get
  926. * the lock, but we also don't want to spin
  927. * nor do we want to disable interrupts,
  928. * so if we miss here, then better luck next time.
  929. */
  930. if (!arch_spin_trylock(&trace_cmdline_lock))
  931. return;
  932. idx = map_pid_to_cmdline[tsk->pid];
  933. if (idx == NO_CMDLINE_MAP) {
  934. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  935. /*
  936. * Check whether the cmdline buffer at idx has a pid
  937. * mapped. We are going to overwrite that entry so we
  938. * need to clear the map_pid_to_cmdline. Otherwise we
  939. * would read the new comm for the old pid.
  940. */
  941. pid = map_cmdline_to_pid[idx];
  942. if (pid != NO_CMDLINE_MAP)
  943. map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
  944. map_cmdline_to_pid[idx] = tsk->pid;
  945. map_pid_to_cmdline[tsk->pid] = idx;
  946. cmdline_idx = idx;
  947. }
  948. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  949. saved_tgids[idx] = tsk->tgid;
  950. arch_spin_unlock(&trace_cmdline_lock);
  951. }
  952. void trace_find_cmdline(int pid, char comm[])
  953. {
  954. unsigned map;
  955. if (!pid) {
  956. strcpy(comm, "<idle>");
  957. return;
  958. }
  959. if (WARN_ON_ONCE(pid < 0)) {
  960. strcpy(comm, "<XXX>");
  961. return;
  962. }
  963. if (pid > PID_MAX_DEFAULT) {
  964. strcpy(comm, "<...>");
  965. return;
  966. }
  967. preempt_disable();
  968. arch_spin_lock(&trace_cmdline_lock);
  969. map = map_pid_to_cmdline[pid];
  970. if (map != NO_CMDLINE_MAP)
  971. strlcpy(comm, saved_cmdlines[map], TASK_COMM_LEN - 1);
  972. else
  973. strcpy(comm, "<...>");
  974. arch_spin_unlock(&trace_cmdline_lock);
  975. preempt_enable();
  976. }
  977. int trace_find_tgid(int pid)
  978. {
  979. unsigned map;
  980. int tgid;
  981. preempt_disable();
  982. arch_spin_lock(&trace_cmdline_lock);
  983. map = map_pid_to_cmdline[pid];
  984. if (map != NO_CMDLINE_MAP)
  985. tgid = saved_tgids[map];
  986. else
  987. tgid = -1;
  988. arch_spin_unlock(&trace_cmdline_lock);
  989. preempt_enable();
  990. return tgid;
  991. }
  992. void tracing_record_cmdline(struct task_struct *tsk)
  993. {
  994. if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
  995. !tracing_is_on())
  996. return;
  997. trace_save_cmdline(tsk);
  998. }
  999. void
  1000. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  1001. int pc)
  1002. {
  1003. struct task_struct *tsk = current;
  1004. entry->preempt_count = pc & 0xff;
  1005. entry->pid = (tsk) ? tsk->pid : 0;
  1006. entry->flags =
  1007. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  1008. (irqs_disabled_flags(flags) ? TRACE_FLAG_IRQS_OFF : 0) |
  1009. #else
  1010. TRACE_FLAG_IRQS_NOSUPPORT |
  1011. #endif
  1012. ((pc & HARDIRQ_MASK) ? TRACE_FLAG_HARDIRQ : 0) |
  1013. ((pc & SOFTIRQ_MASK) ? TRACE_FLAG_SOFTIRQ : 0) |
  1014. (need_resched() ? TRACE_FLAG_NEED_RESCHED : 0);
  1015. }
  1016. EXPORT_SYMBOL_GPL(tracing_generic_entry_update);
  1017. struct ring_buffer_event *
  1018. trace_buffer_lock_reserve(struct ring_buffer *buffer,
  1019. int type,
  1020. unsigned long len,
  1021. unsigned long flags, int pc)
  1022. {
  1023. struct ring_buffer_event *event;
  1024. event = ring_buffer_lock_reserve(buffer, len);
  1025. if (event != NULL) {
  1026. struct trace_entry *ent = ring_buffer_event_data(event);
  1027. tracing_generic_entry_update(ent, flags, pc);
  1028. ent->type = type;
  1029. }
  1030. return event;
  1031. }
  1032. static inline void
  1033. __trace_buffer_unlock_commit(struct ring_buffer *buffer,
  1034. struct ring_buffer_event *event,
  1035. unsigned long flags, int pc,
  1036. int wake)
  1037. {
  1038. ring_buffer_unlock_commit(buffer, event);
  1039. ftrace_trace_stack(buffer, flags, 6, pc);
  1040. ftrace_trace_userstack(buffer, flags, pc);
  1041. if (wake)
  1042. trace_wake_up();
  1043. }
  1044. void trace_buffer_unlock_commit(struct ring_buffer *buffer,
  1045. struct ring_buffer_event *event,
  1046. unsigned long flags, int pc)
  1047. {
  1048. __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
  1049. }
  1050. struct ring_buffer_event *
  1051. trace_current_buffer_lock_reserve(struct ring_buffer **current_rb,
  1052. int type, unsigned long len,
  1053. unsigned long flags, int pc)
  1054. {
  1055. *current_rb = global_trace.buffer;
  1056. return trace_buffer_lock_reserve(*current_rb,
  1057. type, len, flags, pc);
  1058. }
  1059. EXPORT_SYMBOL_GPL(trace_current_buffer_lock_reserve);
  1060. void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
  1061. struct ring_buffer_event *event,
  1062. unsigned long flags, int pc)
  1063. {
  1064. __trace_buffer_unlock_commit(buffer, event, flags, pc, 1);
  1065. }
  1066. EXPORT_SYMBOL_GPL(trace_current_buffer_unlock_commit);
  1067. void trace_nowake_buffer_unlock_commit(struct ring_buffer *buffer,
  1068. struct ring_buffer_event *event,
  1069. unsigned long flags, int pc)
  1070. {
  1071. __trace_buffer_unlock_commit(buffer, event, flags, pc, 0);
  1072. }
  1073. EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit);
  1074. void trace_nowake_buffer_unlock_commit_regs(struct ring_buffer *buffer,
  1075. struct ring_buffer_event *event,
  1076. unsigned long flags, int pc,
  1077. struct pt_regs *regs)
  1078. {
  1079. ring_buffer_unlock_commit(buffer, event);
  1080. ftrace_trace_stack_regs(buffer, flags, 0, pc, regs);
  1081. ftrace_trace_userstack(buffer, flags, pc);
  1082. }
  1083. EXPORT_SYMBOL_GPL(trace_nowake_buffer_unlock_commit_regs);
  1084. void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
  1085. struct ring_buffer_event *event)
  1086. {
  1087. ring_buffer_discard_commit(buffer, event);
  1088. }
  1089. EXPORT_SYMBOL_GPL(trace_current_buffer_discard_commit);
  1090. void
  1091. trace_function(struct trace_array *tr,
  1092. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  1093. int pc)
  1094. {
  1095. struct ftrace_event_call *call = &event_function;
  1096. struct ring_buffer *buffer = tr->buffer;
  1097. struct ring_buffer_event *event;
  1098. struct ftrace_entry *entry;
  1099. /* If we are reading the ring buffer, don't trace */
  1100. if (unlikely(__this_cpu_read(ftrace_cpu_disabled)))
  1101. return;
  1102. event = trace_buffer_lock_reserve(buffer, TRACE_FN, sizeof(*entry),
  1103. flags, pc);
  1104. if (!event)
  1105. return;
  1106. entry = ring_buffer_event_data(event);
  1107. entry->ip = ip;
  1108. entry->parent_ip = parent_ip;
  1109. if (!filter_check_discard(call, entry, buffer, event))
  1110. ring_buffer_unlock_commit(buffer, event);
  1111. }
  1112. void
  1113. ftrace(struct trace_array *tr, struct trace_array_cpu *data,
  1114. unsigned long ip, unsigned long parent_ip, unsigned long flags,
  1115. int pc)
  1116. {
  1117. if (likely(!atomic_read(&data->disabled)))
  1118. trace_function(tr, ip, parent_ip, flags, pc);
  1119. }
  1120. #ifdef CONFIG_STACKTRACE
  1121. #define FTRACE_STACK_MAX_ENTRIES (PAGE_SIZE / sizeof(unsigned long))
  1122. struct ftrace_stack {
  1123. unsigned long calls[FTRACE_STACK_MAX_ENTRIES];
  1124. };
  1125. static DEFINE_PER_CPU(struct ftrace_stack, ftrace_stack);
  1126. static DEFINE_PER_CPU(int, ftrace_stack_reserve);
  1127. static void __ftrace_trace_stack(struct ring_buffer *buffer,
  1128. unsigned long flags,
  1129. int skip, int pc, struct pt_regs *regs)
  1130. {
  1131. struct ftrace_event_call *call = &event_kernel_stack;
  1132. struct ring_buffer_event *event;
  1133. struct stack_entry *entry;
  1134. struct stack_trace trace;
  1135. int use_stack;
  1136. int size = FTRACE_STACK_ENTRIES;
  1137. trace.nr_entries = 0;
  1138. trace.skip = skip;
  1139. /*
  1140. * Since events can happen in NMIs there's no safe way to
  1141. * use the per cpu ftrace_stacks. We reserve it and if an interrupt
  1142. * or NMI comes in, it will just have to use the default
  1143. * FTRACE_STACK_SIZE.
  1144. */
  1145. preempt_disable_notrace();
  1146. use_stack = ++__get_cpu_var(ftrace_stack_reserve);
  1147. /*
  1148. * We don't need any atomic variables, just a barrier.
  1149. * If an interrupt comes in, we don't care, because it would
  1150. * have exited and put the counter back to what we want.
  1151. * We just need a barrier to keep gcc from moving things
  1152. * around.
  1153. */
  1154. barrier();
  1155. if (use_stack == 1) {
  1156. trace.entries = &__get_cpu_var(ftrace_stack).calls[0];
  1157. trace.max_entries = FTRACE_STACK_MAX_ENTRIES;
  1158. if (regs)
  1159. save_stack_trace_regs(regs, &trace);
  1160. else
  1161. save_stack_trace(&trace);
  1162. if (trace.nr_entries > size)
  1163. size = trace.nr_entries;
  1164. } else
  1165. /* From now on, use_stack is a boolean */
  1166. use_stack = 0;
  1167. size *= sizeof(unsigned long);
  1168. event = trace_buffer_lock_reserve(buffer, TRACE_STACK,
  1169. sizeof(*entry) + size, flags, pc);
  1170. if (!event)
  1171. goto out;
  1172. entry = ring_buffer_event_data(event);
  1173. memset(&entry->caller, 0, size);
  1174. if (use_stack)
  1175. memcpy(&entry->caller, trace.entries,
  1176. trace.nr_entries * sizeof(unsigned long));
  1177. else {
  1178. trace.max_entries = FTRACE_STACK_ENTRIES;
  1179. trace.entries = entry->caller;
  1180. if (regs)
  1181. save_stack_trace_regs(regs, &trace);
  1182. else
  1183. save_stack_trace(&trace);
  1184. }
  1185. entry->size = trace.nr_entries;
  1186. if (!filter_check_discard(call, entry, buffer, event))
  1187. ring_buffer_unlock_commit(buffer, event);
  1188. out:
  1189. /* Again, don't let gcc optimize things here */
  1190. barrier();
  1191. __get_cpu_var(ftrace_stack_reserve)--;
  1192. preempt_enable_notrace();
  1193. }
  1194. void ftrace_trace_stack_regs(struct ring_buffer *buffer, unsigned long flags,
  1195. int skip, int pc, struct pt_regs *regs)
  1196. {
  1197. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  1198. return;
  1199. __ftrace_trace_stack(buffer, flags, skip, pc, regs);
  1200. }
  1201. void ftrace_trace_stack(struct ring_buffer *buffer, unsigned long flags,
  1202. int skip, int pc)
  1203. {
  1204. if (!(trace_flags & TRACE_ITER_STACKTRACE))
  1205. return;
  1206. __ftrace_trace_stack(buffer, flags, skip, pc, NULL);
  1207. }
  1208. void __trace_stack(struct trace_array *tr, unsigned long flags, int skip,
  1209. int pc)
  1210. {
  1211. __ftrace_trace_stack(tr->buffer, flags, skip, pc, NULL);
  1212. }
  1213. /**
  1214. * trace_dump_stack - record a stack back trace in the trace buffer
  1215. */
  1216. void trace_dump_stack(void)
  1217. {
  1218. unsigned long flags;
  1219. if (tracing_disabled || tracing_selftest_running)
  1220. return;
  1221. local_save_flags(flags);
  1222. /* skipping 3 traces, seems to get us at the caller of this function */
  1223. __ftrace_trace_stack(global_trace.buffer, flags, 3, preempt_count(), NULL);
  1224. }
  1225. static DEFINE_PER_CPU(int, user_stack_count);
  1226. void
  1227. ftrace_trace_userstack(struct ring_buffer *buffer, unsigned long flags, int pc)
  1228. {
  1229. struct ftrace_event_call *call = &event_user_stack;
  1230. struct ring_buffer_event *event;
  1231. struct userstack_entry *entry;
  1232. struct stack_trace trace;
  1233. if (!(trace_flags & TRACE_ITER_USERSTACKTRACE))
  1234. return;
  1235. /*
  1236. * NMIs can not handle page faults, even with fix ups.
  1237. * The save user stack can (and often does) fault.
  1238. */
  1239. if (unlikely(in_nmi()))
  1240. return;
  1241. /*
  1242. * prevent recursion, since the user stack tracing may
  1243. * trigger other kernel events.
  1244. */
  1245. preempt_disable();
  1246. if (__this_cpu_read(user_stack_count))
  1247. goto out;
  1248. __this_cpu_inc(user_stack_count);
  1249. event = trace_buffer_lock_reserve(buffer, TRACE_USER_STACK,
  1250. sizeof(*entry), flags, pc);
  1251. if (!event)
  1252. goto out_drop_count;
  1253. entry = ring_buffer_event_data(event);
  1254. entry->tgid = current->tgid;
  1255. memset(&entry->caller, 0, sizeof(entry->caller));
  1256. trace.nr_entries = 0;
  1257. trace.max_entries = FTRACE_STACK_ENTRIES;
  1258. trace.skip = 0;
  1259. trace.entries = entry->caller;
  1260. save_stack_trace_user(&trace);
  1261. if (!filter_check_discard(call, entry, buffer, event))
  1262. ring_buffer_unlock_commit(buffer, event);
  1263. out_drop_count:
  1264. __this_cpu_dec(user_stack_count);
  1265. out:
  1266. preempt_enable();
  1267. }
  1268. #ifdef UNUSED
  1269. static void __trace_userstack(struct trace_array *tr, unsigned long flags)
  1270. {
  1271. ftrace_trace_userstack(tr, flags, preempt_count());
  1272. }
  1273. #endif /* UNUSED */
  1274. #endif /* CONFIG_STACKTRACE */
  1275. /**
  1276. * trace_vbprintk - write binary msg to tracing buffer
  1277. *
  1278. */
  1279. int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
  1280. {
  1281. static arch_spinlock_t trace_buf_lock =
  1282. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  1283. static u32 trace_buf[TRACE_BUF_SIZE];
  1284. struct ftrace_event_call *call = &event_bprint;
  1285. struct ring_buffer_event *event;
  1286. struct ring_buffer *buffer;
  1287. struct trace_array *tr = &global_trace;
  1288. struct trace_array_cpu *data;
  1289. struct bprint_entry *entry;
  1290. unsigned long flags;
  1291. int disable;
  1292. int cpu, len = 0, size, pc;
  1293. if (unlikely(tracing_selftest_running || tracing_disabled))
  1294. return 0;
  1295. /* Don't pollute graph traces with trace_vprintk internals */
  1296. pause_graph_tracing();
  1297. pc = preempt_count();
  1298. preempt_disable_notrace();
  1299. cpu = raw_smp_processor_id();
  1300. data = tr->data[cpu];
  1301. disable = atomic_inc_return(&data->disabled);
  1302. if (unlikely(disable != 1))
  1303. goto out;
  1304. /* Lockdep uses trace_printk for lock tracing */
  1305. local_irq_save(flags);
  1306. arch_spin_lock(&trace_buf_lock);
  1307. len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  1308. if (len > TRACE_BUF_SIZE || len < 0)
  1309. goto out_unlock;
  1310. size = sizeof(*entry) + sizeof(u32) * len;
  1311. buffer = tr->buffer;
  1312. event = trace_buffer_lock_reserve(buffer, TRACE_BPRINT, size,
  1313. flags, pc);
  1314. if (!event)
  1315. goto out_unlock;
  1316. entry = ring_buffer_event_data(event);
  1317. entry->ip = ip;
  1318. entry->fmt = fmt;
  1319. memcpy(entry->buf, trace_buf, sizeof(u32) * len);
  1320. if (!filter_check_discard(call, entry, buffer, event)) {
  1321. ring_buffer_unlock_commit(buffer, event);
  1322. ftrace_trace_stack(buffer, flags, 6, pc);
  1323. }
  1324. out_unlock:
  1325. arch_spin_unlock(&trace_buf_lock);
  1326. local_irq_restore(flags);
  1327. out:
  1328. atomic_dec_return(&data->disabled);
  1329. preempt_enable_notrace();
  1330. unpause_graph_tracing();
  1331. return len;
  1332. }
  1333. EXPORT_SYMBOL_GPL(trace_vbprintk);
  1334. int trace_array_printk(struct trace_array *tr,
  1335. unsigned long ip, const char *fmt, ...)
  1336. {
  1337. int ret;
  1338. va_list ap;
  1339. if (!(trace_flags & TRACE_ITER_PRINTK))
  1340. return 0;
  1341. va_start(ap, fmt);
  1342. ret = trace_array_vprintk(tr, ip, fmt, ap);
  1343. va_end(ap);
  1344. return ret;
  1345. }
  1346. int trace_array_vprintk(struct trace_array *tr,
  1347. unsigned long ip, const char *fmt, va_list args)
  1348. {
  1349. static arch_spinlock_t trace_buf_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  1350. static char trace_buf[TRACE_BUF_SIZE];
  1351. struct ftrace_event_call *call = &event_print;
  1352. struct ring_buffer_event *event;
  1353. struct ring_buffer *buffer;
  1354. struct trace_array_cpu *data;
  1355. int cpu, len = 0, size, pc;
  1356. struct print_entry *entry;
  1357. unsigned long irq_flags;
  1358. int disable;
  1359. if (tracing_disabled || tracing_selftest_running)
  1360. return 0;
  1361. pc = preempt_count();
  1362. preempt_disable_notrace();
  1363. cpu = raw_smp_processor_id();
  1364. data = tr->data[cpu];
  1365. disable = atomic_inc_return(&data->disabled);
  1366. if (unlikely(disable != 1))
  1367. goto out;
  1368. pause_graph_tracing();
  1369. raw_local_irq_save(irq_flags);
  1370. arch_spin_lock(&trace_buf_lock);
  1371. len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
  1372. size = sizeof(*entry) + len + 1;
  1373. buffer = tr->buffer;
  1374. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  1375. irq_flags, pc);
  1376. if (!event)
  1377. goto out_unlock;
  1378. entry = ring_buffer_event_data(event);
  1379. entry->ip = ip;
  1380. memcpy(&entry->buf, trace_buf, len);
  1381. entry->buf[len] = '\0';
  1382. if (!filter_check_discard(call, entry, buffer, event)) {
  1383. stm_log(OST_ENTITY_TRACE_PRINTK, entry->buf, len + 1);
  1384. ring_buffer_unlock_commit(buffer, event);
  1385. ftrace_trace_stack(buffer, irq_flags, 6, pc);
  1386. }
  1387. out_unlock:
  1388. arch_spin_unlock(&trace_buf_lock);
  1389. raw_local_irq_restore(irq_flags);
  1390. unpause_graph_tracing();
  1391. out:
  1392. atomic_dec_return(&data->disabled);
  1393. preempt_enable_notrace();
  1394. return len;
  1395. }
  1396. int trace_vprintk(unsigned long ip, const char *fmt, va_list args)
  1397. {
  1398. return trace_array_vprintk(&global_trace, ip, fmt, args);
  1399. }
  1400. EXPORT_SYMBOL_GPL(trace_vprintk);
  1401. static void trace_iterator_increment(struct trace_iterator *iter)
  1402. {
  1403. /* Don't allow ftrace to trace into the ring buffers */
  1404. ftrace_disable_cpu();
  1405. iter->idx++;
  1406. if (iter->buffer_iter[iter->cpu])
  1407. ring_buffer_read(iter->buffer_iter[iter->cpu], NULL);
  1408. ftrace_enable_cpu();
  1409. }
  1410. static struct trace_entry *
  1411. peek_next_entry(struct trace_iterator *iter, int cpu, u64 *ts,
  1412. unsigned long *lost_events)
  1413. {
  1414. struct ring_buffer_event *event;
  1415. struct ring_buffer_iter *buf_iter = iter->buffer_iter[cpu];
  1416. /* Don't allow ftrace to trace into the ring buffers */
  1417. ftrace_disable_cpu();
  1418. if (buf_iter)
  1419. event = ring_buffer_iter_peek(buf_iter, ts);
  1420. else
  1421. event = ring_buffer_peek(iter->tr->buffer, cpu, ts,
  1422. lost_events);
  1423. ftrace_enable_cpu();
  1424. if (event) {
  1425. iter->ent_size = ring_buffer_event_length(event);
  1426. return ring_buffer_event_data(event);
  1427. }
  1428. iter->ent_size = 0;
  1429. return NULL;
  1430. }
  1431. static struct trace_entry *
  1432. __find_next_entry(struct trace_iterator *iter, int *ent_cpu,
  1433. unsigned long *missing_events, u64 *ent_ts)
  1434. {
  1435. struct ring_buffer *buffer = iter->tr->buffer;
  1436. struct trace_entry *ent, *next = NULL;
  1437. unsigned long lost_events = 0, next_lost = 0;
  1438. int cpu_file = iter->cpu_file;
  1439. u64 next_ts = 0, ts;
  1440. int next_cpu = -1;
  1441. int next_size = 0;
  1442. int cpu;
  1443. /*
  1444. * If we are in a per_cpu trace file, don't bother by iterating over
  1445. * all cpu and peek directly.
  1446. */
  1447. if (cpu_file > TRACE_PIPE_ALL_CPU) {
  1448. if (ring_buffer_empty_cpu(buffer, cpu_file))
  1449. return NULL;
  1450. ent = peek_next_entry(iter, cpu_file, ent_ts, missing_events);
  1451. if (ent_cpu)
  1452. *ent_cpu = cpu_file;
  1453. return ent;
  1454. }
  1455. for_each_tracing_cpu(cpu) {
  1456. if (ring_buffer_empty_cpu(buffer, cpu))
  1457. continue;
  1458. ent = peek_next_entry(iter, cpu, &ts, &lost_events);
  1459. /*
  1460. * Pick the entry with the smallest timestamp:
  1461. */
  1462. if (ent && (!next || ts < next_ts)) {
  1463. next = ent;
  1464. next_cpu = cpu;
  1465. next_ts = ts;
  1466. next_lost = lost_events;
  1467. next_size = iter->ent_size;
  1468. }
  1469. }
  1470. iter->ent_size = next_size;
  1471. if (ent_cpu)
  1472. *ent_cpu = next_cpu;
  1473. if (ent_ts)
  1474. *ent_ts = next_ts;
  1475. if (missing_events)
  1476. *missing_events = next_lost;
  1477. return next;
  1478. }
  1479. /* Find the next real entry, without updating the iterator itself */
  1480. struct trace_entry *trace_find_next_entry(struct trace_iterator *iter,
  1481. int *ent_cpu, u64 *ent_ts)
  1482. {
  1483. return __find_next_entry(iter, ent_cpu, NULL, ent_ts);
  1484. }
  1485. /* Find the next real entry, and increment the iterator to the next entry */
  1486. void *trace_find_next_entry_inc(struct trace_iterator *iter)
  1487. {
  1488. iter->ent = __find_next_entry(iter, &iter->cpu,
  1489. &iter->lost_events, &iter->ts);
  1490. if (iter->ent)
  1491. trace_iterator_increment(iter);
  1492. return iter->ent ? iter : NULL;
  1493. }
  1494. static void trace_consume(struct trace_iterator *iter)
  1495. {
  1496. /* Don't allow ftrace to trace into the ring buffers */
  1497. ftrace_disable_cpu();
  1498. ring_buffer_consume(iter->tr->buffer, iter->cpu, &iter->ts,
  1499. &iter->lost_events);
  1500. ftrace_enable_cpu();
  1501. }
  1502. static void *s_next(struct seq_file *m, void *v, loff_t *pos)
  1503. {
  1504. struct trace_iterator *iter = m->private;
  1505. int i = (int)*pos;
  1506. void *ent;
  1507. WARN_ON_ONCE(iter->leftover);
  1508. (*pos)++;
  1509. /* can't go backwards */
  1510. if (iter->idx > i)
  1511. return NULL;
  1512. if (iter->idx < 0)
  1513. ent = trace_find_next_entry_inc(iter);
  1514. else
  1515. ent = iter;
  1516. while (ent && iter->idx < i)
  1517. ent = trace_find_next_entry_inc(iter);
  1518. iter->pos = *pos;
  1519. return ent;
  1520. }
  1521. void tracing_iter_reset(struct trace_iterator *iter, int cpu)
  1522. {
  1523. struct trace_array *tr = iter->tr;
  1524. struct ring_buffer_event *event;
  1525. struct ring_buffer_iter *buf_iter;
  1526. unsigned long entries = 0;
  1527. u64 ts;
  1528. tr->data[cpu]->skipped_entries = 0;
  1529. if (!iter->buffer_iter[cpu])
  1530. return;
  1531. buf_iter = iter->buffer_iter[cpu];
  1532. ring_buffer_iter_reset(buf_iter);
  1533. /*
  1534. * We could have the case with the max latency tracers
  1535. * that a reset never took place on a cpu. This is evident
  1536. * by the timestamp being before the start of the buffer.
  1537. */
  1538. while ((event = ring_buffer_iter_peek(buf_iter, &ts))) {
  1539. if (ts >= iter->tr->time_start)
  1540. break;
  1541. entries++;
  1542. ring_buffer_read(buf_iter, NULL);
  1543. }
  1544. tr->data[cpu]->skipped_entries = entries;
  1545. }
  1546. /*
  1547. * The current tracer is copied to avoid a global locking
  1548. * all around.
  1549. */
  1550. static void *s_start(struct seq_file *m, loff_t *pos)
  1551. {
  1552. struct trace_iterator *iter = m->private;
  1553. static struct tracer *old_tracer;
  1554. int cpu_file = iter->cpu_file;
  1555. void *p = NULL;
  1556. loff_t l = 0;
  1557. int cpu;
  1558. /* copy the tracer to avoid using a global lock all around */
  1559. mutex_lock(&trace_types_lock);
  1560. if (unlikely(old_tracer != current_trace && current_trace)) {
  1561. old_tracer = current_trace;
  1562. *iter->trace = *current_trace;
  1563. }
  1564. mutex_unlock(&trace_types_lock);
  1565. atomic_inc(&trace_record_cmdline_disabled);
  1566. if (*pos != iter->pos) {
  1567. iter->ent = NULL;
  1568. iter->cpu = 0;
  1569. iter->idx = -1;
  1570. ftrace_disable_cpu();
  1571. if (cpu_file == TRACE_PIPE_ALL_CPU) {
  1572. for_each_tracing_cpu(cpu)
  1573. tracing_iter_reset(iter, cpu);
  1574. } else
  1575. tracing_iter_reset(iter, cpu_file);
  1576. ftrace_enable_cpu();
  1577. iter->leftover = 0;
  1578. for (p = iter; p && l < *pos; p = s_next(m, p, &l))
  1579. ;
  1580. } else {
  1581. /*
  1582. * If we overflowed the seq_file before, then we want
  1583. * to just reuse the trace_seq buffer again.
  1584. */
  1585. if (iter->leftover)
  1586. p = iter;
  1587. else {
  1588. l = *pos - 1;
  1589. p = s_next(m, p, &l);
  1590. }
  1591. }
  1592. trace_event_read_lock();
  1593. trace_access_lock(cpu_file);
  1594. return p;
  1595. }
  1596. static void s_stop(struct seq_file *m, void *p)
  1597. {
  1598. struct trace_iterator *iter = m->private;
  1599. atomic_dec(&trace_record_cmdline_disabled);
  1600. trace_access_unlock(iter->cpu_file);
  1601. trace_event_read_unlock();
  1602. }
  1603. static void
  1604. get_total_entries(struct trace_array *tr, unsigned long *total, unsigned long *entries)
  1605. {
  1606. unsigned long count;
  1607. int cpu;
  1608. *total = 0;
  1609. *entries = 0;
  1610. for_each_tracing_cpu(cpu) {
  1611. count = ring_buffer_entries_cpu(tr->buffer, cpu);
  1612. /*
  1613. * If this buffer has skipped entries, then we hold all
  1614. * entries for the trace and we need to ignore the
  1615. * ones before the time stamp.
  1616. */
  1617. if (tr->data[cpu]->skipped_entries) {
  1618. count -= tr->data[cpu]->skipped_entries;
  1619. /* total is the same as the entries */
  1620. *total += count;
  1621. } else
  1622. *total += count +
  1623. ring_buffer_overrun_cpu(tr->buffer, cpu);
  1624. *entries += count;
  1625. }
  1626. }
  1627. static void print_lat_help_header(struct seq_file *m)
  1628. {
  1629. seq_puts(m, "# _------=> CPU# \n");
  1630. seq_puts(m, "# / _-----=> irqs-off \n");
  1631. seq_puts(m, "# | / _----=> need-resched \n");
  1632. seq_puts(m, "# || / _---=> hardirq/softirq \n");
  1633. seq_puts(m, "# ||| / _--=> preempt-depth \n");
  1634. seq_puts(m, "# |||| / delay \n");
  1635. seq_puts(m, "# cmd pid ||||| time | caller \n");
  1636. seq_puts(m, "# \\ / ||||| \\ | / \n");
  1637. }
  1638. static void print_event_info(struct trace_array *tr, struct seq_file *m)
  1639. {
  1640. unsigned long total;
  1641. unsigned long entries;
  1642. get_total_entries(tr, &total, &entries);
  1643. seq_printf(m, "# entries-in-buffer/entries-written: %lu/%lu #P:%d\n",
  1644. entries, total, num_online_cpus());
  1645. seq_puts(m, "#\n");
  1646. }
  1647. static void print_func_help_header(struct trace_array *tr, struct seq_file *m)
  1648. {
  1649. print_event_info(tr, m);
  1650. seq_puts(m, "# TASK-PID CPU# TIMESTAMP FUNCTION\n");
  1651. seq_puts(m, "# | | | | |\n");
  1652. }
  1653. static void print_func_help_header_tgid(struct trace_array *tr, struct seq_file *m)
  1654. {
  1655. print_event_info(tr, m);
  1656. seq_puts(m, "# TASK-PID TGID CPU# TIMESTAMP FUNCTION\n");
  1657. seq_puts(m, "# | | | | | |\n");
  1658. }
  1659. static void print_func_help_header_irq(struct trace_array *tr, struct seq_file *m)
  1660. {
  1661. print_event_info(tr, m);
  1662. seq_puts(m, "# _-----=> irqs-off\n");
  1663. seq_puts(m, "# / _----=> need-resched\n");
  1664. seq_puts(m, "# | / _---=> hardirq/softirq\n");
  1665. seq_puts(m, "# || / _--=> preempt-depth\n");
  1666. seq_puts(m, "# ||| / delay\n");
  1667. seq_puts(m, "# TASK-PID CPU# |||| TIMESTAMP FUNCTION\n");
  1668. seq_puts(m, "# | | | |||| | |\n");
  1669. }
  1670. static void print_func_help_header_irq_tgid(struct trace_array *tr, struct seq_file *m)
  1671. {
  1672. print_event_info(tr, m);
  1673. seq_puts(m, "# _-----=> irqs-off\n");
  1674. seq_puts(m, "# / _----=> need-resched\n");
  1675. seq_puts(m, "# | / _---=> hardirq/softirq\n");
  1676. seq_puts(m, "# || / _--=> preempt-depth\n");
  1677. seq_puts(m, "# ||| / delay\n");
  1678. seq_puts(m, "# TASK-PID TGID CPU# |||| TIMESTAMP FUNCTION\n");
  1679. seq_puts(m, "# | | | | |||| | |\n");
  1680. }
  1681. void
  1682. print_trace_header(struct seq_file *m, struct trace_iterator *iter)
  1683. {
  1684. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1685. struct trace_array *tr = iter->tr;
  1686. struct trace_array_cpu *data = tr->data[tr->cpu];
  1687. struct tracer *type = current_trace;
  1688. unsigned long entries;
  1689. unsigned long total;
  1690. const char *name = "preemption";
  1691. if (type)
  1692. name = type->name;
  1693. get_total_entries(tr, &total, &entries);
  1694. seq_printf(m, "# %s latency trace v1.1.5 on %s\n",
  1695. name, UTS_RELEASE);
  1696. seq_puts(m, "# -----------------------------------"
  1697. "---------------------------------\n");
  1698. seq_printf(m, "# latency: %lu us, #%lu/%lu, CPU#%d |"
  1699. " (M:%s VP:%d, KP:%d, SP:%d HP:%d",
  1700. nsecs_to_usecs(data->saved_latency),
  1701. entries,
  1702. total,
  1703. tr->cpu,
  1704. #if defined(CONFIG_PREEMPT_NONE)
  1705. "server",
  1706. #elif defined(CONFIG_PREEMPT_VOLUNTARY)
  1707. "desktop",
  1708. #elif defined(CONFIG_PREEMPT)
  1709. "preempt",
  1710. #else
  1711. "unknown",
  1712. #endif
  1713. /* These are reserved for later use */
  1714. 0, 0, 0, 0);
  1715. #ifdef CONFIG_SMP
  1716. seq_printf(m, " #P:%d)\n", num_online_cpus());
  1717. #else
  1718. seq_puts(m, ")\n");
  1719. #endif
  1720. seq_puts(m, "# -----------------\n");
  1721. seq_printf(m, "# | task: %.16s-%d "
  1722. "(uid:%d nice:%ld policy:%ld rt_prio:%ld)\n",
  1723. data->comm, data->pid, data->uid, data->nice,
  1724. data->policy, data->rt_priority);
  1725. seq_puts(m, "# -----------------\n");
  1726. if (data->critical_start) {
  1727. seq_puts(m, "# => started at: ");
  1728. seq_print_ip_sym(&iter->seq, data->critical_start, sym_flags);
  1729. trace_print_seq(m, &iter->seq);
  1730. seq_puts(m, "\n# => ended at: ");
  1731. seq_print_ip_sym(&iter->seq, data->critical_end, sym_flags);
  1732. trace_print_seq(m, &iter->seq);
  1733. seq_puts(m, "\n#\n");
  1734. }
  1735. seq_puts(m, "#\n");
  1736. }
  1737. static void test_cpu_buff_start(struct trace_iterator *iter)
  1738. {
  1739. struct trace_seq *s = &iter->seq;
  1740. if (!(trace_flags & TRACE_ITER_ANNOTATE))
  1741. return;
  1742. if (!(iter->iter_flags & TRACE_FILE_ANNOTATE))
  1743. return;
  1744. if (cpumask_test_cpu(iter->cpu, iter->started))
  1745. return;
  1746. if (iter->tr->data[iter->cpu]->skipped_entries)
  1747. return;
  1748. cpumask_set_cpu(iter->cpu, iter->started);
  1749. /* Don't print started cpu buffer for the first entry of the trace */
  1750. if (iter->idx > 1)
  1751. trace_seq_printf(s, "##### CPU %u buffer started ####\n",
  1752. iter->cpu);
  1753. }
  1754. static enum print_line_t print_trace_fmt(struct trace_iterator *iter)
  1755. {
  1756. struct trace_seq *s = &iter->seq;
  1757. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  1758. struct trace_entry *entry;
  1759. struct trace_event *event;
  1760. entry = iter->ent;
  1761. test_cpu_buff_start(iter);
  1762. event = ftrace_find_event(entry->type);
  1763. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1764. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1765. if (!trace_print_lat_context(iter))
  1766. goto partial;
  1767. } else {
  1768. if (!trace_print_context(iter))
  1769. goto partial;
  1770. }
  1771. }
  1772. if (event)
  1773. return event->funcs->trace(iter, sym_flags, event);
  1774. if (!trace_seq_printf(s, "Unknown type %d\n", entry->type))
  1775. goto partial;
  1776. return TRACE_TYPE_HANDLED;
  1777. partial:
  1778. return TRACE_TYPE_PARTIAL_LINE;
  1779. }
  1780. static enum print_line_t print_raw_fmt(struct trace_iterator *iter)
  1781. {
  1782. struct trace_seq *s = &iter->seq;
  1783. struct trace_entry *entry;
  1784. struct trace_event *event;
  1785. entry = iter->ent;
  1786. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1787. if (!trace_seq_printf(s, "%d %d %llu ",
  1788. entry->pid, iter->cpu, iter->ts))
  1789. goto partial;
  1790. }
  1791. event = ftrace_find_event(entry->type);
  1792. if (event)
  1793. return event->funcs->raw(iter, 0, event);
  1794. if (!trace_seq_printf(s, "%d ?\n", entry->type))
  1795. goto partial;
  1796. return TRACE_TYPE_HANDLED;
  1797. partial:
  1798. return TRACE_TYPE_PARTIAL_LINE;
  1799. }
  1800. static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
  1801. {
  1802. struct trace_seq *s = &iter->seq;
  1803. unsigned char newline = '\n';
  1804. struct trace_entry *entry;
  1805. struct trace_event *event;
  1806. entry = iter->ent;
  1807. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1808. SEQ_PUT_HEX_FIELD_RET(s, entry->pid);
  1809. SEQ_PUT_HEX_FIELD_RET(s, iter->cpu);
  1810. SEQ_PUT_HEX_FIELD_RET(s, iter->ts);
  1811. }
  1812. event = ftrace_find_event(entry->type);
  1813. if (event) {
  1814. enum print_line_t ret = event->funcs->hex(iter, 0, event);
  1815. if (ret != TRACE_TYPE_HANDLED)
  1816. return ret;
  1817. }
  1818. SEQ_PUT_FIELD_RET(s, newline);
  1819. return TRACE_TYPE_HANDLED;
  1820. }
  1821. static enum print_line_t print_bin_fmt(struct trace_iterator *iter)
  1822. {
  1823. struct trace_seq *s = &iter->seq;
  1824. struct trace_entry *entry;
  1825. struct trace_event *event;
  1826. entry = iter->ent;
  1827. if (trace_flags & TRACE_ITER_CONTEXT_INFO) {
  1828. SEQ_PUT_FIELD_RET(s, entry->pid);
  1829. SEQ_PUT_FIELD_RET(s, iter->cpu);
  1830. SEQ_PUT_FIELD_RET(s, iter->ts);
  1831. }
  1832. event = ftrace_find_event(entry->type);
  1833. return event ? event->funcs->binary(iter, 0, event) :
  1834. TRACE_TYPE_HANDLED;
  1835. }
  1836. int trace_empty(struct trace_iterator *iter)
  1837. {
  1838. int cpu;
  1839. /* If we are looking at one CPU buffer, only check that one */
  1840. if (iter->cpu_file != TRACE_PIPE_ALL_CPU) {
  1841. cpu = iter->cpu_file;
  1842. if (iter->buffer_iter[cpu]) {
  1843. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1844. return 0;
  1845. } else {
  1846. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1847. return 0;
  1848. }
  1849. return 1;
  1850. }
  1851. for_each_tracing_cpu(cpu) {
  1852. if (iter->buffer_iter[cpu]) {
  1853. if (!ring_buffer_iter_empty(iter->buffer_iter[cpu]))
  1854. return 0;
  1855. } else {
  1856. if (!ring_buffer_empty_cpu(iter->tr->buffer, cpu))
  1857. return 0;
  1858. }
  1859. }
  1860. return 1;
  1861. }
  1862. /* Called with trace_event_read_lock() held. */
  1863. enum print_line_t print_trace_line(struct trace_iterator *iter)
  1864. {
  1865. enum print_line_t ret;
  1866. if (iter->lost_events &&
  1867. !trace_seq_printf(&iter->seq, "CPU:%d [LOST %lu EVENTS]\n",
  1868. iter->cpu, iter->lost_events))
  1869. return TRACE_TYPE_PARTIAL_LINE;
  1870. if (iter->trace && iter->trace->print_line) {
  1871. ret = iter->trace->print_line(iter);
  1872. if (ret != TRACE_TYPE_UNHANDLED)
  1873. return ret;
  1874. }
  1875. if (iter->ent->type == TRACE_BPRINT &&
  1876. trace_flags & TRACE_ITER_PRINTK &&
  1877. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1878. return trace_print_bprintk_msg_only(iter);
  1879. if (iter->ent->type == TRACE_PRINT &&
  1880. trace_flags & TRACE_ITER_PRINTK &&
  1881. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1882. return trace_print_printk_msg_only(iter);
  1883. if (trace_flags & TRACE_ITER_BIN)
  1884. return print_bin_fmt(iter);
  1885. if (trace_flags & TRACE_ITER_HEX)
  1886. return print_hex_fmt(iter);
  1887. if (trace_flags & TRACE_ITER_RAW)
  1888. return print_raw_fmt(iter);
  1889. return print_trace_fmt(iter);
  1890. }
  1891. void trace_latency_header(struct seq_file *m)
  1892. {
  1893. struct trace_iterator *iter = m->private;
  1894. /* print nothing if the buffers are empty */
  1895. if (trace_empty(iter))
  1896. return;
  1897. if (iter->iter_flags & TRACE_FILE_LAT_FMT)
  1898. print_trace_header(m, iter);
  1899. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1900. print_lat_help_header(m);
  1901. }
  1902. void trace_default_header(struct seq_file *m)
  1903. {
  1904. struct trace_iterator *iter = m->private;
  1905. if (!(trace_flags & TRACE_ITER_CONTEXT_INFO))
  1906. return;
  1907. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1908. /* print nothing if the buffers are empty */
  1909. if (trace_empty(iter))
  1910. return;
  1911. print_trace_header(m, iter);
  1912. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1913. print_lat_help_header(m);
  1914. } else {
  1915. if (!(trace_flags & TRACE_ITER_VERBOSE)) {
  1916. if (trace_flags & TRACE_ITER_IRQ_INFO)
  1917. if (trace_flags & TRACE_ITER_TGID)
  1918. print_func_help_header_irq_tgid(iter->tr, m);
  1919. else
  1920. print_func_help_header_irq(iter->tr, m);
  1921. else
  1922. if (trace_flags & TRACE_ITER_TGID)
  1923. print_func_help_header_tgid(iter->tr, m);
  1924. else
  1925. print_func_help_header(iter->tr, m);
  1926. }
  1927. }
  1928. }
  1929. static void test_ftrace_alive(struct seq_file *m)
  1930. {
  1931. if (!ftrace_is_dead())
  1932. return;
  1933. seq_printf(m, "# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  1934. seq_printf(m, "# MAY BE MISSING FUNCTION EVENTS\n");
  1935. }
  1936. static int s_show(struct seq_file *m, void *v)
  1937. {
  1938. struct trace_iterator *iter = v;
  1939. int ret;
  1940. if (iter->ent == NULL) {
  1941. if (iter->tr) {
  1942. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1943. seq_puts(m, "#\n");
  1944. test_ftrace_alive(m);
  1945. }
  1946. if (iter->trace && iter->trace->print_header)
  1947. iter->trace->print_header(m);
  1948. else
  1949. trace_default_header(m);
  1950. } else if (iter->leftover) {
  1951. /*
  1952. * If we filled the seq_file buffer earlier, we
  1953. * want to just show it now.
  1954. */
  1955. ret = trace_print_seq(m, &iter->seq);
  1956. /* ret should this time be zero, but you never know */
  1957. iter->leftover = ret;
  1958. } else {
  1959. print_trace_line(iter);
  1960. ret = trace_print_seq(m, &iter->seq);
  1961. /*
  1962. * If we overflow the seq_file buffer, then it will
  1963. * ask us for this data again at start up.
  1964. * Use that instead.
  1965. * ret is 0 if seq_file write succeeded.
  1966. * -1 otherwise.
  1967. */
  1968. iter->leftover = ret;
  1969. }
  1970. return 0;
  1971. }
  1972. static const struct seq_operations tracer_seq_ops = {
  1973. .start = s_start,
  1974. .next = s_next,
  1975. .stop = s_stop,
  1976. .show = s_show,
  1977. };
  1978. static struct trace_iterator *
  1979. __tracing_open(struct inode *inode, struct file *file)
  1980. {
  1981. long cpu_file = (long) inode->i_private;
  1982. void *fail_ret = ERR_PTR(-ENOMEM);
  1983. struct trace_iterator *iter;
  1984. struct seq_file *m;
  1985. int cpu, ret;
  1986. if (tracing_disabled)
  1987. return ERR_PTR(-ENODEV);
  1988. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1989. if (!iter)
  1990. return ERR_PTR(-ENOMEM);
  1991. /*
  1992. * We make a copy of the current tracer to avoid concurrent
  1993. * changes on it while we are reading.
  1994. */
  1995. mutex_lock(&trace_types_lock);
  1996. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  1997. if (!iter->trace)
  1998. goto fail;
  1999. if (current_trace)
  2000. *iter->trace = *current_trace;
  2001. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  2002. goto fail;
  2003. if (current_trace && current_trace->print_max)
  2004. iter->tr = &max_tr;
  2005. else
  2006. iter->tr = &global_trace;
  2007. iter->pos = -1;
  2008. mutex_init(&iter->mutex);
  2009. iter->cpu_file = cpu_file;
  2010. /* Notify the tracer early; before we stop tracing. */
  2011. if (iter->trace && iter->trace->open)
  2012. iter->trace->open(iter);
  2013. /* Annotate start of buffers if we had overruns */
  2014. if (ring_buffer_overruns(iter->tr->buffer))
  2015. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  2016. /* stop the trace while dumping */
  2017. tracing_stop();
  2018. if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
  2019. for_each_tracing_cpu(cpu) {
  2020. iter->buffer_iter[cpu] =
  2021. ring_buffer_read_prepare(iter->tr->buffer, cpu);
  2022. }
  2023. ring_buffer_read_prepare_sync();
  2024. for_each_tracing_cpu(cpu) {
  2025. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2026. tracing_iter_reset(iter, cpu);
  2027. }
  2028. } else {
  2029. cpu = iter->cpu_file;
  2030. iter->buffer_iter[cpu] =
  2031. ring_buffer_read_prepare(iter->tr->buffer, cpu);
  2032. ring_buffer_read_prepare_sync();
  2033. ring_buffer_read_start(iter->buffer_iter[cpu]);
  2034. tracing_iter_reset(iter, cpu);
  2035. }
  2036. ret = seq_open(file, &tracer_seq_ops);
  2037. if (ret < 0) {
  2038. fail_ret = ERR_PTR(ret);
  2039. goto fail_buffer;
  2040. }
  2041. m = file->private_data;
  2042. m->private = iter;
  2043. mutex_unlock(&trace_types_lock);
  2044. return iter;
  2045. fail_buffer:
  2046. for_each_tracing_cpu(cpu) {
  2047. if (iter->buffer_iter[cpu])
  2048. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2049. }
  2050. free_cpumask_var(iter->started);
  2051. tracing_start();
  2052. fail:
  2053. mutex_unlock(&trace_types_lock);
  2054. kfree(iter->trace);
  2055. kfree(iter);
  2056. return fail_ret;
  2057. }
  2058. int tracing_open_generic(struct inode *inode, struct file *filp)
  2059. {
  2060. if (tracing_disabled)
  2061. return -ENODEV;
  2062. filp->private_data = inode->i_private;
  2063. return 0;
  2064. }
  2065. static int tracing_release(struct inode *inode, struct file *file)
  2066. {
  2067. struct seq_file *m = file->private_data;
  2068. struct trace_iterator *iter;
  2069. int cpu;
  2070. if (!(file->f_mode & FMODE_READ))
  2071. return 0;
  2072. iter = m->private;
  2073. mutex_lock(&trace_types_lock);
  2074. for_each_tracing_cpu(cpu) {
  2075. if (iter->buffer_iter[cpu])
  2076. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  2077. }
  2078. if (iter->trace && iter->trace->close)
  2079. iter->trace->close(iter);
  2080. /* reenable tracing if it was previously enabled */
  2081. tracing_start();
  2082. mutex_unlock(&trace_types_lock);
  2083. seq_release(inode, file);
  2084. mutex_destroy(&iter->mutex);
  2085. free_cpumask_var(iter->started);
  2086. kfree(iter->trace);
  2087. kfree(iter);
  2088. return 0;
  2089. }
  2090. static int tracing_open(struct inode *inode, struct file *file)
  2091. {
  2092. struct trace_iterator *iter;
  2093. int ret = 0;
  2094. /* If this file was open for write, then erase contents */
  2095. if ((file->f_mode & FMODE_WRITE) &&
  2096. (file->f_flags & O_TRUNC)) {
  2097. long cpu = (long) inode->i_private;
  2098. if (cpu == TRACE_PIPE_ALL_CPU)
  2099. tracing_reset_online_cpus(&global_trace);
  2100. else
  2101. tracing_reset(&global_trace, cpu);
  2102. }
  2103. if (file->f_mode & FMODE_READ) {
  2104. iter = __tracing_open(inode, file);
  2105. if (IS_ERR(iter))
  2106. ret = PTR_ERR(iter);
  2107. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2108. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2109. }
  2110. return ret;
  2111. }
  2112. static void *
  2113. t_next(struct seq_file *m, void *v, loff_t *pos)
  2114. {
  2115. struct tracer *t = v;
  2116. (*pos)++;
  2117. if (t)
  2118. t = t->next;
  2119. return t;
  2120. }
  2121. static void *t_start(struct seq_file *m, loff_t *pos)
  2122. {
  2123. struct tracer *t;
  2124. loff_t l = 0;
  2125. mutex_lock(&trace_types_lock);
  2126. for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
  2127. ;
  2128. return t;
  2129. }
  2130. static void t_stop(struct seq_file *m, void *p)
  2131. {
  2132. mutex_unlock(&trace_types_lock);
  2133. }
  2134. static int t_show(struct seq_file *m, void *v)
  2135. {
  2136. struct tracer *t = v;
  2137. if (!t)
  2138. return 0;
  2139. seq_printf(m, "%s", t->name);
  2140. if (t->next)
  2141. seq_putc(m, ' ');
  2142. else
  2143. seq_putc(m, '\n');
  2144. return 0;
  2145. }
  2146. static const struct seq_operations show_traces_seq_ops = {
  2147. .start = t_start,
  2148. .next = t_next,
  2149. .stop = t_stop,
  2150. .show = t_show,
  2151. };
  2152. static int show_traces_open(struct inode *inode, struct file *file)
  2153. {
  2154. if (tracing_disabled)
  2155. return -ENODEV;
  2156. return seq_open(file, &show_traces_seq_ops);
  2157. }
  2158. static ssize_t
  2159. tracing_write_stub(struct file *filp, const char __user *ubuf,
  2160. size_t count, loff_t *ppos)
  2161. {
  2162. return count;
  2163. }
  2164. static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
  2165. {
  2166. if (file->f_mode & FMODE_READ)
  2167. return seq_lseek(file, offset, origin);
  2168. else
  2169. return 0;
  2170. }
  2171. static const struct file_operations tracing_fops = {
  2172. .open = tracing_open,
  2173. .read = seq_read,
  2174. .write = tracing_write_stub,
  2175. .llseek = tracing_seek,
  2176. .release = tracing_release,
  2177. };
  2178. static const struct file_operations show_traces_fops = {
  2179. .open = show_traces_open,
  2180. .read = seq_read,
  2181. .release = seq_release,
  2182. .llseek = seq_lseek,
  2183. };
  2184. /*
  2185. * Only trace on a CPU if the bitmask is set:
  2186. */
  2187. static cpumask_var_t tracing_cpumask;
  2188. /*
  2189. * The tracer itself will not take this lock, but still we want
  2190. * to provide a consistent cpumask to user-space:
  2191. */
  2192. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  2193. /*
  2194. * Temporary storage for the character representation of the
  2195. * CPU bitmask (and one more byte for the newline):
  2196. */
  2197. static char mask_str[NR_CPUS + 1];
  2198. static ssize_t
  2199. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  2200. size_t count, loff_t *ppos)
  2201. {
  2202. int len;
  2203. mutex_lock(&tracing_cpumask_update_lock);
  2204. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  2205. if (count - len < 2) {
  2206. count = -EINVAL;
  2207. goto out_err;
  2208. }
  2209. len += sprintf(mask_str + len, "\n");
  2210. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  2211. out_err:
  2212. mutex_unlock(&tracing_cpumask_update_lock);
  2213. return count;
  2214. }
  2215. static ssize_t
  2216. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  2217. size_t count, loff_t *ppos)
  2218. {
  2219. int err, cpu;
  2220. cpumask_var_t tracing_cpumask_new;
  2221. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  2222. return -ENOMEM;
  2223. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  2224. if (err)
  2225. goto err_unlock;
  2226. mutex_lock(&tracing_cpumask_update_lock);
  2227. local_irq_disable();
  2228. arch_spin_lock(&ftrace_max_lock);
  2229. for_each_tracing_cpu(cpu) {
  2230. /*
  2231. * Increase/decrease the disabled counter if we are
  2232. * about to flip a bit in the cpumask:
  2233. */
  2234. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  2235. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2236. atomic_inc(&global_trace.data[cpu]->disabled);
  2237. ring_buffer_record_disable_cpu(global_trace.buffer, cpu);
  2238. }
  2239. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  2240. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2241. atomic_dec(&global_trace.data[cpu]->disabled);
  2242. ring_buffer_record_enable_cpu(global_trace.buffer, cpu);
  2243. }
  2244. }
  2245. arch_spin_unlock(&ftrace_max_lock);
  2246. local_irq_enable();
  2247. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  2248. mutex_unlock(&tracing_cpumask_update_lock);
  2249. free_cpumask_var(tracing_cpumask_new);
  2250. return count;
  2251. err_unlock:
  2252. free_cpumask_var(tracing_cpumask_new);
  2253. return err;
  2254. }
  2255. static const struct file_operations tracing_cpumask_fops = {
  2256. .open = tracing_open_generic,
  2257. .read = tracing_cpumask_read,
  2258. .write = tracing_cpumask_write,
  2259. .llseek = generic_file_llseek,
  2260. };
  2261. static int tracing_trace_options_show(struct seq_file *m, void *v)
  2262. {
  2263. struct tracer_opt *trace_opts;
  2264. u32 tracer_flags;
  2265. int i;
  2266. mutex_lock(&trace_types_lock);
  2267. tracer_flags = current_trace->flags->val;
  2268. trace_opts = current_trace->flags->opts;
  2269. for (i = 0; trace_options[i]; i++) {
  2270. if (trace_flags & (1 << i))
  2271. seq_printf(m, "%s\n", trace_options[i]);
  2272. else
  2273. seq_printf(m, "no%s\n", trace_options[i]);
  2274. }
  2275. for (i = 0; trace_opts[i].name; i++) {
  2276. if (tracer_flags & trace_opts[i].bit)
  2277. seq_printf(m, "%s\n", trace_opts[i].name);
  2278. else
  2279. seq_printf(m, "no%s\n", trace_opts[i].name);
  2280. }
  2281. mutex_unlock(&trace_types_lock);
  2282. return 0;
  2283. }
  2284. static int __set_tracer_option(struct tracer *trace,
  2285. struct tracer_flags *tracer_flags,
  2286. struct tracer_opt *opts, int neg)
  2287. {
  2288. int ret;
  2289. ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
  2290. if (ret)
  2291. return ret;
  2292. if (neg)
  2293. tracer_flags->val &= ~opts->bit;
  2294. else
  2295. tracer_flags->val |= opts->bit;
  2296. return 0;
  2297. }
  2298. /* Try to assign a tracer specific option */
  2299. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2300. {
  2301. struct tracer_flags *tracer_flags = trace->flags;
  2302. struct tracer_opt *opts = NULL;
  2303. int i;
  2304. for (i = 0; tracer_flags->opts[i].name; i++) {
  2305. opts = &tracer_flags->opts[i];
  2306. if (strcmp(cmp, opts->name) == 0)
  2307. return __set_tracer_option(trace, trace->flags,
  2308. opts, neg);
  2309. }
  2310. return -EINVAL;
  2311. }
  2312. /* Some tracers require overwrite to stay enabled */
  2313. int trace_keep_overwrite(struct tracer *tracer, u32 mask, int set)
  2314. {
  2315. if (tracer->enabled && (mask & TRACE_ITER_OVERWRITE) && !set)
  2316. return -1;
  2317. return 0;
  2318. }
  2319. int set_tracer_flag(unsigned int mask, int enabled)
  2320. {
  2321. /* do nothing if flag is already set */
  2322. if (!!(trace_flags & mask) == !!enabled)
  2323. return 0;
  2324. /* Give the tracer a chance to approve the change */
  2325. if (current_trace->flag_changed)
  2326. if (current_trace->flag_changed(current_trace, mask, !!enabled))
  2327. return -EINVAL;
  2328. if (enabled)
  2329. trace_flags |= mask;
  2330. else
  2331. trace_flags &= ~mask;
  2332. if (mask == TRACE_ITER_RECORD_CMD)
  2333. trace_event_enable_cmd_record(enabled);
  2334. if (mask == TRACE_ITER_OVERWRITE) {
  2335. ring_buffer_change_overwrite(global_trace.buffer, enabled);
  2336. #ifdef CONFIG_TRACER_MAX_TRACE
  2337. ring_buffer_change_overwrite(max_tr.buffer, enabled);
  2338. #endif
  2339. }
  2340. return 0;
  2341. }
  2342. static ssize_t
  2343. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2344. size_t cnt, loff_t *ppos)
  2345. {
  2346. char buf[64];
  2347. char *cmp;
  2348. int neg = 0;
  2349. int ret = -ENODEV;
  2350. int i;
  2351. if (cnt >= sizeof(buf))
  2352. return -EINVAL;
  2353. if (copy_from_user(&buf, ubuf, cnt))
  2354. return -EFAULT;
  2355. buf[cnt] = 0;
  2356. cmp = strstrip(buf);
  2357. if (strncmp(cmp, "no", 2) == 0) {
  2358. neg = 1;
  2359. cmp += 2;
  2360. }
  2361. mutex_lock(&trace_types_lock);
  2362. for (i = 0; trace_options[i]; i++) {
  2363. if (strcmp(cmp, trace_options[i]) == 0) {
  2364. ret = set_tracer_flag(1 << i, !neg);
  2365. break;
  2366. }
  2367. }
  2368. /* If no option could be set, test the specific tracer options */
  2369. if (!trace_options[i])
  2370. ret = set_tracer_option(current_trace, cmp, neg);
  2371. mutex_unlock(&trace_types_lock);
  2372. if (ret < 0)
  2373. return ret;
  2374. *ppos += cnt;
  2375. return cnt;
  2376. }
  2377. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  2378. {
  2379. if (tracing_disabled)
  2380. return -ENODEV;
  2381. return single_open(file, tracing_trace_options_show, NULL);
  2382. }
  2383. static const struct file_operations tracing_iter_fops = {
  2384. .open = tracing_trace_options_open,
  2385. .read = seq_read,
  2386. .llseek = seq_lseek,
  2387. .release = single_release,
  2388. .write = tracing_trace_options_write,
  2389. };
  2390. static const char readme_msg[] =
  2391. "tracing mini-HOWTO:\n\n"
  2392. "# mount -t debugfs nodev /sys/kernel/debug\n\n"
  2393. "# cat /sys/kernel/debug/tracing/available_tracers\n"
  2394. "wakeup wakeup_rt preemptirqsoff preemptoff irqsoff function nop\n\n"
  2395. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2396. "nop\n"
  2397. "# echo wakeup > /sys/kernel/debug/tracing/current_tracer\n"
  2398. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2399. "wakeup\n"
  2400. "# cat /sys/kernel/debug/tracing/trace_options\n"
  2401. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2402. "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
  2403. "# echo 1 > /sys/kernel/debug/tracing/tracing_on\n"
  2404. "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
  2405. "# echo 0 > /sys/kernel/debug/tracing/tracing_on\n"
  2406. ;
  2407. static ssize_t
  2408. tracing_readme_read(struct file *filp, char __user *ubuf,
  2409. size_t cnt, loff_t *ppos)
  2410. {
  2411. return simple_read_from_buffer(ubuf, cnt, ppos,
  2412. readme_msg, strlen(readme_msg));
  2413. }
  2414. static const struct file_operations tracing_readme_fops = {
  2415. .open = tracing_open_generic,
  2416. .read = tracing_readme_read,
  2417. .llseek = generic_file_llseek,
  2418. };
  2419. static ssize_t
  2420. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2421. size_t cnt, loff_t *ppos)
  2422. {
  2423. char *buf_comm;
  2424. char *file_buf;
  2425. char *buf;
  2426. int len = 0;
  2427. int pid;
  2428. int i;
  2429. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2430. if (!file_buf)
  2431. return -ENOMEM;
  2432. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2433. if (!buf_comm) {
  2434. kfree(file_buf);
  2435. return -ENOMEM;
  2436. }
  2437. buf = file_buf;
  2438. for (i = 0; i < SAVED_CMDLINES; i++) {
  2439. int r;
  2440. pid = map_cmdline_to_pid[i];
  2441. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2442. continue;
  2443. trace_find_cmdline(pid, buf_comm);
  2444. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2445. buf += r;
  2446. len += r;
  2447. }
  2448. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2449. file_buf, len);
  2450. kfree(file_buf);
  2451. kfree(buf_comm);
  2452. return len;
  2453. }
  2454. static const struct file_operations tracing_saved_cmdlines_fops = {
  2455. .open = tracing_open_generic,
  2456. .read = tracing_saved_cmdlines_read,
  2457. .llseek = generic_file_llseek,
  2458. };
  2459. static ssize_t
  2460. tracing_saved_tgids_read(struct file *file, char __user *ubuf,
  2461. size_t cnt, loff_t *ppos)
  2462. {
  2463. char *file_buf;
  2464. char *buf;
  2465. int len = 0;
  2466. int pid;
  2467. int i;
  2468. file_buf = kmalloc(SAVED_CMDLINES*(16+1+16), GFP_KERNEL);
  2469. if (!file_buf)
  2470. return -ENOMEM;
  2471. buf = file_buf;
  2472. for (i = 0; i < SAVED_CMDLINES; i++) {
  2473. int tgid;
  2474. int r;
  2475. pid = map_cmdline_to_pid[i];
  2476. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2477. continue;
  2478. tgid = trace_find_tgid(pid);
  2479. r = sprintf(buf, "%d %d\n", pid, tgid);
  2480. buf += r;
  2481. len += r;
  2482. }
  2483. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2484. file_buf, len);
  2485. kfree(file_buf);
  2486. return len;
  2487. }
  2488. static const struct file_operations tracing_saved_tgids_fops = {
  2489. .open = tracing_open_generic,
  2490. .read = tracing_saved_tgids_read,
  2491. .llseek = generic_file_llseek,
  2492. };
  2493. static ssize_t
  2494. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2495. size_t cnt, loff_t *ppos)
  2496. {
  2497. char buf[64];
  2498. int r;
  2499. r = sprintf(buf, "%u\n", tracer_enabled);
  2500. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2501. }
  2502. static ssize_t
  2503. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2504. size_t cnt, loff_t *ppos)
  2505. {
  2506. struct trace_array *tr = filp->private_data;
  2507. unsigned long val;
  2508. int ret;
  2509. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  2510. if (ret)
  2511. return ret;
  2512. val = !!val;
  2513. mutex_lock(&trace_types_lock);
  2514. if (tracer_enabled ^ val) {
  2515. /* Only need to warn if this is used to change the state */
  2516. WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
  2517. if (val) {
  2518. tracer_enabled = 1;
  2519. if (current_trace->start)
  2520. current_trace->start(tr);
  2521. tracing_start();
  2522. } else {
  2523. tracer_enabled = 0;
  2524. tracing_stop();
  2525. if (current_trace->stop)
  2526. current_trace->stop(tr);
  2527. }
  2528. }
  2529. mutex_unlock(&trace_types_lock);
  2530. *ppos += cnt;
  2531. return cnt;
  2532. }
  2533. static ssize_t
  2534. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2535. size_t cnt, loff_t *ppos)
  2536. {
  2537. char buf[MAX_TRACER_SIZE+2];
  2538. int r;
  2539. mutex_lock(&trace_types_lock);
  2540. if (current_trace)
  2541. r = sprintf(buf, "%s\n", current_trace->name);
  2542. else
  2543. r = sprintf(buf, "\n");
  2544. mutex_unlock(&trace_types_lock);
  2545. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2546. }
  2547. int tracer_init(struct tracer *t, struct trace_array *tr)
  2548. {
  2549. tracing_reset_online_cpus(tr);
  2550. return t->init(tr);
  2551. }
  2552. static int __tracing_resize_ring_buffer(unsigned long size)
  2553. {
  2554. int ret;
  2555. /*
  2556. * If kernel or user changes the size of the ring buffer
  2557. * we use the size that was given, and we can forget about
  2558. * expanding it later.
  2559. */
  2560. ring_buffer_expanded = 1;
  2561. ret = ring_buffer_resize(global_trace.buffer, size);
  2562. if (ret < 0)
  2563. return ret;
  2564. if (!current_trace->use_max_tr)
  2565. goto out;
  2566. ret = ring_buffer_resize(max_tr.buffer, size);
  2567. if (ret < 0) {
  2568. int r;
  2569. r = ring_buffer_resize(global_trace.buffer,
  2570. global_trace.entries);
  2571. if (r < 0) {
  2572. /*
  2573. * AARGH! We are left with different
  2574. * size max buffer!!!!
  2575. * The max buffer is our "snapshot" buffer.
  2576. * When a tracer needs a snapshot (one of the
  2577. * latency tracers), it swaps the max buffer
  2578. * with the saved snap shot. We succeeded to
  2579. * update the size of the main buffer, but failed to
  2580. * update the size of the max buffer. But when we tried
  2581. * to reset the main buffer to the original size, we
  2582. * failed there too. This is very unlikely to
  2583. * happen, but if it does, warn and kill all
  2584. * tracing.
  2585. */
  2586. WARN_ON(1);
  2587. tracing_disabled = 1;
  2588. }
  2589. return ret;
  2590. }
  2591. max_tr.entries = size;
  2592. out:
  2593. global_trace.entries = size;
  2594. return ret;
  2595. }
  2596. static ssize_t tracing_resize_ring_buffer(unsigned long size)
  2597. {
  2598. int cpu, ret = size;
  2599. mutex_lock(&trace_types_lock);
  2600. tracing_stop();
  2601. /* disable all cpu buffers */
  2602. for_each_tracing_cpu(cpu) {
  2603. if (global_trace.data[cpu])
  2604. atomic_inc(&global_trace.data[cpu]->disabled);
  2605. if (max_tr.data[cpu])
  2606. atomic_inc(&max_tr.data[cpu]->disabled);
  2607. }
  2608. if (size != global_trace.entries)
  2609. ret = __tracing_resize_ring_buffer(size);
  2610. if (ret < 0)
  2611. ret = -ENOMEM;
  2612. for_each_tracing_cpu(cpu) {
  2613. if (global_trace.data[cpu])
  2614. atomic_dec(&global_trace.data[cpu]->disabled);
  2615. if (max_tr.data[cpu])
  2616. atomic_dec(&max_tr.data[cpu]->disabled);
  2617. }
  2618. tracing_start();
  2619. mutex_unlock(&trace_types_lock);
  2620. return ret;
  2621. }
  2622. /**
  2623. * tracing_update_buffers - used by tracing facility to expand ring buffers
  2624. *
  2625. * To save on memory when the tracing is never used on a system with it
  2626. * configured in. The ring buffers are set to a minimum size. But once
  2627. * a user starts to use the tracing facility, then they need to grow
  2628. * to their default size.
  2629. *
  2630. * This function is to be called when a tracer is about to be used.
  2631. */
  2632. int tracing_update_buffers(void)
  2633. {
  2634. int ret = 0;
  2635. mutex_lock(&trace_types_lock);
  2636. if (!ring_buffer_expanded)
  2637. ret = __tracing_resize_ring_buffer(trace_buf_size);
  2638. mutex_unlock(&trace_types_lock);
  2639. return ret;
  2640. }
  2641. struct trace_option_dentry;
  2642. static struct trace_option_dentry *
  2643. create_trace_option_files(struct tracer *tracer);
  2644. static void
  2645. destroy_trace_option_files(struct trace_option_dentry *topts);
  2646. static int tracing_set_tracer(const char *buf)
  2647. {
  2648. static struct trace_option_dentry *topts;
  2649. struct trace_array *tr = &global_trace;
  2650. struct tracer *t;
  2651. int ret = 0;
  2652. mutex_lock(&trace_types_lock);
  2653. if (!ring_buffer_expanded) {
  2654. ret = __tracing_resize_ring_buffer(trace_buf_size);
  2655. if (ret < 0)
  2656. goto out;
  2657. ret = 0;
  2658. }
  2659. for (t = trace_types; t; t = t->next) {
  2660. if (strcmp(t->name, buf) == 0)
  2661. break;
  2662. }
  2663. if (!t) {
  2664. ret = -EINVAL;
  2665. goto out;
  2666. }
  2667. if (t == current_trace)
  2668. goto out;
  2669. trace_branch_disable();
  2670. current_trace->enabled = false;
  2671. if (current_trace && current_trace->reset)
  2672. current_trace->reset(tr);
  2673. if (current_trace && current_trace->use_max_tr) {
  2674. /*
  2675. * We don't free the ring buffer. instead, resize it because
  2676. * The max_tr ring buffer has some state (e.g. ring->clock) and
  2677. * we want preserve it.
  2678. */
  2679. ring_buffer_resize(max_tr.buffer, 1);
  2680. max_tr.entries = 1;
  2681. }
  2682. destroy_trace_option_files(topts);
  2683. current_trace = t;
  2684. topts = create_trace_option_files(current_trace);
  2685. if (current_trace->use_max_tr) {
  2686. ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
  2687. if (ret < 0)
  2688. goto out;
  2689. max_tr.entries = global_trace.entries;
  2690. }
  2691. if (t->init) {
  2692. ret = tracer_init(t, tr);
  2693. if (ret)
  2694. goto out;
  2695. }
  2696. current_trace->enabled = true;
  2697. trace_branch_enable(tr);
  2698. out:
  2699. mutex_unlock(&trace_types_lock);
  2700. return ret;
  2701. }
  2702. static ssize_t
  2703. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2704. size_t cnt, loff_t *ppos)
  2705. {
  2706. char buf[MAX_TRACER_SIZE+1];
  2707. int i;
  2708. size_t ret;
  2709. int err;
  2710. ret = cnt;
  2711. if (cnt > MAX_TRACER_SIZE)
  2712. cnt = MAX_TRACER_SIZE;
  2713. if (copy_from_user(&buf, ubuf, cnt))
  2714. return -EFAULT;
  2715. buf[cnt] = 0;
  2716. /* strip ending whitespace. */
  2717. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2718. buf[i] = 0;
  2719. err = tracing_set_tracer(buf);
  2720. if (err)
  2721. return err;
  2722. *ppos += ret;
  2723. return ret;
  2724. }
  2725. static ssize_t
  2726. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2727. size_t cnt, loff_t *ppos)
  2728. {
  2729. unsigned long *ptr = filp->private_data;
  2730. char buf[64];
  2731. int r;
  2732. r = snprintf(buf, sizeof(buf), "%ld\n",
  2733. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2734. if (r > sizeof(buf))
  2735. r = sizeof(buf);
  2736. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2737. }
  2738. static ssize_t
  2739. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2740. size_t cnt, loff_t *ppos)
  2741. {
  2742. unsigned long *ptr = filp->private_data;
  2743. unsigned long val;
  2744. int ret;
  2745. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  2746. if (ret)
  2747. return ret;
  2748. *ptr = val * 1000;
  2749. return cnt;
  2750. }
  2751. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2752. {
  2753. long cpu_file = (long) inode->i_private;
  2754. struct trace_iterator *iter;
  2755. int ret = 0;
  2756. if (tracing_disabled)
  2757. return -ENODEV;
  2758. mutex_lock(&trace_types_lock);
  2759. /* create a buffer to store the information to pass to userspace */
  2760. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2761. if (!iter) {
  2762. ret = -ENOMEM;
  2763. goto out;
  2764. }
  2765. /*
  2766. * We make a copy of the current tracer to avoid concurrent
  2767. * changes on it while we are reading.
  2768. */
  2769. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  2770. if (!iter->trace) {
  2771. ret = -ENOMEM;
  2772. goto fail;
  2773. }
  2774. if (current_trace)
  2775. *iter->trace = *current_trace;
  2776. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  2777. ret = -ENOMEM;
  2778. goto fail;
  2779. }
  2780. /* trace pipe does not show start of buffer */
  2781. cpumask_setall(iter->started);
  2782. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2783. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2784. iter->cpu_file = cpu_file;
  2785. iter->tr = &global_trace;
  2786. mutex_init(&iter->mutex);
  2787. filp->private_data = iter;
  2788. if (iter->trace->pipe_open)
  2789. iter->trace->pipe_open(iter);
  2790. nonseekable_open(inode, filp);
  2791. out:
  2792. mutex_unlock(&trace_types_lock);
  2793. return ret;
  2794. fail:
  2795. kfree(iter->trace);
  2796. kfree(iter);
  2797. mutex_unlock(&trace_types_lock);
  2798. return ret;
  2799. }
  2800. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2801. {
  2802. struct trace_iterator *iter = file->private_data;
  2803. mutex_lock(&trace_types_lock);
  2804. if (iter->trace->pipe_close)
  2805. iter->trace->pipe_close(iter);
  2806. mutex_unlock(&trace_types_lock);
  2807. free_cpumask_var(iter->started);
  2808. mutex_destroy(&iter->mutex);
  2809. kfree(iter->trace);
  2810. kfree(iter);
  2811. return 0;
  2812. }
  2813. static unsigned int
  2814. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2815. {
  2816. struct trace_iterator *iter = filp->private_data;
  2817. if (trace_flags & TRACE_ITER_BLOCK) {
  2818. /*
  2819. * Always select as readable when in blocking mode
  2820. */
  2821. return POLLIN | POLLRDNORM;
  2822. } else {
  2823. if (!trace_empty(iter))
  2824. return POLLIN | POLLRDNORM;
  2825. poll_wait(filp, &trace_wait, poll_table);
  2826. if (!trace_empty(iter))
  2827. return POLLIN | POLLRDNORM;
  2828. return 0;
  2829. }
  2830. }
  2831. void default_wait_pipe(struct trace_iterator *iter)
  2832. {
  2833. DEFINE_WAIT(wait);
  2834. prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
  2835. if (trace_empty(iter))
  2836. schedule();
  2837. finish_wait(&trace_wait, &wait);
  2838. }
  2839. /*
  2840. * This is a make-shift waitqueue.
  2841. * A tracer might use this callback on some rare cases:
  2842. *
  2843. * 1) the current tracer might hold the runqueue lock when it wakes up
  2844. * a reader, hence a deadlock (sched, function, and function graph tracers)
  2845. * 2) the function tracers, trace all functions, we don't want
  2846. * the overhead of calling wake_up and friends
  2847. * (and tracing them too)
  2848. *
  2849. * Anyway, this is really very primitive wakeup.
  2850. */
  2851. void poll_wait_pipe(struct trace_iterator *iter)
  2852. {
  2853. set_current_state(TASK_INTERRUPTIBLE);
  2854. /* sleep for 100 msecs, and try again. */
  2855. schedule_timeout(HZ / 10);
  2856. }
  2857. /* Must be called with trace_types_lock mutex held. */
  2858. static int tracing_wait_pipe(struct file *filp)
  2859. {
  2860. struct trace_iterator *iter = filp->private_data;
  2861. while (trace_empty(iter)) {
  2862. if ((filp->f_flags & O_NONBLOCK)) {
  2863. return -EAGAIN;
  2864. }
  2865. mutex_unlock(&iter->mutex);
  2866. iter->trace->wait_pipe(iter);
  2867. mutex_lock(&iter->mutex);
  2868. if (signal_pending(current))
  2869. return -EINTR;
  2870. /*
  2871. * We block until we read something and tracing is disabled.
  2872. * We still block if tracing is disabled, but we have never
  2873. * read anything. This allows a user to cat this file, and
  2874. * then enable tracing. But after we have read something,
  2875. * we give an EOF when tracing is again disabled.
  2876. *
  2877. * iter->pos will be 0 if we haven't read anything.
  2878. */
  2879. if (!tracer_enabled && iter->pos)
  2880. break;
  2881. }
  2882. return 1;
  2883. }
  2884. /*
  2885. * Consumer reader.
  2886. */
  2887. static ssize_t
  2888. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2889. size_t cnt, loff_t *ppos)
  2890. {
  2891. struct trace_iterator *iter = filp->private_data;
  2892. static struct tracer *old_tracer;
  2893. ssize_t sret;
  2894. /* return any leftover data */
  2895. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2896. if (sret != -EBUSY)
  2897. return sret;
  2898. trace_seq_init(&iter->seq);
  2899. /* copy the tracer to avoid using a global lock all around */
  2900. mutex_lock(&trace_types_lock);
  2901. if (unlikely(old_tracer != current_trace && current_trace)) {
  2902. old_tracer = current_trace;
  2903. *iter->trace = *current_trace;
  2904. }
  2905. mutex_unlock(&trace_types_lock);
  2906. /*
  2907. * Avoid more than one consumer on a single file descriptor
  2908. * This is just a matter of traces coherency, the ring buffer itself
  2909. * is protected.
  2910. */
  2911. mutex_lock(&iter->mutex);
  2912. if (iter->trace->read) {
  2913. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2914. if (sret)
  2915. goto out;
  2916. }
  2917. waitagain:
  2918. sret = tracing_wait_pipe(filp);
  2919. if (sret <= 0)
  2920. goto out;
  2921. /* stop when tracing is finished */
  2922. if (trace_empty(iter)) {
  2923. sret = 0;
  2924. goto out;
  2925. }
  2926. if (cnt >= PAGE_SIZE)
  2927. cnt = PAGE_SIZE - 1;
  2928. /* reset all but tr, trace, and overruns */
  2929. memset(&iter->seq, 0,
  2930. sizeof(struct trace_iterator) -
  2931. offsetof(struct trace_iterator, seq));
  2932. cpumask_clear(iter->started);
  2933. iter->pos = -1;
  2934. trace_event_read_lock();
  2935. trace_access_lock(iter->cpu_file);
  2936. while (trace_find_next_entry_inc(iter) != NULL) {
  2937. enum print_line_t ret;
  2938. int len = iter->seq.len;
  2939. ret = print_trace_line(iter);
  2940. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2941. /* don't print partial lines */
  2942. iter->seq.len = len;
  2943. break;
  2944. }
  2945. if (ret != TRACE_TYPE_NO_CONSUME)
  2946. trace_consume(iter);
  2947. if (iter->seq.len >= cnt)
  2948. break;
  2949. /*
  2950. * Setting the full flag means we reached the trace_seq buffer
  2951. * size and we should leave by partial output condition above.
  2952. * One of the trace_seq_* functions is not used properly.
  2953. */
  2954. WARN_ONCE(iter->seq.full, "full flag set for trace type %d",
  2955. iter->ent->type);
  2956. }
  2957. trace_access_unlock(iter->cpu_file);
  2958. trace_event_read_unlock();
  2959. /* Now copy what we have to the user */
  2960. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2961. if (iter->seq.readpos >= iter->seq.len)
  2962. trace_seq_init(&iter->seq);
  2963. /*
  2964. * If there was nothing to send to user, in spite of consuming trace
  2965. * entries, go back to wait for more entries.
  2966. */
  2967. if (sret == -EBUSY)
  2968. goto waitagain;
  2969. out:
  2970. mutex_unlock(&iter->mutex);
  2971. return sret;
  2972. }
  2973. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  2974. struct pipe_buffer *buf)
  2975. {
  2976. __free_page(buf->page);
  2977. }
  2978. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  2979. unsigned int idx)
  2980. {
  2981. __free_page(spd->pages[idx]);
  2982. }
  2983. static const struct pipe_buf_operations tracing_pipe_buf_ops = {
  2984. .can_merge = 0,
  2985. .map = generic_pipe_buf_map,
  2986. .unmap = generic_pipe_buf_unmap,
  2987. .confirm = generic_pipe_buf_confirm,
  2988. .release = tracing_pipe_buf_release,
  2989. .steal = generic_pipe_buf_steal,
  2990. .get = generic_pipe_buf_get,
  2991. };
  2992. static size_t
  2993. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  2994. {
  2995. size_t count;
  2996. int ret;
  2997. /* Seq buffer is page-sized, exactly what we need. */
  2998. for (;;) {
  2999. count = iter->seq.len;
  3000. ret = print_trace_line(iter);
  3001. count = iter->seq.len - count;
  3002. if (rem < count) {
  3003. rem = 0;
  3004. iter->seq.len -= count;
  3005. break;
  3006. }
  3007. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  3008. iter->seq.len -= count;
  3009. break;
  3010. }
  3011. if (ret != TRACE_TYPE_NO_CONSUME)
  3012. trace_consume(iter);
  3013. rem -= count;
  3014. if (!trace_find_next_entry_inc(iter)) {
  3015. rem = 0;
  3016. iter->ent = NULL;
  3017. break;
  3018. }
  3019. }
  3020. return rem;
  3021. }
  3022. static ssize_t tracing_splice_read_pipe(struct file *filp,
  3023. loff_t *ppos,
  3024. struct pipe_inode_info *pipe,
  3025. size_t len,
  3026. unsigned int flags)
  3027. {
  3028. struct page *pages_def[PIPE_DEF_BUFFERS];
  3029. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3030. struct trace_iterator *iter = filp->private_data;
  3031. struct splice_pipe_desc spd = {
  3032. .pages = pages_def,
  3033. .partial = partial_def,
  3034. .nr_pages = 0, /* This gets updated below. */
  3035. .nr_pages_max = PIPE_DEF_BUFFERS,
  3036. .flags = flags,
  3037. .ops = &tracing_pipe_buf_ops,
  3038. .spd_release = tracing_spd_release_pipe,
  3039. };
  3040. static struct tracer *old_tracer;
  3041. ssize_t ret;
  3042. size_t rem;
  3043. unsigned int i;
  3044. if (splice_grow_spd(pipe, &spd))
  3045. return -ENOMEM;
  3046. /* copy the tracer to avoid using a global lock all around */
  3047. mutex_lock(&trace_types_lock);
  3048. if (unlikely(old_tracer != current_trace && current_trace)) {
  3049. old_tracer = current_trace;
  3050. *iter->trace = *current_trace;
  3051. }
  3052. mutex_unlock(&trace_types_lock);
  3053. mutex_lock(&iter->mutex);
  3054. if (iter->trace->splice_read) {
  3055. ret = iter->trace->splice_read(iter, filp,
  3056. ppos, pipe, len, flags);
  3057. if (ret)
  3058. goto out_err;
  3059. }
  3060. ret = tracing_wait_pipe(filp);
  3061. if (ret <= 0)
  3062. goto out_err;
  3063. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  3064. ret = -EFAULT;
  3065. goto out_err;
  3066. }
  3067. trace_event_read_lock();
  3068. trace_access_lock(iter->cpu_file);
  3069. /* Fill as many pages as possible. */
  3070. for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
  3071. spd.pages[i] = alloc_page(GFP_KERNEL);
  3072. if (!spd.pages[i])
  3073. break;
  3074. rem = tracing_fill_pipe_page(rem, iter);
  3075. /* Copy the data into the page, so we can start over. */
  3076. ret = trace_seq_to_buffer(&iter->seq,
  3077. page_address(spd.pages[i]),
  3078. iter->seq.len);
  3079. if (ret < 0) {
  3080. __free_page(spd.pages[i]);
  3081. break;
  3082. }
  3083. spd.partial[i].offset = 0;
  3084. spd.partial[i].len = iter->seq.len;
  3085. trace_seq_init(&iter->seq);
  3086. }
  3087. trace_access_unlock(iter->cpu_file);
  3088. trace_event_read_unlock();
  3089. mutex_unlock(&iter->mutex);
  3090. spd.nr_pages = i;
  3091. ret = splice_to_pipe(pipe, &spd);
  3092. out:
  3093. splice_shrink_spd(&spd);
  3094. return ret;
  3095. out_err:
  3096. mutex_unlock(&iter->mutex);
  3097. goto out;
  3098. }
  3099. static ssize_t
  3100. tracing_entries_read(struct file *filp, char __user *ubuf,
  3101. size_t cnt, loff_t *ppos)
  3102. {
  3103. struct trace_array *tr = filp->private_data;
  3104. char buf[96];
  3105. int r;
  3106. mutex_lock(&trace_types_lock);
  3107. if (!ring_buffer_expanded)
  3108. r = sprintf(buf, "%lu (expanded: %lu)\n",
  3109. tr->entries >> 10,
  3110. trace_buf_size >> 10);
  3111. else
  3112. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  3113. mutex_unlock(&trace_types_lock);
  3114. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3115. }
  3116. static ssize_t
  3117. tracing_entries_write(struct file *filp, const char __user *ubuf,
  3118. size_t cnt, loff_t *ppos)
  3119. {
  3120. unsigned long val;
  3121. int ret;
  3122. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3123. if (ret)
  3124. return ret;
  3125. /* must have at least 1 entry */
  3126. if (!val)
  3127. return -EINVAL;
  3128. /* value is in KB */
  3129. val <<= 10;
  3130. ret = tracing_resize_ring_buffer(val);
  3131. if (ret < 0)
  3132. return ret;
  3133. *ppos += cnt;
  3134. return cnt;
  3135. }
  3136. static ssize_t
  3137. tracing_total_entries_read(struct file *filp, char __user *ubuf,
  3138. size_t cnt, loff_t *ppos)
  3139. {
  3140. struct trace_array *tr = filp->private_data;
  3141. char buf[64];
  3142. int r, cpu;
  3143. unsigned long size = 0, expanded_size = 0;
  3144. mutex_lock(&trace_types_lock);
  3145. for_each_tracing_cpu(cpu) {
  3146. size += tr->entries >> 10;
  3147. if (!ring_buffer_expanded)
  3148. expanded_size += trace_buf_size >> 10;
  3149. }
  3150. if (ring_buffer_expanded)
  3151. r = sprintf(buf, "%lu\n", size);
  3152. else
  3153. r = sprintf(buf, "%lu (expanded: %lu)\n", size, expanded_size);
  3154. mutex_unlock(&trace_types_lock);
  3155. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3156. }
  3157. static ssize_t
  3158. tracing_free_buffer_write(struct file *filp, const char __user *ubuf,
  3159. size_t cnt, loff_t *ppos)
  3160. {
  3161. /*
  3162. * There is no need to read what the user has written, this function
  3163. * is just to make sure that there is no error when "echo" is used
  3164. */
  3165. *ppos += cnt;
  3166. return cnt;
  3167. }
  3168. static int
  3169. tracing_free_buffer_release(struct inode *inode, struct file *filp)
  3170. {
  3171. /* disable tracing ? */
  3172. if (trace_flags & TRACE_ITER_STOP_ON_FREE)
  3173. tracing_off();
  3174. /* resize the ring buffer to 0 */
  3175. tracing_resize_ring_buffer(0);
  3176. return 0;
  3177. }
  3178. static ssize_t
  3179. tracing_mark_write(struct file *filp, const char __user *ubuf,
  3180. size_t cnt, loff_t *fpos)
  3181. {
  3182. unsigned long addr = (unsigned long)ubuf;
  3183. struct ring_buffer_event *event;
  3184. struct ring_buffer *buffer;
  3185. struct print_entry *entry;
  3186. unsigned long irq_flags;
  3187. struct page *pages[2];
  3188. int nr_pages = 1;
  3189. ssize_t written;
  3190. void *page1;
  3191. void *page2 = NULL;
  3192. int offset;
  3193. int size;
  3194. int len;
  3195. int ret;
  3196. if (tracing_disabled)
  3197. return -EINVAL;
  3198. if (cnt > TRACE_BUF_SIZE)
  3199. cnt = TRACE_BUF_SIZE;
  3200. /*
  3201. * Userspace is injecting traces into the kernel trace buffer.
  3202. * We want to be as non intrusive as possible.
  3203. * To do so, we do not want to allocate any special buffers
  3204. * or take any locks, but instead write the userspace data
  3205. * straight into the ring buffer.
  3206. *
  3207. * First we need to pin the userspace buffer into memory,
  3208. * which, most likely it is, because it just referenced it.
  3209. * But there's no guarantee that it is. By using get_user_pages_fast()
  3210. * and kmap_atomic/kunmap_atomic() we can get access to the
  3211. * pages directly. We then write the data directly into the
  3212. * ring buffer.
  3213. */
  3214. BUILD_BUG_ON(TRACE_BUF_SIZE >= PAGE_SIZE);
  3215. /* check if we cross pages */
  3216. if ((addr & PAGE_MASK) != ((addr + cnt) & PAGE_MASK))
  3217. nr_pages = 2;
  3218. offset = addr & (PAGE_SIZE - 1);
  3219. addr &= PAGE_MASK;
  3220. ret = get_user_pages_fast(addr, nr_pages, 0, pages);
  3221. if (ret < nr_pages) {
  3222. while (--ret >= 0)
  3223. put_page(pages[ret]);
  3224. written = -EFAULT;
  3225. goto out;
  3226. }
  3227. page1 = kmap_atomic(pages[0]);
  3228. if (nr_pages == 2)
  3229. page2 = kmap_atomic(pages[1]);
  3230. local_save_flags(irq_flags);
  3231. size = sizeof(*entry) + cnt + 2; /* possible \n added */
  3232. buffer = global_trace.buffer;
  3233. event = trace_buffer_lock_reserve(buffer, TRACE_PRINT, size,
  3234. irq_flags, preempt_count());
  3235. if (!event) {
  3236. /* Ring buffer disabled, return as if not open for write */
  3237. written = -EBADF;
  3238. goto out_unlock;
  3239. }
  3240. entry = ring_buffer_event_data(event);
  3241. entry->ip = _THIS_IP_;
  3242. if (nr_pages == 2) {
  3243. len = PAGE_SIZE - offset;
  3244. memcpy(&entry->buf, page1 + offset, len);
  3245. memcpy(&entry->buf[len], page2, cnt - len);
  3246. } else
  3247. memcpy(&entry->buf, page1 + offset, cnt);
  3248. if (entry->buf[cnt - 1] != '\n') {
  3249. entry->buf[cnt] = '\n';
  3250. entry->buf[cnt + 1] = '\0';
  3251. stm_log(OST_ENTITY_TRACE_MARKER, entry->buf, cnt + 2);
  3252. } else {
  3253. entry->buf[cnt] = '\0';
  3254. stm_log(OST_ENTITY_TRACE_MARKER, entry->buf, cnt + 1);
  3255. }
  3256. ring_buffer_unlock_commit(buffer, event);
  3257. written = cnt;
  3258. *fpos += written;
  3259. out_unlock:
  3260. if (nr_pages == 2)
  3261. kunmap_atomic(page2);
  3262. kunmap_atomic(page1);
  3263. while (nr_pages > 0)
  3264. put_page(pages[--nr_pages]);
  3265. out:
  3266. return written;
  3267. }
  3268. static int tracing_clock_show(struct seq_file *m, void *v)
  3269. {
  3270. int i;
  3271. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  3272. seq_printf(m,
  3273. "%s%s%s%s", i ? " " : "",
  3274. i == trace_clock_id ? "[" : "", trace_clocks[i].name,
  3275. i == trace_clock_id ? "]" : "");
  3276. seq_putc(m, '\n');
  3277. return 0;
  3278. }
  3279. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  3280. size_t cnt, loff_t *fpos)
  3281. {
  3282. char buf[64];
  3283. const char *clockstr;
  3284. int i;
  3285. if (cnt >= sizeof(buf))
  3286. return -EINVAL;
  3287. if (copy_from_user(&buf, ubuf, cnt))
  3288. return -EFAULT;
  3289. buf[cnt] = 0;
  3290. clockstr = strstrip(buf);
  3291. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  3292. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  3293. break;
  3294. }
  3295. if (i == ARRAY_SIZE(trace_clocks))
  3296. return -EINVAL;
  3297. trace_clock_id = i;
  3298. mutex_lock(&trace_types_lock);
  3299. ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
  3300. if (max_tr.buffer)
  3301. ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
  3302. mutex_unlock(&trace_types_lock);
  3303. *fpos += cnt;
  3304. return cnt;
  3305. }
  3306. static int tracing_clock_open(struct inode *inode, struct file *file)
  3307. {
  3308. if (tracing_disabled)
  3309. return -ENODEV;
  3310. return single_open(file, tracing_clock_show, NULL);
  3311. }
  3312. static const struct file_operations tracing_max_lat_fops = {
  3313. .open = tracing_open_generic,
  3314. .read = tracing_max_lat_read,
  3315. .write = tracing_max_lat_write,
  3316. .llseek = generic_file_llseek,
  3317. };
  3318. static const struct file_operations tracing_ctrl_fops = {
  3319. .open = tracing_open_generic,
  3320. .read = tracing_ctrl_read,
  3321. .write = tracing_ctrl_write,
  3322. .llseek = generic_file_llseek,
  3323. };
  3324. static const struct file_operations set_tracer_fops = {
  3325. .open = tracing_open_generic,
  3326. .read = tracing_set_trace_read,
  3327. .write = tracing_set_trace_write,
  3328. .llseek = generic_file_llseek,
  3329. };
  3330. static const struct file_operations tracing_pipe_fops = {
  3331. .open = tracing_open_pipe,
  3332. .poll = tracing_poll_pipe,
  3333. .read = tracing_read_pipe,
  3334. .splice_read = tracing_splice_read_pipe,
  3335. .release = tracing_release_pipe,
  3336. .llseek = no_llseek,
  3337. };
  3338. static const struct file_operations tracing_entries_fops = {
  3339. .open = tracing_open_generic,
  3340. .read = tracing_entries_read,
  3341. .write = tracing_entries_write,
  3342. .llseek = generic_file_llseek,
  3343. };
  3344. static const struct file_operations tracing_total_entries_fops = {
  3345. .open = tracing_open_generic,
  3346. .read = tracing_total_entries_read,
  3347. .llseek = generic_file_llseek,
  3348. };
  3349. static const struct file_operations tracing_free_buffer_fops = {
  3350. .write = tracing_free_buffer_write,
  3351. .release = tracing_free_buffer_release,
  3352. };
  3353. static const struct file_operations tracing_mark_fops = {
  3354. .open = tracing_open_generic,
  3355. .write = tracing_mark_write,
  3356. .llseek = generic_file_llseek,
  3357. };
  3358. static const struct file_operations trace_clock_fops = {
  3359. .open = tracing_clock_open,
  3360. .read = seq_read,
  3361. .llseek = seq_lseek,
  3362. .release = single_release,
  3363. .write = tracing_clock_write,
  3364. };
  3365. struct ftrace_buffer_info {
  3366. struct trace_array *tr;
  3367. void *spare;
  3368. int cpu;
  3369. unsigned int read;
  3370. };
  3371. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  3372. {
  3373. int cpu = (int)(long)inode->i_private;
  3374. struct ftrace_buffer_info *info;
  3375. if (tracing_disabled)
  3376. return -ENODEV;
  3377. info = kzalloc(sizeof(*info), GFP_KERNEL);
  3378. if (!info)
  3379. return -ENOMEM;
  3380. info->tr = &global_trace;
  3381. info->cpu = cpu;
  3382. info->spare = NULL;
  3383. /* Force reading ring buffer for first read */
  3384. info->read = (unsigned int)-1;
  3385. filp->private_data = info;
  3386. return nonseekable_open(inode, filp);
  3387. }
  3388. static ssize_t
  3389. tracing_buffers_read(struct file *filp, char __user *ubuf,
  3390. size_t count, loff_t *ppos)
  3391. {
  3392. struct ftrace_buffer_info *info = filp->private_data;
  3393. ssize_t ret;
  3394. size_t size;
  3395. if (!count)
  3396. return 0;
  3397. if (!info->spare)
  3398. info->spare = ring_buffer_alloc_read_page(info->tr->buffer, info->cpu);
  3399. if (!info->spare)
  3400. return -ENOMEM;
  3401. /* Do we have previous read data to read? */
  3402. if (info->read < PAGE_SIZE)
  3403. goto read;
  3404. trace_access_lock(info->cpu);
  3405. ret = ring_buffer_read_page(info->tr->buffer,
  3406. &info->spare,
  3407. count,
  3408. info->cpu, 0);
  3409. trace_access_unlock(info->cpu);
  3410. if (ret < 0)
  3411. return 0;
  3412. info->read = 0;
  3413. read:
  3414. size = PAGE_SIZE - info->read;
  3415. if (size > count)
  3416. size = count;
  3417. ret = copy_to_user(ubuf, info->spare + info->read, size);
  3418. if (ret == size)
  3419. return -EFAULT;
  3420. size -= ret;
  3421. *ppos += size;
  3422. info->read += size;
  3423. return size;
  3424. }
  3425. static int tracing_buffers_release(struct inode *inode, struct file *file)
  3426. {
  3427. struct ftrace_buffer_info *info = file->private_data;
  3428. if (info->spare)
  3429. ring_buffer_free_read_page(info->tr->buffer, info->spare);
  3430. kfree(info);
  3431. return 0;
  3432. }
  3433. struct buffer_ref {
  3434. struct ring_buffer *buffer;
  3435. void *page;
  3436. int ref;
  3437. };
  3438. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  3439. struct pipe_buffer *buf)
  3440. {
  3441. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3442. if (--ref->ref)
  3443. return;
  3444. ring_buffer_free_read_page(ref->buffer, ref->page);
  3445. kfree(ref);
  3446. buf->private = 0;
  3447. }
  3448. static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
  3449. struct pipe_buffer *buf)
  3450. {
  3451. return 1;
  3452. }
  3453. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  3454. struct pipe_buffer *buf)
  3455. {
  3456. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3457. ref->ref++;
  3458. }
  3459. /* Pipe buffer operations for a buffer. */
  3460. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  3461. .can_merge = 0,
  3462. .map = generic_pipe_buf_map,
  3463. .unmap = generic_pipe_buf_unmap,
  3464. .confirm = generic_pipe_buf_confirm,
  3465. .release = buffer_pipe_buf_release,
  3466. .steal = buffer_pipe_buf_steal,
  3467. .get = buffer_pipe_buf_get,
  3468. };
  3469. /*
  3470. * Callback from splice_to_pipe(), if we need to release some pages
  3471. * at the end of the spd in case we error'ed out in filling the pipe.
  3472. */
  3473. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  3474. {
  3475. struct buffer_ref *ref =
  3476. (struct buffer_ref *)spd->partial[i].private;
  3477. if (--ref->ref)
  3478. return;
  3479. ring_buffer_free_read_page(ref->buffer, ref->page);
  3480. kfree(ref);
  3481. spd->partial[i].private = 0;
  3482. }
  3483. static ssize_t
  3484. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3485. struct pipe_inode_info *pipe, size_t len,
  3486. unsigned int flags)
  3487. {
  3488. struct ftrace_buffer_info *info = file->private_data;
  3489. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3490. struct page *pages_def[PIPE_DEF_BUFFERS];
  3491. struct splice_pipe_desc spd = {
  3492. .pages = pages_def,
  3493. .partial = partial_def,
  3494. .nr_pages_max = PIPE_DEF_BUFFERS,
  3495. .flags = flags,
  3496. .ops = &buffer_pipe_buf_ops,
  3497. .spd_release = buffer_spd_release,
  3498. };
  3499. struct buffer_ref *ref;
  3500. int entries, size, i;
  3501. size_t ret;
  3502. if (splice_grow_spd(pipe, &spd))
  3503. return -ENOMEM;
  3504. if (*ppos & (PAGE_SIZE - 1)) {
  3505. WARN_ONCE(1, "Ftrace: previous read must page-align\n");
  3506. ret = -EINVAL;
  3507. goto out;
  3508. }
  3509. if (len & (PAGE_SIZE - 1)) {
  3510. WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
  3511. if (len < PAGE_SIZE) {
  3512. ret = -EINVAL;
  3513. goto out;
  3514. }
  3515. len &= PAGE_MASK;
  3516. }
  3517. trace_access_lock(info->cpu);
  3518. entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
  3519. for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
  3520. struct page *page;
  3521. int r;
  3522. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  3523. if (!ref)
  3524. break;
  3525. ref->ref = 1;
  3526. ref->buffer = info->tr->buffer;
  3527. ref->page = ring_buffer_alloc_read_page(ref->buffer, info->cpu);
  3528. if (!ref->page) {
  3529. kfree(ref);
  3530. break;
  3531. }
  3532. r = ring_buffer_read_page(ref->buffer, &ref->page,
  3533. len, info->cpu, 1);
  3534. if (r < 0) {
  3535. ring_buffer_free_read_page(ref->buffer, ref->page);
  3536. kfree(ref);
  3537. break;
  3538. }
  3539. /*
  3540. * zero out any left over data, this is going to
  3541. * user land.
  3542. */
  3543. size = ring_buffer_page_len(ref->page);
  3544. if (size < PAGE_SIZE)
  3545. memset(ref->page + size, 0, PAGE_SIZE - size);
  3546. page = virt_to_page(ref->page);
  3547. spd.pages[i] = page;
  3548. spd.partial[i].len = PAGE_SIZE;
  3549. spd.partial[i].offset = 0;
  3550. spd.partial[i].private = (unsigned long)ref;
  3551. spd.nr_pages++;
  3552. *ppos += PAGE_SIZE;
  3553. entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
  3554. }
  3555. trace_access_unlock(info->cpu);
  3556. spd.nr_pages = i;
  3557. /* did we read anything? */
  3558. if (!spd.nr_pages) {
  3559. if (flags & SPLICE_F_NONBLOCK)
  3560. ret = -EAGAIN;
  3561. else
  3562. ret = 0;
  3563. /* TODO: block */
  3564. goto out;
  3565. }
  3566. ret = splice_to_pipe(pipe, &spd);
  3567. splice_shrink_spd(&spd);
  3568. out:
  3569. return ret;
  3570. }
  3571. static const struct file_operations tracing_buffers_fops = {
  3572. .open = tracing_buffers_open,
  3573. .read = tracing_buffers_read,
  3574. .release = tracing_buffers_release,
  3575. .splice_read = tracing_buffers_splice_read,
  3576. .llseek = no_llseek,
  3577. };
  3578. static ssize_t
  3579. tracing_stats_read(struct file *filp, char __user *ubuf,
  3580. size_t count, loff_t *ppos)
  3581. {
  3582. unsigned long cpu = (unsigned long)filp->private_data;
  3583. struct trace_array *tr = &global_trace;
  3584. struct trace_seq *s;
  3585. unsigned long cnt;
  3586. unsigned long long t;
  3587. unsigned long usec_rem;
  3588. s = kmalloc(sizeof(*s), GFP_KERNEL);
  3589. if (!s)
  3590. return -ENOMEM;
  3591. trace_seq_init(s);
  3592. cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
  3593. trace_seq_printf(s, "entries: %ld\n", cnt);
  3594. cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
  3595. trace_seq_printf(s, "overrun: %ld\n", cnt);
  3596. cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
  3597. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  3598. cnt = ring_buffer_bytes_cpu(tr->buffer, cpu);
  3599. trace_seq_printf(s, "bytes: %ld\n", cnt);
  3600. t = ns2usecs(ring_buffer_oldest_event_ts(tr->buffer, cpu));
  3601. usec_rem = do_div(t, USEC_PER_SEC);
  3602. trace_seq_printf(s, "oldest event ts: %5llu.%06lu\n", t, usec_rem);
  3603. t = ns2usecs(ring_buffer_time_stamp(tr->buffer, cpu));
  3604. usec_rem = do_div(t, USEC_PER_SEC);
  3605. trace_seq_printf(s, "now ts: %5llu.%06lu\n", t, usec_rem);
  3606. count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  3607. kfree(s);
  3608. return count;
  3609. }
  3610. static const struct file_operations tracing_stats_fops = {
  3611. .open = tracing_open_generic,
  3612. .read = tracing_stats_read,
  3613. .llseek = generic_file_llseek,
  3614. };
  3615. #ifdef CONFIG_DYNAMIC_FTRACE
  3616. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  3617. {
  3618. return 0;
  3619. }
  3620. static ssize_t
  3621. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  3622. size_t cnt, loff_t *ppos)
  3623. {
  3624. static char ftrace_dyn_info_buffer[1024];
  3625. static DEFINE_MUTEX(dyn_info_mutex);
  3626. unsigned long *p = filp->private_data;
  3627. char *buf = ftrace_dyn_info_buffer;
  3628. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  3629. int r;
  3630. mutex_lock(&dyn_info_mutex);
  3631. r = sprintf(buf, "%ld ", *p);
  3632. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  3633. buf[r++] = '\n';
  3634. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3635. mutex_unlock(&dyn_info_mutex);
  3636. return r;
  3637. }
  3638. static const struct file_operations tracing_dyn_info_fops = {
  3639. .open = tracing_open_generic,
  3640. .read = tracing_read_dyn_info,
  3641. .llseek = generic_file_llseek,
  3642. };
  3643. #endif
  3644. static struct dentry *d_tracer;
  3645. struct dentry *tracing_init_dentry(void)
  3646. {
  3647. static int once;
  3648. if (d_tracer)
  3649. return d_tracer;
  3650. if (!debugfs_initialized())
  3651. return NULL;
  3652. d_tracer = debugfs_create_dir("tracing", NULL);
  3653. if (!d_tracer && !once) {
  3654. once = 1;
  3655. pr_warning("Could not create debugfs directory 'tracing'\n");
  3656. return NULL;
  3657. }
  3658. return d_tracer;
  3659. }
  3660. static struct dentry *d_percpu;
  3661. struct dentry *tracing_dentry_percpu(void)
  3662. {
  3663. static int once;
  3664. struct dentry *d_tracer;
  3665. if (d_percpu)
  3666. return d_percpu;
  3667. d_tracer = tracing_init_dentry();
  3668. if (!d_tracer)
  3669. return NULL;
  3670. d_percpu = debugfs_create_dir("per_cpu", d_tracer);
  3671. if (!d_percpu && !once) {
  3672. once = 1;
  3673. pr_warning("Could not create debugfs directory 'per_cpu'\n");
  3674. return NULL;
  3675. }
  3676. return d_percpu;
  3677. }
  3678. static void tracing_init_debugfs_percpu(long cpu)
  3679. {
  3680. struct dentry *d_percpu = tracing_dentry_percpu();
  3681. struct dentry *d_cpu;
  3682. char cpu_dir[30]; /* 30 characters should be more than enough */
  3683. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  3684. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  3685. if (!d_cpu) {
  3686. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  3687. return;
  3688. }
  3689. /* per cpu trace_pipe */
  3690. trace_create_file("trace_pipe", 0444, d_cpu,
  3691. (void *) cpu, &tracing_pipe_fops);
  3692. /* per cpu trace */
  3693. trace_create_file("trace", 0644, d_cpu,
  3694. (void *) cpu, &tracing_fops);
  3695. trace_create_file("trace_pipe_raw", 0444, d_cpu,
  3696. (void *) cpu, &tracing_buffers_fops);
  3697. trace_create_file("stats", 0444, d_cpu,
  3698. (void *) cpu, &tracing_stats_fops);
  3699. }
  3700. #ifdef CONFIG_FTRACE_SELFTEST
  3701. /* Let selftest have access to static functions in this file */
  3702. #include "trace_selftest.c"
  3703. #endif
  3704. struct trace_option_dentry {
  3705. struct tracer_opt *opt;
  3706. struct tracer_flags *flags;
  3707. struct dentry *entry;
  3708. };
  3709. static ssize_t
  3710. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  3711. loff_t *ppos)
  3712. {
  3713. struct trace_option_dentry *topt = filp->private_data;
  3714. char *buf;
  3715. if (topt->flags->val & topt->opt->bit)
  3716. buf = "1\n";
  3717. else
  3718. buf = "0\n";
  3719. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3720. }
  3721. static ssize_t
  3722. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3723. loff_t *ppos)
  3724. {
  3725. struct trace_option_dentry *topt = filp->private_data;
  3726. unsigned long val;
  3727. int ret;
  3728. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3729. if (ret)
  3730. return ret;
  3731. if (val != 0 && val != 1)
  3732. return -EINVAL;
  3733. if (!!(topt->flags->val & topt->opt->bit) != val) {
  3734. mutex_lock(&trace_types_lock);
  3735. ret = __set_tracer_option(current_trace, topt->flags,
  3736. topt->opt, !val);
  3737. mutex_unlock(&trace_types_lock);
  3738. if (ret)
  3739. return ret;
  3740. }
  3741. *ppos += cnt;
  3742. return cnt;
  3743. }
  3744. static const struct file_operations trace_options_fops = {
  3745. .open = tracing_open_generic,
  3746. .read = trace_options_read,
  3747. .write = trace_options_write,
  3748. .llseek = generic_file_llseek,
  3749. };
  3750. static ssize_t
  3751. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  3752. loff_t *ppos)
  3753. {
  3754. long index = (long)filp->private_data;
  3755. char *buf;
  3756. if (trace_flags & (1 << index))
  3757. buf = "1\n";
  3758. else
  3759. buf = "0\n";
  3760. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3761. }
  3762. static ssize_t
  3763. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3764. loff_t *ppos)
  3765. {
  3766. long index = (long)filp->private_data;
  3767. unsigned long val;
  3768. int ret;
  3769. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3770. if (ret)
  3771. return ret;
  3772. if (val != 0 && val != 1)
  3773. return -EINVAL;
  3774. mutex_lock(&trace_types_lock);
  3775. ret = set_tracer_flag(1 << index, val);
  3776. mutex_unlock(&trace_types_lock);
  3777. if (ret < 0)
  3778. return ret;
  3779. *ppos += cnt;
  3780. return cnt;
  3781. }
  3782. static const struct file_operations trace_options_core_fops = {
  3783. .open = tracing_open_generic,
  3784. .read = trace_options_core_read,
  3785. .write = trace_options_core_write,
  3786. .llseek = generic_file_llseek,
  3787. };
  3788. struct dentry *trace_create_file(const char *name,
  3789. umode_t mode,
  3790. struct dentry *parent,
  3791. void *data,
  3792. const struct file_operations *fops)
  3793. {
  3794. struct dentry *ret;
  3795. ret = debugfs_create_file(name, mode, parent, data, fops);
  3796. if (!ret)
  3797. pr_warning("Could not create debugfs '%s' entry\n", name);
  3798. return ret;
  3799. }
  3800. static struct dentry *trace_options_init_dentry(void)
  3801. {
  3802. struct dentry *d_tracer;
  3803. static struct dentry *t_options;
  3804. if (t_options)
  3805. return t_options;
  3806. d_tracer = tracing_init_dentry();
  3807. if (!d_tracer)
  3808. return NULL;
  3809. t_options = debugfs_create_dir("options", d_tracer);
  3810. if (!t_options) {
  3811. pr_warning("Could not create debugfs directory 'options'\n");
  3812. return NULL;
  3813. }
  3814. return t_options;
  3815. }
  3816. static void
  3817. create_trace_option_file(struct trace_option_dentry *topt,
  3818. struct tracer_flags *flags,
  3819. struct tracer_opt *opt)
  3820. {
  3821. struct dentry *t_options;
  3822. t_options = trace_options_init_dentry();
  3823. if (!t_options)
  3824. return;
  3825. topt->flags = flags;
  3826. topt->opt = opt;
  3827. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  3828. &trace_options_fops);
  3829. }
  3830. static struct trace_option_dentry *
  3831. create_trace_option_files(struct tracer *tracer)
  3832. {
  3833. struct trace_option_dentry *topts;
  3834. struct tracer_flags *flags;
  3835. struct tracer_opt *opts;
  3836. int cnt;
  3837. if (!tracer)
  3838. return NULL;
  3839. flags = tracer->flags;
  3840. if (!flags || !flags->opts)
  3841. return NULL;
  3842. opts = flags->opts;
  3843. for (cnt = 0; opts[cnt].name; cnt++)
  3844. ;
  3845. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  3846. if (!topts)
  3847. return NULL;
  3848. for (cnt = 0; opts[cnt].name; cnt++)
  3849. create_trace_option_file(&topts[cnt], flags,
  3850. &opts[cnt]);
  3851. return topts;
  3852. }
  3853. static void
  3854. destroy_trace_option_files(struct trace_option_dentry *topts)
  3855. {
  3856. int cnt;
  3857. if (!topts)
  3858. return;
  3859. for (cnt = 0; topts[cnt].opt; cnt++) {
  3860. if (topts[cnt].entry)
  3861. debugfs_remove(topts[cnt].entry);
  3862. }
  3863. kfree(topts);
  3864. }
  3865. static struct dentry *
  3866. create_trace_option_core_file(const char *option, long index)
  3867. {
  3868. struct dentry *t_options;
  3869. t_options = trace_options_init_dentry();
  3870. if (!t_options)
  3871. return NULL;
  3872. return trace_create_file(option, 0644, t_options, (void *)index,
  3873. &trace_options_core_fops);
  3874. }
  3875. static __init void create_trace_options_dir(void)
  3876. {
  3877. struct dentry *t_options;
  3878. int i;
  3879. t_options = trace_options_init_dentry();
  3880. if (!t_options)
  3881. return;
  3882. for (i = 0; trace_options[i]; i++)
  3883. create_trace_option_core_file(trace_options[i], i);
  3884. }
  3885. static ssize_t
  3886. rb_simple_read(struct file *filp, char __user *ubuf,
  3887. size_t cnt, loff_t *ppos)
  3888. {
  3889. struct trace_array *tr = filp->private_data;
  3890. struct ring_buffer *buffer = tr->buffer;
  3891. char buf[64];
  3892. int r;
  3893. if (buffer)
  3894. r = ring_buffer_record_is_on(buffer);
  3895. else
  3896. r = 0;
  3897. r = sprintf(buf, "%d\n", r);
  3898. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3899. }
  3900. static ssize_t
  3901. rb_simple_write(struct file *filp, const char __user *ubuf,
  3902. size_t cnt, loff_t *ppos)
  3903. {
  3904. struct trace_array *tr = filp->private_data;
  3905. struct ring_buffer *buffer = tr->buffer;
  3906. unsigned long val;
  3907. int ret;
  3908. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  3909. if (ret)
  3910. return ret;
  3911. if (buffer) {
  3912. if (val)
  3913. ring_buffer_record_on(buffer);
  3914. else
  3915. ring_buffer_record_off(buffer);
  3916. }
  3917. (*ppos)++;
  3918. return cnt;
  3919. }
  3920. static const struct file_operations rb_simple_fops = {
  3921. .open = tracing_open_generic,
  3922. .read = rb_simple_read,
  3923. .write = rb_simple_write,
  3924. .llseek = default_llseek,
  3925. };
  3926. static __init int tracer_init_debugfs(void)
  3927. {
  3928. struct dentry *d_tracer;
  3929. int cpu;
  3930. trace_access_lock_init();
  3931. d_tracer = tracing_init_dentry();
  3932. if (!d_tracer)
  3933. return 0;
  3934. trace_create_file("tracing_enabled", 0644, d_tracer,
  3935. &global_trace, &tracing_ctrl_fops);
  3936. trace_create_file("trace_options", 0644, d_tracer,
  3937. NULL, &tracing_iter_fops);
  3938. trace_create_file("tracing_cpumask", 0644, d_tracer,
  3939. NULL, &tracing_cpumask_fops);
  3940. trace_create_file("trace", 0644, d_tracer,
  3941. (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
  3942. trace_create_file("available_tracers", 0444, d_tracer,
  3943. &global_trace, &show_traces_fops);
  3944. trace_create_file("current_tracer", 0644, d_tracer,
  3945. &global_trace, &set_tracer_fops);
  3946. #ifdef CONFIG_TRACER_MAX_TRACE
  3947. trace_create_file("tracing_max_latency", 0644, d_tracer,
  3948. &tracing_max_latency, &tracing_max_lat_fops);
  3949. #endif
  3950. trace_create_file("tracing_thresh", 0644, d_tracer,
  3951. &tracing_thresh, &tracing_max_lat_fops);
  3952. trace_create_file("README", 0444, d_tracer,
  3953. NULL, &tracing_readme_fops);
  3954. trace_create_file("trace_pipe", 0444, d_tracer,
  3955. (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
  3956. trace_create_file("buffer_size_kb", 0644, d_tracer,
  3957. &global_trace, &tracing_entries_fops);
  3958. trace_create_file("buffer_total_size_kb", 0444, d_tracer,
  3959. &global_trace, &tracing_total_entries_fops);
  3960. trace_create_file("free_buffer", 0644, d_tracer,
  3961. &global_trace, &tracing_free_buffer_fops);
  3962. trace_create_file("trace_marker", 0220, d_tracer,
  3963. NULL, &tracing_mark_fops);
  3964. trace_create_file("saved_cmdlines", 0444, d_tracer,
  3965. NULL, &tracing_saved_cmdlines_fops);
  3966. trace_create_file("saved_tgids", 0444, d_tracer,
  3967. NULL, &tracing_saved_tgids_fops);
  3968. trace_create_file("trace_clock", 0644, d_tracer, NULL,
  3969. &trace_clock_fops);
  3970. trace_create_file("tracing_on", 0644, d_tracer,
  3971. &global_trace, &rb_simple_fops);
  3972. #ifdef CONFIG_DYNAMIC_FTRACE
  3973. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  3974. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  3975. #endif
  3976. create_trace_options_dir();
  3977. for_each_tracing_cpu(cpu)
  3978. tracing_init_debugfs_percpu(cpu);
  3979. return 0;
  3980. }
  3981. static int trace_panic_handler(struct notifier_block *this,
  3982. unsigned long event, void *unused)
  3983. {
  3984. if (ftrace_dump_on_oops)
  3985. ftrace_dump(ftrace_dump_on_oops);
  3986. return NOTIFY_OK;
  3987. }
  3988. static struct notifier_block trace_panic_notifier = {
  3989. .notifier_call = trace_panic_handler,
  3990. .next = NULL,
  3991. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  3992. };
  3993. static int trace_die_handler(struct notifier_block *self,
  3994. unsigned long val,
  3995. void *data)
  3996. {
  3997. switch (val) {
  3998. case DIE_OOPS:
  3999. if (ftrace_dump_on_oops)
  4000. ftrace_dump(ftrace_dump_on_oops);
  4001. break;
  4002. default:
  4003. break;
  4004. }
  4005. return NOTIFY_OK;
  4006. }
  4007. static struct notifier_block trace_die_notifier = {
  4008. .notifier_call = trace_die_handler,
  4009. .priority = 200
  4010. };
  4011. /*
  4012. * printk is set to max of 1024, we really don't need it that big.
  4013. * Nothing should be printing 1000 characters anyway.
  4014. */
  4015. #define TRACE_MAX_PRINT 1000
  4016. /*
  4017. * Define here KERN_TRACE so that we have one place to modify
  4018. * it if we decide to change what log level the ftrace dump
  4019. * should be at.
  4020. */
  4021. #define KERN_TRACE KERN_EMERG
  4022. void
  4023. trace_printk_seq(struct trace_seq *s)
  4024. {
  4025. /* Probably should print a warning here. */
  4026. if (s->len >= 1000)
  4027. s->len = 1000;
  4028. /* should be zero ended, but we are paranoid. */
  4029. s->buffer[s->len] = 0;
  4030. printk(KERN_TRACE "%s", s->buffer);
  4031. trace_seq_init(s);
  4032. }
  4033. void trace_init_global_iter(struct trace_iterator *iter)
  4034. {
  4035. iter->tr = &global_trace;
  4036. iter->trace = current_trace;
  4037. iter->cpu_file = TRACE_PIPE_ALL_CPU;
  4038. }
  4039. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  4040. {
  4041. /* use static because iter can be a bit big for the stack */
  4042. static struct trace_iterator iter;
  4043. static atomic_t dump_running;
  4044. unsigned int old_userobj;
  4045. unsigned long flags;
  4046. int cnt = 0, cpu;
  4047. /* Only allow one dump user at a time. */
  4048. if (atomic_inc_return(&dump_running) != 1) {
  4049. atomic_dec(&dump_running);
  4050. return;
  4051. }
  4052. /*
  4053. * Always turn off tracing when we dump.
  4054. * We don't need to show trace output of what happens
  4055. * between multiple crashes.
  4056. *
  4057. * If the user does a sysrq-z, then they can re-enable
  4058. * tracing with echo 1 > tracing_on.
  4059. */
  4060. tracing_off();
  4061. local_irq_save(flags);
  4062. trace_init_global_iter(&iter);
  4063. for_each_tracing_cpu(cpu) {
  4064. atomic_inc(&iter.tr->data[cpu]->disabled);
  4065. }
  4066. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  4067. /* don't look at user memory in panic mode */
  4068. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  4069. /* Simulate the iterator */
  4070. iter.tr = &global_trace;
  4071. iter.trace = current_trace;
  4072. switch (oops_dump_mode) {
  4073. case DUMP_ALL:
  4074. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  4075. break;
  4076. case DUMP_ORIG:
  4077. iter.cpu_file = raw_smp_processor_id();
  4078. break;
  4079. case DUMP_NONE:
  4080. goto out_enable;
  4081. default:
  4082. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  4083. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  4084. }
  4085. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  4086. /* Did function tracer already get disabled? */
  4087. if (ftrace_is_dead()) {
  4088. printk("# WARNING: FUNCTION TRACING IS CORRUPTED\n");
  4089. printk("# MAY BE MISSING FUNCTION EVENTS\n");
  4090. }
  4091. /*
  4092. * We need to stop all tracing on all CPUS to read the
  4093. * the next buffer. This is a bit expensive, but is
  4094. * not done often. We fill all what we can read,
  4095. * and then release the locks again.
  4096. */
  4097. while (!trace_empty(&iter)) {
  4098. if (!cnt)
  4099. printk(KERN_TRACE "---------------------------------\n");
  4100. cnt++;
  4101. /* reset all but tr, trace, and overruns */
  4102. memset(&iter.seq, 0,
  4103. sizeof(struct trace_iterator) -
  4104. offsetof(struct trace_iterator, seq));
  4105. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  4106. iter.pos = -1;
  4107. if (trace_find_next_entry_inc(&iter) != NULL) {
  4108. int ret;
  4109. ret = print_trace_line(&iter);
  4110. if (ret != TRACE_TYPE_NO_CONSUME)
  4111. trace_consume(&iter);
  4112. }
  4113. touch_nmi_watchdog();
  4114. trace_printk_seq(&iter.seq);
  4115. }
  4116. if (!cnt)
  4117. printk(KERN_TRACE " (ftrace buffer empty)\n");
  4118. else
  4119. printk(KERN_TRACE "---------------------------------\n");
  4120. out_enable:
  4121. trace_flags |= old_userobj;
  4122. for_each_tracing_cpu(cpu) {
  4123. atomic_dec(&iter.tr->data[cpu]->disabled);
  4124. }
  4125. atomic_dec(&dump_running);
  4126. local_irq_restore(flags);
  4127. }
  4128. EXPORT_SYMBOL_GPL(ftrace_dump);
  4129. __init static int tracer_alloc_buffers(void)
  4130. {
  4131. int ring_buf_size;
  4132. enum ring_buffer_flags rb_flags;
  4133. int i;
  4134. int ret = -ENOMEM;
  4135. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  4136. goto out;
  4137. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  4138. goto out_free_buffer_mask;
  4139. /* To save memory, keep the ring buffer size to its minimum */
  4140. if (ring_buffer_expanded)
  4141. ring_buf_size = trace_buf_size;
  4142. else
  4143. ring_buf_size = 1;
  4144. rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  4145. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  4146. cpumask_copy(tracing_cpumask, cpu_all_mask);
  4147. /* TODO: make the number of buffers hot pluggable with CPUS */
  4148. global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
  4149. if (!global_trace.buffer) {
  4150. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  4151. WARN_ON(1);
  4152. goto out_free_cpumask;
  4153. }
  4154. global_trace.entries = ring_buffer_size(global_trace.buffer);
  4155. if (global_trace.buffer_disabled)
  4156. tracing_off();
  4157. #ifdef CONFIG_TRACER_MAX_TRACE
  4158. max_tr.buffer = ring_buffer_alloc(1, rb_flags);
  4159. if (!max_tr.buffer) {
  4160. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  4161. WARN_ON(1);
  4162. ring_buffer_free(global_trace.buffer);
  4163. goto out_free_cpumask;
  4164. }
  4165. max_tr.entries = 1;
  4166. #endif
  4167. /* Allocate the first page for all buffers */
  4168. for_each_tracing_cpu(i) {
  4169. global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  4170. max_tr.data[i] = &per_cpu(max_tr_data, i);
  4171. }
  4172. trace_init_cmdlines();
  4173. register_tracer(&nop_trace);
  4174. current_trace = &nop_trace;
  4175. /* All seems OK, enable tracing */
  4176. tracing_disabled = 0;
  4177. atomic_notifier_chain_register(&panic_notifier_list,
  4178. &trace_panic_notifier);
  4179. register_die_notifier(&trace_die_notifier);
  4180. return 0;
  4181. out_free_cpumask:
  4182. free_cpumask_var(tracing_cpumask);
  4183. out_free_buffer_mask:
  4184. free_cpumask_var(tracing_buffer_mask);
  4185. out:
  4186. return ret;
  4187. }
  4188. __init static int clear_boot_tracer(void)
  4189. {
  4190. /*
  4191. * The default tracer at boot buffer is an init section.
  4192. * This function is called in lateinit. If we did not
  4193. * find the boot tracer, then clear it out, to prevent
  4194. * later registration from accessing the buffer that is
  4195. * about to be freed.
  4196. */
  4197. if (!default_bootup_tracer)
  4198. return 0;
  4199. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  4200. default_bootup_tracer);
  4201. default_bootup_tracer = NULL;
  4202. return 0;
  4203. }
  4204. early_initcall(tracer_alloc_buffers);
  4205. fs_initcall(tracer_init_debugfs);
  4206. late_initcall(clear_boot_tracer);