trace_functions_graph.c 30 KB

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