trace_functions_graph.c 35 KB

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