trace_functions_graph.c 20 KB

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