trace_functions_graph.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090
  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/debugfs.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/ftrace.h>
  12. #include <linux/fs.h>
  13. #include "trace.h"
  14. #include "trace_output.h"
  15. struct fgraph_data {
  16. pid_t last_pid;
  17. int depth;
  18. };
  19. #define TRACE_GRAPH_INDENT 2
  20. /* Flag options */
  21. #define TRACE_GRAPH_PRINT_OVERRUN 0x1
  22. #define TRACE_GRAPH_PRINT_CPU 0x2
  23. #define TRACE_GRAPH_PRINT_OVERHEAD 0x4
  24. #define TRACE_GRAPH_PRINT_PROC 0x8
  25. #define TRACE_GRAPH_PRINT_DURATION 0x10
  26. #define TRACE_GRAPH_PRINT_ABS_TIME 0X20
  27. static struct tracer_opt trace_opts[] = {
  28. /* Display overruns? (for self-debug purpose) */
  29. { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
  30. /* Display CPU ? */
  31. { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
  32. /* Display Overhead ? */
  33. { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
  34. /* Display proc name/pid */
  35. { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
  36. /* Display duration of execution */
  37. { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
  38. /* Display absolute time of an entry */
  39. { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
  40. { } /* Empty entry */
  41. };
  42. static struct tracer_flags tracer_flags = {
  43. /* Don't display overruns and proc by default */
  44. .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
  45. TRACE_GRAPH_PRINT_DURATION,
  46. .opts = trace_opts
  47. };
  48. static struct trace_array *graph_array;
  49. /* Add a function return address to the trace stack on thread info.*/
  50. int
  51. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  52. unsigned long frame_pointer)
  53. {
  54. unsigned long long calltime;
  55. int index;
  56. if (!current->ret_stack)
  57. return -EBUSY;
  58. /*
  59. * We must make sure the ret_stack is tested before we read
  60. * anything else.
  61. */
  62. smp_rmb();
  63. /* The return trace stack is full */
  64. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  65. atomic_inc(&current->trace_overrun);
  66. return -EBUSY;
  67. }
  68. calltime = trace_clock_local();
  69. index = ++current->curr_ret_stack;
  70. barrier();
  71. current->ret_stack[index].ret = ret;
  72. current->ret_stack[index].func = func;
  73. current->ret_stack[index].calltime = calltime;
  74. current->ret_stack[index].subtime = 0;
  75. current->ret_stack[index].fp = frame_pointer;
  76. *depth = index;
  77. return 0;
  78. }
  79. /* Retrieve a function return address to the trace stack on thread info.*/
  80. static void
  81. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret,
  82. unsigned long frame_pointer)
  83. {
  84. int index;
  85. index = current->curr_ret_stack;
  86. if (unlikely(index < 0)) {
  87. ftrace_graph_stop();
  88. WARN_ON(1);
  89. /* Might as well panic, otherwise we have no where to go */
  90. *ret = (unsigned long)panic;
  91. return;
  92. }
  93. #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FP_TEST
  94. /*
  95. * The arch may choose to record the frame pointer used
  96. * and check it here to make sure that it is what we expect it
  97. * to be. If gcc does not set the place holder of the return
  98. * address in the frame pointer, and does a copy instead, then
  99. * the function graph trace will fail. This test detects this
  100. * case.
  101. *
  102. * Currently, x86_32 with optimize for size (-Os) makes the latest
  103. * gcc do the above.
  104. */
  105. if (unlikely(current->ret_stack[index].fp != frame_pointer)) {
  106. ftrace_graph_stop();
  107. WARN(1, "Bad frame pointer: expected %lx, received %lx\n"
  108. " from func %pF return to %lx\n",
  109. current->ret_stack[index].fp,
  110. frame_pointer,
  111. (void *)current->ret_stack[index].func,
  112. current->ret_stack[index].ret);
  113. *ret = (unsigned long)panic;
  114. return;
  115. }
  116. #endif
  117. *ret = current->ret_stack[index].ret;
  118. trace->func = current->ret_stack[index].func;
  119. trace->calltime = current->ret_stack[index].calltime;
  120. trace->overrun = atomic_read(&current->trace_overrun);
  121. trace->depth = index;
  122. }
  123. /*
  124. * Send the trace to the ring-buffer.
  125. * @return the original return address.
  126. */
  127. unsigned long ftrace_return_to_handler(unsigned long frame_pointer)
  128. {
  129. struct ftrace_graph_ret trace;
  130. unsigned long ret;
  131. ftrace_pop_return_trace(&trace, &ret, frame_pointer);
  132. trace.rettime = trace_clock_local();
  133. ftrace_graph_return(&trace);
  134. barrier();
  135. current->curr_ret_stack--;
  136. if (unlikely(!ret)) {
  137. ftrace_graph_stop();
  138. WARN_ON(1);
  139. /* Might as well panic. What else to do? */
  140. ret = (unsigned long)panic;
  141. }
  142. return ret;
  143. }
  144. static int __trace_graph_entry(struct trace_array *tr,
  145. struct ftrace_graph_ent *trace,
  146. unsigned long flags,
  147. int pc)
  148. {
  149. struct ftrace_event_call *call = &event_funcgraph_entry;
  150. struct ring_buffer_event *event;
  151. struct ring_buffer *buffer = tr->buffer;
  152. struct ftrace_graph_ent_entry *entry;
  153. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  154. return 0;
  155. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
  156. sizeof(*entry), flags, pc);
  157. if (!event)
  158. return 0;
  159. entry = ring_buffer_event_data(event);
  160. entry->graph_ent = *trace;
  161. if (!filter_current_check_discard(buffer, call, entry, event))
  162. ring_buffer_unlock_commit(buffer, event);
  163. return 1;
  164. }
  165. int trace_graph_entry(struct ftrace_graph_ent *trace)
  166. {
  167. struct trace_array *tr = graph_array;
  168. struct trace_array_cpu *data;
  169. unsigned long flags;
  170. long disabled;
  171. int ret;
  172. int cpu;
  173. int pc;
  174. if (unlikely(!tr))
  175. return 0;
  176. if (!ftrace_trace_task(current))
  177. return 0;
  178. if (!ftrace_graph_addr(trace->func))
  179. return 0;
  180. local_irq_save(flags);
  181. cpu = raw_smp_processor_id();
  182. data = tr->data[cpu];
  183. disabled = atomic_inc_return(&data->disabled);
  184. if (likely(disabled == 1)) {
  185. pc = preempt_count();
  186. ret = __trace_graph_entry(tr, trace, flags, pc);
  187. } else {
  188. ret = 0;
  189. }
  190. /* Only do the atomic if it is not already set */
  191. if (!test_tsk_trace_graph(current))
  192. set_tsk_trace_graph(current);
  193. atomic_dec(&data->disabled);
  194. local_irq_restore(flags);
  195. return ret;
  196. }
  197. static void __trace_graph_return(struct trace_array *tr,
  198. struct ftrace_graph_ret *trace,
  199. unsigned long flags,
  200. int pc)
  201. {
  202. struct ftrace_event_call *call = &event_funcgraph_exit;
  203. struct ring_buffer_event *event;
  204. struct ring_buffer *buffer = tr->buffer;
  205. struct ftrace_graph_ret_entry *entry;
  206. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  207. return;
  208. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
  209. sizeof(*entry), flags, pc);
  210. if (!event)
  211. return;
  212. entry = ring_buffer_event_data(event);
  213. entry->ret = *trace;
  214. if (!filter_current_check_discard(buffer, call, entry, event))
  215. ring_buffer_unlock_commit(buffer, event);
  216. }
  217. void trace_graph_return(struct ftrace_graph_ret *trace)
  218. {
  219. struct trace_array *tr = graph_array;
  220. struct trace_array_cpu *data;
  221. unsigned long flags;
  222. long disabled;
  223. int cpu;
  224. int pc;
  225. local_irq_save(flags);
  226. cpu = raw_smp_processor_id();
  227. data = tr->data[cpu];
  228. disabled = atomic_inc_return(&data->disabled);
  229. if (likely(disabled == 1)) {
  230. pc = preempt_count();
  231. __trace_graph_return(tr, trace, flags, pc);
  232. }
  233. if (!trace->depth)
  234. clear_tsk_trace_graph(current);
  235. atomic_dec(&data->disabled);
  236. local_irq_restore(flags);
  237. }
  238. static int graph_trace_init(struct trace_array *tr)
  239. {
  240. int ret;
  241. graph_array = tr;
  242. ret = register_ftrace_graph(&trace_graph_return,
  243. &trace_graph_entry);
  244. if (ret)
  245. return ret;
  246. tracing_start_cmdline_record();
  247. return 0;
  248. }
  249. void set_graph_array(struct trace_array *tr)
  250. {
  251. graph_array = tr;
  252. }
  253. static void graph_trace_reset(struct trace_array *tr)
  254. {
  255. tracing_stop_cmdline_record();
  256. unregister_ftrace_graph();
  257. }
  258. static int max_bytes_for_cpu;
  259. static enum print_line_t
  260. print_graph_cpu(struct trace_seq *s, int cpu)
  261. {
  262. int ret;
  263. /*
  264. * Start with a space character - to make it stand out
  265. * to the right a bit when trace output is pasted into
  266. * email:
  267. */
  268. ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
  269. if (!ret)
  270. return TRACE_TYPE_PARTIAL_LINE;
  271. return TRACE_TYPE_HANDLED;
  272. }
  273. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  274. static enum print_line_t
  275. print_graph_proc(struct trace_seq *s, pid_t pid)
  276. {
  277. char comm[TASK_COMM_LEN];
  278. /* sign + log10(MAX_INT) + '\0' */
  279. char pid_str[11];
  280. int spaces = 0;
  281. int ret;
  282. int len;
  283. int i;
  284. trace_find_cmdline(pid, comm);
  285. comm[7] = '\0';
  286. sprintf(pid_str, "%d", pid);
  287. /* 1 stands for the "-" character */
  288. len = strlen(comm) + strlen(pid_str) + 1;
  289. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  290. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  291. /* First spaces to align center */
  292. for (i = 0; i < spaces / 2; i++) {
  293. ret = trace_seq_printf(s, " ");
  294. if (!ret)
  295. return TRACE_TYPE_PARTIAL_LINE;
  296. }
  297. ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
  298. if (!ret)
  299. return TRACE_TYPE_PARTIAL_LINE;
  300. /* Last spaces to align center */
  301. for (i = 0; i < spaces - (spaces / 2); i++) {
  302. ret = trace_seq_printf(s, " ");
  303. if (!ret)
  304. return TRACE_TYPE_PARTIAL_LINE;
  305. }
  306. return TRACE_TYPE_HANDLED;
  307. }
  308. static enum print_line_t
  309. print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  310. {
  311. int hardirq, softirq;
  312. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  313. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  314. if (!trace_seq_printf(s, " %c%c%c",
  315. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  316. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  317. 'X' : '.',
  318. (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
  319. 'N' : '.',
  320. (hardirq && softirq) ? 'H' :
  321. hardirq ? 'h' : softirq ? 's' : '.'))
  322. return 0;
  323. if (entry->preempt_count)
  324. return trace_seq_printf(s, "%x", entry->preempt_count);
  325. return trace_seq_puts(s, ".");
  326. }
  327. /* If the pid changed since the last trace, output this event */
  328. static enum print_line_t
  329. verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
  330. {
  331. pid_t prev_pid;
  332. pid_t *last_pid;
  333. int ret;
  334. if (!data)
  335. return TRACE_TYPE_HANDLED;
  336. last_pid = &(per_cpu_ptr(data, cpu)->last_pid);
  337. if (*last_pid == pid)
  338. return TRACE_TYPE_HANDLED;
  339. prev_pid = *last_pid;
  340. *last_pid = pid;
  341. if (prev_pid == -1)
  342. return TRACE_TYPE_HANDLED;
  343. /*
  344. * Context-switch trace line:
  345. ------------------------------------------
  346. | 1) migration/0--1 => sshd-1755
  347. ------------------------------------------
  348. */
  349. ret = trace_seq_printf(s,
  350. " ------------------------------------------\n");
  351. if (!ret)
  352. return TRACE_TYPE_PARTIAL_LINE;
  353. ret = print_graph_cpu(s, cpu);
  354. if (ret == TRACE_TYPE_PARTIAL_LINE)
  355. return TRACE_TYPE_PARTIAL_LINE;
  356. ret = print_graph_proc(s, prev_pid);
  357. if (ret == TRACE_TYPE_PARTIAL_LINE)
  358. return TRACE_TYPE_PARTIAL_LINE;
  359. ret = trace_seq_printf(s, " => ");
  360. if (!ret)
  361. return TRACE_TYPE_PARTIAL_LINE;
  362. ret = print_graph_proc(s, pid);
  363. if (ret == TRACE_TYPE_PARTIAL_LINE)
  364. return TRACE_TYPE_PARTIAL_LINE;
  365. ret = trace_seq_printf(s,
  366. "\n ------------------------------------------\n\n");
  367. if (!ret)
  368. return TRACE_TYPE_PARTIAL_LINE;
  369. return TRACE_TYPE_HANDLED;
  370. }
  371. static struct ftrace_graph_ret_entry *
  372. get_return_for_leaf(struct trace_iterator *iter,
  373. struct ftrace_graph_ent_entry *curr)
  374. {
  375. struct ring_buffer_iter *ring_iter;
  376. struct ring_buffer_event *event;
  377. struct ftrace_graph_ret_entry *next;
  378. ring_iter = iter->buffer_iter[iter->cpu];
  379. /* First peek to compare current entry and the next one */
  380. if (ring_iter)
  381. event = ring_buffer_iter_peek(ring_iter, NULL);
  382. else {
  383. /* We need to consume the current entry to see the next one */
  384. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  385. event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
  386. NULL);
  387. }
  388. if (!event)
  389. return NULL;
  390. next = ring_buffer_event_data(event);
  391. if (next->ent.type != TRACE_GRAPH_RET)
  392. return NULL;
  393. if (curr->ent.pid != next->ent.pid ||
  394. curr->graph_ent.func != next->ret.func)
  395. return NULL;
  396. /* this is a leaf, now advance the iterator */
  397. if (ring_iter)
  398. ring_buffer_read(ring_iter, NULL);
  399. return next;
  400. }
  401. /* Signal a overhead of time execution to the output */
  402. static int
  403. print_graph_overhead(unsigned long long duration, struct trace_seq *s)
  404. {
  405. /* If duration disappear, we don't need anything */
  406. if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
  407. return 1;
  408. /* Non nested entry or return */
  409. if (duration == -1)
  410. return trace_seq_printf(s, " ");
  411. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
  412. /* Duration exceeded 100 msecs */
  413. if (duration > 100000ULL)
  414. return trace_seq_printf(s, "! ");
  415. /* Duration exceeded 10 msecs */
  416. if (duration > 10000ULL)
  417. return trace_seq_printf(s, "+ ");
  418. }
  419. return trace_seq_printf(s, " ");
  420. }
  421. static int print_graph_abs_time(u64 t, struct trace_seq *s)
  422. {
  423. unsigned long usecs_rem;
  424. usecs_rem = do_div(t, NSEC_PER_SEC);
  425. usecs_rem /= 1000;
  426. return trace_seq_printf(s, "%5lu.%06lu | ",
  427. (unsigned long)t, usecs_rem);
  428. }
  429. static enum print_line_t
  430. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  431. enum trace_type type, int cpu, pid_t pid)
  432. {
  433. int ret;
  434. struct trace_seq *s = &iter->seq;
  435. if (addr < (unsigned long)__irqentry_text_start ||
  436. addr >= (unsigned long)__irqentry_text_end)
  437. return TRACE_TYPE_UNHANDLED;
  438. /* Absolute time */
  439. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  440. ret = print_graph_abs_time(iter->ts, s);
  441. if (!ret)
  442. return TRACE_TYPE_PARTIAL_LINE;
  443. }
  444. /* Cpu */
  445. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  446. ret = print_graph_cpu(s, cpu);
  447. if (ret == TRACE_TYPE_PARTIAL_LINE)
  448. return TRACE_TYPE_PARTIAL_LINE;
  449. }
  450. /* Proc */
  451. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  452. ret = print_graph_proc(s, pid);
  453. if (ret == TRACE_TYPE_PARTIAL_LINE)
  454. return TRACE_TYPE_PARTIAL_LINE;
  455. ret = trace_seq_printf(s, " | ");
  456. if (!ret)
  457. return TRACE_TYPE_PARTIAL_LINE;
  458. }
  459. /* No overhead */
  460. ret = print_graph_overhead(-1, s);
  461. if (!ret)
  462. return TRACE_TYPE_PARTIAL_LINE;
  463. if (type == TRACE_GRAPH_ENT)
  464. ret = trace_seq_printf(s, "==========>");
  465. else
  466. ret = trace_seq_printf(s, "<==========");
  467. if (!ret)
  468. return TRACE_TYPE_PARTIAL_LINE;
  469. /* Don't close the duration column if haven't one */
  470. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  471. trace_seq_printf(s, " |");
  472. ret = trace_seq_printf(s, "\n");
  473. if (!ret)
  474. return TRACE_TYPE_PARTIAL_LINE;
  475. return TRACE_TYPE_HANDLED;
  476. }
  477. enum print_line_t
  478. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
  479. {
  480. unsigned long nsecs_rem = do_div(duration, 1000);
  481. /* log10(ULONG_MAX) + '\0' */
  482. char msecs_str[21];
  483. char nsecs_str[5];
  484. int ret, len;
  485. int i;
  486. sprintf(msecs_str, "%lu", (unsigned long) duration);
  487. /* Print msecs */
  488. ret = trace_seq_printf(s, "%s", msecs_str);
  489. if (!ret)
  490. return TRACE_TYPE_PARTIAL_LINE;
  491. len = strlen(msecs_str);
  492. /* Print nsecs (we don't want to exceed 7 numbers) */
  493. if (len < 7) {
  494. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  495. ret = trace_seq_printf(s, ".%s", nsecs_str);
  496. if (!ret)
  497. return TRACE_TYPE_PARTIAL_LINE;
  498. len += strlen(nsecs_str);
  499. }
  500. ret = trace_seq_printf(s, " us ");
  501. if (!ret)
  502. return TRACE_TYPE_PARTIAL_LINE;
  503. /* Print remaining spaces to fit the row's width */
  504. for (i = len; i < 7; i++) {
  505. ret = trace_seq_printf(s, " ");
  506. if (!ret)
  507. return TRACE_TYPE_PARTIAL_LINE;
  508. }
  509. return TRACE_TYPE_HANDLED;
  510. }
  511. static enum print_line_t
  512. print_graph_duration(unsigned long long duration, struct trace_seq *s)
  513. {
  514. int ret;
  515. ret = trace_print_graph_duration(duration, s);
  516. if (ret != TRACE_TYPE_HANDLED)
  517. return ret;
  518. ret = trace_seq_printf(s, "| ");
  519. if (!ret)
  520. return TRACE_TYPE_PARTIAL_LINE;
  521. return TRACE_TYPE_HANDLED;
  522. }
  523. /* Case of a leaf function on its call entry */
  524. static enum print_line_t
  525. print_graph_entry_leaf(struct trace_iterator *iter,
  526. struct ftrace_graph_ent_entry *entry,
  527. struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
  528. {
  529. struct fgraph_data *data = iter->private;
  530. struct ftrace_graph_ret *graph_ret;
  531. struct ftrace_graph_ent *call;
  532. unsigned long long duration;
  533. int ret;
  534. int i;
  535. graph_ret = &ret_entry->ret;
  536. call = &entry->graph_ent;
  537. duration = graph_ret->rettime - graph_ret->calltime;
  538. if (data) {
  539. int cpu = iter->cpu;
  540. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  541. /*
  542. * Comments display at + 1 to depth. Since
  543. * this is a leaf function, keep the comments
  544. * equal to this depth.
  545. */
  546. *depth = call->depth - 1;
  547. }
  548. /* Overhead */
  549. ret = print_graph_overhead(duration, s);
  550. if (!ret)
  551. return TRACE_TYPE_PARTIAL_LINE;
  552. /* Duration */
  553. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  554. ret = print_graph_duration(duration, s);
  555. if (ret == TRACE_TYPE_PARTIAL_LINE)
  556. return TRACE_TYPE_PARTIAL_LINE;
  557. }
  558. /* Function */
  559. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  560. ret = trace_seq_printf(s, " ");
  561. if (!ret)
  562. return TRACE_TYPE_PARTIAL_LINE;
  563. }
  564. ret = trace_seq_printf(s, "%pf();\n", (void *)call->func);
  565. if (!ret)
  566. return TRACE_TYPE_PARTIAL_LINE;
  567. return TRACE_TYPE_HANDLED;
  568. }
  569. static enum print_line_t
  570. print_graph_entry_nested(struct trace_iterator *iter,
  571. struct ftrace_graph_ent_entry *entry,
  572. struct trace_seq *s, int cpu)
  573. {
  574. struct ftrace_graph_ent *call = &entry->graph_ent;
  575. struct fgraph_data *data = iter->private;
  576. int ret;
  577. int i;
  578. if (data) {
  579. int cpu = iter->cpu;
  580. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  581. *depth = call->depth;
  582. }
  583. /* No overhead */
  584. ret = print_graph_overhead(-1, s);
  585. if (!ret)
  586. return TRACE_TYPE_PARTIAL_LINE;
  587. /* No time */
  588. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  589. ret = trace_seq_printf(s, " | ");
  590. if (!ret)
  591. return TRACE_TYPE_PARTIAL_LINE;
  592. }
  593. /* Function */
  594. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  595. ret = trace_seq_printf(s, " ");
  596. if (!ret)
  597. return TRACE_TYPE_PARTIAL_LINE;
  598. }
  599. ret = trace_seq_printf(s, "%pf() {\n", (void *)call->func);
  600. if (!ret)
  601. return TRACE_TYPE_PARTIAL_LINE;
  602. /*
  603. * we already consumed the current entry to check the next one
  604. * and see if this is a leaf.
  605. */
  606. return TRACE_TYPE_NO_CONSUME;
  607. }
  608. static enum print_line_t
  609. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  610. int type, unsigned long addr)
  611. {
  612. struct fgraph_data *data = iter->private;
  613. struct trace_entry *ent = iter->ent;
  614. int cpu = iter->cpu;
  615. int ret;
  616. /* Pid */
  617. if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
  618. return TRACE_TYPE_PARTIAL_LINE;
  619. if (type) {
  620. /* Interrupt */
  621. ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
  622. if (ret == TRACE_TYPE_PARTIAL_LINE)
  623. return TRACE_TYPE_PARTIAL_LINE;
  624. }
  625. /* Absolute time */
  626. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  627. ret = print_graph_abs_time(iter->ts, s);
  628. if (!ret)
  629. return TRACE_TYPE_PARTIAL_LINE;
  630. }
  631. /* Cpu */
  632. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  633. ret = print_graph_cpu(s, cpu);
  634. if (ret == TRACE_TYPE_PARTIAL_LINE)
  635. return TRACE_TYPE_PARTIAL_LINE;
  636. }
  637. /* Proc */
  638. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  639. ret = print_graph_proc(s, ent->pid);
  640. if (ret == TRACE_TYPE_PARTIAL_LINE)
  641. return TRACE_TYPE_PARTIAL_LINE;
  642. ret = trace_seq_printf(s, " | ");
  643. if (!ret)
  644. return TRACE_TYPE_PARTIAL_LINE;
  645. }
  646. /* Latency format */
  647. if (trace_flags & TRACE_ITER_LATENCY_FMT) {
  648. ret = print_graph_lat_fmt(s, ent);
  649. if (ret == TRACE_TYPE_PARTIAL_LINE)
  650. return TRACE_TYPE_PARTIAL_LINE;
  651. }
  652. return 0;
  653. }
  654. static enum print_line_t
  655. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  656. struct trace_iterator *iter)
  657. {
  658. int cpu = iter->cpu;
  659. struct ftrace_graph_ent *call = &field->graph_ent;
  660. struct ftrace_graph_ret_entry *leaf_ret;
  661. if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
  662. return TRACE_TYPE_PARTIAL_LINE;
  663. leaf_ret = get_return_for_leaf(iter, field);
  664. if (leaf_ret)
  665. return print_graph_entry_leaf(iter, field, leaf_ret, s);
  666. else
  667. return print_graph_entry_nested(iter, field, s, cpu);
  668. }
  669. static enum print_line_t
  670. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  671. struct trace_entry *ent, struct trace_iterator *iter)
  672. {
  673. unsigned long long duration = trace->rettime - trace->calltime;
  674. struct fgraph_data *data = iter->private;
  675. pid_t pid = ent->pid;
  676. int cpu = iter->cpu;
  677. int ret;
  678. int i;
  679. if (data) {
  680. int cpu = iter->cpu;
  681. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  682. /*
  683. * Comments display at + 1 to depth. This is the
  684. * return from a function, we now want the comments
  685. * to display at the same level of the bracket.
  686. */
  687. *depth = trace->depth - 1;
  688. }
  689. if (print_graph_prologue(iter, s, 0, 0))
  690. return TRACE_TYPE_PARTIAL_LINE;
  691. /* Overhead */
  692. ret = print_graph_overhead(duration, s);
  693. if (!ret)
  694. return TRACE_TYPE_PARTIAL_LINE;
  695. /* Duration */
  696. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  697. ret = print_graph_duration(duration, s);
  698. if (ret == TRACE_TYPE_PARTIAL_LINE)
  699. return TRACE_TYPE_PARTIAL_LINE;
  700. }
  701. /* Closing brace */
  702. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
  703. ret = trace_seq_printf(s, " ");
  704. if (!ret)
  705. return TRACE_TYPE_PARTIAL_LINE;
  706. }
  707. ret = trace_seq_printf(s, "}\n");
  708. if (!ret)
  709. return TRACE_TYPE_PARTIAL_LINE;
  710. /* Overrun */
  711. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
  712. ret = trace_seq_printf(s, " (Overruns: %lu)\n",
  713. trace->overrun);
  714. if (!ret)
  715. return TRACE_TYPE_PARTIAL_LINE;
  716. }
  717. ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
  718. if (ret == TRACE_TYPE_PARTIAL_LINE)
  719. return TRACE_TYPE_PARTIAL_LINE;
  720. return TRACE_TYPE_HANDLED;
  721. }
  722. static enum print_line_t
  723. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  724. struct trace_iterator *iter)
  725. {
  726. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  727. struct fgraph_data *data = iter->private;
  728. struct trace_event *event;
  729. int depth = 0;
  730. int ret;
  731. int i;
  732. if (data)
  733. depth = per_cpu_ptr(data, iter->cpu)->depth;
  734. if (print_graph_prologue(iter, s, 0, 0))
  735. return TRACE_TYPE_PARTIAL_LINE;
  736. /* No overhead */
  737. ret = print_graph_overhead(-1, s);
  738. if (!ret)
  739. return TRACE_TYPE_PARTIAL_LINE;
  740. /* No time */
  741. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  742. ret = trace_seq_printf(s, " | ");
  743. if (!ret)
  744. return TRACE_TYPE_PARTIAL_LINE;
  745. }
  746. /* Indentation */
  747. if (depth > 0)
  748. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
  749. ret = trace_seq_printf(s, " ");
  750. if (!ret)
  751. return TRACE_TYPE_PARTIAL_LINE;
  752. }
  753. /* The comment */
  754. ret = trace_seq_printf(s, "/* ");
  755. if (!ret)
  756. return TRACE_TYPE_PARTIAL_LINE;
  757. switch (iter->ent->type) {
  758. case TRACE_BPRINT:
  759. ret = trace_print_bprintk_msg_only(iter);
  760. if (ret != TRACE_TYPE_HANDLED)
  761. return ret;
  762. break;
  763. case TRACE_PRINT:
  764. ret = trace_print_printk_msg_only(iter);
  765. if (ret != TRACE_TYPE_HANDLED)
  766. return ret;
  767. break;
  768. default:
  769. event = ftrace_find_event(ent->type);
  770. if (!event)
  771. return TRACE_TYPE_UNHANDLED;
  772. ret = event->trace(iter, sym_flags);
  773. if (ret != TRACE_TYPE_HANDLED)
  774. return ret;
  775. }
  776. /* Strip ending newline */
  777. if (s->buffer[s->len - 1] == '\n') {
  778. s->buffer[s->len - 1] = '\0';
  779. s->len--;
  780. }
  781. ret = trace_seq_printf(s, " */\n");
  782. if (!ret)
  783. return TRACE_TYPE_PARTIAL_LINE;
  784. return TRACE_TYPE_HANDLED;
  785. }
  786. enum print_line_t
  787. print_graph_function(struct trace_iterator *iter)
  788. {
  789. struct trace_entry *entry = iter->ent;
  790. struct trace_seq *s = &iter->seq;
  791. switch (entry->type) {
  792. case TRACE_GRAPH_ENT: {
  793. /*
  794. * print_graph_entry() may consume the current event,
  795. * thus @field may become invalid, so we need to save it.
  796. * sizeof(struct ftrace_graph_ent_entry) is very small,
  797. * it can be safely saved at the stack.
  798. */
  799. struct ftrace_graph_ent_entry *field, saved;
  800. trace_assign_type(field, entry);
  801. saved = *field;
  802. return print_graph_entry(&saved, s, iter);
  803. }
  804. case TRACE_GRAPH_RET: {
  805. struct ftrace_graph_ret_entry *field;
  806. trace_assign_type(field, entry);
  807. return print_graph_return(&field->ret, s, entry, iter);
  808. }
  809. default:
  810. return print_graph_comment(s, entry, iter);
  811. }
  812. return TRACE_TYPE_HANDLED;
  813. }
  814. static void print_lat_header(struct seq_file *s)
  815. {
  816. static const char spaces[] = " " /* 16 spaces */
  817. " " /* 4 spaces */
  818. " "; /* 17 spaces */
  819. int size = 0;
  820. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  821. size += 16;
  822. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  823. size += 4;
  824. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  825. size += 17;
  826. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  827. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  828. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  829. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  830. seq_printf(s, "#%.*s||| / \n", size, spaces);
  831. seq_printf(s, "#%.*s|||| \n", size, spaces);
  832. }
  833. static void print_graph_headers(struct seq_file *s)
  834. {
  835. int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
  836. if (lat)
  837. print_lat_header(s);
  838. /* 1st line */
  839. seq_printf(s, "#");
  840. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  841. seq_printf(s, " TIME ");
  842. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  843. seq_printf(s, " CPU");
  844. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  845. seq_printf(s, " TASK/PID ");
  846. if (lat)
  847. seq_printf(s, "||||");
  848. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  849. seq_printf(s, " DURATION ");
  850. seq_printf(s, " FUNCTION CALLS\n");
  851. /* 2nd line */
  852. seq_printf(s, "#");
  853. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  854. seq_printf(s, " | ");
  855. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  856. seq_printf(s, " | ");
  857. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  858. seq_printf(s, " | | ");
  859. if (lat)
  860. seq_printf(s, "||||");
  861. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  862. seq_printf(s, " | | ");
  863. seq_printf(s, " | | | |\n");
  864. }
  865. static void graph_trace_open(struct trace_iterator *iter)
  866. {
  867. /* pid and depth on the last trace processed */
  868. struct fgraph_data *data = alloc_percpu(struct fgraph_data);
  869. int cpu;
  870. if (!data)
  871. pr_warning("function graph tracer: not enough memory\n");
  872. else
  873. for_each_possible_cpu(cpu) {
  874. pid_t *pid = &(per_cpu_ptr(data, cpu)->last_pid);
  875. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  876. *pid = -1;
  877. *depth = 0;
  878. }
  879. iter->private = data;
  880. }
  881. static void graph_trace_close(struct trace_iterator *iter)
  882. {
  883. free_percpu(iter->private);
  884. }
  885. static struct tracer graph_trace __read_mostly = {
  886. .name = "function_graph",
  887. .open = graph_trace_open,
  888. .close = graph_trace_close,
  889. .wait_pipe = poll_wait_pipe,
  890. .init = graph_trace_init,
  891. .reset = graph_trace_reset,
  892. .print_line = print_graph_function,
  893. .print_header = print_graph_headers,
  894. .flags = &tracer_flags,
  895. #ifdef CONFIG_FTRACE_SELFTEST
  896. .selftest = trace_selftest_startup_function_graph,
  897. #endif
  898. };
  899. static __init int init_graph_trace(void)
  900. {
  901. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  902. return register_tracer(&graph_trace);
  903. }
  904. device_initcall(init_graph_trace);