trace_functions_graph.c 18 KB

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