trace.c 100 KB

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