trace_functions_graph.c 21 KB

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