trace_functions_graph.c 18 KB

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