trace_functions_graph.c 24 KB

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