trace_functions_graph.c 21 KB

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