trace_functions_graph.c 35 KB

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