blob: f6693969287d83e716733f0d21c6356f70ec8ff5 [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,
96};
97
Soeren Sandmanncf3271a2008-05-12 05:28:50 +020098static int
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +020099trace_kernel(struct pt_regs *regs, struct trace_array *tr,
100 struct trace_array_cpu *data)
101{
102 struct backtrace_info info;
103 unsigned long bp;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200104 char *stack;
105
106 info.tr = tr;
107 info.data = data;
108 info.pos = 1;
109
110 __trace_special(info.tr, info.data, 1, regs->ip, 0);
111
112 stack = ((char *)regs + sizeof(struct pt_regs));
113#ifdef CONFIG_FRAME_POINTER
114 bp = regs->bp;
115#else
116 bp = 0;
117#endif
118
119 dump_trace(NULL, regs, (void *)stack, bp, &backtrace_ops, &info);
120
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200121 return info.pos;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200122}
123
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200124static void timer_notify(struct pt_regs *regs, int cpu)
125{
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200126 struct trace_array_cpu *data;
127 struct stack_frame frame;
128 struct trace_array *tr;
Ingo Molnar9f6b4e32008-05-12 21:20:48 +0200129 const void __user *fp;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200130 int is_user;
131 int i;
132
133 if (!regs)
134 return;
135
136 tr = sysprof_trace;
137 data = tr->data[cpu];
138 is_user = user_mode(regs);
139
140 if (!current || current->pid == 0)
141 return;
142
143 if (is_user && current->state != TASK_RUNNING)
144 return;
145
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200146 __trace_special(tr, data, 0, 0, current->pid);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200147
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200148 if (!is_user)
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200149 i = trace_kernel(regs, tr, data);
150 else
151 i = 0;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200152
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200153 /*
154 * Trace user stack if we are not a kernel thread
155 */
156 if (current->mm && i < sample_max_depth) {
157 regs = (struct pt_regs *)current->thread.sp0 - 1;
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200158
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200159 fp = (void __user *)regs->bp;
Soeren Sandmann Pedersencd2134b2008-05-12 21:20:54 +0200160
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200161 __trace_special(tr, data, 2, regs->ip, 0);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200162
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200163 while (i < sample_max_depth) {
Harvey Harrisona89cc192008-07-25 01:48:39 -0700164 frame.next_fp = NULL;
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200165 frame.return_address = 0;
166 if (!copy_stack_frame(fp, &frame))
167 break;
168 if ((unsigned long)fp < regs->sp)
169 break;
170
171 __trace_special(tr, data, 2, frame.return_address,
172 (unsigned long)fp);
173 fp = frame.next_fp;
174
175 i++;
176 }
177
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200178 }
179
Ingo Molnar9f6b4e32008-05-12 21:20:48 +0200180 /*
181 * Special trace entry if we overflow the max depth:
182 */
Ingo Molnar842af312008-05-12 21:20:47 +0200183 if (i == sample_max_depth)
Thomas Gleixner9caee612008-05-23 23:55:54 +0200184 __trace_special(tr, data, -1, -1, -1);
Soeren Sandmanncf3271a2008-05-12 05:28:50 +0200185
186 __trace_special(tr, data, 3, current->pid, i);
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200187}
188
Ingo Molnar0075fa82008-05-12 21:20:47 +0200189static enum hrtimer_restart stack_trace_timer_fn(struct hrtimer *hrtimer)
190{
191 /* trace here */
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200192 timer_notify(get_irq_regs(), smp_processor_id());
Ingo Molnar0075fa82008-05-12 21:20:47 +0200193
194 hrtimer_forward_now(hrtimer, ns_to_ktime(sample_period));
195
196 return HRTIMER_RESTART;
197}
198
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030199static void start_stack_timer(void *unused)
Ingo Molnar0075fa82008-05-12 21:20:47 +0200200{
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030201 struct hrtimer *hrtimer = &__get_cpu_var(stack_trace_hrtimer);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200202
203 hrtimer_init(hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
204 hrtimer->function = stack_trace_timer_fn;
Ingo Molnar0075fa82008-05-12 21:20:47 +0200205
Arun R Bharadwaj5c333862009-04-16 12:14:37 +0530206 hrtimer_start(hrtimer, ns_to_ktime(sample_period),
207 HRTIMER_MODE_REL_PINNED);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200208}
209
210static void start_stack_timers(void)
211{
Rusty Russell9e01c1b2009-01-01 10:12:22 +1030212 on_each_cpu(start_stack_timer, NULL, 1);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200213}
214
215static void stop_stack_timer(int cpu)
216{
217 struct hrtimer *hrtimer = &per_cpu(stack_trace_hrtimer, cpu);
218
219 hrtimer_cancel(hrtimer);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200220}
221
222static void stop_stack_timers(void)
223{
224 int cpu;
225
226 for_each_online_cpu(cpu)
227 stop_stack_timer(cpu);
228}
229
Thomas Gleixnerada6b832008-05-23 23:50:41 +0200230static void stop_stack_trace(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200231{
Ingo Molnard618b3e2008-05-12 21:20:49 +0200232 mutex_lock(&sample_timer_lock);
Ingo Molnar0075fa82008-05-12 21:20:47 +0200233 stop_stack_timers();
Ingo Molnarf06c3812008-05-12 21:20:47 +0200234 tracer_enabled = 0;
Ingo Molnard618b3e2008-05-12 21:20:49 +0200235 mutex_unlock(&sample_timer_lock);
Ingo Molnarf06c3812008-05-12 21:20:47 +0200236}
237
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100238static int stack_trace_init(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200239{
Ingo Molnar56a08bd2008-05-12 21:20:47 +0200240 sysprof_trace = tr;
Ingo Molnarf06c3812008-05-12 21:20:47 +0200241
Frederic Weisbeckerb22f48582009-02-10 15:49:11 +0100242 tracing_start_cmdline_record();
243
Arnaldo Carvalho de Melob6f11df2009-02-05 18:02:00 -0200244 mutex_lock(&sample_timer_lock);
245 start_stack_timers();
246 tracer_enabled = 1;
247 mutex_unlock(&sample_timer_lock);
Frederic Weisbecker1c800252008-11-16 05:57:26 +0100248 return 0;
Ingo Molnarf06c3812008-05-12 21:20:47 +0200249}
250
Thomas Gleixnerada6b832008-05-23 23:50:41 +0200251static void stack_trace_reset(struct trace_array *tr)
Ingo Molnarf06c3812008-05-12 21:20:47 +0200252{
Frederic Weisbeckerb22f48582009-02-10 15:49:11 +0100253 tracing_stop_cmdline_record();
Steven Rostedtc76f0692008-11-07 22:36:02 -0500254 stop_stack_trace(tr);
Ingo Molnarf06c3812008-05-12 21:20:47 +0200255}
256
Ingo Molnarf06c3812008-05-12 21:20:47 +0200257static struct tracer stack_trace __read_mostly =
258{
259 .name = "sysprof",
260 .init = stack_trace_init,
261 .reset = stack_trace_reset,
Ingo Molnarf06c3812008-05-12 21:20:47 +0200262#ifdef CONFIG_FTRACE_SELFTEST
Ingo Molnara6dd24f2008-05-12 21:20:47 +0200263 .selftest = trace_selftest_startup_sysprof,
Ingo Molnarf06c3812008-05-12 21:20:47 +0200264#endif
265};
266
267__init static int init_stack_trace(void)
268{
269 return register_tracer(&stack_trace);
270}
271device_initcall(init_stack_trace);
Ingo Molnard618b3e2008-05-12 21:20:49 +0200272
273#define MAX_LONG_DIGITS 22
274
275static ssize_t
276sysprof_sample_read(struct file *filp, char __user *ubuf,
277 size_t cnt, loff_t *ppos)
278{
279 char buf[MAX_LONG_DIGITS];
280 int r;
281
282 r = sprintf(buf, "%ld\n", nsecs_to_usecs(sample_period));
283
284 return simple_read_from_buffer(ubuf, cnt, ppos, buf, r);
285}
286
287static ssize_t
288sysprof_sample_write(struct file *filp, const char __user *ubuf,
289 size_t cnt, loff_t *ppos)
290{
291 char buf[MAX_LONG_DIGITS];
292 unsigned long val;
293
294 if (cnt > MAX_LONG_DIGITS-1)
295 cnt = MAX_LONG_DIGITS-1;
296
297 if (copy_from_user(&buf, ubuf, cnt))
298 return -EFAULT;
299
300 buf[cnt] = 0;
301
302 val = simple_strtoul(buf, NULL, 10);
303 /*
304 * Enforce a minimum sample period of 100 usecs:
305 */
306 if (val < 100)
307 val = 100;
308
309 mutex_lock(&sample_timer_lock);
310 stop_stack_timers();
311 sample_period = val * 1000;
312 start_stack_timers();
313 mutex_unlock(&sample_timer_lock);
314
315 return cnt;
316}
317
Steven Rostedt5e2336a2009-03-05 21:44:55 -0500318static const struct file_operations sysprof_sample_fops = {
Ingo Molnard618b3e2008-05-12 21:20:49 +0200319 .read = sysprof_sample_read,
320 .write = sysprof_sample_write,
321};
322
323void init_tracer_sysprof_debugfs(struct dentry *d_tracer)
324{
Ingo Molnard618b3e2008-05-12 21:20:49 +0200325
Frederic Weisbecker5452af62009-03-27 00:25:38 +0100326 trace_create_file("sysprof_sample_period", 0644,
Ingo Molnard618b3e2008-05-12 21:20:49 +0200327 d_tracer, NULL, &sysprof_sample_fops);
Ingo Molnard618b3e2008-05-12 21:20:49 +0200328}