blob: 4a9c49c08ec987516e360ca2c7a5c3893324f97e [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,
99 unsigned long flags,
100 int pc)
101{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400102 struct trace_event_call *call = &event_funcgraph_entry;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200103 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500104 struct trace_buffer *buffer = tr->array_buffer.buffer;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200105 struct ftrace_graph_ent_entry *entry;
106
Steven Rostedte77405a2009-09-02 14:17:06 -0400107 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_ENT,
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200108 sizeof(*entry), flags, pc);
109 if (!event)
110 return 0;
111 entry = ring_buffer_event_data(event);
112 entry->graph_ent = *trace;
Tom Zanussif306cc82013-10-24 08:34:17 -0500113 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500114 trace_buffer_unlock_commit_nostack(buffer, event);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200115
116 return 1;
117}
118
Steven Rostedtb304d042010-09-14 18:58:33 -0400119static inline int ftrace_graph_ignore_irqs(void)
120{
Steven Rostedte4a3f542011-06-14 19:02:29 -0400121 if (!ftrace_graph_skip_irqs || trace_recursion_test(TRACE_IRQ_BIT))
Steven Rostedtb304d042010-09-14 18:58:33 -0400122 return 0;
123
124 return in_irq();
125}
126
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200127int trace_graph_entry(struct ftrace_graph_ent *trace)
128{
129 struct trace_array *tr = graph_array;
130 struct trace_array_cpu *data;
131 unsigned long flags;
132 long disabled;
133 int ret;
134 int cpu;
135 int pc;
136
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500137 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT))
138 return 0;
139
Changbin Du6c772212019-07-30 22:08:50 +0800140 /*
141 * Do not trace a function if it's filtered by set_graph_notrace.
142 * Make the index of ret stack negative to indicate that it should
143 * ignore further functions. But it needs its own ret stack entry
144 * to recover the original index in order to continue tracing after
145 * returning from the function.
146 */
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500147 if (ftrace_graph_notrace_addr(trace->func)) {
148 trace_recursion_set(TRACE_GRAPH_NOTRACE_BIT);
149 /*
150 * Need to return 1 to have the return called
151 * that will clear the NOTRACE bit.
152 */
153 return 1;
154 }
155
Steven Rostedt (Red Hat)345ddcc2016-04-22 18:11:33 -0400156 if (!ftrace_trace_task(tr))
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200157 return 0;
158
Steven Rostedt (Red Hat)1a414422016-12-08 19:28:28 -0500159 if (ftrace_graph_ignore_func(trace))
160 return 0;
161
162 if (ftrace_graph_ignore_irqs())
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200163 return 0;
164
Namhyung Kim29ad23b2013-10-14 17:24:26 +0900165 /*
Joel Fernandes7fa8b712016-06-17 22:44:54 -0700166 * Stop here if tracing_threshold is set. We only write function return
167 * events to the ring buffer.
168 */
169 if (tracing_thresh)
170 return 1;
171
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200172 local_irq_save(flags);
173 cpu = raw_smp_processor_id();
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500174 data = per_cpu_ptr(tr->array_buffer.data, cpu);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200175 disabled = atomic_inc_return(&data->disabled);
176 if (likely(disabled == 1)) {
177 pc = preempt_count();
178 ret = __trace_graph_entry(tr, trace, flags, pc);
179 } else {
180 ret = 0;
181 }
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200182
183 atomic_dec(&data->disabled);
184 local_irq_restore(flags);
185
186 return ret;
187}
188
Jiri Olsa0a772622010-09-23 14:00:52 +0200189static void
190__trace_graph_function(struct trace_array *tr,
191 unsigned long ip, unsigned long flags, int pc)
192{
193 u64 time = trace_clock_local();
194 struct ftrace_graph_ent ent = {
195 .func = ip,
196 .depth = 0,
197 };
198 struct ftrace_graph_ret ret = {
199 .func = ip,
200 .depth = 0,
201 .calltime = time,
202 .rettime = time,
203 };
204
205 __trace_graph_entry(tr, &ent, flags, pc);
206 __trace_graph_return(tr, &ret, flags, pc);
207}
208
209void
210trace_graph_function(struct trace_array *tr,
211 unsigned long ip, unsigned long parent_ip,
212 unsigned long flags, int pc)
213{
Jiri Olsa0a772622010-09-23 14:00:52 +0200214 __trace_graph_function(tr, ip, flags, pc);
215}
216
Jiri Olsa62b915f2010-04-02 19:01:22 +0200217void __trace_graph_return(struct trace_array *tr,
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200218 struct ftrace_graph_ret *trace,
219 unsigned long flags,
220 int pc)
221{
Steven Rostedt (Red Hat)2425bcb2015-05-05 11:45:27 -0400222 struct trace_event_call *call = &event_funcgraph_exit;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200223 struct ring_buffer_event *event;
Steven Rostedt (VMware)13292492019-12-13 13:58:57 -0500224 struct trace_buffer *buffer = tr->array_buffer.buffer;
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200225 struct ftrace_graph_ret_entry *entry;
226
Steven Rostedte77405a2009-09-02 14:17:06 -0400227 event = trace_buffer_lock_reserve(buffer, TRACE_GRAPH_RET,
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200228 sizeof(*entry), flags, pc);
229 if (!event)
230 return;
231 entry = ring_buffer_event_data(event);
232 entry->ret = *trace;
Tom Zanussif306cc82013-10-24 08:34:17 -0500233 if (!call_filter_check_discard(call, entry, buffer, event))
Steven Rostedt (Red Hat)52ffabe32016-11-23 20:28:38 -0500234 trace_buffer_unlock_commit_nostack(buffer, event);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200235}
236
237void trace_graph_return(struct ftrace_graph_ret *trace)
238{
239 struct trace_array *tr = graph_array;
240 struct trace_array_cpu *data;
241 unsigned long flags;
242 long disabled;
243 int cpu;
244 int pc;
245
Steven Rostedt (VMware)5cf99a0f2018-11-29 08:50:27 -0500246 ftrace_graph_addr_finish(trace);
247
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500248 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
249 trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
250 return;
251 }
252
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200253 local_irq_save(flags);
254 cpu = raw_smp_processor_id();
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500255 data = per_cpu_ptr(tr->array_buffer.data, cpu);
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200256 disabled = atomic_inc_return(&data->disabled);
257 if (likely(disabled == 1)) {
258 pc = preempt_count();
259 __trace_graph_return(tr, trace, flags, pc);
260 }
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200261 atomic_dec(&data->disabled);
262 local_irq_restore(flags);
263}
264
Frederic Weisbecker24a53652010-01-14 08:23:15 +0100265void set_graph_array(struct trace_array *tr)
266{
267 graph_array = tr;
268
269 /* Make graph_array visible before we start tracing */
270
271 smp_mb();
272}
273
Steven Rostedt (Red Hat)ba1afef2014-07-18 18:07:49 -0400274static void trace_graph_thresh_return(struct ftrace_graph_ret *trace)
Tim Bird0e950172010-02-25 15:36:43 -0800275{
Steven Rostedt (VMware)5cf99a0f2018-11-29 08:50:27 -0500276 ftrace_graph_addr_finish(trace);
277
Steven Rostedt (VMware)9cd29922018-11-14 13:14:58 -0500278 if (trace_recursion_test(TRACE_GRAPH_NOTRACE_BIT)) {
279 trace_recursion_clear(TRACE_GRAPH_NOTRACE_BIT);
280 return;
281 }
282
Tim Bird0e950172010-02-25 15:36:43 -0800283 if (tracing_thresh &&
284 (trace->rettime - trace->calltime < tracing_thresh))
285 return;
286 else
287 trace_graph_return(trace);
288}
289
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500290static struct fgraph_ops funcgraph_thresh_ops = {
291 .entryfunc = &trace_graph_entry,
292 .retfunc = &trace_graph_thresh_return,
293};
294
295static struct fgraph_ops funcgraph_ops = {
296 .entryfunc = &trace_graph_entry,
297 .retfunc = &trace_graph_return,
298};
299
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100300static int graph_trace_init(struct trace_array *tr)
301{
Frederic Weisbecker1a0799a2009-07-29 18:59:58 +0200302 int ret;
303
Frederic Weisbecker24a53652010-01-14 08:23:15 +0100304 set_graph_array(tr);
Tim Bird0e950172010-02-25 15:36:43 -0800305 if (tracing_thresh)
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500306 ret = register_ftrace_graph(&funcgraph_thresh_ops);
Tim Bird0e950172010-02-25 15:36:43 -0800307 else
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500308 ret = register_ftrace_graph(&funcgraph_ops);
Steven Rostedt660c7f92008-11-26 00:16:26 -0500309 if (ret)
310 return ret;
311 tracing_start_cmdline_record();
312
313 return 0;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100314}
315
316static void graph_trace_reset(struct trace_array *tr)
317{
Steven Rostedt660c7f92008-11-26 00:16:26 -0500318 tracing_stop_cmdline_record();
Steven Rostedt (VMware)688f7082018-11-15 14:06:47 -0500319 if (tracing_thresh)
320 unregister_ftrace_graph(&funcgraph_thresh_ops);
321 else
322 unregister_ftrace_graph(&funcgraph_ops);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100323}
324
Steven Rostedt (Red Hat)ba1afef2014-07-18 18:07:49 -0400325static int graph_trace_update_thresh(struct trace_array *tr)
Stanislav Fomichev6508fa72014-07-18 15:17:27 +0400326{
327 graph_trace_reset(tr);
328 return graph_trace_init(tr);
329}
330
Lai Jiangshan0c9e6f62009-07-28 20:26:06 +0800331static int max_bytes_for_cpu;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100332
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500333static void print_graph_cpu(struct trace_seq *s, int cpu)
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100334{
Ingo Molnard51090b2008-11-28 09:55:16 +0100335 /*
336 * Start with a space character - to make it stand out
337 * to the right a bit when trace output is pasted into
338 * email:
339 */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500340 trace_seq_printf(s, " %*d) ", max_bytes_for_cpu, cpu);
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100341}
342
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100343#define TRACE_GRAPH_PROCINFO_LENGTH 14
344
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500345static void print_graph_proc(struct trace_seq *s, pid_t pid)
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100346{
Steven Rostedt4ca530852009-03-16 19:20:15 -0400347 char comm[TASK_COMM_LEN];
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100348 /* sign + log10(MAX_INT) + '\0' */
349 char pid_str[11];
Steven Rostedt4ca530852009-03-16 19:20:15 -0400350 int spaces = 0;
Steven Rostedt4ca530852009-03-16 19:20:15 -0400351 int len;
352 int i;
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100353
Steven Rostedt4ca530852009-03-16 19:20:15 -0400354 trace_find_cmdline(pid, comm);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100355 comm[7] = '\0';
356 sprintf(pid_str, "%d", pid);
357
358 /* 1 stands for the "-" character */
359 len = strlen(comm) + strlen(pid_str) + 1;
360
361 if (len < TRACE_GRAPH_PROCINFO_LENGTH)
362 spaces = TRACE_GRAPH_PROCINFO_LENGTH - len;
363
364 /* First spaces to align center */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500365 for (i = 0; i < spaces / 2; i++)
366 trace_seq_putc(s, ' ');
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100367
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500368 trace_seq_printf(s, "%s-%s", comm, pid_str);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100369
370 /* Last spaces to align center */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500371 for (i = 0; i < spaces - (spaces / 2); i++)
372 trace_seq_putc(s, ' ');
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100373}
374
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100375
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500376static void print_graph_lat_fmt(struct trace_seq *s, struct trace_entry *entry)
Steven Rostedt49ff5902009-09-11 00:30:26 -0400377{
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500378 trace_seq_putc(s, ' ');
379 trace_print_lat_fmt(s, entry);
Changbin Duafbab502019-01-01 23:46:12 +0800380 trace_seq_puts(s, " | ");
Steven Rostedt49ff5902009-09-11 00:30:26 -0400381}
382
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100383/* If the pid changed since the last trace, output this event */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500384static void
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400385verif_pid(struct trace_seq *s, pid_t pid, int cpu, struct fgraph_data *data)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100386{
Ingo Molnard51090b2008-11-28 09:55:16 +0100387 pid_t prev_pid;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800388 pid_t *last_pid;
Steven Rostedt660c7f92008-11-26 00:16:26 -0500389
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400390 if (!data)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500391 return;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100392
Jiri Olsabe1eca32009-11-24 13:57:38 +0100393 last_pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
Steven Rostedt660c7f92008-11-26 00:16:26 -0500394
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800395 if (*last_pid == pid)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500396 return;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800397
398 prev_pid = *last_pid;
399 *last_pid = pid;
400
401 if (prev_pid == -1)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500402 return;
Ingo Molnard51090b2008-11-28 09:55:16 +0100403/*
404 * Context-switch trace line:
405
406 ------------------------------------------
407 | 1) migration/0--1 => sshd-1755
408 ------------------------------------------
409
410 */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500411 trace_seq_puts(s, " ------------------------------------------\n");
412 print_graph_cpu(s, cpu);
413 print_graph_proc(s, prev_pid);
414 trace_seq_puts(s, " => ");
415 print_graph_proc(s, pid);
416 trace_seq_puts(s, "\n ------------------------------------------\n\n");
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100417}
418
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100419static struct ftrace_graph_ret_entry *
420get_return_for_leaf(struct trace_iterator *iter,
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100421 struct ftrace_graph_ent_entry *curr)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100422{
Jiri Olsabe1eca32009-11-24 13:57:38 +0100423 struct fgraph_data *data = iter->private;
424 struct ring_buffer_iter *ring_iter = NULL;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100425 struct ring_buffer_event *event;
426 struct ftrace_graph_ret_entry *next;
427
Jiri Olsabe1eca32009-11-24 13:57:38 +0100428 /*
429 * If the previous output failed to write to the seq buffer,
430 * then we just reuse the data from before.
431 */
432 if (data && data->failed) {
433 curr = &data->ent;
434 next = &data->ret;
435 } else {
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100436
Steven Rostedt6d158a82012-06-27 20:46:14 -0400437 ring_iter = trace_buffer_iter(iter, iter->cpu);
Jiri Olsabe1eca32009-11-24 13:57:38 +0100438
439 /* First peek to compare current entry and the next one */
440 if (ring_iter)
441 event = ring_buffer_iter_peek(ring_iter, NULL);
442 else {
443 /*
444 * We need to consume the current entry to see
445 * the next one.
446 */
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500447 ring_buffer_consume(iter->array_buffer->buffer, iter->cpu,
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400448 NULL, NULL);
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500449 event = ring_buffer_peek(iter->array_buffer->buffer, iter->cpu,
Steven Rostedt66a8cb92010-03-31 13:21:56 -0400450 NULL, NULL);
Jiri Olsabe1eca32009-11-24 13:57:38 +0100451 }
452
453 if (!event)
454 return NULL;
455
456 next = ring_buffer_event_data(event);
457
458 if (data) {
459 /*
460 * Save current and next entries for later reference
461 * if the output fails.
462 */
463 data->ent = *curr;
Shaohua Li575570f2010-07-27 16:06:34 +0800464 /*
465 * If the next event is not a return type, then
466 * we only care about what type it is. Otherwise we can
467 * safely copy the entire event.
468 */
469 if (next->ent.type == TRACE_GRAPH_RET)
470 data->ret = *next;
471 else
472 data->ret.ent.type = next->ent.type;
Jiri Olsabe1eca32009-11-24 13:57:38 +0100473 }
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100474 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100475
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100476 if (next->ent.type != TRACE_GRAPH_RET)
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100477 return NULL;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100478
479 if (curr->ent.pid != next->ent.pid ||
480 curr->graph_ent.func != next->ret.func)
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100481 return NULL;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100482
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100483 /* this is a leaf, now advance the iterator */
484 if (ring_iter)
Steven Rostedt (VMware)bc1a72a2020-03-17 17:32:25 -0400485 ring_buffer_iter_advance(ring_iter);
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100486
487 return next;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100488}
489
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500490static void print_graph_abs_time(u64 t, struct trace_seq *s)
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100491{
492 unsigned long usecs_rem;
493
494 usecs_rem = do_div(t, NSEC_PER_SEC);
495 usecs_rem /= 1000;
496
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500497 trace_seq_printf(s, "%5lu.%06lu | ",
498 (unsigned long)t, usecs_rem);
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100499}
500
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500501static void
Changbin Du9acd8de2019-01-01 23:46:10 +0800502print_graph_rel_time(struct trace_iterator *iter, struct trace_seq *s)
503{
504 unsigned long long usecs;
505
Steven Rostedt (VMware)1c5eb442020-01-09 18:53:48 -0500506 usecs = iter->ts - iter->array_buffer->time_start;
Changbin Du9acd8de2019-01-01 23:46:10 +0800507 do_div(usecs, NSEC_PER_USEC);
508
509 trace_seq_printf(s, "%9llu us | ", usecs);
510}
511
512static void
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100513print_graph_irq(struct trace_iterator *iter, unsigned long addr,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200514 enum trace_type type, int cpu, pid_t pid, u32 flags)
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100515{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400516 struct trace_array *tr = iter->tr;
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100517 struct trace_seq *s = &iter->seq;
Daniel Bristot de Oliveira678f8452014-11-06 15:25:09 -0200518 struct trace_entry *ent = iter->ent;
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100519
520 if (addr < (unsigned long)__irqentry_text_start ||
521 addr >= (unsigned long)__irqentry_text_end)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500522 return;
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100523
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400524 if (tr->trace_flags & TRACE_ITER_CONTEXT_INFO) {
Jiri Olsa749230b2011-06-03 16:58:51 +0200525 /* Absolute time */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500526 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
527 print_graph_abs_time(iter->ts, s);
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100528
Changbin Du9acd8de2019-01-01 23:46:10 +0800529 /* Relative time */
530 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
531 print_graph_rel_time(iter, s);
532
Jiri Olsa749230b2011-06-03 16:58:51 +0200533 /* Cpu */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500534 if (flags & TRACE_GRAPH_PRINT_CPU)
535 print_graph_cpu(s, cpu);
Steven Rostedt49ff5902009-09-11 00:30:26 -0400536
Jiri Olsa749230b2011-06-03 16:58:51 +0200537 /* Proc */
538 if (flags & TRACE_GRAPH_PRINT_PROC) {
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500539 print_graph_proc(s, pid);
540 trace_seq_puts(s, " | ");
Jiri Olsa749230b2011-06-03 16:58:51 +0200541 }
Daniel Bristot de Oliveira678f8452014-11-06 15:25:09 -0200542
543 /* Latency format */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400544 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500545 print_graph_lat_fmt(s, ent);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800546 }
547
548 /* No overhead */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400549 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_START);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800550
551 if (type == TRACE_GRAPH_ENT)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500552 trace_seq_puts(s, "==========>");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800553 else
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500554 trace_seq_puts(s, "<==========");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800555
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400556 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_END);
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500557 trace_seq_putc(s, '\n');
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100558}
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100559
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500560void
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400561trace_print_graph_duration(unsigned long long duration, struct trace_seq *s)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100562{
563 unsigned long nsecs_rem = do_div(duration, 1000);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100564 /* log10(ULONG_MAX) + '\0' */
Byungchul Park4526d062014-11-05 16:18:44 +0900565 char usecs_str[21];
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100566 char nsecs_str[5];
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500567 int len;
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100568 int i;
569
Byungchul Park4526d062014-11-05 16:18:44 +0900570 sprintf(usecs_str, "%lu", (unsigned long) duration);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100571
572 /* Print msecs */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500573 trace_seq_printf(s, "%s", usecs_str);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100574
Byungchul Park4526d062014-11-05 16:18:44 +0900575 len = strlen(usecs_str);
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100576
577 /* Print nsecs (we don't want to exceed 7 numbers) */
578 if (len < 7) {
Borislav Petkov14cae9b2010-09-29 10:08:23 +0200579 size_t slen = min_t(size_t, sizeof(nsecs_str), 8UL - len);
580
581 snprintf(nsecs_str, slen, "%03lu", nsecs_rem);
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500582 trace_seq_printf(s, ".%s", nsecs_str);
Steven Rostedt (Red Hat)82c355e2015-07-16 21:58:52 -0400583 len += strlen(nsecs_str) + 1;
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100584 }
585
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500586 trace_seq_puts(s, " us ");
Frederic Weisbecker166d3c72008-12-03 02:32:12 +0100587
588 /* Print remaining spaces to fit the row's width */
Steven Rostedt (Red Hat)82c355e2015-07-16 21:58:52 -0400589 for (i = len; i < 8; i++)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500590 trace_seq_putc(s, ' ');
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400591}
592
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500593static void
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400594print_graph_duration(struct trace_array *tr, unsigned long long duration,
595 struct trace_seq *s, u32 flags)
Steven Rostedt0706f1c2009-03-23 23:12:58 -0400596{
Jiri Olsa749230b2011-06-03 16:58:51 +0200597 if (!(flags & TRACE_GRAPH_PRINT_DURATION) ||
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400598 !(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500599 return;
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200600
601 /* No real adata, just filling the column with spaces */
Steven Rostedt (Red Hat)6fc84ea2013-11-06 14:50:06 -0500602 switch (flags & TRACE_GRAPH_PRINT_FILL_MASK) {
603 case FLAGS_FILL_FULL:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500604 trace_seq_puts(s, " | ");
605 return;
Steven Rostedt (Red Hat)6fc84ea2013-11-06 14:50:06 -0500606 case FLAGS_FILL_START:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500607 trace_seq_puts(s, " ");
608 return;
Steven Rostedt (Red Hat)6fc84ea2013-11-06 14:50:06 -0500609 case FLAGS_FILL_END:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500610 trace_seq_puts(s, " |");
611 return;
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200612 }
613
614 /* Signal a overhead of time execution to the output */
Byungchul Park8e1e1df2014-11-24 09:34:19 +0900615 if (flags & TRACE_GRAPH_PRINT_OVERHEAD)
616 trace_seq_printf(s, "%c ", trace_find_mark(duration));
617 else
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500618 trace_seq_puts(s, " ");
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200619
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500620 trace_print_graph_duration(duration, s);
621 trace_seq_puts(s, "| ");
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100622}
623
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100624/* Case of a leaf function on its call entry */
625static enum print_line_t
626print_graph_entry_leaf(struct trace_iterator *iter,
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100627 struct ftrace_graph_ent_entry *entry,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200628 struct ftrace_graph_ret_entry *ret_entry,
629 struct trace_seq *s, u32 flags)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100630{
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400631 struct fgraph_data *data = iter->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400632 struct trace_array *tr = iter->tr;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100633 struct ftrace_graph_ret *graph_ret;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100634 struct ftrace_graph_ent *call;
635 unsigned long long duration;
Changbin Du1fe42932018-01-31 23:48:49 +0800636 int cpu = iter->cpu;
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100637 int i;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100638
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100639 graph_ret = &ret_entry->ret;
640 call = &entry->graph_ent;
641 duration = graph_ret->rettime - graph_ret->calltime;
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500642
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400643 if (data) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500644 struct fgraph_cpu_data *cpu_data;
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500645
646 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400647
648 /*
649 * Comments display at + 1 to depth. Since
650 * this is a leaf function, keep the comments
651 * equal to this depth.
652 */
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500653 cpu_data->depth = call->depth - 1;
654
655 /* No need to keep this function around for this depth */
Steven Rostedt (Red Hat)794de082016-12-08 20:54:49 -0500656 if (call->depth < FTRACE_RETFUNC_DEPTH &&
657 !WARN_ON_ONCE(call->depth < 0))
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500658 cpu_data->enter_funcs[call->depth] = 0;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400659 }
660
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200661 /* Overhead and duration */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400662 print_graph_duration(tr, duration, s, flags);
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100663
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100664 /* Function */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500665 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
666 trace_seq_putc(s, ' ');
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100667
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500668 trace_seq_printf(s, "%ps();\n", (void *)call->func);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100669
Changbin Du1fe42932018-01-31 23:48:49 +0800670 print_graph_irq(iter, graph_ret->func, TRACE_GRAPH_RET,
671 cpu, iter->ent->pid, flags);
672
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500673 return trace_handle_return(s);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100674}
675
676static enum print_line_t
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400677print_graph_entry_nested(struct trace_iterator *iter,
678 struct ftrace_graph_ent_entry *entry,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200679 struct trace_seq *s, int cpu, u32 flags)
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100680{
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100681 struct ftrace_graph_ent *call = &entry->graph_ent;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400682 struct fgraph_data *data = iter->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400683 struct trace_array *tr = iter->tr;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400684 int i;
685
686 if (data) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500687 struct fgraph_cpu_data *cpu_data;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400688 int cpu = iter->cpu;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400689
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500690 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
691 cpu_data->depth = call->depth;
692
693 /* Save this function pointer to see if the exit matches */
Steven Rostedt (Red Hat)794de082016-12-08 20:54:49 -0500694 if (call->depth < FTRACE_RETFUNC_DEPTH &&
695 !WARN_ON_ONCE(call->depth < 0))
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500696 cpu_data->enter_funcs[call->depth] = call->func;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400697 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100698
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800699 /* No time */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400700 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100701
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100702 /* Function */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500703 for (i = 0; i < call->depth * TRACE_GRAPH_INDENT; i++)
704 trace_seq_putc(s, ' ');
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100705
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500706 trace_seq_printf(s, "%ps() {\n", (void *)call->func);
707
708 if (trace_seq_has_overflowed(s))
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100709 return TRACE_TYPE_PARTIAL_LINE;
710
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100711 /*
712 * we already consumed the current entry to check the next one
713 * and see if this is a leaf.
714 */
715 return TRACE_TYPE_NO_CONSUME;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100716}
717
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500718static void
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400719print_graph_prologue(struct trace_iterator *iter, struct trace_seq *s,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200720 int type, unsigned long addr, u32 flags)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100721{
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400722 struct fgraph_data *data = iter->private;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100723 struct trace_entry *ent = iter->ent;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400724 struct trace_array *tr = iter->tr;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400725 int cpu = iter->cpu;
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100726
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100727 /* Pid */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500728 verif_pid(s, ent->pid, cpu, data);
Steven Rostedt437f24fb2008-11-26 00:16:27 -0500729
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500730 if (type)
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400731 /* Interrupt */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500732 print_graph_irq(iter, addr, type, cpu, ent->pid, flags);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800733
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400734 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500735 return;
Jiri Olsa749230b2011-06-03 16:58:51 +0200736
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800737 /* Absolute time */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500738 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
739 print_graph_abs_time(iter->ts, s);
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800740
Changbin Du9acd8de2019-01-01 23:46:10 +0800741 /* Relative time */
742 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
743 print_graph_rel_time(iter, s);
744
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100745 /* Cpu */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500746 if (flags & TRACE_GRAPH_PRINT_CPU)
747 print_graph_cpu(s, cpu);
Frederic Weisbecker11e84ac2008-12-03 02:30:37 +0100748
749 /* Proc */
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200750 if (flags & TRACE_GRAPH_PRINT_PROC) {
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500751 print_graph_proc(s, ent->pid);
752 trace_seq_puts(s, " | ");
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100753 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100754
Steven Rostedt49ff5902009-09-11 00:30:26 -0400755 /* Latency format */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400756 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500757 print_graph_lat_fmt(s, ent);
Steven Rostedt49ff5902009-09-11 00:30:26 -0400758
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500759 return;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400760}
761
Jiri Olsa2bd162122010-09-07 16:53:44 +0200762/*
763 * Entry check for irq code
764 *
765 * returns 1 if
766 * - we are inside irq code
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300767 * - we just entered irq code
Jiri Olsa2bd162122010-09-07 16:53:44 +0200768 *
769 * retunns 0 if
770 * - funcgraph-interrupts option is set
771 * - we are not inside irq code
772 */
773static int
774check_irq_entry(struct trace_iterator *iter, u32 flags,
775 unsigned long addr, int depth)
776{
777 int cpu = iter->cpu;
Jiri Olsaa9d61172010-09-24 17:41:02 +0200778 int *depth_irq;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200779 struct fgraph_data *data = iter->private;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200780
Jiri Olsaa9d61172010-09-24 17:41:02 +0200781 /*
782 * If we are either displaying irqs, or we got called as
783 * a graph event and private data does not exist,
784 * then we bypass the irq check.
785 */
786 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
787 (!data))
Jiri Olsa2bd162122010-09-07 16:53:44 +0200788 return 0;
789
Jiri Olsaa9d61172010-09-24 17:41:02 +0200790 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
791
Jiri Olsa2bd162122010-09-07 16:53:44 +0200792 /*
793 * We are inside the irq code
794 */
795 if (*depth_irq >= 0)
796 return 1;
797
798 if ((addr < (unsigned long)__irqentry_text_start) ||
799 (addr >= (unsigned long)__irqentry_text_end))
800 return 0;
801
802 /*
803 * We are entering irq code.
804 */
805 *depth_irq = depth;
806 return 1;
807}
808
809/*
810 * Return check for irq code
811 *
812 * returns 1 if
813 * - we are inside irq code
814 * - we just left irq code
815 *
816 * returns 0 if
817 * - funcgraph-interrupts option is set
818 * - we are not inside irq code
819 */
820static int
821check_irq_return(struct trace_iterator *iter, u32 flags, int depth)
822{
823 int cpu = iter->cpu;
Jiri Olsaa9d61172010-09-24 17:41:02 +0200824 int *depth_irq;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200825 struct fgraph_data *data = iter->private;
Jiri Olsa2bd162122010-09-07 16:53:44 +0200826
Jiri Olsaa9d61172010-09-24 17:41:02 +0200827 /*
828 * If we are either displaying irqs, or we got called as
829 * a graph event and private data does not exist,
830 * then we bypass the irq check.
831 */
832 if ((flags & TRACE_GRAPH_PRINT_IRQS) ||
833 (!data))
Jiri Olsa2bd162122010-09-07 16:53:44 +0200834 return 0;
835
Jiri Olsaa9d61172010-09-24 17:41:02 +0200836 depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
837
Jiri Olsa2bd162122010-09-07 16:53:44 +0200838 /*
839 * We are not inside the irq code.
840 */
841 if (*depth_irq == -1)
842 return 0;
843
844 /*
845 * We are inside the irq code, and this is returning entry.
846 * Let's not trace it and clear the entry depth, since
847 * we are out of irq code.
848 *
849 * This condition ensures that we 'leave the irq code' once
850 * we are out of the entry depth. Thus protecting us from
851 * the RETURN entry loss.
852 */
853 if (*depth_irq >= depth) {
854 *depth_irq = -1;
855 return 1;
856 }
857
858 /*
859 * We are inside the irq code, and this is not the entry.
860 */
861 return 1;
862}
863
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400864static enum print_line_t
865print_graph_entry(struct ftrace_graph_ent_entry *field, struct trace_seq *s,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200866 struct trace_iterator *iter, u32 flags)
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400867{
Jiri Olsabe1eca32009-11-24 13:57:38 +0100868 struct fgraph_data *data = iter->private;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400869 struct ftrace_graph_ent *call = &field->graph_ent;
870 struct ftrace_graph_ret_entry *leaf_ret;
Jiri Olsabe1eca32009-11-24 13:57:38 +0100871 static enum print_line_t ret;
872 int cpu = iter->cpu;
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400873
Jiri Olsa2bd162122010-09-07 16:53:44 +0200874 if (check_irq_entry(iter, flags, call->func, call->depth))
875 return TRACE_TYPE_HANDLED;
876
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500877 print_graph_prologue(iter, s, TRACE_GRAPH_ENT, call->func, flags);
Steven Rostedtac5f6c92009-03-19 11:29:23 -0400878
Frederic Weisbeckerb91facc2009-02-06 18:30:44 +0100879 leaf_ret = get_return_for_leaf(iter, field);
880 if (leaf_ret)
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200881 ret = print_graph_entry_leaf(iter, field, leaf_ret, s, flags);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100882 else
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200883 ret = print_graph_entry_nested(iter, field, s, cpu, flags);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100884
Jiri Olsabe1eca32009-11-24 13:57:38 +0100885 if (data) {
886 /*
887 * If we failed to write our output, then we need to make
888 * note of it. Because we already consumed our entry.
889 */
890 if (s->full) {
891 data->failed = 1;
892 data->cpu = cpu;
893 } else
894 data->failed = 0;
895 }
896
897 return ret;
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100898}
899
900static enum print_line_t
901print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200902 struct trace_entry *ent, struct trace_iterator *iter,
903 u32 flags)
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100904{
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100905 unsigned long long duration = trace->rettime - trace->calltime;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400906 struct fgraph_data *data = iter->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400907 struct trace_array *tr = iter->tr;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400908 pid_t pid = ent->pid;
909 int cpu = iter->cpu;
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500910 int func_match = 1;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400911 int i;
912
Jiri Olsa2bd162122010-09-07 16:53:44 +0200913 if (check_irq_return(iter, flags, trace->depth))
914 return TRACE_TYPE_HANDLED;
915
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400916 if (data) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500917 struct fgraph_cpu_data *cpu_data;
918 int cpu = iter->cpu;
919
920 cpu_data = per_cpu_ptr(data->cpu_data, cpu);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400921
922 /*
923 * Comments display at + 1 to depth. This is the
924 * return from a function, we now want the comments
925 * to display at the same level of the bracket.
926 */
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500927 cpu_data->depth = trace->depth - 1;
928
Steven Rostedt (Red Hat)794de082016-12-08 20:54:49 -0500929 if (trace->depth < FTRACE_RETFUNC_DEPTH &&
930 !WARN_ON_ONCE(trace->depth < 0)) {
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500931 if (cpu_data->enter_funcs[trace->depth] != trace->func)
932 func_match = 0;
933 cpu_data->enter_funcs[trace->depth] = 0;
934 }
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400935 }
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100936
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500937 print_graph_prologue(iter, s, 0, 0, flags);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100938
Jiri Olsaffeb80f2011-06-03 16:58:48 +0200939 /* Overhead and duration */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400940 print_graph_duration(tr, duration, s, flags);
Frederic Weisbecker1a056152008-11-28 00:42:46 +0100941
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100942 /* Closing brace */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500943 for (i = 0; i < trace->depth * TRACE_GRAPH_INDENT; i++)
944 trace_seq_putc(s, ' ');
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100945
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500946 /*
947 * If the return function does not have a matching entry,
948 * then the entry was lost. Instead of just printing
949 * the '}' and letting the user guess what function this
Robert Elliott607e3a22014-05-20 17:10:51 -0500950 * belongs to, write out the function name. Always do
951 * that if the funcgraph-tail option is enabled.
Steven Rostedtf1c7f512010-02-26 17:08:16 -0500952 */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500953 if (func_match && !(flags & TRACE_GRAPH_PRINT_TAIL))
954 trace_seq_puts(s, "}\n");
955 else
956 trace_seq_printf(s, "} /* %ps */\n", (void *)trace->func);
Frederic Weisbecker83a8df62008-11-27 01:46:33 +0100957
958 /* Overrun */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500959 if (flags & TRACE_GRAPH_PRINT_OVERRUN)
960 trace_seq_printf(s, " (Overruns: %lu)\n",
961 trace->overrun);
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100962
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500963 print_graph_irq(iter, trace->func, TRACE_GRAPH_RET,
964 cpu, pid, flags);
Frederic Weisbeckerf8b755a2008-12-09 23:55:25 +0100965
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500966 return trace_handle_return(s);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +0100967}
Frederic Weisbeckerfb526072008-11-25 21:07:04 +0100968
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100969static enum print_line_t
Jiri Olsad7a8d9e2010-04-02 19:01:21 +0200970print_graph_comment(struct trace_seq *s, struct trace_entry *ent,
971 struct trace_iterator *iter, u32 flags)
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100972{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400973 struct trace_array *tr = iter->tr;
974 unsigned long sym_flags = (tr->trace_flags & TRACE_ITER_SYM_MASK);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400975 struct fgraph_data *data = iter->private;
Steven Rostedt5087f8d2009-03-19 15:14:46 -0400976 struct trace_event *event;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400977 int depth = 0;
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100978 int ret;
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400979 int i;
980
981 if (data)
Jiri Olsabe1eca32009-11-24 13:57:38 +0100982 depth = per_cpu_ptr(data->cpu_data, iter->cpu)->depth;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800983
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500984 print_graph_prologue(iter, s, 0, 0, flags);
Frederic Weisbeckerd1f9cbd2009-02-18 04:25:25 +0100985
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -0800986 /* No time */
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -0400987 print_graph_duration(tr, 0, s, flags | FLAGS_FILL_FULL);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100988
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100989 /* Indentation */
Steven Rostedt2fbcdb32009-03-19 13:24:42 -0400990 if (depth > 0)
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500991 for (i = 0; i < (depth + 1) * TRACE_GRAPH_INDENT; i++)
992 trace_seq_putc(s, ' ');
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +0100993
994 /* The comment */
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -0500995 trace_seq_puts(s, "/* ");
Frederic Weisbecker769b0442009-03-06 17:21:49 +0100996
Steven Rostedt5087f8d2009-03-19 15:14:46 -0400997 switch (iter->ent->type) {
Namhyung Kim613dccd2016-09-01 11:43:54 +0900998 case TRACE_BPUTS:
999 ret = trace_print_bputs_msg_only(iter);
1000 if (ret != TRACE_TYPE_HANDLED)
1001 return ret;
1002 break;
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001003 case TRACE_BPRINT:
1004 ret = trace_print_bprintk_msg_only(iter);
1005 if (ret != TRACE_TYPE_HANDLED)
1006 return ret;
1007 break;
1008 case TRACE_PRINT:
1009 ret = trace_print_printk_msg_only(iter);
1010 if (ret != TRACE_TYPE_HANDLED)
1011 return ret;
1012 break;
1013 default:
1014 event = ftrace_find_event(ent->type);
1015 if (!event)
1016 return TRACE_TYPE_UNHANDLED;
1017
Steven Rostedta9a57762010-04-22 18:46:14 -04001018 ret = event->funcs->trace(iter, sym_flags, event);
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001019 if (ret != TRACE_TYPE_HANDLED)
1020 return ret;
1021 }
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01001022
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001023 if (trace_seq_has_overflowed(s))
1024 goto out;
1025
Frederic Weisbecker412d0bb2008-12-24 01:43:25 +01001026 /* Strip ending newline */
Steven Rostedt (Red Hat)3a161d92014-06-25 15:54:42 -04001027 if (s->buffer[s->seq.len - 1] == '\n') {
1028 s->buffer[s->seq.len - 1] = '\0';
1029 s->seq.len--;
Frederic Weisbecker412d0bb2008-12-24 01:43:25 +01001030 }
1031
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -05001032 trace_seq_puts(s, " */\n");
Steven Rostedt (Red Hat)5ac483782014-11-14 15:49:41 -05001033 out:
Steven Rostedt (Red Hat)9d9add32014-11-12 14:57:38 -05001034 return trace_handle_return(s);
Frederic Weisbecker1fd8f2a2008-12-03 23:45:11 +01001035}
1036
1037
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001038enum print_line_t
Jiri Olsa321e68b2011-06-03 16:58:47 +02001039print_graph_function_flags(struct trace_iterator *iter, u32 flags)
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001040{
Jiri Olsabe1eca32009-11-24 13:57:38 +01001041 struct ftrace_graph_ent_entry *field;
1042 struct fgraph_data *data = iter->private;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001043 struct trace_entry *entry = iter->ent;
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001044 struct trace_seq *s = &iter->seq;
Jiri Olsabe1eca32009-11-24 13:57:38 +01001045 int cpu = iter->cpu;
1046 int ret;
1047
1048 if (data && per_cpu_ptr(data->cpu_data, cpu)->ignore) {
1049 per_cpu_ptr(data->cpu_data, cpu)->ignore = 0;
1050 return TRACE_TYPE_HANDLED;
1051 }
1052
1053 /*
1054 * If the last output failed, there's a possibility we need
1055 * to print out the missing entry which would never go out.
1056 */
1057 if (data && data->failed) {
1058 field = &data->ent;
1059 iter->cpu = data->cpu;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001060 ret = print_graph_entry(field, s, iter, flags);
Jiri Olsabe1eca32009-11-24 13:57:38 +01001061 if (ret == TRACE_TYPE_HANDLED && iter->cpu != cpu) {
1062 per_cpu_ptr(data->cpu_data, iter->cpu)->ignore = 1;
1063 ret = TRACE_TYPE_NO_CONSUME;
1064 }
1065 iter->cpu = cpu;
1066 return ret;
1067 }
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001068
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001069 switch (entry->type) {
1070 case TRACE_GRAPH_ENT: {
Lai Jiangshan38ceb592009-07-28 20:11:24 +08001071 /*
1072 * print_graph_entry() may consume the current event,
1073 * thus @field may become invalid, so we need to save it.
1074 * sizeof(struct ftrace_graph_ent_entry) is very small,
1075 * it can be safely saved at the stack.
1076 */
Jiri Olsabe1eca32009-11-24 13:57:38 +01001077 struct ftrace_graph_ent_entry saved;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001078 trace_assign_type(field, entry);
Lai Jiangshan38ceb592009-07-28 20:11:24 +08001079 saved = *field;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001080 return print_graph_entry(&saved, s, iter, flags);
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001081 }
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001082 case TRACE_GRAPH_RET: {
1083 struct ftrace_graph_ret_entry *field;
1084 trace_assign_type(field, entry);
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001085 return print_graph_return(&field->ret, s, entry, iter, flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001086 }
Jiri Olsa62b915f2010-04-02 19:01:22 +02001087 case TRACE_STACK:
1088 case TRACE_FN:
1089 /* dont trace stack and functions as comments */
1090 return TRACE_TYPE_UNHANDLED;
1091
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001092 default:
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001093 return print_graph_comment(s, entry, iter, flags);
Frederic Weisbecker287b6e62008-11-26 00:57:25 +01001094 }
Steven Rostedt5087f8d2009-03-19 15:14:46 -04001095
1096 return TRACE_TYPE_HANDLED;
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001097}
1098
Jiri Olsa9106b692010-04-02 19:01:20 +02001099static enum print_line_t
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001100print_graph_function(struct trace_iterator *iter)
1101{
Jiri Olsa321e68b2011-06-03 16:58:47 +02001102 return print_graph_function_flags(iter, tracer_flags.val);
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001103}
1104
1105static enum print_line_t
Steven Rostedta9a57762010-04-22 18:46:14 -04001106print_graph_function_event(struct trace_iterator *iter, int flags,
1107 struct trace_event *event)
Jiri Olsa9106b692010-04-02 19:01:20 +02001108{
1109 return print_graph_function(iter);
1110}
1111
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001112static void print_lat_header(struct seq_file *s, u32 flags)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001113{
1114 static const char spaces[] = " " /* 16 spaces */
1115 " " /* 4 spaces */
1116 " "; /* 17 spaces */
1117 int size = 0;
1118
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001119 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001120 size += 16;
Changbin Du9acd8de2019-01-01 23:46:10 +08001121 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1122 size += 16;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001123 if (flags & TRACE_GRAPH_PRINT_CPU)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001124 size += 4;
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001125 if (flags & TRACE_GRAPH_PRINT_PROC)
Steven Rostedt49ff5902009-09-11 00:30:26 -04001126 size += 17;
1127
1128 seq_printf(s, "#%.*s _-----=> irqs-off \n", size, spaces);
1129 seq_printf(s, "#%.*s / _----=> need-resched \n", size, spaces);
1130 seq_printf(s, "#%.*s| / _---=> hardirq/softirq \n", size, spaces);
1131 seq_printf(s, "#%.*s|| / _--=> preempt-depth \n", size, spaces);
Jiri Olsa199abfa2011-06-03 16:58:50 +02001132 seq_printf(s, "#%.*s||| / \n", size, spaces);
Steven Rostedt49ff5902009-09-11 00:30:26 -04001133}
1134
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001135static void __print_graph_headers_flags(struct trace_array *tr,
1136 struct seq_file *s, u32 flags)
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001137{
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001138 int lat = tr->trace_flags & TRACE_ITER_LATENCY_FMT;
Steven Rostedt49ff5902009-09-11 00:30:26 -04001139
1140 if (lat)
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001141 print_lat_header(s, flags);
Steven Rostedt49ff5902009-09-11 00:30:26 -04001142
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001143 /* 1st line */
Rasmus Villemoes1177e432014-11-08 21:42:12 +01001144 seq_putc(s, '#');
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001145 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001146 seq_puts(s, " TIME ");
Changbin Du9acd8de2019-01-01 23:46:10 +08001147 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1148 seq_puts(s, " REL TIME ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001149 if (flags & TRACE_GRAPH_PRINT_CPU)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001150 seq_puts(s, " CPU");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001151 if (flags & TRACE_GRAPH_PRINT_PROC)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001152 seq_puts(s, " TASK/PID ");
Steven Rostedt49ff5902009-09-11 00:30:26 -04001153 if (lat)
Changbin Duafbab502019-01-01 23:46:12 +08001154 seq_puts(s, "|||| ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001155 if (flags & TRACE_GRAPH_PRINT_DURATION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001156 seq_puts(s, " DURATION ");
1157 seq_puts(s, " FUNCTION CALLS\n");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001158
1159 /* 2nd line */
Rasmus Villemoes1177e432014-11-08 21:42:12 +01001160 seq_putc(s, '#');
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001161 if (flags & TRACE_GRAPH_PRINT_ABS_TIME)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001162 seq_puts(s, " | ");
Changbin Du9acd8de2019-01-01 23:46:10 +08001163 if (flags & TRACE_GRAPH_PRINT_REL_TIME)
1164 seq_puts(s, " | ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001165 if (flags & TRACE_GRAPH_PRINT_CPU)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001166 seq_puts(s, " | ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001167 if (flags & TRACE_GRAPH_PRINT_PROC)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001168 seq_puts(s, " | | ");
Steven Rostedt49ff5902009-09-11 00:30:26 -04001169 if (lat)
Changbin Duafbab502019-01-01 23:46:12 +08001170 seq_puts(s, "|||| ");
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001171 if (flags & TRACE_GRAPH_PRINT_DURATION)
Rasmus Villemoesfa6f0cc2014-11-08 21:42:10 +01001172 seq_puts(s, " | | ");
1173 seq_puts(s, " | | | |\n");
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001174}
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001175
Steven Rostedt (Red Hat)ba1afef2014-07-18 18:07:49 -04001176static void print_graph_headers(struct seq_file *s)
Jiri Olsad7a8d9e2010-04-02 19:01:21 +02001177{
1178 print_graph_headers_flags(s, tracer_flags.val);
1179}
1180
Jiri Olsa0a772622010-09-23 14:00:52 +02001181void print_graph_headers_flags(struct seq_file *s, u32 flags)
1182{
1183 struct trace_iterator *iter = s->private;
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001184 struct trace_array *tr = iter->tr;
Jiri Olsa0a772622010-09-23 14:00:52 +02001185
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001186 if (!(tr->trace_flags & TRACE_ITER_CONTEXT_INFO))
Jiri Olsa749230b2011-06-03 16:58:51 +02001187 return;
1188
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001189 if (tr->trace_flags & TRACE_ITER_LATENCY_FMT) {
Jiri Olsa0a772622010-09-23 14:00:52 +02001190 /* print nothing if the buffers are empty */
1191 if (trace_empty(iter))
1192 return;
1193
1194 print_trace_header(s, iter);
Jiri Olsa321e68b2011-06-03 16:58:47 +02001195 }
Jiri Olsa0a772622010-09-23 14:00:52 +02001196
Steven Rostedt (Red Hat)983f9382015-09-30 09:42:05 -04001197 __print_graph_headers_flags(tr, s, flags);
Jiri Olsa0a772622010-09-23 14:00:52 +02001198}
1199
Jiri Olsa62b915f2010-04-02 19:01:22 +02001200void graph_trace_open(struct trace_iterator *iter)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001201{
Steven Rostedt2fbcdb32009-03-19 13:24:42 -04001202 /* pid and depth on the last trace processed */
Jiri Olsabe1eca32009-11-24 13:57:38 +01001203 struct fgraph_data *data;
Rabin Vincentef99b882015-04-13 22:30:12 +02001204 gfp_t gfpflags;
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001205 int cpu;
1206
Jiri Olsabe1eca32009-11-24 13:57:38 +01001207 iter->private = NULL;
1208
Rabin Vincentef99b882015-04-13 22:30:12 +02001209 /* We can be called in atomic context via ftrace_dump() */
1210 gfpflags = (in_atomic() || irqs_disabled()) ? GFP_ATOMIC : GFP_KERNEL;
1211
1212 data = kzalloc(sizeof(*data), gfpflags);
Steven Rostedt2fbcdb32009-03-19 13:24:42 -04001213 if (!data)
Jiri Olsabe1eca32009-11-24 13:57:38 +01001214 goto out_err;
1215
Rabin Vincentef99b882015-04-13 22:30:12 +02001216 data->cpu_data = alloc_percpu_gfp(struct fgraph_cpu_data, gfpflags);
Jiri Olsabe1eca32009-11-24 13:57:38 +01001217 if (!data->cpu_data)
1218 goto out_err_free;
1219
1220 for_each_possible_cpu(cpu) {
1221 pid_t *pid = &(per_cpu_ptr(data->cpu_data, cpu)->last_pid);
1222 int *depth = &(per_cpu_ptr(data->cpu_data, cpu)->depth);
1223 int *ignore = &(per_cpu_ptr(data->cpu_data, cpu)->ignore);
Jiri Olsa2bd162122010-09-07 16:53:44 +02001224 int *depth_irq = &(per_cpu_ptr(data->cpu_data, cpu)->depth_irq);
1225
Jiri Olsabe1eca32009-11-24 13:57:38 +01001226 *pid = -1;
1227 *depth = 0;
1228 *ignore = 0;
Jiri Olsa2bd162122010-09-07 16:53:44 +02001229 *depth_irq = -1;
Jiri Olsabe1eca32009-11-24 13:57:38 +01001230 }
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001231
Steven Rostedt2fbcdb32009-03-19 13:24:42 -04001232 iter->private = data;
Jiri Olsabe1eca32009-11-24 13:57:38 +01001233
1234 return;
1235
1236 out_err_free:
1237 kfree(data);
1238 out_err:
Joe Perchesa395d6a2016-03-22 14:28:09 -07001239 pr_warn("function graph tracer: not enough memory\n");
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001240}
1241
Jiri Olsa62b915f2010-04-02 19:01:22 +02001242void graph_trace_close(struct trace_iterator *iter)
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001243{
Jiri Olsabe1eca32009-11-24 13:57:38 +01001244 struct fgraph_data *data = iter->private;
1245
1246 if (data) {
1247 free_percpu(data->cpu_data);
1248 kfree(data);
1249 }
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001250}
1251
Steven Rostedt (Red Hat)8c1a49a2014-01-10 11:13:54 -05001252static int
1253func_graph_set_flag(struct trace_array *tr, u32 old_flags, u32 bit, int set)
Steven Rostedtb304d042010-09-14 18:58:33 -04001254{
1255 if (bit == TRACE_GRAPH_PRINT_IRQS)
1256 ftrace_graph_skip_irqs = !set;
1257
Steven Rostedt (Red Hat)55577202015-09-29 19:06:50 -04001258 if (bit == TRACE_GRAPH_SLEEP_TIME)
1259 ftrace_graph_sleep_time_control(set);
1260
1261 if (bit == TRACE_GRAPH_GRAPH_TIME)
1262 ftrace_graph_graph_time_control(set);
1263
Steven Rostedtb304d042010-09-14 18:58:33 -04001264 return 0;
1265}
1266
Steven Rostedta9a57762010-04-22 18:46:14 -04001267static struct trace_event_functions graph_functions = {
1268 .trace = print_graph_function_event,
1269};
1270
Jiri Olsa9106b692010-04-02 19:01:20 +02001271static struct trace_event graph_trace_entry_event = {
1272 .type = TRACE_GRAPH_ENT,
Steven Rostedta9a57762010-04-22 18:46:14 -04001273 .funcs = &graph_functions,
Jiri Olsa9106b692010-04-02 19:01:20 +02001274};
1275
1276static struct trace_event graph_trace_ret_event = {
1277 .type = TRACE_GRAPH_RET,
Steven Rostedta9a57762010-04-22 18:46:14 -04001278 .funcs = &graph_functions
Jiri Olsa9106b692010-04-02 19:01:20 +02001279};
1280
Steven Rostedt (Red Hat)8f768992013-07-18 14:41:51 -04001281static struct tracer graph_trace __tracer_data = {
Steven Rostedtef180122009-03-10 14:10:56 -04001282 .name = "function_graph",
Stanislav Fomichev6508fa72014-07-18 15:17:27 +04001283 .update_thresh = graph_trace_update_thresh,
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001284 .open = graph_trace_open,
Jiri Olsabe1eca32009-11-24 13:57:38 +01001285 .pipe_open = graph_trace_open,
Frederic Weisbecker9005f3e2009-01-22 17:04:53 -08001286 .close = graph_trace_close,
Jiri Olsabe1eca32009-11-24 13:57:38 +01001287 .pipe_close = graph_trace_close,
Steven Rostedtef180122009-03-10 14:10:56 -04001288 .init = graph_trace_init,
1289 .reset = graph_trace_reset,
Frederic Weisbeckerdecbec32008-12-08 01:56:06 +01001290 .print_line = print_graph_function,
1291 .print_header = print_graph_headers,
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001292 .flags = &tracer_flags,
Steven Rostedtb304d042010-09-14 18:58:33 -04001293 .set_flag = func_graph_set_flag,
Frederic Weisbecker7447dce2009-02-07 21:33:57 +01001294#ifdef CONFIG_FTRACE_SELFTEST
1295 .selftest = trace_selftest_startup_function_graph,
1296#endif
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001297};
1298
Steven Rostedt8741db52013-01-16 10:49:37 -05001299
1300static ssize_t
1301graph_depth_write(struct file *filp, const char __user *ubuf, size_t cnt,
1302 loff_t *ppos)
1303{
1304 unsigned long val;
1305 int ret;
1306
1307 ret = kstrtoul_from_user(ubuf, cnt, 10, &val);
1308 if (ret)
1309 return ret;
1310
Steven Rostedt (Red Hat)1a414422016-12-08 19:28:28 -05001311 fgraph_max_depth = val;
Steven Rostedt8741db52013-01-16 10:49:37 -05001312
1313 *ppos += cnt;
1314
1315 return cnt;
1316}
1317
1318static ssize_t
1319graph_depth_read(struct file *filp, char __user *ubuf, size_t cnt,
1320 loff_t *ppos)
1321{
1322 char buf[15]; /* More than enough to hold UINT_MAX + "\n"*/
1323 int n;
1324
Steven Rostedt (Red Hat)1a414422016-12-08 19:28:28 -05001325 n = sprintf(buf, "%d\n", fgraph_max_depth);
Steven Rostedt8741db52013-01-16 10:49:37 -05001326
1327 return simple_read_from_buffer(ubuf, cnt, ppos, buf, n);
1328}
1329
1330static const struct file_operations graph_depth_fops = {
1331 .open = tracing_open_generic,
1332 .write = graph_depth_write,
1333 .read = graph_depth_read,
1334 .llseek = generic_file_llseek,
1335};
1336
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001337static __init int init_graph_tracefs(void)
Steven Rostedt8741db52013-01-16 10:49:37 -05001338{
1339 struct dentry *d_tracer;
1340
1341 d_tracer = tracing_init_dentry();
Steven Rostedt (Red Hat)14a5ae42015-01-20 11:14:16 -05001342 if (IS_ERR(d_tracer))
Steven Rostedt8741db52013-01-16 10:49:37 -05001343 return 0;
1344
1345 trace_create_file("max_graph_depth", 0644, d_tracer,
1346 NULL, &graph_depth_fops);
1347
1348 return 0;
1349}
Steven Rostedt (Red Hat)8434dc92015-01-20 12:13:40 -05001350fs_initcall(init_graph_tracefs);
Steven Rostedt8741db52013-01-16 10:49:37 -05001351
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001352static __init int init_graph_trace(void)
1353{
Alexey Dobriyan9b130ad2017-09-08 16:14:18 -07001354 max_bytes_for_cpu = snprintf(NULL, 0, "%u", nr_cpu_ids - 1);
Lai Jiangshan0c9e6f62009-07-28 20:26:06 +08001355
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001356 if (!register_trace_event(&graph_trace_entry_event)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001357 pr_warn("Warning: could not register graph trace events\n");
Jiri Olsa9106b692010-04-02 19:01:20 +02001358 return 1;
1359 }
1360
Steven Rostedt (Red Hat)9023c932015-05-05 09:39:12 -04001361 if (!register_trace_event(&graph_trace_ret_event)) {
Joe Perchesa395d6a2016-03-22 14:28:09 -07001362 pr_warn("Warning: could not register graph trace events\n");
Jiri Olsa9106b692010-04-02 19:01:20 +02001363 return 1;
1364 }
1365
Frederic Weisbeckerfb526072008-11-25 21:07:04 +01001366 return register_tracer(&graph_trace);
1367}
1368
Steven Rostedt6f415672012-10-05 12:13:07 -04001369core_initcall(init_graph_trace);