trace.c 100 KB

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