trace.c 94 KB

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