]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - sys/sun4v/sun4v/db_trace.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / sys / sun4v / sun4v / db_trace.c
1 /*-
2  * Copyright (c) 2001 Jake Burkholder.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kdb.h>
33 #include <sys/linker_set.h>
34 #include <sys/proc.h>
35 #include <sys/stack.h>
36 #include <sys/sysent.h>
37
38 #include <vm/vm.h>
39 #include <vm/vm_page.h>
40 #include <vm/vm_map.h>
41
42 #include <machine/cpu.h>
43 #include <machine/pcb.h>
44 #include <machine/stack.h>
45 #include <machine/trap.h>
46 #include <machine/vmparam.h>
47
48 #include <ddb/ddb.h>
49 #include <ddb/db_access.h>
50 #include <ddb/db_sym.h>
51 #include <ddb/db_variables.h>
52 #include <ddb/db_watch.h>
53
54 static db_varfcn_t db_frame;
55
56 #define DB_OFFSET(x)    (db_expr_t *)offsetof(struct trapframe, x)
57 struct  db_variable db_regs[] = {
58         { "g0",         DB_OFFSET(tf_global[0]),        db_frame },
59         { "g1",         DB_OFFSET(tf_global[1]),        db_frame },
60         { "g2",         DB_OFFSET(tf_global[2]),        db_frame },
61         { "g3",         DB_OFFSET(tf_global[3]),        db_frame },
62         { "g4",         DB_OFFSET(tf_global[4]),        db_frame },
63         { "g5",         DB_OFFSET(tf_global[5]),        db_frame },
64         { "g6",         DB_OFFSET(tf_global[6]),        db_frame },
65         { "g7",         DB_OFFSET(tf_global[7]),        db_frame },
66         { "i0",         DB_OFFSET(tf_out[0]),           db_frame },
67         { "i1",         DB_OFFSET(tf_out[1]),           db_frame },
68         { "i2",         DB_OFFSET(tf_out[2]),           db_frame },
69         { "i3",         DB_OFFSET(tf_out[3]),           db_frame },
70         { "i4",         DB_OFFSET(tf_out[4]),           db_frame },
71         { "i5",         DB_OFFSET(tf_out[5]),           db_frame },
72         { "i6",         DB_OFFSET(tf_out[6]),           db_frame },
73         { "i7",         DB_OFFSET(tf_out[7]),           db_frame },
74         { "tnpc",       DB_OFFSET(tf_tnpc),             db_frame },
75         { "tpc",        DB_OFFSET(tf_tpc),              db_frame },
76         { "tstate",     DB_OFFSET(tf_tstate),           db_frame },
77 };
78 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
79
80 static int
81 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
82 {
83         uint64_t *reg;
84
85         if (kdb_frame == NULL)
86                 return (0);
87         reg = (uint64_t*)((uintptr_t)kdb_frame + (uintptr_t)vp->valuep);
88         if (op == DB_VAR_GET)
89                 *valuep = *reg;
90         else
91                 *reg = *valuep;
92         return (1);
93 }
94
95 /*
96  * User stack trace (debugging aid).
97  */
98 static void
99 db_utrace(struct thread *td, struct trapframe *tf, int count)
100 {
101         struct pcb *pcb;
102         db_addr_t sp, rsp, o7, pc;
103         int i, found;
104
105         pcb = td->td_pcb;
106         sp = db_get_value((db_addr_t)&tf->tf_sp, sizeof(tf->tf_sp), FALSE);
107         o7 = db_get_value((db_addr_t)&tf->tf_out[7], sizeof(tf->tf_out[7]),
108             FALSE);
109         pc = db_get_value((db_addr_t)&tf->tf_tpc, sizeof(tf->tf_tpc), FALSE);
110         db_printf("user trace: trap %%o7=%#lx\n", o7);
111         while (count-- && sp != 0 && !db_pager_quit) {
112                 db_printf("pc %#lx, sp %#lx\n", pc, sp);
113                 /* First, check whether the frame is in the pcb. */
114                 found = 0;
115                 for (i = 0; i < pcb->pcb_nsaved; i++) {
116                         if (pcb->pcb_rwsp[i] == sp) {
117                                 found = 1;
118                                 sp = pcb->pcb_rw[i].rw_in[6];
119                                 pc = pcb->pcb_rw[i].rw_in[7];
120                                 break;
121                         }
122                 }
123                 if (!found) {
124                         rsp = sp + SPOFF;
125                         sp = 0;
126                         if (copyin((void *)(rsp + offsetof(struct frame, fr_fp)),
127                             &sp, sizeof(sp)) != 0 ||
128                             copyin((void *)(rsp + offsetof(struct frame, fr_pc)),
129                             &pc, sizeof(pc)) != 0)
130                                 break;
131                 }
132         }
133         db_printf("done\n");
134 }
135
136 static int
137 db_print_trap(struct thread *td, struct trapframe *tf, int count)
138 {
139         struct proc *p;
140         const char *symname;
141         c_db_sym_t sym;
142         db_expr_t diff;
143         db_addr_t func;
144         db_addr_t tpc;
145         u_long type;
146 #if 0
147         u_long sfar;
148         u_long sfsr;
149         u_long tar;
150         u_long level;
151         u_long pil;
152 #endif
153         u_long code;
154         u_long o7;
155         int user;
156
157         type = 5;
158         p = td->td_proc;
159 #if 0
160         type = db_get_value((db_addr_t)&tf->tf_type,
161             sizeof(tf->tf_type), FALSE);
162         db_printf("-- %s", trap_msg[type & ~T_KERNEL]);
163 #endif
164         switch (type & ~T_KERNEL) {
165         case T_DATA_PROTECTION:
166 #if 0
167                 tar = (u_long)db_get_value((db_addr_t)&tf->tf_tar,
168                     sizeof(tf->tf_tar), FALSE);
169                 db_printf(" tar=%#lx", tar);
170 #endif
171                 /* fall through */
172         case T_DATA_EXCEPTION:
173         case T_INSTRUCTION_EXCEPTION:
174         case T_MEM_ADDRESS_NOT_ALIGNED:
175 #if 0
176                 sfar = (u_long)db_get_value((db_addr_t)&tf->tf_sfar,
177                     sizeof(tf->tf_sfar), FALSE);
178                 sfsr = (u_long)db_get_value((db_addr_t)&tf->tf_sfsr,
179                     sizeof(tf->tf_sfsr), FALSE);
180                 db_printf(" sfar=%#lx sfsr=%#lx", sfar, sfsr);
181 #endif
182                 break;
183         case T_DATA_MISS:
184         case T_INSTRUCTION_MISS:
185 #if 0
186                 tar = (u_long)db_get_value((db_addr_t)&tf->tf_tar,
187                     sizeof(tf->tf_tar), FALSE);
188                 db_printf(" tar=%#lx", tar);
189 #endif
190                 break;
191         case T_SYSCALL:
192                 code = db_get_value((db_addr_t)&tf->tf_global[1],
193                     sizeof(tf->tf_global[1]), FALSE);
194                 db_printf(" (%ld", code);
195                 if (code >= 0 && code < p->p_sysent->sv_size) {
196                         func = (db_addr_t)p->p_sysent->sv_table[code].sy_call;
197                         sym = db_search_symbol(func, DB_STGY_ANY, &diff);
198                         if (sym != DB_SYM_NULL && diff == 0) {
199                                 db_symbol_values(sym, &symname, NULL);
200                                 db_printf(", %s, %s", p->p_sysent->sv_name,
201                                     symname);
202                         }
203                         db_printf(")");
204                 }
205                 break;
206         case T_INTERRUPT:
207 #if 0
208                 level = (u_long)db_get_value((db_addr_t)&tf->tf_level,
209                     sizeof(tf->tf_level), FALSE);
210                 pil = (u_long)db_get_value((db_addr_t)&tf->tf_pil,
211                     sizeof(tf->tf_pil), FALSE);
212                 db_printf(" level=%#lx pil=%#lx", level, pil);
213 #endif
214                 break;
215         default:
216                 break;
217         }
218         o7 = (u_long)db_get_value((db_addr_t)&tf->tf_out[7],
219             sizeof(tf->tf_out[7]), FALSE);
220         db_printf(" %%o7=%#lx --\n", o7);
221         user = (type & T_KERNEL) == 0;
222         if (user) {
223                 tpc = db_get_value((db_addr_t)&tf->tf_tpc,
224                     sizeof(tf->tf_tpc), FALSE);
225                 db_printf("userland() at ");
226                 db_printsym(tpc, DB_STGY_PROC);
227                 db_printf("\n");
228                 db_utrace(td, tf, count);
229         }
230         return (user);
231 }
232
233 static int
234 db_backtrace(struct thread *td, struct frame *fp, int count)
235 {
236         struct trapframe *tf;
237         const char *name;
238         c_db_sym_t sym;
239         db_expr_t offset;
240         db_expr_t value;
241         db_addr_t npc;
242         db_addr_t pc;
243         int trap;
244         int user;
245
246         if (count == -1)
247                 count = 1024;
248
249         trap = 0;
250         user = 0;
251         npc = 0;
252         while (count-- && !user && !db_pager_quit) {
253                 pc = (db_addr_t)db_get_value((db_addr_t)&fp->fr_pc,
254                     sizeof(fp->fr_pc), FALSE);
255                 if (trap) {
256                         pc = npc;
257                         trap = 0;
258                 }
259                 if (!INKERNEL((vm_offset_t)pc))
260                         break;
261                 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
262                 if (sym == C_DB_SYM_NULL) {
263                         value = 0;
264                         name = NULL;
265                 } else
266                         db_symbol_values(sym, &name, &value);
267                 if (name == NULL)
268                         name = "(null)";
269                 fp = (struct frame *)(db_get_value((db_addr_t)&fp->fr_fp,
270                    sizeof(fp->fr_fp), FALSE) + SPOFF);
271                 if (bcmp(name, "tl0_", 4) == 0 ||
272                     bcmp(name, "tl1_", 4) == 0) {
273                         tf = (struct trapframe *)(fp + 1);
274                         npc = db_get_value((db_addr_t)&tf->tf_tpc,
275                             sizeof(tf->tf_tpc), FALSE);
276                         user = db_print_trap(td, tf, count);
277                         trap = 1;
278                 } else {
279                         db_printf("%s() at ", name);
280                         db_printsym(pc, DB_STGY_PROC);
281                         db_printf("\n");
282                 }
283         }
284         return (0);
285 }
286
287 void
288 db_trace_self(void)
289 {
290
291         db_backtrace(curthread,
292             (struct frame *)__builtin_frame_address(1), -1);
293 }
294
295 int
296 db_trace_thread(struct thread *td, int count)
297 {
298         struct pcb *ctx;
299
300         ctx = kdb_thr_ctx(td);
301         return (db_backtrace(td,
302             (struct frame *)(ctx->pcb_sp + SPOFF), count));
303 }