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,
60 * Symbols can be loaded by specifying the exact addresses of
61 * the symtab and strtab in memory. This is used when loaded from
62 * boot loaders different than the native one (like Xen).
64 vm_offset_t ksymtab, kstrtab, ksymtab_size;
67 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
74 X_db_lookup(db_symtab_t *symtab, const char *symbol)
79 if (symtab->private == NULL) {
80 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
83 sym = (Elf_Sym *)symtab->start;
84 while ((char *)sym < symtab->end) {
85 if (sym->st_name != 0 &&
86 !strcmp(symtab->private + sym->st_name, symbol))
87 return ((c_db_sym_t)sym);
95 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
102 if (symtab->private == NULL) {
103 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
104 *diffp = (db_expr_t)diff;
105 return ((c_db_sym_t)lsym);
112 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
113 if (sym->st_name == 0 || sym->st_shndx == SHN_UNDEF)
115 if (off < sym->st_value)
117 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
118 ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
119 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
121 if ((off - sym->st_value) > diff)
123 if ((off - sym->st_value) < diff) {
124 diff = off - sym->st_value;
129 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
130 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
134 if (strat == DB_STGY_PROC &&
135 ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
136 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
138 if (strat == DB_STGY_ANY &&
139 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
144 *diffp = (match == NULL) ? off : diff;
145 return ((c_db_sym_t)match);
149 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
156 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
159 linker_symval_t lval;
161 if (symtab->private == NULL) {
162 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
164 *namep = (const char*)lval.name;
166 *valp = (db_expr_t)lval.value;
169 *namep = (const char *)symtab->private +
170 ((const Elf_Sym *)sym)->st_name;
172 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
177 db_fetch_ksymtab(vm_offset_t ksym_start, vm_offset_t ksym_end)
181 if (ksym_end > ksym_start && ksym_start != 0) {
182 ksymtab = ksym_start;
183 ksymtab_size = *(Elf_Size*)ksymtab;
184 ksymtab += sizeof(Elf_Size);
185 kstrtab = ksymtab + ksymtab_size;
186 strsz = *(Elf_Size*)kstrtab;
187 kstrtab += sizeof(Elf_Size);
188 if (kstrtab + strsz > ksym_end) {
189 /* Sizes doesn't match, unset everything. */
190 ksymtab = ksymtab_size = kstrtab = 0;
194 if (ksymtab == 0 || ksymtab_size == 0 || kstrtab == 0)
206 if (ksymtab != 0 && kstrtab != 0 && ksymtab_size != 0) {
207 db_add_symbol_table((char *)ksymtab,
208 (char *)(ksymtab + ksymtab_size), "elf", (char *)kstrtab);
210 db_add_symbol_table(NULL, NULL, "kld", NULL);
211 return (1); /* We're the default debugger. */
215 db_trap(int type, int code)
223 * Don't handle the trap if the console is unavailable (i.e. it
224 * is in graphics mode).
229 if (db_stop_at_pc(type, code, &bkpt, &watchpt)) {
231 db_printf("After %d instructions (%d loads, %d stores),\n",
232 db_inst_count, db_load_count, db_store_count);
234 prev_jb = kdb_jmpbuf(jb);
235 if (setjmp(jb) == 0) {
239 db_printf("Breakpoint at\t");
241 db_printf("Watchpoint at\t");
243 db_printf("Stopped at\t");
244 db_print_loc_and_inst(db_dot);
247 db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown");
249 (void)kdb_jmpbuf(prev_jb);
252 db_restart_at_pc(watchpt);
258 db_trace_self_wrapper(void)
263 prev_jb = kdb_jmpbuf(jb);
266 (void)kdb_jmpbuf(prev_jb);
270 db_trace_thread_wrapper(struct thread *td)
275 prev_jb = kdb_jmpbuf(jb);
277 db_trace_thread(td, -1);
278 (void)kdb_jmpbuf(prev_jb);