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;
25 unsigned int code_bytes = 64;
26 int kstack_depth_to_print = 3 * STACKSLOTS_PER_LINE;
27 static int die_counter;
29 void printk_address(unsigned long address, int reliable)
31 printk(" [<%p>] %s%pS\n", (void *) address,
32 reliable ? "" : "? ", (void *) address);
35 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
37 print_ftrace_graph_addr(unsigned long addr, void *data,
38 const struct stacktrace_ops *ops,
39 struct thread_info *tinfo, int *graph)
41 struct task_struct *task = tinfo->task;
42 unsigned long ret_addr;
43 int index = task->curr_ret_stack;
45 if (addr != (unsigned long)return_to_handler)
48 if (!task->ret_stack || index < *graph)
52 ret_addr = task->ret_stack[index].ret;
54 ops->address(data, ret_addr, 1);
60 print_ftrace_graph_addr(unsigned long addr, void *data,
61 const struct stacktrace_ops *ops,
62 struct thread_info *tinfo, int *graph)
67 * x86-64 can have up to three kernel stacks:
70 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
73 static inline int valid_stack_ptr(struct thread_info *tinfo,
74 void *p, unsigned int size, void *end)
78 if (p < end && p >= (end-THREAD_SIZE))
83 return p > t && p < t + THREAD_SIZE - size;
87 print_context_stack(struct thread_info *tinfo,
88 unsigned long *stack, unsigned long bp,
89 const struct stacktrace_ops *ops, void *data,
90 unsigned long *end, int *graph)
92 struct stack_frame *frame = (struct stack_frame *)bp;
94 while (valid_stack_ptr(tinfo, stack, sizeof(*stack), end)) {
98 if (__kernel_text_address(addr)) {
99 if ((unsigned long) stack == bp + sizeof(long)) {
100 ops->address(data, addr, 1);
101 frame = frame->next_frame;
102 bp = (unsigned long) frame;
104 ops->address(data, addr, bp == 0);
106 print_ftrace_graph_addr(addr, data, ops, tinfo, graph);
115 print_trace_warning_symbol(void *data, char *msg, unsigned long symbol)
118 print_symbol(msg, symbol);
122 static void print_trace_warning(void *data, char *msg)
124 printk("%s%s\n", (char *)data, msg);
127 static int print_trace_stack(void *data, char *name)
129 printk("%s <%s> ", (char *)data, name);
134 * Print one address/symbol entries per line.
136 static void print_trace_address(void *data, unsigned long addr, int reliable)
138 touch_nmi_watchdog();
140 printk_address(addr, reliable);
143 static const struct stacktrace_ops print_trace_ops = {
144 .warning = print_trace_warning,
145 .warning_symbol = print_trace_warning_symbol,
146 .stack = print_trace_stack,
147 .address = print_trace_address,
151 show_trace_log_lvl(struct task_struct *task, struct pt_regs *regs,
152 unsigned long *stack, unsigned long bp, char *log_lvl)
154 printk("%sCall Trace:\n", log_lvl);
155 dump_trace(task, regs, stack, bp, &print_trace_ops, log_lvl);
158 void show_trace(struct task_struct *task, struct pt_regs *regs,
159 unsigned long *stack, unsigned long bp)
161 show_trace_log_lvl(task, regs, stack, bp, "");
164 void show_stack(struct task_struct *task, unsigned long *sp)
166 show_stack_log_lvl(task, NULL, sp, 0, "");
170 * The architecture-independent dump_stack generator
172 void dump_stack(void)
174 unsigned long bp = 0;
177 #ifdef CONFIG_FRAME_POINTER
182 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
183 current->pid, current->comm, print_tainted(),
184 init_utsname()->release,
185 (int)strcspn(init_utsname()->version, " "),
186 init_utsname()->version);
187 show_trace(NULL, NULL, &stack, bp);
189 EXPORT_SYMBOL(dump_stack);
191 static raw_spinlock_t die_lock = __RAW_SPIN_LOCK_UNLOCKED;
192 static int die_owner = -1;
193 static unsigned int die_nest_count;
195 unsigned __kprobes long oops_begin(void)
200 /* notify the hw-branch tracer so it may disable tracing and
201 add the last trace to the trace buffer -
202 the earlier this happens, the more useful the trace. */
203 trace_hw_branch_oops();
207 /* racy, but better than risking deadlock. */
208 raw_local_irq_save(flags);
209 cpu = smp_processor_id();
210 if (!__raw_spin_trylock(&die_lock)) {
211 if (cpu == die_owner)
212 /* nested oops. should stop eventually */;
214 __raw_spin_lock(&die_lock);
223 void __kprobes oops_end(unsigned long flags, struct pt_regs *regs, int signr)
225 if (regs && kexec_should_crash(current))
230 add_taint(TAINT_DIE);
233 /* Nest count reaches zero, release the lock. */
234 __raw_spin_unlock(&die_lock);
235 raw_local_irq_restore(flags);
241 panic("Fatal exception in interrupt");
243 panic("Fatal exception");
247 int __kprobes __die(const char *str, struct pt_regs *regs, long err)
253 printk(KERN_EMERG "%s: %04lx [#%d] ", str, err & 0xffff, ++die_counter);
254 #ifdef CONFIG_PREEMPT
260 #ifdef CONFIG_DEBUG_PAGEALLOC
261 printk("DEBUG_PAGEALLOC");
264 sysfs_printk_last_file();
265 if (notify_die(DIE_OOPS, str, regs, err,
266 current->thread.trap_no, SIGSEGV) == NOTIFY_STOP)
269 show_registers(regs);
271 sp = (unsigned long) (®s->sp);
273 if (user_mode(regs)) {
275 ss = regs->ss & 0xffff;
277 printk(KERN_EMERG "EIP: [<%08lx>] ", regs->ip);
278 print_symbol("%s", regs->ip);
279 printk(" SS:ESP %04x:%08lx\n", ss, sp);
281 /* Executive summary in case the oops scrolled away */
282 printk(KERN_ALERT "RIP ");
283 printk_address(regs->ip, 1);
284 printk(" RSP <%016lx>\n", regs->sp);
290 * This is gone through when something in the kernel has done something bad
291 * and is about to be terminated:
293 void die(const char *str, struct pt_regs *regs, long err)
295 unsigned long flags = oops_begin();
298 if (!user_mode_vm(regs))
299 report_bug(regs->ip, regs);
301 if (__die(str, regs, err))
303 oops_end(flags, regs, sig);
306 void notrace __kprobes
307 die_nmi(char *str, struct pt_regs *regs, int do_panic)
311 if (notify_die(DIE_NMIWATCHDOG, str, regs, 0, 2, SIGINT) == NOTIFY_STOP)
315 * We are in trouble anyway, lets at least try
316 * to get a message out.
318 flags = oops_begin();
319 printk(KERN_EMERG "%s", str);
320 printk(" on CPU%d, ip %08lx, registers:\n",
321 smp_processor_id(), regs->ip);
322 show_registers(regs);
323 oops_end(flags, regs, 0);
324 if (do_panic || panic_on_oops)
325 panic("Non maskable interrupt");
331 static int __init oops_setup(char *s)
335 if (!strcmp(s, "panic"))
339 early_param("oops", oops_setup);
341 static int __init kstack_setup(char *s)
345 kstack_depth_to_print = simple_strtoul(s, NULL, 0);
348 early_param("kstack", kstack_setup);
350 static int __init code_bytes_setup(char *s)
352 code_bytes = simple_strtoul(s, NULL, 0);
353 if (code_bytes > 8192)
358 __setup("code_bytes=", code_bytes_setup);