trace_functions_graph.c 22 KB

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