trace_functions_graph.c 31 KB

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