- more 2.6.17 port work (still does not build)
[linux-flexiantxendom0-3.2.10.git] / arch / x86_64 / kdb / kdba_bt.c
1 /*
2  * Kernel Debugger Architecture Dependent Stack Traceback
3  *
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
6  * for more details.
7  *
8  * Copyright (c) 1999-2004 Silicon Graphics, Inc.  All Rights Reserved.
9  */
10
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>
20
21 #ifdef CONFIG_FRAME_POINTER
22 #define RFPSTR  "RBP"
23 #define RFP     rbp
24 #define NOBP    0
25 #else
26 #define RFPSTR  "RSP"
27 #define RFP     rsp
28 #define NOBP    rsp
29 #endif
30
31 /*
32  * bt_print_one
33  *
34  *      Print one back trace entry.
35  *
36  * Inputs:
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.
43  * Outputs:
44  *      None.
45  * Returns:
46  *      None.
47  * Locking:
48  *      None.
49  * Remarks:
50  *      None.
51  */
52
53 static void
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)
56 {
57         int     btsymarg = 0;
58         int     nosect = 0;
59         kdb_machreg_t word;
60
61         kdbgetintenv("BTSYMARG", &btsymarg);
62         kdbgetintenv("NOSECT", &nosect);
63
64         if (efp)
65                 kdb_printf("0x%08lx", efp);
66         else
67                 kdb_printf("          ");
68         kdb_symbol_print(rip, symtab, KDB_SP_SPACEB|KDB_SP_VALUE);
69         if (argcount && ar->args) {
70                 int i, argc = ar->args / 8;
71
72                 kdb_printf(" (");
73                 if (argc > argcount)
74                         argc = argcount;
75
76                 for(i=1; i<=argc; i++){
77                         kdb_machreg_t argp = ar->arg0 - ar->args + 8*i;
78
79                         if (i != 1)
80                                 kdb_printf(", ");
81                         kdb_getword(&word, argp, sizeof(word));
82                         kdb_printf("0x%lx", word);
83                 }
84                 kdb_printf(")");
85         }
86         if (symtab->sym_name) {
87                 if (!nosect) {
88                         kdb_printf("\n");
89                         kdb_printf("                                  %s %s 0x%lx 0x%lx 0x%lx",
90                                 symtab->mod_name,
91                                 symtab->sec_name,
92                                 symtab->sec_start,
93                                 symtab->sym_start,
94                                 symtab->sym_end);
95                 }
96         }
97         kdb_printf("\n");
98         if (argcount && ar->args && btsymarg) {
99                 int i, argc = ar->args / 8;
100                 kdb_symtab_t    arg_symtab;
101                 kdb_machreg_t   arg;
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)) {
106                                 kdb_printf("                               ");
107                                 kdb_symbol_print(arg, &arg_symtab, KDB_SP_DEFAULT|KDB_SP_NEWLINE);
108                         }
109                 }
110         }
111 }
112
113 /*
114  * kdba_bt_stack
115  *
116  * Inputs:
117  *      addr    Pointer to Address provided to 'bt' command, if any.
118  *      argcount
119  *      p       Pointer to task for 'btp' command.
120  * Outputs:
121  *      None.
122  * Returns:
123  *      zero for success, a kdb diagnostic if error
124  * Locking:
125  *      none.
126  * Remarks:
127  *      mds comes in handy when examining the stack to do a manual
128  *      traceback.
129  */
130
131 static int
132 kdba_bt_stack(kdb_machreg_t addr, int argcount, const struct task_struct *p)
133 {
134         extern void thread_return(void);
135         kdb_ar_t        ar;
136         kdb_machreg_t   rip, rsp, rbp, ss, cs;
137         kdb_symtab_t    symtab;
138
139         /*
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.
144          *
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.
148          */
149         if (addr) {
150                 rip = 0;
151                 rbp = 0;
152                 rsp = addr;
153                 cs  = __KERNEL_CS;      /* have to assume kernel space */
154         } else {
155                 if (task_curr(p)) {
156                         struct kdb_running_process *krp = kdb_running_process + task_cpu(p);
157                         struct pt_regs *regs;
158
159                         if (!krp->seqno) {
160                                 kdb_printf("Process did not save state, cannot backtrace\n");
161                                 kdb_ps1(p);
162                                 return 0;
163                         }
164                         regs = krp->regs;
165                         if (KDB_NULL_REGS(regs))
166                                 return KDB_BADREG;
167                         kdba_getregcontents("rip", regs, &rip);
168                         kdba_getregcontents("rbp", regs, &rbp);
169                         kdba_getregcontents("rsp", regs, &rsp);
170                         kdba_getregcontents("cs", regs, &cs);
171                 }
172                 else {
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.
176                          */
177                         rsp = p->thread.rsp;
178                         /*
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.
188                          */
189                         rbp = *(unsigned long *)rsp;
190                         rip = (kdb_machreg_t)&thread_return-1;
191                         rsp = rsp + 16;
192                         cs  = __KERNEL_CS;
193                 }
194         }
195         ss = rsp & -THREAD_SIZE;
196
197         if ((cs & 0xffff) != __KERNEL_CS) {
198                 kdb_printf("Stack is not in kernel space, backtrace not available\n");
199                 return 0;
200         }
201
202         kdb_printf(RFPSTR "           RIP                Function (args)\n");
203
204         /*
205          * Run through the activation records and print them.
206          */
207
208         while (1) {
209                 kdb_ar_t save_ar = ar;
210                 kdbnearsym(rip, &symtab);
211                 if (!kdb_get_next_ar(rsp, symtab.sym_start, rip, rbp, ss,
212                         &ar, &symtab)) {
213                         break;
214                 }
215
216                 if (strncmp(".text.lock.", symtab.sym_name, 11) == 0) {
217                         /*
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.
226                          */
227                         unsigned char inst;
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;
233
234                         kdb_di.fprintf_func = &kdb_dis_fprintf_dummy;
235                         while((length = kdba_id_printinsn(realrip, &kdb_di)) > 0) {
236                                 kdb_getarea(inst, realrip);
237                                 offsize = 0;
238                                 switch (inst) {
239                                 case 0xeb:      /* jmp with 1 byte offset */
240                                         offsize = 1-4;
241                                         /* drop through */
242                                 case 0xe9:      /* jmp with 4 byte offset */
243                                         offsize += 4;
244                                         kdb_getword(&offset, realrip+1, offsize);
245                                         break;
246                                 default:
247                                         realrip += length;      /* next instruction */
248                                         break;
249                                 }
250                                 if (offsize)
251                                         break;
252                         }
253                         kdb_di.fprintf_func = save_fprintf_func;
254
255                         if (offsize) {
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 */
261                                         rip = realrip;
262                                         ar = save_ar;   /* lock text does not consume an activation frame */
263                                         continue;
264                                 }
265                         }
266                 }
267
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) {
271                                 /*
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.
276                                  */
277                                 ar.start = ar.end + 11*4;
278                         }
279                         if (strcmp("error_code", symtab.sym_name) == 0) {
280                                 /*
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.
286                                  */
287                                 ar.start = ar.end + 13*4;
288                         }
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);
295                         rbp = 0;
296                         rsp = ar.start;
297                         if ((((struct pt_regs *)(ar.end))->cs & 0xffff) != __KERNEL_CS) {
298                                 kdb_printf("Interrupt from user space, end of kernel trace\n");
299                                 break;
300                         }
301                         continue;
302                 }
303
304                 bt_print_one(rip, RFP, &ar, &symtab, argcount);
305
306                 if (ar.ret == 0)
307                         break;  /* End of frames */
308                 rip = ar.ret;
309                 rbp = ar.oldfp;
310                 rsp = ar.start;
311         }
312
313         return 0;
314 }
315
316 /*
317  * kdba_bt_address
318  *
319  *      Do a backtrace starting at a specified stack address.  Use this if the
320  *      heuristics get the i386 stack decode wrong.
321  *
322  * Inputs:
323  *      addr    Address provided to 'bt' command.
324  *      argcount
325  * Outputs:
326  *      None.
327  * Returns:
328  *      zero for success, a kdb diagnostic if error
329  * Locking:
330  *      none.
331  * Remarks:
332  *      mds %esp comes in handy when examining the stack to do a manual
333  *      traceback.
334  */
335
336 int
337 kdba_bt_address(kdb_machreg_t addr, int argcount)
338 {
339         return kdba_bt_stack(addr, argcount, NULL);
340 }
341
342 /*
343  * kdba_bt_process
344  *
345  *      Do a backtrace for a specified process.
346  *
347  * Inputs:
348  *      p       Struct task pointer extracted by 'bt' command.
349  *      argcount
350  * Outputs:
351  *      None.
352  * Returns:
353  *      zero for success, a kdb diagnostic if error
354  * Locking:
355  *      none.
356  */
357
358 int
359 kdba_bt_process(const struct task_struct *p, int argcount)
360 {
361         return kdba_bt_stack(0, argcount, p);
362 }