trace.c 103 KB

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