trace_functions_graph.c 24 KB

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