trace.c 71 KB

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