blob: 203204cadf92f3d0e67c3a03da521bfef8880100 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01002/*
3 *
4 * Function graph tracer.
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08005 * Copyright (c) 2008-2009 Frederic Weisbecker <fweisbec@gmail.com>
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01006 * Mostly borrowed from function tracer which
7 * is Copyright (c) Steven Rostedt <srostedt@redhat.com>
8 *
9 */
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010010#include <linux/uaccess.h>
11#include <linux/ftrace.h>
Alexander Potapenkobe7635e2016-03-25 14:22:05 -070012#include <linux/interrupt.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010014#include <linux/fs.h>
15
16#include "trace.h"
Steven Rostedtf0868d12008-12-23 23:24:12 -050017#include "trace_output.h"
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010018
Steven Rostedtb304d042010-09-14 18:58:33 -040019/* When set, irq functions will be ignored */
20static int ftrace_graph_skip_irqs;
21
Jiri Olsabe1eca32009-11-24 13:57:38 +010022struct fgraph_cpu_data {
Steven Rostedt2fbcdb32009-03-19 13:24:42 -040023 pid_t last_pid;
24 int depth;
Jiri Olsa2bd162122010-09-07 16:53:44 +020025 int depth_irq;
Jiri Olsabe1eca32009-11-24 13:57:38 +010026 int ignore;
Steven Rostedtf1c7f512010-02-26 17:08:16 -050027 unsigned long enter_funcs[FTRACE_RETFUNC_DEPTH];
Jiri Olsabe1eca32009-11-24 13:57:38 +010028};
29
30struct fgraph_data {
Namhyung Kim6016ee12010-08-11 12:47:59 +090031 struct fgraph_cpu_data __percpu *cpu_data;
Jiri Olsabe1eca32009-11-24 13:57:38 +010032
33 /* Place to preserve last processed entry. */
34 struct ftrace_graph_ent_entry ent;
35 struct ftrace_graph_ret_entry ret;
36 int failed;
37 int cpu;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -040038};
39
Frederic Weisbecker287b6e62008-11-26 00:57:25 +010040#define TRACE_GRAPH_INDENT 2
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010041
Steven Rostedt (Red Hat)1a414422016-12-08 19:28:28 -050042unsigned int fgraph_max_depth;
Steven Rostedt8741db52013-01-16 10:49:37 -050043
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010044static struct tracer_opt trace_opts[] = {
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080045 /* Display overruns? (for self-debug purpose) */
Frederic Weisbecker1a056152008-11-28 00:42:46 +010046 { TRACER_OPT(funcgraph-overrun, TRACE_GRAPH_PRINT_OVERRUN) },
47 /* Display CPU ? */
48 { TRACER_OPT(funcgraph-cpu, TRACE_GRAPH_PRINT_CPU) },
49 /* Display Overhead ? */
50 { TRACER_OPT(funcgraph-overhead, TRACE_GRAPH_PRINT_OVERHEAD) },
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +010051 /* Display proc name/pid */
52 { TRACER_OPT(funcgraph-proc, TRACE_GRAPH_PRINT_PROC) },
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080053 /* Display duration of execution */
54 { TRACER_OPT(funcgraph-duration, TRACE_GRAPH_PRINT_DURATION) },
55 /* Display absolute time of an entry */
56 { TRACER_OPT(funcgraph-abstime, TRACE_GRAPH_PRINT_ABS_TIME) },
Jiri Olsa2bd162122010-09-07 16:53:44 +020057 /* Display interrupts */
58 { TRACER_OPT(funcgraph-irqs, TRACE_GRAPH_PRINT_IRQS) },
Robert Elliott607e3a22014-05-20 17:10:51 -050059 /* Display function name after trailing } */
60 { TRACER_OPT(funcgraph-tail, TRACE_GRAPH_PRINT_TAIL) },
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -040061 /* Include sleep time (scheduled out) between entry and return */
62 { TRACER_OPT(sleep-time, TRACE_GRAPH_SLEEP_TIME) },
Steven Rostedt (VMware)c8dd0f42018-11-23 13:06:07 -050063
64#ifdef CONFIG_FUNCTION_PROFILER
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -040065 /* Include time within nested functions */
66 { TRACER_OPT(graph-time, TRACE_GRAPH_GRAPH_TIME) },
Steven Rostedt (VMware)c8dd0f42018-11-23 13:06:07 -050067#endif
68
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010069 { } /* Empty entry */
70};
71
72static struct tracer_flags tracer_flags = {
Robert Elliott607e3a22014-05-20 17:10:51 -050073 /* Don't display overruns, proc, or tail by default */
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080074 .val = TRACE_GRAPH_PRINT_CPU | TRACE_GRAPH_PRINT_OVERHEAD |
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -040075 TRACE_GRAPH_PRINT_DURATION | TRACE_GRAPH_PRINT_IRQS |
76 TRACE_GRAPH_SLEEP_TIME | TRACE_GRAPH_GRAPH_TIME,
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010077 .opts = trace_opts
78};
79
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +020080static struct trace_array *graph_array;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -080081
Jiri Olsaffeb80f2011-06-03 16:58:48 +020082/*
83 * DURATION column is being also used to display IRQ signs,
84 * following values are used by print_graph_irq and others
85 * to fill in space into DURATION column.
86 */
87enum {
Steven Rostedt (Red Hat)6fc84ea2013-11-06 14:50:06 -050088 FLAGS_FILL_FULL = 1 << TRACE_GRAPH_PRINT_FILL_SHIFT,
89 FLAGS_FILL_START = 2 << TRACE_GRAPH_PRINT_FILL_SHIFT,
90 FLAGS_FILL_END = 3 << TRACE_GRAPH_PRINT_FILL_SHIFT,
Jiri Olsaffeb80f2011-06-03 16:58:48 +020091};
92
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -050093static void
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -040094print_graph_duration(struct trace_array *tr, unsigned long long duration,
95 struct trace_seq *s, u32 flags);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +010096
Jiri Olsa62b915f2010-04-02 19:01:22 +020097int __trace_graph_entry(struct trace_array *tr,
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +020098 struct ftrace_graph_ent *trace,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +010099 unsigned int trace_ctx)
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200100{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400101 struct trace_event_call *call = &event_funcgraph_entry;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200102 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500103 struct trace_buffer *buffer = tr->array_buffer.buffer;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200104 struct ftrace_graph_ent_entry *entry;
105
Steven Rostedte77405a2009-09-02 14:17:06 -0400106 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100107 sizeof(*entry), trace_ctx);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200108 if (!event)
109 return 0;
110 entry = ring_buffer_event_data(event);
111 entry->graph_ent = *trace;
Tom Zanussif306cc82013-10-24 08:34:17 -0500112 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500113 trace_buffer_unlock_commit_nostack(buffer, event);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200114
115 return 1;
116}
117
Steven Rostedtb304d042010-09-14 18:58:33 -0400118static inline int ftrace_graph_ignore_irqs(void)
119{
Steven Rostedte4a3f542011-06-14 19:02:29 -0400120 if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
Steven Rostedtb304d042010-09-14 18:58:33 -0400121 return 0;
122
Changbin Duaffc6592021-09-30 08:03:42 +0800123 return in_hardirq();
Steven Rostedtb304d042010-09-14 18:58:33 -0400124}
125
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200126int trace_graph_entry(struct ftrace_graph_ent *trace)
127{
128 struct trace_array *tr = graph_array;
129 struct trace_array_cpu *data;
130 unsigned long flags;
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100131 unsigned int trace_ctx;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200132 long disabled;
133 int ret;
134 int cpu;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200135
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500136 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT))
137 return 0;
138
Changbin Du6c772212019-07-30 22:08:50 +0800139 /*
140 * Do not trace a function if it's filtered by set_graph_notrace.
141 * Make the index of ret stack negative to indicate that it should
142 * ignore further functions. But it needs its own ret stack entry
143 * to recover the original index in order to continue tracing after
144 * returning from the function.
145 */
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500146 if (ftrace_graph_notrace_addr(trace->func)) {
147 trace_recursion_set(TRACE_GRAPH_NOTRACE_BIT);
148 /*
149 * Need to return 1 to have the return called
150 * that will clear the NOTRACE bit.
151 */
152 return 1;
153 }
154
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400155 if (!ftrace_trace_task(tr))
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200156 return 0;
157
Steven Rostedt (Red Hat)1a414422016-12-08 19:28:28 -0500158 if (ftrace_graph_ignore_func(trace))
159 return 0;
160
161 if (ftrace_graph_ignore_irqs())
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200162 return 0;
163
Namhyung Kim29ad23b2013-10-14 17:24:26 +0900164 /*
Joel Fernandes7fa8b712016-06-17 22:44:54 -0700165 * Stop here if tracing_threshold is set. We only write function return
166 * events to the ring buffer.
167 */
168 if (tracing_thresh)
169 return 1;
170
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200171 local_irq_save(flags);
172 cpu = raw_smp_processor_id();
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500173 data = per_cpu_ptr(tr->array_buffer.data, cpu);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200174 disabled = atomic_inc_return(&data->disabled);
175 if (likely(disabled == 1)) {
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100176 trace_ctx = tracing_gen_ctx_flags(flags);
177 ret = __trace_graph_entry(tr, trace, trace_ctx);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200178 } else {
179 ret = 0;
180 }
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200181
182 atomic_dec(&data->disabled);
183 local_irq_restore(flags);
184
185 return ret;
186}
187
Jiri Olsa0a772622010-09-23 14:00:52 +0200188static void
189__trace_graph_function(struct trace_array *tr,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100190 unsigned long ip, unsigned int trace_ctx)
Jiri Olsa0a772622010-09-23 14:00:52 +0200191{
192 u64 time = trace_clock_local();
193 struct ftrace_graph_ent ent = {
194 .func = ip,
195 .depth = 0,
196 };
197 struct ftrace_graph_ret ret = {
198 .func = ip,
199 .depth = 0,
200 .calltime = time,
201 .rettime = time,
202 };
203
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100204 __trace_graph_entry(tr, &ent, trace_ctx);
205 __trace_graph_return(tr, &ret, trace_ctx);
Jiri Olsa0a772622010-09-23 14:00:52 +0200206}
207
208void
209trace_graph_function(struct trace_array *tr,
210 unsigned long ip, unsigned long parent_ip,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100211 unsigned int trace_ctx)
Jiri Olsa0a772622010-09-23 14:00:52 +0200212{
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100213 __trace_graph_function(tr, ip, trace_ctx);
Jiri Olsa0a772622010-09-23 14:00:52 +0200214}
215
Jiri Olsa62b915f2010-04-02 19:01:22 +0200216void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200217 struct ftrace_graph_ret *trace,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100218 unsigned int trace_ctx)
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200219{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400220 struct trace_event_call *call = &event_funcgraph_exit;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200221 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500222 struct trace_buffer *buffer = tr->array_buffer.buffer;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200223 struct ftrace_graph_ret_entry *entry;
224
Steven Rostedte77405a2009-09-02 14:17:06 -0400225 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100226 sizeof(*entry), trace_ctx);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200227 if (!event)
228 return;
229 entry = ring_buffer_event_data(event);
230 entry->ret = *trace;
Tom Zanussif306cc82013-10-24 08:34:17 -0500231 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500232 trace_buffer_unlock_commit_nostack(buffer, event);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200233}
234
235void trace_graph_return(struct ftrace_graph_ret *trace)
236{
237 struct trace_array *tr = graph_array;
238 struct trace_array_cpu *data;
239 unsigned long flags;
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100240 unsigned int trace_ctx;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200241 long disabled;
242 int cpu;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200243
Steven Rostedt (VMware)5cf99a0f2018-11-29 08:50:27 -0500244 ftrace_graph_addr_finish(trace);
245
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500246 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
247 trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
248 return;
249 }
250
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200251 local_irq_save(flags);
252 cpu = raw_smp_processor_id();
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500253 data = per_cpu_ptr(tr->array_buffer.data, cpu);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200254 disabled = atomic_inc_return(&data->disabled);
255 if (likely(disabled == 1)) {
Sebastian Andrzej Siewior36590c502021-01-25 20:45:08 +0100256 trace_ctx = tracing_gen_ctx_flags(flags);
257 __trace_graph_return(tr, trace, trace_ctx);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200258 }
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200259 atomic_dec(&data->disabled);
260 local_irq_restore(flags);
261}
262
Frederic Weisbecker24a53652010-01-14 08:23:15 +0100263void set_graph_array(struct trace_array *tr)
264{
265 graph_array = tr;
266
267 /* Make graph_array visible before we start tracing */
268
269 smp_mb();
270}
271
Steven Rostedt (Red Hat)ba1afef2014-07-18 18:07:49 -0400272static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
Tim Bird0e950172010-02-25 15:36:43 -0800273{
Steven Rostedt (VMware)5cf99a0f2018-11-29 08:50:27 -0500274 ftrace_graph_addr_finish(trace);
275
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500276 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
277 trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
278 return;
279 }
280
Tim Bird0e950172010-02-25 15:36:43 -0800281 if (tracing_thresh &&
282 (trace->rettime - trace->calltime < tracing_thresh))
283 return;
284 else
285 trace_graph_return(trace);
286}
287
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500288static struct fgraph_ops funcgraph_thresh_ops = {
289 .entryfunc = &trace_graph_entry,
290 .retfunc = &trace_graph_thresh_return,
291};
292
293static struct fgraph_ops funcgraph_ops = {
294 .entryfunc = &trace_graph_entry,
295 .retfunc = &trace_graph_return,
296};
297
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100298static int graph_trace_init(struct trace_array *tr)
299{
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200300 int ret;
301
Frederic Weisbecker24a53652010-01-14 08:23:15 +0100302 set_graph_array(tr);
Tim Bird0e950172010-02-25 15:36:43 -0800303 if (tracing_thresh)
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500304 ret = register_ftrace_graph(&funcgraph_thresh_ops);
Tim Bird0e950172010-02-25 15:36:43 -0800305 else
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500306 ret = register_ftrace_graph(&funcgraph_ops);
Steven Rostedt660c7f92008-11-26 00:16:26 -0500307 if (ret)
308 return ret;
309 tracing_start_cmdline_record();
310
311 return 0;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100312}
313
314static void graph_trace_reset(struct trace_array *tr)
315{
Steven Rostedt660c7f92008-11-26 00:16:26 -0500316 tracing_stop_cmdline_record();
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500317 if (tracing_thresh)
318 unregister_ftrace_graph(&funcgraph_thresh_ops);
319 else
320 unregister_ftrace_graph(&funcgraph_ops);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100321}
322
Steven Rostedt (Red Hat)ba1afef2014-07-18 18:07:49 -0400323static int graph_trace_update_thresh(struct trace_array *tr)
Stanislav Fomichev6508fa72014-07-18 15:17:27 +0400324{
325 graph_trace_reset(tr);
326 return graph_trace_init(tr);
327}
328
Lai Jiangshan0c9e6f62009-07-28 20:26:06 +0800329static int max_bytes_for_cpu;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100330
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500331static void print_graph_cpu(struct trace_seq *s, int cpu)
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100332{
Ingo Molnard51090b2008-11-28 09:55:16 +0100333 /*
334 * Start with a space character - to make it stand out
335 * to the right a bit when trace output is pasted into
336 * email:
337 */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500338 trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100339}
340
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100341#define TRACE_GRAPH_PROCINFO_LENGTH 14
342
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500343static void print_graph_proc(struct trace_seq *s, pid_t pid)
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100344{
Steven Rostedt4ca530852009-03-16 19:20:15 -0400345 char comm[TASK_COMM_LEN];
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100346 /* sign + log10(MAX_INT) + '\0' */
347 char pid_str[11];
Steven Rostedt4ca530852009-03-16 19:20:15 -0400348 int spaces = 0;
Steven Rostedt4ca530852009-03-16 19:20:15 -0400349 int len;
350 int i;
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100351
Steven Rostedt4ca530852009-03-16 19:20:15 -0400352 trace_find_cmdline(pid, comm);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100353 comm[7] = '\0';
354 sprintf(pid_str, "%d", pid);
355
356 /* 1 stands for the "-" character */
357 len = strlen(comm) + strlen(pid_str) + 1;
358
359 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
360 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
361
362 /* First spaces to align center */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500363 for (i = 0; i < spaces / 2; i++)
364 trace_seq_putc(s, ' ');
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100365
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500366 trace_seq_printf(s, "%s-%s", comm, pid_str);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100367
368 /* Last spaces to align center */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500369 for (i = 0; i < spaces - (spaces / 2); i++)
370 trace_seq_putc(s, ' ');
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100371}
372
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100373
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500374static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
Steven Rostedt49ff5902009-09-11 00:30:26 -0400375{
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500376 trace_seq_putc(s, ' ');
377 trace_print_lat_fmt(s, entry);
Changbin Duafbab502019-01-01 23:46:12 +0800378 trace_seq_puts(s, " | ");
Steven Rostedt49ff5902009-09-11 00:30:26 -0400379}
380
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100381/* If the pid changed since the last trace, output this event */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500382static void
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400383verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100384{
Ingo Molnard51090b2008-11-28 09:55:16 +0100385 pid_t prev_pid;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800386 pid_t *last_pid;
Steven Rostedt660c7f92008-11-26 00:16:26 -0500387
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400388 if (!data)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500389 return;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100390
Jiri Olsabe1eca32009-11-24 13:57:38 +0100391 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
Steven Rostedt660c7f92008-11-26 00:16:26 -0500392
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800393 if (*last_pid == pid)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500394 return;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800395
396 prev_pid = *last_pid;
397 *last_pid = pid;
398
399 if (prev_pid == -1)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500400 return;
Ingo Molnard51090b2008-11-28 09:55:16 +0100401/*
402 * Context-switch trace line:
403
404 ------------------------------------------
405 | 1) migration/0--1 => sshd-1755
406 ------------------------------------------
407
408 */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500409 trace_seq_puts(s, " ------------------------------------------\n");
410 print_graph_cpu(s, cpu);
411 print_graph_proc(s, prev_pid);
412 trace_seq_puts(s, " => ");
413 print_graph_proc(s, pid);
414 trace_seq_puts(s, "\n ------------------------------------------\n\n");
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100415}
416
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100417static struct ftrace_graph_ret_entry *
418get_return_for_leaf(struct trace_iterator *iter,
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100419 struct ftrace_graph_ent_entry *curr)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100420{
Jiri Olsabe1eca32009-11-24 13:57:38 +0100421 struct fgraph_data *data = iter->private;
422 struct ring_buffer_iter *ring_iter = NULL;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100423 struct ring_buffer_event *event;
424 struct ftrace_graph_ret_entry *next;
425
Jiri Olsabe1eca32009-11-24 13:57:38 +0100426 /*
427 * If the previous output failed to write to the seq buffer,
428 * then we just reuse the data from before.
429 */
430 if (data && data->failed) {
431 curr = &data->ent;
432 next = &data->ret;
433 } else {
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100434
Steven Rostedt6d158a82012-06-27 20:46:14 -0400435 ring_iter = trace_buffer_iter(iter, iter->cpu);
Jiri Olsabe1eca32009-11-24 13:57:38 +0100436
437 /* First peek to compare current entry and the next one */
438 if (ring_iter)
439 event = ring_buffer_iter_peek(ring_iter, NULL);
440 else {
441 /*
442 * We need to consume the current entry to see
443 * the next one.
444 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500445 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu,
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400446 NULL, NULL);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500447 event = ring_buffer_peek(iter->array_buffer->buffer, iter->cpu,
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400448 NULL, NULL);
Jiri Olsabe1eca32009-11-24 13:57:38 +0100449 }
450
451 if (!event)
452 return NULL;
453
454 next = ring_buffer_event_data(event);
455
456 if (data) {
457 /*
458 * Save current and next entries for later reference
459 * if the output fails.
460 */
461 data->ent = *curr;
Shaohua Li575570f2010-07-27 16:06:34 +0800462 /*
463 * If the next event is not a return type, then
464 * we only care about what type it is. Otherwise we can
465 * safely copy the entire event.
466 */
467 if (next->ent.type == TRACE_GRAPH_RET)
468 data->ret = *next;
469 else
470 data->ret.ent.type = next->ent.type;
Jiri Olsabe1eca32009-11-24 13:57:38 +0100471 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100472 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100473
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100474 if (next->ent.type != TRACE_GRAPH_RET)
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100475 return NULL;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100476
477 if (curr->ent.pid != next->ent.pid ||
478 curr->graph_ent.func != next->ret.func)
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100479 return NULL;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100480
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100481 /* this is a leaf, now advance the iterator */
482 if (ring_iter)
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -0400483 ring_buffer_iter_advance(ring_iter);
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100484
485 return next;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100486}
487
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500488static void print_graph_abs_time(u64 t, struct trace_seq *s)
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100489{
490 unsigned long usecs_rem;
491
492 usecs_rem = do_div(t, NSEC_PER_SEC);
493 usecs_rem /= 1000;
494
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500495 trace_seq_printf(s, "%5lu.%06lu | ",
496 (unsigned long)t, usecs_rem);
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100497}
498
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500499static void
Changbin Du9acd8de2019-01-01 23:46:10 +0800500print_graph_rel_time(struct trace_iterator *iter, struct trace_seq *s)
501{
502 unsigned long long usecs;
503
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500504 usecs = iter->ts - iter->array_buffer->time_start;
Changbin Du9acd8de2019-01-01 23:46:10 +0800505 do_div(usecs, NSEC_PER_USEC);
506
507 trace_seq_printf(s, "%9llu us | ", usecs);
508}
509
510static void
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100511print_graph_irq(struct trace_iterator *iter, unsigned long addr,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200512 enum trace_type type, int cpu, pid_t pid, u32 flags)
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100513{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400514 struct trace_array *tr = iter->tr;
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100515 struct trace_seq *s = &iter->seq;
Daniel Bristot de Oliveira678f8452014-11-06 15:25:09 -0200516 struct trace_entry *ent = iter->ent;
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100517
518 if (addr < (unsigned long)__irqentry_text_start ||
519 addr >= (unsigned long)__irqentry_text_end)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500520 return;
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100521
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400522 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Jiri Olsa749230b2011-06-03 16:58:51 +0200523 /* Absolute time */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500524 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
525 print_graph_abs_time(iter->ts, s);
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100526
Changbin Du9acd8de2019-01-01 23:46:10 +0800527 /* Relative time */
528 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
529 print_graph_rel_time(iter, s);
530
Jiri Olsa749230b2011-06-03 16:58:51 +0200531 /* Cpu */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500532 if (flags & TRACE_GRAPH_PRINT_CPU)
533 print_graph_cpu(s, cpu);
Steven Rostedt49ff5902009-09-11 00:30:26 -0400534
Jiri Olsa749230b2011-06-03 16:58:51 +0200535 /* Proc */
536 if (flags & TRACE_GRAPH_PRINT_PROC) {
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500537 print_graph_proc(s, pid);
538 trace_seq_puts(s, " | ");
Jiri Olsa749230b2011-06-03 16:58:51 +0200539 }
Daniel Bristot de Oliveira678f8452014-11-06 15:25:09 -0200540
541 /* Latency format */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400542 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500543 print_graph_lat_fmt(s, ent);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800544 }
545
546 /* No overhead */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400547 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800548
549 if (type == TRACE_GRAPH_ENT)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500550 trace_seq_puts(s, "==========>");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800551 else
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500552 trace_seq_puts(s, "<==========");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800553
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400554 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END);
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500555 trace_seq_putc(s, '\n');
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100556}
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100557
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500558void
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400559trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100560{
561 unsigned long nsecs_rem = do_div(duration, 1000);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100562 /* log10(ULONG_MAX) + '\0' */
Byungchul Park4526d062014-11-05 16:18:44 +0900563 char usecs_str[21];
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100564 char nsecs_str[5];
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500565 int len;
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100566 int i;
567
Byungchul Park4526d062014-11-05 16:18:44 +0900568 sprintf(usecs_str, "%lu", (unsigned long) duration);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100569
570 /* Print msecs */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500571 trace_seq_printf(s, "%s", usecs_str);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100572
Byungchul Park4526d062014-11-05 16:18:44 +0900573 len = strlen(usecs_str);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100574
575 /* Print nsecs (we don't want to exceed 7 numbers) */
576 if (len < 7) {
Borislav Petkov14cae9b2010-09-29 10:08:23 +0200577 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
578
579 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500580 trace_seq_printf(s, ".%s", nsecs_str);
Steven Rostedt (Red Hat)82c355e2015-07-16 21:58:52 -0400581 len += strlen(nsecs_str) + 1;
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100582 }
583
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500584 trace_seq_puts(s, " us ");
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100585
586 /* Print remaining spaces to fit the row's width */
Steven Rostedt (Red Hat)82c355e2015-07-16 21:58:52 -0400587 for (i = len; i < 8; i++)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500588 trace_seq_putc(s, ' ');
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400589}
590
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500591static void
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400592print_graph_duration(struct trace_array *tr, unsigned long long duration,
593 struct trace_seq *s, u32 flags)
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400594{
Jiri Olsa749230b2011-06-03 16:58:51 +0200595 if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400596 !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500597 return;
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200598
599 /* No real adata, just filling the column with spaces */
Steven Rostedt (Red Hat)6fc84ea2013-11-06 14:50:06 -0500600 switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
601 case FLAGS_FILL_FULL:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500602 trace_seq_puts(s, " | ");
603 return;
Steven Rostedt (Red Hat)6fc84ea2013-11-06 14:50:06 -0500604 case FLAGS_FILL_START:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500605 trace_seq_puts(s, " ");
606 return;
Steven Rostedt (Red Hat)6fc84ea2013-11-06 14:50:06 -0500607 case FLAGS_FILL_END:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500608 trace_seq_puts(s, " |");
609 return;
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200610 }
611
612 /* Signal a overhead of time execution to the output */
Byungchul Park8e1e1df2014-11-24 09:34:19 +0900613 if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
614 trace_seq_printf(s, "%c ", trace_find_mark(duration));
615 else
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500616 trace_seq_puts(s, " ");
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200617
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500618 trace_print_graph_duration(duration, s);
619 trace_seq_puts(s, "| ");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100620}
621
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100622/* Case of a leaf function on its call entry */
623static enum print_line_t
624print_graph_entry_leaf(struct trace_iterator *iter,
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100625 struct ftrace_graph_ent_entry *entry,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200626 struct ftrace_graph_ret_entry *ret_entry,
627 struct trace_seq *s, u32 flags)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100628{
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400629 struct fgraph_data *data = iter->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400630 struct trace_array *tr = iter->tr;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100631 struct ftrace_graph_ret *graph_ret;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100632 struct ftrace_graph_ent *call;
633 unsigned long long duration;
Changbin Du1fe42932018-01-31 23:48:49 +0800634 int cpu = iter->cpu;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100635 int i;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100636
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100637 graph_ret = &ret_entry->ret;
638 call = &entry->graph_ent;
639 duration = graph_ret->rettime - graph_ret->calltime;
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500640
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400641 if (data) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500642 struct fgraph_cpu_data *cpu_data;
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500643
644 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400645
646 /*
647 * Comments display at + 1 to depth. Since
648 * this is a leaf function, keep the comments
649 * equal to this depth.
650 */
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500651 cpu_data->depth = call->depth - 1;
652
653 /* No need to keep this function around for this depth */
Steven Rostedt (Red Hat)794de082016-12-08 20:54:49 -0500654 if (call->depth < FTRACE_RETFUNC_DEPTH &&
655 !WARN_ON_ONCE(call->depth < 0))
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500656 cpu_data->enter_funcs[call->depth] = 0;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400657 }
658
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200659 /* Overhead and duration */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400660 print_graph_duration(tr, duration, s, flags);
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100661
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100662 /* Function */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500663 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
664 trace_seq_putc(s, ' ');
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100665
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500666 trace_seq_printf(s, "%ps();\n", (void *)call->func);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100667
Changbin Du1fe42932018-01-31 23:48:49 +0800668 print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET,
669 cpu, iter->ent->pid, flags);
670
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500671 return trace_handle_return(s);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100672}
673
674static enum print_line_t
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400675print_graph_entry_nested(struct trace_iterator *iter,
676 struct ftrace_graph_ent_entry *entry,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200677 struct trace_seq *s, int cpu, u32 flags)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100678{
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100679 struct ftrace_graph_ent *call = &entry->graph_ent;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400680 struct fgraph_data *data = iter->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400681 struct trace_array *tr = iter->tr;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400682 int i;
683
684 if (data) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500685 struct fgraph_cpu_data *cpu_data;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400686 int cpu = iter->cpu;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400687
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500688 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
689 cpu_data->depth = call->depth;
690
691 /* Save this function pointer to see if the exit matches */
Steven Rostedt (Red Hat)794de082016-12-08 20:54:49 -0500692 if (call->depth < FTRACE_RETFUNC_DEPTH &&
693 !WARN_ON_ONCE(call->depth < 0))
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500694 cpu_data->enter_funcs[call->depth] = call->func;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400695 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100696
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800697 /* No time */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400698 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100699
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100700 /* Function */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500701 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
702 trace_seq_putc(s, ' ');
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100703
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500704 trace_seq_printf(s, "%ps() {\n", (void *)call->func);
705
706 if (trace_seq_has_overflowed(s))
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100707 return TRACE_TYPE_PARTIAL_LINE;
708
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100709 /*
710 * we already consumed the current entry to check the next one
711 * and see if this is a leaf.
712 */
713 return TRACE_TYPE_NO_CONSUME;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100714}
715
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500716static void
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400717print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200718 int type, unsigned long addr, u32 flags)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100719{
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400720 struct fgraph_data *data = iter->private;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100721 struct trace_entry *ent = iter->ent;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400722 struct trace_array *tr = iter->tr;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400723 int cpu = iter->cpu;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100724
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100725 /* Pid */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500726 verif_pid(s, ent->pid, cpu, data);
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500727
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500728 if (type)
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400729 /* Interrupt */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500730 print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800731
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400732 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500733 return;
Jiri Olsa749230b2011-06-03 16:58:51 +0200734
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800735 /* Absolute time */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500736 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
737 print_graph_abs_time(iter->ts, s);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800738
Changbin Du9acd8de2019-01-01 23:46:10 +0800739 /* Relative time */
740 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
741 print_graph_rel_time(iter, s);
742
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100743 /* Cpu */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500744 if (flags & TRACE_GRAPH_PRINT_CPU)
745 print_graph_cpu(s, cpu);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100746
747 /* Proc */
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200748 if (flags & TRACE_GRAPH_PRINT_PROC) {
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500749 print_graph_proc(s, ent->pid);
750 trace_seq_puts(s, " | ");
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100751 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100752
Steven Rostedt49ff5902009-09-11 00:30:26 -0400753 /* Latency format */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400754 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500755 print_graph_lat_fmt(s, ent);
Steven Rostedt49ff5902009-09-11 00:30:26 -0400756
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500757 return;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400758}
759
Jiri Olsa2bd162122010-09-07 16:53:44 +0200760/*
761 * Entry check for irq code
762 *
763 * returns 1 if
764 * - we are inside irq code
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300765 * - we just entered irq code
Jiri Olsa2bd162122010-09-07 16:53:44 +0200766 *
Ingo Molnarf2cc0202021-03-23 18:49:35 +0100767 * returns 0 if
Jiri Olsa2bd162122010-09-07 16:53:44 +0200768 * - funcgraph-interrupts option is set
769 * - we are not inside irq code
770 */
771static int
772check_irq_entry(struct trace_iterator *iter, u32 flags,
773 unsigned long addr, int depth)
774{
775 int cpu = iter->cpu;
Jiri Olsaa9d61172010-09-24 17:41:02 +0200776 int *depth_irq;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200777 struct fgraph_data *data = iter->private;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200778
Jiri Olsaa9d61172010-09-24 17:41:02 +0200779 /*
780 * If we are either displaying irqs, or we got called as
781 * a graph event and private data does not exist,
782 * then we bypass the irq check.
783 */
784 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
785 (!data))
Jiri Olsa2bd162122010-09-07 16:53:44 +0200786 return 0;
787
Jiri Olsaa9d61172010-09-24 17:41:02 +0200788 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
789
Jiri Olsa2bd162122010-09-07 16:53:44 +0200790 /*
791 * We are inside the irq code
792 */
793 if (*depth_irq >= 0)
794 return 1;
795
796 if ((addr < (unsigned long)__irqentry_text_start) ||
797 (addr >= (unsigned long)__irqentry_text_end))
798 return 0;
799
800 /*
801 * We are entering irq code.
802 */
803 *depth_irq = depth;
804 return 1;
805}
806
807/*
808 * Return check for irq code
809 *
810 * returns 1 if
811 * - we are inside irq code
812 * - we just left irq code
813 *
814 * returns 0 if
815 * - funcgraph-interrupts option is set
816 * - we are not inside irq code
817 */
818static int
819check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
820{
821 int cpu = iter->cpu;
Jiri Olsaa9d61172010-09-24 17:41:02 +0200822 int *depth_irq;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200823 struct fgraph_data *data = iter->private;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200824
Jiri Olsaa9d61172010-09-24 17:41:02 +0200825 /*
826 * If we are either displaying irqs, or we got called as
827 * a graph event and private data does not exist,
828 * then we bypass the irq check.
829 */
830 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
831 (!data))
Jiri Olsa2bd162122010-09-07 16:53:44 +0200832 return 0;
833
Jiri Olsaa9d61172010-09-24 17:41:02 +0200834 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
835
Jiri Olsa2bd162122010-09-07 16:53:44 +0200836 /*
837 * We are not inside the irq code.
838 */
839 if (*depth_irq == -1)
840 return 0;
841
842 /*
843 * We are inside the irq code, and this is returning entry.
844 * Let's not trace it and clear the entry depth, since
845 * we are out of irq code.
846 *
847 * This condition ensures that we 'leave the irq code' once
848 * we are out of the entry depth. Thus protecting us from
849 * the RETURN entry loss.
850 */
851 if (*depth_irq >= depth) {
852 *depth_irq = -1;
853 return 1;
854 }
855
856 /*
857 * We are inside the irq code, and this is not the entry.
858 */
859 return 1;
860}
861
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400862static enum print_line_t
863print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200864 struct trace_iterator *iter, u32 flags)
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400865{
Jiri Olsabe1eca32009-11-24 13:57:38 +0100866 struct fgraph_data *data = iter->private;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400867 struct ftrace_graph_ent *call = &field->graph_ent;
868 struct ftrace_graph_ret_entry *leaf_ret;
Jiri Olsabe1eca32009-11-24 13:57:38 +0100869 static enum print_line_t ret;
870 int cpu = iter->cpu;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400871
Jiri Olsa2bd162122010-09-07 16:53:44 +0200872 if (check_irq_entry(iter, flags, call->func, call->depth))
873 return TRACE_TYPE_HANDLED;
874
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500875 print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400876
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100877 leaf_ret = get_return_for_leaf(iter, field);
878 if (leaf_ret)
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200879 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100880 else
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200881 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100882
Jiri Olsabe1eca32009-11-24 13:57:38 +0100883 if (data) {
884 /*
885 * If we failed to write our output, then we need to make
886 * note of it. Because we already consumed our entry.
887 */
888 if (s->full) {
889 data->failed = 1;
890 data->cpu = cpu;
891 } else
892 data->failed = 0;
893 }
894
895 return ret;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100896}
897
898static enum print_line_t
899print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200900 struct trace_entry *ent, struct trace_iterator *iter,
901 u32 flags)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100902{
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100903 unsigned long long duration = trace->rettime - trace->calltime;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400904 struct fgraph_data *data = iter->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400905 struct trace_array *tr = iter->tr;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400906 pid_t pid = ent->pid;
907 int cpu = iter->cpu;
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500908 int func_match = 1;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400909 int i;
910
Jiri Olsa2bd162122010-09-07 16:53:44 +0200911 if (check_irq_return(iter, flags, trace->depth))
912 return TRACE_TYPE_HANDLED;
913
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400914 if (data) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500915 struct fgraph_cpu_data *cpu_data;
916 int cpu = iter->cpu;
917
918 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400919
920 /*
921 * Comments display at + 1 to depth. This is the
922 * return from a function, we now want the comments
923 * to display at the same level of the bracket.
924 */
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500925 cpu_data->depth = trace->depth - 1;
926
Steven Rostedt (Red Hat)794de082016-12-08 20:54:49 -0500927 if (trace->depth < FTRACE_RETFUNC_DEPTH &&
928 !WARN_ON_ONCE(trace->depth < 0)) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500929 if (cpu_data->enter_funcs[trace->depth] != trace->func)
930 func_match = 0;
931 cpu_data->enter_funcs[trace->depth] = 0;
932 }
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400933 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100934
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500935 print_graph_prologue(iter, s, 0, 0, flags);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100936
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200937 /* Overhead and duration */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400938 print_graph_duration(tr, duration, s, flags);
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100939
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100940 /* Closing brace */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500941 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
942 trace_seq_putc(s, ' ');
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100943
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500944 /*
945 * If the return function does not have a matching entry,
946 * then the entry was lost. Instead of just printing
947 * the '}' and letting the user guess what function this
Robert Elliott607e3a22014-05-20 17:10:51 -0500948 * belongs to, write out the function name. Always do
949 * that if the funcgraph-tail option is enabled.
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500950 */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500951 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
952 trace_seq_puts(s, "}\n");
953 else
954 trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100955
956 /* Overrun */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500957 if (flags & TRACE_GRAPH_PRINT_OVERRUN)
Steven Rostedt (VMware)60602cb2020-10-28 08:19:24 -0400958 trace_seq_printf(s, " (Overruns: %u)\n",
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500959 trace->overrun);
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100960
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500961 print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
962 cpu, pid, flags);
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100963
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500964 return trace_handle_return(s);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100965}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100966
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100967static enum print_line_t
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200968print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
969 struct trace_iterator *iter, u32 flags)
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100970{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400971 struct trace_array *tr = iter->tr;
972 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400973 struct fgraph_data *data = iter->private;
Steven Rostedt5087f8d2009-03-19 15:14:46 -0400974 struct trace_event *event;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400975 int depth = 0;
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100976 int ret;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400977 int i;
978
979 if (data)
Jiri Olsabe1eca32009-11-24 13:57:38 +0100980 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800981
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500982 print_graph_prologue(iter, s, 0, 0, flags);
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100983
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800984 /* No time */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400985 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100986
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100987 /* Indentation */
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400988 if (depth > 0)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500989 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
990 trace_seq_putc(s, ' ');
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100991
992 /* The comment */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500993 trace_seq_puts(s, "/* ");
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100994
Steven Rostedt5087f8d2009-03-19 15:14:46 -0400995 switch (iter->ent->type) {
Namhyung Kim613dccd2016-09-01 11:43:54 +0900996 case TRACE_BPUTS:
997 ret = trace_print_bputs_msg_only(iter);
998 if (ret != TRACE_TYPE_HANDLED)
999 return ret;
1000 break;
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001001 case TRACE_BPRINT:
1002 ret = trace_print_bprintk_msg_only(iter);
1003 if (ret != TRACE_TYPE_HANDLED)
1004 return ret;
1005 break;
1006 case TRACE_PRINT:
1007 ret = trace_print_printk_msg_only(iter);
1008 if (ret != TRACE_TYPE_HANDLED)
1009 return ret;
1010 break;
1011 default:
1012 event = ftrace_find_event(ent->type);
1013 if (!event)
1014 return TRACE_TYPE_UNHANDLED;
1015
Steven Rostedta9a57762010-04-22 18:46:14 -04001016 ret = event->funcs->trace(iter, sym_flags, event);
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001017 if (ret != TRACE_TYPE_HANDLED)
1018 return ret;
1019 }
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01001020
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001021 if (trace_seq_has_overflowed(s))
1022 goto out;
1023
Frederic Weisbecker412d0bb2008-12-24 01:43:25 +01001024 /* Strip ending newline */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001025 if (s->buffer[s->seq.len - 1] == '\n') {
1026 s->buffer[s->seq.len - 1] = '\0';
1027 s->seq.len--;
Frederic Weisbecker412d0bb2008-12-24 01:43:25 +01001028 }
1029
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -05001030 trace_seq_puts(s, " */\n");
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001031 out:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -05001032 return trace_handle_return(s);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01001033}
1034
1035
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001036enum print_line_t
Jiri Olsa321e68b2011-06-03 16:58:47 +02001037print_graph_function_flags(struct trace_iterator *iter, u32 flags)
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001038{
Jiri Olsabe1eca32009-11-24 13:57:38 +01001039 struct ftrace_graph_ent_entry *field;
1040 struct fgraph_data *data = iter->private;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001041 struct trace_entry *entry = iter->ent;
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001042 struct trace_seq *s = &iter->seq;
Jiri Olsabe1eca32009-11-24 13:57:38 +01001043 int cpu = iter->cpu;
1044 int ret;
1045
1046 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1047 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1048 return TRACE_TYPE_HANDLED;
1049 }
1050
1051 /*
1052 * If the last output failed, there's a possibility we need
1053 * to print out the missing entry which would never go out.
1054 */
1055 if (data && data->failed) {
1056 field = &data->ent;
1057 iter->cpu = data->cpu;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001058 ret = print_graph_entry(field, s, iter, flags);
Jiri Olsabe1eca32009-11-24 13:57:38 +01001059 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1060 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1061 ret = TRACE_TYPE_NO_CONSUME;
1062 }
1063 iter->cpu = cpu;
1064 return ret;
1065 }
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001066
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001067 switch (entry->type) {
1068 case TRACE_GRAPH_ENT: {
Lai Jiangshan38ceb592009-07-28 20:11:24 +08001069 /*
1070 * print_graph_entry() may consume the current event,
1071 * thus @field may become invalid, so we need to save it.
1072 * sizeof(struct ftrace_graph_ent_entry) is very small,
1073 * it can be safely saved at the stack.
1074 */
Jiri Olsabe1eca32009-11-24 13:57:38 +01001075 struct ftrace_graph_ent_entry saved;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001076 trace_assign_type(field, entry);
Lai Jiangshan38ceb592009-07-28 20:11:24 +08001077 saved = *field;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001078 return print_graph_entry(&saved, s, iter, flags);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001079 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001080 case TRACE_GRAPH_RET: {
1081 struct ftrace_graph_ret_entry *field;
1082 trace_assign_type(field, entry);
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001083 return print_graph_return(&field->ret, s, entry, iter, flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001084 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02001085 case TRACE_STACK:
1086 case TRACE_FN:
1087 /* dont trace stack and functions as comments */
1088 return TRACE_TYPE_UNHANDLED;
1089
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001090 default:
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001091 return print_graph_comment(s, entry, iter, flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001092 }
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001093
1094 return TRACE_TYPE_HANDLED;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001095}
1096
Jiri Olsa9106b692010-04-02 19:01:20 +02001097static enum print_line_t
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001098print_graph_function(struct trace_iterator *iter)
1099{
Jiri Olsa321e68b2011-06-03 16:58:47 +02001100 return print_graph_function_flags(iter, tracer_flags.val);
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001101}
1102
1103static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001104print_graph_function_event(struct trace_iterator *iter, int flags,
1105 struct trace_event *event)
Jiri Olsa9106b692010-04-02 19:01:20 +02001106{
1107 return print_graph_function(iter);
1108}
1109
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001110static void print_lat_header(struct seq_file *s, u32 flags)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001111{
1112 static const char spaces[] = " " /* 16 spaces */
1113 " " /* 4 spaces */
1114 " "; /* 17 spaces */
1115 int size = 0;
1116
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001117 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001118 size += 16;
Changbin Du9acd8de2019-01-01 23:46:10 +08001119 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1120 size += 16;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001121 if (flags & TRACE_GRAPH_PRINT_CPU)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001122 size += 4;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001123 if (flags & TRACE_GRAPH_PRINT_PROC)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001124 size += 17;
1125
1126 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1127 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1128 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1129 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
Jiri Olsa199abfa2011-06-03 16:58:50 +02001130 seq_printf(s, "#%.*s||| / \n", size, spaces);
Steven Rostedt49ff5902009-09-11 00:30:26 -04001131}
1132
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001133static void __print_graph_headers_flags(struct trace_array *tr,
1134 struct seq_file *s, u32 flags)
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001135{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001136 int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
Steven Rostedt49ff5902009-09-11 00:30:26 -04001137
1138 if (lat)
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001139 print_lat_header(s, flags);
Steven Rostedt49ff5902009-09-11 00:30:26 -04001140
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001141 /* 1st line */
Rasmus Villemoes1177e432014-11-08 21:42:12 +01001142 seq_putc(s, '#');
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001143 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001144 seq_puts(s, " TIME ");
Changbin Du9acd8de2019-01-01 23:46:10 +08001145 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1146 seq_puts(s, " REL TIME ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001147 if (flags & TRACE_GRAPH_PRINT_CPU)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001148 seq_puts(s, " CPU");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001149 if (flags & TRACE_GRAPH_PRINT_PROC)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001150 seq_puts(s, " TASK/PID ");
Steven Rostedt49ff5902009-09-11 00:30:26 -04001151 if (lat)
Changbin Duafbab502019-01-01 23:46:12 +08001152 seq_puts(s, "|||| ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001153 if (flags & TRACE_GRAPH_PRINT_DURATION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001154 seq_puts(s, " DURATION ");
1155 seq_puts(s, " FUNCTION CALLS\n");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001156
1157 /* 2nd line */
Rasmus Villemoes1177e432014-11-08 21:42:12 +01001158 seq_putc(s, '#');
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001159 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001160 seq_puts(s, " | ");
Changbin Du9acd8de2019-01-01 23:46:10 +08001161 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1162 seq_puts(s, " | ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001163 if (flags & TRACE_GRAPH_PRINT_CPU)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001164 seq_puts(s, " | ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001165 if (flags & TRACE_GRAPH_PRINT_PROC)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001166 seq_puts(s, " | | ");
Steven Rostedt49ff5902009-09-11 00:30:26 -04001167 if (lat)
Changbin Duafbab502019-01-01 23:46:12 +08001168 seq_puts(s, "|||| ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001169 if (flags & TRACE_GRAPH_PRINT_DURATION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001170 seq_puts(s, " | | ");
1171 seq_puts(s, " | | | |\n");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001172}
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001173
Steven Rostedt (Red Hat)ba1afef2014-07-18 18:07:49 -04001174static void print_graph_headers(struct seq_file *s)
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001175{
1176 print_graph_headers_flags(s, tracer_flags.val);
1177}
1178
Jiri Olsa0a772622010-09-23 14:00:52 +02001179void print_graph_headers_flags(struct seq_file *s, u32 flags)
1180{
1181 struct trace_iterator *iter = s->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001182 struct trace_array *tr = iter->tr;
Jiri Olsa0a772622010-09-23 14:00:52 +02001183
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001184 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
Jiri Olsa749230b2011-06-03 16:58:51 +02001185 return;
1186
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001187 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
Jiri Olsa0a772622010-09-23 14:00:52 +02001188 /* print nothing if the buffers are empty */
1189 if (trace_empty(iter))
1190 return;
1191
1192 print_trace_header(s, iter);
Jiri Olsa321e68b2011-06-03 16:58:47 +02001193 }
Jiri Olsa0a772622010-09-23 14:00:52 +02001194
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001195 __print_graph_headers_flags(tr, s, flags);
Jiri Olsa0a772622010-09-23 14:00:52 +02001196}
1197
Jiri Olsa62b915f2010-04-02 19:01:22 +02001198void graph_trace_open(struct trace_iterator *iter)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001199{
Steven Rostedt2fbcdb32009-03-19 13:24:42 -04001200 /* pid and depth on the last trace processed */
Jiri Olsabe1eca32009-11-24 13:57:38 +01001201 struct fgraph_data *data;
Rabin Vincentef99b882015-04-13 22:30:12 +02001202 gfp_t gfpflags;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001203 int cpu;
1204
Jiri Olsabe1eca32009-11-24 13:57:38 +01001205 iter->private = NULL;
1206
Rabin Vincentef99b882015-04-13 22:30:12 +02001207 /* We can be called in atomic context via ftrace_dump() */
1208 gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
1209
1210 data = kzalloc(sizeof(*data), gfpflags);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -04001211 if (!data)
Jiri Olsabe1eca32009-11-24 13:57:38 +01001212 goto out_err;
1213
Rabin Vincentef99b882015-04-13 22:30:12 +02001214 data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
Jiri Olsabe1eca32009-11-24 13:57:38 +01001215 if (!data->cpu_data)
1216 goto out_err_free;
1217
1218 for_each_possible_cpu(cpu) {
1219 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1220 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1221 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
Jiri Olsa2bd162122010-09-07 16:53:44 +02001222 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1223
Jiri Olsabe1eca32009-11-24 13:57:38 +01001224 *pid = -1;
1225 *depth = 0;
1226 *ignore = 0;
Jiri Olsa2bd162122010-09-07 16:53:44 +02001227 *depth_irq = -1;
Jiri Olsabe1eca32009-11-24 13:57:38 +01001228 }
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001229
Steven Rostedt2fbcdb32009-03-19 13:24:42 -04001230 iter->private = data;
Jiri Olsabe1eca32009-11-24 13:57:38 +01001231
1232 return;
1233
1234 out_err_free:
1235 kfree(data);
1236 out_err:
Joe Perchesa395d6a2016-03-22 14:28:09 -07001237 pr_warn("function graph tracer: not enough memory\n");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001238}
1239
Jiri Olsa62b915f2010-04-02 19:01:22 +02001240void graph_trace_close(struct trace_iterator *iter)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001241{
Jiri Olsabe1eca32009-11-24 13:57:38 +01001242 struct fgraph_data *data = iter->private;
1243
1244 if (data) {
1245 free_percpu(data->cpu_data);
1246 kfree(data);
1247 }
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001248}
1249
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05001250static int
1251func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Steven Rostedtb304d042010-09-14 18:58:33 -04001252{
1253 if (bit == TRACE_GRAPH_PRINT_IRQS)
1254 ftrace_graph_skip_irqs = !set;
1255
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04001256 if (bit == TRACE_GRAPH_SLEEP_TIME)
1257 ftrace_graph_sleep_time_control(set);
1258
1259 if (bit == TRACE_GRAPH_GRAPH_TIME)
1260 ftrace_graph_graph_time_control(set);
1261
Steven Rostedtb304d042010-09-14 18:58:33 -04001262 return 0;
1263}
1264
Steven Rostedta9a57762010-04-22 18:46:14 -04001265static struct trace_event_functions graph_functions = {
1266 .trace = print_graph_function_event,
1267};
1268
Jiri Olsa9106b692010-04-02 19:01:20 +02001269static struct trace_event graph_trace_entry_event = {
1270 .type = TRACE_GRAPH_ENT,
Steven Rostedta9a57762010-04-22 18:46:14 -04001271 .funcs = &graph_functions,
Jiri Olsa9106b692010-04-02 19:01:20 +02001272};
1273
1274static struct trace_event graph_trace_ret_event = {
1275 .type = TRACE_GRAPH_RET,
Steven Rostedta9a57762010-04-22 18:46:14 -04001276 .funcs = &graph_functions
Jiri Olsa9106b692010-04-02 19:01:20 +02001277};
1278
Steven Rostedt (Red Hat)8f768992013-07-18 14:41:51 -04001279static struct tracer graph_trace __tracer_data = {
Steven Rostedtef180122009-03-10 14:10:56 -04001280 .name = "function_graph",
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04001281 .update_thresh = graph_trace_update_thresh,
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001282 .open = graph_trace_open,
Jiri Olsabe1eca32009-11-24 13:57:38 +01001283 .pipe_open = graph_trace_open,
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001284 .close = graph_trace_close,
Jiri Olsabe1eca32009-11-24 13:57:38 +01001285 .pipe_close = graph_trace_close,
Steven Rostedtef180122009-03-10 14:10:56 -04001286 .init = graph_trace_init,
1287 .reset = graph_trace_reset,
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001288 .print_line = print_graph_function,
1289 .print_header = print_graph_headers,
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001290 .flags = &tracer_flags,
Steven Rostedtb304d042010-09-14 18:58:33 -04001291 .set_flag = func_graph_set_flag,
Frederic Weisbecker7447dce2009-02-07 21:33:57 +01001292#ifdef CONFIG_FTRACE_SELFTEST
1293 .selftest = trace_selftest_startup_function_graph,
1294#endif
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001295};
1296
Steven Rostedt8741db52013-01-16 10:49:37 -05001297
1298static ssize_t
1299graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1300 loff_t *ppos)
1301{
1302 unsigned long val;
1303 int ret;
1304
1305 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1306 if (ret)
1307 return ret;
1308
Steven Rostedt (Red Hat)1a414422016-12-08 19:28:28 -05001309 fgraph_max_depth = val;
Steven Rostedt8741db52013-01-16 10:49:37 -05001310
1311 *ppos += cnt;
1312
1313 return cnt;
1314}
1315
1316static ssize_t
1317graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1318 loff_t *ppos)
1319{
1320 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1321 int n;
1322
Steven Rostedt (Red Hat)1a414422016-12-08 19:28:28 -05001323 n = sprintf(buf, "%d\n", fgraph_max_depth);
Steven Rostedt8741db52013-01-16 10:49:37 -05001324
1325 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1326}
1327
1328static const struct file_operations graph_depth_fops = {
1329 .open = tracing_open_generic,
1330 .write = graph_depth_write,
1331 .read = graph_depth_read,
1332 .llseek = generic_file_llseek,
1333};
1334
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001335static __init int init_graph_tracefs(void)
Steven Rostedt8741db52013-01-16 10:49:37 -05001336{
Wei Yang22c36b12020-07-12 09:10:36 +08001337 int ret;
Steven Rostedt8741db52013-01-16 10:49:37 -05001338
Wei Yang22c36b12020-07-12 09:10:36 +08001339 ret = tracing_init_dentry();
1340 if (ret)
Steven Rostedt8741db52013-01-16 10:49:37 -05001341 return 0;
1342
Steven Rostedt (VMware)21ccc9c2021-08-18 11:24:51 -04001343 trace_create_file("max_graph_depth", TRACE_MODE_WRITE, NULL,
Steven Rostedt8741db52013-01-16 10:49:37 -05001344 NULL, &graph_depth_fops);
1345
1346 return 0;
1347}
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001348fs_initcall(init_graph_tracefs);
Steven Rostedt8741db52013-01-16 10:49:37 -05001349
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001350static __init int init_graph_trace(void)
1351{
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -07001352 max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1);
Lai Jiangshan0c9e6f62009-07-28 20:26:06 +08001353
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001354 if (!register_trace_event(&graph_trace_entry_event)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001355 pr_warn("Warning: could not register graph trace events\n");
Jiri Olsa9106b692010-04-02 19:01:20 +02001356 return 1;
1357 }
1358
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001359 if (!register_trace_event(&graph_trace_ret_event)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001360 pr_warn("Warning: could not register graph trace events\n");
Jiri Olsa9106b692010-04-02 19:01:20 +02001361 return 1;
1362 }
1363
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001364 return register_tracer(&graph_trace);
1365}
1366
Steven Rostedt6f415672012-10-05 12:13:07 -04001367core_initcall(init_graph_trace);