trace.c 80 KB

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