trace.c 94 KB

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