db_main.c revision 139747
1/*- 2 * Mach Operating System 3 * Copyright (c) 1991,1990 Carnegie Mellon University 4 * All Rights Reserved. 5 * 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. 11 * 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. 15 * 16 * Carnegie Mellon requests users of this software to return to 17 * 18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19 * School of Computer Science 20 * Carnegie Mellon University 21 * Pittsburgh PA 15213-3890 22 * 23 * any improvements or extensions that they make and grant Carnegie the 24 * rights to redistribute these changes. 25 */ 26 27#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/ddb/db_main.c 139747 2005-01-06 01:34:41Z imp $"); 29 30#include <sys/param.h> 31#include <sys/systm.h> 32#include <sys/cons.h> 33#include <sys/linker.h> 34#include <sys/kdb.h> 35#include <sys/kernel.h> 36#include <sys/pcpu.h> 37#include <sys/proc.h> 38#include <sys/reboot.h> 39 40#include <machine/kdb.h> 41#include <machine/pcb.h> 42#include <machine/setjmp.h> 43 44#include <ddb/ddb.h> 45#include <ddb/db_command.h> 46#include <ddb/db_sym.h> 47 48static dbbe_init_f db_init; 49static dbbe_trap_f db_trap; 50 51KDB_BACKEND(ddb, db_init, db_trace_self, db_trap); 52 53vm_offset_t ksym_start, ksym_end; 54 55boolean_t 56X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line, 57 db_expr_t off) 58{ 59 return (FALSE); 60} 61 62c_db_sym_t 63X_db_lookup(db_symtab_t *symtab, const char *symbol) 64{ 65 c_linker_sym_t lsym; 66 Elf_Sym *sym; 67 68 if (symtab->private == NULL) { 69 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym)) 70 ? lsym : NULL)); 71 } else { 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); 77 sym++; 78 } 79 } 80 return (NULL); 81} 82 83c_db_sym_t 84X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat, 85 db_expr_t *diffp) 86{ 87 c_linker_sym_t lsym; 88 Elf_Sym *sym, *match; 89 unsigned long diff; 90 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); 95 } 96 return (NULL); 97 } 98 99 diff = ~0UL; 100 match = NULL; 101 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) { 102 if (sym->st_name == 0) 103 continue; 104 if (off < sym->st_value) 105 continue; 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) 109 continue; 110 if ((off - sym->st_value) > diff) 111 continue; 112 if ((off - sym->st_value) < diff) { 113 diff = off - sym->st_value; 114 match = sym; 115 } else { 116 if (match == NULL) 117 match = sym; 118 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL && 119 ELF_ST_BIND(sym->st_info) != STB_LOCAL) 120 match = sym; 121 } 122 if (diff == 0) { 123 if (strat == DB_STGY_PROC && 124 ELF_ST_TYPE(sym->st_info) == STT_FUNC && 125 ELF_ST_BIND(sym->st_info) != STB_LOCAL) 126 break; 127 if (strat == DB_STGY_ANY && 128 ELF_ST_BIND(sym->st_info) != STB_LOCAL) 129 break; 130 } 131 } 132 133 *diffp = (match == NULL) ? off : diff; 134 return ((c_db_sym_t)match); 135} 136 137boolean_t 138X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp, 139 char **argp) 140{ 141 return (FALSE); 142} 143 144void 145X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep, 146 db_expr_t *valp) 147{ 148 linker_symval_t lval; 149 150 if (symtab->private == NULL) { 151 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval); 152 if (namep != NULL) 153 *namep = (const char*)lval.name; 154 if (valp != NULL) 155 *valp = (db_expr_t)lval.value; 156 } else { 157 if (namep != NULL) 158 *namep = (const char *)symtab->private + 159 ((const Elf_Sym *)sym)->st_name; 160 if (valp != NULL) 161 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value; 162 } 163} 164 165static int 166db_init(void) 167{ 168 uintptr_t symtab, strtab; 169 Elf_Size tabsz, strsz; 170 171 if (ksym_end > ksym_start && ksym_start != 0) { 172 symtab = ksym_start; 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); 181 } 182 } 183 db_add_symbol_table(NULL, NULL, "kld", NULL); 184 return (1); /* We're the default debugger. */ 185} 186 187static int 188db_trap(int type, int code) 189{ 190 jmp_buf jb; 191 void *prev_jb; 192 boolean_t bkpt, watchpt; 193 194 /* 195 * Don't handle the trap if the console is unavailable (i.e. it 196 * is in graphics mode). 197 */ 198 if (cnunavailable()) 199 return (0); 200 201 bkpt = IS_BREAKPOINT_TRAP(type, code); 202 watchpt = IS_WATCHPOINT_TRAP(type, code); 203 204 if (db_stop_at_pc(&bkpt)) { 205 if (db_inst_count) { 206 db_printf("After %d instructions (%d loads, %d stores),\n", 207 db_inst_count, db_load_count, db_store_count); 208 } 209 prev_jb = kdb_jmpbuf(jb); 210 if (setjmp(jb) == 0) { 211 db_dot = PC_REGS(); 212 db_print_thread(); 213 if (bkpt) 214 db_printf("Breakpoint at\t"); 215 else if (watchpt) 216 db_printf("Watchpoint at\t"); 217 else 218 db_printf("Stopped at\t"); 219 db_print_loc_and_inst(db_dot); 220 } 221 db_command_loop(); 222 (void)kdb_jmpbuf(prev_jb); 223 } 224 225 db_restart_at_pc(watchpt); 226 227 return (1); 228} 229