trace_functions_graph.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561
  1. /*
  2. *
  3. * Function graph tracer.
  4. * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
  5. * Mostly borrowed from function tracer which
  6. * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
  7. *
  8. */
  9. #include <linux/uaccess.h>
  10. #include <linux/ftrace.h>
  11. #include <linux/interrupt.h>
  12. #include <linux/slab.h>
  13. #include <linux/fs.h>
  14. #include "trace.h"
  15. #include "trace_output.h"
  16. static bool kill_ftrace_graph;
  17. /**
  18. * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
  19. *
  20. * ftrace_graph_stop() is called when a severe error is detected in
  21. * the function graph tracing. This function is called by the critical
  22. * paths of function graph to keep those paths from doing any more harm.
  23. */
  24. bool ftrace_graph_is_dead(void)
  25. {
  26. return kill_ftrace_graph;
  27. }
  28. /**
  29. * ftrace_graph_stop - set to permanently disable function graph tracincg
  30. *
  31. * In case of an error int function graph tracing, this is called
  32. * to try to keep function graph tracing from causing any more harm.
  33. * Usually this is pretty severe and this is called to try to at least
  34. * get a warning out to the user.
  35. */
  36. void ftrace_graph_stop(void)
  37. {
  38. kill_ftrace_graph = true;
  39. }
  40. /* When set, irq functions will be ignored */
  41. static int ftrace_graph_skip_irqs;
  42. struct fgraph_cpu_data {
  43. pid_t last_pid;
  44. int depth;
  45. int depth_irq;
  46. int ignore;
  47. unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH];
  48. };
  49. struct fgraph_data {
  50. struct fgraph_cpu_data __percpu *cpu_data;
  51. /* Place to preserve last processed entry. */
  52. struct ftrace_graph_ent_entry ent;
  53. struct ftrace_graph_ret_entry ret;
  54. int failed;
  55. int cpu;
  56. };
  57. #define TRACE_GRAPH_INDENT 2
  58. static unsigned int max_depth;
  59. static struct tracer_opt trace_opts[] = {
  60. /* Display overruns? (for self-debug purpose) */
  61. { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
  62. /* Display CPU ? */
  63. { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
  64. /* Display Overhead ? */
  65. { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
  66. /* Display proc name/pid */
  67. { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
  68. /* Display duration of execution */
  69. { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
  70. /* Display absolute time of an entry */
  71. { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
  72. /* Display interrupts */
  73. { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
  74. /* Display function name after trailing } */
  75. { TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },
  76. /* Include sleep time (scheduled out) between entry and return */
  77. { TRACER_OPT(sleep-time, TRACE_GRAPH_SLEEP_TIME) },
  78. /* Include time within nested functions */
  79. { TRACER_OPT(graph-time, TRACE_GRAPH_GRAPH_TIME) },
  80. { } /* Empty entry */
  81. };
  82. static struct tracer_flags tracer_flags = {
  83. /* Don't display overruns, proc, or tail by default */
  84. .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
  85. TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS |
  86. TRACE_GRAPH_SLEEP_TIME | TRACE_GRAPH_GRAPH_TIME,
  87. .opts = trace_opts
  88. };
  89. static struct trace_array *graph_array;
  90. /*
  91. * DURATION column is being also used to display IRQ signs,
  92. * following values are used by print_graph_irq and others
  93. * to fill in space into DURATION column.
  94. */
  95. enum {
  96. FLAGS_FILL_FULL = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  97. FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  98. FLAGS_FILL_END = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,
  99. };
  100. static void
  101. print_graph_duration(struct trace_array *tr, unsigned long long duration,
  102. struct trace_seq *s, u32 flags);
  103. /* Add a function return address to the trace stack on thread info.*/
  104. int
  105. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  106. unsigned long frame_pointer, unsigned long *retp)
  107. {
  108. unsigned long long calltime;
  109. int index;
  110. if (unlikely(ftrace_graph_is_dead()))
  111. return -EBUSY;
  112. if (!current->ret_stack)
  113. return -EBUSY;
  114. /*
  115. * We must make sure the ret_stack is tested before we read
  116. * anything else.
  117. */
  118. smp_rmb();
  119. /* The return trace stack is full */
  120. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  121. atomic_inc(&current->trace_overrun);
  122. return -EBUSY;
  123. }
  124. /*
  125. * The curr_ret_stack is an index to ftrace return stack of
  126. * current task. Its value should be in [0, FTRACE_RETFUNC_
  127. * DEPTH) when the function graph tracer is used. To support
  128. * filtering out specific functions, it makes the index
  129. * negative by subtracting huge value (FTRACE_NOTRACE_DEPTH)
  130. * so when it sees a negative index the ftrace will ignore
  131. * the record. And the index gets recovered when returning
  132. * from the filtered function by adding the FTRACE_NOTRACE_
  133. * DEPTH and then it'll continue to record functions normally.
  134. *
  135. * The curr_ret_stack is initialized to -1 and get increased
  136. * in this function. So it can be less than -1 only if it was
  137. * filtered out via ftrace_graph_notrace_addr() which can be
  138. * set from set_graph_notrace file in tracefs by user.
  139. */
  140. if (current->curr_ret_stack < -1)
  141. return -EBUSY;
  142. calltime = trace_clock_local();
  143. index = ++current->curr_ret_stack;
  144. if (ftrace_graph_notrace_addr(func))
  145. current->curr_ret_stack -= FTRACE_NOTRACE_DEPTH;
  146. barrier();
  147. current->ret_stack[index].ret = ret;
  148. current->ret_stack[index].func = func;
  149. current->ret_stack[index].calltime = calltime;
  150. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  151. current->ret_stack[index].fp = frame_pointer;
  152. #endif
  153. #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  154. current->ret_stack[index].retp = retp;
  155. #endif
  156. *depth = current->curr_ret_stack;
  157. return 0;
  158. }
  159. /* Retrieve a function return address to the trace stack on thread info.*/
  160. static void
  161. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
  162. unsigned long frame_pointer)
  163. {
  164. int index;
  165. index = current->curr_ret_stack;
  166. /*
  167. * A negative index here means that it's just returned from a
  168. * notrace'd function. Recover index to get an original
  169. * return address. See ftrace_push_return_trace().
  170. *
  171. * TODO: Need to check whether the stack gets corrupted.
  172. */
  173. if (index < 0)
  174. index += FTRACE_NOTRACE_DEPTH;
  175. if (unlikely(index < 0 || index >= FTRACE_RETFUNC_DEPTH)) {
  176. ftrace_graph_stop();
  177. WARN_ON(1);
  178. /* Might as well panic, otherwise we have no where to go */
  179. *ret = (unsigned long)panic;
  180. return;
  181. }
  182. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  183. /*
  184. * The arch may choose to record the frame pointer used
  185. * and check it here to make sure that it is what we expect it
  186. * to be. If gcc does not set the place holder of the return
  187. * address in the frame pointer, and does a copy instead, then
  188. * the function graph trace will fail. This test detects this
  189. * case.
  190. *
  191. * Currently, x86_32 with optimize for size (-Os) makes the latest
  192. * gcc do the above.
  193. *
  194. * Note, -mfentry does not use frame pointers, and this test
  195. * is not needed if CC_USING_FENTRY is set.
  196. */
  197. if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
  198. ftrace_graph_stop();
  199. WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
  200. " from func %ps return to %lx\n",
  201. current->ret_stack[index].fp,
  202. frame_pointer,
  203. (void *)current->ret_stack[index].func,
  204. current->ret_stack[index].ret);
  205. *ret = (unsigned long)panic;
  206. return;
  207. }
  208. #endif
  209. *ret = current->ret_stack[index].ret;
  210. trace->func = current->ret_stack[index].func;
  211. trace->calltime = current->ret_stack[index].calltime;
  212. trace->overrun = atomic_read(&current->trace_overrun);
  213. trace->depth = index;
  214. }
  215. /*
  216. * Send the trace to the ring-buffer.
  217. * @return the original return address.
  218. */
  219. unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
  220. {
  221. struct ftrace_graph_ret trace;
  222. unsigned long ret;
  223. ftrace_pop_return_trace(&trace, &ret, frame_pointer);
  224. trace.rettime = trace_clock_local();
  225. barrier();
  226. current->curr_ret_stack--;
  227. /*
  228. * The curr_ret_stack can be less than -1 only if it was
  229. * filtered out and it's about to return from the function.
  230. * Recover the index and continue to trace normal functions.
  231. */
  232. if (current->curr_ret_stack < -1) {
  233. current->curr_ret_stack += FTRACE_NOTRACE_DEPTH;
  234. return ret;
  235. }
  236. /*
  237. * The trace should run after decrementing the ret counter
  238. * in case an interrupt were to come in. We don't want to
  239. * lose the interrupt if max_depth is set.
  240. */
  241. ftrace_graph_return(&trace);
  242. if (unlikely(!ret)) {
  243. ftrace_graph_stop();
  244. WARN_ON(1);
  245. /* Might as well panic. What else to do? */
  246. ret = (unsigned long)panic;
  247. }
  248. return ret;
  249. }
  250. /**
  251. * ftrace_graph_ret_addr - convert a potentially modified stack return address
  252. * to its original value
  253. *
  254. * This function can be called by stack unwinding code to convert a found stack
  255. * return address ('ret') to its original value, in case the function graph
  256. * tracer has modified it to be 'return_to_handler'. If the address hasn't
  257. * been modified, the unchanged value of 'ret' is returned.
  258. *
  259. * 'idx' is a state variable which should be initialized by the caller to zero
  260. * before the first call.
  261. *
  262. * 'retp' is a pointer to the return address on the stack. It's ignored if
  263. * the arch doesn't have HAVE_FUNCTION_GRAPH_RET_ADDR_PTR defined.
  264. */
  265. #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  266. unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
  267. unsigned long ret, unsigned long *retp)
  268. {
  269. int index = task->curr_ret_stack;
  270. int i;
  271. if (ret != (unsigned long)return_to_handler)
  272. return ret;
  273. if (index < -1)
  274. index += FTRACE_NOTRACE_DEPTH;
  275. if (index < 0)
  276. return ret;
  277. for (i = 0; i <= index; i++)
  278. if (task->ret_stack[i].retp == retp)
  279. return task->ret_stack[i].ret;
  280. return ret;
  281. }
  282. #else /* !HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
  283. unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
  284. unsigned long ret, unsigned long *retp)
  285. {
  286. int task_idx;
  287. if (ret != (unsigned long)return_to_handler)
  288. return ret;
  289. task_idx = task->curr_ret_stack;
  290. if (!task->ret_stack || task_idx < *idx)
  291. return ret;
  292. task_idx -= *idx;
  293. (*idx)++;
  294. return task->ret_stack[task_idx].ret;
  295. }
  296. #endif /* HAVE_FUNCTION_GRAPH_RET_ADDR_PTR */
  297. int __trace_graph_entry(struct trace_array *tr,
  298. struct ftrace_graph_ent *trace,
  299. unsigned long flags,
  300. int pc)
  301. {
  302. struct trace_event_call *call = &event_funcgraph_entry;
  303. struct ring_buffer_event *event;
  304. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  305. struct ftrace_graph_ent_entry *entry;
  306. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
  307. sizeof(*entry), flags, pc);
  308. if (!event)
  309. return 0;
  310. entry = ring_buffer_event_data(event);
  311. entry->graph_ent = *trace;
  312. if (!call_filter_check_discard(call, entry, buffer, event))
  313. __buffer_unlock_commit(buffer, event);
  314. return 1;
  315. }
  316. static inline int ftrace_graph_ignore_irqs(void)
  317. {
  318. if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
  319. return 0;
  320. return in_irq();
  321. }
  322. int trace_graph_entry(struct ftrace_graph_ent *trace)
  323. {
  324. struct trace_array *tr = graph_array;
  325. struct trace_array_cpu *data;
  326. unsigned long flags;
  327. long disabled;
  328. int ret;
  329. int cpu;
  330. int pc;
  331. if (!ftrace_trace_task(tr))
  332. return 0;
  333. /* trace it when it is-nested-in or is a function enabled. */
  334. if ((!(trace->depth || ftrace_graph_addr(trace->func)) ||
  335. ftrace_graph_ignore_irqs()) || (trace->depth < 0) ||
  336. (max_depth && trace->depth >= max_depth))
  337. return 0;
  338. /*
  339. * Do not trace a function if it's filtered by set_graph_notrace.
  340. * Make the index of ret stack negative to indicate that it should
  341. * ignore further functions. But it needs its own ret stack entry
  342. * to recover the original index in order to continue tracing after
  343. * returning from the function.
  344. */
  345. if (ftrace_graph_notrace_addr(trace->func))
  346. return 1;
  347. /*
  348. * Stop here if tracing_threshold is set. We only write function return
  349. * events to the ring buffer.
  350. */
  351. if (tracing_thresh)
  352. return 1;
  353. local_irq_save(flags);
  354. cpu = raw_smp_processor_id();
  355. data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  356. disabled = atomic_inc_return(&data->disabled);
  357. if (likely(disabled == 1)) {
  358. pc = preempt_count();
  359. ret = __trace_graph_entry(tr, trace, flags, pc);
  360. } else {
  361. ret = 0;
  362. }
  363. atomic_dec(&data->disabled);
  364. local_irq_restore(flags);
  365. return ret;
  366. }
  367. static void
  368. __trace_graph_function(struct trace_array *tr,
  369. unsigned long ip, unsigned long flags, int pc)
  370. {
  371. u64 time = trace_clock_local();
  372. struct ftrace_graph_ent ent = {
  373. .func = ip,
  374. .depth = 0,
  375. };
  376. struct ftrace_graph_ret ret = {
  377. .func = ip,
  378. .depth = 0,
  379. .calltime = time,
  380. .rettime = time,
  381. };
  382. __trace_graph_entry(tr, &ent, flags, pc);
  383. __trace_graph_return(tr, &ret, flags, pc);
  384. }
  385. void
  386. trace_graph_function(struct trace_array *tr,
  387. unsigned long ip, unsigned long parent_ip,
  388. unsigned long flags, int pc)
  389. {
  390. __trace_graph_function(tr, ip, flags, pc);
  391. }
  392. void __trace_graph_return(struct trace_array *tr,
  393. struct ftrace_graph_ret *trace,
  394. unsigned long flags,
  395. int pc)
  396. {
  397. struct trace_event_call *call = &event_funcgraph_exit;
  398. struct ring_buffer_event *event;
  399. struct ring_buffer *buffer = tr->trace_buffer.buffer;
  400. struct ftrace_graph_ret_entry *entry;
  401. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
  402. sizeof(*entry), flags, pc);
  403. if (!event)
  404. return;
  405. entry = ring_buffer_event_data(event);
  406. entry->ret = *trace;
  407. if (!call_filter_check_discard(call, entry, buffer, event))
  408. __buffer_unlock_commit(buffer, event);
  409. }
  410. void trace_graph_return(struct ftrace_graph_ret *trace)
  411. {
  412. struct trace_array *tr = graph_array;
  413. struct trace_array_cpu *data;
  414. unsigned long flags;
  415. long disabled;
  416. int cpu;
  417. int pc;
  418. local_irq_save(flags);
  419. cpu = raw_smp_processor_id();
  420. data = per_cpu_ptr(tr->trace_buffer.data, cpu);
  421. disabled = atomic_inc_return(&data->disabled);
  422. if (likely(disabled == 1)) {
  423. pc = preempt_count();
  424. __trace_graph_return(tr, trace, flags, pc);
  425. }
  426. atomic_dec(&data->disabled);
  427. local_irq_restore(flags);
  428. }
  429. void set_graph_array(struct trace_array *tr)
  430. {
  431. graph_array = tr;
  432. /* Make graph_array visible before we start tracing */
  433. smp_mb();
  434. }
  435. static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
  436. {
  437. if (tracing_thresh &&
  438. (trace->rettime - trace->calltime < tracing_thresh))
  439. return;
  440. else
  441. trace_graph_return(trace);
  442. }
  443. static int graph_trace_init(struct trace_array *tr)
  444. {
  445. int ret;
  446. set_graph_array(tr);
  447. if (tracing_thresh)
  448. ret = register_ftrace_graph(&trace_graph_thresh_return,
  449. &trace_graph_entry);
  450. else
  451. ret = register_ftrace_graph(&trace_graph_return,
  452. &trace_graph_entry);
  453. if (ret)
  454. return ret;
  455. tracing_start_cmdline_record();
  456. return 0;
  457. }
  458. static void graph_trace_reset(struct trace_array *tr)
  459. {
  460. tracing_stop_cmdline_record();
  461. unregister_ftrace_graph();
  462. }
  463. static int graph_trace_update_thresh(struct trace_array *tr)
  464. {
  465. graph_trace_reset(tr);
  466. return graph_trace_init(tr);
  467. }
  468. static int max_bytes_for_cpu;
  469. static void print_graph_cpu(struct trace_seq *s, int cpu)
  470. {
  471. /*
  472. * Start with a space character - to make it stand out
  473. * to the right a bit when trace output is pasted into
  474. * email:
  475. */
  476. trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
  477. }
  478. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  479. static void print_graph_proc(struct trace_seq *s, pid_t pid)
  480. {
  481. char comm[TASK_COMM_LEN];
  482. /* sign + log10(MAX_INT) + '\0' */
  483. char pid_str[11];
  484. int spaces = 0;
  485. int len;
  486. int i;
  487. trace_find_cmdline(pid, comm);
  488. comm[7] = '\0';
  489. sprintf(pid_str, "%d", pid);
  490. /* 1 stands for the "-" character */
  491. len = strlen(comm) + strlen(pid_str) + 1;
  492. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  493. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  494. /* First spaces to align center */
  495. for (i = 0; i < spaces / 2; i++)
  496. trace_seq_putc(s, ' ');
  497. trace_seq_printf(s, "%s-%s", comm, pid_str);
  498. /* Last spaces to align center */
  499. for (i = 0; i < spaces - (spaces / 2); i++)
  500. trace_seq_putc(s, ' ');
  501. }
  502. static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  503. {
  504. trace_seq_putc(s, ' ');
  505. trace_print_lat_fmt(s, entry);
  506. }
  507. /* If the pid changed since the last trace, output this event */
  508. static void
  509. verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
  510. {
  511. pid_t prev_pid;
  512. pid_t *last_pid;
  513. if (!data)
  514. return;
  515. last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  516. if (*last_pid == pid)
  517. return;
  518. prev_pid = *last_pid;
  519. *last_pid = pid;
  520. if (prev_pid == -1)
  521. return;
  522. /*
  523. * Context-switch trace line:
  524. ------------------------------------------
  525. | 1) migration/0--1 => sshd-1755
  526. ------------------------------------------
  527. */
  528. trace_seq_puts(s, " ------------------------------------------\n");
  529. print_graph_cpu(s, cpu);
  530. print_graph_proc(s, prev_pid);
  531. trace_seq_puts(s, " => ");
  532. print_graph_proc(s, pid);
  533. trace_seq_puts(s, "\n ------------------------------------------\n\n");
  534. }
  535. static struct ftrace_graph_ret_entry *
  536. get_return_for_leaf(struct trace_iterator *iter,
  537. struct ftrace_graph_ent_entry *curr)
  538. {
  539. struct fgraph_data *data = iter->private;
  540. struct ring_buffer_iter *ring_iter = NULL;
  541. struct ring_buffer_event *event;
  542. struct ftrace_graph_ret_entry *next;
  543. /*
  544. * If the previous output failed to write to the seq buffer,
  545. * then we just reuse the data from before.
  546. */
  547. if (data && data->failed) {
  548. curr = &data->ent;
  549. next = &data->ret;
  550. } else {
  551. ring_iter = trace_buffer_iter(iter, iter->cpu);
  552. /* First peek to compare current entry and the next one */
  553. if (ring_iter)
  554. event = ring_buffer_iter_peek(ring_iter, NULL);
  555. else {
  556. /*
  557. * We need to consume the current entry to see
  558. * the next one.
  559. */
  560. ring_buffer_consume(iter->trace_buffer->buffer, iter->cpu,
  561. NULL, NULL);
  562. event = ring_buffer_peek(iter->trace_buffer->buffer, iter->cpu,
  563. NULL, NULL);
  564. }
  565. if (!event)
  566. return NULL;
  567. next = ring_buffer_event_data(event);
  568. if (data) {
  569. /*
  570. * Save current and next entries for later reference
  571. * if the output fails.
  572. */
  573. data->ent = *curr;
  574. /*
  575. * If the next event is not a return type, then
  576. * we only care about what type it is. Otherwise we can
  577. * safely copy the entire event.
  578. */
  579. if (next->ent.type == TRACE_GRAPH_RET)
  580. data->ret = *next;
  581. else
  582. data->ret.ent.type = next->ent.type;
  583. }
  584. }
  585. if (next->ent.type != TRACE_GRAPH_RET)
  586. return NULL;
  587. if (curr->ent.pid != next->ent.pid ||
  588. curr->graph_ent.func != next->ret.func)
  589. return NULL;
  590. /* this is a leaf, now advance the iterator */
  591. if (ring_iter)
  592. ring_buffer_read(ring_iter, NULL);
  593. return next;
  594. }
  595. static void print_graph_abs_time(u64 t, struct trace_seq *s)
  596. {
  597. unsigned long usecs_rem;
  598. usecs_rem = do_div(t, NSEC_PER_SEC);
  599. usecs_rem /= 1000;
  600. trace_seq_printf(s, "%5lu.%06lu | ",
  601. (unsigned long)t, usecs_rem);
  602. }
  603. static void
  604. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  605. enum trace_type type, int cpu, pid_t pid, u32 flags)
  606. {
  607. struct trace_array *tr = iter->tr;
  608. struct trace_seq *s = &iter->seq;
  609. struct trace_entry *ent = iter->ent;
  610. if (addr < (unsigned long)__irqentry_text_start ||
  611. addr >= (unsigned long)__irqentry_text_end)
  612. return;
  613. if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
  614. /* Absolute time */
  615. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  616. print_graph_abs_time(iter->ts, s);
  617. /* Cpu */
  618. if (flags & TRACE_GRAPH_PRINT_CPU)
  619. print_graph_cpu(s, cpu);
  620. /* Proc */
  621. if (flags & TRACE_GRAPH_PRINT_PROC) {
  622. print_graph_proc(s, pid);
  623. trace_seq_puts(s, " | ");
  624. }
  625. /* Latency format */
  626. if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
  627. print_graph_lat_fmt(s, ent);
  628. }
  629. /* No overhead */
  630. print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START);
  631. if (type == TRACE_GRAPH_ENT)
  632. trace_seq_puts(s, "==========>");
  633. else
  634. trace_seq_puts(s, "<==========");
  635. print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END);
  636. trace_seq_putc(s, '\n');
  637. }
  638. void
  639. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
  640. {
  641. unsigned long nsecs_rem = do_div(duration, 1000);
  642. /* log10(ULONG_MAX) + '\0' */
  643. char usecs_str[21];
  644. char nsecs_str[5];
  645. int len;
  646. int i;
  647. sprintf(usecs_str, "%lu", (unsigned long) duration);
  648. /* Print msecs */
  649. trace_seq_printf(s, "%s", usecs_str);
  650. len = strlen(usecs_str);
  651. /* Print nsecs (we don't want to exceed 7 numbers) */
  652. if (len < 7) {
  653. size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
  654. snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
  655. trace_seq_printf(s, ".%s", nsecs_str);
  656. len += strlen(nsecs_str) + 1;
  657. }
  658. trace_seq_puts(s, " us ");
  659. /* Print remaining spaces to fit the row's width */
  660. for (i = len; i < 8; i++)
  661. trace_seq_putc(s, ' ');
  662. }
  663. static void
  664. print_graph_duration(struct trace_array *tr, unsigned long long duration,
  665. struct trace_seq *s, u32 flags)
  666. {
  667. if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
  668. !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
  669. return;
  670. /* No real adata, just filling the column with spaces */
  671. switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
  672. case FLAGS_FILL_FULL:
  673. trace_seq_puts(s, " | ");
  674. return;
  675. case FLAGS_FILL_START:
  676. trace_seq_puts(s, " ");
  677. return;
  678. case FLAGS_FILL_END:
  679. trace_seq_puts(s, " |");
  680. return;
  681. }
  682. /* Signal a overhead of time execution to the output */
  683. if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
  684. trace_seq_printf(s, "%c ", trace_find_mark(duration));
  685. else
  686. trace_seq_puts(s, " ");
  687. trace_print_graph_duration(duration, s);
  688. trace_seq_puts(s, "| ");
  689. }
  690. /* Case of a leaf function on its call entry */
  691. static enum print_line_t
  692. print_graph_entry_leaf(struct trace_iterator *iter,
  693. struct ftrace_graph_ent_entry *entry,
  694. struct ftrace_graph_ret_entry *ret_entry,
  695. struct trace_seq *s, u32 flags)
  696. {
  697. struct fgraph_data *data = iter->private;
  698. struct trace_array *tr = iter->tr;
  699. struct ftrace_graph_ret *graph_ret;
  700. struct ftrace_graph_ent *call;
  701. unsigned long long duration;
  702. int i;
  703. graph_ret = &ret_entry->ret;
  704. call = &entry->graph_ent;
  705. duration = graph_ret->rettime - graph_ret->calltime;
  706. if (data) {
  707. struct fgraph_cpu_data *cpu_data;
  708. int cpu = iter->cpu;
  709. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  710. /* If a graph tracer ignored set_graph_notrace */
  711. if (call->depth < -1)
  712. call->depth += FTRACE_NOTRACE_DEPTH;
  713. /*
  714. * Comments display at + 1 to depth. Since
  715. * this is a leaf function, keep the comments
  716. * equal to this depth.
  717. */
  718. cpu_data->depth = call->depth - 1;
  719. /* No need to keep this function around for this depth */
  720. if (call->depth < FTRACE_RETFUNC_DEPTH &&
  721. !WARN_ON_ONCE(call->depth < 0))
  722. cpu_data->enter_funcs[call->depth] = 0;
  723. }
  724. /* Overhead and duration */
  725. print_graph_duration(tr, duration, s, flags);
  726. /* Function */
  727. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  728. trace_seq_putc(s, ' ');
  729. trace_seq_printf(s, "%ps();\n", (void *)call->func);
  730. return trace_handle_return(s);
  731. }
  732. static enum print_line_t
  733. print_graph_entry_nested(struct trace_iterator *iter,
  734. struct ftrace_graph_ent_entry *entry,
  735. struct trace_seq *s, int cpu, u32 flags)
  736. {
  737. struct ftrace_graph_ent *call = &entry->graph_ent;
  738. struct fgraph_data *data = iter->private;
  739. struct trace_array *tr = iter->tr;
  740. int i;
  741. if (data) {
  742. struct fgraph_cpu_data *cpu_data;
  743. int cpu = iter->cpu;
  744. /* If a graph tracer ignored set_graph_notrace */
  745. if (call->depth < -1)
  746. call->depth += FTRACE_NOTRACE_DEPTH;
  747. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  748. cpu_data->depth = call->depth;
  749. /* Save this function pointer to see if the exit matches */
  750. if (call->depth < FTRACE_RETFUNC_DEPTH &&
  751. !WARN_ON_ONCE(call->depth < 0))
  752. cpu_data->enter_funcs[call->depth] = call->func;
  753. }
  754. /* No time */
  755. print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
  756. /* Function */
  757. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
  758. trace_seq_putc(s, ' ');
  759. trace_seq_printf(s, "%ps() {\n", (void *)call->func);
  760. if (trace_seq_has_overflowed(s))
  761. return TRACE_TYPE_PARTIAL_LINE;
  762. /*
  763. * we already consumed the current entry to check the next one
  764. * and see if this is a leaf.
  765. */
  766. return TRACE_TYPE_NO_CONSUME;
  767. }
  768. static void
  769. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  770. int type, unsigned long addr, u32 flags)
  771. {
  772. struct fgraph_data *data = iter->private;
  773. struct trace_entry *ent = iter->ent;
  774. struct trace_array *tr = iter->tr;
  775. int cpu = iter->cpu;
  776. /* Pid */
  777. verif_pid(s, ent->pid, cpu, data);
  778. if (type)
  779. /* Interrupt */
  780. print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
  781. if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
  782. return;
  783. /* Absolute time */
  784. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  785. print_graph_abs_time(iter->ts, s);
  786. /* Cpu */
  787. if (flags & TRACE_GRAPH_PRINT_CPU)
  788. print_graph_cpu(s, cpu);
  789. /* Proc */
  790. if (flags & TRACE_GRAPH_PRINT_PROC) {
  791. print_graph_proc(s, ent->pid);
  792. trace_seq_puts(s, " | ");
  793. }
  794. /* Latency format */
  795. if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
  796. print_graph_lat_fmt(s, ent);
  797. return;
  798. }
  799. /*
  800. * Entry check for irq code
  801. *
  802. * returns 1 if
  803. * - we are inside irq code
  804. * - we just entered irq code
  805. *
  806. * retunns 0 if
  807. * - funcgraph-interrupts option is set
  808. * - we are not inside irq code
  809. */
  810. static int
  811. check_irq_entry(struct trace_iterator *iter, u32 flags,
  812. unsigned long addr, int depth)
  813. {
  814. int cpu = iter->cpu;
  815. int *depth_irq;
  816. struct fgraph_data *data = iter->private;
  817. /*
  818. * If we are either displaying irqs, or we got called as
  819. * a graph event and private data does not exist,
  820. * then we bypass the irq check.
  821. */
  822. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  823. (!data))
  824. return 0;
  825. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  826. /*
  827. * We are inside the irq code
  828. */
  829. if (*depth_irq >= 0)
  830. return 1;
  831. if ((addr < (unsigned long)__irqentry_text_start) ||
  832. (addr >= (unsigned long)__irqentry_text_end))
  833. return 0;
  834. /*
  835. * We are entering irq code.
  836. */
  837. *depth_irq = depth;
  838. return 1;
  839. }
  840. /*
  841. * Return check for irq code
  842. *
  843. * returns 1 if
  844. * - we are inside irq code
  845. * - we just left irq code
  846. *
  847. * returns 0 if
  848. * - funcgraph-interrupts option is set
  849. * - we are not inside irq code
  850. */
  851. static int
  852. check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
  853. {
  854. int cpu = iter->cpu;
  855. int *depth_irq;
  856. struct fgraph_data *data = iter->private;
  857. /*
  858. * If we are either displaying irqs, or we got called as
  859. * a graph event and private data does not exist,
  860. * then we bypass the irq check.
  861. */
  862. if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
  863. (!data))
  864. return 0;
  865. depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  866. /*
  867. * We are not inside the irq code.
  868. */
  869. if (*depth_irq == -1)
  870. return 0;
  871. /*
  872. * We are inside the irq code, and this is returning entry.
  873. * Let's not trace it and clear the entry depth, since
  874. * we are out of irq code.
  875. *
  876. * This condition ensures that we 'leave the irq code' once
  877. * we are out of the entry depth. Thus protecting us from
  878. * the RETURN entry loss.
  879. */
  880. if (*depth_irq >= depth) {
  881. *depth_irq = -1;
  882. return 1;
  883. }
  884. /*
  885. * We are inside the irq code, and this is not the entry.
  886. */
  887. return 1;
  888. }
  889. static enum print_line_t
  890. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  891. struct trace_iterator *iter, u32 flags)
  892. {
  893. struct fgraph_data *data = iter->private;
  894. struct ftrace_graph_ent *call = &field->graph_ent;
  895. struct ftrace_graph_ret_entry *leaf_ret;
  896. static enum print_line_t ret;
  897. int cpu = iter->cpu;
  898. if (check_irq_entry(iter, flags, call->func, call->depth))
  899. return TRACE_TYPE_HANDLED;
  900. print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
  901. leaf_ret = get_return_for_leaf(iter, field);
  902. if (leaf_ret)
  903. ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
  904. else
  905. ret = print_graph_entry_nested(iter, field, s, cpu, flags);
  906. if (data) {
  907. /*
  908. * If we failed to write our output, then we need to make
  909. * note of it. Because we already consumed our entry.
  910. */
  911. if (s->full) {
  912. data->failed = 1;
  913. data->cpu = cpu;
  914. } else
  915. data->failed = 0;
  916. }
  917. return ret;
  918. }
  919. static enum print_line_t
  920. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  921. struct trace_entry *ent, struct trace_iterator *iter,
  922. u32 flags)
  923. {
  924. unsigned long long duration = trace->rettime - trace->calltime;
  925. struct fgraph_data *data = iter->private;
  926. struct trace_array *tr = iter->tr;
  927. pid_t pid = ent->pid;
  928. int cpu = iter->cpu;
  929. int func_match = 1;
  930. int i;
  931. if (check_irq_return(iter, flags, trace->depth))
  932. return TRACE_TYPE_HANDLED;
  933. if (data) {
  934. struct fgraph_cpu_data *cpu_data;
  935. int cpu = iter->cpu;
  936. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  937. /*
  938. * Comments display at + 1 to depth. This is the
  939. * return from a function, we now want the comments
  940. * to display at the same level of the bracket.
  941. */
  942. cpu_data->depth = trace->depth - 1;
  943. if (trace->depth < FTRACE_RETFUNC_DEPTH &&
  944. !WARN_ON_ONCE(trace->depth < 0)) {
  945. if (cpu_data->enter_funcs[trace->depth] != trace->func)
  946. func_match = 0;
  947. cpu_data->enter_funcs[trace->depth] = 0;
  948. }
  949. }
  950. print_graph_prologue(iter, s, 0, 0, flags);
  951. /* Overhead and duration */
  952. print_graph_duration(tr, duration, s, flags);
  953. /* Closing brace */
  954. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
  955. trace_seq_putc(s, ' ');
  956. /*
  957. * If the return function does not have a matching entry,
  958. * then the entry was lost. Instead of just printing
  959. * the '}' and letting the user guess what function this
  960. * belongs to, write out the function name. Always do
  961. * that if the funcgraph-tail option is enabled.
  962. */
  963. if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
  964. trace_seq_puts(s, "}\n");
  965. else
  966. trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
  967. /* Overrun */
  968. if (flags & TRACE_GRAPH_PRINT_OVERRUN)
  969. trace_seq_printf(s, " (Overruns: %lu)\n",
  970. trace->overrun);
  971. print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
  972. cpu, pid, flags);
  973. return trace_handle_return(s);
  974. }
  975. static enum print_line_t
  976. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  977. struct trace_iterator *iter, u32 flags)
  978. {
  979. struct trace_array *tr = iter->tr;
  980. unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
  981. struct fgraph_data *data = iter->private;
  982. struct trace_event *event;
  983. int depth = 0;
  984. int ret;
  985. int i;
  986. if (data)
  987. depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
  988. print_graph_prologue(iter, s, 0, 0, flags);
  989. /* No time */
  990. print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
  991. /* Indentation */
  992. if (depth > 0)
  993. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
  994. trace_seq_putc(s, ' ');
  995. /* The comment */
  996. trace_seq_puts(s, "/* ");
  997. switch (iter->ent->type) {
  998. case TRACE_BPUTS:
  999. ret = trace_print_bputs_msg_only(iter);
  1000. if (ret != TRACE_TYPE_HANDLED)
  1001. return ret;
  1002. break;
  1003. case TRACE_BPRINT:
  1004. ret = trace_print_bprintk_msg_only(iter);
  1005. if (ret != TRACE_TYPE_HANDLED)
  1006. return ret;
  1007. break;
  1008. case TRACE_PRINT:
  1009. ret = trace_print_printk_msg_only(iter);
  1010. if (ret != TRACE_TYPE_HANDLED)
  1011. return ret;
  1012. break;
  1013. default:
  1014. event = ftrace_find_event(ent->type);
  1015. if (!event)
  1016. return TRACE_TYPE_UNHANDLED;
  1017. ret = event->funcs->trace(iter, sym_flags, event);
  1018. if (ret != TRACE_TYPE_HANDLED)
  1019. return ret;
  1020. }
  1021. if (trace_seq_has_overflowed(s))
  1022. goto out;
  1023. /* Strip ending newline */
  1024. if (s->buffer[s->seq.len - 1] == '\n') {
  1025. s->buffer[s->seq.len - 1] = '\0';
  1026. s->seq.len--;
  1027. }
  1028. trace_seq_puts(s, " */\n");
  1029. out:
  1030. return trace_handle_return(s);
  1031. }
  1032. enum print_line_t
  1033. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  1034. {
  1035. struct ftrace_graph_ent_entry *field;
  1036. struct fgraph_data *data = iter->private;
  1037. struct trace_entry *entry = iter->ent;
  1038. struct trace_seq *s = &iter->seq;
  1039. int cpu = iter->cpu;
  1040. int ret;
  1041. if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
  1042. per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
  1043. return TRACE_TYPE_HANDLED;
  1044. }
  1045. /*
  1046. * If the last output failed, there's a possibility we need
  1047. * to print out the missing entry which would never go out.
  1048. */
  1049. if (data && data->failed) {
  1050. field = &data->ent;
  1051. iter->cpu = data->cpu;
  1052. ret = print_graph_entry(field, s, iter, flags);
  1053. if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
  1054. per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
  1055. ret = TRACE_TYPE_NO_CONSUME;
  1056. }
  1057. iter->cpu = cpu;
  1058. return ret;
  1059. }
  1060. switch (entry->type) {
  1061. case TRACE_GRAPH_ENT: {
  1062. /*
  1063. * print_graph_entry() may consume the current event,
  1064. * thus @field may become invalid, so we need to save it.
  1065. * sizeof(struct ftrace_graph_ent_entry) is very small,
  1066. * it can be safely saved at the stack.
  1067. */
  1068. struct ftrace_graph_ent_entry saved;
  1069. trace_assign_type(field, entry);
  1070. saved = *field;
  1071. return print_graph_entry(&saved, s, iter, flags);
  1072. }
  1073. case TRACE_GRAPH_RET: {
  1074. struct ftrace_graph_ret_entry *field;
  1075. trace_assign_type(field, entry);
  1076. return print_graph_return(&field->ret, s, entry, iter, flags);
  1077. }
  1078. case TRACE_STACK:
  1079. case TRACE_FN:
  1080. /* dont trace stack and functions as comments */
  1081. return TRACE_TYPE_UNHANDLED;
  1082. default:
  1083. return print_graph_comment(s, entry, iter, flags);
  1084. }
  1085. return TRACE_TYPE_HANDLED;
  1086. }
  1087. static enum print_line_t
  1088. print_graph_function(struct trace_iterator *iter)
  1089. {
  1090. return print_graph_function_flags(iter, tracer_flags.val);
  1091. }
  1092. static enum print_line_t
  1093. print_graph_function_event(struct trace_iterator *iter, int flags,
  1094. struct trace_event *event)
  1095. {
  1096. return print_graph_function(iter);
  1097. }
  1098. static void print_lat_header(struct seq_file *s, u32 flags)
  1099. {
  1100. static const char spaces[] = " " /* 16 spaces */
  1101. " " /* 4 spaces */
  1102. " "; /* 17 spaces */
  1103. int size = 0;
  1104. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1105. size += 16;
  1106. if (flags & TRACE_GRAPH_PRINT_CPU)
  1107. size += 4;
  1108. if (flags & TRACE_GRAPH_PRINT_PROC)
  1109. size += 17;
  1110. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  1111. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  1112. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  1113. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  1114. seq_printf(s, "#%.*s||| / \n", size, spaces);
  1115. }
  1116. static void __print_graph_headers_flags(struct trace_array *tr,
  1117. struct seq_file *s, u32 flags)
  1118. {
  1119. int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
  1120. if (lat)
  1121. print_lat_header(s, flags);
  1122. /* 1st line */
  1123. seq_putc(s, '#');
  1124. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1125. seq_puts(s, " TIME ");
  1126. if (flags & TRACE_GRAPH_PRINT_CPU)
  1127. seq_puts(s, " CPU");
  1128. if (flags & TRACE_GRAPH_PRINT_PROC)
  1129. seq_puts(s, " TASK/PID ");
  1130. if (lat)
  1131. seq_puts(s, "||||");
  1132. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1133. seq_puts(s, " DURATION ");
  1134. seq_puts(s, " FUNCTION CALLS\n");
  1135. /* 2nd line */
  1136. seq_putc(s, '#');
  1137. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  1138. seq_puts(s, " | ");
  1139. if (flags & TRACE_GRAPH_PRINT_CPU)
  1140. seq_puts(s, " | ");
  1141. if (flags & TRACE_GRAPH_PRINT_PROC)
  1142. seq_puts(s, " | | ");
  1143. if (lat)
  1144. seq_puts(s, "||||");
  1145. if (flags & TRACE_GRAPH_PRINT_DURATION)
  1146. seq_puts(s, " | | ");
  1147. seq_puts(s, " | | | |\n");
  1148. }
  1149. static void print_graph_headers(struct seq_file *s)
  1150. {
  1151. print_graph_headers_flags(s, tracer_flags.val);
  1152. }
  1153. void print_graph_headers_flags(struct seq_file *s, u32 flags)
  1154. {
  1155. struct trace_iterator *iter = s->private;
  1156. struct trace_array *tr = iter->tr;
  1157. if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
  1158. return;
  1159. if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
  1160. /* print nothing if the buffers are empty */
  1161. if (trace_empty(iter))
  1162. return;
  1163. print_trace_header(s, iter);
  1164. }
  1165. __print_graph_headers_flags(tr, s, flags);
  1166. }
  1167. void graph_trace_open(struct trace_iterator *iter)
  1168. {
  1169. /* pid and depth on the last trace processed */
  1170. struct fgraph_data *data;
  1171. gfp_t gfpflags;
  1172. int cpu;
  1173. iter->private = NULL;
  1174. /* We can be called in atomic context via ftrace_dump() */
  1175. gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
  1176. data = kzalloc(sizeof(*data), gfpflags);
  1177. if (!data)
  1178. goto out_err;
  1179. data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
  1180. if (!data->cpu_data)
  1181. goto out_err_free;
  1182. for_each_possible_cpu(cpu) {
  1183. pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  1184. int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
  1185. int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
  1186. int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
  1187. *pid = -1;
  1188. *depth = 0;
  1189. *ignore = 0;
  1190. *depth_irq = -1;
  1191. }
  1192. iter->private = data;
  1193. return;
  1194. out_err_free:
  1195. kfree(data);
  1196. out_err:
  1197. pr_warn("function graph tracer: not enough memory\n");
  1198. }
  1199. void graph_trace_close(struct trace_iterator *iter)
  1200. {
  1201. struct fgraph_data *data = iter->private;
  1202. if (data) {
  1203. free_percpu(data->cpu_data);
  1204. kfree(data);
  1205. }
  1206. }
  1207. static int
  1208. func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
  1209. {
  1210. if (bit == TRACE_GRAPH_PRINT_IRQS)
  1211. ftrace_graph_skip_irqs = !set;
  1212. if (bit == TRACE_GRAPH_SLEEP_TIME)
  1213. ftrace_graph_sleep_time_control(set);
  1214. if (bit == TRACE_GRAPH_GRAPH_TIME)
  1215. ftrace_graph_graph_time_control(set);
  1216. return 0;
  1217. }
  1218. static struct trace_event_functions graph_functions = {
  1219. .trace = print_graph_function_event,
  1220. };
  1221. static struct trace_event graph_trace_entry_event = {
  1222. .type = TRACE_GRAPH_ENT,
  1223. .funcs = &graph_functions,
  1224. };
  1225. static struct trace_event graph_trace_ret_event = {
  1226. .type = TRACE_GRAPH_RET,
  1227. .funcs = &graph_functions
  1228. };
  1229. static struct tracer graph_trace __tracer_data = {
  1230. .name = "function_graph",
  1231. .update_thresh = graph_trace_update_thresh,
  1232. .open = graph_trace_open,
  1233. .pipe_open = graph_trace_open,
  1234. .close = graph_trace_close,
  1235. .pipe_close = graph_trace_close,
  1236. .init = graph_trace_init,
  1237. .reset = graph_trace_reset,
  1238. .print_line = print_graph_function,
  1239. .print_header = print_graph_headers,
  1240. .flags = &tracer_flags,
  1241. .set_flag = func_graph_set_flag,
  1242. #ifdef CONFIG_FTRACE_SELFTEST
  1243. .selftest = trace_selftest_startup_function_graph,
  1244. #endif
  1245. };
  1246. static ssize_t
  1247. graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
  1248. loff_t *ppos)
  1249. {
  1250. unsigned long val;
  1251. int ret;
  1252. ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
  1253. if (ret)
  1254. return ret;
  1255. max_depth = val;
  1256. *ppos += cnt;
  1257. return cnt;
  1258. }
  1259. static ssize_t
  1260. graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
  1261. loff_t *ppos)
  1262. {
  1263. char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
  1264. int n;
  1265. n = sprintf(buf, "%d\n", max_depth);
  1266. return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
  1267. }
  1268. static const struct file_operations graph_depth_fops = {
  1269. .open = tracing_open_generic,
  1270. .write = graph_depth_write,
  1271. .read = graph_depth_read,
  1272. .llseek = generic_file_llseek,
  1273. };
  1274. static __init int init_graph_tracefs(void)
  1275. {
  1276. struct dentry *d_tracer;
  1277. d_tracer = tracing_init_dentry();
  1278. if (IS_ERR(d_tracer))
  1279. return 0;
  1280. trace_create_file("max_graph_depth", 0644, d_tracer,
  1281. NULL, &graph_depth_fops);
  1282. return 0;
  1283. }
  1284. fs_initcall(init_graph_tracefs);
  1285. static __init int init_graph_trace(void)
  1286. {
  1287. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  1288. if (!register_trace_event(&graph_trace_entry_event)) {
  1289. pr_warn("Warning: could not register graph trace events\n");
  1290. return 1;
  1291. }
  1292. if (!register_trace_event(&graph_trace_ret_event)) {
  1293. pr_warn("Warning: could not register graph trace events\n");
  1294. return 1;
  1295. }
  1296. return register_tracer(&graph_trace);
  1297. }
  1298. core_initcall(init_graph_trace);