trace.c 96 KB

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