trace_functions_graph.c 28 KB

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