- more 2.6.17 port work (still does not build)
[linux-flexiantxendom0-3.2.10.git] / arch / ia64 / 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 #include <asm/sections.h>
21
22 /*
23  * bt_print_one
24  *
25  *      Print one back trace entry.
26  *
27  * Inputs:
28  *      ip      Current program counter.
29  *      symtab  Information about symbol that ip falls within.
30  *      ar      Activation record for this frame.
31  *      argcount Maximum number of arguments to print.
32  * Outputs:
33  *      None.
34  * Returns:
35  *      None.
36  * Locking:
37  *      None.
38  * Remarks:
39  *      None.
40  */
41
42 static void
43 bt_print_one(kdb_machreg_t ip, const kdb_ar_t *ar,
44              const kdb_symtab_t *symtab, int argcount,
45              struct unw_frame_info *info /* FIXME: should be part of ar for ia64 */)
46 {
47         int btsymarg = 0;               /* Convert arguments to symbols */
48         int btsp = 0;                   /* Print stack and backing store pointers */
49         int nosect = 0;                 /* Suppress section data */
50         kdb_machreg_t sp, bsp, cfm;     /* FIXME: should be part of ar for ia64 */
51
52         kdbgetintenv("BTSYMARG", &btsymarg);
53         kdbgetintenv("BTSP", &btsp);
54         kdbgetintenv("NOSECT", &nosect);
55
56         unw_get_sp(info, &sp);          /* FIXME: should be part of ar for ia64 */
57         unw_get_bsp(info, &bsp);        /* FIXME: should be part of ar for ia64 */
58         unw_get_cfm(info, &cfm);        /* FIXME: info/cfm should be part of ar for ia64 */
59         kdb_symbol_print(ip, symtab, KDB_SP_VALUE|KDB_SP_NEWLINE);
60         /* FIXME: number of args should be set in prologue code */
61         ((kdb_ar_t *)ar)->args = (cfm >> 7) & 0x7f;     /* sol */
62         if (!ar->args)
63                 ((kdb_ar_t *)ar)->args = cfm & 0x7f;    /* no in/local, use sof instead */
64         if (argcount && ar->args) {
65                 int i, argc = ar->args;
66
67                 kdb_printf("        args (");
68                 if (argc > argcount)
69                         argc = argcount;
70
71                 for(i = 0; i < argc; i++){
72                         /* FIXME: prologue code should extract arguments */
73                         kdb_machreg_t arg;
74                         char nat;
75                         if (unw_access_gr(info, i+32, &arg, &nat, 0))
76                                 arg = 0;
77
78                         if (i)
79                                 kdb_printf(", ");
80                         kdb_printf("0x%lx", arg);
81                 }
82                 kdb_printf(")\n");
83                 if (btsymarg) {
84                         kdb_symtab_t    arg_symtab;
85                         kdb_machreg_t   arg;
86                         for(i = 0; i < argc; i++){
87                                 /* FIXME: prologue code should extract arguments */
88                                 char nat;
89                                 if (unw_access_gr(info, i+32, &arg, &nat, 0))
90                                         arg = 0;
91                                 if (kdbnearsym(arg, &arg_symtab)) {
92                                         kdb_printf("        arg %d ", i);
93                                         kdb_symbol_print(arg, &arg_symtab, KDB_SP_DEFAULT|KDB_SP_NEWLINE);
94                                 }
95                         }
96                 }
97         }
98         if (symtab->sym_name) {
99                 if (!nosect) {
100                         kdb_printf("        %s", symtab->mod_name);
101                         if (symtab->sec_name)
102                                 kdb_printf(" %s 0x%lx", symtab->sec_name, symtab->sec_start);
103                         kdb_printf(" 0x%lx", symtab->sym_start);
104                         if (symtab->sym_end)
105                                 kdb_printf(" 0x%lx", symtab->sym_end);
106                         kdb_printf("\n");
107                 }
108                 if (strncmp(symtab->sym_name, "ia64_spinlock_contention", 24) == 0) {
109                         kdb_machreg_t r31;
110                         char nat;
111                         kdb_printf("        r31 (spinlock address) ");
112                         if (unw_access_gr(info, 31, &r31, &nat, 0))
113                                 r31 = 0;
114                         kdb_symbol_print(r31, NULL, KDB_SP_VALUE|KDB_SP_NEWLINE);
115                 }
116         }
117         if (btsp)
118                 kdb_printf("        sp 0x%016lx bsp 0x%016lx cfm 0x%016lx info->pfs_loc 0x%016lx 0x%016lx\n",
119                                 sp, bsp, cfm, (u64) info->pfs_loc, info->pfs_loc ? *(info->pfs_loc) : 0);
120 }
121
122 /*
123  * kdba_bt_stack
124  *
125  *      Unwind the ia64 backtrace for a specified process.
126  *
127  * Inputs:
128  *      argcount
129  *      p       Pointer to task structure to unwind.
130  * Outputs:
131  *      None.
132  * Returns:
133  *      zero for success, a kdb diagnostic if error
134  * Locking:
135  *      none.
136  * Remarks:
137  *      none.
138  */
139
140 static int
141 kdba_bt_stack(int argcount, const struct task_struct *p)
142 {
143         kdb_symtab_t symtab;
144         kdb_ar_t ar;
145         struct unw_frame_info info;     /* FIXME: should be part of ar */
146         struct switch_stack *sw;        /* FIXME: should be part of ar */
147         struct pt_regs *regs = NULL;    /* FIXME: should be part of ar */
148         int count = 0;
149         int btsp = 0;                   /* Backtrace the kdb code as well */
150         u64 *prev_pfs_loc = NULL;
151         extern char __attribute__ ((weak)) ia64_spinlock_contention_pre3_4[];
152         extern char __attribute__ ((weak)) ia64_spinlock_contention_pre3_4_end[];
153
154         /* FIXME: All the arch specific code should be in activation records, not here */
155         memset(&ar, 0, sizeof(ar));
156
157         /*
158          * Upon entering kdb_main_loop, the stack frame looks like this:
159          *
160          *      +---------------------+
161          *      |   struct pt_regs    |
162          *      +---------------------+
163          *      |                     |
164          *      |   kernel stack      |
165          *      |                     |
166          *      +=====================+ <--- top of stack upon entering kdb
167          *      |   struct pt_regs    |
168          *      +---------------------+
169          *      |                     |
170          *      |   kdb stack         |
171          *      |                     |
172          *      +---------------------+
173          *      | struct switch_stack |
174          *      +=====================+ <--- kdb_running_process[cpu].arch.sw from do_kdba_main_loop
175          *
176          * When looking at another process, we do not have the address of the
177          * current pt_regs, it is NULL.  If the process has saved its state, use
178          * that pt_regs instead.
179          */
180
181         kdbgetintenv("BTSP", &btsp);
182
183         if (kdb_task_has_cpu(p)) {
184                 struct kdb_running_process *krp = kdb_running_process + kdb_process_cpu(p);
185                 if (krp->seqno) {
186                         sw = krp->arch.sw;
187                         regs = krp->regs;
188                 }
189                 else
190                         sw = NULL;
191         }
192         else {
193                 /* Not running, assume blocked */
194                 sw = (struct switch_stack *) (p->thread.ksp + 16);
195         }
196         if (!sw) {
197                 kdb_printf("Process does not have a switch_stack, cannot backtrace\n");
198                 kdb_ps1(p);
199                 return 0;
200         }
201
202         unw_init_frame_info(&info, (struct task_struct *)p, sw);        /* FIXME: should be using activation records */
203
204         /* If we have the address of pt_regs, suppress backtrace on the frames below
205          * pt_regs.  No point in displaying kdb itself, unless the user is debugging
206          * the unwinder using set BTSP=1.
207          */
208         if (regs && !btsp) {
209                 kdb_machreg_t sp;               /* FIXME: should be part of ar for ia64 */
210                 if (user_mode(regs)) {
211                         kdb_printf("Process was interrupted in user mode, no backtrace available\n");
212                         return 0;
213                 }
214                 do {
215                         unw_get_sp(&info, &sp);
216                         if (sp >= (kdb_machreg_t)regs)
217                                 break;
218                 } while (unw_unwind(&info) >= 0 && count++ < 200);      /* FIXME: should be using activation records */
219         }
220
221         do {
222                 kdb_machreg_t ip;
223
224                 /* Avoid unsightly console message from unw_unwind() when attempting
225                  * to unwind through the Interrupt Vector Table which has no unwind
226                  * information.  dispatch_illegal_op_fault() is an exception, it sits
227                  * in the 0x3c00 slot.
228                  */
229                 if (info.ip >= (u64)__start_ivt_text && info.ip < (u64)__end_ivt_text) {
230                         if (info.ip < (u64)__start_ivt_text + 0x3c00 ||
231                             info.ip >= (u64)__start_ivt_text + 0x4000)
232                                 return 0;
233                 }
234
235                 /* WAR for spinlock contention from leaf functions.  ia64_spinlock_contention_pre3_4
236                  * has ar.pfs == r0.  Leaf functions do not modify ar.pfs so ar.pfs remains
237                  * as 0, stopping the backtrace.  Record the previous ar.pfs when the current
238                  * IP is in ia64_spinlock_contention_pre3_4 then unwind, if pfs_loc has not changed
239                  * after unwind then use pt_regs.ar_pfs which is where the real ar.pfs is for
240                  * leaf functions.
241                  */
242                 if (prev_pfs_loc && regs && info.pfs_loc == prev_pfs_loc)
243                         info.pfs_loc = &regs->ar_pfs;
244                 prev_pfs_loc = (info.ip >= (u64)ia64_spinlock_contention_pre3_4 &&
245                                 info.ip < (u64)ia64_spinlock_contention_pre3_4_end) ?
246                                info.pfs_loc : NULL;
247
248                 unw_get_ip(&info, &ip); /* FIXME: should be using activation records */
249                 if (ip == 0)
250                         break;
251
252                 kdbnearsym(ip, &symtab);
253                 if (!symtab.sym_name) {
254                         kdb_printf("0x%0*lx - No name.  May be an area that has no unwind data\n",
255                                 (int)(2*sizeof(ip)), ip);
256                         return 0;
257                 }
258                 bt_print_one(ip, &ar, &symtab, argcount, &info);
259         } while (unw_unwind(&info) >= 0 && count++ < 200);      /* FIXME: should be using activation records */
260         if (count >= 200)
261                 kdb_printf("bt truncated, count limit reached\n");
262
263         return 0;
264 }
265
266 int
267 kdba_bt_address(kdb_machreg_t addr, int argcount)
268 {
269         kdb_printf("Backtrace from a stack address is not supported on ia64\n");
270         return KDB_NOTIMP;
271 }
272
273 /*
274  * kdba_bt_process
275  *
276  *      Do a backtrace for a specified process.
277  *
278  * Inputs:
279  *      p       Struct task pointer extracted by 'bt' command.
280  *      argcount
281  * Outputs:
282  *      None.
283  * Returns:
284  *      zero for success, a kdb diagnostic if error
285  * Locking:
286  *      none.
287  */
288
289 int
290 kdba_bt_process(const struct task_struct *p, int argcount)
291 {
292         return kdba_bt_stack(argcount, p);
293 }