trace_functions_graph.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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 %ps 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(__this_cpu_read(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(__this_cpu_read(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. static enum print_line_t
  309. print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
  310. {
  311. if (!trace_seq_putc(s, ' '))
  312. return 0;
  313. return trace_print_lat_fmt(s, entry);
  314. }
  315. /* If the pid changed since the last trace, output this event */
  316. static enum print_line_t
  317. verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
  318. {
  319. pid_t prev_pid;
  320. pid_t *last_pid;
  321. int ret;
  322. if (!data)
  323. return TRACE_TYPE_HANDLED;
  324. last_pid = &(per_cpu_ptr(data, cpu)->last_pid);
  325. if (*last_pid == pid)
  326. return TRACE_TYPE_HANDLED;
  327. prev_pid = *last_pid;
  328. *last_pid = pid;
  329. if (prev_pid == -1)
  330. return TRACE_TYPE_HANDLED;
  331. /*
  332. * Context-switch trace line:
  333. ------------------------------------------
  334. | 1) migration/0--1 => sshd-1755
  335. ------------------------------------------
  336. */
  337. ret = trace_seq_printf(s,
  338. " ------------------------------------------\n");
  339. if (!ret)
  340. return TRACE_TYPE_PARTIAL_LINE;
  341. ret = print_graph_cpu(s, cpu);
  342. if (ret == TRACE_TYPE_PARTIAL_LINE)
  343. return TRACE_TYPE_PARTIAL_LINE;
  344. ret = print_graph_proc(s, prev_pid);
  345. if (ret == TRACE_TYPE_PARTIAL_LINE)
  346. return TRACE_TYPE_PARTIAL_LINE;
  347. ret = trace_seq_printf(s, " => ");
  348. if (!ret)
  349. return TRACE_TYPE_PARTIAL_LINE;
  350. ret = print_graph_proc(s, pid);
  351. if (ret == TRACE_TYPE_PARTIAL_LINE)
  352. return TRACE_TYPE_PARTIAL_LINE;
  353. ret = trace_seq_printf(s,
  354. "\n ------------------------------------------\n\n");
  355. if (!ret)
  356. return TRACE_TYPE_PARTIAL_LINE;
  357. return TRACE_TYPE_HANDLED;
  358. }
  359. static struct ftrace_graph_ret_entry *
  360. get_return_for_leaf(struct trace_iterator *iter,
  361. struct ftrace_graph_ent_entry *curr)
  362. {
  363. struct ring_buffer_iter *ring_iter;
  364. struct ring_buffer_event *event;
  365. struct ftrace_graph_ret_entry *next;
  366. ring_iter = iter->buffer_iter[iter->cpu];
  367. /* First peek to compare current entry and the next one */
  368. if (ring_iter)
  369. event = ring_buffer_iter_peek(ring_iter, NULL);
  370. else {
  371. /* We need to consume the current entry to see the next one */
  372. ring_buffer_consume(iter->tr->buffer, iter->cpu, NULL);
  373. event = ring_buffer_peek(iter->tr->buffer, iter->cpu,
  374. NULL);
  375. }
  376. if (!event)
  377. return NULL;
  378. next = ring_buffer_event_data(event);
  379. if (next->ent.type != TRACE_GRAPH_RET)
  380. return NULL;
  381. if (curr->ent.pid != next->ent.pid ||
  382. curr->graph_ent.func != next->ret.func)
  383. return NULL;
  384. /* this is a leaf, now advance the iterator */
  385. if (ring_iter)
  386. ring_buffer_read(ring_iter, NULL);
  387. return next;
  388. }
  389. /* Signal a overhead of time execution to the output */
  390. static int
  391. print_graph_overhead(unsigned long long duration, struct trace_seq *s)
  392. {
  393. /* If duration disappear, we don't need anything */
  394. if (!(tracer_flags.val & TRACE_GRAPH_PRINT_DURATION))
  395. return 1;
  396. /* Non nested entry or return */
  397. if (duration == -1)
  398. return trace_seq_printf(s, " ");
  399. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERHEAD) {
  400. /* Duration exceeded 100 msecs */
  401. if (duration > 100000ULL)
  402. return trace_seq_printf(s, "! ");
  403. /* Duration exceeded 10 msecs */
  404. if (duration > 10000ULL)
  405. return trace_seq_printf(s, "+ ");
  406. }
  407. return trace_seq_printf(s, " ");
  408. }
  409. static int print_graph_abs_time(u64 t, struct trace_seq *s)
  410. {
  411. unsigned long usecs_rem;
  412. usecs_rem = do_div(t, NSEC_PER_SEC);
  413. usecs_rem /= 1000;
  414. return trace_seq_printf(s, "%5lu.%06lu | ",
  415. (unsigned long)t, usecs_rem);
  416. }
  417. static enum print_line_t
  418. print_graph_irq(struct trace_iterator *iter, unsigned long addr,
  419. enum trace_type type, int cpu, pid_t pid)
  420. {
  421. int ret;
  422. struct trace_seq *s = &iter->seq;
  423. if (addr < (unsigned long)__irqentry_text_start ||
  424. addr >= (unsigned long)__irqentry_text_end)
  425. return TRACE_TYPE_UNHANDLED;
  426. /* Absolute time */
  427. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  428. ret = print_graph_abs_time(iter->ts, s);
  429. if (!ret)
  430. return TRACE_TYPE_PARTIAL_LINE;
  431. }
  432. /* Cpu */
  433. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  434. ret = print_graph_cpu(s, cpu);
  435. if (ret == TRACE_TYPE_PARTIAL_LINE)
  436. return TRACE_TYPE_PARTIAL_LINE;
  437. }
  438. /* Proc */
  439. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  440. ret = print_graph_proc(s, pid);
  441. if (ret == TRACE_TYPE_PARTIAL_LINE)
  442. return TRACE_TYPE_PARTIAL_LINE;
  443. ret = trace_seq_printf(s, " | ");
  444. if (!ret)
  445. return TRACE_TYPE_PARTIAL_LINE;
  446. }
  447. /* No overhead */
  448. ret = print_graph_overhead(-1, s);
  449. if (!ret)
  450. return TRACE_TYPE_PARTIAL_LINE;
  451. if (type == TRACE_GRAPH_ENT)
  452. ret = trace_seq_printf(s, "==========>");
  453. else
  454. ret = trace_seq_printf(s, "<==========");
  455. if (!ret)
  456. return TRACE_TYPE_PARTIAL_LINE;
  457. /* Don't close the duration column if haven't one */
  458. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  459. trace_seq_printf(s, " |");
  460. ret = trace_seq_printf(s, "\n");
  461. if (!ret)
  462. return TRACE_TYPE_PARTIAL_LINE;
  463. return TRACE_TYPE_HANDLED;
  464. }
  465. enum print_line_t
  466. trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
  467. {
  468. unsigned long nsecs_rem = do_div(duration, 1000);
  469. /* log10(ULONG_MAX) + '\0' */
  470. char msecs_str[21];
  471. char nsecs_str[5];
  472. int ret, len;
  473. int i;
  474. sprintf(msecs_str, "%lu", (unsigned long) duration);
  475. /* Print msecs */
  476. ret = trace_seq_printf(s, "%s", msecs_str);
  477. if (!ret)
  478. return TRACE_TYPE_PARTIAL_LINE;
  479. len = strlen(msecs_str);
  480. /* Print nsecs (we don't want to exceed 7 numbers) */
  481. if (len < 7) {
  482. snprintf(nsecs_str, 8 - len, "%03lu", nsecs_rem);
  483. ret = trace_seq_printf(s, ".%s", nsecs_str);
  484. if (!ret)
  485. return TRACE_TYPE_PARTIAL_LINE;
  486. len += strlen(nsecs_str);
  487. }
  488. ret = trace_seq_printf(s, " us ");
  489. if (!ret)
  490. return TRACE_TYPE_PARTIAL_LINE;
  491. /* Print remaining spaces to fit the row's width */
  492. for (i = len; i < 7; i++) {
  493. ret = trace_seq_printf(s, " ");
  494. if (!ret)
  495. return TRACE_TYPE_PARTIAL_LINE;
  496. }
  497. return TRACE_TYPE_HANDLED;
  498. }
  499. static enum print_line_t
  500. print_graph_duration(unsigned long long duration, struct trace_seq *s)
  501. {
  502. int ret;
  503. ret = trace_print_graph_duration(duration, s);
  504. if (ret != TRACE_TYPE_HANDLED)
  505. return ret;
  506. ret = trace_seq_printf(s, "| ");
  507. if (!ret)
  508. return TRACE_TYPE_PARTIAL_LINE;
  509. return TRACE_TYPE_HANDLED;
  510. }
  511. /* Case of a leaf function on its call entry */
  512. static enum print_line_t
  513. print_graph_entry_leaf(struct trace_iterator *iter,
  514. struct ftrace_graph_ent_entry *entry,
  515. struct ftrace_graph_ret_entry *ret_entry, struct trace_seq *s)
  516. {
  517. struct fgraph_data *data = iter->private;
  518. struct ftrace_graph_ret *graph_ret;
  519. struct ftrace_graph_ent *call;
  520. unsigned long long duration;
  521. int ret;
  522. int i;
  523. graph_ret = &ret_entry->ret;
  524. call = &entry->graph_ent;
  525. duration = graph_ret->rettime - graph_ret->calltime;
  526. if (data) {
  527. int cpu = iter->cpu;
  528. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  529. /*
  530. * Comments display at + 1 to depth. Since
  531. * this is a leaf function, keep the comments
  532. * equal to this depth.
  533. */
  534. *depth = call->depth - 1;
  535. }
  536. /* Overhead */
  537. ret = print_graph_overhead(duration, s);
  538. if (!ret)
  539. return TRACE_TYPE_PARTIAL_LINE;
  540. /* Duration */
  541. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  542. ret = print_graph_duration(duration, s);
  543. if (ret == TRACE_TYPE_PARTIAL_LINE)
  544. return TRACE_TYPE_PARTIAL_LINE;
  545. }
  546. /* Function */
  547. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  548. ret = trace_seq_printf(s, " ");
  549. if (!ret)
  550. return TRACE_TYPE_PARTIAL_LINE;
  551. }
  552. ret = trace_seq_printf(s, "%ps();\n", (void *)call->func);
  553. if (!ret)
  554. return TRACE_TYPE_PARTIAL_LINE;
  555. return TRACE_TYPE_HANDLED;
  556. }
  557. static enum print_line_t
  558. print_graph_entry_nested(struct trace_iterator *iter,
  559. struct ftrace_graph_ent_entry *entry,
  560. struct trace_seq *s, int cpu)
  561. {
  562. struct ftrace_graph_ent *call = &entry->graph_ent;
  563. struct fgraph_data *data = iter->private;
  564. int ret;
  565. int i;
  566. if (data) {
  567. int cpu = iter->cpu;
  568. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  569. *depth = call->depth;
  570. }
  571. /* No overhead */
  572. ret = print_graph_overhead(-1, s);
  573. if (!ret)
  574. return TRACE_TYPE_PARTIAL_LINE;
  575. /* No time */
  576. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  577. ret = trace_seq_printf(s, " | ");
  578. if (!ret)
  579. return TRACE_TYPE_PARTIAL_LINE;
  580. }
  581. /* Function */
  582. for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++) {
  583. ret = trace_seq_printf(s, " ");
  584. if (!ret)
  585. return TRACE_TYPE_PARTIAL_LINE;
  586. }
  587. ret = trace_seq_printf(s, "%ps() {\n", (void *)call->func);
  588. if (!ret)
  589. return TRACE_TYPE_PARTIAL_LINE;
  590. /*
  591. * we already consumed the current entry to check the next one
  592. * and see if this is a leaf.
  593. */
  594. return TRACE_TYPE_NO_CONSUME;
  595. }
  596. static enum print_line_t
  597. print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
  598. int type, unsigned long addr)
  599. {
  600. struct fgraph_data *data = iter->private;
  601. struct trace_entry *ent = iter->ent;
  602. int cpu = iter->cpu;
  603. int ret;
  604. /* Pid */
  605. if (verif_pid(s, ent->pid, cpu, data) == TRACE_TYPE_PARTIAL_LINE)
  606. return TRACE_TYPE_PARTIAL_LINE;
  607. if (type) {
  608. /* Interrupt */
  609. ret = print_graph_irq(iter, addr, type, cpu, ent->pid);
  610. if (ret == TRACE_TYPE_PARTIAL_LINE)
  611. return TRACE_TYPE_PARTIAL_LINE;
  612. }
  613. /* Absolute time */
  614. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME) {
  615. ret = print_graph_abs_time(iter->ts, s);
  616. if (!ret)
  617. return TRACE_TYPE_PARTIAL_LINE;
  618. }
  619. /* Cpu */
  620. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU) {
  621. ret = print_graph_cpu(s, cpu);
  622. if (ret == TRACE_TYPE_PARTIAL_LINE)
  623. return TRACE_TYPE_PARTIAL_LINE;
  624. }
  625. /* Proc */
  626. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC) {
  627. ret = print_graph_proc(s, ent->pid);
  628. if (ret == TRACE_TYPE_PARTIAL_LINE)
  629. return TRACE_TYPE_PARTIAL_LINE;
  630. ret = trace_seq_printf(s, " | ");
  631. if (!ret)
  632. return TRACE_TYPE_PARTIAL_LINE;
  633. }
  634. /* Latency format */
  635. if (trace_flags & TRACE_ITER_LATENCY_FMT) {
  636. ret = print_graph_lat_fmt(s, ent);
  637. if (ret == TRACE_TYPE_PARTIAL_LINE)
  638. return TRACE_TYPE_PARTIAL_LINE;
  639. }
  640. return 0;
  641. }
  642. static enum print_line_t
  643. print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
  644. struct trace_iterator *iter)
  645. {
  646. int cpu = iter->cpu;
  647. struct ftrace_graph_ent *call = &field->graph_ent;
  648. struct ftrace_graph_ret_entry *leaf_ret;
  649. if (print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func))
  650. return TRACE_TYPE_PARTIAL_LINE;
  651. leaf_ret = get_return_for_leaf(iter, field);
  652. if (leaf_ret)
  653. return print_graph_entry_leaf(iter, field, leaf_ret, s);
  654. else
  655. return print_graph_entry_nested(iter, field, s, cpu);
  656. }
  657. static enum print_line_t
  658. print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
  659. struct trace_entry *ent, struct trace_iterator *iter)
  660. {
  661. unsigned long long duration = trace->rettime - trace->calltime;
  662. struct fgraph_data *data = iter->private;
  663. pid_t pid = ent->pid;
  664. int cpu = iter->cpu;
  665. int ret;
  666. int i;
  667. if (data) {
  668. int cpu = iter->cpu;
  669. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  670. /*
  671. * Comments display at + 1 to depth. This is the
  672. * return from a function, we now want the comments
  673. * to display at the same level of the bracket.
  674. */
  675. *depth = trace->depth - 1;
  676. }
  677. if (print_graph_prologue(iter, s, 0, 0))
  678. return TRACE_TYPE_PARTIAL_LINE;
  679. /* Overhead */
  680. ret = print_graph_overhead(duration, s);
  681. if (!ret)
  682. return TRACE_TYPE_PARTIAL_LINE;
  683. /* Duration */
  684. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  685. ret = print_graph_duration(duration, s);
  686. if (ret == TRACE_TYPE_PARTIAL_LINE)
  687. return TRACE_TYPE_PARTIAL_LINE;
  688. }
  689. /* Closing brace */
  690. for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++) {
  691. ret = trace_seq_printf(s, " ");
  692. if (!ret)
  693. return TRACE_TYPE_PARTIAL_LINE;
  694. }
  695. ret = trace_seq_printf(s, "}\n");
  696. if (!ret)
  697. return TRACE_TYPE_PARTIAL_LINE;
  698. /* Overrun */
  699. if (tracer_flags.val & TRACE_GRAPH_PRINT_OVERRUN) {
  700. ret = trace_seq_printf(s, " (Overruns: %lu)\n",
  701. trace->overrun);
  702. if (!ret)
  703. return TRACE_TYPE_PARTIAL_LINE;
  704. }
  705. ret = print_graph_irq(iter, trace->func, TRACE_GRAPH_RET, cpu, pid);
  706. if (ret == TRACE_TYPE_PARTIAL_LINE)
  707. return TRACE_TYPE_PARTIAL_LINE;
  708. return TRACE_TYPE_HANDLED;
  709. }
  710. static enum print_line_t
  711. print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
  712. struct trace_iterator *iter)
  713. {
  714. unsigned long sym_flags = (trace_flags & TRACE_ITER_SYM_MASK);
  715. struct fgraph_data *data = iter->private;
  716. struct trace_event *event;
  717. int depth = 0;
  718. int ret;
  719. int i;
  720. if (data)
  721. depth = per_cpu_ptr(data, iter->cpu)->depth;
  722. if (print_graph_prologue(iter, s, 0, 0))
  723. return TRACE_TYPE_PARTIAL_LINE;
  724. /* No overhead */
  725. ret = print_graph_overhead(-1, s);
  726. if (!ret)
  727. return TRACE_TYPE_PARTIAL_LINE;
  728. /* No time */
  729. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION) {
  730. ret = trace_seq_printf(s, " | ");
  731. if (!ret)
  732. return TRACE_TYPE_PARTIAL_LINE;
  733. }
  734. /* Indentation */
  735. if (depth > 0)
  736. for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++) {
  737. ret = trace_seq_printf(s, " ");
  738. if (!ret)
  739. return TRACE_TYPE_PARTIAL_LINE;
  740. }
  741. /* The comment */
  742. ret = trace_seq_printf(s, "/* ");
  743. if (!ret)
  744. return TRACE_TYPE_PARTIAL_LINE;
  745. switch (iter->ent->type) {
  746. case TRACE_BPRINT:
  747. ret = trace_print_bprintk_msg_only(iter);
  748. if (ret != TRACE_TYPE_HANDLED)
  749. return ret;
  750. break;
  751. case TRACE_PRINT:
  752. ret = trace_print_printk_msg_only(iter);
  753. if (ret != TRACE_TYPE_HANDLED)
  754. return ret;
  755. break;
  756. default:
  757. event = ftrace_find_event(ent->type);
  758. if (!event)
  759. return TRACE_TYPE_UNHANDLED;
  760. ret = event->trace(iter, sym_flags);
  761. if (ret != TRACE_TYPE_HANDLED)
  762. return ret;
  763. }
  764. /* Strip ending newline */
  765. if (s->buffer[s->len - 1] == '\n') {
  766. s->buffer[s->len - 1] = '\0';
  767. s->len--;
  768. }
  769. ret = trace_seq_printf(s, " */\n");
  770. if (!ret)
  771. return TRACE_TYPE_PARTIAL_LINE;
  772. return TRACE_TYPE_HANDLED;
  773. }
  774. enum print_line_t
  775. print_graph_function(struct trace_iterator *iter)
  776. {
  777. struct trace_entry *entry = iter->ent;
  778. struct trace_seq *s = &iter->seq;
  779. switch (entry->type) {
  780. case TRACE_GRAPH_ENT: {
  781. /*
  782. * print_graph_entry() may consume the current event,
  783. * thus @field may become invalid, so we need to save it.
  784. * sizeof(struct ftrace_graph_ent_entry) is very small,
  785. * it can be safely saved at the stack.
  786. */
  787. struct ftrace_graph_ent_entry *field, saved;
  788. trace_assign_type(field, entry);
  789. saved = *field;
  790. return print_graph_entry(&saved, s, iter);
  791. }
  792. case TRACE_GRAPH_RET: {
  793. struct ftrace_graph_ret_entry *field;
  794. trace_assign_type(field, entry);
  795. return print_graph_return(&field->ret, s, entry, iter);
  796. }
  797. default:
  798. return print_graph_comment(s, entry, iter);
  799. }
  800. return TRACE_TYPE_HANDLED;
  801. }
  802. static void print_lat_header(struct seq_file *s)
  803. {
  804. static const char spaces[] = " " /* 16 spaces */
  805. " " /* 4 spaces */
  806. " "; /* 17 spaces */
  807. int size = 0;
  808. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  809. size += 16;
  810. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  811. size += 4;
  812. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  813. size += 17;
  814. seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
  815. seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
  816. seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
  817. seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
  818. seq_printf(s, "#%.*s||| / _-=> lock-depth \n", size, spaces);
  819. seq_printf(s, "#%.*s|||| / \n", size, spaces);
  820. }
  821. static void print_graph_headers(struct seq_file *s)
  822. {
  823. int lat = trace_flags & TRACE_ITER_LATENCY_FMT;
  824. if (lat)
  825. print_lat_header(s);
  826. /* 1st line */
  827. seq_printf(s, "#");
  828. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  829. seq_printf(s, " TIME ");
  830. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  831. seq_printf(s, " CPU");
  832. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  833. seq_printf(s, " TASK/PID ");
  834. if (lat)
  835. seq_printf(s, "|||||");
  836. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  837. seq_printf(s, " DURATION ");
  838. seq_printf(s, " FUNCTION CALLS\n");
  839. /* 2nd line */
  840. seq_printf(s, "#");
  841. if (tracer_flags.val & TRACE_GRAPH_PRINT_ABS_TIME)
  842. seq_printf(s, " | ");
  843. if (tracer_flags.val & TRACE_GRAPH_PRINT_CPU)
  844. seq_printf(s, " | ");
  845. if (tracer_flags.val & TRACE_GRAPH_PRINT_PROC)
  846. seq_printf(s, " | | ");
  847. if (lat)
  848. seq_printf(s, "|||||");
  849. if (tracer_flags.val & TRACE_GRAPH_PRINT_DURATION)
  850. seq_printf(s, " | | ");
  851. seq_printf(s, " | | | |\n");
  852. }
  853. static void graph_trace_open(struct trace_iterator *iter)
  854. {
  855. /* pid and depth on the last trace processed */
  856. struct fgraph_data *data = alloc_percpu(struct fgraph_data);
  857. int cpu;
  858. if (!data)
  859. pr_warning("function graph tracer: not enough memory\n");
  860. else
  861. for_each_possible_cpu(cpu) {
  862. pid_t *pid = &(per_cpu_ptr(data, cpu)->last_pid);
  863. int *depth = &(per_cpu_ptr(data, cpu)->depth);
  864. *pid = -1;
  865. *depth = 0;
  866. }
  867. iter->private = data;
  868. }
  869. static void graph_trace_close(struct trace_iterator *iter)
  870. {
  871. free_percpu(iter->private);
  872. }
  873. static struct tracer graph_trace __read_mostly = {
  874. .name = "function_graph",
  875. .open = graph_trace_open,
  876. .close = graph_trace_close,
  877. .wait_pipe = poll_wait_pipe,
  878. .init = graph_trace_init,
  879. .reset = graph_trace_reset,
  880. .print_line = print_graph_function,
  881. .print_header = print_graph_headers,
  882. .flags = &tracer_flags,
  883. #ifdef CONFIG_FTRACE_SELFTEST
  884. .selftest = trace_selftest_startup_function_graph,
  885. #endif
  886. };
  887. static __init int init_graph_trace(void)
  888. {
  889. max_bytes_for_cpu = snprintf(NULL, 0, "%d", nr_cpu_ids - 1);
  890. return register_tracer(&graph_trace);
  891. }
  892. device_initcall(init_graph_trace);