trace_functions_graph.c 28 KB

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