2 * Kernel Debugger Architecture Dependent Stack Traceback
4 * This file is subject to the terms and conditions of the GNU General Public
5 * License. See the file "COPYING" in the main directory of this archive
8 * Copyright (c) 1999-2004 Silicon Graphics, Inc. All Rights Reserved.
11 #include <linux/config.h>
12 #include <linux/ctype.h>
13 #include <linux/string.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/kallsyms.h>
17 #include <linux/kdb.h>
18 #include <linux/kdbprivate.h>
19 #include <asm/system.h>
21 #ifdef CONFIG_FRAME_POINTER
34 * Print one back trace entry.
37 * rip Current program counter, or return address.
38 * efp #ifdef CONFIG_FRAME_POINTER: Previous frame pointer rbp,
39 * 0 if not valid; #else: Stack pointer rsp when at rip.
40 * ar Activation record for this frame.
41 * symtab Information about symbol that rip falls within.
42 * argcount Maximum number of arguments to print.
54 bt_print_one(kdb_machreg_t rip, kdb_machreg_t efp, const kdb_ar_t *ar,
55 const kdb_symtab_t *symtab, int argcount)
61 kdbgetintenv("BTSYMARG", &btsymarg);
62 kdbgetintenv("NOSECT", &nosect);
65 kdb_printf("0x%08lx", efp);
68 kdb_symbol_print(rip, symtab, KDB_SP_SPACEB|KDB_SP_VALUE);
69 if (argcount && ar->args) {
70 int i, argc = ar->args / 8;
76 for(i=1; i<=argc; i++){
77 kdb_machreg_t argp = ar->arg0 - ar->args + 8*i;
81 kdb_getword(&word, argp, sizeof(word));
82 kdb_printf("0x%lx", word);
86 if (symtab->sym_name) {
89 kdb_printf(" %s %s 0x%lx 0x%lx 0x%lx",
98 if (argcount && ar->args && btsymarg) {
99 int i, argc = ar->args / 8;
100 kdb_symtab_t arg_symtab;
102 for(i=1; i<=argc; i++){
103 kdb_machreg_t argp = ar->arg0 - ar->args + 8*i;
104 kdb_getword(&arg, argp, sizeof(arg));
105 if (kdbnearsym(arg, &arg_symtab)) {
107 kdb_symbol_print(arg, &arg_symtab, KDB_SP_DEFAULT|KDB_SP_NEWLINE);
117 * addr Pointer to Address provided to 'bt' command, if any.
119 * p Pointer to task for 'btp' command.
123 * zero for success, a kdb diagnostic if error
127 * mds comes in handy when examining the stack to do a manual
132 kdba_bt_stack(kdb_machreg_t addr, int argcount, const struct task_struct *p)
134 extern void thread_return(void);
136 kdb_machreg_t rip, rsp, rbp, ss, cs;
140 * The caller may have supplied an address at which the
141 * stack traceback operation should begin. This address
142 * is assumed by this code to point to a return-address
143 * on the stack to be traced back.
145 * The end result of this will make it appear as if a function
146 * entitled '<unknown>' was called from the function which
147 * contains return-address.
153 cs = __KERNEL_CS; /* have to assume kernel space */
156 struct kdb_running_process *krp = kdb_running_process + task_cpu(p);
157 struct pt_regs *regs;
160 kdb_printf("Process did not save state, cannot backtrace\n");
165 if (KDB_NULL_REGS(regs))
167 kdba_getregcontents("rip", regs, &rip);
168 kdba_getregcontents("rbp", regs, &rbp);
169 kdba_getregcontents("rsp", regs, &rsp);
170 kdba_getregcontents("cs", regs, &cs);
173 /* Not on cpu, assume blocked. Blocked tasks do
174 * not have pt_regs. p->thread.rsp is set, rsp
175 * points to the rbp value, assume kernel space.
179 * The rip is no longer in the thread struct.
180 * We know that the stack value was saved in
181 * schedule near the label thread_return.
182 * Setting rip to thread_return-1 lets the
183 * stack trace find that we are in schedule
184 * and correctly decode its prologue. We
185 * extract the saved rbp and adjust the stack
186 * to undo the effects of the inline assembly
187 * code which switches the stack.
189 rbp = *(unsigned long *)rsp;
190 rip = (kdb_machreg_t)&thread_return-1;
195 ss = rsp & -THREAD_SIZE;
197 if ((cs & 0xffff) != __KERNEL_CS) {
198 kdb_printf("Stack is not in kernel space, backtrace not available\n");
202 kdb_printf(RFPSTR " RIP Function (args)\n");
205 * Run through the activation records and print them.
209 kdb_ar_t save_ar = ar;
210 kdbnearsym(rip, &symtab);
211 if (!kdb_get_next_ar(rsp, symtab.sym_start, rip, rbp, ss,
216 if (strncmp(".text.lock.", symtab.sym_name, 11) == 0) {
218 * Instructions in the .text.lock area are generated by
219 * the out of line code in lock handling, see
220 * include/asm-x86_64 semaphore.h and rwlock.h. There can
221 * be multiple instructions which eventually end with a
222 * jump back to the mainline code. Use the disassmebler
223 * to silently step through the code until we find the
224 * jump, resolve its destination and translate it to a
225 * symbol. Replace '.text.lock' with the symbol.
228 kdb_machreg_t offset = 0, realrip = rip;
229 int length, offsize = 0;
230 kdb_symtab_t lock_symtab;
231 /* Dummy out the disassembler print function */
232 fprintf_ftype save_fprintf_func = kdb_di.fprintf_func;
234 kdb_di.fprintf_func = &kdb_dis_fprintf_dummy;
235 while((length = kdba_id_printinsn(realrip, &kdb_di)) > 0) {
236 kdb_getarea(inst, realrip);
239 case 0xeb: /* jmp with 1 byte offset */
242 case 0xe9: /* jmp with 4 byte offset */
244 kdb_getword(&offset, realrip+1, offsize);
247 realrip += length; /* next instruction */
253 kdb_di.fprintf_func = save_fprintf_func;
256 realrip += 1 + offsize + (offsize == 1 ? (s8)offset : (s32)offset);
257 if (kdbnearsym(realrip, &lock_symtab)) {
258 /* Print the stext entry without args */
259 bt_print_one(rip, NOBP, &ar, &symtab, 0);
260 /* Point to mainline code */
262 ar = save_ar; /* lock text does not consume an activation frame */
268 if (strcmp("ret_from_intr", symtab.sym_name) == 0 ||
269 strcmp("error_code", symtab.sym_name) == 0) {
270 if (strcmp("ret_from_intr", symtab.sym_name) == 0) {
272 * Non-standard frame. ret_from_intr is
273 * preceded by 9 registers (ebx, ecx, edx, esi,
274 * edi, ebp, eax, ds, cs), original eax and the
275 * return address for a total of 11 words.
277 ar.start = ar.end + 11*4;
279 if (strcmp("error_code", symtab.sym_name) == 0) {
281 * Non-standard frame. error_code is preceded
282 * by two parameters (-> registers, error code),
283 * 9 registers (ebx, ecx, edx, esi, edi, ebp,
284 * eax, ds, cs), original eax and the return
285 * address for a total of 13 words.
287 ar.start = ar.end + 13*4;
289 /* Print the non-standard entry without args */
290 bt_print_one(rip, NOBP, &ar, &symtab, 0);
291 kdb_printf("Interrupt registers:\n");
292 kdba_dumpregs((struct pt_regs *)(ar.end), NULL, NULL);
293 /* Step the frame to the interrupted code */
294 kdb_getword(&rip, ar.start-8, 8);
297 if ((((struct pt_regs *)(ar.end))->cs & 0xffff) != __KERNEL_CS) {
298 kdb_printf("Interrupt from user space, end of kernel trace\n");
304 bt_print_one(rip, RFP, &ar, &symtab, argcount);
307 break; /* End of frames */
319 * Do a backtrace starting at a specified stack address. Use this if the
320 * heuristics get the i386 stack decode wrong.
323 * addr Address provided to 'bt' command.
328 * zero for success, a kdb diagnostic if error
332 * mds %esp comes in handy when examining the stack to do a manual
337 kdba_bt_address(kdb_machreg_t addr, int argcount)
339 return kdba_bt_stack(addr, argcount, NULL);
345 * Do a backtrace for a specified process.
348 * p Struct task pointer extracted by 'bt' command.
353 * zero for success, a kdb diagnostic if error
359 kdba_bt_process(const struct task_struct *p, int argcount)
361 return kdba_bt_stack(0, argcount, p);