trace.c 107 KB

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