trace_functions_graph.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 ring_buffer *buffer = tr->buffer;
  152. struct ftrace_graph_ent_entry *entry;
  153. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  154. return 0;
  155. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
  156. sizeof(*entry), flags, pc);
  157. if (!event)
  158. return 0;
  159. entry = ring_buffer_event_data(event);
  160. entry->graph_ent = *trace;
  161. if (!filter_current_check_discard(buffer, call, entry, event))
  162. ring_buffer_unlock_commit(buffer, event);
  163. return 1;
  164. }
  165. int trace_graph_entry(struct ftrace_graph_ent *trace)
  166. {
  167. struct trace_array *tr = graph_array;
  168. struct trace_array_cpu *data;
  169. unsigned long flags;
  170. long disabled;
  171. int ret;
  172. int cpu;
  173. int pc;
  174. if (unlikely(!tr))
  175. return 0;
  176. if (!ftrace_trace_task(current))
  177. return 0;
  178. if (!ftrace_graph_addr(trace->func))
  179. return 0;
  180. local_irq_save(flags);
  181. cpu = raw_smp_processor_id();
  182. data = tr->data[cpu];
  183. disabled = atomic_inc_return(&data->disabled);
  184. if (likely(disabled == 1)) {
  185. pc = preempt_count();
  186. ret = __trace_graph_entry(tr, trace, flags, pc);
  187. } else {
  188. ret = 0;
  189. }
  190. /* Only do the atomic if it is not already set */
  191. if (!test_tsk_trace_graph(current))
  192. set_tsk_trace_graph(current);
  193. atomic_dec(&data->disabled);
  194. local_irq_restore(flags);
  195. return ret;
  196. }
  197. static void __trace_graph_return(struct trace_array *tr,
  198. struct ftrace_graph_ret *trace,
  199. unsigned long flags,
  200. int pc)
  201. {
  202. struct ftrace_event_call *call = &event_funcgraph_exit;
  203. struct ring_buffer_event *event;
  204. struct ring_buffer *buffer = tr->buffer;
  205. struct ftrace_graph_ret_entry *entry;
  206. if (unlikely(local_read(&__get_cpu_var(ftrace_cpu_disabled))))
  207. return;
  208. event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
  209. sizeof(*entry), flags, pc);
  210. if (!event)
  211. return;
  212. entry = ring_buffer_event_data(event);
  213. entry->ret = *trace;
  214. if (!filter_current_check_discard(buffer, call, entry, event))
  215. ring_buffer_unlock_commit(buffer, event);
  216. }
  217. void trace_graph_return(struct ftrace_graph_ret *trace)
  218. {
  219. struct trace_array *tr = graph_array;
  220. struct trace_array_cpu *data;
  221. unsigned long flags;
  222. long disabled;
  223. int cpu;
  224. int pc;
  225. local_irq_save(flags);
  226. cpu = raw_smp_processor_id();
  227. data = tr->data[cpu];
  228. disabled = atomic_inc_return(&data->disabled);
  229. if (likely(disabled == 1)) {
  230. pc = preempt_count();
  231. __trace_graph_return(tr, trace, flags, pc);
  232. }
  233. if (!trace->depth)
  234. clear_tsk_trace_graph(current);
  235. atomic_dec(&data->disabled);
  236. local_irq_restore(flags);
  237. }
  238. static int graph_trace_init(struct trace_array *tr)
  239. {
  240. int ret;
  241. graph_array = tr;
  242. ret = register_ftrace_graph(&trace_graph_return,
  243. &trace_graph_entry);
  244. if (ret)
  245. return ret;
  246. tracing_start_cmdline_record();
  247. return 0;
  248. }
  249. void set_graph_array(struct trace_array *tr)
  250. {
  251. graph_array = tr;
  252. }
  253. static void graph_trace_reset(struct trace_array *tr)
  254. {
  255. tracing_stop_cmdline_record();
  256. unregister_ftrace_graph();
  257. }
  258. static int max_bytes_for_cpu;
  259. static enum print_line_t
  260. print_graph_cpu(struct trace_seq *s, int cpu)
  261. {
  262. int ret;
  263. /*
  264. * Start with a space character - to make it stand out
  265. * to the right a bit when trace output is pasted into
  266. * email:
  267. */
  268. ret = trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
  269. if (!ret)
  270. return TRACE_TYPE_PARTIAL_LINE;
  271. return TRACE_TYPE_HANDLED;
  272. }
  273. #define TRACE_GRAPH_PROCINFO_LENGTH 14
  274. static enum print_line_t
  275. print_graph_proc(struct trace_seq *s, pid_t pid)
  276. {
  277. char comm[TASK_COMM_LEN];
  278. /* sign + log10(MAX_INT) + '\0' */
  279. char pid_str[11];
  280. int spaces = 0;
  281. int ret;
  282. int len;
  283. int i;
  284. trace_find_cmdline(pid, comm);
  285. comm[7] = '\0';
  286. sprintf(pid_str, "%d", pid);
  287. /* 1 stands for the "-" character */
  288. len = strlen(comm) + strlen(pid_str) + 1;
  289. if (len < TRACE_GRAPH_PROCINFO_LENGTH)
  290. spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
  291. /* First spaces to align center */
  292. for (i = 0; i < spaces / 2; i++) {
  293. ret = trace_seq_printf(s, " ");
  294. if (!ret)
  295. return TRACE_TYPE_PARTIAL_LINE;
  296. }
  297. ret = trace_seq_printf(s, "%s-%s", comm, pid_str);
  298. if (!ret)
  299. return TRACE_TYPE_PARTIAL_LINE;
  300. /* Last spaces to align center */
  301. for (i = 0; i < spaces - (spaces / 2); i++) {
  302. ret = trace_seq_printf(s, " ");
  303. if (!ret)
  304. return TRACE_TYPE_PARTIAL_LINE;
  305. }
  306. return TRACE_TYPE_HANDLED;
  307. }
  308. static enum print_line_t
  309. print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  310. {
  311. int hardirq, softirq;
  312. int ret;
  313. hardirq = entry->flags & TRACE_FLAG_HARDIRQ;
  314. softirq = entry->flags & TRACE_FLAG_SOFTIRQ;
  315. if (!trace_seq_printf(s, " %c%c%c",
  316. (entry->flags & TRACE_FLAG_IRQS_OFF) ? 'd' :
  317. (entry->flags & TRACE_FLAG_IRQS_NOSUPPORT) ?
  318. 'X' : '.',
  319. (entry->flags & TRACE_FLAG_NEED_RESCHED) ?
  320. 'N' : '.',
  321. (hardirq && softirq) ? 'H' :
  322. hardirq ? 'h' : softirq ? 's' : '.'))
  323. return 0;
  324. if (entry->lock_depth < 0)
  325. ret = trace_seq_putc(s, '.');
  326. else
  327. ret = trace_seq_printf(s, "%d", entry->lock_depth);
  328. if (!ret)
  329. return 0;
  330. if (entry->preempt_count)
  331. return trace_seq_printf(s, "%x", entry->preempt_count);
  332. return trace_seq_puts(s, ".");
  333. }
  334. /* If the pid changed since the last trace, output this event */
  335. static enum print_line_t
  336. verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
  337. {
  338. pid_t prev_pid;
  339. pid_t *last_pid;
  340. int ret;
  341. if (!data)
  342. return TRACE_TYPE_HANDLED;
  343. last_pid = &(per_cpu_ptr(data, cpu)->last_pid);
  344. if (*last_pid == pid)
  345. return TRACE_TYPE_HANDLED;
  346. prev_pid = *last_pid;
  347. *last_pid = pid;
  348. if (prev_pid == -1)
  349. return TRACE_TYPE_HANDLED;
  350. /*
  351. * Context-switch trace line:
  352. ------------------------------------------
  353. | 1) migration/0--1 => sshd-1755
  354. ------------------------------------------
  355. */
  356. ret = trace_seq_printf(s,
  357. " ------------------------------------------\n");
  358. if (!ret)
  359. return TRACE_TYPE_PARTIAL_LINE;
  360. ret = print_graph_cpu(s, cpu);
  361. if (ret == TRACE_TYPE_PARTIAL_LINE)
  362. return TRACE_TYPE_PARTIAL_LINE;
  363. ret = print_graph_proc(s, prev_pid);
  364. if (ret == TRACE_TYPE_PARTIAL_LINE)
  365. return TRACE_TYPE_PARTIAL_LINE;
  366. ret = trace_seq_printf(s, " => ");
  367. if (!ret)
  368. return TRACE_TYPE_PARTIAL_LINE;
  369. ret = print_graph_proc(s, pid);
  370. if (ret == TRACE_TYPE_PARTIAL_LINE)
  371. return TRACE_TYPE_PARTIAL_LINE;
  372. ret = trace_seq_printf(s,
  373. "\n ------------------------------------------\n\n");
  374. if (!ret)
  375. return TRACE_TYPE_PARTIAL_LINE;
  376. return TRACE_TYPE_HANDLED;
  377. }
  378. static struct ftrace_graph_ret_entry *
  379. get_return_for_leaf(struct trace_iterator *iter,
  380. struct ftrace_graph_ent_entry *curr)
  381. {
  382. struct ring_buffer_iter *ring_iter;
  383. struct ring_buffer_event *event;
  384. struct ftrace_graph_ret_entry *next;
  385. ring_iter = iter->buffer_iter[iter->cpu];
  386. /* First peek to compare current entry and the next one */
  387. if (ring_iter)
  388. event = ring_buffer_iter_peek(ring_iter, NULL);
  389. else {
  390. /* We need to consume the current entry to see the next one */
  391. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  392. event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
  393. NULL);
  394. }
  395. if (!event)
  396. return NULL;
  397. next = ring_buffer_event_data(event);
  398. if (next->ent.type != TRACE_GRAPH_RET)
  399. return NULL;
  400. if (curr->ent.pid != next->ent.pid ||
  401. curr->graph_ent.func != next->ret.func)
  402. return NULL;
  403. /* this is a leaf, now advance the iterator */
  404. if (ring_iter)
  405. ring_buffer_read(ring_iter, NULL);
  406. return next;
  407. }
  408. /* Signal a overhead of time execution to the output */
  409. static int
  410. print_graph_overhead(unsigned long long duration, struct trace_seq *s)
  411. {
  412. /* If duration disappear, we don't need anything */
  413. if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
  414. return 1;
  415. /* Non nested entry or return */
  416. if (duration == -1)
  417. return trace_seq_printf(s, " ");
  418. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
  419. /* Duration exceeded 100 msecs */
  420. if (duration > 100000ULL)
  421. return trace_seq_printf(s, "! ");
  422. /* Duration exceeded 10 msecs */
  423. if (duration > 10000ULL)
  424. return trace_seq_printf(s, "+ ");
  425. }
  426. return trace_seq_printf(s, " ");
  427. }
  428. static int print_graph_abs_time(u64 t, struct trace_seq *s)
  429. {
  430. unsigned long usecs_rem;
  431. usecs_rem = do_div(t, NSEC_PER_SEC);
  432. usecs_rem /= 1000;
  433. return trace_seq_printf(s, "%5lu.%06lu | ",
  434. (unsigned long)t, usecs_rem);
  435. }
  436. static enum print_line_t
  437. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  438. enum trace_type type, int cpu, pid_t pid)
  439. {
  440. int ret;
  441. struct trace_seq *s = &iter->seq;
  442. if (addr < (unsigned long)__irqentry_text_start ||
  443. addr >= (unsigned long)__irqentry_text_end)
  444. return TRACE_TYPE_UNHANDLED;
  445. /* Absolute time */
  446. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  447. ret = print_graph_abs_time(iter->ts, s);
  448. if (!ret)
  449. return TRACE_TYPE_PARTIAL_LINE;
  450. }
  451. /* Cpu */
  452. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  453. ret = print_graph_cpu(s, cpu);
  454. if (ret == TRACE_TYPE_PARTIAL_LINE)
  455. return TRACE_TYPE_PARTIAL_LINE;
  456. }
  457. /* Proc */
  458. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  459. ret = print_graph_proc(s, pid);
  460. if (ret == TRACE_TYPE_PARTIAL_LINE)
  461. return TRACE_TYPE_PARTIAL_LINE;
  462. ret = trace_seq_printf(s, " | ");
  463. if (!ret)
  464. return TRACE_TYPE_PARTIAL_LINE;
  465. }
  466. /* No overhead */
  467. ret = print_graph_overhead(-1, s);
  468. if (!ret)
  469. return TRACE_TYPE_PARTIAL_LINE;
  470. if (type == TRACE_GRAPH_ENT)
  471. ret = trace_seq_printf(s, "==========>");
  472. else
  473. ret = trace_seq_printf(s, "<==========");
  474. if (!ret)
  475. return TRACE_TYPE_PARTIAL_LINE;
  476. /* Don't close the duration column if haven't one */
  477. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  478. trace_seq_printf(s, " |");
  479. ret = trace_seq_printf(s, "\n");
  480. if (!ret)
  481. return TRACE_TYPE_PARTIAL_LINE;
  482. return TRACE_TYPE_HANDLED;
  483. }
  484. enum print_line_t
  485. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
  486. {
  487. unsigned long nsecs_rem = do_div(duration, 1000);
  488. /* log10(ULONG_MAX) + '\0' */
  489. char msecs_str[21];
  490. char nsecs_str[5];
  491. int ret, len;
  492. int i;
  493. sprintf(msecs_str, "%lu", (unsigned long) duration);
  494. /* Print msecs */
  495. ret = trace_seq_printf(s, "%s", msecs_str);
  496. if (!ret)
  497. return TRACE_TYPE_PARTIAL_LINE;
  498. len = strlen(msecs_str);
  499. /* Print nsecs (we don't want to exceed 7 numbers) */
  500. if (len < 7) {
  501. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  502. ret = trace_seq_printf(s, ".%s", nsecs_str);
  503. if (!ret)
  504. return TRACE_TYPE_PARTIAL_LINE;
  505. len += strlen(nsecs_str);
  506. }
  507. ret = trace_seq_printf(s, " us ");
  508. if (!ret)
  509. return TRACE_TYPE_PARTIAL_LINE;
  510. /* Print remaining spaces to fit the row's width */
  511. for (i = len; i < 7; i++) {
  512. ret = trace_seq_printf(s, " ");
  513. if (!ret)
  514. return TRACE_TYPE_PARTIAL_LINE;
  515. }
  516. return TRACE_TYPE_HANDLED;
  517. }
  518. static enum print_line_t
  519. print_graph_duration(unsigned long long duration, struct trace_seq *s)
  520. {
  521. int ret;
  522. ret = trace_print_graph_duration(duration, s);
  523. if (ret != TRACE_TYPE_HANDLED)
  524. return ret;
  525. ret = trace_seq_printf(s, "| ");
  526. if (!ret)
  527. return TRACE_TYPE_PARTIAL_LINE;
  528. return TRACE_TYPE_HANDLED;
  529. }
  530. /* Case of a leaf function on its call entry */
  531. static enum print_line_t
  532. print_graph_entry_leaf(struct trace_iterator *iter,
  533. struct ftrace_graph_ent_entry *entry,
  534. struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
  535. {
  536. struct fgraph_data *data = iter->private;
  537. struct ftrace_graph_ret *graph_ret;
  538. struct ftrace_graph_ent *call;
  539. unsigned long long duration;
  540. int ret;
  541. int i;
  542. graph_ret = &ret_entry->ret;
  543. call = &entry->graph_ent;
  544. duration = graph_ret->rettime - graph_ret->calltime;
  545. if (data) {
  546. int cpu = iter->cpu;
  547. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  548. /*
  549. * Comments display at + 1 to depth. Since
  550. * this is a leaf function, keep the comments
  551. * equal to this depth.
  552. */
  553. *depth = call->depth - 1;
  554. }
  555. /* Overhead */
  556. ret = print_graph_overhead(duration, s);
  557. if (!ret)
  558. return TRACE_TYPE_PARTIAL_LINE;
  559. /* Duration */
  560. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  561. ret = print_graph_duration(duration, s);
  562. if (ret == TRACE_TYPE_PARTIAL_LINE)
  563. return TRACE_TYPE_PARTIAL_LINE;
  564. }
  565. /* Function */
  566. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  567. ret = trace_seq_printf(s, " ");
  568. if (!ret)
  569. return TRACE_TYPE_PARTIAL_LINE;
  570. }
  571. ret = trace_seq_printf(s, "%pf();\n", (void *)call->func);
  572. if (!ret)
  573. return TRACE_TYPE_PARTIAL_LINE;
  574. return TRACE_TYPE_HANDLED;
  575. }
  576. static enum print_line_t
  577. print_graph_entry_nested(struct trace_iterator *iter,
  578. struct ftrace_graph_ent_entry *entry,
  579. struct trace_seq *s, int cpu)
  580. {
  581. struct ftrace_graph_ent *call = &entry->graph_ent;
  582. struct fgraph_data *data = iter->private;
  583. int ret;
  584. int i;
  585. if (data) {
  586. int cpu = iter->cpu;
  587. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  588. *depth = call->depth;
  589. }
  590. /* No overhead */
  591. ret = print_graph_overhead(-1, s);
  592. if (!ret)
  593. return TRACE_TYPE_PARTIAL_LINE;
  594. /* No time */
  595. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  596. ret = trace_seq_printf(s, " | ");
  597. if (!ret)
  598. return TRACE_TYPE_PARTIAL_LINE;
  599. }
  600. /* Function */
  601. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  602. ret = trace_seq_printf(s, " ");
  603. if (!ret)
  604. return TRACE_TYPE_PARTIAL_LINE;
  605. }
  606. ret = trace_seq_printf(s, "%pf() {\n", (void *)call->func);
  607. if (!ret)
  608. return TRACE_TYPE_PARTIAL_LINE;
  609. /*
  610. * we already consumed the current entry to check the next one
  611. * and see if this is a leaf.
  612. */
  613. return TRACE_TYPE_NO_CONSUME;
  614. }
  615. static enum print_line_t
  616. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  617. int type, unsigned long addr)
  618. {
  619. struct fgraph_data *data = iter->private;
  620. struct trace_entry *ent = iter->ent;
  621. int cpu = iter->cpu;
  622. int ret;
  623. /* Pid */
  624. if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
  625. return TRACE_TYPE_PARTIAL_LINE;
  626. if (type) {
  627. /* Interrupt */
  628. ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
  629. if (ret == TRACE_TYPE_PARTIAL_LINE)
  630. return TRACE_TYPE_PARTIAL_LINE;
  631. }
  632. /* Absolute time */
  633. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  634. ret = print_graph_abs_time(iter->ts, s);
  635. if (!ret)
  636. return TRACE_TYPE_PARTIAL_LINE;
  637. }
  638. /* Cpu */
  639. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  640. ret = print_graph_cpu(s, cpu);
  641. if (ret == TRACE_TYPE_PARTIAL_LINE)
  642. return TRACE_TYPE_PARTIAL_LINE;
  643. }
  644. /* Proc */
  645. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  646. ret = print_graph_proc(s, ent->pid);
  647. if (ret == TRACE_TYPE_PARTIAL_LINE)
  648. return TRACE_TYPE_PARTIAL_LINE;
  649. ret = trace_seq_printf(s, " | ");
  650. if (!ret)
  651. return TRACE_TYPE_PARTIAL_LINE;
  652. }
  653. /* Latency format */
  654. if (trace_flags & TRACE_ITER_LATENCY_FMT) {
  655. ret = print_graph_lat_fmt(s, ent);
  656. if (ret == TRACE_TYPE_PARTIAL_LINE)
  657. return TRACE_TYPE_PARTIAL_LINE;
  658. }
  659. return 0;
  660. }
  661. static enum print_line_t
  662. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  663. struct trace_iterator *iter)
  664. {
  665. int cpu = iter->cpu;
  666. struct ftrace_graph_ent *call = &field->graph_ent;
  667. struct ftrace_graph_ret_entry *leaf_ret;
  668. if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
  669. return TRACE_TYPE_PARTIAL_LINE;
  670. leaf_ret = get_return_for_leaf(iter, field);
  671. if (leaf_ret)
  672. return print_graph_entry_leaf(iter, field, leaf_ret, s);
  673. else
  674. return print_graph_entry_nested(iter, field, s, cpu);
  675. }
  676. static enum print_line_t
  677. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  678. struct trace_entry *ent, struct trace_iterator *iter)
  679. {
  680. unsigned long long duration = trace->rettime - trace->calltime;
  681. struct fgraph_data *data = iter->private;
  682. pid_t pid = ent->pid;
  683. int cpu = iter->cpu;
  684. int ret;
  685. int i;
  686. if (data) {
  687. int cpu = iter->cpu;
  688. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  689. /*
  690. * Comments display at + 1 to depth. This is the
  691. * return from a function, we now want the comments
  692. * to display at the same level of the bracket.
  693. */
  694. *depth = trace->depth - 1;
  695. }
  696. if (print_graph_prologue(iter, s, 0, 0))
  697. return TRACE_TYPE_PARTIAL_LINE;
  698. /* Overhead */
  699. ret = print_graph_overhead(duration, s);
  700. if (!ret)
  701. return TRACE_TYPE_PARTIAL_LINE;
  702. /* Duration */
  703. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  704. ret = print_graph_duration(duration, s);
  705. if (ret == TRACE_TYPE_PARTIAL_LINE)
  706. return TRACE_TYPE_PARTIAL_LINE;
  707. }
  708. /* Closing brace */
  709. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
  710. ret = trace_seq_printf(s, " ");
  711. if (!ret)
  712. return TRACE_TYPE_PARTIAL_LINE;
  713. }
  714. ret = trace_seq_printf(s, "}\n");
  715. if (!ret)
  716. return TRACE_TYPE_PARTIAL_LINE;
  717. /* Overrun */
  718. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
  719. ret = trace_seq_printf(s, " (Overruns: %lu)\n",
  720. trace->overrun);
  721. if (!ret)
  722. return TRACE_TYPE_PARTIAL_LINE;
  723. }
  724. ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
  725. if (ret == TRACE_TYPE_PARTIAL_LINE)
  726. return TRACE_TYPE_PARTIAL_LINE;
  727. return TRACE_TYPE_HANDLED;
  728. }
  729. static enum print_line_t
  730. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  731. struct trace_iterator *iter)
  732. {
  733. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  734. struct fgraph_data *data = iter->private;
  735. struct trace_event *event;
  736. int depth = 0;
  737. int ret;
  738. int i;
  739. if (data)
  740. depth = per_cpu_ptr(data, iter->cpu)->depth;
  741. if (print_graph_prologue(iter, s, 0, 0))
  742. return TRACE_TYPE_PARTIAL_LINE;
  743. /* No overhead */
  744. ret = print_graph_overhead(-1, s);
  745. if (!ret)
  746. return TRACE_TYPE_PARTIAL_LINE;
  747. /* No time */
  748. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  749. ret = trace_seq_printf(s, " | ");
  750. if (!ret)
  751. return TRACE_TYPE_PARTIAL_LINE;
  752. }
  753. /* Indentation */
  754. if (depth > 0)
  755. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
  756. ret = trace_seq_printf(s, " ");
  757. if (!ret)
  758. return TRACE_TYPE_PARTIAL_LINE;
  759. }
  760. /* The comment */
  761. ret = trace_seq_printf(s, "/* ");
  762. if (!ret)
  763. return TRACE_TYPE_PARTIAL_LINE;
  764. switch (iter->ent->type) {
  765. case TRACE_BPRINT:
  766. ret = trace_print_bprintk_msg_only(iter);
  767. if (ret != TRACE_TYPE_HANDLED)
  768. return ret;
  769. break;
  770. case TRACE_PRINT:
  771. ret = trace_print_printk_msg_only(iter);
  772. if (ret != TRACE_TYPE_HANDLED)
  773. return ret;
  774. break;
  775. default:
  776. event = ftrace_find_event(ent->type);
  777. if (!event)
  778. return TRACE_TYPE_UNHANDLED;
  779. ret = event->trace(iter, sym_flags);
  780. if (ret != TRACE_TYPE_HANDLED)
  781. return ret;
  782. }
  783. /* Strip ending newline */
  784. if (s->buffer[s->len - 1] == '\n') {
  785. s->buffer[s->len - 1] = '\0';
  786. s->len--;
  787. }
  788. ret = trace_seq_printf(s, " */\n");
  789. if (!ret)
  790. return TRACE_TYPE_PARTIAL_LINE;
  791. return TRACE_TYPE_HANDLED;
  792. }
  793. enum print_line_t
  794. print_graph_function(struct trace_iterator *iter)
  795. {
  796. struct trace_entry *entry = iter->ent;
  797. struct trace_seq *s = &iter->seq;
  798. switch (entry->type) {
  799. case TRACE_GRAPH_ENT: {
  800. /*
  801. * print_graph_entry() may consume the current event,
  802. * thus @field may become invalid, so we need to save it.
  803. * sizeof(struct ftrace_graph_ent_entry) is very small,
  804. * it can be safely saved at the stack.
  805. */
  806. struct ftrace_graph_ent_entry *field, saved;
  807. trace_assign_type(field, entry);
  808. saved = *field;
  809. return print_graph_entry(&saved, s, iter);
  810. }
  811. case TRACE_GRAPH_RET: {
  812. struct ftrace_graph_ret_entry *field;
  813. trace_assign_type(field, entry);
  814. return print_graph_return(&field->ret, s, entry, iter);
  815. }
  816. default:
  817. return print_graph_comment(s, entry, iter);
  818. }
  819. return TRACE_TYPE_HANDLED;
  820. }
  821. static void print_lat_header(struct seq_file *s)
  822. {
  823. static const char spaces[] = " " /* 16 spaces */
  824. " " /* 4 spaces */
  825. " "; /* 17 spaces */
  826. int size = 0;
  827. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  828. size += 16;
  829. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  830. size += 4;
  831. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  832. size += 17;
  833. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  834. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  835. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  836. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  837. seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces);
  838. seq_printf(s, "#%.*s|||| / \n", size, spaces);
  839. }
  840. static void print_graph_headers(struct seq_file *s)
  841. {
  842. int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
  843. if (lat)
  844. print_lat_header(s);
  845. /* 1st line */
  846. seq_printf(s, "#");
  847. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  848. seq_printf(s, " TIME ");
  849. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  850. seq_printf(s, " CPU");
  851. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  852. seq_printf(s, " TASK/PID ");
  853. if (lat)
  854. seq_printf(s, "|||||");
  855. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  856. seq_printf(s, " DURATION ");
  857. seq_printf(s, " FUNCTION CALLS\n");
  858. /* 2nd line */
  859. seq_printf(s, "#");
  860. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  861. seq_printf(s, " | ");
  862. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  863. seq_printf(s, " | ");
  864. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  865. seq_printf(s, " | | ");
  866. if (lat)
  867. seq_printf(s, "|||||");
  868. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  869. seq_printf(s, " | | ");
  870. seq_printf(s, " | | | |\n");
  871. }
  872. static void graph_trace_open(struct trace_iterator *iter)
  873. {
  874. /* pid and depth on the last trace processed */
  875. struct fgraph_data *data = alloc_percpu(struct fgraph_data);
  876. int cpu;
  877. if (!data)
  878. pr_warning("function graph tracer: not enough memory\n");
  879. else
  880. for_each_possible_cpu(cpu) {
  881. pid_t *pid = &(per_cpu_ptr(data, cpu)->last_pid);
  882. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  883. *pid = -1;
  884. *depth = 0;
  885. }
  886. iter->private = data;
  887. }
  888. static void graph_trace_close(struct trace_iterator *iter)
  889. {
  890. free_percpu(iter->private);
  891. }
  892. static struct tracer graph_trace __read_mostly = {
  893. .name = "function_graph",
  894. .open = graph_trace_open,
  895. .close = graph_trace_close,
  896. .wait_pipe = poll_wait_pipe,
  897. .init = graph_trace_init,
  898. .reset = graph_trace_reset,
  899. .print_line = print_graph_function,
  900. .print_header = print_graph_headers,
  901. .flags = &tracer_flags,
  902. #ifdef CONFIG_FTRACE_SELFTEST
  903. .selftest = trace_selftest_startup_function_graph,
  904. #endif
  905. };
  906. static __init int init_graph_trace(void)
  907. {
  908. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  909. return register_tracer(&graph_trace);
  910. }
  911. device_initcall(init_graph_trace);