trace_functions_graph.c 21 KB

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