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>
20 #define N_EXCEPTION_STACKS_END \
21 (N_EXCEPTION_STACKS + DEBUG_STKSZ/EXCEPTION_STKSZ - 2)
23 static char x86_stack_ids
[][8] = {
24 [ DEBUG_STACK
-1 ] = "#DB",
25 [ NMI_STACK
-1 ] = "NMI",
26 [ DOUBLEFAULT_STACK
-1 ] = "#DF",
27 [ MCE_STACK
-1 ] = "#MC",
28 #if DEBUG_STKSZ > EXCEPTION_STKSZ
29 [ N_EXCEPTION_STACKS
...
30 N_EXCEPTION_STACKS_END
] = "#DB[?]"
34 static unsigned long *in_exception_stack(unsigned cpu
, unsigned long stack
,
35 unsigned *usedp
, char **idp
)
40 * Iterate over all exception stacks, and figure out whether
41 * 'stack' is in one of them:
43 for (k
= 0; k
< N_EXCEPTION_STACKS
; k
++) {
44 unsigned long end
= per_cpu(orig_ist
, cpu
).ist
[k
];
46 * Is 'stack' above this exception frame's end?
47 * If yes then skip to the next frame.
52 * Is 'stack' above this exception frame's start address?
53 * If yes then we found the right frame.
55 if (stack
>= end
- EXCEPTION_STKSZ
) {
57 * Make sure we only iterate through an exception
58 * stack once. If it comes up for the second time
59 * then there's something wrong going on - just
60 * break out and return NULL:
62 if (*usedp
& (1U << k
))
65 *idp
= x86_stack_ids
[k
];
66 return (unsigned long *)end
;
69 * If this is a debug stack, and if it has a larger size than
70 * the usual exception stacks, then 'stack' might still
71 * be within the lower portion of the debug stack:
73 #if DEBUG_STKSZ > EXCEPTION_STKSZ
74 if (k
== DEBUG_STACK
- 1 && stack
>= end
- DEBUG_STKSZ
) {
75 unsigned j
= N_EXCEPTION_STACKS
- 1;
78 * Black magic. A large debug stack is composed of
79 * multiple exception stack entries, which we
80 * iterate through now. Dont look:
84 end
-= EXCEPTION_STKSZ
;
85 x86_stack_ids
[j
][4] = '1' +
86 (j
- N_EXCEPTION_STACKS
);
87 } while (stack
< end
- EXCEPTION_STKSZ
);
88 if (*usedp
& (1U << j
))
91 *idp
= x86_stack_ids
[j
];
92 return (unsigned long *)end
;
100 in_irq_stack(unsigned long *stack
, unsigned long *irq_stack
,
101 unsigned long *irq_stack_end
)
103 return (stack
>= irq_stack
&& stack
< irq_stack_end
);
113 static enum stack_type
114 analyze_stack(int cpu
, struct task_struct
*task
, unsigned long *stack
,
115 unsigned long **stack_end
, unsigned long *irq_stack
,
116 unsigned *used
, char **id
)
120 addr
= ((unsigned long)stack
& (~(THREAD_SIZE
- 1)));
121 if ((unsigned long)task_stack_page(task
) == addr
)
122 return STACK_IS_NORMAL
;
124 *stack_end
= in_exception_stack(cpu
, (unsigned long)stack
,
127 return STACK_IS_EXCEPTION
;
130 return STACK_IS_NORMAL
;
132 *stack_end
= irq_stack
;
133 irq_stack
-= (IRQ_STACK_SIZE
/ sizeof(long));
135 if (in_irq_stack(stack
, irq_stack
, *stack_end
))
138 return STACK_IS_UNKNOWN
;
142 * x86-64 can have up to three kernel stacks:
145 * severe exception (double fault, nmi, stack fault, debug, mce) hardware stack
148 void dump_trace(struct task_struct
*task
, struct pt_regs
*regs
,
149 unsigned long *stack
, unsigned long bp
,
150 const struct stacktrace_ops
*ops
, void *data
)
152 const unsigned cpu
= get_cpu();
153 unsigned long *irq_stack
= (unsigned long *)per_cpu(irq_stack_ptr
, cpu
);
158 task
= task
? : current
;
159 stack
= stack
? : get_stack_pointer(task
, regs
);
160 bp
= bp
? : (unsigned long)get_frame_pointer(task
, regs
);
163 * Print function call entries in all stacks, starting at the
164 * current stack address. If the stacks consist of nested
168 unsigned long *stack_end
;
169 enum stack_type stype
;
172 stype
= analyze_stack(cpu
, task
, stack
, &stack_end
,
173 irq_stack
, &used
, &id
);
175 /* Default finish unless specified to continue */
180 /* Break out early if we are on the thread stack */
181 case STACK_IS_NORMAL
:
184 case STACK_IS_EXCEPTION
:
186 if (ops
->stack(data
, id
) < 0)
189 bp
= ops
->walk_stack(task
, stack
, bp
, ops
,
190 data
, stack_end
, &graph
);
191 ops
->stack(data
, "EOE");
193 * We link to the next stack via the
194 * second-to-last pointer (index -2 to end) in the
197 stack
= (unsigned long *) stack_end
[-2];
203 if (ops
->stack(data
, "IRQ") < 0)
205 bp
= ops
->walk_stack(task
, stack
, bp
,
206 ops
, data
, stack_end
, &graph
);
208 * We link to the next stack (which would be
209 * the process stack normally) the last
210 * pointer (index -1 to end) in the IRQ stack:
212 stack
= (unsigned long *) (stack_end
[-1]);
214 ops
->stack(data
, "EOI");
218 case STACK_IS_UNKNOWN
:
219 ops
->stack(data
, "UNK");
225 * This handles the process stack:
227 bp
= ops
->walk_stack(task
, stack
, bp
, ops
, data
, NULL
, &graph
);
230 EXPORT_SYMBOL(dump_trace
);
233 show_stack_log_lvl(struct task_struct
*task
, struct pt_regs
*regs
,
234 unsigned long *sp
, unsigned long bp
, char *log_lvl
)
236 unsigned long *irq_stack_end
;
237 unsigned long *irq_stack
;
238 unsigned long *stack
;
243 cpu
= smp_processor_id();
245 irq_stack_end
= (unsigned long *)(per_cpu(irq_stack_ptr
, cpu
));
246 irq_stack
= irq_stack_end
- (IRQ_STACK_SIZE
/ sizeof(long));
248 sp
= sp
? : get_stack_pointer(task
, regs
);
251 for (i
= 0; i
< kstack_depth_to_print
; i
++) {
254 if (stack
>= irq_stack
&& stack
<= irq_stack_end
) {
255 if (stack
== irq_stack_end
) {
256 stack
= (unsigned long *) (irq_stack_end
[-1]);
260 if (kstack_end(stack
))
264 if (probe_kernel_address(stack
, word
))
267 if ((i
% STACKSLOTS_PER_LINE
) == 0) {
270 printk("%s %016lx", log_lvl
, word
);
272 pr_cont(" %016lx", word
);
275 touch_nmi_watchdog();
280 show_trace_log_lvl(task
, regs
, sp
, bp
, log_lvl
);
283 void show_regs(struct pt_regs
*regs
)
287 show_regs_print_info(KERN_DEFAULT
);
288 __show_regs(regs
, 1);
291 * When in-kernel, we also print out the stack and code at the
292 * time of the fault..
294 if (!user_mode(regs
)) {
295 unsigned int code_prologue
= code_bytes
* 43 / 64;
296 unsigned int code_len
= code_bytes
;
300 printk(KERN_DEFAULT
"Stack:\n");
301 show_stack_log_lvl(NULL
, regs
, NULL
, 0, KERN_DEFAULT
);
303 printk(KERN_DEFAULT
"Code: ");
305 ip
= (u8
*)regs
->ip
- code_prologue
;
306 if (ip
< (u8
*)PAGE_OFFSET
|| probe_kernel_address(ip
, c
)) {
307 /* try starting at IP */
309 code_len
= code_len
- code_prologue
+ 1;
311 for (i
= 0; i
< code_len
; i
++, ip
++) {
312 if (ip
< (u8
*)PAGE_OFFSET
||
313 probe_kernel_address(ip
, c
)) {
314 pr_cont(" Bad RIP value.");
317 if (ip
== (u8
*)regs
->ip
)
318 pr_cont("<%02x> ", c
);
326 int is_valid_bugaddr(unsigned long ip
)
330 if (__copy_from_user(&ud2
, (const void __user
*) ip
, sizeof(ud2
)))
333 return ud2
== 0x0b0f;
This page took 0.083525 seconds and 6 git commands to generate.