trace_functions_graph.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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. /* pid on the last trace processed */
  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 long time,
  52. unsigned long func, int *depth)
  53. {
  54. int index;
  55. if (!current->ret_stack)
  56. return -EBUSY;
  57. /* The return trace stack is full */
  58. if (current->curr_ret_stack == FTRACE_RETFUNC_DEPTH - 1) {
  59. atomic_inc(&current->trace_overrun);
  60. return -EBUSY;
  61. }
  62. index = ++current->curr_ret_stack;
  63. barrier();
  64. current->ret_stack[index].ret = ret;
  65. current->ret_stack[index].func = func;
  66. current->ret_stack[index].calltime = time;
  67. *depth = index;
  68. return 0;
  69. }
  70. /* Retrieve a function return address to the trace stack on thread info.*/
  71. void
  72. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret)
  73. {
  74. int index;
  75. index = current->curr_ret_stack;
  76. if (unlikely(index < 0)) {
  77. ftrace_graph_stop();
  78. WARN_ON(1);
  79. /* Might as well panic, otherwise we have no where to go */
  80. *ret = (unsigned long)panic;
  81. return;
  82. }
  83. *ret = current->ret_stack[index].ret;
  84. trace->func = current->ret_stack[index].func;
  85. trace->calltime = current->ret_stack[index].calltime;
  86. trace->overrun = atomic_read(&current->trace_overrun);
  87. trace->depth = index;
  88. barrier();
  89. current->curr_ret_stack--;
  90. }
  91. /*
  92. * Send the trace to the ring-buffer.
  93. * @return the original return address.
  94. */
  95. unsigned long ftrace_return_to_handler(void)
  96. {
  97. struct ftrace_graph_ret trace;
  98. unsigned long ret;
  99. ftrace_pop_return_trace(&trace, &ret);
  100. trace.rettime = trace_clock_local();
  101. ftrace_graph_return(&trace);
  102. if (unlikely(!ret)) {
  103. ftrace_graph_stop();
  104. WARN_ON(1);
  105. /* Might as well panic. What else to do? */
  106. ret = (unsigned long)panic;
  107. }
  108. return ret;
  109. }
  110. static int graph_trace_init(struct trace_array *tr)
  111. {
  112. int ret = register_ftrace_graph(&trace_graph_return,
  113. &trace_graph_entry);
  114. if (ret)
  115. return ret;
  116. tracing_start_cmdline_record();
  117. return 0;
  118. }
  119. static void graph_trace_reset(struct trace_array *tr)
  120. {
  121. tracing_stop_cmdline_record();
  122. unregister_ftrace_graph();
  123. }
  124. static inline int log10_cpu(int nb)
  125. {
  126. if (nb / 100)
  127. return 3;
  128. if (nb / 10)
  129. return 2;
  130. return 1;
  131. }
  132. static enum print_line_t
  133. print_graph_cpu(struct trace_seq *s, int cpu)
  134. {
  135. int i;
  136. int ret;
  137. int log10_this = log10_cpu(cpu);
  138. int log10_all = log10_cpu(cpumask_weight(cpu_online_mask));
  139. /*
  140. * Start with a space character - to make it stand out
  141. * to the right a bit when trace output is pasted into
  142. * email:
  143. */
  144. ret = trace_seq_printf(s, " ");
  145. /*
  146. * Tricky - we space the CPU field according to the max
  147. * number of online CPUs. On a 2-cpu system it would take
  148. * a maximum of 1 digit - on a 128 cpu system it would
  149. * take up to 3 digits:
  150. */
  151. for (i = 0; i < log10_all - log10_this; i++) {
  152. ret = trace_seq_printf(s, " ");
  153. if (!ret)
  154. return TRACE_TYPE_PARTIAL_LINE;
  155. }
  156. ret = trace_seq_printf(s, "%d) ", cpu);
  157. if (!ret)
  158. return TRACE_TYPE_PARTIAL_LINE;
  159. return TRACE_TYPE_HANDLED;
  160. }
  161. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  162. static enum print_line_t
  163. print_graph_proc(struct trace_seq *s, pid_t pid)
  164. {
  165. char comm[TASK_COMM_LEN];
  166. /* sign + log10(MAX_INT) + '\0' */
  167. char pid_str[11];
  168. int spaces = 0;
  169. int ret;
  170. int len;
  171. int i;
  172. trace_find_cmdline(pid, comm);
  173. comm[7] = '\0';
  174. sprintf(pid_str, "%d", pid);
  175. /* 1 stands for the "-" character */
  176. len = strlen(comm) + strlen(pid_str) + 1;
  177. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  178. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  179. /* First spaces to align center */
  180. for (i = 0; i < spaces / 2; i++) {
  181. ret = trace_seq_printf(s, " ");
  182. if (!ret)
  183. return TRACE_TYPE_PARTIAL_LINE;
  184. }
  185. ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
  186. if (!ret)
  187. return TRACE_TYPE_PARTIAL_LINE;
  188. /* Last spaces to align center */
  189. for (i = 0; i < spaces - (spaces / 2); i++) {
  190. ret = trace_seq_printf(s, " ");
  191. if (!ret)
  192. return TRACE_TYPE_PARTIAL_LINE;
  193. }
  194. return TRACE_TYPE_HANDLED;
  195. }
  196. /* If the pid changed since the last trace, output this event */
  197. static enum print_line_t
  198. verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
  199. {
  200. pid_t prev_pid;
  201. pid_t *last_pid;
  202. int ret;
  203. if (!data)
  204. return TRACE_TYPE_HANDLED;
  205. last_pid = &(per_cpu_ptr(data, cpu)->last_pid);
  206. if (*last_pid == pid)
  207. return TRACE_TYPE_HANDLED;
  208. prev_pid = *last_pid;
  209. *last_pid = pid;
  210. if (prev_pid == -1)
  211. return TRACE_TYPE_HANDLED;
  212. /*
  213. * Context-switch trace line:
  214. ------------------------------------------
  215. | 1) migration/0--1 => sshd-1755
  216. ------------------------------------------
  217. */
  218. ret = trace_seq_printf(s,
  219. " ------------------------------------------\n");
  220. if (!ret)
  221. return TRACE_TYPE_PARTIAL_LINE;
  222. ret = print_graph_cpu(s, cpu);
  223. if (ret == TRACE_TYPE_PARTIAL_LINE)
  224. return TRACE_TYPE_PARTIAL_LINE;
  225. ret = print_graph_proc(s, prev_pid);
  226. if (ret == TRACE_TYPE_PARTIAL_LINE)
  227. return TRACE_TYPE_PARTIAL_LINE;
  228. ret = trace_seq_printf(s, " => ");
  229. if (!ret)
  230. return TRACE_TYPE_PARTIAL_LINE;
  231. ret = print_graph_proc(s, pid);
  232. if (ret == TRACE_TYPE_PARTIAL_LINE)
  233. return TRACE_TYPE_PARTIAL_LINE;
  234. ret = trace_seq_printf(s,
  235. "\n ------------------------------------------\n\n");
  236. if (!ret)
  237. return TRACE_TYPE_PARTIAL_LINE;
  238. return TRACE_TYPE_HANDLED;
  239. }
  240. static struct ftrace_graph_ret_entry *
  241. get_return_for_leaf(struct trace_iterator *iter,
  242. struct ftrace_graph_ent_entry *curr)
  243. {
  244. struct ring_buffer_iter *ring_iter;
  245. struct ring_buffer_event *event;
  246. struct ftrace_graph_ret_entry *next;
  247. ring_iter = iter->buffer_iter[iter->cpu];
  248. /* First peek to compare current entry and the next one */
  249. if (ring_iter)
  250. event = ring_buffer_iter_peek(ring_iter, NULL);
  251. else {
  252. /* We need to consume the current entry to see the next one */
  253. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  254. event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
  255. NULL);
  256. }
  257. if (!event)
  258. return NULL;
  259. next = ring_buffer_event_data(event);
  260. if (next->ent.type != TRACE_GRAPH_RET)
  261. return NULL;
  262. if (curr->ent.pid != next->ent.pid ||
  263. curr->graph_ent.func != next->ret.func)
  264. return NULL;
  265. /* this is a leaf, now advance the iterator */
  266. if (ring_iter)
  267. ring_buffer_read(ring_iter, NULL);
  268. return next;
  269. }
  270. /* Signal a overhead of time execution to the output */
  271. static int
  272. print_graph_overhead(unsigned long long duration, struct trace_seq *s)
  273. {
  274. /* If duration disappear, we don't need anything */
  275. if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
  276. return 1;
  277. /* Non nested entry or return */
  278. if (duration == -1)
  279. return trace_seq_printf(s, " ");
  280. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
  281. /* Duration exceeded 100 msecs */
  282. if (duration > 100000ULL)
  283. return trace_seq_printf(s, "! ");
  284. /* Duration exceeded 10 msecs */
  285. if (duration > 10000ULL)
  286. return trace_seq_printf(s, "+ ");
  287. }
  288. return trace_seq_printf(s, " ");
  289. }
  290. static int print_graph_abs_time(u64 t, struct trace_seq *s)
  291. {
  292. unsigned long usecs_rem;
  293. usecs_rem = do_div(t, NSEC_PER_SEC);
  294. usecs_rem /= 1000;
  295. return trace_seq_printf(s, "%5lu.%06lu | ",
  296. (unsigned long)t, usecs_rem);
  297. }
  298. static enum print_line_t
  299. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  300. enum trace_type type, int cpu, pid_t pid)
  301. {
  302. int ret;
  303. struct trace_seq *s = &iter->seq;
  304. if (addr < (unsigned long)__irqentry_text_start ||
  305. addr >= (unsigned long)__irqentry_text_end)
  306. return TRACE_TYPE_UNHANDLED;
  307. /* Absolute time */
  308. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  309. ret = print_graph_abs_time(iter->ts, s);
  310. if (!ret)
  311. return TRACE_TYPE_PARTIAL_LINE;
  312. }
  313. /* Cpu */
  314. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  315. ret = print_graph_cpu(s, cpu);
  316. if (ret == TRACE_TYPE_PARTIAL_LINE)
  317. return TRACE_TYPE_PARTIAL_LINE;
  318. }
  319. /* Proc */
  320. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  321. ret = print_graph_proc(s, pid);
  322. if (ret == TRACE_TYPE_PARTIAL_LINE)
  323. return TRACE_TYPE_PARTIAL_LINE;
  324. ret = trace_seq_printf(s, " | ");
  325. if (!ret)
  326. return TRACE_TYPE_PARTIAL_LINE;
  327. }
  328. /* No overhead */
  329. ret = print_graph_overhead(-1, s);
  330. if (!ret)
  331. return TRACE_TYPE_PARTIAL_LINE;
  332. if (type == TRACE_GRAPH_ENT)
  333. ret = trace_seq_printf(s, "==========>");
  334. else
  335. ret = trace_seq_printf(s, "<==========");
  336. if (!ret)
  337. return TRACE_TYPE_PARTIAL_LINE;
  338. /* Don't close the duration column if haven't one */
  339. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  340. trace_seq_printf(s, " |");
  341. ret = trace_seq_printf(s, "\n");
  342. if (!ret)
  343. return TRACE_TYPE_PARTIAL_LINE;
  344. return TRACE_TYPE_HANDLED;
  345. }
  346. static enum print_line_t
  347. print_graph_duration(unsigned long long duration, struct trace_seq *s)
  348. {
  349. unsigned long nsecs_rem = do_div(duration, 1000);
  350. /* log10(ULONG_MAX) + '\0' */
  351. char msecs_str[21];
  352. char nsecs_str[5];
  353. int ret, len;
  354. int i;
  355. sprintf(msecs_str, "%lu", (unsigned long) duration);
  356. /* Print msecs */
  357. ret = trace_seq_printf(s, "%s", msecs_str);
  358. if (!ret)
  359. return TRACE_TYPE_PARTIAL_LINE;
  360. len = strlen(msecs_str);
  361. /* Print nsecs (we don't want to exceed 7 numbers) */
  362. if (len < 7) {
  363. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  364. ret = trace_seq_printf(s, ".%s", nsecs_str);
  365. if (!ret)
  366. return TRACE_TYPE_PARTIAL_LINE;
  367. len += strlen(nsecs_str);
  368. }
  369. ret = trace_seq_printf(s, " us ");
  370. if (!ret)
  371. return TRACE_TYPE_PARTIAL_LINE;
  372. /* Print remaining spaces to fit the row's width */
  373. for (i = len; i < 7; i++) {
  374. ret = trace_seq_printf(s, " ");
  375. if (!ret)
  376. return TRACE_TYPE_PARTIAL_LINE;
  377. }
  378. ret = trace_seq_printf(s, "| ");
  379. if (!ret)
  380. return TRACE_TYPE_PARTIAL_LINE;
  381. return TRACE_TYPE_HANDLED;
  382. }
  383. /* Case of a leaf function on its call entry */
  384. static enum print_line_t
  385. print_graph_entry_leaf(struct trace_iterator *iter,
  386. struct ftrace_graph_ent_entry *entry,
  387. struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
  388. {
  389. struct fgraph_data *data = iter->private;
  390. struct ftrace_graph_ret *graph_ret;
  391. struct ftrace_graph_ent *call;
  392. unsigned long long duration;
  393. int ret;
  394. int i;
  395. graph_ret = &ret_entry->ret;
  396. call = &entry->graph_ent;
  397. duration = graph_ret->rettime - graph_ret->calltime;
  398. if (data) {
  399. int cpu = iter->cpu;
  400. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  401. /*
  402. * Comments display at + 1 to depth. Since
  403. * this is a leaf function, keep the comments
  404. * equal to this depth.
  405. */
  406. *depth = call->depth - 1;
  407. }
  408. /* Overhead */
  409. ret = print_graph_overhead(duration, s);
  410. if (!ret)
  411. return TRACE_TYPE_PARTIAL_LINE;
  412. /* Duration */
  413. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  414. ret = print_graph_duration(duration, s);
  415. if (ret == TRACE_TYPE_PARTIAL_LINE)
  416. return TRACE_TYPE_PARTIAL_LINE;
  417. }
  418. /* Function */
  419. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  420. ret = trace_seq_printf(s, " ");
  421. if (!ret)
  422. return TRACE_TYPE_PARTIAL_LINE;
  423. }
  424. ret = seq_print_ip_sym(s, call->func, 0);
  425. if (!ret)
  426. return TRACE_TYPE_PARTIAL_LINE;
  427. ret = trace_seq_printf(s, "();\n");
  428. if (!ret)
  429. return TRACE_TYPE_PARTIAL_LINE;
  430. return TRACE_TYPE_HANDLED;
  431. }
  432. static enum print_line_t
  433. print_graph_entry_nested(struct trace_iterator *iter,
  434. struct ftrace_graph_ent_entry *entry,
  435. struct trace_seq *s, int cpu)
  436. {
  437. struct ftrace_graph_ent *call = &entry->graph_ent;
  438. struct fgraph_data *data = iter->private;
  439. int ret;
  440. int i;
  441. if (data) {
  442. int cpu = iter->cpu;
  443. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  444. *depth = call->depth;
  445. }
  446. /* No overhead */
  447. ret = print_graph_overhead(-1, s);
  448. if (!ret)
  449. return TRACE_TYPE_PARTIAL_LINE;
  450. /* No time */
  451. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  452. ret = trace_seq_printf(s, " | ");
  453. if (!ret)
  454. return TRACE_TYPE_PARTIAL_LINE;
  455. }
  456. /* Function */
  457. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  458. ret = trace_seq_printf(s, " ");
  459. if (!ret)
  460. return TRACE_TYPE_PARTIAL_LINE;
  461. }
  462. ret = seq_print_ip_sym(s, call->func, 0);
  463. if (!ret)
  464. return TRACE_TYPE_PARTIAL_LINE;
  465. ret = trace_seq_printf(s, "() {\n");
  466. if (!ret)
  467. return TRACE_TYPE_PARTIAL_LINE;
  468. /*
  469. * we already consumed the current entry to check the next one
  470. * and see if this is a leaf.
  471. */
  472. return TRACE_TYPE_NO_CONSUME;
  473. }
  474. static enum print_line_t
  475. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  476. int type, unsigned long addr)
  477. {
  478. struct fgraph_data *data = iter->private;
  479. struct trace_entry *ent = iter->ent;
  480. int cpu = iter->cpu;
  481. int ret;
  482. /* Pid */
  483. if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
  484. return TRACE_TYPE_PARTIAL_LINE;
  485. if (type) {
  486. /* Interrupt */
  487. ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
  488. if (ret == TRACE_TYPE_PARTIAL_LINE)
  489. return TRACE_TYPE_PARTIAL_LINE;
  490. }
  491. /* Absolute time */
  492. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  493. ret = print_graph_abs_time(iter->ts, s);
  494. if (!ret)
  495. return TRACE_TYPE_PARTIAL_LINE;
  496. }
  497. /* Cpu */
  498. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  499. ret = print_graph_cpu(s, cpu);
  500. if (ret == TRACE_TYPE_PARTIAL_LINE)
  501. return TRACE_TYPE_PARTIAL_LINE;
  502. }
  503. /* Proc */
  504. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  505. ret = print_graph_proc(s, ent->pid);
  506. if (ret == TRACE_TYPE_PARTIAL_LINE)
  507. return TRACE_TYPE_PARTIAL_LINE;
  508. ret = trace_seq_printf(s, " | ");
  509. if (!ret)
  510. return TRACE_TYPE_PARTIAL_LINE;
  511. }
  512. return 0;
  513. }
  514. static enum print_line_t
  515. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  516. struct trace_iterator *iter)
  517. {
  518. int cpu = iter->cpu;
  519. struct ftrace_graph_ent *call = &field->graph_ent;
  520. struct ftrace_graph_ret_entry *leaf_ret;
  521. if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
  522. return TRACE_TYPE_PARTIAL_LINE;
  523. leaf_ret = get_return_for_leaf(iter, field);
  524. if (leaf_ret)
  525. return print_graph_entry_leaf(iter, field, leaf_ret, s);
  526. else
  527. return print_graph_entry_nested(iter, field, s, cpu);
  528. }
  529. static enum print_line_t
  530. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  531. struct trace_entry *ent, struct trace_iterator *iter)
  532. {
  533. unsigned long long duration = trace->rettime - trace->calltime;
  534. struct fgraph_data *data = iter->private;
  535. pid_t pid = ent->pid;
  536. int cpu = iter->cpu;
  537. int ret;
  538. int i;
  539. if (data) {
  540. int cpu = iter->cpu;
  541. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  542. /*
  543. * Comments display at + 1 to depth. This is the
  544. * return from a function, we now want the comments
  545. * to display at the same level of the bracket.
  546. */
  547. *depth = trace->depth - 1;
  548. }
  549. if (print_graph_prologue(iter, s, 0, 0))
  550. return TRACE_TYPE_PARTIAL_LINE;
  551. /* Overhead */
  552. ret = print_graph_overhead(duration, s);
  553. if (!ret)
  554. return TRACE_TYPE_PARTIAL_LINE;
  555. /* Duration */
  556. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  557. ret = print_graph_duration(duration, s);
  558. if (ret == TRACE_TYPE_PARTIAL_LINE)
  559. return TRACE_TYPE_PARTIAL_LINE;
  560. }
  561. /* Closing brace */
  562. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
  563. ret = trace_seq_printf(s, " ");
  564. if (!ret)
  565. return TRACE_TYPE_PARTIAL_LINE;
  566. }
  567. ret = trace_seq_printf(s, "}\n");
  568. if (!ret)
  569. return TRACE_TYPE_PARTIAL_LINE;
  570. /* Overrun */
  571. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
  572. ret = trace_seq_printf(s, " (Overruns: %lu)\n",
  573. trace->overrun);
  574. if (!ret)
  575. return TRACE_TYPE_PARTIAL_LINE;
  576. }
  577. ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
  578. if (ret == TRACE_TYPE_PARTIAL_LINE)
  579. return TRACE_TYPE_PARTIAL_LINE;
  580. return TRACE_TYPE_HANDLED;
  581. }
  582. static enum print_line_t
  583. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  584. struct trace_iterator *iter)
  585. {
  586. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  587. struct fgraph_data *data = iter->private;
  588. struct trace_event *event;
  589. int depth = 0;
  590. int ret;
  591. int i;
  592. if (data)
  593. depth = per_cpu_ptr(data, iter->cpu)->depth;
  594. if (print_graph_prologue(iter, s, 0, 0))
  595. return TRACE_TYPE_PARTIAL_LINE;
  596. /* No overhead */
  597. ret = print_graph_overhead(-1, s);
  598. if (!ret)
  599. return TRACE_TYPE_PARTIAL_LINE;
  600. /* No time */
  601. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  602. ret = trace_seq_printf(s, " | ");
  603. if (!ret)
  604. return TRACE_TYPE_PARTIAL_LINE;
  605. }
  606. /* Indentation */
  607. if (depth > 0)
  608. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
  609. ret = trace_seq_printf(s, " ");
  610. if (!ret)
  611. return TRACE_TYPE_PARTIAL_LINE;
  612. }
  613. /* The comment */
  614. ret = trace_seq_printf(s, "/* ");
  615. if (!ret)
  616. return TRACE_TYPE_PARTIAL_LINE;
  617. switch (iter->ent->type) {
  618. case TRACE_BPRINT:
  619. ret = trace_print_bprintk_msg_only(iter);
  620. if (ret != TRACE_TYPE_HANDLED)
  621. return ret;
  622. break;
  623. case TRACE_PRINT:
  624. ret = trace_print_printk_msg_only(iter);
  625. if (ret != TRACE_TYPE_HANDLED)
  626. return ret;
  627. break;
  628. default:
  629. event = ftrace_find_event(ent->type);
  630. if (!event)
  631. return TRACE_TYPE_UNHANDLED;
  632. ret = event->trace(iter, sym_flags);
  633. if (ret != TRACE_TYPE_HANDLED)
  634. return ret;
  635. }
  636. /* Strip ending newline */
  637. if (s->buffer[s->len - 1] == '\n') {
  638. s->buffer[s->len - 1] = '\0';
  639. s->len--;
  640. }
  641. ret = trace_seq_printf(s, " */\n");
  642. if (!ret)
  643. return TRACE_TYPE_PARTIAL_LINE;
  644. return TRACE_TYPE_HANDLED;
  645. }
  646. enum print_line_t
  647. print_graph_function(struct trace_iterator *iter)
  648. {
  649. struct trace_entry *entry = iter->ent;
  650. struct trace_seq *s = &iter->seq;
  651. switch (entry->type) {
  652. case TRACE_GRAPH_ENT: {
  653. struct ftrace_graph_ent_entry *field;
  654. trace_assign_type(field, entry);
  655. return print_graph_entry(field, s, iter);
  656. }
  657. case TRACE_GRAPH_RET: {
  658. struct ftrace_graph_ret_entry *field;
  659. trace_assign_type(field, entry);
  660. return print_graph_return(&field->ret, s, entry, iter);
  661. }
  662. default:
  663. return print_graph_comment(s, entry, iter);
  664. }
  665. return TRACE_TYPE_HANDLED;
  666. }
  667. static void print_graph_headers(struct seq_file *s)
  668. {
  669. /* 1st line */
  670. seq_printf(s, "# ");
  671. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  672. seq_printf(s, " TIME ");
  673. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  674. seq_printf(s, "CPU");
  675. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  676. seq_printf(s, " TASK/PID ");
  677. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  678. seq_printf(s, " DURATION ");
  679. seq_printf(s, " FUNCTION CALLS\n");
  680. /* 2nd line */
  681. seq_printf(s, "# ");
  682. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  683. seq_printf(s, " | ");
  684. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  685. seq_printf(s, "| ");
  686. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  687. seq_printf(s, " | | ");
  688. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  689. seq_printf(s, " | | ");
  690. seq_printf(s, " | | | |\n");
  691. }
  692. static void graph_trace_open(struct trace_iterator *iter)
  693. {
  694. /* pid and depth on the last trace processed */
  695. struct fgraph_data *data = alloc_percpu(struct fgraph_data);
  696. int cpu;
  697. if (!data)
  698. pr_warning("function graph tracer: not enough memory\n");
  699. else
  700. for_each_possible_cpu(cpu) {
  701. pid_t *pid = &(per_cpu_ptr(data, cpu)->last_pid);
  702. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  703. *pid = -1;
  704. *depth = 0;
  705. }
  706. iter->private = data;
  707. }
  708. static void graph_trace_close(struct trace_iterator *iter)
  709. {
  710. free_percpu(iter->private);
  711. }
  712. static struct tracer graph_trace __read_mostly = {
  713. .name = "function_graph",
  714. .open = graph_trace_open,
  715. .close = graph_trace_close,
  716. .wait_pipe = poll_wait_pipe,
  717. .init = graph_trace_init,
  718. .reset = graph_trace_reset,
  719. .print_line = print_graph_function,
  720. .print_header = print_graph_headers,
  721. .flags = &tracer_flags,
  722. #ifdef CONFIG_FTRACE_SELFTEST
  723. .selftest = trace_selftest_startup_function_graph,
  724. #endif
  725. };
  726. static __init int init_graph_trace(void)
  727. {
  728. return register_tracer(&graph_trace);
  729. }
  730. device_initcall(init_graph_trace);