2 * Copyright (C) 2008 Steven Rostedt <srostedt@redhat.com>
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>
17 #define STACK_TRACE_ENTRIES 500
19 static unsigned long stack_dump_trace[STACK_TRACE_ENTRIES+1] =
20 { [0 ... (STACK_TRACE_ENTRIES)] = ULONG_MAX };
21 static unsigned stack_dump_index[STACK_TRACE_ENTRIES];
23 static struct stack_trace max_stack_trace = {
24 .max_entries = STACK_TRACE_ENTRIES,
25 .entries = stack_dump_trace,
28 static unsigned long max_stack_size;
29 static raw_spinlock_t max_stack_lock =
30 (raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
32 static int stack_trace_disabled __read_mostly;
33 static DEFINE_PER_CPU(int, trace_active);
35 static inline void check_stack(void)
37 unsigned long this_size, flags;
38 unsigned long *p, *top, *start;
41 this_size = ((unsigned long)&this_size) & (THREAD_SIZE-1);
42 this_size = THREAD_SIZE - this_size;
44 if (this_size <= max_stack_size)
47 /* we do not handle interrupt stacks yet */
48 if (!object_is_on_stack(&this_size))
51 local_irq_save(flags);
52 __raw_spin_lock(&max_stack_lock);
54 /* a race could have already updated it */
55 if (this_size <= max_stack_size)
58 max_stack_size = this_size;
60 max_stack_trace.nr_entries = 0;
61 max_stack_trace.skip = 3;
63 save_stack_trace(&max_stack_trace);
66 * Now find where in the stack these are.
70 top = (unsigned long *)
71 (((unsigned long)start & ~(THREAD_SIZE-1)) + THREAD_SIZE);
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.
80 while (i < max_stack_trace.nr_entries) {
83 stack_dump_index[i] = this_size;
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);
91 /* Start the search from here */
101 __raw_spin_unlock(&max_stack_lock);
102 local_irq_restore(flags);
106 stack_trace_call(unsigned long ip, unsigned long parent_ip)
110 if (unlikely(!ftrace_enabled || stack_trace_disabled))
113 resched = ftrace_preempt_disable();
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)
123 per_cpu(trace_active, cpu)--;
124 /* prevent recursion in schedule */
125 ftrace_preempt_enable(resched);
128 static struct ftrace_ops trace_ops __read_mostly =
130 .func = stack_trace_call,
134 stack_max_size_read(struct file *filp, char __user *ubuf,
135 size_t count, loff_t *ppos)
137 unsigned long *ptr = filp->private_data;
141 r = snprintf(buf, sizeof(buf), "%ld\n", *ptr);
144 return simple_read_from_buffer(ubuf, count, ppos, buf, r);
148 stack_max_size_write(struct file *filp, const char __user *ubuf,
149 size_t count, loff_t *ppos)
151 long *ptr = filp->private_data;
152 unsigned long val, flags;
156 if (count >= sizeof(buf))
159 if (copy_from_user(&buf, ubuf, count))
164 ret = strict_strtoul(buf, 10, &val);
168 local_irq_save(flags);
169 __raw_spin_lock(&max_stack_lock);
171 __raw_spin_unlock(&max_stack_lock);
172 local_irq_restore(flags);
177 static struct file_operations stack_max_size_fops = {
178 .open = tracing_open_generic,
179 .read = stack_max_size_read,
180 .write = stack_max_size_write,
184 t_next(struct seq_file *m, void *v, loff_t *pos)
190 if (v == SEQ_START_TOKEN)
197 if (i >= max_stack_trace.nr_entries ||
198 stack_dump_trace[i] == ULONG_MAX)
201 m->private = (void *)i;
206 static void *t_start(struct seq_file *m, loff_t *pos)
208 void *t = SEQ_START_TOKEN;
212 __raw_spin_lock(&max_stack_lock);
215 return SEQ_START_TOKEN;
217 for (; t && l < *pos; t = t_next(m, t, &l))
223 static void t_stop(struct seq_file *m, void *p)
225 __raw_spin_unlock(&max_stack_lock);
229 static int trace_lookup_stack(struct seq_file *m, long i)
231 unsigned long addr = stack_dump_trace[i];
232 #ifdef CONFIG_KALLSYMS
233 char str[KSYM_SYMBOL_LEN];
235 sprint_symbol(str, addr);
237 return seq_printf(m, "%s\n", str);
239 return seq_printf(m, "%p\n", (void*)addr);
243 static int t_show(struct seq_file *m, void *v)
248 if (v == SEQ_START_TOKEN) {
249 seq_printf(m, " Depth Size Location"
251 " ----- ---- --------\n",
252 max_stack_trace.nr_entries);
258 if (i >= max_stack_trace.nr_entries ||
259 stack_dump_trace[i] == ULONG_MAX)
262 if (i+1 == max_stack_trace.nr_entries ||
263 stack_dump_trace[i+1] == ULONG_MAX)
264 size = stack_dump_index[i];
266 size = stack_dump_index[i] - stack_dump_index[i+1];
268 seq_printf(m, "%3ld) %8d %5d ", i, stack_dump_index[i], size);
270 trace_lookup_stack(m, i);
275 static struct seq_operations stack_trace_seq_ops = {
282 static int stack_trace_open(struct inode *inode, struct file *file)
286 ret = seq_open(file, &stack_trace_seq_ops);
291 static struct file_operations stack_trace_fops = {
292 .open = stack_trace_open,
297 static __init int stack_trace_init(void)
299 struct dentry *d_tracer;
300 struct dentry *entry;
302 d_tracer = tracing_init_dentry();
304 entry = debugfs_create_file("stack_max_size", 0644, d_tracer,
305 &max_stack_size, &stack_max_size_fops);
307 pr_warning("Could not create debugfs 'stack_max_size' entry\n");
309 entry = debugfs_create_file("stack_trace", 0444, d_tracer,
310 NULL, &stack_trace_fops);
312 pr_warning("Could not create debugfs 'stack_trace' entry\n");
314 register_ftrace_function(&trace_ops);
319 device_initcall(stack_trace_init);