Merge branch 'tip/tracing/ftrace' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6] / arch / x86 / kernel / dumpstack.c
1 /*
2  *  Copyright (C) 1991, 1992  Linus Torvalds
3  *  Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
4  */
5 #include <linux/kallsyms.h>
6 #include <linux/kprobes.h>
7 #include <linux/uaccess.h>
8 #include <linux/utsname.h>
9 #include <linux/hardirq.h>
10 #include <linux/kdebug.h>
11 #include <linux/module.h>
12 #include <linux/ptrace.h>
13 #include <linux/kexec.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
16 #include <linux/sysfs.h>
17 #include <linux/ftrace.h>
18
19 #include <asm/stacktrace.h>
20
21 #include "dumpstack.h"
22
23 int panic_on_unrecovered_nmi;
24 unsigned int code_bytes = 64;
25 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
26 static int die_counter;
27
28 void printk_address(unsigned long address, int reliable)
29 {
30         printk(" [<%p>] %s%pS\n", (void *) address,
31                         reliable ? "" : "? ", (void *) address);
32 }
33
34 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
35 static void
36 print_ftrace_graph_addr(unsigned long addr, void *data,
37                         const struct stacktrace_ops *ops,
38                         struct thread_info *tinfo, int *graph)
39 {
40         struct task_struct *task = tinfo->task;
41         unsigned long ret_addr;
42         int index = task->curr_ret_stack;
43
44         if (addr != (unsigned long)return_to_handler)
45                 return;
46
47         if (!task->ret_stack || index < *graph)
48                 return;
49
50         index -= *graph;
51         ret_addr = task->ret_stack[index].ret;
52
53         ops->address(data, ret_addr, 1);
54
55         (*graph)++;
56 }
57 #else
58 static inline void
59 print_ftrace_graph_addr(unsigned long addr, void *data,
60                         const struct stacktrace_ops *ops,
61                         struct thread_info *tinfo, int *graph)
62 { }
63 #endif
64
65 /*
66  * x86-64 can have up to three kernel stacks:
67  * process stack
68  * interrupt stack
69  * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
70  */
71
72 static inline int valid_stack_ptr(struct thread_info *tinfo,
73                         void *p, unsigned int size, void *end)
74 {
75         void *t = tinfo;
76         if (end) {
77                 if (p < end && p >= (end-THREAD_SIZE))
78                         return 1;
79                 else
80                         return 0;
81         }
82         return p > t && p < t + THREAD_SIZE - size;
83 }
84
85 unsigned long
86 print_context_stack(struct thread_info *tinfo,
87                 unsigned long *stack, unsigned long bp,
88                 const struct stacktrace_ops *ops, void *data,
89                 unsigned long *end, int *graph)
90 {
91         struct stack_frame *frame = (struct stack_frame *)bp;
92
93         while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
94                 unsigned long addr;
95
96                 addr = *stack;
97                 if (__kernel_text_address(addr)) {
98                         if ((unsigned long) stack == bp + sizeof(long)) {
99                                 ops->address(data, addr, 1);
100                                 frame = frame->next_frame;
101                                 bp = (unsigned long) frame;
102                         } else {
103                                 ops->address(data, addr, bp == 0);
104                         }
105                         print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
106                 }
107                 stack++;
108         }
109         return bp;
110 }
111
112
113 static void
114 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
115 {
116         printk(data);
117         print_symbol(msg, symbol);
118         printk("\n");
119 }
120
121 static void print_trace_warning(void *data, char *msg)
122 {
123         printk("%s%s\n", (char *)data, msg);
124 }
125
126 static int print_trace_stack(void *data, char *name)
127 {
128         printk("%s <%s> ", (char *)data, name);
129         return 0;
130 }
131
132 /*
133  * Print one address/symbol entries per line.
134  */
135 static void print_trace_address(void *data, unsigned long addr, int reliable)
136 {
137         touch_nmi_watchdog();
138         printk(data);
139         printk_address(addr, reliable);
140 }
141
142 static const struct stacktrace_ops print_trace_ops = {
143         .warning = print_trace_warning,
144         .warning_symbol = print_trace_warning_symbol,
145         .stack = print_trace_stack,
146         .address = print_trace_address,
147 };
148
149 void
150 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
151                 unsigned long *stack, unsigned long bp, char *log_lvl)
152 {
153         printk("%sCall Trace:\n", log_lvl);
154         dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
155 }
156
157 void show_trace(struct task_struct *task, struct pt_regs *regs,
158                 unsigned long *stack, unsigned long bp)
159 {
160         show_trace_log_lvl(task, regs, stack, bp, "");
161 }
162
163 void show_stack(struct task_struct *task, unsigned long *sp)
164 {
165         show_stack_log_lvl(task, NULL, sp, 0, "");
166 }
167
168 /*
169  * The architecture-independent dump_stack generator
170  */
171 void dump_stack(void)
172 {
173         unsigned long bp = 0;
174         unsigned long stack;
175
176 #ifdef CONFIG_FRAME_POINTER
177         if (!bp)
178                 get_bp(bp);
179 #endif
180
181         printk("Pid: %d, comm: %.20s %s %s %.*s\n",
182                 current->pid, current->comm, print_tainted(),
183                 init_utsname()->release,
184                 (int)strcspn(init_utsname()->version, " "),
185                 init_utsname()->version);
186         show_trace(NULL, NULL, &stack, bp);
187 }
188 EXPORT_SYMBOL(dump_stack);
189
190 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
191 static int die_owner = -1;
192 static unsigned int die_nest_count;
193
194 unsigned __kprobes long oops_begin(void)
195 {
196         int cpu;
197         unsigned long flags;
198
199         /* notify the hw-branch tracer so it may disable tracing and
200            add the last trace to the trace buffer -
201            the earlier this happens, the more useful the trace. */
202         trace_hw_branch_oops();
203
204         oops_enter();
205
206         /* racy, but better than risking deadlock. */
207         raw_local_irq_save(flags);
208         cpu = smp_processor_id();
209         if (!__raw_spin_trylock(&die_lock)) {
210                 if (cpu == die_owner)
211                         /* nested oops. should stop eventually */;
212                 else
213                         __raw_spin_lock(&die_lock);
214         }
215         die_nest_count++;
216         die_owner = cpu;
217         console_verbose();
218         bust_spinlocks(1);
219         return flags;
220 }
221
222 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
223 {
224         if (regs && kexec_should_crash(current))
225                 crash_kexec(regs);
226
227         bust_spinlocks(0);
228         die_owner = -1;
229         add_taint(TAINT_DIE);
230         die_nest_count--;
231         if (!die_nest_count)
232                 /* Nest count reaches zero, release the lock. */
233                 __raw_spin_unlock(&die_lock);
234         raw_local_irq_restore(flags);
235         oops_exit();
236
237         if (!signr)
238                 return;
239         if (in_interrupt())
240                 panic("Fatal exception in interrupt");
241         if (panic_on_oops)
242                 panic("Fatal exception");
243         do_exit(signr);
244 }
245
246 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
247 {
248 #ifdef CONFIG_X86_32
249         unsigned short ss;
250         unsigned long sp;
251 #endif
252         printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
253 #ifdef CONFIG_PREEMPT
254         printk("PREEMPT ");
255 #endif
256 #ifdef CONFIG_SMP
257         printk("SMP ");
258 #endif
259 #ifdef CONFIG_DEBUG_PAGEALLOC
260         printk("DEBUG_PAGEALLOC");
261 #endif
262         printk("\n");
263         sysfs_printk_last_file();
264         if (notify_die(DIE_OOPS, str, regs, err,
265                         current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
266                 return 1;
267
268         show_registers(regs);
269 #ifdef CONFIG_X86_32
270         sp = (unsigned long) (&regs->sp);
271         savesegment(ss, ss);
272         if (user_mode(regs)) {
273                 sp = regs->sp;
274                 ss = regs->ss & 0xffff;
275         }
276         printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
277         print_symbol("%s", regs->ip);
278         printk(" SS:ESP %04x:%08lx\n", ss, sp);
279 #else
280         /* Executive summary in case the oops scrolled away */
281         printk(KERN_ALERT "RIP ");
282         printk_address(regs->ip, 1);
283         printk(" RSP <%016lx>\n", regs->sp);
284 #endif
285         return 0;
286 }
287
288 /*
289  * This is gone through when something in the kernel has done something bad
290  * and is about to be terminated:
291  */
292 void die(const char *str, struct pt_regs *regs, long err)
293 {
294         unsigned long flags = oops_begin();
295         int sig = SIGSEGV;
296
297         if (!user_mode_vm(regs))
298                 report_bug(regs->ip, regs);
299
300         if (__die(str, regs, err))
301                 sig = 0;
302         oops_end(flags, regs, sig);
303 }
304
305 void notrace __kprobes
306 die_nmi(char *str, struct pt_regs *regs, int do_panic)
307 {
308         unsigned long flags;
309
310         if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
311                 return;
312
313         /*
314          * We are in trouble anyway, lets at least try
315          * to get a message out.
316          */
317         flags = oops_begin();
318         printk(KERN_EMERG "%s", str);
319         printk(" on CPU%d, ip %08lx, registers:\n",
320                 smp_processor_id(), regs->ip);
321         show_registers(regs);
322         oops_end(flags, regs, 0);
323         if (do_panic || panic_on_oops)
324                 panic("Non maskable interrupt");
325         nmi_exit();
326         local_irq_enable();
327         do_exit(SIGBUS);
328 }
329
330 static int __init oops_setup(char *s)
331 {
332         if (!s)
333                 return -EINVAL;
334         if (!strcmp(s, "panic"))
335                 panic_on_oops = 1;
336         return 0;
337 }
338 early_param("oops", oops_setup);
339
340 static int __init kstack_setup(char *s)
341 {
342         if (!s)
343                 return -EINVAL;
344         kstack_depth_to_print = simple_strtoul(s, NULL, 0);
345         return 0;
346 }
347 early_param("kstack", kstack_setup);
348
349 static int __init code_bytes_setup(char *s)
350 {
351         code_bytes = simple_strtoul(s, NULL, 0);
352         if (code_bytes > 8192)
353                 code_bytes = 8192;
354
355         return 1;
356 }
357 __setup("code_bytes=", code_bytes_setup);