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>
18 #include <linux/ftrace.h>
20 #include <asm/stacktrace.h>
22 #include "dumpstack.h"
24 int panic_on_unrecovered_nmi;
26 unsigned int code_bytes = 64;
27 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
28 static int die_counter;
30 void printk_address(unsigned long address, int reliable)
32 printk(" [<%p>] %s%pS\n", (void *) address,
33 reliable ? "" : "? ", (void *) address);
36 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
38 print_ftrace_graph_addr(unsigned long addr, void *data,
39 const struct stacktrace_ops *ops,
40 struct thread_info *tinfo, int *graph)
42 struct task_struct *task = tinfo->task;
43 unsigned long ret_addr;
44 int index = task->curr_ret_stack;
46 if (addr != (unsigned long)return_to_handler)
49 if (!task->ret_stack || index < *graph)
53 ret_addr = task->ret_stack[index].ret;
55 ops->address(data, ret_addr, 1);
61 print_ftrace_graph_addr(unsigned long addr, void *data,
62 const struct stacktrace_ops *ops,
63 struct thread_info *tinfo, int *graph)
68 * x86-64 can have up to three kernel stacks:
71 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
74 static inline int valid_stack_ptr(struct thread_info *tinfo,
75 void *p, unsigned int size, void *end)
79 if (p < end && p >= (end-THREAD_SIZE))
84 return p > t && p < t + THREAD_SIZE - size;
88 print_context_stack(struct thread_info *tinfo,
89 unsigned long *stack, unsigned long bp,
90 const struct stacktrace_ops *ops, void *data,
91 unsigned long *end, int *graph)
93 struct stack_frame *frame = (struct stack_frame *)bp;
95 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
99 if (__kernel_text_address(addr)) {
100 if ((unsigned long) stack == bp + sizeof(long)) {
101 ops->address(data, addr, 1);
102 frame = frame->next_frame;
103 bp = (unsigned long) frame;
105 ops->address(data, addr, 0);
107 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
116 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
119 print_symbol(msg, symbol);
123 static void print_trace_warning(void *data, char *msg)
125 printk("%s%s\n", (char *)data, msg);
128 static int print_trace_stack(void *data, char *name)
130 printk("%s <%s> ", (char *)data, name);
135 * Print one address/symbol entries per line.
137 static void print_trace_address(void *data, unsigned long addr, int reliable)
139 touch_nmi_watchdog();
141 printk_address(addr, reliable);
144 static const struct stacktrace_ops print_trace_ops = {
145 .warning = print_trace_warning,
146 .warning_symbol = print_trace_warning_symbol,
147 .stack = print_trace_stack,
148 .address = print_trace_address,
152 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
153 unsigned long *stack, unsigned long bp, char *log_lvl)
155 printk("%sCall Trace:\n", log_lvl);
156 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
159 void show_trace(struct task_struct *task, struct pt_regs *regs,
160 unsigned long *stack, unsigned long bp)
162 show_trace_log_lvl(task, regs, stack, bp, "");
165 void show_stack(struct task_struct *task, unsigned long *sp)
167 show_stack_log_lvl(task, NULL, sp, 0, "");
171 * The architecture-independent dump_stack generator
173 void dump_stack(void)
175 unsigned long bp = 0;
178 #ifdef CONFIG_FRAME_POINTER
183 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
184 current->pid, current->comm, print_tainted(),
185 init_utsname()->release,
186 (int)strcspn(init_utsname()->version, " "),
187 init_utsname()->version);
188 show_trace(NULL, NULL, &stack, bp);
190 EXPORT_SYMBOL(dump_stack);
192 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
193 static int die_owner = -1;
194 static unsigned int die_nest_count;
196 unsigned __kprobes long oops_begin(void)
201 /* notify the hw-branch tracer so it may disable tracing and
202 add the last trace to the trace buffer -
203 the earlier this happens, the more useful the trace. */
204 trace_hw_branch_oops();
208 /* racy, but better than risking deadlock. */
209 raw_local_irq_save(flags);
210 cpu = smp_processor_id();
211 if (!__raw_spin_trylock(&die_lock)) {
212 if (cpu == die_owner)
213 /* nested oops. should stop eventually */;
215 __raw_spin_lock(&die_lock);
224 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
226 if (regs && kexec_should_crash(current))
231 add_taint(TAINT_DIE);
234 /* Nest count reaches zero, release the lock. */
235 __raw_spin_unlock(&die_lock);
236 raw_local_irq_restore(flags);
242 panic("Fatal exception in interrupt");
244 panic("Fatal exception");
248 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
254 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
255 #ifdef CONFIG_PREEMPT
261 #ifdef CONFIG_DEBUG_PAGEALLOC
262 printk("DEBUG_PAGEALLOC");
265 sysfs_printk_last_file();
266 if (notify_die(DIE_OOPS, str, regs, err,
267 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
270 show_registers(regs);
272 sp = (unsigned long) (®s->sp);
274 if (user_mode(regs)) {
276 ss = regs->ss & 0xffff;
278 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
279 print_symbol("%s", regs->ip);
280 printk(" SS:ESP %04x:%08lx\n", ss, sp);
282 /* Executive summary in case the oops scrolled away */
283 printk(KERN_ALERT "RIP ");
284 printk_address(regs->ip, 1);
285 printk(" RSP <%016lx>\n", regs->sp);
291 * This is gone through when something in the kernel has done something bad
292 * and is about to be terminated:
294 void die(const char *str, struct pt_regs *regs, long err)
296 unsigned long flags = oops_begin();
299 if (!user_mode_vm(regs))
300 report_bug(regs->ip, regs);
302 if (__die(str, regs, err))
304 oops_end(flags, regs, sig);
307 void notrace __kprobes
308 die_nmi(char *str, struct pt_regs *regs, int do_panic)
312 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
316 * We are in trouble anyway, lets at least try
317 * to get a message out.
319 flags = oops_begin();
320 printk(KERN_EMERG "%s", str);
321 printk(" on CPU%d, ip %08lx, registers:\n",
322 smp_processor_id(), regs->ip);
323 show_registers(regs);
324 oops_end(flags, regs, 0);
325 if (do_panic || panic_on_oops)
326 panic("Non maskable interrupt");
332 static int __init oops_setup(char *s)
336 if (!strcmp(s, "panic"))
340 early_param("oops", oops_setup);
342 static int __init kstack_setup(char *s)
346 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
349 early_param("kstack", kstack_setup);
351 static int __init code_bytes_setup(char *s)
353 code_bytes = simple_strtoul(s, NULL, 0);
354 if (code_bytes > 8192)
359 __setup("code_bytes=", code_bytes_setup);