trace.c 100 KB

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