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;
51 KDB_BACKEND(ddb, db_init, db_trace_self, db_trap);
53 vm_offset_t ksym_start, ksym_end;
56 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
63 X_db_lookup(db_symtab_t *symtab, const char *symbol)
68 if (symtab->private == NULL) {
69 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
72 sym = (Elf_Sym *)symtab->start;
73 while ((char *)sym < symtab->end) {
74 if (sym->st_name != 0 &&
75 !strcmp(symtab->private + sym->st_name, symbol))
76 return ((c_db_sym_t)sym);
84 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
91 if (symtab->private == NULL) {
92 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
93 *diffp = (db_expr_t)diff;
94 return ((c_db_sym_t)lsym);
101 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
102 if (sym->st_name == 0)
104 if (off < sym->st_value)
106 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
107 ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
108 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
110 if ((off - sym->st_value) > diff)
112 if ((off - sym->st_value) < diff) {
113 diff = off - sym->st_value;
118 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
119 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
123 if (strat == DB_STGY_PROC &&
124 ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
125 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
127 if (strat == DB_STGY_ANY &&
128 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
133 *diffp = (match == NULL) ? off : diff;
134 return ((c_db_sym_t)match);
138 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
145 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
148 linker_symval_t lval;
150 if (symtab->private == NULL) {
151 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
153 *namep = (const char*)lval.name;
155 *valp = (db_expr_t)lval.value;
158 *namep = (const char *)symtab->private +
159 ((const Elf_Sym *)sym)->st_name;
161 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
168 uintptr_t symtab, strtab;
169 Elf_Size tabsz, strsz;
171 if (ksym_end > ksym_start && ksym_start != 0) {
173 tabsz = *((Elf_Size*)symtab);
174 symtab += sizeof(Elf_Size);
175 strtab = symtab + tabsz;
176 strsz = *((Elf_Size*)strtab);
177 strtab += sizeof(Elf_Size);
178 if (strtab + strsz <= ksym_end) {
179 db_add_symbol_table((char *)symtab,
180 (char *)(symtab + tabsz), "elf", (char *)strtab);
183 db_add_symbol_table(NULL, NULL, "kld", NULL);
184 return (1); /* We're the default debugger. */
188 db_trap(int type, int code)
192 boolean_t bkpt, watchpt;
195 * Don't handle the trap if the console is unavailable (i.e. it
196 * is in graphics mode).
201 bkpt = IS_BREAKPOINT_TRAP(type, code);
202 watchpt = IS_WATCHPOINT_TRAP(type, code);
204 if (db_stop_at_pc(&bkpt)) {
206 db_printf("After %d instructions (%d loads, %d stores),\n",
207 db_inst_count, db_load_count, db_store_count);
209 prev_jb = kdb_jmpbuf(jb);
210 if (setjmp(jb) == 0) {
214 db_printf("Breakpoint at\t");
216 db_printf("Watchpoint at\t");
218 db_printf("Stopped at\t");
219 db_print_loc_and_inst(db_dot);
222 (void)kdb_jmpbuf(prev_jb);
225 db_restart_at_pc(watchpt);