trace_functions_graph.c 28 KB

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