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/hardirq.h>
9 #include <linux/kdebug.h>
10 #include <linux/export.h>
11 #include <linux/ptrace.h>
12 #include <linux/kexec.h>
13 #include <linux/sysfs.h>
14 #include <linux/bug.h>
15 #include <linux/nmi.h>
17 #include <asm/stacktrace.h>
19 void stack_type_str(enum stack_type type, const char **begin, const char **end)
23 case STACK_TYPE_SOFTIRQ:
33 static bool in_hardirq_stack(unsigned long *stack, struct stack_info *info)
35 unsigned long *begin = (unsigned long *)this_cpu_read(hardirq_stack);
36 unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
39 * This is a software stack, so 'end' can be a valid stack pointer.
40 * It just means the stack is empty.
42 if (stack < begin || stack > end)
45 info->type = STACK_TYPE_IRQ;
50 * See irq_32.c -- the next stack pointer is stored at the beginning of
53 info->next_sp = (unsigned long *)*begin;
58 static bool in_softirq_stack(unsigned long *stack, struct stack_info *info)
60 unsigned long *begin = (unsigned long *)this_cpu_read(softirq_stack);
61 unsigned long *end = begin + (THREAD_SIZE / sizeof(long));
64 * This is a software stack, so 'end' can be a valid stack pointer.
65 * It just means the stack is empty.
67 if (stack < begin || stack > end)
70 info->type = STACK_TYPE_SOFTIRQ;
75 * The next stack pointer is stored at the beginning of the stack.
78 info->next_sp = (unsigned long *)*begin;
83 int get_stack_info(unsigned long *stack, struct task_struct *task,
84 struct stack_info *info, unsigned long *visit_mask)
89 task = task ? : current;
91 if (in_task_stack(stack, task, info))
97 if (in_hardirq_stack(stack, info))
100 if (in_softirq_stack(stack, info))
104 info->type = STACK_TYPE_UNKNOWN;
108 void dump_trace(struct task_struct *task, struct pt_regs *regs,
109 unsigned long *stack, unsigned long bp,
110 const struct stacktrace_ops *ops, void *data)
112 unsigned long visit_mask = 0;
115 task = task ? : current;
116 stack = stack ? : get_stack_pointer(task, regs);
117 bp = bp ? : (unsigned long)get_frame_pointer(task, regs);
120 const char *begin_str, *end_str;
121 struct stack_info info;
123 if (get_stack_info(stack, task, &info, &visit_mask))
126 stack_type_str(info.type, &begin_str, &end_str);
128 if (begin_str && ops->stack(data, begin_str) < 0)
131 bp = ops->walk_stack(task, stack, bp, ops, data, &info, &graph);
133 if (end_str && ops->stack(data, end_str) < 0)
136 stack = info.next_sp;
138 touch_nmi_watchdog();
141 EXPORT_SYMBOL(dump_trace);
144 show_stack_log_lvl(struct task_struct *task, struct pt_regs *regs,
145 unsigned long *sp, unsigned long bp, char *log_lvl)
147 unsigned long *stack;
150 sp = sp ? : get_stack_pointer(task, regs);
153 for (i = 0; i < kstack_depth_to_print; i++) {
154 if (kstack_end(stack))
156 if ((i % STACKSLOTS_PER_LINE) == 0) {
159 printk("%s %08lx", log_lvl, *stack++);
161 pr_cont(" %08lx", *stack++);
162 touch_nmi_watchdog();
165 show_trace_log_lvl(task, regs, sp, bp, log_lvl);
169 void show_regs(struct pt_regs *regs)
173 show_regs_print_info(KERN_EMERG);
174 __show_regs(regs, !user_mode(regs));
177 * When in-kernel, we also print out the stack and code at the
178 * time of the fault..
180 if (!user_mode(regs)) {
181 unsigned int code_prologue = code_bytes * 43 / 64;
182 unsigned int code_len = code_bytes;
186 pr_emerg("Stack:\n");
187 show_stack_log_lvl(NULL, regs, NULL, 0, KERN_EMERG);
191 ip = (u8 *)regs->ip - code_prologue;
192 if (ip < (u8 *)PAGE_OFFSET || probe_kernel_address(ip, c)) {
193 /* try starting at IP */
195 code_len = code_len - code_prologue + 1;
197 for (i = 0; i < code_len; i++, ip++) {
198 if (ip < (u8 *)PAGE_OFFSET ||
199 probe_kernel_address(ip, c)) {
200 pr_cont(" Bad EIP value.");
203 if (ip == (u8 *)regs->ip)
204 pr_cont(" <%02x>", c);
212 int is_valid_bugaddr(unsigned long ip)
216 if (ip < PAGE_OFFSET)
218 if (probe_kernel_address((unsigned short *)ip, ud2))
221 return ud2 == 0x0b0f;