blob: a7974a552ca90689f3ff358f96b6eb2622c5748c [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
Ingo Molnar56a08bd2008-05-12 21:20:47 +020036struct stack_frame {
37 const void __user *next_fp;
38 unsigned long return_address;
39};
40
41static int copy_stack_frame(const void __user *fp, struct stack_frame *frame)
42{
Ingo Molnar9f6b4e32008-05-12 21:20:48 +020043 int ret;
44
Ingo Molnar56a08bd2008-05-12 21:20:47 +020045 if (!access_ok(VERIFY_READ, fp, sizeof(*frame)))
46 return 0;
47
Ingo Molnar9f6b4e32008-05-12 21:20:48 +020048 ret = 1;
49 pagefault_disable();
50 if (__copy_from_user_inatomic(frame, fp, sizeof(*frame)))
51 ret = 0;
52 pagefault_enable();
Ingo Molnar56a08bd2008-05-12 21:20:47 +020053
Ingo Molnar9f6b4e32008-05-12 21:20:48 +020054 return ret;
Ingo Molnar56a08bd2008-05-12 21:20:47 +020055}
56
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020057struct backtrace_info {
58 struct trace_array_cpu *data;
59 struct trace_array *tr;
60 int pos;
61};
62
63static void
64backtrace_warning_symbol(void *data, char *msg, unsigned long symbol)
65{
66 /* Ignore warnings */
67}
68
69static void backtrace_warning(void *data, char *msg)
70{
71 /* Ignore warnings */
72}
73
74static int backtrace_stack(void *data, char *name)
75{
76 /* Don't bother with IRQ stacks for now */
77 return -1;
78}
79
80static void backtrace_address(void *data, unsigned long addr, int reliable)
81{
82 struct backtrace_info *info = data;
83
84 if (info->pos < sample_max_depth && reliable) {
85 __trace_special(info->tr, info->data, 1, addr, 0);
86
87 info->pos++;
88 }
89}
90
Tobias Klauser4543ae72009-02-09 23:09:32 +010091static const struct stacktrace_ops backtrace_ops = {
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020092 .warning = backtrace_warning,
93 .warning_symbol = backtrace_warning_symbol,
94 .stack = backtrace_stack,
95 .address = backtrace_address,
Frederic Weisbecker61c19172009-12-17 05:40:33 +010096 .walk_stack = print_context_stack,
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020097};
98
Soeren Sandmanncf3271a2008-05-12 05:28:50 +020099static int
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200100trace_kernel(struct pt_regs *regs, struct trace_array *tr,
101 struct trace_array_cpu *data)
102{
103 struct backtrace_info info;
104 unsigned long bp;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200105 char *stack;
106
107 info.tr = tr;
108 info.data = data;
109 info.pos = 1;
110
111 __trace_special(info.tr, info.data, 1, regs->ip, 0);
112
113 stack = ((char *)regs + sizeof(struct pt_regs));
114#ifdef CONFIG_FRAME_POINTER
115 bp = regs->bp;
116#else
117 bp = 0;
118#endif
119
120 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);
121
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200122 return info.pos;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200123}
124
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200125static void timer_notify(struct pt_regs *regs, int cpu)
126{
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200127 struct trace_array_cpu *data;
128 struct stack_frame frame;
129 struct trace_array *tr;
Ingo Molnar9f6b4e32008-05-12 21:20:48 +0200130 const void __user *fp;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200131 int is_user;
132 int i;
133
134 if (!regs)
135 return;
136
137 tr = sysprof_trace;
138 data = tr->data[cpu];
139 is_user = user_mode(regs);
140
141 if (!current || current->pid == 0)
142 return;
143
144 if (is_user && current->state != TASK_RUNNING)
145 return;
146
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200147 __trace_special(tr, data, 0, 0, current->pid);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200148
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200149 if (!is_user)
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200150 i = trace_kernel(regs, tr, data);
151 else
152 i = 0;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200153
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200154 /*
155 * Trace user stack if we are not a kernel thread
156 */
157 if (current->mm && i < sample_max_depth) {
158 regs = (struct pt_regs *)current->thread.sp0 - 1;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200159
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200160 fp = (void __user *)regs->bp;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200161
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200162 __trace_special(tr, data, 2, regs->ip, 0);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200163
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200164 while (i < sample_max_depth) {
Harvey Harrisona89cc192008-07-25 01:48:39 -0700165 frame.next_fp = NULL;
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200166 frame.return_address = 0;
167 if (!copy_stack_frame(fp, &frame))
168 break;
169 if ((unsigned long)fp < regs->sp)
170 break;
171
172 __trace_special(tr, data, 2, frame.return_address,
173 (unsigned long)fp);
174 fp = frame.next_fp;
175
176 i++;
177 }
178
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200179 }
180
Ingo Molnar9f6b4e32008-05-12 21:20:48 +0200181 /*
182 * Special trace entry if we overflow the max depth:
183 */
Ingo Molnar842af312008-05-12 21:20:47 +0200184 if (i == sample_max_depth)
Thomas Gleixner9caee612008-05-23 23:55:54 +0200185 __trace_special(tr, data, -1, -1, -1);
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200186
187 __trace_special(tr, data, 3, current->pid, i);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200188}
189
Ingo Molnar0075fa82008-05-12 21:20:47 +0200190static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
191{
192 /* trace here */
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200193 timer_notify(get_irq_regs(), smp_processor_id());
Ingo Molnar0075fa82008-05-12 21:20:47 +0200194
195 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
196
197 return HRTIMER_RESTART;
198}
199
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030200static void start_stack_timer(void *unused)
Ingo Molnar0075fa82008-05-12 21:20:47 +0200201{
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030202 struct hrtimer *hrtimer = &__get_cpu_var(stack_trace_hrtimer);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200203
204 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
205 hrtimer->function = stack_trace_timer_fn;
Ingo Molnar0075fa82008-05-12 21:20:47 +0200206
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530207 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
208 HRTIMER_MODE_REL_PINNED);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200209}
210
211static void start_stack_timers(void)
212{
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030213 on_each_cpu(start_stack_timer, NULL, 1);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200214}
215
216static void stop_stack_timer(int cpu)
217{
218 struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
219
220 hrtimer_cancel(hrtimer);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200221}
222
223static void stop_stack_timers(void)
224{
225 int cpu;
226
227 for_each_online_cpu(cpu)
228 stop_stack_timer(cpu);
229}
230
Thomas Gleixnerada6b832008-05-23 23:50:41 +0200231static void stop_stack_trace(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200232{
Ingo Molnard618b3e2008-05-12 21:20:49 +0200233 mutex_lock(&sample_timer_lock);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200234 stop_stack_timers();
Ingo Molnarf06c3812008-05-12 21:20:47 +0200235 tracer_enabled = 0;
Ingo Molnard618b3e2008-05-12 21:20:49 +0200236 mutex_unlock(&sample_timer_lock);
Ingo Molnarf06c3812008-05-12 21:20:47 +0200237}
238
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100239static int stack_trace_init(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200240{
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200241 sysprof_trace = tr;
Ingo Molnarf06c3812008-05-12 21:20:47 +0200242
Frederic Weisbeckerb22f48582009-02-10 15:49:11 +0100243 tracing_start_cmdline_record();
244
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200245 mutex_lock(&sample_timer_lock);
246 start_stack_timers();
247 tracer_enabled = 1;
248 mutex_unlock(&sample_timer_lock);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100249 return 0;
Ingo Molnarf06c3812008-05-12 21:20:47 +0200250}
251
Thomas Gleixnerada6b832008-05-23 23:50:41 +0200252static void stack_trace_reset(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200253{
Frederic Weisbeckerb22f48582009-02-10 15:49:11 +0100254 tracing_stop_cmdline_record();
Steven Rostedtc76f0692008-11-07 22:36:02 -0500255 stop_stack_trace(tr);
Ingo Molnarf06c3812008-05-12 21:20:47 +0200256}
257
Ingo Molnarf06c3812008-05-12 21:20:47 +0200258static struct tracer stack_trace __read_mostly =
259{
260 .name = "sysprof",
261 .init = stack_trace_init,
262 .reset = stack_trace_reset,
Ingo Molnarf06c3812008-05-12 21:20:47 +0200263#ifdef CONFIG_FTRACE_SELFTEST
Ingo Molnara6dd24f2008-05-12 21:20:47 +0200264 .selftest = trace_selftest_startup_sysprof,
Ingo Molnarf06c3812008-05-12 21:20:47 +0200265#endif
266};
267
268__init static int init_stack_trace(void)
269{
270 return register_tracer(&stack_trace);
271}
272device_initcall(init_stack_trace);
Ingo Molnard618b3e2008-05-12 21:20:49 +0200273
274#define MAX_LONG_DIGITS 22
275
276static ssize_t
277sysprof_sample_read(struct file *filp, char __user *ubuf,
278 size_t cnt, loff_t *ppos)
279{
280 char buf[MAX_LONG_DIGITS];
281 int r;
282
283 r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
284
285 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
286}
287
288static ssize_t
289sysprof_sample_write(struct file *filp, const char __user *ubuf,
290 size_t cnt, loff_t *ppos)
291{
292 char buf[MAX_LONG_DIGITS];
293 unsigned long val;
294
295 if (cnt > MAX_LONG_DIGITS-1)
296 cnt = MAX_LONG_DIGITS-1;
297
298 if (copy_from_user(&buf, ubuf, cnt))
299 return -EFAULT;
300
301 buf[cnt] = 0;
302
303 val = simple_strtoul(buf, NULL, 10);
304 /*
305 * Enforce a minimum sample period of 100 usecs:
306 */
307 if (val < 100)
308 val = 100;
309
310 mutex_lock(&sample_timer_lock);
311 stop_stack_timers();
312 sample_period = val * 1000;
313 start_stack_timers();
314 mutex_unlock(&sample_timer_lock);
315
316 return cnt;
317}
318
Steven Rostedt5e2336a2009-03-05 21:44:55 -0500319static const struct file_operations sysprof_sample_fops = {
Ingo Molnard618b3e2008-05-12 21:20:49 +0200320 .read = sysprof_sample_read,
321 .write = sysprof_sample_write,
322};
323
324void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
325{
Ingo Molnard618b3e2008-05-12 21:20:49 +0200326
Frederic Weisbecker5452af62009-03-27 00:25:38 +0100327 trace_create_file("sysprof_sample_period", 0644,
Ingo Molnard618b3e2008-05-12 21:20:49 +0200328 d_tracer, NULL, &sysprof_sample_fops);
Ingo Molnard618b3e2008-05-12 21:20:49 +0200329}