trace.c 95 KB

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