trace.c 100 KB

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