trace.c 71 KB

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