trace_functions_graph.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. data->ret = *next;
  425. }
  426. }
  427. if (next->ent.type != TRACE_GRAPH_RET)
  428. return NULL;
  429. if (curr->ent.pid != next->ent.pid ||
  430. curr->graph_ent.func != next->ret.func)
  431. return NULL;
  432. /* this is a leaf, now advance the iterator */
  433. if (ring_iter)
  434. ring_buffer_read(ring_iter, NULL);
  435. return next;
  436. }
  437. /* Signal a overhead of time execution to the output */
  438. static int
  439. print_graph_overhead(unsigned long long duration, struct trace_seq *s,
  440. u32 flags)
  441. {
  442. /* If duration disappear, we don't need anything */
  443. if (!(flags & TRACE_GRAPH_PRINT_DURATION))
  444. return 1;
  445. /* Non nested entry or return */
  446. if (duration == -1)
  447. return trace_seq_printf(s, " ");
  448. if (flags & TRACE_GRAPH_PRINT_OVERHEAD) {
  449. /* Duration exceeded 100 msecs */
  450. if (duration > 100000ULL)
  451. return trace_seq_printf(s, "! ");
  452. /* Duration exceeded 10 msecs */
  453. if (duration > 10000ULL)
  454. return trace_seq_printf(s, "+ ");
  455. }
  456. return trace_seq_printf(s, " ");
  457. }
  458. static int print_graph_abs_time(u64 t, struct trace_seq *s)
  459. {
  460. unsigned long usecs_rem;
  461. usecs_rem = do_div(t, NSEC_PER_SEC);
  462. usecs_rem /= 1000;
  463. return trace_seq_printf(s, "%5lu.%06lu | ",
  464. (unsigned long)t, usecs_rem);
  465. }
  466. static enum print_line_t
  467. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  468. enum trace_type type, int cpu, pid_t pid, u32 flags)
  469. {
  470. int ret;
  471. struct trace_seq *s = &iter->seq;
  472. if (addr < (unsigned long)__irqentry_text_start ||
  473. addr >= (unsigned long)__irqentry_text_end)
  474. return TRACE_TYPE_UNHANDLED;
  475. /* Absolute time */
  476. if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
  477. ret = print_graph_abs_time(iter->ts, s);
  478. if (!ret)
  479. return TRACE_TYPE_PARTIAL_LINE;
  480. }
  481. /* Cpu */
  482. if (flags & TRACE_GRAPH_PRINT_CPU) {
  483. ret = print_graph_cpu(s, cpu);
  484. if (ret == TRACE_TYPE_PARTIAL_LINE)
  485. return TRACE_TYPE_PARTIAL_LINE;
  486. }
  487. /* Proc */
  488. if (flags & TRACE_GRAPH_PRINT_PROC) {
  489. ret = print_graph_proc(s, pid);
  490. if (ret == TRACE_TYPE_PARTIAL_LINE)
  491. return TRACE_TYPE_PARTIAL_LINE;
  492. ret = trace_seq_printf(s, " | ");
  493. if (!ret)
  494. return TRACE_TYPE_PARTIAL_LINE;
  495. }
  496. /* No overhead */
  497. ret = print_graph_overhead(-1, s, flags);
  498. if (!ret)
  499. return TRACE_TYPE_PARTIAL_LINE;
  500. if (type == TRACE_GRAPH_ENT)
  501. ret = trace_seq_printf(s, "==========>");
  502. else
  503. ret = trace_seq_printf(s, "<==========");
  504. if (!ret)
  505. return TRACE_TYPE_PARTIAL_LINE;
  506. /* Don't close the duration column if haven't one */
  507. if (flags & TRACE_GRAPH_PRINT_DURATION)
  508. trace_seq_printf(s, " |");
  509. ret = trace_seq_printf(s, "\n");
  510. if (!ret)
  511. return TRACE_TYPE_PARTIAL_LINE;
  512. return TRACE_TYPE_HANDLED;
  513. }
  514. enum print_line_t
  515. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
  516. {
  517. unsigned long nsecs_rem = do_div(duration, 1000);
  518. /* log10(ULONG_MAX) + '\0' */
  519. char msecs_str[21];
  520. char nsecs_str[5];
  521. int ret, len;
  522. int i;
  523. sprintf(msecs_str, "%lu", (unsigned long) duration);
  524. /* Print msecs */
  525. ret = trace_seq_printf(s, "%s", msecs_str);
  526. if (!ret)
  527. return TRACE_TYPE_PARTIAL_LINE;
  528. len = strlen(msecs_str);
  529. /* Print nsecs (we don't want to exceed 7 numbers) */
  530. if (len < 7) {
  531. snprintf(nsecs_str, min(sizeof(nsecs_str), 8UL - len), "%03lu",
  532. nsecs_rem);
  533. ret = trace_seq_printf(s, ".%s", nsecs_str);
  534. if (!ret)
  535. return TRACE_TYPE_PARTIAL_LINE;
  536. len += strlen(nsecs_str);
  537. }
  538. ret = trace_seq_printf(s, " us ");
  539. if (!ret)
  540. return TRACE_TYPE_PARTIAL_LINE;
  541. /* Print remaining spaces to fit the row's width */
  542. for (i = len; i < 7; i++) {
  543. ret = trace_seq_printf(s, " ");
  544. if (!ret)
  545. return TRACE_TYPE_PARTIAL_LINE;
  546. }
  547. return TRACE_TYPE_HANDLED;
  548. }
  549. static enum print_line_t
  550. print_graph_duration(unsigned long long duration, struct trace_seq *s)
  551. {
  552. int ret;
  553. ret = trace_print_graph_duration(duration, s);
  554. if (ret != TRACE_TYPE_HANDLED)
  555. return ret;
  556. ret = trace_seq_printf(s, "| ");
  557. if (!ret)
  558. return TRACE_TYPE_PARTIAL_LINE;
  559. return TRACE_TYPE_HANDLED;
  560. }
  561. /* Case of a leaf function on its call entry */
  562. static enum print_line_t
  563. print_graph_entry_leaf(struct trace_iterator *iter,
  564. struct ftrace_graph_ent_entry *entry,
  565. struct ftrace_graph_ret_entry *ret_entry,
  566. struct trace_seq *s, u32 flags)
  567. {
  568. struct fgraph_data *data = iter->private;
  569. struct ftrace_graph_ret *graph_ret;
  570. struct ftrace_graph_ent *call;
  571. unsigned long long duration;
  572. int ret;
  573. int i;
  574. graph_ret = &ret_entry->ret;
  575. call = &entry->graph_ent;
  576. duration = graph_ret->rettime - graph_ret->calltime;
  577. if (data) {
  578. struct fgraph_cpu_data *cpu_data;
  579. int cpu = iter->cpu;
  580. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  581. /*
  582. * Comments display at + 1 to depth. Since
  583. * this is a leaf function, keep the comments
  584. * equal to this depth.
  585. */
  586. cpu_data->depth = call->depth - 1;
  587. /* No need to keep this function around for this depth */
  588. if (call->depth < FTRACE_RETFUNC_DEPTH)
  589. cpu_data->enter_funcs[call->depth] = 0;
  590. }
  591. /* Overhead */
  592. ret = print_graph_overhead(duration, s, flags);
  593. if (!ret)
  594. return TRACE_TYPE_PARTIAL_LINE;
  595. /* Duration */
  596. if (flags & TRACE_GRAPH_PRINT_DURATION) {
  597. ret = print_graph_duration(duration, s);
  598. if (ret == TRACE_TYPE_PARTIAL_LINE)
  599. return TRACE_TYPE_PARTIAL_LINE;
  600. }
  601. /* Function */
  602. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  603. ret = trace_seq_printf(s, " ");
  604. if (!ret)
  605. return TRACE_TYPE_PARTIAL_LINE;
  606. }
  607. ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
  608. if (!ret)
  609. return TRACE_TYPE_PARTIAL_LINE;
  610. return TRACE_TYPE_HANDLED;
  611. }
  612. static enum print_line_t
  613. print_graph_entry_nested(struct trace_iterator *iter,
  614. struct ftrace_graph_ent_entry *entry,
  615. struct trace_seq *s, int cpu, u32 flags)
  616. {
  617. struct ftrace_graph_ent *call = &entry->graph_ent;
  618. struct fgraph_data *data = iter->private;
  619. int ret;
  620. int i;
  621. if (data) {
  622. struct fgraph_cpu_data *cpu_data;
  623. int cpu = iter->cpu;
  624. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  625. cpu_data->depth = call->depth;
  626. /* Save this function pointer to see if the exit matches */
  627. if (call->depth < FTRACE_RETFUNC_DEPTH)
  628. cpu_data->enter_funcs[call->depth] = call->func;
  629. }
  630. /* No overhead */
  631. ret = print_graph_overhead(-1, s, flags);
  632. if (!ret)
  633. return TRACE_TYPE_PARTIAL_LINE;
  634. /* No time */
  635. if (flags & TRACE_GRAPH_PRINT_DURATION) {
  636. ret = trace_seq_printf(s, " | ");
  637. if (!ret)
  638. return TRACE_TYPE_PARTIAL_LINE;
  639. }
  640. /* Function */
  641. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  642. ret = trace_seq_printf(s, " ");
  643. if (!ret)
  644. return TRACE_TYPE_PARTIAL_LINE;
  645. }
  646. ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
  647. if (!ret)
  648. return TRACE_TYPE_PARTIAL_LINE;
  649. /*
  650. * we already consumed the current entry to check the next one
  651. * and see if this is a leaf.
  652. */
  653. return TRACE_TYPE_NO_CONSUME;
  654. }
  655. static enum print_line_t
  656. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  657. int type, unsigned long addr, u32 flags)
  658. {
  659. struct fgraph_data *data = iter->private;
  660. struct trace_entry *ent = iter->ent;
  661. int cpu = iter->cpu;
  662. int ret;
  663. /* Pid */
  664. if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
  665. return TRACE_TYPE_PARTIAL_LINE;
  666. if (type) {
  667. /* Interrupt */
  668. ret = print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
  669. if (ret == TRACE_TYPE_PARTIAL_LINE)
  670. return TRACE_TYPE_PARTIAL_LINE;
  671. }
  672. /* Absolute time */
  673. if (flags & TRACE_GRAPH_PRINT_ABS_TIME) {
  674. ret = print_graph_abs_time(iter->ts, s);
  675. if (!ret)
  676. return TRACE_TYPE_PARTIAL_LINE;
  677. }
  678. /* Cpu */
  679. if (flags & TRACE_GRAPH_PRINT_CPU) {
  680. ret = print_graph_cpu(s, cpu);
  681. if (ret == TRACE_TYPE_PARTIAL_LINE)
  682. return TRACE_TYPE_PARTIAL_LINE;
  683. }
  684. /* Proc */
  685. if (flags & TRACE_GRAPH_PRINT_PROC) {
  686. ret = print_graph_proc(s, ent->pid);
  687. if (ret == TRACE_TYPE_PARTIAL_LINE)
  688. return TRACE_TYPE_PARTIAL_LINE;
  689. ret = trace_seq_printf(s, " | ");
  690. if (!ret)
  691. return TRACE_TYPE_PARTIAL_LINE;
  692. }
  693. /* Latency format */
  694. if (trace_flags & TRACE_ITER_LATENCY_FMT) {
  695. ret = print_graph_lat_fmt(s, ent);
  696. if (ret == TRACE_TYPE_PARTIAL_LINE)
  697. return TRACE_TYPE_PARTIAL_LINE;
  698. }
  699. return 0;
  700. }
  701. static enum print_line_t
  702. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  703. struct trace_iterator *iter, u32 flags)
  704. {
  705. struct fgraph_data *data = iter->private;
  706. struct ftrace_graph_ent *call = &field->graph_ent;
  707. struct ftrace_graph_ret_entry *leaf_ret;
  708. static enum print_line_t ret;
  709. int cpu = iter->cpu;
  710. if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags))
  711. return TRACE_TYPE_PARTIAL_LINE;
  712. leaf_ret = get_return_for_leaf(iter, field);
  713. if (leaf_ret)
  714. ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
  715. else
  716. ret = print_graph_entry_nested(iter, field, s, cpu, flags);
  717. if (data) {
  718. /*
  719. * If we failed to write our output, then we need to make
  720. * note of it. Because we already consumed our entry.
  721. */
  722. if (s->full) {
  723. data->failed = 1;
  724. data->cpu = cpu;
  725. } else
  726. data->failed = 0;
  727. }
  728. return ret;
  729. }
  730. static enum print_line_t
  731. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  732. struct trace_entry *ent, struct trace_iterator *iter,
  733. u32 flags)
  734. {
  735. unsigned long long duration = trace->rettime - trace->calltime;
  736. struct fgraph_data *data = iter->private;
  737. pid_t pid = ent->pid;
  738. int cpu = iter->cpu;
  739. int func_match = 1;
  740. int ret;
  741. int i;
  742. if (data) {
  743. struct fgraph_cpu_data *cpu_data;
  744. int cpu = iter->cpu;
  745. cpu_data = per_cpu_ptr(data->cpu_data, cpu);
  746. /*
  747. * Comments display at + 1 to depth. This is the
  748. * return from a function, we now want the comments
  749. * to display at the same level of the bracket.
  750. */
  751. cpu_data->depth = trace->depth - 1;
  752. if (trace->depth < FTRACE_RETFUNC_DEPTH) {
  753. if (cpu_data->enter_funcs[trace->depth] != trace->func)
  754. func_match = 0;
  755. cpu_data->enter_funcs[trace->depth] = 0;
  756. }
  757. }
  758. if (print_graph_prologue(iter, s, 0, 0, flags))
  759. return TRACE_TYPE_PARTIAL_LINE;
  760. /* Overhead */
  761. ret = print_graph_overhead(duration, s, flags);
  762. if (!ret)
  763. return TRACE_TYPE_PARTIAL_LINE;
  764. /* Duration */
  765. if (flags & TRACE_GRAPH_PRINT_DURATION) {
  766. ret = print_graph_duration(duration, s);
  767. if (ret == TRACE_TYPE_PARTIAL_LINE)
  768. return TRACE_TYPE_PARTIAL_LINE;
  769. }
  770. /* Closing brace */
  771. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
  772. ret = trace_seq_printf(s, " ");
  773. if (!ret)
  774. return TRACE_TYPE_PARTIAL_LINE;
  775. }
  776. /*
  777. * If the return function does not have a matching entry,
  778. * then the entry was lost. Instead of just printing
  779. * the '}' and letting the user guess what function this
  780. * belongs to, write out the function name.
  781. */
  782. if (func_match) {
  783. ret = trace_seq_printf(s, "}\n");
  784. if (!ret)
  785. return TRACE_TYPE_PARTIAL_LINE;
  786. } else {
  787. ret = trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
  788. if (!ret)
  789. return TRACE_TYPE_PARTIAL_LINE;
  790. }
  791. /* Overrun */
  792. if (flags & TRACE_GRAPH_PRINT_OVERRUN) {
  793. ret = trace_seq_printf(s, " (Overruns: %lu)\n",
  794. trace->overrun);
  795. if (!ret)
  796. return TRACE_TYPE_PARTIAL_LINE;
  797. }
  798. ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
  799. cpu, pid, flags);
  800. if (ret == TRACE_TYPE_PARTIAL_LINE)
  801. return TRACE_TYPE_PARTIAL_LINE;
  802. return TRACE_TYPE_HANDLED;
  803. }
  804. static enum print_line_t
  805. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  806. struct trace_iterator *iter, u32 flags)
  807. {
  808. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  809. struct fgraph_data *data = iter->private;
  810. struct trace_event *event;
  811. int depth = 0;
  812. int ret;
  813. int i;
  814. if (data)
  815. depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
  816. if (print_graph_prologue(iter, s, 0, 0, flags))
  817. return TRACE_TYPE_PARTIAL_LINE;
  818. /* No overhead */
  819. ret = print_graph_overhead(-1, s, flags);
  820. if (!ret)
  821. return TRACE_TYPE_PARTIAL_LINE;
  822. /* No time */
  823. if (flags & TRACE_GRAPH_PRINT_DURATION) {
  824. ret = trace_seq_printf(s, " | ");
  825. if (!ret)
  826. return TRACE_TYPE_PARTIAL_LINE;
  827. }
  828. /* Indentation */
  829. if (depth > 0)
  830. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
  831. ret = trace_seq_printf(s, " ");
  832. if (!ret)
  833. return TRACE_TYPE_PARTIAL_LINE;
  834. }
  835. /* The comment */
  836. ret = trace_seq_printf(s, "/* ");
  837. if (!ret)
  838. return TRACE_TYPE_PARTIAL_LINE;
  839. switch (iter->ent->type) {
  840. case TRACE_BPRINT:
  841. ret = trace_print_bprintk_msg_only(iter);
  842. if (ret != TRACE_TYPE_HANDLED)
  843. return ret;
  844. break;
  845. case TRACE_PRINT:
  846. ret = trace_print_printk_msg_only(iter);
  847. if (ret != TRACE_TYPE_HANDLED)
  848. return ret;
  849. break;
  850. default:
  851. event = ftrace_find_event(ent->type);
  852. if (!event)
  853. return TRACE_TYPE_UNHANDLED;
  854. ret = event->funcs->trace(iter, sym_flags, event);
  855. if (ret != TRACE_TYPE_HANDLED)
  856. return ret;
  857. }
  858. /* Strip ending newline */
  859. if (s->buffer[s->len - 1] == '\n') {
  860. s->buffer[s->len - 1] = '\0';
  861. s->len--;
  862. }
  863. ret = trace_seq_printf(s, " */\n");
  864. if (!ret)
  865. return TRACE_TYPE_PARTIAL_LINE;
  866. return TRACE_TYPE_HANDLED;
  867. }
  868. enum print_line_t
  869. print_graph_function_flags(struct trace_iterator *iter, u32 flags)
  870. {
  871. struct ftrace_graph_ent_entry *field;
  872. struct fgraph_data *data = iter->private;
  873. struct trace_entry *entry = iter->ent;
  874. struct trace_seq *s = &iter->seq;
  875. int cpu = iter->cpu;
  876. int ret;
  877. if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
  878. per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
  879. return TRACE_TYPE_HANDLED;
  880. }
  881. /*
  882. * If the last output failed, there's a possibility we need
  883. * to print out the missing entry which would never go out.
  884. */
  885. if (data && data->failed) {
  886. field = &data->ent;
  887. iter->cpu = data->cpu;
  888. ret = print_graph_entry(field, s, iter, flags);
  889. if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
  890. per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
  891. ret = TRACE_TYPE_NO_CONSUME;
  892. }
  893. iter->cpu = cpu;
  894. return ret;
  895. }
  896. switch (entry->type) {
  897. case TRACE_GRAPH_ENT: {
  898. /*
  899. * print_graph_entry() may consume the current event,
  900. * thus @field may become invalid, so we need to save it.
  901. * sizeof(struct ftrace_graph_ent_entry) is very small,
  902. * it can be safely saved at the stack.
  903. */
  904. struct ftrace_graph_ent_entry saved;
  905. trace_assign_type(field, entry);
  906. saved = *field;
  907. return print_graph_entry(&saved, s, iter, flags);
  908. }
  909. case TRACE_GRAPH_RET: {
  910. struct ftrace_graph_ret_entry *field;
  911. trace_assign_type(field, entry);
  912. return print_graph_return(&field->ret, s, entry, iter, flags);
  913. }
  914. case TRACE_STACK:
  915. case TRACE_FN:
  916. /* dont trace stack and functions as comments */
  917. return TRACE_TYPE_UNHANDLED;
  918. default:
  919. return print_graph_comment(s, entry, iter, flags);
  920. }
  921. return TRACE_TYPE_HANDLED;
  922. }
  923. static enum print_line_t
  924. print_graph_function(struct trace_iterator *iter)
  925. {
  926. return print_graph_function_flags(iter, tracer_flags.val);
  927. }
  928. static enum print_line_t
  929. print_graph_function_event(struct trace_iterator *iter, int flags,
  930. struct trace_event *event)
  931. {
  932. return print_graph_function(iter);
  933. }
  934. static void print_lat_header(struct seq_file *s, u32 flags)
  935. {
  936. static const char spaces[] = " " /* 16 spaces */
  937. " " /* 4 spaces */
  938. " "; /* 17 spaces */
  939. int size = 0;
  940. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  941. size += 16;
  942. if (flags & TRACE_GRAPH_PRINT_CPU)
  943. size += 4;
  944. if (flags & TRACE_GRAPH_PRINT_PROC)
  945. size += 17;
  946. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  947. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  948. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  949. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  950. seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces);
  951. seq_printf(s, "#%.*s|||| / \n", size, spaces);
  952. }
  953. void print_graph_headers_flags(struct seq_file *s, u32 flags)
  954. {
  955. int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
  956. if (lat)
  957. print_lat_header(s, flags);
  958. /* 1st line */
  959. seq_printf(s, "#");
  960. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  961. seq_printf(s, " TIME ");
  962. if (flags & TRACE_GRAPH_PRINT_CPU)
  963. seq_printf(s, " CPU");
  964. if (flags & TRACE_GRAPH_PRINT_PROC)
  965. seq_printf(s, " TASK/PID ");
  966. if (lat)
  967. seq_printf(s, "|||||");
  968. if (flags & TRACE_GRAPH_PRINT_DURATION)
  969. seq_printf(s, " DURATION ");
  970. seq_printf(s, " FUNCTION CALLS\n");
  971. /* 2nd line */
  972. seq_printf(s, "#");
  973. if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
  974. seq_printf(s, " | ");
  975. if (flags & TRACE_GRAPH_PRINT_CPU)
  976. seq_printf(s, " | ");
  977. if (flags & TRACE_GRAPH_PRINT_PROC)
  978. seq_printf(s, " | | ");
  979. if (lat)
  980. seq_printf(s, "|||||");
  981. if (flags & TRACE_GRAPH_PRINT_DURATION)
  982. seq_printf(s, " | | ");
  983. seq_printf(s, " | | | |\n");
  984. }
  985. void print_graph_headers(struct seq_file *s)
  986. {
  987. print_graph_headers_flags(s, tracer_flags.val);
  988. }
  989. void graph_trace_open(struct trace_iterator *iter)
  990. {
  991. /* pid and depth on the last trace processed */
  992. struct fgraph_data *data;
  993. int cpu;
  994. iter->private = NULL;
  995. data = kzalloc(sizeof(*data), GFP_KERNEL);
  996. if (!data)
  997. goto out_err;
  998. data->cpu_data = alloc_percpu(struct fgraph_cpu_data);
  999. if (!data->cpu_data)
  1000. goto out_err_free;
  1001. for_each_possible_cpu(cpu) {
  1002. pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
  1003. int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
  1004. int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
  1005. *pid = -1;
  1006. *depth = 0;
  1007. *ignore = 0;
  1008. }
  1009. iter->private = data;
  1010. return;
  1011. out_err_free:
  1012. kfree(data);
  1013. out_err:
  1014. pr_warning("function graph tracer: not enough memory\n");
  1015. }
  1016. void graph_trace_close(struct trace_iterator *iter)
  1017. {
  1018. struct fgraph_data *data = iter->private;
  1019. if (data) {
  1020. free_percpu(data->cpu_data);
  1021. kfree(data);
  1022. }
  1023. }
  1024. static struct trace_event_functions graph_functions = {
  1025. .trace = print_graph_function_event,
  1026. };
  1027. static struct trace_event graph_trace_entry_event = {
  1028. .type = TRACE_GRAPH_ENT,
  1029. .funcs = &graph_functions,
  1030. };
  1031. static struct trace_event graph_trace_ret_event = {
  1032. .type = TRACE_GRAPH_RET,
  1033. .funcs = &graph_functions
  1034. };
  1035. static struct tracer graph_trace __read_mostly = {
  1036. .name = "function_graph",
  1037. .open = graph_trace_open,
  1038. .pipe_open = graph_trace_open,
  1039. .close = graph_trace_close,
  1040. .pipe_close = graph_trace_close,
  1041. .wait_pipe = poll_wait_pipe,
  1042. .init = graph_trace_init,
  1043. .reset = graph_trace_reset,
  1044. .print_line = print_graph_function,
  1045. .print_header = print_graph_headers,
  1046. .flags = &tracer_flags,
  1047. #ifdef CONFIG_FTRACE_SELFTEST
  1048. .selftest = trace_selftest_startup_function_graph,
  1049. #endif
  1050. };
  1051. static __init int init_graph_trace(void)
  1052. {
  1053. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  1054. if (!register_ftrace_event(&graph_trace_entry_event)) {
  1055. pr_warning("Warning: could not register graph trace events\n");
  1056. return 1;
  1057. }
  1058. if (!register_ftrace_event(&graph_trace_ret_event)) {
  1059. pr_warning("Warning: could not register graph trace events\n");
  1060. return 1;
  1061. }
  1062. return register_tracer(&graph_trace);
  1063. }
  1064. device_initcall(init_graph_trace);