trace.c 74 KB

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