2 * Copyright (c) 2001 Jake Burkholder.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
34 #include <sys/stack.h>
35 #include <sys/sysent.h>
38 #include <vm/vm_page.h>
39 #include <vm/vm_map.h>
41 #include <machine/cpu.h>
42 #include <machine/pcb.h>
43 #include <machine/stack.h>
44 #include <machine/trap.h>
45 #include <machine/vmparam.h>
48 #include <ddb/db_access.h>
49 #include <ddb/db_sym.h>
50 #include <ddb/db_variables.h>
51 #include <ddb/db_watch.h>
53 static db_varfcn_t db_frame;
55 #define DB_OFFSET(x) (db_expr_t *)offsetof(struct trapframe, x)
56 struct db_variable db_regs[] = {
57 { "g0", DB_OFFSET(tf_global[0]), db_frame },
58 { "g1", DB_OFFSET(tf_global[1]), db_frame },
59 { "g2", DB_OFFSET(tf_global[2]), db_frame },
60 { "g3", DB_OFFSET(tf_global[3]), db_frame },
61 { "g4", DB_OFFSET(tf_global[4]), db_frame },
62 { "g5", DB_OFFSET(tf_global[5]), db_frame },
63 { "g6", DB_OFFSET(tf_global[6]), db_frame },
64 { "g7", DB_OFFSET(tf_global[7]), db_frame },
65 { "i0", DB_OFFSET(tf_out[0]), db_frame },
66 { "i1", DB_OFFSET(tf_out[1]), db_frame },
67 { "i2", DB_OFFSET(tf_out[2]), db_frame },
68 { "i3", DB_OFFSET(tf_out[3]), db_frame },
69 { "i4", DB_OFFSET(tf_out[4]), db_frame },
70 { "i5", DB_OFFSET(tf_out[5]), db_frame },
71 { "i6", DB_OFFSET(tf_out[6]), db_frame },
72 { "i7", DB_OFFSET(tf_out[7]), db_frame },
73 { "tnpc", DB_OFFSET(tf_tnpc), db_frame },
74 { "tpc", DB_OFFSET(tf_tpc), db_frame },
75 { "tstate", DB_OFFSET(tf_tstate), db_frame },
77 struct db_variable *db_eregs = db_regs + sizeof(db_regs)/sizeof(db_regs[0]);
80 db_frame(struct db_variable *vp, db_expr_t *valuep, int op)
84 if (kdb_frame == NULL)
86 reg = (uint64_t*)((uintptr_t)kdb_frame + (uintptr_t)vp->valuep);
95 * User stack trace (debugging aid).
98 db_utrace(struct thread *td, struct trapframe *tf, int count)
101 db_addr_t sp, rsp, o7, pc;
105 sp = db_get_value((db_addr_t)&tf->tf_sp, sizeof(tf->tf_sp), FALSE);
106 o7 = db_get_value((db_addr_t)&tf->tf_out[7], sizeof(tf->tf_out[7]),
108 pc = db_get_value((db_addr_t)&tf->tf_tpc, sizeof(tf->tf_tpc), FALSE);
109 db_printf("user trace: trap %%o7=%#lx\n", o7);
110 while (count-- && sp != 0 && !db_pager_quit) {
111 db_printf("pc %#lx, sp %#lx\n", pc, sp);
112 /* First, check whether the frame is in the pcb. */
114 for (i = 0; i < pcb->pcb_nsaved; i++) {
115 if (pcb->pcb_rwsp[i] == sp) {
117 sp = pcb->pcb_rw[i].rw_in[6];
118 pc = pcb->pcb_rw[i].rw_in[7];
125 if (copyin((void *)(rsp + offsetof(struct frame, fr_fp)),
126 &sp, sizeof(sp)) != 0 ||
127 copyin((void *)(rsp + offsetof(struct frame, fr_pc)),
128 &pc, sizeof(pc)) != 0)
136 db_print_trap(struct thread *td, struct trapframe *tf, int count)
155 type = db_get_value((db_addr_t)&tf->tf_type,
156 sizeof(tf->tf_type), FALSE);
157 db_printf("-- %s", trap_msg[type & ~T_KERNEL]);
158 switch (type & ~T_KERNEL) {
159 case T_DATA_PROTECTION:
160 tar = (u_long)db_get_value((db_addr_t)&tf->tf_tar,
161 sizeof(tf->tf_tar), FALSE);
162 db_printf(" tar=%#lx", tar);
164 case T_DATA_EXCEPTION:
165 case T_INSTRUCTION_EXCEPTION:
166 case T_MEM_ADDRESS_NOT_ALIGNED:
167 sfar = (u_long)db_get_value((db_addr_t)&tf->tf_sfar,
168 sizeof(tf->tf_sfar), FALSE);
169 sfsr = (u_long)db_get_value((db_addr_t)&tf->tf_sfsr,
170 sizeof(tf->tf_sfsr), FALSE);
171 db_printf(" sfar=%#lx sfsr=%#lx", sfar, sfsr);
174 case T_INSTRUCTION_MISS:
175 tar = (u_long)db_get_value((db_addr_t)&tf->tf_tar,
176 sizeof(tf->tf_tar), FALSE);
177 db_printf(" tar=%#lx", tar);
180 code = db_get_value((db_addr_t)&tf->tf_global[1],
181 sizeof(tf->tf_global[1]), FALSE);
182 db_printf(" (%ld", code);
183 if (code >= 0 && code < p->p_sysent->sv_size) {
184 func = (db_addr_t)p->p_sysent->sv_table[code].sy_call;
185 sym = db_search_symbol(func, DB_STGY_ANY, &diff);
186 if (sym != DB_SYM_NULL && diff == 0) {
187 db_symbol_values(sym, &symname, NULL);
188 db_printf(", %s, %s", p->p_sysent->sv_name,
195 level = (u_long)db_get_value((db_addr_t)&tf->tf_level,
196 sizeof(tf->tf_level), FALSE);
197 pil = (u_long)db_get_value((db_addr_t)&tf->tf_pil,
198 sizeof(tf->tf_pil), FALSE);
199 db_printf(" level=%#lx pil=%#lx", level, pil);
204 o7 = (u_long)db_get_value((db_addr_t)&tf->tf_out[7],
205 sizeof(tf->tf_out[7]), FALSE);
206 db_printf(" %%o7=%#lx --\n", o7);
207 user = (type & T_KERNEL) == 0;
209 tpc = db_get_value((db_addr_t)&tf->tf_tpc,
210 sizeof(tf->tf_tpc), FALSE);
211 db_printf("userland() at ");
212 db_printsym(tpc, DB_STGY_PROC);
214 db_utrace(td, tf, count);
220 db_backtrace(struct thread *td, struct frame *fp, int count)
222 struct trapframe *tf;
238 while (count-- && !user && !db_pager_quit) {
239 pc = (db_addr_t)db_get_value((db_addr_t)&fp->fr_pc,
240 sizeof(fp->fr_pc), FALSE);
245 if (!INKERNEL((vm_offset_t)pc))
247 sym = db_search_symbol(pc, DB_STGY_ANY, &offset);
248 if (sym == C_DB_SYM_NULL) {
252 db_symbol_values(sym, &name, &value);
255 fp = (struct frame *)(db_get_value((db_addr_t)&fp->fr_fp,
256 sizeof(fp->fr_fp), FALSE) + SPOFF);
257 if ((value > (u_long)tl_trap_begin &&
258 value < (u_long)tl_trap_end) ||
259 (value > (u_long)tl_text_begin &&
260 value < (u_long)tl_text_end)) {
261 tf = (struct trapframe *)(fp + 1);
262 npc = db_get_value((db_addr_t)&tf->tf_tpc,
263 sizeof(tf->tf_tpc), FALSE);
264 user = db_print_trap(td, tf, count);
267 db_printf("%s() at ", name);
268 db_printsym(pc, DB_STGY_PROC);
279 db_backtrace(curthread,
280 (struct frame *)__builtin_frame_address(1), -1);
284 db_trace_thread(struct thread *td, int count)
288 ctx = kdb_thr_ctx(td);
289 return (db_backtrace(td,
290 (struct frame *)(ctx->pcb_sp + SPOFF), count));