db_main.c revision 174914
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 174914 2007-12-26 09:33:19Z rwatson $"); 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#include <sys/sysctl.h> 40 41#include <machine/kdb.h> 42#include <machine/pcb.h> 43#include <machine/setjmp.h> 44 45#include <ddb/ddb.h> 46#include <ddb/db_command.h> 47#include <ddb/db_sym.h> 48 49SYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW, 0, "DDB settings"); 50 51static dbbe_init_f db_init; 52static dbbe_trap_f db_trap; 53static dbbe_trace_f db_trace_self_wrapper; 54 55KDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trap); 56 57vm_offset_t ksym_start, ksym_end; 58 59boolean_t 60X_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line, 61 db_expr_t off) 62{ 63 return (FALSE); 64} 65 66c_db_sym_t 67X_db_lookup(db_symtab_t *symtab, const char *symbol) 68{ 69 c_linker_sym_t lsym; 70 Elf_Sym *sym; 71 72 if (symtab->private == NULL) { 73 return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym)) 74 ? lsym : NULL)); 75 } else { 76 sym = (Elf_Sym *)symtab->start; 77 while ((char *)sym < symtab->end) { 78 if (sym->st_name != 0 && 79 !strcmp(symtab->private + sym->st_name, symbol)) 80 return ((c_db_sym_t)sym); 81 sym++; 82 } 83 } 84 return (NULL); 85} 86 87c_db_sym_t 88X_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat, 89 db_expr_t *diffp) 90{ 91 c_linker_sym_t lsym; 92 Elf_Sym *sym, *match; 93 unsigned long diff; 94 95 if (symtab->private == NULL) { 96 if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) { 97 *diffp = (db_expr_t)diff; 98 return ((c_db_sym_t)lsym); 99 } 100 return (NULL); 101 } 102 103 diff = ~0UL; 104 match = NULL; 105 for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) { 106 if (sym->st_name == 0) 107 continue; 108 if (off < sym->st_value) 109 continue; 110 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT && 111 ELF_ST_TYPE(sym->st_info) != STT_FUNC && 112 ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) 113 continue; 114 if ((off - sym->st_value) > diff) 115 continue; 116 if ((off - sym->st_value) < diff) { 117 diff = off - sym->st_value; 118 match = sym; 119 } else { 120 if (match == NULL) 121 match = sym; 122 else if (ELF_ST_BIND(match->st_info) == STB_LOCAL && 123 ELF_ST_BIND(sym->st_info) != STB_LOCAL) 124 match = sym; 125 } 126 if (diff == 0) { 127 if (strat == DB_STGY_PROC && 128 ELF_ST_TYPE(sym->st_info) == STT_FUNC && 129 ELF_ST_BIND(sym->st_info) != STB_LOCAL) 130 break; 131 if (strat == DB_STGY_ANY && 132 ELF_ST_BIND(sym->st_info) != STB_LOCAL) 133 break; 134 } 135 } 136 137 *diffp = (match == NULL) ? off : diff; 138 return ((c_db_sym_t)match); 139} 140 141boolean_t 142X_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp, 143 char **argp) 144{ 145 return (FALSE); 146} 147 148void 149X_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep, 150 db_expr_t *valp) 151{ 152 linker_symval_t lval; 153 154 if (symtab->private == NULL) { 155 linker_ddb_symbol_values((c_linker_sym_t)sym, &lval); 156 if (namep != NULL) 157 *namep = (const char*)lval.name; 158 if (valp != NULL) 159 *valp = (db_expr_t)lval.value; 160 } else { 161 if (namep != NULL) 162 *namep = (const char *)symtab->private + 163 ((const Elf_Sym *)sym)->st_name; 164 if (valp != NULL) 165 *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value; 166 } 167} 168 169static int 170db_init(void) 171{ 172 uintptr_t symtab, strtab; 173 Elf_Size tabsz, strsz; 174 175 if (ksym_end > ksym_start && ksym_start != 0) { 176 symtab = ksym_start; 177 tabsz = *((Elf_Size*)symtab); 178 symtab += sizeof(Elf_Size); 179 strtab = symtab + tabsz; 180 strsz = *((Elf_Size*)strtab); 181 strtab += sizeof(Elf_Size); 182 if (strtab + strsz <= ksym_end) { 183 db_add_symbol_table((char *)symtab, 184 (char *)(symtab + tabsz), "elf", (char *)strtab); 185 } 186 } 187 db_add_symbol_table(NULL, NULL, "kld", NULL); 188 return (1); /* We're the default debugger. */ 189} 190 191static int 192db_trap(int type, int code) 193{ 194 jmp_buf jb; 195 void *prev_jb; 196 boolean_t bkpt, watchpt; 197 const char *why; 198 199 /* 200 * Don't handle the trap if the console is unavailable (i.e. it 201 * is in graphics mode). 202 */ 203 if (cnunavailable()) 204 return (0); 205 206 bkpt = IS_BREAKPOINT_TRAP(type, code); 207 watchpt = IS_WATCHPOINT_TRAP(type, code); 208 209 if (db_stop_at_pc(&bkpt)) { 210 if (db_inst_count) { 211 db_printf("After %d instructions (%d loads, %d stores),\n", 212 db_inst_count, db_load_count, db_store_count); 213 } 214 prev_jb = kdb_jmpbuf(jb); 215 if (setjmp(jb) == 0) { 216 db_dot = PC_REGS(); 217 db_print_thread(); 218 if (bkpt) 219 db_printf("Breakpoint at\t"); 220 else if (watchpt) 221 db_printf("Watchpoint at\t"); 222 else 223 db_printf("Stopped at\t"); 224 db_print_loc_and_inst(db_dot); 225 } 226 why = kdb_why; 227 db_script_kdbenter(why != KDB_WHY_UNSET ? why : "unknown"); 228 db_command_loop(); 229 (void)kdb_jmpbuf(prev_jb); 230 } 231 232 db_restart_at_pc(watchpt); 233 234 return (1); 235} 236 237static void 238db_trace_self_wrapper(void) 239{ 240 jmp_buf jb; 241 void *prev_jb; 242 243 prev_jb = kdb_jmpbuf(jb); 244 if (setjmp(jb) == 0) 245 db_trace_self(); 246 (void)kdb_jmpbuf(prev_jb); 247} 248