trace_functions_graph.c 19 KB

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