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 #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>
53 typedef struct db_private *db_private_t;
55 #define DB_PRIVATE(x) ((db_private_t)(x->private))
57 SYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
60 static dbbe_init_f db_init;
61 static dbbe_trap_f db_trap;
62 static dbbe_trace_f db_trace_self_wrapper;
63 static dbbe_trace_thread_f db_trace_thread_wrapper;
65 KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trace_thread_wrapper,
69 * Symbols can be loaded by specifying the exact addresses of
70 * the symtab and strtab in memory. This is used when loaded from
71 * boot loaders different than the native one (like Xen).
73 vm_offset_t ksymtab, kstrtab, ksymtab_size, ksymtab_relbase;
74 static struct db_private ksymtab_private;
77 X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line,
84 X_db_lookup(db_symtab_t *symtab, const char *symbol)
89 if (symtab->private == NULL) {
90 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym))
93 sym = (Elf_Sym *)symtab->start;
94 while ((char *)sym < symtab->end) {
95 if (sym->st_name != 0 &&
96 !strcmp(DB_PRIVATE(symtab)->strtab +
97 sym->st_name, symbol))
98 return ((c_db_sym_t)sym);
106 X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat,
110 Elf_Sym *sym, *match;
112 db_addr_t stoffs = off;
114 if (symtab->private == NULL) {
115 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) {
116 *diffp = (db_expr_t)diff;
117 return ((c_db_sym_t)lsym);
122 stoffs -= DB_PRIVATE(symtab)->relbase;
126 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) {
127 if (sym->st_name == 0 || sym->st_shndx == SHN_UNDEF)
129 if (stoffs < sym->st_value)
131 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT &&
132 ELF_ST_TYPE(sym->st_info) != STT_FUNC &&
133 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE)
135 if ((stoffs - sym->st_value) > diff)
137 if ((stoffs - sym->st_value) < diff) {
138 diff = stoffs - sym->st_value;
143 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL &&
144 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
148 if (strat == DB_STGY_PROC &&
149 ELF_ST_TYPE(sym->st_info) == STT_FUNC &&
150 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
152 if (strat == DB_STGY_ANY &&
153 ELF_ST_BIND(sym->st_info) != STB_LOCAL)
158 *diffp = (match == NULL) ? off : diff;
159 return ((c_db_sym_t)match);
163 X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp,
170 X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep,
173 linker_symval_t lval;
175 if (symtab->private == NULL) {
176 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval);
178 *namep = (const char*)lval.name;
180 *valp = (db_expr_t)lval.value;
183 *namep = (const char *)DB_PRIVATE(symtab)->strtab +
184 ((const Elf_Sym *)sym)->st_name;
186 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value +
187 DB_PRIVATE(symtab)->relbase;
192 db_fetch_ksymtab(vm_offset_t ksym_start, vm_offset_t ksym_end,
197 if (ksym_end > ksym_start && ksym_start != 0) {
198 ksymtab = ksym_start;
199 ksymtab_size = *(Elf_Size*)ksymtab;
200 ksymtab += sizeof(Elf_Size);
201 kstrtab = ksymtab + ksymtab_size;
202 strsz = *(Elf_Size*)kstrtab;
203 kstrtab += sizeof(Elf_Size);
204 ksymtab_relbase = relbase;
205 if (kstrtab + strsz > ksym_end) {
206 /* Sizes doesn't match, unset everything. */
207 ksymtab = ksymtab_size = kstrtab = ksymtab_relbase
212 if (ksymtab == 0 || ksymtab_size == 0 || kstrtab == 0)
224 if (ksymtab != 0 && kstrtab != 0 && ksymtab_size != 0) {
225 ksymtab_private.strtab = (char *)kstrtab;
226 ksymtab_private.relbase = ksymtab_relbase;
227 db_add_symbol_table((char *)ksymtab,
228 (char *)(ksymtab + ksymtab_size), "elf", (char *)&ksymtab_private);
230 db_add_symbol_table(NULL, NULL, "kld", NULL);
231 return (1); /* We're the default debugger. */
235 db_trap(int type, int code)
243 * Don't handle the trap if the console is unavailable (i.e. it
244 * is in graphics mode).
249 if (db_stop_at_pc(type, code, &bkpt, &watchpt)) {
251 db_printf("After %d instructions (%d loads, %d stores),\n",
252 db_inst_count, db_load_count, db_store_count);
254 prev_jb = kdb_jmpbuf(jb);
255 if (setjmp(jb) == 0) {
259 db_printf("Breakpoint at\t");
261 db_printf("Watchpoint at\t");
263 db_printf("Stopped at\t");
264 db_print_loc_and_inst(db_dot);
267 db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown");
269 (void)kdb_jmpbuf(prev_jb);
272 db_restart_at_pc(watchpt);
278 db_trace_self_wrapper(void)
283 prev_jb = kdb_jmpbuf(jb);
286 (void)kdb_jmpbuf(prev_jb);
290 db_trace_thread_wrapper(struct thread *td)
295 prev_jb = kdb_jmpbuf(jb);
297 db_trace_thread(td, -1);
298 (void)kdb_jmpbuf(prev_jb);