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;
54 static dbbe_trace_thread_f db_trace_thread_wrapper;
56 KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trace_thread_wrapper,
59 vm_offset_t ksym_start, ksym_end;
62 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
69 X_db_lookup(db_symtab_t *symtab, const char *symbol)
74 if (symtab->private == NULL) {
75 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
78 sym = (Elf_Sym *)symtab->start;
79 while ((char *)sym < symtab->end) {
80 if (sym->st_name != 0 &&
81 !strcmp(symtab->private + sym->st_name, symbol))
82 return ((c_db_sym_t)sym);
90 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
97 if (symtab->private == NULL) {
98 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
99 *diffp = (db_expr_t)diff;
100 return ((c_db_sym_t)lsym);
107 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
108 if (sym->st_name == 0)
110 if (off < sym->st_value)
112 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
113 ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
114 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
116 if ((off - sym->st_value) > diff)
118 if ((off - sym->st_value) < diff) {
119 diff = off - sym->st_value;
124 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
125 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
129 if (strat == DB_STGY_PROC &&
130 ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
131 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
133 if (strat == DB_STGY_ANY &&
134 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
139 *diffp = (match == NULL) ? off : diff;
140 return ((c_db_sym_t)match);
144 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
151 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
154 linker_symval_t lval;
156 if (symtab->private == NULL) {
157 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
159 *namep = (const char*)lval.name;
161 *valp = (db_expr_t)lval.value;
164 *namep = (const char *)symtab->private +
165 ((const Elf_Sym *)sym)->st_name;
167 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
174 uintptr_t symtab, strtab;
175 Elf_Size tabsz, strsz;
178 if (ksym_end > ksym_start && ksym_start != 0) {
180 tabsz = *((Elf_Size*)symtab);
181 symtab += sizeof(Elf_Size);
182 strtab = symtab + tabsz;
183 strsz = *((Elf_Size*)strtab);
184 strtab += sizeof(Elf_Size);
185 if (strtab + strsz <= ksym_end) {
186 db_add_symbol_table((char *)symtab,
187 (char *)(symtab + tabsz), "elf", (char *)strtab);
190 db_add_symbol_table(NULL, NULL, "kld", NULL);
191 return (1); /* We're the default debugger. */
195 db_trap(int type, int code)
199 boolean_t bkpt, watchpt;
203 * Don't handle the trap if the console is unavailable (i.e. it
204 * is in graphics mode).
209 bkpt = IS_BREAKPOINT_TRAP(type, code);
210 watchpt = IS_WATCHPOINT_TRAP(type, code);
212 if (db_stop_at_pc(&bkpt)) {
214 db_printf("After %d instructions (%d loads, %d stores),\n",
215 db_inst_count, db_load_count, db_store_count);
217 prev_jb = kdb_jmpbuf(jb);
218 if (setjmp(jb) == 0) {
222 db_printf("Breakpoint at\t");
224 db_printf("Watchpoint at\t");
226 db_printf("Stopped at\t");
227 db_print_loc_and_inst(db_dot);
230 db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown");
232 (void)kdb_jmpbuf(prev_jb);
235 db_restart_at_pc(watchpt);
241 db_trace_self_wrapper(void)
246 prev_jb = kdb_jmpbuf(jb);
249 (void)kdb_jmpbuf(prev_jb);
253 db_trace_thread_wrapper(struct thread *td)
258 prev_jb = kdb_jmpbuf(jb);
260 db_trace_thread(td, -1);
261 (void)kdb_jmpbuf(prev_jb);