trace.c 84 KB

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