trace_functions_graph.c 21 KB

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