trace.c 103 KB

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