trace.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659
  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. /**
  292. * trace_wake_up - wake up tasks waiting for trace input
  293. *
  294. * Simply wakes up any task that is blocked on the trace_wait
  295. * queue. These is used with trace_poll for tasks polling the trace.
  296. */
  297. void trace_wake_up(void)
  298. {
  299. int cpu;
  300. if (trace_flags & TRACE_ITER_BLOCK)
  301. return;
  302. /*
  303. * The runqueue_is_locked() can fail, but this is the best we
  304. * have for now:
  305. */
  306. cpu = get_cpu();
  307. if (!runqueue_is_locked(cpu))
  308. wake_up(&trace_wait);
  309. put_cpu();
  310. }
  311. static int __init set_buf_size(char *str)
  312. {
  313. unsigned long buf_size;
  314. if (!str)
  315. return 0;
  316. buf_size = memparse(str, &str);
  317. /* nr_entries can not be zero */
  318. if (buf_size == 0)
  319. return 0;
  320. trace_buf_size = buf_size;
  321. return 1;
  322. }
  323. __setup("trace_buf_size=", set_buf_size);
  324. static int __init set_tracing_thresh(char *str)
  325. {
  326. unsigned long threshhold;
  327. int ret;
  328. if (!str)
  329. return 0;
  330. ret = strict_strtoul(str, 0, &threshhold);
  331. if (ret < 0)
  332. return 0;
  333. tracing_thresh = threshhold * 1000;
  334. return 1;
  335. }
  336. __setup("tracing_thresh=", set_tracing_thresh);
  337. unsigned long nsecs_to_usecs(unsigned long nsecs)
  338. {
  339. return nsecs / 1000;
  340. }
  341. /* These must match the bit postions in trace_iterator_flags */
  342. static const char *trace_options[] = {
  343. "print-parent",
  344. "sym-offset",
  345. "sym-addr",
  346. "verbose",
  347. "raw",
  348. "hex",
  349. "bin",
  350. "block",
  351. "stacktrace",
  352. "trace_printk",
  353. "ftrace_preempt",
  354. "branch",
  355. "annotate",
  356. "userstacktrace",
  357. "sym-userobj",
  358. "printk-msg-only",
  359. "context-info",
  360. "latency-format",
  361. "sleep-time",
  362. "graph-time",
  363. "record-cmd",
  364. "overwrite",
  365. NULL
  366. };
  367. static struct {
  368. u64 (*func)(void);
  369. const char *name;
  370. } trace_clocks[] = {
  371. { trace_clock_local, "local" },
  372. { trace_clock_global, "global" },
  373. };
  374. int trace_clock_id;
  375. /*
  376. * trace_parser_get_init - gets the buffer for trace parser
  377. */
  378. int trace_parser_get_init(struct trace_parser *parser, int size)
  379. {
  380. memset(parser, 0, sizeof(*parser));
  381. parser->buffer = kmalloc(size, GFP_KERNEL);
  382. if (!parser->buffer)
  383. return 1;
  384. parser->size = size;
  385. return 0;
  386. }
  387. /*
  388. * trace_parser_put - frees the buffer for trace parser
  389. */
  390. void trace_parser_put(struct trace_parser *parser)
  391. {
  392. kfree(parser->buffer);
  393. }
  394. /*
  395. * trace_get_user - reads the user input string separated by space
  396. * (matched by isspace(ch))
  397. *
  398. * For each string found the 'struct trace_parser' is updated,
  399. * and the function returns.
  400. *
  401. * Returns number of bytes read.
  402. *
  403. * See kernel/trace/trace.h for 'struct trace_parser' details.
  404. */
  405. int trace_get_user(struct trace_parser *parser, const char __user *ubuf,
  406. size_t cnt, loff_t *ppos)
  407. {
  408. char ch;
  409. size_t read = 0;
  410. ssize_t ret;
  411. if (!*ppos)
  412. trace_parser_clear(parser);
  413. ret = get_user(ch, ubuf++);
  414. if (ret)
  415. goto out;
  416. read++;
  417. cnt--;
  418. /*
  419. * The parser is not finished with the last write,
  420. * continue reading the user input without skipping spaces.
  421. */
  422. if (!parser->cont) {
  423. /* skip white space */
  424. while (cnt && isspace(ch)) {
  425. ret = get_user(ch, ubuf++);
  426. if (ret)
  427. goto out;
  428. read++;
  429. cnt--;
  430. }
  431. /* only spaces were written */
  432. if (isspace(ch)) {
  433. *ppos += read;
  434. ret = read;
  435. goto out;
  436. }
  437. parser->idx = 0;
  438. }
  439. /* read the non-space input */
  440. while (cnt && !isspace(ch)) {
  441. if (parser->idx < parser->size - 1)
  442. parser->buffer[parser->idx++] = ch;
  443. else {
  444. ret = -EINVAL;
  445. goto out;
  446. }
  447. ret = get_user(ch, ubuf++);
  448. if (ret)
  449. goto out;
  450. read++;
  451. cnt--;
  452. }
  453. /* We either got finished input or we have to wait for another call. */
  454. if (isspace(ch)) {
  455. parser->buffer[parser->idx] = 0;
  456. parser->cont = false;
  457. } else {
  458. parser->cont = true;
  459. parser->buffer[parser->idx++] = ch;
  460. }
  461. *ppos += read;
  462. ret = read;
  463. out:
  464. return ret;
  465. }
  466. ssize_t trace_seq_to_user(struct trace_seq *s, char __user *ubuf, size_t cnt)
  467. {
  468. int len;
  469. int ret;
  470. if (!cnt)
  471. return 0;
  472. if (s->len <= s->readpos)
  473. return -EBUSY;
  474. len = s->len - s->readpos;
  475. if (cnt > len)
  476. cnt = len;
  477. ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
  478. if (ret == cnt)
  479. return -EFAULT;
  480. cnt -= ret;
  481. s->readpos += cnt;
  482. return cnt;
  483. }
  484. static ssize_t trace_seq_to_buffer(struct trace_seq *s, void *buf, size_t cnt)
  485. {
  486. int len;
  487. void *ret;
  488. if (s->len <= s->readpos)
  489. return -EBUSY;
  490. len = s->len - s->readpos;
  491. if (cnt > len)
  492. cnt = len;
  493. ret = memcpy(buf, s->buffer + s->readpos, cnt);
  494. if (!ret)
  495. return -EFAULT;
  496. s->readpos += cnt;
  497. return cnt;
  498. }
  499. /*
  500. * ftrace_max_lock is used to protect the swapping of buffers
  501. * when taking a max snapshot. The buffers themselves are
  502. * protected by per_cpu spinlocks. But the action of the swap
  503. * needs its own lock.
  504. *
  505. * This is defined as a arch_spinlock_t in order to help
  506. * with performance when lockdep debugging is enabled.
  507. *
  508. * It is also used in other places outside the update_max_tr
  509. * so it needs to be defined outside of the
  510. * CONFIG_TRACER_MAX_TRACE.
  511. */
  512. static arch_spinlock_t ftrace_max_lock =
  513. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  514. unsigned long __read_mostly tracing_thresh;
  515. #ifdef CONFIG_TRACER_MAX_TRACE
  516. unsigned long __read_mostly tracing_max_latency;
  517. /*
  518. * Copy the new maximum trace into the separate maximum-trace
  519. * structure. (this way the maximum trace is permanently saved,
  520. * for later retrieval via /sys/kernel/debug/tracing/latency_trace)
  521. */
  522. static void
  523. __update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  524. {
  525. struct trace_array_cpu *data = tr->data[cpu];
  526. struct trace_array_cpu *max_data;
  527. max_tr.cpu = cpu;
  528. max_tr.time_start = data->preempt_timestamp;
  529. max_data = max_tr.data[cpu];
  530. max_data->saved_latency = tracing_max_latency;
  531. max_data->critical_start = data->critical_start;
  532. max_data->critical_end = data->critical_end;
  533. memcpy(max_data->comm, tsk->comm, TASK_COMM_LEN);
  534. max_data->pid = tsk->pid;
  535. max_data->uid = task_uid(tsk);
  536. max_data->nice = tsk->static_prio - 20 - MAX_RT_PRIO;
  537. max_data->policy = tsk->policy;
  538. max_data->rt_priority = tsk->rt_priority;
  539. /* record this tasks comm */
  540. tracing_record_cmdline(tsk);
  541. }
  542. /**
  543. * update_max_tr - snapshot all trace buffers from global_trace to max_tr
  544. * @tr: tracer
  545. * @tsk: the task with the latency
  546. * @cpu: The cpu that initiated the trace.
  547. *
  548. * Flip the buffers between the @tr and the max_tr and record information
  549. * about which task was the cause of this latency.
  550. */
  551. void
  552. update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu)
  553. {
  554. struct ring_buffer *buf = tr->buffer;
  555. if (trace_stop_count)
  556. return;
  557. WARN_ON_ONCE(!irqs_disabled());
  558. if (!current_trace->use_max_tr) {
  559. WARN_ON_ONCE(1);
  560. return;
  561. }
  562. arch_spin_lock(&ftrace_max_lock);
  563. tr->buffer = max_tr.buffer;
  564. max_tr.buffer = buf;
  565. __update_max_tr(tr, tsk, cpu);
  566. arch_spin_unlock(&ftrace_max_lock);
  567. }
  568. /**
  569. * update_max_tr_single - only copy one trace over, and reset the rest
  570. * @tr - tracer
  571. * @tsk - task with the latency
  572. * @cpu - the cpu of the buffer to copy.
  573. *
  574. * Flip the trace of a single CPU buffer between the @tr and the max_tr.
  575. */
  576. void
  577. update_max_tr_single(struct trace_array *tr, struct task_struct *tsk, int cpu)
  578. {
  579. int ret;
  580. if (trace_stop_count)
  581. return;
  582. WARN_ON_ONCE(!irqs_disabled());
  583. if (!current_trace->use_max_tr) {
  584. WARN_ON_ONCE(1);
  585. return;
  586. }
  587. arch_spin_lock(&ftrace_max_lock);
  588. ftrace_disable_cpu();
  589. ret = ring_buffer_swap_cpu(max_tr.buffer, tr->buffer, cpu);
  590. if (ret == -EBUSY) {
  591. /*
  592. * We failed to swap the buffer due to a commit taking
  593. * place on this CPU. We fail to record, but we reset
  594. * the max trace buffer (no one writes directly to it)
  595. * and flag that it failed.
  596. */
  597. trace_array_printk(&max_tr, _THIS_IP_,
  598. "Failed to swap buffers due to commit in progress\n");
  599. }
  600. ftrace_enable_cpu();
  601. WARN_ON_ONCE(ret && ret != -EAGAIN && ret != -EBUSY);
  602. __update_max_tr(tr, tsk, cpu);
  603. arch_spin_unlock(&ftrace_max_lock);
  604. }
  605. #endif /* CONFIG_TRACER_MAX_TRACE */
  606. /**
  607. * register_tracer - register a tracer with the ftrace system.
  608. * @type - the plugin for the tracer
  609. *
  610. * Register a new plugin tracer.
  611. */
  612. int register_tracer(struct tracer *type)
  613. __releases(kernel_lock)
  614. __acquires(kernel_lock)
  615. {
  616. struct tracer *t;
  617. int ret = 0;
  618. if (!type->name) {
  619. pr_info("Tracer must have a name\n");
  620. return -1;
  621. }
  622. if (strlen(type->name) >= MAX_TRACER_SIZE) {
  623. pr_info("Tracer has a name longer than %d\n", MAX_TRACER_SIZE);
  624. return -1;
  625. }
  626. mutex_lock(&trace_types_lock);
  627. tracing_selftest_running = true;
  628. for (t = trace_types; t; t = t->next) {
  629. if (strcmp(type->name, t->name) == 0) {
  630. /* already found */
  631. pr_info("Tracer %s already registered\n",
  632. type->name);
  633. ret = -1;
  634. goto out;
  635. }
  636. }
  637. if (!type->set_flag)
  638. type->set_flag = &dummy_set_flag;
  639. if (!type->flags)
  640. type->flags = &dummy_tracer_flags;
  641. else
  642. if (!type->flags->opts)
  643. type->flags->opts = dummy_tracer_opt;
  644. if (!type->wait_pipe)
  645. type->wait_pipe = default_wait_pipe;
  646. #ifdef CONFIG_FTRACE_STARTUP_TEST
  647. if (type->selftest && !tracing_selftest_disabled) {
  648. struct tracer *saved_tracer = current_trace;
  649. struct trace_array *tr = &global_trace;
  650. /*
  651. * Run a selftest on this tracer.
  652. * Here we reset the trace buffer, and set the current
  653. * tracer to be this tracer. The tracer can then run some
  654. * internal tracing to verify that everything is in order.
  655. * If we fail, we do not register this tracer.
  656. */
  657. tracing_reset_online_cpus(tr);
  658. current_trace = type;
  659. /* If we expanded the buffers, make sure the max is expanded too */
  660. if (ring_buffer_expanded && type->use_max_tr)
  661. ring_buffer_resize(max_tr.buffer, trace_buf_size);
  662. /* the test is responsible for initializing and enabling */
  663. pr_info("Testing tracer %s: ", type->name);
  664. ret = type->selftest(type, tr);
  665. /* the test is responsible for resetting too */
  666. current_trace = saved_tracer;
  667. if (ret) {
  668. printk(KERN_CONT "FAILED!\n");
  669. goto out;
  670. }
  671. /* Only reset on passing, to avoid touching corrupted buffers */
  672. tracing_reset_online_cpus(tr);
  673. /* Shrink the max buffer again */
  674. if (ring_buffer_expanded && type->use_max_tr)
  675. ring_buffer_resize(max_tr.buffer, 1);
  676. printk(KERN_CONT "PASSED\n");
  677. }
  678. #endif
  679. type->next = trace_types;
  680. trace_types = type;
  681. out:
  682. tracing_selftest_running = false;
  683. mutex_unlock(&trace_types_lock);
  684. if (ret || !default_bootup_tracer)
  685. goto out_unlock;
  686. if (strncmp(default_bootup_tracer, type->name, MAX_TRACER_SIZE))
  687. goto out_unlock;
  688. printk(KERN_INFO "Starting tracer '%s'\n", type->name);
  689. /* Do we want this tracer to start on bootup? */
  690. tracing_set_tracer(type->name);
  691. default_bootup_tracer = NULL;
  692. /* disable other selftests, since this will break it. */
  693. tracing_selftest_disabled = 1;
  694. #ifdef CONFIG_FTRACE_STARTUP_TEST
  695. printk(KERN_INFO "Disabling FTRACE selftests due to running tracer '%s'\n",
  696. type->name);
  697. #endif
  698. out_unlock:
  699. return ret;
  700. }
  701. void unregister_tracer(struct tracer *type)
  702. {
  703. struct tracer **t;
  704. mutex_lock(&trace_types_lock);
  705. for (t = &trace_types; *t; t = &(*t)->next) {
  706. if (*t == type)
  707. goto found;
  708. }
  709. pr_info("Tracer %s not registered\n", type->name);
  710. goto out;
  711. found:
  712. *t = (*t)->next;
  713. if (type == current_trace && tracer_enabled) {
  714. tracer_enabled = 0;
  715. tracing_stop();
  716. if (current_trace->stop)
  717. current_trace->stop(&global_trace);
  718. current_trace = &nop_trace;
  719. }
  720. out:
  721. mutex_unlock(&trace_types_lock);
  722. }
  723. static void __tracing_reset(struct ring_buffer *buffer, int cpu)
  724. {
  725. ftrace_disable_cpu();
  726. ring_buffer_reset_cpu(buffer, cpu);
  727. ftrace_enable_cpu();
  728. }
  729. void tracing_reset(struct trace_array *tr, int cpu)
  730. {
  731. struct ring_buffer *buffer = tr->buffer;
  732. ring_buffer_record_disable(buffer);
  733. /* Make sure all commits have finished */
  734. synchronize_sched();
  735. __tracing_reset(buffer, cpu);
  736. ring_buffer_record_enable(buffer);
  737. }
  738. void tracing_reset_online_cpus(struct trace_array *tr)
  739. {
  740. struct ring_buffer *buffer = tr->buffer;
  741. int cpu;
  742. ring_buffer_record_disable(buffer);
  743. /* Make sure all commits have finished */
  744. synchronize_sched();
  745. tr->time_start = ftrace_now(tr->cpu);
  746. for_each_online_cpu(cpu)
  747. __tracing_reset(buffer, cpu);
  748. ring_buffer_record_enable(buffer);
  749. }
  750. void tracing_reset_current(int cpu)
  751. {
  752. tracing_reset(&global_trace, cpu);
  753. }
  754. void tracing_reset_current_online_cpus(void)
  755. {
  756. tracing_reset_online_cpus(&global_trace);
  757. }
  758. #define SAVED_CMDLINES 128
  759. #define NO_CMDLINE_MAP UINT_MAX
  760. static unsigned map_pid_to_cmdline[PID_MAX_DEFAULT+1];
  761. static unsigned map_cmdline_to_pid[SAVED_CMDLINES];
  762. static char saved_cmdlines[SAVED_CMDLINES][TASK_COMM_LEN];
  763. static int cmdline_idx;
  764. static arch_spinlock_t trace_cmdline_lock = __ARCH_SPIN_LOCK_UNLOCKED;
  765. /* temporary disable recording */
  766. static atomic_t trace_record_cmdline_disabled __read_mostly;
  767. static void trace_init_cmdlines(void)
  768. {
  769. memset(&map_pid_to_cmdline, NO_CMDLINE_MAP, sizeof(map_pid_to_cmdline));
  770. memset(&map_cmdline_to_pid, NO_CMDLINE_MAP, sizeof(map_cmdline_to_pid));
  771. cmdline_idx = 0;
  772. }
  773. int is_tracing_stopped(void)
  774. {
  775. return trace_stop_count;
  776. }
  777. /**
  778. * ftrace_off_permanent - disable all ftrace code permanently
  779. *
  780. * This should only be called when a serious anomally has
  781. * been detected. This will turn off the function tracing,
  782. * ring buffers, and other tracing utilites. It takes no
  783. * locks and can be called from any context.
  784. */
  785. void ftrace_off_permanent(void)
  786. {
  787. tracing_disabled = 1;
  788. ftrace_stop();
  789. tracing_off_permanent();
  790. }
  791. /**
  792. * tracing_start - quick start of the tracer
  793. *
  794. * If tracing is enabled but was stopped by tracing_stop,
  795. * this will start the tracer back up.
  796. */
  797. void tracing_start(void)
  798. {
  799. struct ring_buffer *buffer;
  800. unsigned long flags;
  801. if (tracing_disabled)
  802. return;
  803. spin_lock_irqsave(&tracing_start_lock, flags);
  804. if (--trace_stop_count) {
  805. if (trace_stop_count < 0) {
  806. /* Someone screwed up their debugging */
  807. WARN_ON_ONCE(1);
  808. trace_stop_count = 0;
  809. }
  810. goto out;
  811. }
  812. /* Prevent the buffers from switching */
  813. arch_spin_lock(&ftrace_max_lock);
  814. buffer = global_trace.buffer;
  815. if (buffer)
  816. ring_buffer_record_enable(buffer);
  817. buffer = max_tr.buffer;
  818. if (buffer)
  819. ring_buffer_record_enable(buffer);
  820. arch_spin_unlock(&ftrace_max_lock);
  821. ftrace_start();
  822. out:
  823. spin_unlock_irqrestore(&tracing_start_lock, flags);
  824. }
  825. /**
  826. * tracing_stop - quick stop of the tracer
  827. *
  828. * Light weight way to stop tracing. Use in conjunction with
  829. * tracing_start.
  830. */
  831. void tracing_stop(void)
  832. {
  833. struct ring_buffer *buffer;
  834. unsigned long flags;
  835. ftrace_stop();
  836. spin_lock_irqsave(&tracing_start_lock, flags);
  837. if (trace_stop_count++)
  838. goto out;
  839. /* Prevent the buffers from switching */
  840. arch_spin_lock(&ftrace_max_lock);
  841. buffer = global_trace.buffer;
  842. if (buffer)
  843. ring_buffer_record_disable(buffer);
  844. buffer = max_tr.buffer;
  845. if (buffer)
  846. ring_buffer_record_disable(buffer);
  847. arch_spin_unlock(&ftrace_max_lock);
  848. out:
  849. spin_unlock_irqrestore(&tracing_start_lock, flags);
  850. }
  851. void trace_stop_cmdline_recording(void);
  852. static void trace_save_cmdline(struct task_struct *tsk)
  853. {
  854. unsigned pid, idx;
  855. if (!tsk->pid || unlikely(tsk->pid > PID_MAX_DEFAULT))
  856. return;
  857. /*
  858. * It's not the end of the world if we don't get
  859. * the lock, but we also don't want to spin
  860. * nor do we want to disable interrupts,
  861. * so if we miss here, then better luck next time.
  862. */
  863. if (!arch_spin_trylock(&trace_cmdline_lock))
  864. return;
  865. idx = map_pid_to_cmdline[tsk->pid];
  866. if (idx == NO_CMDLINE_MAP) {
  867. idx = (cmdline_idx + 1) % SAVED_CMDLINES;
  868. /*
  869. * Check whether the cmdline buffer at idx has a pid
  870. * mapped. We are going to overwrite that entry so we
  871. * need to clear the map_pid_to_cmdline. Otherwise we
  872. * would read the new comm for the old pid.
  873. */
  874. pid = map_cmdline_to_pid[idx];
  875. if (pid != NO_CMDLINE_MAP)
  876. map_pid_to_cmdline[pid] = NO_CMDLINE_MAP;
  877. map_cmdline_to_pid[idx] = tsk->pid;
  878. map_pid_to_cmdline[tsk->pid] = idx;
  879. cmdline_idx = idx;
  880. }
  881. memcpy(&saved_cmdlines[idx], tsk->comm, TASK_COMM_LEN);
  882. arch_spin_unlock(&trace_cmdline_lock);
  883. }
  884. void trace_find_cmdline(int pid, char comm[])
  885. {
  886. unsigned map;
  887. if (!pid) {
  888. strcpy(comm, "<idle>");
  889. return;
  890. }
  891. if (WARN_ON_ONCE(pid < 0)) {
  892. strcpy(comm, "<XXX>");
  893. return;
  894. }
  895. if (pid > PID_MAX_DEFAULT) {
  896. strcpy(comm, "<...>");
  897. return;
  898. }
  899. preempt_disable();
  900. arch_spin_lock(&trace_cmdline_lock);
  901. map = map_pid_to_cmdline[pid];
  902. if (map != NO_CMDLINE_MAP)
  903. strcpy(comm, saved_cmdlines[map]);
  904. else
  905. strcpy(comm, "<...>");
  906. arch_spin_unlock(&trace_cmdline_lock);
  907. preempt_enable();
  908. }
  909. void tracing_record_cmdline(struct task_struct *tsk)
  910. {
  911. if (atomic_read(&trace_record_cmdline_disabled) || !tracer_enabled ||
  912. !tracing_is_on())
  913. return;
  914. trace_save_cmdline(tsk);
  915. }
  916. void
  917. tracing_generic_entry_update(struct trace_entry *entry, unsigned long flags,
  918. int pc)
  919. {
  920. struct task_struct *tsk = current;
  921. entry->preempt_count = pc & 0xff;
  922. entry->pid = (tsk) ? tsk->pid : 0;
  923. entry->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. if (iter->trace && iter->trace->print_line) {
  1665. ret = iter->trace->print_line(iter);
  1666. if (ret != TRACE_TYPE_UNHANDLED)
  1667. return ret;
  1668. }
  1669. if (iter->ent->type == TRACE_BPRINT &&
  1670. trace_flags & TRACE_ITER_PRINTK &&
  1671. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1672. return trace_print_bprintk_msg_only(iter);
  1673. if (iter->ent->type == TRACE_PRINT &&
  1674. trace_flags & TRACE_ITER_PRINTK &&
  1675. trace_flags & TRACE_ITER_PRINTK_MSGONLY)
  1676. return trace_print_printk_msg_only(iter);
  1677. if (trace_flags & TRACE_ITER_BIN)
  1678. return print_bin_fmt(iter);
  1679. if (trace_flags & TRACE_ITER_HEX)
  1680. return print_hex_fmt(iter);
  1681. if (trace_flags & TRACE_ITER_RAW)
  1682. return print_raw_fmt(iter);
  1683. return print_trace_fmt(iter);
  1684. }
  1685. void trace_default_header(struct seq_file *m)
  1686. {
  1687. struct trace_iterator *iter = m->private;
  1688. if (iter->iter_flags & TRACE_FILE_LAT_FMT) {
  1689. /* print nothing if the buffers are empty */
  1690. if (trace_empty(iter))
  1691. return;
  1692. print_trace_header(m, iter);
  1693. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1694. print_lat_help_header(m);
  1695. } else {
  1696. if (!(trace_flags & TRACE_ITER_VERBOSE))
  1697. print_func_help_header(m);
  1698. }
  1699. }
  1700. static int s_show(struct seq_file *m, void *v)
  1701. {
  1702. struct trace_iterator *iter = v;
  1703. int ret;
  1704. if (iter->ent == NULL) {
  1705. if (iter->tr) {
  1706. seq_printf(m, "# tracer: %s\n", iter->trace->name);
  1707. seq_puts(m, "#\n");
  1708. }
  1709. if (iter->trace && iter->trace->print_header)
  1710. iter->trace->print_header(m);
  1711. else
  1712. trace_default_header(m);
  1713. } else if (iter->leftover) {
  1714. /*
  1715. * If we filled the seq_file buffer earlier, we
  1716. * want to just show it now.
  1717. */
  1718. ret = trace_print_seq(m, &iter->seq);
  1719. /* ret should this time be zero, but you never know */
  1720. iter->leftover = ret;
  1721. } else {
  1722. print_trace_line(iter);
  1723. ret = trace_print_seq(m, &iter->seq);
  1724. /*
  1725. * If we overflow the seq_file buffer, then it will
  1726. * ask us for this data again at start up.
  1727. * Use that instead.
  1728. * ret is 0 if seq_file write succeeded.
  1729. * -1 otherwise.
  1730. */
  1731. iter->leftover = ret;
  1732. }
  1733. return 0;
  1734. }
  1735. static const struct seq_operations tracer_seq_ops = {
  1736. .start = s_start,
  1737. .next = s_next,
  1738. .stop = s_stop,
  1739. .show = s_show,
  1740. };
  1741. static struct trace_iterator *
  1742. __tracing_open(struct inode *inode, struct file *file)
  1743. {
  1744. long cpu_file = (long) inode->i_private;
  1745. void *fail_ret = ERR_PTR(-ENOMEM);
  1746. struct trace_iterator *iter;
  1747. struct seq_file *m;
  1748. int cpu, ret;
  1749. if (tracing_disabled)
  1750. return ERR_PTR(-ENODEV);
  1751. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  1752. if (!iter)
  1753. return ERR_PTR(-ENOMEM);
  1754. /*
  1755. * We make a copy of the current tracer to avoid concurrent
  1756. * changes on it while we are reading.
  1757. */
  1758. mutex_lock(&trace_types_lock);
  1759. iter->trace = kzalloc(sizeof(*iter->trace), GFP_KERNEL);
  1760. if (!iter->trace)
  1761. goto fail;
  1762. if (current_trace)
  1763. *iter->trace = *current_trace;
  1764. if (!zalloc_cpumask_var(&iter->started, GFP_KERNEL))
  1765. goto fail;
  1766. if (current_trace && current_trace->print_max)
  1767. iter->tr = &max_tr;
  1768. else
  1769. iter->tr = &global_trace;
  1770. iter->pos = -1;
  1771. mutex_init(&iter->mutex);
  1772. iter->cpu_file = cpu_file;
  1773. /* Notify the tracer early; before we stop tracing. */
  1774. if (iter->trace && iter->trace->open)
  1775. iter->trace->open(iter);
  1776. /* Annotate start of buffers if we had overruns */
  1777. if (ring_buffer_overruns(iter->tr->buffer))
  1778. iter->iter_flags |= TRACE_FILE_ANNOTATE;
  1779. /* stop the trace while dumping */
  1780. tracing_stop();
  1781. if (iter->cpu_file == TRACE_PIPE_ALL_CPU) {
  1782. for_each_tracing_cpu(cpu) {
  1783. iter->buffer_iter[cpu] =
  1784. ring_buffer_read_prepare(iter->tr->buffer, cpu);
  1785. }
  1786. ring_buffer_read_prepare_sync();
  1787. for_each_tracing_cpu(cpu) {
  1788. ring_buffer_read_start(iter->buffer_iter[cpu]);
  1789. tracing_iter_reset(iter, cpu);
  1790. }
  1791. } else {
  1792. cpu = iter->cpu_file;
  1793. iter->buffer_iter[cpu] =
  1794. ring_buffer_read_prepare(iter->tr->buffer, cpu);
  1795. ring_buffer_read_prepare_sync();
  1796. ring_buffer_read_start(iter->buffer_iter[cpu]);
  1797. tracing_iter_reset(iter, cpu);
  1798. }
  1799. ret = seq_open(file, &tracer_seq_ops);
  1800. if (ret < 0) {
  1801. fail_ret = ERR_PTR(ret);
  1802. goto fail_buffer;
  1803. }
  1804. m = file->private_data;
  1805. m->private = iter;
  1806. mutex_unlock(&trace_types_lock);
  1807. return iter;
  1808. fail_buffer:
  1809. for_each_tracing_cpu(cpu) {
  1810. if (iter->buffer_iter[cpu])
  1811. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1812. }
  1813. free_cpumask_var(iter->started);
  1814. tracing_start();
  1815. fail:
  1816. mutex_unlock(&trace_types_lock);
  1817. kfree(iter->trace);
  1818. kfree(iter);
  1819. return fail_ret;
  1820. }
  1821. int tracing_open_generic(struct inode *inode, struct file *filp)
  1822. {
  1823. if (tracing_disabled)
  1824. return -ENODEV;
  1825. filp->private_data = inode->i_private;
  1826. return 0;
  1827. }
  1828. static int tracing_release(struct inode *inode, struct file *file)
  1829. {
  1830. struct seq_file *m = file->private_data;
  1831. struct trace_iterator *iter;
  1832. int cpu;
  1833. if (!(file->f_mode & FMODE_READ))
  1834. return 0;
  1835. iter = m->private;
  1836. mutex_lock(&trace_types_lock);
  1837. for_each_tracing_cpu(cpu) {
  1838. if (iter->buffer_iter[cpu])
  1839. ring_buffer_read_finish(iter->buffer_iter[cpu]);
  1840. }
  1841. if (iter->trace && iter->trace->close)
  1842. iter->trace->close(iter);
  1843. /* reenable tracing if it was previously enabled */
  1844. tracing_start();
  1845. mutex_unlock(&trace_types_lock);
  1846. seq_release(inode, file);
  1847. mutex_destroy(&iter->mutex);
  1848. free_cpumask_var(iter->started);
  1849. kfree(iter->trace);
  1850. kfree(iter);
  1851. return 0;
  1852. }
  1853. static int tracing_open(struct inode *inode, struct file *file)
  1854. {
  1855. struct trace_iterator *iter;
  1856. int ret = 0;
  1857. /* If this file was open for write, then erase contents */
  1858. if ((file->f_mode & FMODE_WRITE) &&
  1859. (file->f_flags & O_TRUNC)) {
  1860. long cpu = (long) inode->i_private;
  1861. if (cpu == TRACE_PIPE_ALL_CPU)
  1862. tracing_reset_online_cpus(&global_trace);
  1863. else
  1864. tracing_reset(&global_trace, cpu);
  1865. }
  1866. if (file->f_mode & FMODE_READ) {
  1867. iter = __tracing_open(inode, file);
  1868. if (IS_ERR(iter))
  1869. ret = PTR_ERR(iter);
  1870. else if (trace_flags & TRACE_ITER_LATENCY_FMT)
  1871. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  1872. }
  1873. return ret;
  1874. }
  1875. static void *
  1876. t_next(struct seq_file *m, void *v, loff_t *pos)
  1877. {
  1878. struct tracer *t = v;
  1879. (*pos)++;
  1880. if (t)
  1881. t = t->next;
  1882. return t;
  1883. }
  1884. static void *t_start(struct seq_file *m, loff_t *pos)
  1885. {
  1886. struct tracer *t;
  1887. loff_t l = 0;
  1888. mutex_lock(&trace_types_lock);
  1889. for (t = trace_types; t && l < *pos; t = t_next(m, t, &l))
  1890. ;
  1891. return t;
  1892. }
  1893. static void t_stop(struct seq_file *m, void *p)
  1894. {
  1895. mutex_unlock(&trace_types_lock);
  1896. }
  1897. static int t_show(struct seq_file *m, void *v)
  1898. {
  1899. struct tracer *t = v;
  1900. if (!t)
  1901. return 0;
  1902. seq_printf(m, "%s", t->name);
  1903. if (t->next)
  1904. seq_putc(m, ' ');
  1905. else
  1906. seq_putc(m, '\n');
  1907. return 0;
  1908. }
  1909. static const struct seq_operations show_traces_seq_ops = {
  1910. .start = t_start,
  1911. .next = t_next,
  1912. .stop = t_stop,
  1913. .show = t_show,
  1914. };
  1915. static int show_traces_open(struct inode *inode, struct file *file)
  1916. {
  1917. if (tracing_disabled)
  1918. return -ENODEV;
  1919. return seq_open(file, &show_traces_seq_ops);
  1920. }
  1921. static ssize_t
  1922. tracing_write_stub(struct file *filp, const char __user *ubuf,
  1923. size_t count, loff_t *ppos)
  1924. {
  1925. return count;
  1926. }
  1927. static loff_t tracing_seek(struct file *file, loff_t offset, int origin)
  1928. {
  1929. if (file->f_mode & FMODE_READ)
  1930. return seq_lseek(file, offset, origin);
  1931. else
  1932. return 0;
  1933. }
  1934. static const struct file_operations tracing_fops = {
  1935. .open = tracing_open,
  1936. .read = seq_read,
  1937. .write = tracing_write_stub,
  1938. .llseek = tracing_seek,
  1939. .release = tracing_release,
  1940. };
  1941. static const struct file_operations show_traces_fops = {
  1942. .open = show_traces_open,
  1943. .read = seq_read,
  1944. .release = seq_release,
  1945. .llseek = seq_lseek,
  1946. };
  1947. /*
  1948. * Only trace on a CPU if the bitmask is set:
  1949. */
  1950. static cpumask_var_t tracing_cpumask;
  1951. /*
  1952. * The tracer itself will not take this lock, but still we want
  1953. * to provide a consistent cpumask to user-space:
  1954. */
  1955. static DEFINE_MUTEX(tracing_cpumask_update_lock);
  1956. /*
  1957. * Temporary storage for the character representation of the
  1958. * CPU bitmask (and one more byte for the newline):
  1959. */
  1960. static char mask_str[NR_CPUS + 1];
  1961. static ssize_t
  1962. tracing_cpumask_read(struct file *filp, char __user *ubuf,
  1963. size_t count, loff_t *ppos)
  1964. {
  1965. int len;
  1966. mutex_lock(&tracing_cpumask_update_lock);
  1967. len = cpumask_scnprintf(mask_str, count, tracing_cpumask);
  1968. if (count - len < 2) {
  1969. count = -EINVAL;
  1970. goto out_err;
  1971. }
  1972. len += sprintf(mask_str + len, "\n");
  1973. count = simple_read_from_buffer(ubuf, count, ppos, mask_str, NR_CPUS+1);
  1974. out_err:
  1975. mutex_unlock(&tracing_cpumask_update_lock);
  1976. return count;
  1977. }
  1978. static ssize_t
  1979. tracing_cpumask_write(struct file *filp, const char __user *ubuf,
  1980. size_t count, loff_t *ppos)
  1981. {
  1982. int err, cpu;
  1983. cpumask_var_t tracing_cpumask_new;
  1984. if (!alloc_cpumask_var(&tracing_cpumask_new, GFP_KERNEL))
  1985. return -ENOMEM;
  1986. err = cpumask_parse_user(ubuf, count, tracing_cpumask_new);
  1987. if (err)
  1988. goto err_unlock;
  1989. mutex_lock(&tracing_cpumask_update_lock);
  1990. local_irq_disable();
  1991. arch_spin_lock(&ftrace_max_lock);
  1992. for_each_tracing_cpu(cpu) {
  1993. /*
  1994. * Increase/decrease the disabled counter if we are
  1995. * about to flip a bit in the cpumask:
  1996. */
  1997. if (cpumask_test_cpu(cpu, tracing_cpumask) &&
  1998. !cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  1999. atomic_inc(&global_trace.data[cpu]->disabled);
  2000. }
  2001. if (!cpumask_test_cpu(cpu, tracing_cpumask) &&
  2002. cpumask_test_cpu(cpu, tracing_cpumask_new)) {
  2003. atomic_dec(&global_trace.data[cpu]->disabled);
  2004. }
  2005. }
  2006. arch_spin_unlock(&ftrace_max_lock);
  2007. local_irq_enable();
  2008. cpumask_copy(tracing_cpumask, tracing_cpumask_new);
  2009. mutex_unlock(&tracing_cpumask_update_lock);
  2010. free_cpumask_var(tracing_cpumask_new);
  2011. return count;
  2012. err_unlock:
  2013. free_cpumask_var(tracing_cpumask_new);
  2014. return err;
  2015. }
  2016. static const struct file_operations tracing_cpumask_fops = {
  2017. .open = tracing_open_generic,
  2018. .read = tracing_cpumask_read,
  2019. .write = tracing_cpumask_write,
  2020. .llseek = generic_file_llseek,
  2021. };
  2022. static int tracing_trace_options_show(struct seq_file *m, void *v)
  2023. {
  2024. struct tracer_opt *trace_opts;
  2025. u32 tracer_flags;
  2026. int i;
  2027. mutex_lock(&trace_types_lock);
  2028. tracer_flags = current_trace->flags->val;
  2029. trace_opts = current_trace->flags->opts;
  2030. for (i = 0; trace_options[i]; i++) {
  2031. if (trace_flags & (1 << i))
  2032. seq_printf(m, "%s\n", trace_options[i]);
  2033. else
  2034. seq_printf(m, "no%s\n", trace_options[i]);
  2035. }
  2036. for (i = 0; trace_opts[i].name; i++) {
  2037. if (tracer_flags & trace_opts[i].bit)
  2038. seq_printf(m, "%s\n", trace_opts[i].name);
  2039. else
  2040. seq_printf(m, "no%s\n", trace_opts[i].name);
  2041. }
  2042. mutex_unlock(&trace_types_lock);
  2043. return 0;
  2044. }
  2045. static int __set_tracer_option(struct tracer *trace,
  2046. struct tracer_flags *tracer_flags,
  2047. struct tracer_opt *opts, int neg)
  2048. {
  2049. int ret;
  2050. ret = trace->set_flag(tracer_flags->val, opts->bit, !neg);
  2051. if (ret)
  2052. return ret;
  2053. if (neg)
  2054. tracer_flags->val &= ~opts->bit;
  2055. else
  2056. tracer_flags->val |= opts->bit;
  2057. return 0;
  2058. }
  2059. /* Try to assign a tracer specific option */
  2060. static int set_tracer_option(struct tracer *trace, char *cmp, int neg)
  2061. {
  2062. struct tracer_flags *tracer_flags = trace->flags;
  2063. struct tracer_opt *opts = NULL;
  2064. int i;
  2065. for (i = 0; tracer_flags->opts[i].name; i++) {
  2066. opts = &tracer_flags->opts[i];
  2067. if (strcmp(cmp, opts->name) == 0)
  2068. return __set_tracer_option(trace, trace->flags,
  2069. opts, neg);
  2070. }
  2071. return -EINVAL;
  2072. }
  2073. static void set_tracer_flags(unsigned int mask, int enabled)
  2074. {
  2075. /* do nothing if flag is already set */
  2076. if (!!(trace_flags & mask) == !!enabled)
  2077. return;
  2078. if (enabled)
  2079. trace_flags |= mask;
  2080. else
  2081. trace_flags &= ~mask;
  2082. if (mask == TRACE_ITER_RECORD_CMD)
  2083. trace_event_enable_cmd_record(enabled);
  2084. if (mask == TRACE_ITER_OVERWRITE)
  2085. ring_buffer_change_overwrite(global_trace.buffer, enabled);
  2086. }
  2087. static ssize_t
  2088. tracing_trace_options_write(struct file *filp, const char __user *ubuf,
  2089. size_t cnt, loff_t *ppos)
  2090. {
  2091. char buf[64];
  2092. char *cmp;
  2093. int neg = 0;
  2094. int ret;
  2095. int i;
  2096. if (cnt >= sizeof(buf))
  2097. return -EINVAL;
  2098. if (copy_from_user(&buf, ubuf, cnt))
  2099. return -EFAULT;
  2100. buf[cnt] = 0;
  2101. cmp = strstrip(buf);
  2102. if (strncmp(cmp, "no", 2) == 0) {
  2103. neg = 1;
  2104. cmp += 2;
  2105. }
  2106. for (i = 0; trace_options[i]; i++) {
  2107. if (strcmp(cmp, trace_options[i]) == 0) {
  2108. set_tracer_flags(1 << i, !neg);
  2109. break;
  2110. }
  2111. }
  2112. /* If no option could be set, test the specific tracer options */
  2113. if (!trace_options[i]) {
  2114. mutex_lock(&trace_types_lock);
  2115. ret = set_tracer_option(current_trace, cmp, neg);
  2116. mutex_unlock(&trace_types_lock);
  2117. if (ret)
  2118. return ret;
  2119. }
  2120. *ppos += cnt;
  2121. return cnt;
  2122. }
  2123. static int tracing_trace_options_open(struct inode *inode, struct file *file)
  2124. {
  2125. if (tracing_disabled)
  2126. return -ENODEV;
  2127. return single_open(file, tracing_trace_options_show, NULL);
  2128. }
  2129. static const struct file_operations tracing_iter_fops = {
  2130. .open = tracing_trace_options_open,
  2131. .read = seq_read,
  2132. .llseek = seq_lseek,
  2133. .release = single_release,
  2134. .write = tracing_trace_options_write,
  2135. };
  2136. static const char readme_msg[] =
  2137. "tracing mini-HOWTO:\n\n"
  2138. "# mount -t debugfs nodev /sys/kernel/debug\n\n"
  2139. "# cat /sys/kernel/debug/tracing/available_tracers\n"
  2140. "wakeup preemptirqsoff preemptoff irqsoff function sched_switch nop\n\n"
  2141. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2142. "nop\n"
  2143. "# echo sched_switch > /sys/kernel/debug/tracing/current_tracer\n"
  2144. "# cat /sys/kernel/debug/tracing/current_tracer\n"
  2145. "sched_switch\n"
  2146. "# cat /sys/kernel/debug/tracing/trace_options\n"
  2147. "noprint-parent nosym-offset nosym-addr noverbose\n"
  2148. "# echo print-parent > /sys/kernel/debug/tracing/trace_options\n"
  2149. "# echo 1 > /sys/kernel/debug/tracing/tracing_enabled\n"
  2150. "# cat /sys/kernel/debug/tracing/trace > /tmp/trace.txt\n"
  2151. "# echo 0 > /sys/kernel/debug/tracing/tracing_enabled\n"
  2152. ;
  2153. static ssize_t
  2154. tracing_readme_read(struct file *filp, char __user *ubuf,
  2155. size_t cnt, loff_t *ppos)
  2156. {
  2157. return simple_read_from_buffer(ubuf, cnt, ppos,
  2158. readme_msg, strlen(readme_msg));
  2159. }
  2160. static const struct file_operations tracing_readme_fops = {
  2161. .open = tracing_open_generic,
  2162. .read = tracing_readme_read,
  2163. .llseek = generic_file_llseek,
  2164. };
  2165. static ssize_t
  2166. tracing_saved_cmdlines_read(struct file *file, char __user *ubuf,
  2167. size_t cnt, loff_t *ppos)
  2168. {
  2169. char *buf_comm;
  2170. char *file_buf;
  2171. char *buf;
  2172. int len = 0;
  2173. int pid;
  2174. int i;
  2175. file_buf = kmalloc(SAVED_CMDLINES*(16+TASK_COMM_LEN), GFP_KERNEL);
  2176. if (!file_buf)
  2177. return -ENOMEM;
  2178. buf_comm = kmalloc(TASK_COMM_LEN, GFP_KERNEL);
  2179. if (!buf_comm) {
  2180. kfree(file_buf);
  2181. return -ENOMEM;
  2182. }
  2183. buf = file_buf;
  2184. for (i = 0; i < SAVED_CMDLINES; i++) {
  2185. int r;
  2186. pid = map_cmdline_to_pid[i];
  2187. if (pid == -1 || pid == NO_CMDLINE_MAP)
  2188. continue;
  2189. trace_find_cmdline(pid, buf_comm);
  2190. r = sprintf(buf, "%d %s\n", pid, buf_comm);
  2191. buf += r;
  2192. len += r;
  2193. }
  2194. len = simple_read_from_buffer(ubuf, cnt, ppos,
  2195. file_buf, len);
  2196. kfree(file_buf);
  2197. kfree(buf_comm);
  2198. return len;
  2199. }
  2200. static const struct file_operations tracing_saved_cmdlines_fops = {
  2201. .open = tracing_open_generic,
  2202. .read = tracing_saved_cmdlines_read,
  2203. .llseek = generic_file_llseek,
  2204. };
  2205. static ssize_t
  2206. tracing_ctrl_read(struct file *filp, char __user *ubuf,
  2207. size_t cnt, loff_t *ppos)
  2208. {
  2209. char buf[64];
  2210. int r;
  2211. r = sprintf(buf, "%u\n", tracer_enabled);
  2212. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2213. }
  2214. static ssize_t
  2215. tracing_ctrl_write(struct file *filp, const char __user *ubuf,
  2216. size_t cnt, loff_t *ppos)
  2217. {
  2218. struct trace_array *tr = filp->private_data;
  2219. char buf[64];
  2220. unsigned long val;
  2221. int ret;
  2222. if (cnt >= sizeof(buf))
  2223. return -EINVAL;
  2224. if (copy_from_user(&buf, ubuf, cnt))
  2225. return -EFAULT;
  2226. buf[cnt] = 0;
  2227. ret = strict_strtoul(buf, 10, &val);
  2228. if (ret < 0)
  2229. return ret;
  2230. val = !!val;
  2231. mutex_lock(&trace_types_lock);
  2232. if (tracer_enabled ^ val) {
  2233. /* Only need to warn if this is used to change the state */
  2234. WARN_ONCE(1, "tracing_enabled is deprecated. Use tracing_on");
  2235. if (val) {
  2236. tracer_enabled = 1;
  2237. if (current_trace->start)
  2238. current_trace->start(tr);
  2239. tracing_start();
  2240. } else {
  2241. tracer_enabled = 0;
  2242. tracing_stop();
  2243. if (current_trace->stop)
  2244. current_trace->stop(tr);
  2245. }
  2246. }
  2247. mutex_unlock(&trace_types_lock);
  2248. *ppos += cnt;
  2249. return cnt;
  2250. }
  2251. static ssize_t
  2252. tracing_set_trace_read(struct file *filp, char __user *ubuf,
  2253. size_t cnt, loff_t *ppos)
  2254. {
  2255. char buf[MAX_TRACER_SIZE+2];
  2256. int r;
  2257. mutex_lock(&trace_types_lock);
  2258. if (current_trace)
  2259. r = sprintf(buf, "%s\n", current_trace->name);
  2260. else
  2261. r = sprintf(buf, "\n");
  2262. mutex_unlock(&trace_types_lock);
  2263. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2264. }
  2265. int tracer_init(struct tracer *t, struct trace_array *tr)
  2266. {
  2267. tracing_reset_online_cpus(tr);
  2268. return t->init(tr);
  2269. }
  2270. static int tracing_resize_ring_buffer(unsigned long size)
  2271. {
  2272. int ret;
  2273. /*
  2274. * If kernel or user changes the size of the ring buffer
  2275. * we use the size that was given, and we can forget about
  2276. * expanding it later.
  2277. */
  2278. ring_buffer_expanded = 1;
  2279. ret = ring_buffer_resize(global_trace.buffer, size);
  2280. if (ret < 0)
  2281. return ret;
  2282. if (!current_trace->use_max_tr)
  2283. goto out;
  2284. ret = ring_buffer_resize(max_tr.buffer, size);
  2285. if (ret < 0) {
  2286. int r;
  2287. r = ring_buffer_resize(global_trace.buffer,
  2288. global_trace.entries);
  2289. if (r < 0) {
  2290. /*
  2291. * AARGH! We are left with different
  2292. * size max buffer!!!!
  2293. * The max buffer is our "snapshot" buffer.
  2294. * When a tracer needs a snapshot (one of the
  2295. * latency tracers), it swaps the max buffer
  2296. * with the saved snap shot. We succeeded to
  2297. * update the size of the main buffer, but failed to
  2298. * update the size of the max buffer. But when we tried
  2299. * to reset the main buffer to the original size, we
  2300. * failed there too. This is very unlikely to
  2301. * happen, but if it does, warn and kill all
  2302. * tracing.
  2303. */
  2304. WARN_ON(1);
  2305. tracing_disabled = 1;
  2306. }
  2307. return ret;
  2308. }
  2309. max_tr.entries = size;
  2310. out:
  2311. global_trace.entries = size;
  2312. return ret;
  2313. }
  2314. /**
  2315. * tracing_update_buffers - used by tracing facility to expand ring buffers
  2316. *
  2317. * To save on memory when the tracing is never used on a system with it
  2318. * configured in. The ring buffers are set to a minimum size. But once
  2319. * a user starts to use the tracing facility, then they need to grow
  2320. * to their default size.
  2321. *
  2322. * This function is to be called when a tracer is about to be used.
  2323. */
  2324. int tracing_update_buffers(void)
  2325. {
  2326. int ret = 0;
  2327. mutex_lock(&trace_types_lock);
  2328. if (!ring_buffer_expanded)
  2329. ret = tracing_resize_ring_buffer(trace_buf_size);
  2330. mutex_unlock(&trace_types_lock);
  2331. return ret;
  2332. }
  2333. struct trace_option_dentry;
  2334. static struct trace_option_dentry *
  2335. create_trace_option_files(struct tracer *tracer);
  2336. static void
  2337. destroy_trace_option_files(struct trace_option_dentry *topts);
  2338. static int tracing_set_tracer(const char *buf)
  2339. {
  2340. static struct trace_option_dentry *topts;
  2341. struct trace_array *tr = &global_trace;
  2342. struct tracer *t;
  2343. int ret = 0;
  2344. mutex_lock(&trace_types_lock);
  2345. if (!ring_buffer_expanded) {
  2346. ret = tracing_resize_ring_buffer(trace_buf_size);
  2347. if (ret < 0)
  2348. goto out;
  2349. ret = 0;
  2350. }
  2351. for (t = trace_types; t; t = t->next) {
  2352. if (strcmp(t->name, buf) == 0)
  2353. break;
  2354. }
  2355. if (!t) {
  2356. ret = -EINVAL;
  2357. goto out;
  2358. }
  2359. if (t == current_trace)
  2360. goto out;
  2361. trace_branch_disable();
  2362. if (current_trace && current_trace->reset)
  2363. current_trace->reset(tr);
  2364. if (current_trace && current_trace->use_max_tr) {
  2365. /*
  2366. * We don't free the ring buffer. instead, resize it because
  2367. * The max_tr ring buffer has some state (e.g. ring->clock) and
  2368. * we want preserve it.
  2369. */
  2370. ring_buffer_resize(max_tr.buffer, 1);
  2371. max_tr.entries = 1;
  2372. }
  2373. destroy_trace_option_files(topts);
  2374. current_trace = t;
  2375. topts = create_trace_option_files(current_trace);
  2376. if (current_trace->use_max_tr) {
  2377. ret = ring_buffer_resize(max_tr.buffer, global_trace.entries);
  2378. if (ret < 0)
  2379. goto out;
  2380. max_tr.entries = global_trace.entries;
  2381. }
  2382. if (t->init) {
  2383. ret = tracer_init(t, tr);
  2384. if (ret)
  2385. goto out;
  2386. }
  2387. trace_branch_enable(tr);
  2388. out:
  2389. mutex_unlock(&trace_types_lock);
  2390. return ret;
  2391. }
  2392. static ssize_t
  2393. tracing_set_trace_write(struct file *filp, const char __user *ubuf,
  2394. size_t cnt, loff_t *ppos)
  2395. {
  2396. char buf[MAX_TRACER_SIZE+1];
  2397. int i;
  2398. size_t ret;
  2399. int err;
  2400. ret = cnt;
  2401. if (cnt > MAX_TRACER_SIZE)
  2402. cnt = MAX_TRACER_SIZE;
  2403. if (copy_from_user(&buf, ubuf, cnt))
  2404. return -EFAULT;
  2405. buf[cnt] = 0;
  2406. /* strip ending whitespace. */
  2407. for (i = cnt - 1; i > 0 && isspace(buf[i]); i--)
  2408. buf[i] = 0;
  2409. err = tracing_set_tracer(buf);
  2410. if (err)
  2411. return err;
  2412. *ppos += ret;
  2413. return ret;
  2414. }
  2415. static ssize_t
  2416. tracing_max_lat_read(struct file *filp, char __user *ubuf,
  2417. size_t cnt, loff_t *ppos)
  2418. {
  2419. unsigned long *ptr = filp->private_data;
  2420. char buf[64];
  2421. int r;
  2422. r = snprintf(buf, sizeof(buf), "%ld\n",
  2423. *ptr == (unsigned long)-1 ? -1 : nsecs_to_usecs(*ptr));
  2424. if (r > sizeof(buf))
  2425. r = sizeof(buf);
  2426. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2427. }
  2428. static ssize_t
  2429. tracing_max_lat_write(struct file *filp, const char __user *ubuf,
  2430. size_t cnt, loff_t *ppos)
  2431. {
  2432. unsigned long *ptr = filp->private_data;
  2433. char buf[64];
  2434. unsigned long val;
  2435. int ret;
  2436. if (cnt >= sizeof(buf))
  2437. return -EINVAL;
  2438. if (copy_from_user(&buf, ubuf, cnt))
  2439. return -EFAULT;
  2440. buf[cnt] = 0;
  2441. ret = strict_strtoul(buf, 10, &val);
  2442. if (ret < 0)
  2443. return ret;
  2444. *ptr = val * 1000;
  2445. return cnt;
  2446. }
  2447. static int tracing_open_pipe(struct inode *inode, struct file *filp)
  2448. {
  2449. long cpu_file = (long) inode->i_private;
  2450. struct trace_iterator *iter;
  2451. int ret = 0;
  2452. if (tracing_disabled)
  2453. return -ENODEV;
  2454. mutex_lock(&trace_types_lock);
  2455. /* create a buffer to store the information to pass to userspace */
  2456. iter = kzalloc(sizeof(*iter), GFP_KERNEL);
  2457. if (!iter) {
  2458. ret = -ENOMEM;
  2459. goto out;
  2460. }
  2461. /*
  2462. * We make a copy of the current tracer to avoid concurrent
  2463. * changes on it while we are reading.
  2464. */
  2465. iter->trace = kmalloc(sizeof(*iter->trace), GFP_KERNEL);
  2466. if (!iter->trace) {
  2467. ret = -ENOMEM;
  2468. goto fail;
  2469. }
  2470. if (current_trace)
  2471. *iter->trace = *current_trace;
  2472. if (!alloc_cpumask_var(&iter->started, GFP_KERNEL)) {
  2473. ret = -ENOMEM;
  2474. goto fail;
  2475. }
  2476. /* trace pipe does not show start of buffer */
  2477. cpumask_setall(iter->started);
  2478. if (trace_flags & TRACE_ITER_LATENCY_FMT)
  2479. iter->iter_flags |= TRACE_FILE_LAT_FMT;
  2480. iter->cpu_file = cpu_file;
  2481. iter->tr = &global_trace;
  2482. mutex_init(&iter->mutex);
  2483. filp->private_data = iter;
  2484. if (iter->trace->pipe_open)
  2485. iter->trace->pipe_open(iter);
  2486. nonseekable_open(inode, filp);
  2487. out:
  2488. mutex_unlock(&trace_types_lock);
  2489. return ret;
  2490. fail:
  2491. kfree(iter->trace);
  2492. kfree(iter);
  2493. mutex_unlock(&trace_types_lock);
  2494. return ret;
  2495. }
  2496. static int tracing_release_pipe(struct inode *inode, struct file *file)
  2497. {
  2498. struct trace_iterator *iter = file->private_data;
  2499. mutex_lock(&trace_types_lock);
  2500. if (iter->trace->pipe_close)
  2501. iter->trace->pipe_close(iter);
  2502. mutex_unlock(&trace_types_lock);
  2503. free_cpumask_var(iter->started);
  2504. mutex_destroy(&iter->mutex);
  2505. kfree(iter->trace);
  2506. kfree(iter);
  2507. return 0;
  2508. }
  2509. static unsigned int
  2510. tracing_poll_pipe(struct file *filp, poll_table *poll_table)
  2511. {
  2512. struct trace_iterator *iter = filp->private_data;
  2513. if (trace_flags & TRACE_ITER_BLOCK) {
  2514. /*
  2515. * Always select as readable when in blocking mode
  2516. */
  2517. return POLLIN | POLLRDNORM;
  2518. } else {
  2519. if (!trace_empty(iter))
  2520. return POLLIN | POLLRDNORM;
  2521. poll_wait(filp, &trace_wait, poll_table);
  2522. if (!trace_empty(iter))
  2523. return POLLIN | POLLRDNORM;
  2524. return 0;
  2525. }
  2526. }
  2527. void default_wait_pipe(struct trace_iterator *iter)
  2528. {
  2529. DEFINE_WAIT(wait);
  2530. prepare_to_wait(&trace_wait, &wait, TASK_INTERRUPTIBLE);
  2531. if (trace_empty(iter))
  2532. schedule();
  2533. finish_wait(&trace_wait, &wait);
  2534. }
  2535. /*
  2536. * This is a make-shift waitqueue.
  2537. * A tracer might use this callback on some rare cases:
  2538. *
  2539. * 1) the current tracer might hold the runqueue lock when it wakes up
  2540. * a reader, hence a deadlock (sched, function, and function graph tracers)
  2541. * 2) the function tracers, trace all functions, we don't want
  2542. * the overhead of calling wake_up and friends
  2543. * (and tracing them too)
  2544. *
  2545. * Anyway, this is really very primitive wakeup.
  2546. */
  2547. void poll_wait_pipe(struct trace_iterator *iter)
  2548. {
  2549. set_current_state(TASK_INTERRUPTIBLE);
  2550. /* sleep for 100 msecs, and try again. */
  2551. schedule_timeout(HZ / 10);
  2552. }
  2553. /* Must be called with trace_types_lock mutex held. */
  2554. static int tracing_wait_pipe(struct file *filp)
  2555. {
  2556. struct trace_iterator *iter = filp->private_data;
  2557. while (trace_empty(iter)) {
  2558. if ((filp->f_flags & O_NONBLOCK)) {
  2559. return -EAGAIN;
  2560. }
  2561. mutex_unlock(&iter->mutex);
  2562. iter->trace->wait_pipe(iter);
  2563. mutex_lock(&iter->mutex);
  2564. if (signal_pending(current))
  2565. return -EINTR;
  2566. /*
  2567. * We block until we read something and tracing is disabled.
  2568. * We still block if tracing is disabled, but we have never
  2569. * read anything. This allows a user to cat this file, and
  2570. * then enable tracing. But after we have read something,
  2571. * we give an EOF when tracing is again disabled.
  2572. *
  2573. * iter->pos will be 0 if we haven't read anything.
  2574. */
  2575. if (!tracer_enabled && iter->pos)
  2576. break;
  2577. }
  2578. return 1;
  2579. }
  2580. /*
  2581. * Consumer reader.
  2582. */
  2583. static ssize_t
  2584. tracing_read_pipe(struct file *filp, char __user *ubuf,
  2585. size_t cnt, loff_t *ppos)
  2586. {
  2587. struct trace_iterator *iter = filp->private_data;
  2588. static struct tracer *old_tracer;
  2589. ssize_t sret;
  2590. /* return any leftover data */
  2591. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2592. if (sret != -EBUSY)
  2593. return sret;
  2594. trace_seq_init(&iter->seq);
  2595. /* copy the tracer to avoid using a global lock all around */
  2596. mutex_lock(&trace_types_lock);
  2597. if (unlikely(old_tracer != current_trace && current_trace)) {
  2598. old_tracer = current_trace;
  2599. *iter->trace = *current_trace;
  2600. }
  2601. mutex_unlock(&trace_types_lock);
  2602. /*
  2603. * Avoid more than one consumer on a single file descriptor
  2604. * This is just a matter of traces coherency, the ring buffer itself
  2605. * is protected.
  2606. */
  2607. mutex_lock(&iter->mutex);
  2608. if (iter->trace->read) {
  2609. sret = iter->trace->read(iter, filp, ubuf, cnt, ppos);
  2610. if (sret)
  2611. goto out;
  2612. }
  2613. waitagain:
  2614. sret = tracing_wait_pipe(filp);
  2615. if (sret <= 0)
  2616. goto out;
  2617. /* stop when tracing is finished */
  2618. if (trace_empty(iter)) {
  2619. sret = 0;
  2620. goto out;
  2621. }
  2622. if (cnt >= PAGE_SIZE)
  2623. cnt = PAGE_SIZE - 1;
  2624. /* reset all but tr, trace, and overruns */
  2625. memset(&iter->seq, 0,
  2626. sizeof(struct trace_iterator) -
  2627. offsetof(struct trace_iterator, seq));
  2628. iter->pos = -1;
  2629. trace_event_read_lock();
  2630. trace_access_lock(iter->cpu_file);
  2631. while (trace_find_next_entry_inc(iter) != NULL) {
  2632. enum print_line_t ret;
  2633. int len = iter->seq.len;
  2634. ret = print_trace_line(iter);
  2635. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2636. /* don't print partial lines */
  2637. iter->seq.len = len;
  2638. break;
  2639. }
  2640. if (ret != TRACE_TYPE_NO_CONSUME)
  2641. trace_consume(iter);
  2642. if (iter->seq.len >= cnt)
  2643. break;
  2644. }
  2645. trace_access_unlock(iter->cpu_file);
  2646. trace_event_read_unlock();
  2647. /* Now copy what we have to the user */
  2648. sret = trace_seq_to_user(&iter->seq, ubuf, cnt);
  2649. if (iter->seq.readpos >= iter->seq.len)
  2650. trace_seq_init(&iter->seq);
  2651. /*
  2652. * If there was nothing to send to user, inspite of consuming trace
  2653. * entries, go back to wait for more entries.
  2654. */
  2655. if (sret == -EBUSY)
  2656. goto waitagain;
  2657. out:
  2658. mutex_unlock(&iter->mutex);
  2659. return sret;
  2660. }
  2661. static void tracing_pipe_buf_release(struct pipe_inode_info *pipe,
  2662. struct pipe_buffer *buf)
  2663. {
  2664. __free_page(buf->page);
  2665. }
  2666. static void tracing_spd_release_pipe(struct splice_pipe_desc *spd,
  2667. unsigned int idx)
  2668. {
  2669. __free_page(spd->pages[idx]);
  2670. }
  2671. static const struct pipe_buf_operations tracing_pipe_buf_ops = {
  2672. .can_merge = 0,
  2673. .map = generic_pipe_buf_map,
  2674. .unmap = generic_pipe_buf_unmap,
  2675. .confirm = generic_pipe_buf_confirm,
  2676. .release = tracing_pipe_buf_release,
  2677. .steal = generic_pipe_buf_steal,
  2678. .get = generic_pipe_buf_get,
  2679. };
  2680. static size_t
  2681. tracing_fill_pipe_page(size_t rem, struct trace_iterator *iter)
  2682. {
  2683. size_t count;
  2684. int ret;
  2685. /* Seq buffer is page-sized, exactly what we need. */
  2686. for (;;) {
  2687. count = iter->seq.len;
  2688. ret = print_trace_line(iter);
  2689. count = iter->seq.len - count;
  2690. if (rem < count) {
  2691. rem = 0;
  2692. iter->seq.len -= count;
  2693. break;
  2694. }
  2695. if (ret == TRACE_TYPE_PARTIAL_LINE) {
  2696. iter->seq.len -= count;
  2697. break;
  2698. }
  2699. if (ret != TRACE_TYPE_NO_CONSUME)
  2700. trace_consume(iter);
  2701. rem -= count;
  2702. if (!trace_find_next_entry_inc(iter)) {
  2703. rem = 0;
  2704. iter->ent = NULL;
  2705. break;
  2706. }
  2707. }
  2708. return rem;
  2709. }
  2710. static ssize_t tracing_splice_read_pipe(struct file *filp,
  2711. loff_t *ppos,
  2712. struct pipe_inode_info *pipe,
  2713. size_t len,
  2714. unsigned int flags)
  2715. {
  2716. struct page *pages_def[PIPE_DEF_BUFFERS];
  2717. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  2718. struct trace_iterator *iter = filp->private_data;
  2719. struct splice_pipe_desc spd = {
  2720. .pages = pages_def,
  2721. .partial = partial_def,
  2722. .nr_pages = 0, /* This gets updated below. */
  2723. .flags = flags,
  2724. .ops = &tracing_pipe_buf_ops,
  2725. .spd_release = tracing_spd_release_pipe,
  2726. };
  2727. static struct tracer *old_tracer;
  2728. ssize_t ret;
  2729. size_t rem;
  2730. unsigned int i;
  2731. if (splice_grow_spd(pipe, &spd))
  2732. return -ENOMEM;
  2733. /* copy the tracer to avoid using a global lock all around */
  2734. mutex_lock(&trace_types_lock);
  2735. if (unlikely(old_tracer != current_trace && current_trace)) {
  2736. old_tracer = current_trace;
  2737. *iter->trace = *current_trace;
  2738. }
  2739. mutex_unlock(&trace_types_lock);
  2740. mutex_lock(&iter->mutex);
  2741. if (iter->trace->splice_read) {
  2742. ret = iter->trace->splice_read(iter, filp,
  2743. ppos, pipe, len, flags);
  2744. if (ret)
  2745. goto out_err;
  2746. }
  2747. ret = tracing_wait_pipe(filp);
  2748. if (ret <= 0)
  2749. goto out_err;
  2750. if (!iter->ent && !trace_find_next_entry_inc(iter)) {
  2751. ret = -EFAULT;
  2752. goto out_err;
  2753. }
  2754. trace_event_read_lock();
  2755. trace_access_lock(iter->cpu_file);
  2756. /* Fill as many pages as possible. */
  2757. for (i = 0, rem = len; i < pipe->buffers && rem; i++) {
  2758. spd.pages[i] = alloc_page(GFP_KERNEL);
  2759. if (!spd.pages[i])
  2760. break;
  2761. rem = tracing_fill_pipe_page(rem, iter);
  2762. /* Copy the data into the page, so we can start over. */
  2763. ret = trace_seq_to_buffer(&iter->seq,
  2764. page_address(spd.pages[i]),
  2765. iter->seq.len);
  2766. if (ret < 0) {
  2767. __free_page(spd.pages[i]);
  2768. break;
  2769. }
  2770. spd.partial[i].offset = 0;
  2771. spd.partial[i].len = iter->seq.len;
  2772. trace_seq_init(&iter->seq);
  2773. }
  2774. trace_access_unlock(iter->cpu_file);
  2775. trace_event_read_unlock();
  2776. mutex_unlock(&iter->mutex);
  2777. spd.nr_pages = i;
  2778. ret = splice_to_pipe(pipe, &spd);
  2779. out:
  2780. splice_shrink_spd(pipe, &spd);
  2781. return ret;
  2782. out_err:
  2783. mutex_unlock(&iter->mutex);
  2784. goto out;
  2785. }
  2786. static ssize_t
  2787. tracing_entries_read(struct file *filp, char __user *ubuf,
  2788. size_t cnt, loff_t *ppos)
  2789. {
  2790. struct trace_array *tr = filp->private_data;
  2791. char buf[96];
  2792. int r;
  2793. mutex_lock(&trace_types_lock);
  2794. if (!ring_buffer_expanded)
  2795. r = sprintf(buf, "%lu (expanded: %lu)\n",
  2796. tr->entries >> 10,
  2797. trace_buf_size >> 10);
  2798. else
  2799. r = sprintf(buf, "%lu\n", tr->entries >> 10);
  2800. mutex_unlock(&trace_types_lock);
  2801. return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  2802. }
  2803. static ssize_t
  2804. tracing_entries_write(struct file *filp, const char __user *ubuf,
  2805. size_t cnt, loff_t *ppos)
  2806. {
  2807. unsigned long val;
  2808. char buf[64];
  2809. int ret, cpu;
  2810. if (cnt >= sizeof(buf))
  2811. return -EINVAL;
  2812. if (copy_from_user(&buf, ubuf, cnt))
  2813. return -EFAULT;
  2814. buf[cnt] = 0;
  2815. ret = strict_strtoul(buf, 10, &val);
  2816. if (ret < 0)
  2817. return ret;
  2818. /* must have at least 1 entry */
  2819. if (!val)
  2820. return -EINVAL;
  2821. mutex_lock(&trace_types_lock);
  2822. tracing_stop();
  2823. /* disable all cpu buffers */
  2824. for_each_tracing_cpu(cpu) {
  2825. if (global_trace.data[cpu])
  2826. atomic_inc(&global_trace.data[cpu]->disabled);
  2827. if (max_tr.data[cpu])
  2828. atomic_inc(&max_tr.data[cpu]->disabled);
  2829. }
  2830. /* value is in KB */
  2831. val <<= 10;
  2832. if (val != global_trace.entries) {
  2833. ret = tracing_resize_ring_buffer(val);
  2834. if (ret < 0) {
  2835. cnt = ret;
  2836. goto out;
  2837. }
  2838. }
  2839. *ppos += cnt;
  2840. /* If check pages failed, return ENOMEM */
  2841. if (tracing_disabled)
  2842. cnt = -ENOMEM;
  2843. out:
  2844. for_each_tracing_cpu(cpu) {
  2845. if (global_trace.data[cpu])
  2846. atomic_dec(&global_trace.data[cpu]->disabled);
  2847. if (max_tr.data[cpu])
  2848. atomic_dec(&max_tr.data[cpu]->disabled);
  2849. }
  2850. tracing_start();
  2851. mutex_unlock(&trace_types_lock);
  2852. return cnt;
  2853. }
  2854. static int mark_printk(const char *fmt, ...)
  2855. {
  2856. int ret;
  2857. va_list args;
  2858. va_start(args, fmt);
  2859. ret = trace_vprintk(0, fmt, args);
  2860. va_end(args);
  2861. return ret;
  2862. }
  2863. static ssize_t
  2864. tracing_mark_write(struct file *filp, const char __user *ubuf,
  2865. size_t cnt, loff_t *fpos)
  2866. {
  2867. char *buf;
  2868. size_t written;
  2869. if (tracing_disabled)
  2870. return -EINVAL;
  2871. if (cnt > TRACE_BUF_SIZE)
  2872. cnt = TRACE_BUF_SIZE;
  2873. buf = kmalloc(cnt + 2, GFP_KERNEL);
  2874. if (buf == NULL)
  2875. return -ENOMEM;
  2876. if (copy_from_user(buf, ubuf, cnt)) {
  2877. kfree(buf);
  2878. return -EFAULT;
  2879. }
  2880. if (buf[cnt-1] != '\n') {
  2881. buf[cnt] = '\n';
  2882. buf[cnt+1] = '\0';
  2883. } else
  2884. buf[cnt] = '\0';
  2885. written = mark_printk("%s", buf);
  2886. kfree(buf);
  2887. *fpos += written;
  2888. /* don't tell userspace we wrote more - it might confuse them */
  2889. if (written > cnt)
  2890. written = cnt;
  2891. return written;
  2892. }
  2893. static int tracing_clock_show(struct seq_file *m, void *v)
  2894. {
  2895. int i;
  2896. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++)
  2897. seq_printf(m,
  2898. "%s%s%s%s", i ? " " : "",
  2899. i == trace_clock_id ? "[" : "", trace_clocks[i].name,
  2900. i == trace_clock_id ? "]" : "");
  2901. seq_putc(m, '\n');
  2902. return 0;
  2903. }
  2904. static ssize_t tracing_clock_write(struct file *filp, const char __user *ubuf,
  2905. size_t cnt, loff_t *fpos)
  2906. {
  2907. char buf[64];
  2908. const char *clockstr;
  2909. int i;
  2910. if (cnt >= sizeof(buf))
  2911. return -EINVAL;
  2912. if (copy_from_user(&buf, ubuf, cnt))
  2913. return -EFAULT;
  2914. buf[cnt] = 0;
  2915. clockstr = strstrip(buf);
  2916. for (i = 0; i < ARRAY_SIZE(trace_clocks); i++) {
  2917. if (strcmp(trace_clocks[i].name, clockstr) == 0)
  2918. break;
  2919. }
  2920. if (i == ARRAY_SIZE(trace_clocks))
  2921. return -EINVAL;
  2922. trace_clock_id = i;
  2923. mutex_lock(&trace_types_lock);
  2924. ring_buffer_set_clock(global_trace.buffer, trace_clocks[i].func);
  2925. if (max_tr.buffer)
  2926. ring_buffer_set_clock(max_tr.buffer, trace_clocks[i].func);
  2927. mutex_unlock(&trace_types_lock);
  2928. *fpos += cnt;
  2929. return cnt;
  2930. }
  2931. static int tracing_clock_open(struct inode *inode, struct file *file)
  2932. {
  2933. if (tracing_disabled)
  2934. return -ENODEV;
  2935. return single_open(file, tracing_clock_show, NULL);
  2936. }
  2937. static const struct file_operations tracing_max_lat_fops = {
  2938. .open = tracing_open_generic,
  2939. .read = tracing_max_lat_read,
  2940. .write = tracing_max_lat_write,
  2941. .llseek = generic_file_llseek,
  2942. };
  2943. static const struct file_operations tracing_ctrl_fops = {
  2944. .open = tracing_open_generic,
  2945. .read = tracing_ctrl_read,
  2946. .write = tracing_ctrl_write,
  2947. .llseek = generic_file_llseek,
  2948. };
  2949. static const struct file_operations set_tracer_fops = {
  2950. .open = tracing_open_generic,
  2951. .read = tracing_set_trace_read,
  2952. .write = tracing_set_trace_write,
  2953. .llseek = generic_file_llseek,
  2954. };
  2955. static const struct file_operations tracing_pipe_fops = {
  2956. .open = tracing_open_pipe,
  2957. .poll = tracing_poll_pipe,
  2958. .read = tracing_read_pipe,
  2959. .splice_read = tracing_splice_read_pipe,
  2960. .release = tracing_release_pipe,
  2961. .llseek = no_llseek,
  2962. };
  2963. static const struct file_operations tracing_entries_fops = {
  2964. .open = tracing_open_generic,
  2965. .read = tracing_entries_read,
  2966. .write = tracing_entries_write,
  2967. .llseek = generic_file_llseek,
  2968. };
  2969. static const struct file_operations tracing_mark_fops = {
  2970. .open = tracing_open_generic,
  2971. .write = tracing_mark_write,
  2972. .llseek = generic_file_llseek,
  2973. };
  2974. static const struct file_operations trace_clock_fops = {
  2975. .open = tracing_clock_open,
  2976. .read = seq_read,
  2977. .llseek = seq_lseek,
  2978. .release = single_release,
  2979. .write = tracing_clock_write,
  2980. };
  2981. struct ftrace_buffer_info {
  2982. struct trace_array *tr;
  2983. void *spare;
  2984. int cpu;
  2985. unsigned int read;
  2986. };
  2987. static int tracing_buffers_open(struct inode *inode, struct file *filp)
  2988. {
  2989. int cpu = (int)(long)inode->i_private;
  2990. struct ftrace_buffer_info *info;
  2991. if (tracing_disabled)
  2992. return -ENODEV;
  2993. info = kzalloc(sizeof(*info), GFP_KERNEL);
  2994. if (!info)
  2995. return -ENOMEM;
  2996. info->tr = &global_trace;
  2997. info->cpu = cpu;
  2998. info->spare = NULL;
  2999. /* Force reading ring buffer for first read */
  3000. info->read = (unsigned int)-1;
  3001. filp->private_data = info;
  3002. return nonseekable_open(inode, filp);
  3003. }
  3004. static ssize_t
  3005. tracing_buffers_read(struct file *filp, char __user *ubuf,
  3006. size_t count, loff_t *ppos)
  3007. {
  3008. struct ftrace_buffer_info *info = filp->private_data;
  3009. ssize_t ret;
  3010. size_t size;
  3011. if (!count)
  3012. return 0;
  3013. if (!info->spare)
  3014. info->spare = ring_buffer_alloc_read_page(info->tr->buffer);
  3015. if (!info->spare)
  3016. return -ENOMEM;
  3017. /* Do we have previous read data to read? */
  3018. if (info->read < PAGE_SIZE)
  3019. goto read;
  3020. info->read = 0;
  3021. trace_access_lock(info->cpu);
  3022. ret = ring_buffer_read_page(info->tr->buffer,
  3023. &info->spare,
  3024. count,
  3025. info->cpu, 0);
  3026. trace_access_unlock(info->cpu);
  3027. if (ret < 0)
  3028. return 0;
  3029. read:
  3030. size = PAGE_SIZE - info->read;
  3031. if (size > count)
  3032. size = count;
  3033. ret = copy_to_user(ubuf, info->spare + info->read, size);
  3034. if (ret == size)
  3035. return -EFAULT;
  3036. size -= ret;
  3037. *ppos += size;
  3038. info->read += size;
  3039. return size;
  3040. }
  3041. static int tracing_buffers_release(struct inode *inode, struct file *file)
  3042. {
  3043. struct ftrace_buffer_info *info = file->private_data;
  3044. if (info->spare)
  3045. ring_buffer_free_read_page(info->tr->buffer, info->spare);
  3046. kfree(info);
  3047. return 0;
  3048. }
  3049. struct buffer_ref {
  3050. struct ring_buffer *buffer;
  3051. void *page;
  3052. int ref;
  3053. };
  3054. static void buffer_pipe_buf_release(struct pipe_inode_info *pipe,
  3055. struct pipe_buffer *buf)
  3056. {
  3057. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3058. if (--ref->ref)
  3059. return;
  3060. ring_buffer_free_read_page(ref->buffer, ref->page);
  3061. kfree(ref);
  3062. buf->private = 0;
  3063. }
  3064. static int buffer_pipe_buf_steal(struct pipe_inode_info *pipe,
  3065. struct pipe_buffer *buf)
  3066. {
  3067. return 1;
  3068. }
  3069. static void buffer_pipe_buf_get(struct pipe_inode_info *pipe,
  3070. struct pipe_buffer *buf)
  3071. {
  3072. struct buffer_ref *ref = (struct buffer_ref *)buf->private;
  3073. ref->ref++;
  3074. }
  3075. /* Pipe buffer operations for a buffer. */
  3076. static const struct pipe_buf_operations buffer_pipe_buf_ops = {
  3077. .can_merge = 0,
  3078. .map = generic_pipe_buf_map,
  3079. .unmap = generic_pipe_buf_unmap,
  3080. .confirm = generic_pipe_buf_confirm,
  3081. .release = buffer_pipe_buf_release,
  3082. .steal = buffer_pipe_buf_steal,
  3083. .get = buffer_pipe_buf_get,
  3084. };
  3085. /*
  3086. * Callback from splice_to_pipe(), if we need to release some pages
  3087. * at the end of the spd in case we error'ed out in filling the pipe.
  3088. */
  3089. static void buffer_spd_release(struct splice_pipe_desc *spd, unsigned int i)
  3090. {
  3091. struct buffer_ref *ref =
  3092. (struct buffer_ref *)spd->partial[i].private;
  3093. if (--ref->ref)
  3094. return;
  3095. ring_buffer_free_read_page(ref->buffer, ref->page);
  3096. kfree(ref);
  3097. spd->partial[i].private = 0;
  3098. }
  3099. static ssize_t
  3100. tracing_buffers_splice_read(struct file *file, loff_t *ppos,
  3101. struct pipe_inode_info *pipe, size_t len,
  3102. unsigned int flags)
  3103. {
  3104. struct ftrace_buffer_info *info = file->private_data;
  3105. struct partial_page partial_def[PIPE_DEF_BUFFERS];
  3106. struct page *pages_def[PIPE_DEF_BUFFERS];
  3107. struct splice_pipe_desc spd = {
  3108. .pages = pages_def,
  3109. .partial = partial_def,
  3110. .flags = flags,
  3111. .ops = &buffer_pipe_buf_ops,
  3112. .spd_release = buffer_spd_release,
  3113. };
  3114. struct buffer_ref *ref;
  3115. int entries, size, i;
  3116. size_t ret;
  3117. if (splice_grow_spd(pipe, &spd))
  3118. return -ENOMEM;
  3119. if (*ppos & (PAGE_SIZE - 1)) {
  3120. WARN_ONCE(1, "Ftrace: previous read must page-align\n");
  3121. ret = -EINVAL;
  3122. goto out;
  3123. }
  3124. if (len & (PAGE_SIZE - 1)) {
  3125. WARN_ONCE(1, "Ftrace: splice_read should page-align\n");
  3126. if (len < PAGE_SIZE) {
  3127. ret = -EINVAL;
  3128. goto out;
  3129. }
  3130. len &= PAGE_MASK;
  3131. }
  3132. trace_access_lock(info->cpu);
  3133. entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
  3134. for (i = 0; i < pipe->buffers && len && entries; i++, len -= PAGE_SIZE) {
  3135. struct page *page;
  3136. int r;
  3137. ref = kzalloc(sizeof(*ref), GFP_KERNEL);
  3138. if (!ref)
  3139. break;
  3140. ref->ref = 1;
  3141. ref->buffer = info->tr->buffer;
  3142. ref->page = ring_buffer_alloc_read_page(ref->buffer);
  3143. if (!ref->page) {
  3144. kfree(ref);
  3145. break;
  3146. }
  3147. r = ring_buffer_read_page(ref->buffer, &ref->page,
  3148. len, info->cpu, 1);
  3149. if (r < 0) {
  3150. ring_buffer_free_read_page(ref->buffer,
  3151. ref->page);
  3152. kfree(ref);
  3153. break;
  3154. }
  3155. /*
  3156. * zero out any left over data, this is going to
  3157. * user land.
  3158. */
  3159. size = ring_buffer_page_len(ref->page);
  3160. if (size < PAGE_SIZE)
  3161. memset(ref->page + size, 0, PAGE_SIZE - size);
  3162. page = virt_to_page(ref->page);
  3163. spd.pages[i] = page;
  3164. spd.partial[i].len = PAGE_SIZE;
  3165. spd.partial[i].offset = 0;
  3166. spd.partial[i].private = (unsigned long)ref;
  3167. spd.nr_pages++;
  3168. *ppos += PAGE_SIZE;
  3169. entries = ring_buffer_entries_cpu(info->tr->buffer, info->cpu);
  3170. }
  3171. trace_access_unlock(info->cpu);
  3172. spd.nr_pages = i;
  3173. /* did we read anything? */
  3174. if (!spd.nr_pages) {
  3175. if (flags & SPLICE_F_NONBLOCK)
  3176. ret = -EAGAIN;
  3177. else
  3178. ret = 0;
  3179. /* TODO: block */
  3180. goto out;
  3181. }
  3182. ret = splice_to_pipe(pipe, &spd);
  3183. splice_shrink_spd(pipe, &spd);
  3184. out:
  3185. return ret;
  3186. }
  3187. static const struct file_operations tracing_buffers_fops = {
  3188. .open = tracing_buffers_open,
  3189. .read = tracing_buffers_read,
  3190. .release = tracing_buffers_release,
  3191. .splice_read = tracing_buffers_splice_read,
  3192. .llseek = no_llseek,
  3193. };
  3194. static ssize_t
  3195. tracing_stats_read(struct file *filp, char __user *ubuf,
  3196. size_t count, loff_t *ppos)
  3197. {
  3198. unsigned long cpu = (unsigned long)filp->private_data;
  3199. struct trace_array *tr = &global_trace;
  3200. struct trace_seq *s;
  3201. unsigned long cnt;
  3202. s = kmalloc(sizeof(*s), GFP_KERNEL);
  3203. if (!s)
  3204. return -ENOMEM;
  3205. trace_seq_init(s);
  3206. cnt = ring_buffer_entries_cpu(tr->buffer, cpu);
  3207. trace_seq_printf(s, "entries: %ld\n", cnt);
  3208. cnt = ring_buffer_overrun_cpu(tr->buffer, cpu);
  3209. trace_seq_printf(s, "overrun: %ld\n", cnt);
  3210. cnt = ring_buffer_commit_overrun_cpu(tr->buffer, cpu);
  3211. trace_seq_printf(s, "commit overrun: %ld\n", cnt);
  3212. count = simple_read_from_buffer(ubuf, count, ppos, s->buffer, s->len);
  3213. kfree(s);
  3214. return count;
  3215. }
  3216. static const struct file_operations tracing_stats_fops = {
  3217. .open = tracing_open_generic,
  3218. .read = tracing_stats_read,
  3219. .llseek = generic_file_llseek,
  3220. };
  3221. #ifdef CONFIG_DYNAMIC_FTRACE
  3222. int __weak ftrace_arch_read_dyn_info(char *buf, int size)
  3223. {
  3224. return 0;
  3225. }
  3226. static ssize_t
  3227. tracing_read_dyn_info(struct file *filp, char __user *ubuf,
  3228. size_t cnt, loff_t *ppos)
  3229. {
  3230. static char ftrace_dyn_info_buffer[1024];
  3231. static DEFINE_MUTEX(dyn_info_mutex);
  3232. unsigned long *p = filp->private_data;
  3233. char *buf = ftrace_dyn_info_buffer;
  3234. int size = ARRAY_SIZE(ftrace_dyn_info_buffer);
  3235. int r;
  3236. mutex_lock(&dyn_info_mutex);
  3237. r = sprintf(buf, "%ld ", *p);
  3238. r += ftrace_arch_read_dyn_info(buf+r, (size-1)-r);
  3239. buf[r++] = '\n';
  3240. r = simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
  3241. mutex_unlock(&dyn_info_mutex);
  3242. return r;
  3243. }
  3244. static const struct file_operations tracing_dyn_info_fops = {
  3245. .open = tracing_open_generic,
  3246. .read = tracing_read_dyn_info,
  3247. .llseek = generic_file_llseek,
  3248. };
  3249. #endif
  3250. static struct dentry *d_tracer;
  3251. struct dentry *tracing_init_dentry(void)
  3252. {
  3253. static int once;
  3254. if (d_tracer)
  3255. return d_tracer;
  3256. if (!debugfs_initialized())
  3257. return NULL;
  3258. d_tracer = debugfs_create_dir("tracing", NULL);
  3259. if (!d_tracer && !once) {
  3260. once = 1;
  3261. pr_warning("Could not create debugfs directory 'tracing'\n");
  3262. return NULL;
  3263. }
  3264. return d_tracer;
  3265. }
  3266. static struct dentry *d_percpu;
  3267. struct dentry *tracing_dentry_percpu(void)
  3268. {
  3269. static int once;
  3270. struct dentry *d_tracer;
  3271. if (d_percpu)
  3272. return d_percpu;
  3273. d_tracer = tracing_init_dentry();
  3274. if (!d_tracer)
  3275. return NULL;
  3276. d_percpu = debugfs_create_dir("per_cpu", d_tracer);
  3277. if (!d_percpu && !once) {
  3278. once = 1;
  3279. pr_warning("Could not create debugfs directory 'per_cpu'\n");
  3280. return NULL;
  3281. }
  3282. return d_percpu;
  3283. }
  3284. static void tracing_init_debugfs_percpu(long cpu)
  3285. {
  3286. struct dentry *d_percpu = tracing_dentry_percpu();
  3287. struct dentry *d_cpu;
  3288. char cpu_dir[30]; /* 30 characters should be more than enough */
  3289. snprintf(cpu_dir, 30, "cpu%ld", cpu);
  3290. d_cpu = debugfs_create_dir(cpu_dir, d_percpu);
  3291. if (!d_cpu) {
  3292. pr_warning("Could not create debugfs '%s' entry\n", cpu_dir);
  3293. return;
  3294. }
  3295. /* per cpu trace_pipe */
  3296. trace_create_file("trace_pipe", 0444, d_cpu,
  3297. (void *) cpu, &tracing_pipe_fops);
  3298. /* per cpu trace */
  3299. trace_create_file("trace", 0644, d_cpu,
  3300. (void *) cpu, &tracing_fops);
  3301. trace_create_file("trace_pipe_raw", 0444, d_cpu,
  3302. (void *) cpu, &tracing_buffers_fops);
  3303. trace_create_file("stats", 0444, d_cpu,
  3304. (void *) cpu, &tracing_stats_fops);
  3305. }
  3306. #ifdef CONFIG_FTRACE_SELFTEST
  3307. /* Let selftest have access to static functions in this file */
  3308. #include "trace_selftest.c"
  3309. #endif
  3310. struct trace_option_dentry {
  3311. struct tracer_opt *opt;
  3312. struct tracer_flags *flags;
  3313. struct dentry *entry;
  3314. };
  3315. static ssize_t
  3316. trace_options_read(struct file *filp, char __user *ubuf, size_t cnt,
  3317. loff_t *ppos)
  3318. {
  3319. struct trace_option_dentry *topt = filp->private_data;
  3320. char *buf;
  3321. if (topt->flags->val & topt->opt->bit)
  3322. buf = "1\n";
  3323. else
  3324. buf = "0\n";
  3325. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3326. }
  3327. static ssize_t
  3328. trace_options_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3329. loff_t *ppos)
  3330. {
  3331. struct trace_option_dentry *topt = filp->private_data;
  3332. unsigned long val;
  3333. char buf[64];
  3334. int ret;
  3335. if (cnt >= sizeof(buf))
  3336. return -EINVAL;
  3337. if (copy_from_user(&buf, ubuf, cnt))
  3338. return -EFAULT;
  3339. buf[cnt] = 0;
  3340. ret = strict_strtoul(buf, 10, &val);
  3341. if (ret < 0)
  3342. return ret;
  3343. if (val != 0 && val != 1)
  3344. return -EINVAL;
  3345. if (!!(topt->flags->val & topt->opt->bit) != val) {
  3346. mutex_lock(&trace_types_lock);
  3347. ret = __set_tracer_option(current_trace, topt->flags,
  3348. topt->opt, !val);
  3349. mutex_unlock(&trace_types_lock);
  3350. if (ret)
  3351. return ret;
  3352. }
  3353. *ppos += cnt;
  3354. return cnt;
  3355. }
  3356. static const struct file_operations trace_options_fops = {
  3357. .open = tracing_open_generic,
  3358. .read = trace_options_read,
  3359. .write = trace_options_write,
  3360. .llseek = generic_file_llseek,
  3361. };
  3362. static ssize_t
  3363. trace_options_core_read(struct file *filp, char __user *ubuf, size_t cnt,
  3364. loff_t *ppos)
  3365. {
  3366. long index = (long)filp->private_data;
  3367. char *buf;
  3368. if (trace_flags & (1 << index))
  3369. buf = "1\n";
  3370. else
  3371. buf = "0\n";
  3372. return simple_read_from_buffer(ubuf, cnt, ppos, buf, 2);
  3373. }
  3374. static ssize_t
  3375. trace_options_core_write(struct file *filp, const char __user *ubuf, size_t cnt,
  3376. loff_t *ppos)
  3377. {
  3378. long index = (long)filp->private_data;
  3379. char buf[64];
  3380. unsigned long val;
  3381. int ret;
  3382. if (cnt >= sizeof(buf))
  3383. return -EINVAL;
  3384. if (copy_from_user(&buf, ubuf, cnt))
  3385. return -EFAULT;
  3386. buf[cnt] = 0;
  3387. ret = strict_strtoul(buf, 10, &val);
  3388. if (ret < 0)
  3389. return ret;
  3390. if (val != 0 && val != 1)
  3391. return -EINVAL;
  3392. set_tracer_flags(1 << index, val);
  3393. *ppos += cnt;
  3394. return cnt;
  3395. }
  3396. static const struct file_operations trace_options_core_fops = {
  3397. .open = tracing_open_generic,
  3398. .read = trace_options_core_read,
  3399. .write = trace_options_core_write,
  3400. .llseek = generic_file_llseek,
  3401. };
  3402. struct dentry *trace_create_file(const char *name,
  3403. mode_t mode,
  3404. struct dentry *parent,
  3405. void *data,
  3406. const struct file_operations *fops)
  3407. {
  3408. struct dentry *ret;
  3409. ret = debugfs_create_file(name, mode, parent, data, fops);
  3410. if (!ret)
  3411. pr_warning("Could not create debugfs '%s' entry\n", name);
  3412. return ret;
  3413. }
  3414. static struct dentry *trace_options_init_dentry(void)
  3415. {
  3416. struct dentry *d_tracer;
  3417. static struct dentry *t_options;
  3418. if (t_options)
  3419. return t_options;
  3420. d_tracer = tracing_init_dentry();
  3421. if (!d_tracer)
  3422. return NULL;
  3423. t_options = debugfs_create_dir("options", d_tracer);
  3424. if (!t_options) {
  3425. pr_warning("Could not create debugfs directory 'options'\n");
  3426. return NULL;
  3427. }
  3428. return t_options;
  3429. }
  3430. static void
  3431. create_trace_option_file(struct trace_option_dentry *topt,
  3432. struct tracer_flags *flags,
  3433. struct tracer_opt *opt)
  3434. {
  3435. struct dentry *t_options;
  3436. t_options = trace_options_init_dentry();
  3437. if (!t_options)
  3438. return;
  3439. topt->flags = flags;
  3440. topt->opt = opt;
  3441. topt->entry = trace_create_file(opt->name, 0644, t_options, topt,
  3442. &trace_options_fops);
  3443. }
  3444. static struct trace_option_dentry *
  3445. create_trace_option_files(struct tracer *tracer)
  3446. {
  3447. struct trace_option_dentry *topts;
  3448. struct tracer_flags *flags;
  3449. struct tracer_opt *opts;
  3450. int cnt;
  3451. if (!tracer)
  3452. return NULL;
  3453. flags = tracer->flags;
  3454. if (!flags || !flags->opts)
  3455. return NULL;
  3456. opts = flags->opts;
  3457. for (cnt = 0; opts[cnt].name; cnt++)
  3458. ;
  3459. topts = kcalloc(cnt + 1, sizeof(*topts), GFP_KERNEL);
  3460. if (!topts)
  3461. return NULL;
  3462. for (cnt = 0; opts[cnt].name; cnt++)
  3463. create_trace_option_file(&topts[cnt], flags,
  3464. &opts[cnt]);
  3465. return topts;
  3466. }
  3467. static void
  3468. destroy_trace_option_files(struct trace_option_dentry *topts)
  3469. {
  3470. int cnt;
  3471. if (!topts)
  3472. return;
  3473. for (cnt = 0; topts[cnt].opt; cnt++) {
  3474. if (topts[cnt].entry)
  3475. debugfs_remove(topts[cnt].entry);
  3476. }
  3477. kfree(topts);
  3478. }
  3479. static struct dentry *
  3480. create_trace_option_core_file(const char *option, long index)
  3481. {
  3482. struct dentry *t_options;
  3483. t_options = trace_options_init_dentry();
  3484. if (!t_options)
  3485. return NULL;
  3486. return trace_create_file(option, 0644, t_options, (void *)index,
  3487. &trace_options_core_fops);
  3488. }
  3489. static __init void create_trace_options_dir(void)
  3490. {
  3491. struct dentry *t_options;
  3492. int i;
  3493. t_options = trace_options_init_dentry();
  3494. if (!t_options)
  3495. return;
  3496. for (i = 0; trace_options[i]; i++)
  3497. create_trace_option_core_file(trace_options[i], i);
  3498. }
  3499. static __init int tracer_init_debugfs(void)
  3500. {
  3501. struct dentry *d_tracer;
  3502. int cpu;
  3503. trace_access_lock_init();
  3504. d_tracer = tracing_init_dentry();
  3505. trace_create_file("tracing_enabled", 0644, d_tracer,
  3506. &global_trace, &tracing_ctrl_fops);
  3507. trace_create_file("trace_options", 0644, d_tracer,
  3508. NULL, &tracing_iter_fops);
  3509. trace_create_file("tracing_cpumask", 0644, d_tracer,
  3510. NULL, &tracing_cpumask_fops);
  3511. trace_create_file("trace", 0644, d_tracer,
  3512. (void *) TRACE_PIPE_ALL_CPU, &tracing_fops);
  3513. trace_create_file("available_tracers", 0444, d_tracer,
  3514. &global_trace, &show_traces_fops);
  3515. trace_create_file("current_tracer", 0644, d_tracer,
  3516. &global_trace, &set_tracer_fops);
  3517. #ifdef CONFIG_TRACER_MAX_TRACE
  3518. trace_create_file("tracing_max_latency", 0644, d_tracer,
  3519. &tracing_max_latency, &tracing_max_lat_fops);
  3520. #endif
  3521. trace_create_file("tracing_thresh", 0644, d_tracer,
  3522. &tracing_thresh, &tracing_max_lat_fops);
  3523. trace_create_file("README", 0444, d_tracer,
  3524. NULL, &tracing_readme_fops);
  3525. trace_create_file("trace_pipe", 0444, d_tracer,
  3526. (void *) TRACE_PIPE_ALL_CPU, &tracing_pipe_fops);
  3527. trace_create_file("buffer_size_kb", 0644, d_tracer,
  3528. &global_trace, &tracing_entries_fops);
  3529. trace_create_file("trace_marker", 0220, d_tracer,
  3530. NULL, &tracing_mark_fops);
  3531. trace_create_file("saved_cmdlines", 0444, d_tracer,
  3532. NULL, &tracing_saved_cmdlines_fops);
  3533. trace_create_file("trace_clock", 0644, d_tracer, NULL,
  3534. &trace_clock_fops);
  3535. #ifdef CONFIG_DYNAMIC_FTRACE
  3536. trace_create_file("dyn_ftrace_total_info", 0444, d_tracer,
  3537. &ftrace_update_tot_cnt, &tracing_dyn_info_fops);
  3538. #endif
  3539. create_trace_options_dir();
  3540. for_each_tracing_cpu(cpu)
  3541. tracing_init_debugfs_percpu(cpu);
  3542. return 0;
  3543. }
  3544. static int trace_panic_handler(struct notifier_block *this,
  3545. unsigned long event, void *unused)
  3546. {
  3547. if (ftrace_dump_on_oops)
  3548. ftrace_dump(ftrace_dump_on_oops);
  3549. return NOTIFY_OK;
  3550. }
  3551. static struct notifier_block trace_panic_notifier = {
  3552. .notifier_call = trace_panic_handler,
  3553. .next = NULL,
  3554. .priority = 150 /* priority: INT_MAX >= x >= 0 */
  3555. };
  3556. static int trace_die_handler(struct notifier_block *self,
  3557. unsigned long val,
  3558. void *data)
  3559. {
  3560. switch (val) {
  3561. case DIE_OOPS:
  3562. if (ftrace_dump_on_oops)
  3563. ftrace_dump(ftrace_dump_on_oops);
  3564. break;
  3565. default:
  3566. break;
  3567. }
  3568. return NOTIFY_OK;
  3569. }
  3570. static struct notifier_block trace_die_notifier = {
  3571. .notifier_call = trace_die_handler,
  3572. .priority = 200
  3573. };
  3574. /*
  3575. * printk is set to max of 1024, we really don't need it that big.
  3576. * Nothing should be printing 1000 characters anyway.
  3577. */
  3578. #define TRACE_MAX_PRINT 1000
  3579. /*
  3580. * Define here KERN_TRACE so that we have one place to modify
  3581. * it if we decide to change what log level the ftrace dump
  3582. * should be at.
  3583. */
  3584. #define KERN_TRACE KERN_EMERG
  3585. void
  3586. trace_printk_seq(struct trace_seq *s)
  3587. {
  3588. /* Probably should print a warning here. */
  3589. if (s->len >= 1000)
  3590. s->len = 1000;
  3591. /* should be zero ended, but we are paranoid. */
  3592. s->buffer[s->len] = 0;
  3593. printk(KERN_TRACE "%s", s->buffer);
  3594. trace_seq_init(s);
  3595. }
  3596. void trace_init_global_iter(struct trace_iterator *iter)
  3597. {
  3598. iter->tr = &global_trace;
  3599. iter->trace = current_trace;
  3600. iter->cpu_file = TRACE_PIPE_ALL_CPU;
  3601. }
  3602. static void
  3603. __ftrace_dump(bool disable_tracing, enum ftrace_dump_mode oops_dump_mode)
  3604. {
  3605. static arch_spinlock_t ftrace_dump_lock =
  3606. (arch_spinlock_t)__ARCH_SPIN_LOCK_UNLOCKED;
  3607. /* use static because iter can be a bit big for the stack */
  3608. static struct trace_iterator iter;
  3609. unsigned int old_userobj;
  3610. static int dump_ran;
  3611. unsigned long flags;
  3612. int cnt = 0, cpu;
  3613. /* only one dump */
  3614. local_irq_save(flags);
  3615. arch_spin_lock(&ftrace_dump_lock);
  3616. if (dump_ran)
  3617. goto out;
  3618. dump_ran = 1;
  3619. tracing_off();
  3620. if (disable_tracing)
  3621. ftrace_kill();
  3622. trace_init_global_iter(&iter);
  3623. for_each_tracing_cpu(cpu) {
  3624. atomic_inc(&iter.tr->data[cpu]->disabled);
  3625. }
  3626. old_userobj = trace_flags & TRACE_ITER_SYM_USEROBJ;
  3627. /* don't look at user memory in panic mode */
  3628. trace_flags &= ~TRACE_ITER_SYM_USEROBJ;
  3629. /* Simulate the iterator */
  3630. iter.tr = &global_trace;
  3631. iter.trace = current_trace;
  3632. switch (oops_dump_mode) {
  3633. case DUMP_ALL:
  3634. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  3635. break;
  3636. case DUMP_ORIG:
  3637. iter.cpu_file = raw_smp_processor_id();
  3638. break;
  3639. case DUMP_NONE:
  3640. goto out_enable;
  3641. default:
  3642. printk(KERN_TRACE "Bad dumping mode, switching to all CPUs dump\n");
  3643. iter.cpu_file = TRACE_PIPE_ALL_CPU;
  3644. }
  3645. printk(KERN_TRACE "Dumping ftrace buffer:\n");
  3646. /*
  3647. * We need to stop all tracing on all CPUS to read the
  3648. * the next buffer. This is a bit expensive, but is
  3649. * not done often. We fill all what we can read,
  3650. * and then release the locks again.
  3651. */
  3652. while (!trace_empty(&iter)) {
  3653. if (!cnt)
  3654. printk(KERN_TRACE "---------------------------------\n");
  3655. cnt++;
  3656. /* reset all but tr, trace, and overruns */
  3657. memset(&iter.seq, 0,
  3658. sizeof(struct trace_iterator) -
  3659. offsetof(struct trace_iterator, seq));
  3660. iter.iter_flags |= TRACE_FILE_LAT_FMT;
  3661. iter.pos = -1;
  3662. if (trace_find_next_entry_inc(&iter) != NULL) {
  3663. int ret;
  3664. ret = print_trace_line(&iter);
  3665. if (ret != TRACE_TYPE_NO_CONSUME)
  3666. trace_consume(&iter);
  3667. }
  3668. trace_printk_seq(&iter.seq);
  3669. }
  3670. if (!cnt)
  3671. printk(KERN_TRACE " (ftrace buffer empty)\n");
  3672. else
  3673. printk(KERN_TRACE "---------------------------------\n");
  3674. out_enable:
  3675. /* Re-enable tracing if requested */
  3676. if (!disable_tracing) {
  3677. trace_flags |= old_userobj;
  3678. for_each_tracing_cpu(cpu) {
  3679. atomic_dec(&iter.tr->data[cpu]->disabled);
  3680. }
  3681. tracing_on();
  3682. }
  3683. out:
  3684. arch_spin_unlock(&ftrace_dump_lock);
  3685. local_irq_restore(flags);
  3686. }
  3687. /* By default: disable tracing after the dump */
  3688. void ftrace_dump(enum ftrace_dump_mode oops_dump_mode)
  3689. {
  3690. __ftrace_dump(true, oops_dump_mode);
  3691. }
  3692. __init static int tracer_alloc_buffers(void)
  3693. {
  3694. int ring_buf_size;
  3695. enum ring_buffer_flags rb_flags;
  3696. int i;
  3697. int ret = -ENOMEM;
  3698. if (!alloc_cpumask_var(&tracing_buffer_mask, GFP_KERNEL))
  3699. goto out;
  3700. if (!alloc_cpumask_var(&tracing_cpumask, GFP_KERNEL))
  3701. goto out_free_buffer_mask;
  3702. /* To save memory, keep the ring buffer size to its minimum */
  3703. if (ring_buffer_expanded)
  3704. ring_buf_size = trace_buf_size;
  3705. else
  3706. ring_buf_size = 1;
  3707. rb_flags = trace_flags & TRACE_ITER_OVERWRITE ? RB_FL_OVERWRITE : 0;
  3708. cpumask_copy(tracing_buffer_mask, cpu_possible_mask);
  3709. cpumask_copy(tracing_cpumask, cpu_all_mask);
  3710. /* TODO: make the number of buffers hot pluggable with CPUS */
  3711. global_trace.buffer = ring_buffer_alloc(ring_buf_size, rb_flags);
  3712. if (!global_trace.buffer) {
  3713. printk(KERN_ERR "tracer: failed to allocate ring buffer!\n");
  3714. WARN_ON(1);
  3715. goto out_free_cpumask;
  3716. }
  3717. global_trace.entries = ring_buffer_size(global_trace.buffer);
  3718. #ifdef CONFIG_TRACER_MAX_TRACE
  3719. max_tr.buffer = ring_buffer_alloc(1, rb_flags);
  3720. if (!max_tr.buffer) {
  3721. printk(KERN_ERR "tracer: failed to allocate max ring buffer!\n");
  3722. WARN_ON(1);
  3723. ring_buffer_free(global_trace.buffer);
  3724. goto out_free_cpumask;
  3725. }
  3726. max_tr.entries = 1;
  3727. #endif
  3728. /* Allocate the first page for all buffers */
  3729. for_each_tracing_cpu(i) {
  3730. global_trace.data[i] = &per_cpu(global_trace_cpu, i);
  3731. max_tr.data[i] = &per_cpu(max_tr_data, i);
  3732. }
  3733. trace_init_cmdlines();
  3734. register_tracer(&nop_trace);
  3735. current_trace = &nop_trace;
  3736. /* All seems OK, enable tracing */
  3737. tracing_disabled = 0;
  3738. atomic_notifier_chain_register(&panic_notifier_list,
  3739. &trace_panic_notifier);
  3740. register_die_notifier(&trace_die_notifier);
  3741. return 0;
  3742. out_free_cpumask:
  3743. free_cpumask_var(tracing_cpumask);
  3744. out_free_buffer_mask:
  3745. free_cpumask_var(tracing_buffer_mask);
  3746. out:
  3747. return ret;
  3748. }
  3749. __init static int clear_boot_tracer(void)
  3750. {
  3751. /*
  3752. * The default tracer at boot buffer is an init section.
  3753. * This function is called in lateinit. If we did not
  3754. * find the boot tracer, then clear it out, to prevent
  3755. * later registration from accessing the buffer that is
  3756. * about to be freed.
  3757. */
  3758. if (!default_bootup_tracer)
  3759. return 0;
  3760. printk(KERN_INFO "ftrace bootup tracer '%s' not registered.\n",
  3761. default_bootup_tracer);
  3762. default_bootup_tracer = NULL;
  3763. return 0;
  3764. }
  3765. early_initcall(tracer_alloc_buffers);
  3766. fs_initcall(tracer_init_debugfs);
  3767. late_initcall(clear_boot_tracer);