trace_functions_graph.c 36 KB

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