trace.c 105 KB

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