trace.c 93 KB

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