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
);
33 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
35 print_ftrace_graph_addr(unsigned long addr
, void *data
,
36 const struct stacktrace_ops
*ops
,
37 struct thread_info
*tinfo
, int *graph
)
39 struct task_struct
*task
= tinfo
->task
;
40 unsigned long ret_addr
;
41 int index
= task
->curr_ret_stack
;
43 if (addr
!= (unsigned long)return_to_handler
)
46 if (!task
->ret_stack
|| index
< *graph
)
50 ret_addr
= task
->ret_stack
[index
].ret
;
52 ops
->address(data
, ret_addr
, 1);
58 print_ftrace_graph_addr(unsigned long addr
, void *data
,
59 const struct stacktrace_ops
*ops
,
60 struct thread_info
*tinfo
, int *graph
)
65 * x86-64 can have up to three kernel stacks:
68 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
71 static inline int valid_stack_ptr(struct thread_info
*tinfo
,
72 void *p
, unsigned int size
, void *end
)
76 if (p
< end
&& p
>= (end
-THREAD_SIZE
))
81 return p
> t
&& p
< t
+ THREAD_SIZE
- size
;
85 print_context_stack(struct thread_info
*tinfo
,
86 unsigned long *stack
, unsigned long bp
,
87 const struct stacktrace_ops
*ops
, void *data
,
88 unsigned long *end
, int *graph
)
90 struct stack_frame
*frame
= (struct stack_frame
*)bp
;
92 while (valid_stack_ptr(tinfo
, stack
, sizeof(*stack
), end
)) {
96 if (__kernel_text_address(addr
)) {
97 if ((unsigned long) stack
== bp
+ sizeof(long)) {
98 ops
->address(data
, addr
, 1);
99 frame
= frame
->next_frame
;
100 bp
= (unsigned long) frame
;
102 ops
->address(data
, addr
, bp
== 0);
104 print_ftrace_graph_addr(addr
, data
, ops
, tinfo
, graph
);
113 print_trace_warning_symbol(void *data
, char *msg
, unsigned long symbol
)
116 print_symbol(msg
, symbol
);
120 static void print_trace_warning(void *data
, char *msg
)
122 printk("%s%s\n", (char *)data
, msg
);
125 static int print_trace_stack(void *data
, char *name
)
127 printk("%s <%s> ", (char *)data
, name
);
132 * Print one address/symbol entries per line.
134 static void print_trace_address(void *data
, unsigned long addr
, int reliable
)
136 touch_nmi_watchdog();
138 printk_address(addr
, reliable
);
141 static const struct stacktrace_ops print_trace_ops
= {
142 .warning
= print_trace_warning
,
143 .warning_symbol
= print_trace_warning_symbol
,
144 .stack
= print_trace_stack
,
145 .address
= print_trace_address
,
149 show_trace_log_lvl(struct task_struct
*task
, struct pt_regs
*regs
,
150 unsigned long *stack
, unsigned long bp
, char *log_lvl
)
152 printk("%sCall Trace:\n", log_lvl
);
153 dump_trace(task
, regs
, stack
, bp
, &print_trace_ops
, log_lvl
);
156 void show_trace(struct task_struct
*task
, struct pt_regs
*regs
,
157 unsigned long *stack
, unsigned long bp
)
159 show_trace_log_lvl(task
, regs
, stack
, bp
, "");
162 void show_stack(struct task_struct
*task
, unsigned long *sp
)
164 show_stack_log_lvl(task
, NULL
, sp
, 0, "");
168 * The architecture-independent dump_stack generator
170 void dump_stack(void)
172 unsigned long bp
= 0;
175 #ifdef CONFIG_FRAME_POINTER
180 printk("Pid: %d, comm: %.20s %s %s %.*s\n",
181 current
->pid
, current
->comm
, print_tainted(),
182 init_utsname()->release
,
183 (int)strcspn(init_utsname()->version
, " "),
184 init_utsname()->version
);
185 show_trace(NULL
, NULL
, &stack
, bp
);
187 EXPORT_SYMBOL(dump_stack
);
189 static raw_spinlock_t die_lock
= __RAW_SPIN_LOCK_UNLOCKED
;
190 static int die_owner
= -1;
191 static unsigned int die_nest_count
;
193 unsigned __kprobes
long oops_begin(void)
200 /* racy, but better than risking deadlock. */
201 raw_local_irq_save(flags
);
202 cpu
= smp_processor_id();
203 if (!__raw_spin_trylock(&die_lock
)) {
204 if (cpu
== die_owner
)
205 /* nested oops. should stop eventually */;
207 __raw_spin_lock(&die_lock
);
216 void __kprobes
oops_end(unsigned long flags
, struct pt_regs
*regs
, int signr
)
218 if (regs
&& kexec_should_crash(current
))
223 add_taint(TAINT_DIE
);
226 /* Nest count reaches zero, release the lock. */
227 __raw_spin_unlock(&die_lock
);
228 raw_local_irq_restore(flags
);
234 panic("Fatal exception in interrupt");
236 panic("Fatal exception");
240 int __kprobes
__die(const char *str
, struct pt_regs
*regs
, long err
)
246 printk(KERN_EMERG
"%s: %04lx [#%d] ", str
, err
& 0xffff, ++die_counter
);
247 #ifdef CONFIG_PREEMPT
253 #ifdef CONFIG_DEBUG_PAGEALLOC
254 printk("DEBUG_PAGEALLOC");
257 sysfs_printk_last_file();
258 if (notify_die(DIE_OOPS
, str
, regs
, err
,
259 current
->thread
.trap_no
, SIGSEGV
) == NOTIFY_STOP
)
262 show_registers(regs
);
264 sp
= (unsigned long) (®s
->sp
);
266 if (user_mode(regs
)) {
268 ss
= regs
->ss
& 0xffff;
270 printk(KERN_EMERG
"EIP: [<%08lx>] ", regs
->ip
);
271 print_symbol("%s", regs
->ip
);
272 printk(" SS:ESP %04x:%08lx\n", ss
, sp
);
274 /* Executive summary in case the oops scrolled away */
275 printk(KERN_ALERT
"RIP ");
276 printk_address(regs
->ip
, 1);
277 printk(" RSP <%016lx>\n", regs
->sp
);
283 * This is gone through when something in the kernel has done something bad
284 * and is about to be terminated:
286 void die(const char *str
, struct pt_regs
*regs
, long err
)
288 unsigned long flags
= oops_begin();
291 if (!user_mode_vm(regs
))
292 report_bug(regs
->ip
, regs
);
294 if (__die(str
, regs
, err
))
296 oops_end(flags
, regs
, sig
);
299 void notrace __kprobes
300 die_nmi(char *str
, struct pt_regs
*regs
, int do_panic
)
304 if (notify_die(DIE_NMIWATCHDOG
, str
, regs
, 0, 2, SIGINT
) == NOTIFY_STOP
)
308 * We are in trouble anyway, lets at least try
309 * to get a message out.
311 flags
= oops_begin();
312 printk(KERN_EMERG
"%s", str
);
313 printk(" on CPU%d, ip %08lx, registers:\n",
314 smp_processor_id(), regs
->ip
);
315 show_registers(regs
);
316 oops_end(flags
, regs
, 0);
317 if (do_panic
|| panic_on_oops
)
318 panic("Non maskable interrupt");
324 static int __init
oops_setup(char *s
)
328 if (!strcmp(s
, "panic"))
332 early_param("oops", oops_setup
);
334 static int __init
kstack_setup(char *s
)
338 kstack_depth_to_print
= simple_strtoul(s
, NULL
, 0);
341 early_param("kstack", kstack_setup
);
343 static int __init
code_bytes_setup(char *s
)
345 code_bytes
= simple_strtoul(s
, NULL
, 0);
346 if (code_bytes
> 8192)
351 __setup("code_bytes=", code_bytes_setup
);