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>
39 #include <sys/sysctl.h>
41 #include <machine/kdb.h>
42 #include <machine/pcb.h>
43 #include <machine/setjmp.h>
46 #include <ddb/db_command.h>
47 #include <ddb/db_sym.h>
49 SYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW, 0, "DDB settings");
51 static dbbe_init_f db_init;
52 static dbbe_trap_f db_trap;
53 static dbbe_trace_f db_trace_self_wrapper;
55 KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trap);
57 vm_offset_t ksym_start, ksym_end;
60 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
67 X_db_lookup(db_symtab_t *symtab, const char *symbol)
72 if (symtab->private == NULL) {
73 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
76 sym = (Elf_Sym *)symtab->start;
77 while ((char *)sym < symtab->end) {
78 if (sym->st_name != 0 &&
79 !strcmp(symtab->private + sym->st_name, symbol))
80 return ((c_db_sym_t)sym);
88 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
95 if (symtab->private == NULL) {
96 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
97 *diffp = (db_expr_t)diff;
98 return ((c_db_sym_t)lsym);
105 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
106 if (sym->st_name == 0)
108 if (off < sym->st_value)
110 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
111 ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
112 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
114 if ((off - sym->st_value) > diff)
116 if ((off - sym->st_value) < diff) {
117 diff = off - sym->st_value;
122 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
123 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
127 if (strat == DB_STGY_PROC &&
128 ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
129 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
131 if (strat == DB_STGY_ANY &&
132 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
137 *diffp = (match == NULL) ? off : diff;
138 return ((c_db_sym_t)match);
142 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
149 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
152 linker_symval_t lval;
154 if (symtab->private == NULL) {
155 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
157 *namep = (const char*)lval.name;
159 *valp = (db_expr_t)lval.value;
162 *namep = (const char *)symtab->private +
163 ((const Elf_Sym *)sym)->st_name;
165 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
172 uintptr_t symtab, strtab;
173 Elf_Size tabsz, strsz;
176 if (ksym_end > ksym_start && ksym_start != 0) {
178 tabsz = *((Elf_Size*)symtab);
179 symtab += sizeof(Elf_Size);
180 strtab = symtab + tabsz;
181 strsz = *((Elf_Size*)strtab);
182 strtab += sizeof(Elf_Size);
183 if (strtab + strsz <= ksym_end) {
184 db_add_symbol_table((char *)symtab,
185 (char *)(symtab + tabsz), "elf", (char *)strtab);
188 db_add_symbol_table(NULL, NULL, "kld", NULL);
189 return (1); /* We're the default debugger. */
193 db_trap(int type, int code)
197 boolean_t bkpt, watchpt;
201 * Don't handle the trap if the console is unavailable (i.e. it
202 * is in graphics mode).
207 bkpt = IS_BREAKPOINT_TRAP(type, code);
208 watchpt = IS_WATCHPOINT_TRAP(type, code);
210 if (db_stop_at_pc(&bkpt)) {
212 db_printf("After %d instructions (%d loads, %d stores),\n",
213 db_inst_count, db_load_count, db_store_count);
215 prev_jb = kdb_jmpbuf(jb);
216 if (setjmp(jb) == 0) {
220 db_printf("Breakpoint at\t");
222 db_printf("Watchpoint at\t");
224 db_printf("Stopped at\t");
225 db_print_loc_and_inst(db_dot);
228 db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown");
230 (void)kdb_jmpbuf(prev_jb);
233 db_restart_at_pc(watchpt);
239 db_trace_self_wrapper(void)
244 prev_jb = kdb_jmpbuf(jb);
247 (void)kdb_jmpbuf(prev_jb);