2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
35 #include <sys/linker.h>
37 #include <sys/kernel.h>
40 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
43 #include <machine/kdb.h>
44 #include <machine/pcb.h>
45 #include <machine/setjmp.h>
48 #include <ddb/db_command.h>
49 #include <ddb/db_sym.h>
51 SYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW, 0, "DDB settings");
53 static dbbe_init_f db_init;
54 static dbbe_trap_f db_trap;
55 static dbbe_trace_f db_trace_self_wrapper;
56 static dbbe_trace_thread_f db_trace_thread_wrapper;
58 KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trace_thread_wrapper,
62 * Symbols can be loaded by specifying the exact addresses of
63 * the symtab and strtab in memory. This is used when loaded from
64 * boot loaders different than the native one (like Xen).
66 vm_offset_t ksymtab, kstrtab, ksymtab_size;
69 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
76 X_db_lookup(db_symtab_t *symtab, const char *symbol)
81 if (symtab->private == NULL) {
82 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
85 sym = (Elf_Sym *)symtab->start;
86 while ((char *)sym < symtab->end) {
87 if (sym->st_name != 0 &&
88 !strcmp(symtab->private + sym->st_name, symbol))
89 return ((c_db_sym_t)sym);
97 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
101 Elf_Sym *sym, *match;
104 if (symtab->private == NULL) {
105 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
106 *diffp = (db_expr_t)diff;
107 return ((c_db_sym_t)lsym);
114 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
115 if (sym->st_name == 0 || sym->st_shndx == SHN_UNDEF)
117 if (off < sym->st_value)
119 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
120 ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
121 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
123 if ((off - sym->st_value) > diff)
125 if ((off - sym->st_value) < diff) {
126 diff = off - sym->st_value;
131 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
132 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
136 if (strat == DB_STGY_PROC &&
137 ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
138 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
140 if (strat == DB_STGY_ANY &&
141 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
146 *diffp = (match == NULL) ? off : diff;
147 return ((c_db_sym_t)match);
151 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
158 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
161 linker_symval_t lval;
163 if (symtab->private == NULL) {
164 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
166 *namep = (const char*)lval.name;
168 *valp = (db_expr_t)lval.value;
171 *namep = (const char *)symtab->private +
172 ((const Elf_Sym *)sym)->st_name;
174 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value;
179 db_fetch_ksymtab(vm_offset_t ksym_start, vm_offset_t ksym_end)
183 if (ksym_end > ksym_start && ksym_start != 0) {
184 ksymtab = ksym_start;
185 ksymtab_size = *(Elf_Size*)ksymtab;
186 ksymtab += sizeof(Elf_Size);
187 kstrtab = ksymtab + ksymtab_size;
188 strsz = *(Elf_Size*)kstrtab;
189 kstrtab += sizeof(Elf_Size);
190 if (kstrtab + strsz > ksym_end) {
191 /* Sizes doesn't match, unset everything. */
192 ksymtab = ksymtab_size = kstrtab = 0;
196 if (ksymtab == 0 || ksymtab_size == 0 || kstrtab == 0)
208 if (ksymtab != 0 && kstrtab != 0 && ksymtab_size != 0) {
209 db_add_symbol_table((char *)ksymtab,
210 (char *)(ksymtab + ksymtab_size), "elf", (char *)kstrtab);
212 db_add_symbol_table(NULL, NULL, "kld", NULL);
213 return (1); /* We're the default debugger. */
217 db_trap(int type, int code)
225 * Don't handle the trap if the console is unavailable (i.e. it
226 * is in graphics mode).
231 if (db_stop_at_pc(type, code, &bkpt, &watchpt)) {
233 db_printf("After %d instructions (%d loads, %d stores),\n",
234 db_inst_count, db_load_count, db_store_count);
236 prev_jb = kdb_jmpbuf(jb);
237 if (setjmp(jb) == 0) {
241 db_printf("Breakpoint at\t");
243 db_printf("Watchpoint at\t");
245 db_printf("Stopped at\t");
246 db_print_loc_and_inst(db_dot);
249 db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown");
251 (void)kdb_jmpbuf(prev_jb);
254 db_restart_at_pc(watchpt);
260 db_trace_self_wrapper(void)
265 prev_jb = kdb_jmpbuf(jb);
268 (void)kdb_jmpbuf(prev_jb);
272 db_trace_thread_wrapper(struct thread *td)
277 prev_jb = kdb_jmpbuf(jb);
279 db_trace_thread(td, -1);
280 (void)kdb_jmpbuf(prev_jb);