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/kexec.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
16 #include <linux/sysfs.h>
18 #include <asm/stacktrace.h>
20 #include "dumpstack.h"
22 int panic_on_unrecovered_nmi;
23 unsigned int code_bytes = 64;
24 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
25 static int die_counter;
27 void printk_address(unsigned long address, int reliable)
29 printk(" [<%p>] %s%pS\n", (void *) address,
30 reliable ? "" : "? ", (void *) address);
34 * x86-64 can have up to three kernel stacks:
37 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
40 static inline int valid_stack_ptr(struct thread_info *tinfo,
41 void *p, unsigned int size, void *end)
45 if (p < end && p >= (end-THREAD_SIZE))
50 return p > t && p < t + THREAD_SIZE - size;
54 print_context_stack(struct thread_info *tinfo,
55 unsigned long *stack, unsigned long bp,
56 const struct stacktrace_ops *ops, void *data,
59 struct stack_frame *frame = (struct stack_frame *)bp;
61 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
65 if (__kernel_text_address(addr)) {
66 if ((unsigned long) stack == bp + sizeof(long)) {
67 ops->address(data, addr, 1);
68 frame = frame->next_frame;
69 bp = (unsigned long) frame;
71 ops->address(data, addr, bp == 0);
81 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
84 print_symbol(msg, symbol);
88 static void print_trace_warning(void *data, char *msg)
90 printk("%s%s\n", (char *)data, msg);
93 static int print_trace_stack(void *data, char *name)
95 printk("%s <%s> ", (char *)data, name);
100 * Print one address/symbol entries per line.
102 static void print_trace_address(void *data, unsigned long addr, int reliable)
104 touch_nmi_watchdog();
106 printk_address(addr, reliable);
109 static const struct stacktrace_ops print_trace_ops = {
110 .warning = print_trace_warning,
111 .warning_symbol = print_trace_warning_symbol,
112 .stack = print_trace_stack,
113 .address = print_trace_address,
117 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
118 unsigned long *stack, unsigned long bp, char *log_lvl)
120 printk("%sCall Trace:\n", log_lvl);
121 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
124 void show_trace(struct task_struct *task, struct pt_regs *regs,
125 unsigned long *stack, unsigned long bp)
127 show_trace_log_lvl(task, regs, stack, bp, "");
130 void show_stack(struct task_struct *task, unsigned long *sp)
132 show_stack_log_lvl(task, NULL, sp, 0, "");
136 * The architecture-independent dump_stack generator
138 void dump_stack(void)
140 unsigned long bp = 0;
143 #ifdef CONFIG_FRAME_POINTER
148 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
149 current->pid, current->comm, print_tainted(),
150 init_utsname()->release,
151 (int)strcspn(init_utsname()->version, " "),
152 init_utsname()->version);
153 show_trace(NULL, NULL, &stack, bp);
155 EXPORT_SYMBOL(dump_stack);
157 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
158 static int die_owner = -1;
159 static unsigned int die_nest_count;
161 unsigned __kprobes long oops_begin(void)
168 /* racy, but better than risking deadlock. */
169 raw_local_irq_save(flags);
170 cpu = smp_processor_id();
171 if (!__raw_spin_trylock(&die_lock)) {
172 if (cpu == die_owner)
173 /* nested oops. should stop eventually */;
175 __raw_spin_lock(&die_lock);
184 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
186 if (regs && kexec_should_crash(current))
191 add_taint(TAINT_DIE);
194 /* Nest count reaches zero, release the lock. */
195 __raw_spin_unlock(&die_lock);
196 raw_local_irq_restore(flags);
202 panic("Fatal exception in interrupt");
204 panic("Fatal exception");
208 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
214 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
215 #ifdef CONFIG_PREEMPT
221 #ifdef CONFIG_DEBUG_PAGEALLOC
222 printk("DEBUG_PAGEALLOC");
225 sysfs_printk_last_file();
226 if (notify_die(DIE_OOPS, str, regs, err,
227 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
230 show_registers(regs);
232 sp = (unsigned long) (®s->sp);
234 if (user_mode(regs)) {
236 ss = regs->ss & 0xffff;
238 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
239 print_symbol("%s", regs->ip);
240 printk(" SS:ESP %04x:%08lx\n", ss, sp);
242 /* Executive summary in case the oops scrolled away */
243 printk(KERN_ALERT "RIP ");
244 printk_address(regs->ip, 1);
245 printk(" RSP <%016lx>\n", regs->sp);
251 * This is gone through when something in the kernel has done something bad
252 * and is about to be terminated:
254 void die(const char *str, struct pt_regs *regs, long err)
256 unsigned long flags = oops_begin();
259 if (!user_mode_vm(regs))
260 report_bug(regs->ip, regs);
262 if (__die(str, regs, err))
264 oops_end(flags, regs, sig);
267 void notrace __kprobes
268 die_nmi(char *str, struct pt_regs *regs, int do_panic)
272 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
276 * We are in trouble anyway, lets at least try
277 * to get a message out.
279 flags = oops_begin();
280 printk(KERN_EMERG "%s", str);
281 printk(" on CPU%d, ip %08lx, registers:\n",
282 smp_processor_id(), regs->ip);
283 show_registers(regs);
284 oops_end(flags, regs, 0);
285 if (do_panic || panic_on_oops)
286 panic("Non maskable interrupt");
292 static int __init oops_setup(char *s)
296 if (!strcmp(s, "panic"))
300 early_param("oops", oops_setup);
302 static int __init kstack_setup(char *s)
306 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
309 early_param("kstack", kstack_setup);
311 static int __init code_bytes_setup(char *s)
313 code_bytes = simple_strtoul(s, NULL, 0);
314 if (code_bytes > 8192)
319 __setup("code_bytes=", code_bytes_setup);