trace.c 101 KB

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