trace.c 98 KB

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