trace_functions_graph.c 29 KB

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