2 * Copyright (C) 1991, 1992 Linus Torvalds
3 * Copyright (C) 2000, 2001, 2002 Andi Kleen, SuSE Labs
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/ftrace.h>
14 #include <linux/kexec.h>
15 #include <linux/bug.h>
16 #include <linux/nmi.h>
17 #include <linux/sysfs.h>
19 #include <asm/stacktrace.h>
21 #include "dumpstack.h"
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;
28 void printk_address(unsigned long address, int reliable)
30 printk(" [<%p>] %s%pS\n", (void *) address,
31 reliable ? "" : "? ", (void *) address);
34 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
36 print_ftrace_graph_addr(unsigned long addr, void *data,
37 const struct stacktrace_ops *ops,
38 struct thread_info *tinfo, int *graph)
40 struct task_struct *task = tinfo->task;
41 unsigned long ret_addr;
42 int index = task->curr_ret_stack;
44 if (addr != (unsigned long)return_to_handler)
47 if (!task->ret_stack || index < *graph)
51 ret_addr = task->ret_stack[index].ret;
53 ops->address(data, ret_addr, 1);
59 print_ftrace_graph_addr(unsigned long addr, void *data,
60 const struct stacktrace_ops *ops,
61 struct thread_info *tinfo, int *graph)
66 * x86-64 can have up to three kernel stacks:
69 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
72 static inline int valid_stack_ptr(struct thread_info *tinfo,
73 void *p, unsigned int size, void *end)
77 if (p < end && p >= (end-THREAD_SIZE))
82 return p > t && p < t + THREAD_SIZE - size;
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)
91 struct stack_frame *frame = (struct stack_frame *)bp;
93 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
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;
103 ops->address(data, addr, 0);
105 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
114 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
117 print_symbol(msg, symbol);
121 static void print_trace_warning(void *data, char *msg)
123 printk("%s%s\n", (char *)data, msg);
126 static int print_trace_stack(void *data, char *name)
128 printk("%s <%s> ", (char *)data, name);
133 * Print one address/symbol entries per line.
135 static void print_trace_address(void *data, unsigned long addr, int reliable)
137 touch_nmi_watchdog();
139 printk_address(addr, reliable);
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,
150 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
151 unsigned long *stack, unsigned long bp, char *log_lvl)
153 printk("%sCall Trace:\n", log_lvl);
154 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
157 void show_trace(struct task_struct *task, struct pt_regs *regs,
158 unsigned long *stack, unsigned long bp)
160 show_trace_log_lvl(task, regs, stack, bp, "");
163 void show_stack(struct task_struct *task, unsigned long *sp)
165 show_stack_log_lvl(task, NULL, sp, 0, "");
169 * The architecture-independent dump_stack generator
171 void dump_stack(void)
173 unsigned long bp = 0;
176 #ifdef CONFIG_FRAME_POINTER
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);
188 EXPORT_SYMBOL(dump_stack);
190 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
191 static int die_owner = -1;
192 static unsigned int die_nest_count;
194 unsigned __kprobes long oops_begin(void)
201 /* racy, but better than risking deadlock. */
202 raw_local_irq_save(flags);
203 cpu = smp_processor_id();
204 if (!__raw_spin_trylock(&die_lock)) {
205 if (cpu == die_owner)
206 /* nested oops. should stop eventually */;
208 __raw_spin_lock(&die_lock);
217 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
219 if (regs && kexec_should_crash(current))
224 add_taint(TAINT_DIE);
227 /* Nest count reaches zero, release the lock. */
228 __raw_spin_unlock(&die_lock);
229 raw_local_irq_restore(flags);
235 panic("Fatal exception in interrupt");
237 panic("Fatal exception");
241 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
247 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
248 #ifdef CONFIG_PREEMPT
254 #ifdef CONFIG_DEBUG_PAGEALLOC
255 printk("DEBUG_PAGEALLOC");
258 sysfs_printk_last_file();
259 if (notify_die(DIE_OOPS, str, regs, err,
260 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
263 show_registers(regs);
265 sp = (unsigned long) (®s->sp);
267 if (user_mode(regs)) {
269 ss = regs->ss & 0xffff;
271 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
272 print_symbol("%s", regs->ip);
273 printk(" SS:ESP %04x:%08lx\n", ss, sp);
275 /* Executive summary in case the oops scrolled away */
276 printk(KERN_ALERT "RIP ");
277 printk_address(regs->ip, 1);
278 printk(" RSP <%016lx>\n", regs->sp);
284 * This is gone through when something in the kernel has done something bad
285 * and is about to be terminated:
287 void die(const char *str, struct pt_regs *regs, long err)
289 unsigned long flags = oops_begin();
292 if (!user_mode_vm(regs))
293 report_bug(regs->ip, regs);
295 if (__die(str, regs, err))
297 oops_end(flags, regs, sig);
300 void notrace __kprobes
301 die_nmi(char *str, struct pt_regs *regs, int do_panic)
305 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
309 * We are in trouble anyway, lets at least try
310 * to get a message out.
312 flags = oops_begin();
313 printk(KERN_EMERG "%s", str);
314 printk(" on CPU%d, ip %08lx, registers:\n",
315 smp_processor_id(), regs->ip);
316 show_registers(regs);
317 oops_end(flags, regs, 0);
318 if (do_panic || panic_on_oops)
319 panic("Non maskable interrupt");
325 static int __init oops_setup(char *s)
329 if (!strcmp(s, "panic"))
333 early_param("oops", oops_setup);
335 static int __init kstack_setup(char *s)
339 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
342 early_param("kstack", kstack_setup);
344 static int __init code_bytes_setup(char *s)
346 code_bytes = simple_strtoul(s, NULL, 0);
347 if (code_bytes > 8192)
352 __setup("code_bytes=", code_bytes_setup);