trace.c 77 KB

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