blob: 0b863f2cbc8e3ff04c9ea8347f4ffb6795dab446 [file] [log] [blame]
Steven Rostedte5a81b62008-08-27 23:31:01 -04001/*
2 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
3 *
4 */
5#include <linux/stacktrace.h>
6#include <linux/kallsyms.h>
7#include <linux/seq_file.h>
8#include <linux/spinlock.h>
9#include <linux/uaccess.h>
10#include <linux/debugfs.h>
11#include <linux/ftrace.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/fs.h>
15#include "trace.h"
16
17#define STACK_TRACE_ENTRIES 500
18
Steven Rostedt1b6cced2008-08-29 16:51:43 -040019static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
20 { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
21static unsigned stack_dump_index[STACK_TRACE_ENTRIES];
22
Steven Rostedte5a81b62008-08-27 23:31:01 -040023static struct stack_trace max_stack_trace = {
24 .max_entries = STACK_TRACE_ENTRIES,
25 .entries = stack_dump_trace,
26};
27
28static unsigned long max_stack_size;
29static raw_spinlock_t max_stack_lock =
30 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
31
32static int stack_trace_disabled __read_mostly;
33static DEFINE_PER_CPU(int, trace_active);
34
35static inline void check_stack(void)
36{
Steven Rostedt1b6cced2008-08-29 16:51:43 -040037 unsigned long this_size, flags;
38 unsigned long *p, *top, *start;
39 int i;
Steven Rostedte5a81b62008-08-27 23:31:01 -040040
41 this_size = ((unsigned long)&this_size) & (THREAD_SIZE-1);
42 this_size = THREAD_SIZE - this_size;
43
44 if (this_size <= max_stack_size)
45 return;
46
Steven Rostedt81520a12008-10-06 21:24:18 -040047 /* we do not handle interrupt stacks yet */
48 if (!object_is_on_stack(&this_size))
49 return;
50
Steven Rostedta5e25882008-12-02 15:34:05 -050051 local_irq_save(flags);
Steven Rostedte5a81b62008-08-27 23:31:01 -040052 __raw_spin_lock(&max_stack_lock);
53
54 /* a race could have already updated it */
55 if (this_size <= max_stack_size)
56 goto out;
57
58 max_stack_size = this_size;
59
60 max_stack_trace.nr_entries = 0;
Steven Rostedt1b6cced2008-08-29 16:51:43 -040061 max_stack_trace.skip = 3;
Steven Rostedte5a81b62008-08-27 23:31:01 -040062
63 save_stack_trace(&max_stack_trace);
64
Steven Rostedt1b6cced2008-08-29 16:51:43 -040065 /*
66 * Now find where in the stack these are.
67 */
68 i = 0;
69 start = &this_size;
70 top = (unsigned long *)
71 (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
72
73 /*
74 * Loop through all the entries. One of the entries may
75 * for some reason be missed on the stack, so we may
76 * have to account for them. If they are all there, this
77 * loop will only happen once. This code only takes place
78 * on a new max, so it is far from a fast path.
79 */
80 while (i < max_stack_trace.nr_entries) {
Steven Rostedt0a371192008-12-03 11:04:50 -050081 int found = 0;
Steven Rostedt1b6cced2008-08-29 16:51:43 -040082
83 stack_dump_index[i] = this_size;
84 p = start;
85
86 for (; p < top && i < max_stack_trace.nr_entries; p++) {
87 if (*p == stack_dump_trace[i]) {
88 this_size = stack_dump_index[i++] =
89 (top - p) * sizeof(unsigned long);
Steven Rostedt0a371192008-12-03 11:04:50 -050090 found = 1;
Steven Rostedt1b6cced2008-08-29 16:51:43 -040091 /* Start the search from here */
92 start = p + 1;
93 }
94 }
95
Steven Rostedt0a371192008-12-03 11:04:50 -050096 if (!found)
97 i++;
Steven Rostedt1b6cced2008-08-29 16:51:43 -040098 }
99
Steven Rostedte5a81b62008-08-27 23:31:01 -0400100 out:
101 __raw_spin_unlock(&max_stack_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -0500102 local_irq_restore(flags);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400103}
104
105static void
106stack_trace_call(unsigned long ip, unsigned long parent_ip)
107{
108 int cpu, resched;
109
110 if (unlikely(!ftrace_enabled || stack_trace_disabled))
111 return;
112
Steven Rostedt182e9f52008-11-03 23:15:56 -0500113 resched = ftrace_preempt_disable();
Steven Rostedte5a81b62008-08-27 23:31:01 -0400114
115 cpu = raw_smp_processor_id();
116 /* no atomic needed, we only modify this variable by this cpu */
117 if (per_cpu(trace_active, cpu)++ != 0)
118 goto out;
119
120 check_stack();
121
122 out:
123 per_cpu(trace_active, cpu)--;
124 /* prevent recursion in schedule */
Steven Rostedt182e9f52008-11-03 23:15:56 -0500125 ftrace_preempt_enable(resched);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400126}
127
128static struct ftrace_ops trace_ops __read_mostly =
129{
130 .func = stack_trace_call,
131};
132
133static ssize_t
134stack_max_size_read(struct file *filp, char __user *ubuf,
135 size_t count, loff_t *ppos)
136{
137 unsigned long *ptr = filp->private_data;
138 char buf[64];
139 int r;
140
141 r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
142 if (r > sizeof(buf))
143 r = sizeof(buf);
144 return simple_read_from_buffer(ubuf, count, ppos, buf, r);
145}
146
147static ssize_t
148stack_max_size_write(struct file *filp, const char __user *ubuf,
149 size_t count, loff_t *ppos)
150{
151 long *ptr = filp->private_data;
152 unsigned long val, flags;
153 char buf[64];
154 int ret;
155
156 if (count >= sizeof(buf))
157 return -EINVAL;
158
159 if (copy_from_user(&buf, ubuf, count))
160 return -EFAULT;
161
162 buf[count] = 0;
163
164 ret = strict_strtoul(buf, 10, &val);
165 if (ret < 0)
166 return ret;
167
Steven Rostedta5e25882008-12-02 15:34:05 -0500168 local_irq_save(flags);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400169 __raw_spin_lock(&max_stack_lock);
170 *ptr = val;
171 __raw_spin_unlock(&max_stack_lock);
Steven Rostedta5e25882008-12-02 15:34:05 -0500172 local_irq_restore(flags);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400173
174 return count;
175}
176
177static struct file_operations stack_max_size_fops = {
178 .open = tracing_open_generic,
179 .read = stack_max_size_read,
180 .write = stack_max_size_write,
181};
182
183static void *
184t_next(struct seq_file *m, void *v, loff_t *pos)
185{
Liming Wang522a1102008-11-21 11:00:18 +0800186 long i;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400187
188 (*pos)++;
189
Liming Wang522a1102008-11-21 11:00:18 +0800190 if (v == SEQ_START_TOKEN)
191 i = 0;
192 else {
193 i = *(long *)v;
194 i++;
195 }
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400196
197 if (i >= max_stack_trace.nr_entries ||
198 stack_dump_trace[i] == ULONG_MAX)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400199 return NULL;
200
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400201 m->private = (void *)i;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400202
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400203 return &m->private;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400204}
205
206static void *t_start(struct seq_file *m, loff_t *pos)
207{
Liming Wang522a1102008-11-21 11:00:18 +0800208 void *t = SEQ_START_TOKEN;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400209 loff_t l = 0;
210
211 local_irq_disable();
212 __raw_spin_lock(&max_stack_lock);
213
Liming Wang522a1102008-11-21 11:00:18 +0800214 if (*pos == 0)
215 return SEQ_START_TOKEN;
216
Steven Rostedte5a81b62008-08-27 23:31:01 -0400217 for (; t && l < *pos; t = t_next(m, t, &l))
218 ;
219
220 return t;
221}
222
223static void t_stop(struct seq_file *m, void *p)
224{
225 __raw_spin_unlock(&max_stack_lock);
226 local_irq_enable();
227}
228
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400229static int trace_lookup_stack(struct seq_file *m, long i)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400230{
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400231 unsigned long addr = stack_dump_trace[i];
Steven Rostedte5a81b62008-08-27 23:31:01 -0400232#ifdef CONFIG_KALLSYMS
233 char str[KSYM_SYMBOL_LEN];
234
235 sprint_symbol(str, addr);
236
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400237 return seq_printf(m, "%s\n", str);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400238#else
239 return seq_printf(m, "%p\n", (void*)addr);
240#endif
241}
242
243static int t_show(struct seq_file *m, void *v)
244{
Liming Wang522a1102008-11-21 11:00:18 +0800245 long i;
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400246 int size;
Steven Rostedte5a81b62008-08-27 23:31:01 -0400247
Liming Wang522a1102008-11-21 11:00:18 +0800248 if (v == SEQ_START_TOKEN) {
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400249 seq_printf(m, " Depth Size Location"
250 " (%d entries)\n"
251 " ----- ---- --------\n",
252 max_stack_trace.nr_entries);
253 return 0;
254 }
255
Liming Wang522a1102008-11-21 11:00:18 +0800256 i = *(long *)v;
257
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400258 if (i >= max_stack_trace.nr_entries ||
259 stack_dump_trace[i] == ULONG_MAX)
Steven Rostedte5a81b62008-08-27 23:31:01 -0400260 return 0;
261
Steven Rostedt1b6cced2008-08-29 16:51:43 -0400262 if (i+1 == max_stack_trace.nr_entries ||
263 stack_dump_trace[i+1] == ULONG_MAX)
264 size = stack_dump_index[i];
265 else
266 size = stack_dump_index[i] - stack_dump_index[i+1];
267
268 seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
269
270 trace_lookup_stack(m, i);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400271
272 return 0;
273}
274
275static struct seq_operations stack_trace_seq_ops = {
276 .start = t_start,
277 .next = t_next,
278 .stop = t_stop,
279 .show = t_show,
280};
281
282static int stack_trace_open(struct inode *inode, struct file *file)
283{
284 int ret;
285
286 ret = seq_open(file, &stack_trace_seq_ops);
Steven Rostedte5a81b62008-08-27 23:31:01 -0400287
288 return ret;
289}
290
291static struct file_operations stack_trace_fops = {
292 .open = stack_trace_open,
293 .read = seq_read,
294 .llseek = seq_lseek,
295};
296
297static __init int stack_trace_init(void)
298{
299 struct dentry *d_tracer;
300 struct dentry *entry;
301
302 d_tracer = tracing_init_dentry();
303
304 entry = debugfs_create_file("stack_max_size", 0644, d_tracer,
305 &max_stack_size, &stack_max_size_fops);
306 if (!entry)
307 pr_warning("Could not create debugfs 'stack_max_size' entry\n");
308
309 entry = debugfs_create_file("stack_trace", 0444, d_tracer,
310 NULL, &stack_trace_fops);
311 if (!entry)
312 pr_warning("Could not create debugfs 'stack_trace' entry\n");
313
314 register_ftrace_function(&trace_ops);
315
316 return 0;
317}
318
319device_initcall(stack_trace_init);