4 * Copyright (C) 2004-2008, Soeren Sandmann
5 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
8 #include <linux/kallsyms.h>
9 #include <linux/debugfs.h>
10 #include <linux/hrtimer.h>
11 #include <linux/uaccess.h>
12 #include <linux/ftrace.h>
13 #include <linux/module.h>
14 #include <linux/irq.h>
17 #include <asm/stacktrace.h>
21 static struct trace_array
*sysprof_trace
;
22 static int __read_mostly tracer_enabled
;
25 * 1 msec sample interval by default:
27 static unsigned long sample_period
= 1000000;
28 static const unsigned int sample_max_depth
= 512;
30 static DEFINE_MUTEX(sample_timer_lock
);
32 * Per CPU hrtimers that do the profiling:
34 static DEFINE_PER_CPU(struct hrtimer
, stack_trace_hrtimer
);
37 const void __user
*next_fp
;
38 unsigned long return_address
;
41 static int copy_stack_frame(const void __user
*fp
, struct stack_frame
*frame
)
45 if (!access_ok(VERIFY_READ
, fp
, sizeof(*frame
)))
50 if (__copy_from_user_inatomic(frame
, fp
, sizeof(*frame
)))
57 struct backtrace_info
{
58 struct trace_array_cpu
*data
;
59 struct trace_array
*tr
;
64 backtrace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
69 static void backtrace_warning(void *data
, char *msg
)
74 static int backtrace_stack(void *data
, char *name
)
76 /* Don't bother with IRQ stacks for now */
80 static void backtrace_address(void *data
, unsigned long addr
, int reliable
)
82 struct backtrace_info
*info
= data
;
84 if (info
->pos
< sample_max_depth
&& reliable
) {
85 __trace_special(info
->tr
, info
->data
, 1, addr
, 0);
91 static const struct stacktrace_ops backtrace_ops
= {
92 .warning
= backtrace_warning
,
93 .warning_symbol
= backtrace_warning_symbol
,
94 .stack
= backtrace_stack
,
95 .address
= backtrace_address
,
96 .walk_stack
= print_context_stack
,
100 trace_kernel(struct pt_regs
*regs
, struct trace_array
*tr
,
101 struct trace_array_cpu
*data
)
103 struct backtrace_info info
;
111 __trace_special(info
.tr
, info
.data
, 1, regs
->ip
, 0);
113 stack
= ((char *)regs
+ sizeof(struct pt_regs
));
114 #ifdef CONFIG_FRAME_POINTER
120 dump_trace(NULL
, regs
, (void *)stack
, bp
, &backtrace_ops
, &info
);
125 static void timer_notify(struct pt_regs
*regs
, int cpu
)
127 struct trace_array_cpu
*data
;
128 struct stack_frame frame
;
129 struct trace_array
*tr
;
130 const void __user
*fp
;
138 data
= tr
->data
[cpu
];
139 is_user
= user_mode(regs
);
141 if (!current
|| current
->pid
== 0)
144 if (is_user
&& current
->state
!= TASK_RUNNING
)
147 __trace_special(tr
, data
, 0, 0, current
->pid
);
150 i
= trace_kernel(regs
, tr
, data
);
155 * Trace user stack if we are not a kernel thread
157 if (current
->mm
&& i
< sample_max_depth
) {
158 regs
= (struct pt_regs
*)current
->thread
.sp0
- 1;
160 fp
= (void __user
*)regs
->bp
;
162 __trace_special(tr
, data
, 2, regs
->ip
, 0);
164 while (i
< sample_max_depth
) {
165 frame
.next_fp
= NULL
;
166 frame
.return_address
= 0;
167 if (!copy_stack_frame(fp
, &frame
))
169 if ((unsigned long)fp
< regs
->sp
)
172 __trace_special(tr
, data
, 2, frame
.return_address
,
182 * Special trace entry if we overflow the max depth:
184 if (i
== sample_max_depth
)
185 __trace_special(tr
, data
, -1, -1, -1);
187 __trace_special(tr
, data
, 3, current
->pid
, i
);
190 static enum hrtimer_restart
stack_trace_timer_fn(struct hrtimer
*hrtimer
)
193 timer_notify(get_irq_regs(), smp_processor_id());
195 hrtimer_forward_now(hrtimer
, ns_to_ktime(sample_period
));
197 return HRTIMER_RESTART
;
200 static void start_stack_timer(void *unused
)
202 struct hrtimer
*hrtimer
= &__get_cpu_var(stack_trace_hrtimer
);
204 hrtimer_init(hrtimer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
205 hrtimer
->function
= stack_trace_timer_fn
;
207 hrtimer_start(hrtimer
, ns_to_ktime(sample_period
),
208 HRTIMER_MODE_REL_PINNED
);
211 static void start_stack_timers(void)
213 on_each_cpu(start_stack_timer
, NULL
, 1);
216 static void stop_stack_timer(int cpu
)
218 struct hrtimer
*hrtimer
= &per_cpu(stack_trace_hrtimer
, cpu
);
220 hrtimer_cancel(hrtimer
);
223 static void stop_stack_timers(void)
227 for_each_online_cpu(cpu
)
228 stop_stack_timer(cpu
);
231 static void stop_stack_trace(struct trace_array
*tr
)
233 mutex_lock(&sample_timer_lock
);
236 mutex_unlock(&sample_timer_lock
);
239 static int stack_trace_init(struct trace_array
*tr
)
243 tracing_start_cmdline_record();
245 mutex_lock(&sample_timer_lock
);
246 start_stack_timers();
248 mutex_unlock(&sample_timer_lock
);
252 static void stack_trace_reset(struct trace_array
*tr
)
254 tracing_stop_cmdline_record();
255 stop_stack_trace(tr
);
258 static struct tracer stack_trace __read_mostly
=
261 .init
= stack_trace_init
,
262 .reset
= stack_trace_reset
,
263 #ifdef CONFIG_FTRACE_SELFTEST
264 .selftest
= trace_selftest_startup_sysprof
,
268 __init
static int init_stack_trace(void)
270 return register_tracer(&stack_trace
);
272 device_initcall(init_stack_trace
);
274 #define MAX_LONG_DIGITS 22
277 sysprof_sample_read(struct file
*filp
, char __user
*ubuf
,
278 size_t cnt
, loff_t
*ppos
)
280 char buf
[MAX_LONG_DIGITS
];
283 r
= sprintf(buf
, "%ld\n", nsecs_to_usecs(sample_period
));
285 return simple_read_from_buffer(ubuf
, cnt
, ppos
, buf
, r
);
289 sysprof_sample_write(struct file
*filp
, const char __user
*ubuf
,
290 size_t cnt
, loff_t
*ppos
)
292 char buf
[MAX_LONG_DIGITS
];
295 if (cnt
> MAX_LONG_DIGITS
-1)
296 cnt
= MAX_LONG_DIGITS
-1;
298 if (copy_from_user(&buf
, ubuf
, cnt
))
303 val
= simple_strtoul(buf
, NULL
, 10);
305 * Enforce a minimum sample period of 100 usecs:
310 mutex_lock(&sample_timer_lock
);
312 sample_period
= val
* 1000;
313 start_stack_timers();
314 mutex_unlock(&sample_timer_lock
);
319 static const struct file_operations sysprof_sample_fops
= {
320 .read
= sysprof_sample_read
,
321 .write
= sysprof_sample_write
,
324 void init_tracer_sysprof_debugfs(struct dentry
*d_tracer
)
327 trace_create_file("sysprof_sample_period", 0644,
328 d_tracer
, NULL
, &sysprof_sample_fops
);