trace_functions_graph.c 21 KB

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