trace.c 69 KB

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