trace_functions_graph.c 34 KB

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