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>
17 #include <asm/stacktrace.h>
19 int panic_on_unrecovered_nmi;
20 int kstack_depth_to_print = 24;
21 static unsigned int code_bytes = 64;
22 static int die_counter;
24 void printk_address(unsigned long address, int reliable)
26 #ifdef CONFIG_KALLSYMS
27 unsigned long offset = 0;
28 unsigned long symsize;
32 char namebuf[KSYM_NAME_LEN];
35 symname = kallsyms_lookup(address, &symsize, &offset,
38 printk(" [<%08lx>]\n", address);
46 printk(" [<%08lx>] %s%s%s%s%s+0x%lx/0x%lx\n",
47 address, reliab, delim, modname, delim, symname, offset, symsize);
49 printk(" [<%08lx>]\n", address);
53 static inline int valid_stack_ptr(struct thread_info *tinfo,
54 void *p, unsigned int size)
57 return p > t && p <= t + THREAD_SIZE - size;
60 /* The form of the top of the frame on the stack */
62 struct stack_frame *next_frame;
63 unsigned long return_address;
66 static inline unsigned long
67 print_context_stack(struct thread_info *tinfo,
68 unsigned long *stack, unsigned long bp,
69 const struct stacktrace_ops *ops, void *data)
71 struct stack_frame *frame = (struct stack_frame *)bp;
73 while (valid_stack_ptr(tinfo, stack, sizeof(*stack))) {
77 if (__kernel_text_address(addr)) {
78 if ((unsigned long) stack == bp + 4) {
79 ops->address(data, addr, 1);
80 frame = frame->next_frame;
81 bp = (unsigned long) frame;
83 ops->address(data, addr, bp == 0);
91 void dump_trace(struct task_struct *task, struct pt_regs *regs,
92 unsigned long *stack, unsigned long bp,
93 const struct stacktrace_ops *ops, void *data)
102 stack = (unsigned long *)task->thread.sp;
105 #ifdef CONFIG_FRAME_POINTER
107 if (task == current) {
108 /* Grab bp right from our regs */
109 asm("movl %%ebp, %0" : "=r" (bp) :);
111 /* bp is the last reg pushed by switch_to */
112 bp = *(unsigned long *) task->thread.sp;
118 struct thread_info *context;
120 context = (struct thread_info *)
121 ((unsigned long)stack & (~(THREAD_SIZE - 1)));
122 bp = print_context_stack(context, stack, bp, ops, data);
124 * Should be after the line below, but somewhere
125 * in early boot context comes out corrupted and we
126 * can't reference it:
128 if (ops->stack(data, "IRQ") < 0)
130 stack = (unsigned long *)context->previous_esp;
133 touch_nmi_watchdog();
136 EXPORT_SYMBOL(dump_trace);
139 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
142 print_symbol(msg, symbol);
146 static void print_trace_warning(void *data, char *msg)
148 printk("%s%s\n", (char *)data, msg);
151 static int print_trace_stack(void *data, char *name)
157 * Print one address/symbol entries per line.
159 static void print_trace_address(void *data, unsigned long addr, int reliable)
161 printk("%s [<%08lx>] ", (char *)data, addr);
164 print_symbol("%s\n", addr);
165 touch_nmi_watchdog();
168 static const struct stacktrace_ops print_trace_ops = {
169 .warning = print_trace_warning,
170 .warning_symbol = print_trace_warning_symbol,
171 .stack = print_trace_stack,
172 .address = print_trace_address,
176 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
177 unsigned long *stack, unsigned long bp, char *log_lvl)
179 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
180 printk("%s =======================\n", log_lvl);
183 void show_trace(struct task_struct *task, struct pt_regs *regs,
184 unsigned long *stack, unsigned long bp)
186 show_trace_log_lvl(task, regs, stack, bp, "");
190 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
191 unsigned long *sp, unsigned long bp, char *log_lvl)
193 unsigned long *stack;
198 sp = (unsigned long *)task->thread.sp;
200 sp = (unsigned long *)&sp;
204 for (i = 0; i < kstack_depth_to_print; i++) {
205 if (kstack_end(stack))
207 if (i && ((i % 8) == 0))
208 printk("\n%s ", log_lvl);
209 printk("%08lx ", *stack++);
211 printk("\n%sCall Trace:\n", log_lvl);
213 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
216 void show_stack(struct task_struct *task, unsigned long *sp)
219 show_stack_log_lvl(task, NULL, sp, 0, "");
223 * The architecture-independent dump_stack generator
225 void dump_stack(void)
227 unsigned long bp = 0;
230 #ifdef CONFIG_FRAME_POINTER
232 asm("movl %%ebp, %0" : "=r" (bp):);
235 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
236 current->pid, current->comm, print_tainted(),
237 init_utsname()->release,
238 (int)strcspn(init_utsname()->version, " "),
239 init_utsname()->version);
241 show_trace(current, NULL, &stack, bp);
244 EXPORT_SYMBOL(dump_stack);
246 void show_registers(struct pt_regs *regs)
251 __show_regs(regs, 0);
253 printk(KERN_EMERG "Process %.*s (pid: %d, ti=%p task=%p task.ti=%p)",
254 TASK_COMM_LEN, current->comm, task_pid_nr(current),
255 current_thread_info(), current, task_thread_info(current));
257 * When in-kernel, we also print out the stack and code at the
258 * time of the fault..
260 if (!user_mode_vm(regs)) {
261 unsigned int code_prologue = code_bytes * 43 / 64;
262 unsigned int code_len = code_bytes;
266 printk("\n" KERN_EMERG "Stack: ");
267 show_stack_log_lvl(NULL, regs, ®s->sp, 0, KERN_EMERG);
269 printk(KERN_EMERG "Code: ");
271 ip = (u8 *)regs->ip - code_prologue;
272 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
273 /* try starting at EIP */
275 code_len = code_len - code_prologue + 1;
277 for (i = 0; i < code_len; i++, ip++) {
278 if (ip < (u8 *)PAGE_OFFSET ||
279 probe_kernel_address(ip, c)) {
280 printk(" Bad EIP value.");
283 if (ip == (u8 *)regs->ip)
284 printk("<%02x> ", c);
292 int is_valid_bugaddr(unsigned long ip)
296 if (ip < PAGE_OFFSET)
298 if (probe_kernel_address((unsigned short *)ip, ud2))
301 return ud2 == 0x0b0f;
304 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
305 static int die_owner = -1;
306 static unsigned int die_nest_count;
308 unsigned __kprobes long oops_begin(void)
314 if (die_owner != raw_smp_processor_id()) {
316 raw_local_irq_save(flags);
317 __raw_spin_lock(&die_lock);
318 die_owner = smp_processor_id();
322 raw_local_irq_save(flags);
328 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
332 add_taint(TAINT_DIE);
333 __raw_spin_unlock(&die_lock);
334 raw_local_irq_restore(flags);
339 if (kexec_should_crash(current))
343 panic("Fatal exception in interrupt");
346 panic("Fatal exception");
352 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
357 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
358 #ifdef CONFIG_PREEMPT
364 #ifdef CONFIG_DEBUG_PAGEALLOC
365 printk("DEBUG_PAGEALLOC");
368 if (notify_die(DIE_OOPS, str, regs, err,
369 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
372 show_registers(regs);
373 /* Executive summary in case the oops scrolled away */
374 sp = (unsigned long) (®s->sp);
376 if (user_mode(regs)) {
378 ss = regs->ss & 0xffff;
380 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
381 print_symbol("%s", regs->ip);
382 printk(" SS:ESP %04x:%08lx\n", ss, sp);
387 * This is gone through when something in the kernel has done something bad
388 * and is about to be terminated:
390 void die(const char *str, struct pt_regs *regs, long err)
392 unsigned long flags = oops_begin();
394 if (die_nest_count < 3) {
395 report_bug(regs->ip, regs);
397 if (__die(str, regs, err))
400 printk(KERN_EMERG "Recursive die() failure, output suppressed\n");
403 oops_end(flags, regs, SIGSEGV);
406 static int __init kstack_setup(char *s)
408 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
412 __setup("kstack=", kstack_setup);
414 static int __init code_bytes_setup(char *s)
416 code_bytes = simple_strtoul(s, NULL, 0);
417 if (code_bytes > 8192)
422 __setup("code_bytes=", code_bytes_setup);