2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * Permission to use, copy, modify and distribute this software and its
7 * documentation is hereby granted, provided that both the copyright
8 * notice and this permission notice appear in all copies of the
9 * software, derivative works or modified versions, and any portions
10 * thereof, and that both notices appear in supporting documentation.
12 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/systm.h>
33 #include <sys/linker.h>
35 #include <sys/kernel.h>
38 #include <sys/reboot.h>
40 #include <machine/kdb.h>
41 #include <machine/pcb.h>
42 #include <machine/setjmp.h>
45 #include <ddb/db_command.h>
46 #include <ddb/db_sym.h>
48 static dbbe_init_f db_init;
49 static dbbe_trap_f db_trap;
50 static dbbe_trace_f db_trace_self_wrapper;
52 KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trap);
54 vm_offset_t ksym_start, ksym_end;
57 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
64 X_db_lookup(db_symtab_t *symtab, const char *symbol)
69 if (symtab->private == NULL) {
70 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
73 sym = (Elf_Sym *)symtab->start;
74 while ((char *)sym < symtab->end) {
75 if (sym->st_name != 0 &&
76 !strcmp(symtab->private + sym->st_name, symbol))
77 return ((c_db_sym_t)sym);
85 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
92 if (symtab->private == NULL) {
93 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
94 *diffp = (db_expr_t)diff;
95 return ((c_db_sym_t)lsym);
102 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
103 if (sym->st_name == 0)
105 if (off < sym->st_value)
107 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
108 ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
109 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
111 if ((off - sym->st_value) > diff)
113 if ((off - sym->st_value) < diff) {
114 diff = off - sym->st_value;
119 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
120 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
124 if (strat == DB_STGY_PROC &&
125 ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
126 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
128 if (strat == DB_STGY_ANY &&
129 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
134 *diffp = (match == NULL) ? off : diff;
135 return ((c_db_sym_t)match);
139 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
146 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
149 linker_symval_t lval;
151 if (symtab->private == NULL) {
152 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
154 *namep = (const char*)lval.name;
156 *valp = (db_expr_t)lval.value;
159 *namep = (const char *)symtab->private +
160 ((const Elf_Sym *)sym)->st_name;
162 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
169 uintptr_t symtab, strtab;
170 Elf_Size tabsz, strsz;
172 if (ksym_end > ksym_start && ksym_start != 0) {
174 tabsz = *((Elf_Size*)symtab);
175 symtab += sizeof(Elf_Size);
176 strtab = symtab + tabsz;
177 strsz = *((Elf_Size*)strtab);
178 strtab += sizeof(Elf_Size);
179 if (strtab + strsz <= ksym_end) {
180 db_add_symbol_table((char *)symtab,
181 (char *)(symtab + tabsz), "elf", (char *)strtab);
184 db_add_symbol_table(NULL, NULL, "kld", NULL);
185 return (1); /* We're the default debugger. */
189 db_trap(int type, int code)
193 boolean_t bkpt, watchpt;
196 * Don't handle the trap if the console is unavailable (i.e. it
197 * is in graphics mode).
202 bkpt = IS_BREAKPOINT_TRAP(type, code);
203 watchpt = IS_WATCHPOINT_TRAP(type, code);
205 if (db_stop_at_pc(&bkpt)) {
207 db_printf("After %d instructions (%d loads, %d stores),\n",
208 db_inst_count, db_load_count, db_store_count);
210 prev_jb = kdb_jmpbuf(jb);
211 if (setjmp(jb) == 0) {
215 db_printf("Breakpoint at\t");
217 db_printf("Watchpoint at\t");
219 db_printf("Stopped at\t");
220 db_print_loc_and_inst(db_dot);
223 (void)kdb_jmpbuf(prev_jb);
226 db_restart_at_pc(watchpt);
232 db_trace_self_wrapper(void)
237 prev_jb = kdb_jmpbuf(jb);
240 (void)kdb_jmpbuf(prev_jb);