trace_functions_graph.c 18 KB

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