trace_functions_graph.c 24 KB

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