blob: c080956f4d8eb96b3a6e03c4e879598de9303830 [file] [log] [blame]
Ingo Molnarf06c3812008-05-12 21:20:47 +02001/*
2 * trace stack traces
3 *
Ingo Molnar8a9e94c2008-05-12 21:20:54 +02004 * Copyright (C) 2004-2008, Soeren Sandmann
Ingo Molnarf06c3812008-05-12 21:20:47 +02005 * Copyright (C) 2007 Steven Rostedt <srostedt@redhat.com>
6 * Copyright (C) 2008 Ingo Molnar <mingo@redhat.com>
Ingo Molnarf06c3812008-05-12 21:20:47 +02007 */
Ingo Molnar0075fa82008-05-12 21:20:47 +02008#include <linux/kallsyms.h>
9#include <linux/debugfs.h>
10#include <linux/hrtimer.h>
11#include <linux/uaccess.h>
12#include <linux/ftrace.h>
Ingo Molnarf06c3812008-05-12 21:20:47 +020013#include <linux/module.h>
Ingo Molnar56a08bd2008-05-12 21:20:47 +020014#include <linux/irq.h>
Ingo Molnarf06c3812008-05-12 21:20:47 +020015#include <linux/fs.h>
Ingo Molnarf06c3812008-05-12 21:20:47 +020016
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020017#include <asm/stacktrace.h>
18
Ingo Molnarf06c3812008-05-12 21:20:47 +020019#include "trace.h"
20
Ingo Molnar56a08bd2008-05-12 21:20:47 +020021static struct trace_array *sysprof_trace;
Ingo Molnarf06c3812008-05-12 21:20:47 +020022static int __read_mostly tracer_enabled;
23
Ingo Molnar56a08bd2008-05-12 21:20:47 +020024/*
Ingo Molnard618b3e2008-05-12 21:20:49 +020025 * 1 msec sample interval by default:
Ingo Molnar56a08bd2008-05-12 21:20:47 +020026 */
Ingo Molnard618b3e2008-05-12 21:20:49 +020027static unsigned long sample_period = 1000000;
Ingo Molnar842af312008-05-12 21:20:47 +020028static const unsigned int sample_max_depth = 512;
Ingo Molnar0075fa82008-05-12 21:20:47 +020029
Ingo Molnard618b3e2008-05-12 21:20:49 +020030static DEFINE_MUTEX(sample_timer_lock);
Ingo Molnar0075fa82008-05-12 21:20:47 +020031/*
32 * Per CPU hrtimers that do the profiling:
33 */
34static DEFINE_PER_CPU(struct hrtimer, stack_trace_hrtimer);
35
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +020036struct stack_frame_user {
Ingo Molnar56a08bd2008-05-12 21:20:47 +020037 const void __user *next_fp;
38 unsigned long return_address;
39};
40
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +020041static int
42copy_stack_frame(const void __user *fp, struct stack_frame_user *frame)
Ingo Molnar56a08bd2008-05-12 21:20:47 +020043{
Ingo Molnar9f6b4e32008-05-12 21:20:48 +020044 int ret;
45
Ingo Molnar56a08bd2008-05-12 21:20:47 +020046 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
47 return 0;
48
Ingo Molnar9f6b4e32008-05-12 21:20:48 +020049 ret = 1;
50 pagefault_disable();
51 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
52 ret = 0;
53 pagefault_enable();
Ingo Molnar56a08bd2008-05-12 21:20:47 +020054
Ingo Molnar9f6b4e32008-05-12 21:20:48 +020055 return ret;
Ingo Molnar56a08bd2008-05-12 21:20:47 +020056}
57
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020058struct backtrace_info {
59 struct trace_array_cpu *data;
60 struct trace_array *tr;
61 int pos;
62};
63
64static void
65backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
66{
67 /* Ignore warnings */
68}
69
70static void backtrace_warning(void *data, char *msg)
71{
72 /* Ignore warnings */
73}
74
75static int backtrace_stack(void *data, char *name)
76{
77 /* Don't bother with IRQ stacks for now */
78 return -1;
79}
80
81static void backtrace_address(void *data, unsigned long addr, int reliable)
82{
83 struct backtrace_info *info = data;
84
85 if (info->pos < sample_max_depth && reliable) {
86 __trace_special(info->tr, info->data, 1, addr, 0);
87
88 info->pos++;
89 }
90}
91
Tobias Klauser4543ae72009-02-09 23:09:32 +010092static const struct stacktrace_ops backtrace_ops = {
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020093 .warning = backtrace_warning,
94 .warning_symbol = backtrace_warning_symbol,
95 .stack = backtrace_stack,
96 .address = backtrace_address,
Frederic Weisbecker61c19172009-12-17 05:40:33 +010097 .walk_stack = print_context_stack,
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020098};
99
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200100static int
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200101trace_kernel(struct pt_regs *regs, struct trace_array *tr,
102 struct trace_array_cpu *data)
103{
104 struct backtrace_info info;
105 unsigned long bp;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200106 char *stack;
107
108 info.tr = tr;
109 info.data = data;
110 info.pos = 1;
111
112 __trace_special(info.tr, info.data, 1, regs->ip, 0);
113
114 stack = ((char *)regs + sizeof(struct pt_regs));
115#ifdef CONFIG_FRAME_POINTER
116 bp = regs->bp;
117#else
118 bp = 0;
119#endif
120
121 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);
122
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200123 return info.pos;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200124}
125
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200126static void timer_notify(struct pt_regs *regs, int cpu)
127{
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200128 struct trace_array_cpu *data;
Frederic Weisbeckerc9cf4db2010-05-19 21:35:17 +0200129 struct stack_frame_user frame;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200130 struct trace_array *tr;
Ingo Molnar9f6b4e32008-05-12 21:20:48 +0200131 const void __user *fp;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200132 int is_user;
133 int i;
134
135 if (!regs)
136 return;
137
138 tr = sysprof_trace;
139 data = tr->data[cpu];
140 is_user = user_mode(regs);
141
142 if (!current || current->pid == 0)
143 return;
144
145 if (is_user && current->state != TASK_RUNNING)
146 return;
147
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200148 __trace_special(tr, data, 0, 0, current->pid);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200149
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200150 if (!is_user)
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200151 i = trace_kernel(regs, tr, data);
152 else
153 i = 0;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200154
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200155 /*
156 * Trace user stack if we are not a kernel thread
157 */
158 if (current->mm && i < sample_max_depth) {
159 regs = (struct pt_regs *)current->thread.sp0 - 1;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200160
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200161 fp = (void __user *)regs->bp;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200162
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200163 __trace_special(tr, data, 2, regs->ip, 0);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200164
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200165 while (i < sample_max_depth) {
Harvey Harrisona89cc192008-07-25 01:48:39 -0700166 frame.next_fp = NULL;
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200167 frame.return_address = 0;
168 if (!copy_stack_frame(fp, &frame))
169 break;
170 if ((unsigned long)fp < regs->sp)
171 break;
172
173 __trace_special(tr, data, 2, frame.return_address,
174 (unsigned long)fp);
175 fp = frame.next_fp;
176
177 i++;
178 }
179
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200180 }
181
Ingo Molnar9f6b4e32008-05-12 21:20:48 +0200182 /*
183 * Special trace entry if we overflow the max depth:
184 */
Ingo Molnar842af312008-05-12 21:20:47 +0200185 if (i == sample_max_depth)
Thomas Gleixner9caee612008-05-23 23:55:54 +0200186 __trace_special(tr, data, -1, -1, -1);
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200187
188 __trace_special(tr, data, 3, current->pid, i);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200189}
190
Ingo Molnar0075fa82008-05-12 21:20:47 +0200191static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
192{
193 /* trace here */
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200194 timer_notify(get_irq_regs(), smp_processor_id());
Ingo Molnar0075fa82008-05-12 21:20:47 +0200195
196 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
197
198 return HRTIMER_RESTART;
199}
200
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030201static void start_stack_timer(void *unused)
Ingo Molnar0075fa82008-05-12 21:20:47 +0200202{
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030203 struct hrtimer *hrtimer = &__get_cpu_var(stack_trace_hrtimer);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200204
205 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
206 hrtimer->function = stack_trace_timer_fn;
Ingo Molnar0075fa82008-05-12 21:20:47 +0200207
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530208 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
209 HRTIMER_MODE_REL_PINNED);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200210}
211
212static void start_stack_timers(void)
213{
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030214 on_each_cpu(start_stack_timer, NULL, 1);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200215}
216
217static void stop_stack_timer(int cpu)
218{
219 struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
220
221 hrtimer_cancel(hrtimer);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200222}
223
224static void stop_stack_timers(void)
225{
226 int cpu;
227
228 for_each_online_cpu(cpu)
229 stop_stack_timer(cpu);
230}
231
Thomas Gleixnerada6b832008-05-23 23:50:41 +0200232static void stop_stack_trace(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200233{
Ingo Molnard618b3e2008-05-12 21:20:49 +0200234 mutex_lock(&sample_timer_lock);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200235 stop_stack_timers();
Ingo Molnarf06c3812008-05-12 21:20:47 +0200236 tracer_enabled = 0;
Ingo Molnard618b3e2008-05-12 21:20:49 +0200237 mutex_unlock(&sample_timer_lock);
Ingo Molnarf06c3812008-05-12 21:20:47 +0200238}
239
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100240static int stack_trace_init(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200241{
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200242 sysprof_trace = tr;
Ingo Molnarf06c3812008-05-12 21:20:47 +0200243
Frederic Weisbeckerb22f4852009-02-10 15:49:11 +0100244 tracing_start_cmdline_record();
245
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200246 mutex_lock(&sample_timer_lock);
247 start_stack_timers();
248 tracer_enabled = 1;
249 mutex_unlock(&sample_timer_lock);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100250 return 0;
Ingo Molnarf06c3812008-05-12 21:20:47 +0200251}
252
Thomas Gleixnerada6b832008-05-23 23:50:41 +0200253static void stack_trace_reset(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200254{
Frederic Weisbeckerb22f4852009-02-10 15:49:11 +0100255 tracing_stop_cmdline_record();
Steven Rostedtc76f0692008-11-07 22:36:02 -0500256 stop_stack_trace(tr);
Ingo Molnarf06c3812008-05-12 21:20:47 +0200257}
258
Ingo Molnarf06c3812008-05-12 21:20:47 +0200259static struct tracer stack_trace __read_mostly =
260{
261 .name = "sysprof",
262 .init = stack_trace_init,
263 .reset = stack_trace_reset,
Ingo Molnarf06c3812008-05-12 21:20:47 +0200264#ifdef CONFIG_FTRACE_SELFTEST
Ingo Molnara6dd24f2008-05-12 21:20:47 +0200265 .selftest = trace_selftest_startup_sysprof,
Ingo Molnarf06c3812008-05-12 21:20:47 +0200266#endif
267};
268
269__init static int init_stack_trace(void)
270{
271 return register_tracer(&stack_trace);
272}
273device_initcall(init_stack_trace);
Ingo Molnard618b3e2008-05-12 21:20:49 +0200274
275#define MAX_LONG_DIGITS 22
276
277static ssize_t
278sysprof_sample_read(struct file *filp, char __user *ubuf,
279 size_t cnt, loff_t *ppos)
280{
281 char buf[MAX_LONG_DIGITS];
282 int r;
283
284 r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
285
286 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
287}
288
289static ssize_t
290sysprof_sample_write(struct file *filp, const char __user *ubuf,
291 size_t cnt, loff_t *ppos)
292{
293 char buf[MAX_LONG_DIGITS];
294 unsigned long val;
295
296 if (cnt > MAX_LONG_DIGITS-1)
297 cnt = MAX_LONG_DIGITS-1;
298
299 if (copy_from_user(&buf, ubuf, cnt))
300 return -EFAULT;
301
302 buf[cnt] = 0;
303
304 val = simple_strtoul(buf, NULL, 10);
305 /*
306 * Enforce a minimum sample period of 100 usecs:
307 */
308 if (val < 100)
309 val = 100;
310
311 mutex_lock(&sample_timer_lock);
312 stop_stack_timers();
313 sample_period = val * 1000;
314 start_stack_timers();
315 mutex_unlock(&sample_timer_lock);
316
317 return cnt;
318}
319
Steven Rostedt5e2336a02009-03-05 21:44:55 -0500320static const struct file_operations sysprof_sample_fops = {
Ingo Molnard618b3e2008-05-12 21:20:49 +0200321 .read = sysprof_sample_read,
322 .write = sysprof_sample_write,
323};
324
325void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
326{
Ingo Molnard618b3e2008-05-12 21:20:49 +0200327
Frederic Weisbecker5452af62009-03-27 00:25:38 +0100328 trace_create_file("sysprof_sample_period", 0644,
Ingo Molnard618b3e2008-05-12 21:20:49 +0200329 d_tracer, NULL, &sysprof_sample_fops);
Ingo Molnard618b3e2008-05-12 21:20:49 +0200330}