trace.c 103 KB

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