db_main.c revision 174914
1160814Ssimon/*- 2160814Ssimon * Mach Operating System 3160814Ssimon * Copyright (c) 1991,1990 Carnegie Mellon University 4160814Ssimon * All Rights Reserved. 5160814Ssimon * 6160814Ssimon * Permission to use, copy, modify and distribute this software and its 7160814Ssimon * documentation is hereby granted, provided that both the copyright 8160814Ssimon * notice and this permission notice appear in all copies of the 9160814Ssimon * software, derivative works or modified versions, and any portions 10160814Ssimon * thereof, and that both notices appear in supporting documentation. 11160814Ssimon * 12160814Ssimon * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS 13160814Ssimon * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 14160814Ssimon * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 15160814Ssimon * 16160814Ssimon * Carnegie Mellon requests users of this software to return to 17160814Ssimon * 18160814Ssimon * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 19160814Ssimon * School of Computer Science 20160814Ssimon * Carnegie Mellon University 21160814Ssimon * Pittsburgh PA 15213-3890 22160814Ssimon * 23160814Ssimon * any improvements or extensions that they make and grant Carnegie the 24160814Ssimon * rights to redistribute these changes. 25160814Ssimon */ 26160814Ssimon 27160814Ssimon#include <sys/cdefs.h> 28160814Ssimon__FBSDID("$FreeBSD: head/sys/ddb/db_main.c 174914 2007-12-26 09:33:19Z rwatson $"); 29160814Ssimon 30160814Ssimon#include <sys/param.h> 31160814Ssimon#include <sys/systm.h> 32160814Ssimon#include <sys/cons.h> 33160814Ssimon#include <sys/linker.h> 34160814Ssimon#include <sys/kdb.h> 35160814Ssimon#include <sys/kernel.h> 36160814Ssimon#include <sys/pcpu.h> 37160814Ssimon#include <sys/proc.h> 38160814Ssimon#include <sys/reboot.h> 39160814Ssimon#include <sys/sysctl.h> 40160814Ssimon 41160814Ssimon#include <machine/kdb.h> 42160814Ssimon#include <machine/pcb.h> 43160814Ssimon#include <machine/setjmp.h> 44160814Ssimon 45160814Ssimon#include <ddb/ddb.h> 46160814Ssimon#include <ddb/db_command.h> 47160814Ssimon#include <ddb/db_sym.h> 48160814Ssimon 49160814SsimonSYSCTL_NODE(_debug, OID_AUTO, ddb, CTLFLAG_RW, 0, "DDB settings"); 50160814Ssimon 51160814Ssimonstatic dbbe_init_f db_init; 52160814Ssimonstatic dbbe_trap_f db_trap; 53160814Ssimonstatic dbbe_trace_f db_trace_self_wrapper; 54160814Ssimon 55160814SsimonKDB_BACKEND(ddb, db_init, db_trace_self_wrapper, db_trap); 56160814Ssimon 57160814Ssimonvm_offset_t ksym_start, ksym_end; 58160814Ssimon 59160814Ssimonboolean_t 60160814SsimonX_db_line_at_pc(db_symtab_t *symtab, c_db_sym_t sym, char **file, int *line, 61160814Ssimon db_expr_t off) 62160814Ssimon{ 63160814Ssimon return (FALSE); 64160814Ssimon} 65160814Ssimon 66160814Ssimonc_db_sym_t 67160814SsimonX_db_lookup(db_symtab_t *symtab, const char *symbol) 68160814Ssimon{ 69160814Ssimon c_linker_sym_t lsym; 70160814Ssimon Elf_Sym *sym; 71160814Ssimon 72160814Ssimon if (symtab->private == NULL) { 73160814Ssimon return ((c_db_sym_t)((!linker_ddb_lookup(symbol, &lsym)) 74160814Ssimon ? lsym : NULL)); 75160814Ssimon } else { 76160814Ssimon sym = (Elf_Sym *)symtab->start; 77160814Ssimon while ((char *)sym < symtab->end) { 78160814Ssimon if (sym->st_name != 0 && 79160814Ssimon !strcmp(symtab->private + sym->st_name, symbol)) 80160814Ssimon return ((c_db_sym_t)sym); 81160814Ssimon sym++; 82160814Ssimon } 83160814Ssimon } 84160814Ssimon return (NULL); 85160814Ssimon} 86160814Ssimon 87160814Ssimonc_db_sym_t 88160814SsimonX_db_search_symbol(db_symtab_t *symtab, db_addr_t off, db_strategy_t strat, 89160814Ssimon db_expr_t *diffp) 90160814Ssimon{ 91160814Ssimon c_linker_sym_t lsym; 92160814Ssimon Elf_Sym *sym, *match; 93160814Ssimon unsigned long diff; 94160814Ssimon 95160814Ssimon if (symtab->private == NULL) { 96160814Ssimon if (!linker_ddb_search_symbol((caddr_t)off, &lsym, &diff)) { 97160814Ssimon *diffp = (db_expr_t)diff; 98160814Ssimon return ((c_db_sym_t)lsym); 99160814Ssimon } 100160814Ssimon return (NULL); 101160814Ssimon } 102160814Ssimon 103160814Ssimon diff = ~0UL; 104160814Ssimon match = NULL; 105160814Ssimon for (sym = (Elf_Sym*)symtab->start; (char*)sym < symtab->end; sym++) { 106160814Ssimon if (sym->st_name == 0) 107160814Ssimon continue; 108160814Ssimon if (off < sym->st_value) 109160814Ssimon continue; 110160814Ssimon if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT && 111160814Ssimon ELF_ST_TYPE(sym->st_info) != STT_FUNC && 112160814Ssimon ELF_ST_TYPE(sym->st_info) != STT_NOTYPE) 113160814Ssimon continue; 114160814Ssimon if ((off - sym->st_value) > diff) 115160814Ssimon continue; 116160814Ssimon if ((off - sym->st_value) < diff) { 117160814Ssimon diff = off - sym->st_value; 118160814Ssimon match = sym; 119160814Ssimon } else { 120160814Ssimon if (match == NULL) 121160814Ssimon match = sym; 122160814Ssimon else if (ELF_ST_BIND(match->st_info) == STB_LOCAL && 123160814Ssimon ELF_ST_BIND(sym->st_info) != STB_LOCAL) 124160814Ssimon match = sym; 125160814Ssimon } 126160814Ssimon if (diff == 0) { 127160814Ssimon if (strat == DB_STGY_PROC && 128160814Ssimon ELF_ST_TYPE(sym->st_info) == STT_FUNC && 129160814Ssimon ELF_ST_BIND(sym->st_info) != STB_LOCAL) 130160814Ssimon break; 131160814Ssimon if (strat == DB_STGY_ANY && 132160814Ssimon ELF_ST_BIND(sym->st_info) != STB_LOCAL) 133160814Ssimon break; 134160814Ssimon } 135160814Ssimon } 136160814Ssimon 137160814Ssimon *diffp = (match == NULL) ? off : diff; 138160814Ssimon return ((c_db_sym_t)match); 139160814Ssimon} 140160814Ssimon 141160814Ssimonboolean_t 142160814SsimonX_db_sym_numargs(db_symtab_t *symtab, c_db_sym_t sym, int *nargp, 143160814Ssimon char **argp) 144160814Ssimon{ 145160814Ssimon return (FALSE); 146160814Ssimon} 147160814Ssimon 148160814Ssimonvoid 149160814SsimonX_db_symbol_values(db_symtab_t *symtab, c_db_sym_t sym, const char **namep, 150160814Ssimon db_expr_t *valp) 151160814Ssimon{ 152160814Ssimon linker_symval_t lval; 153160814Ssimon 154160814Ssimon if (symtab->private == NULL) { 155160814Ssimon linker_ddb_symbol_values((c_linker_sym_t)sym, &lval); 156160814Ssimon if (namep != NULL) 157160814Ssimon *namep = (const char*)lval.name; 158160814Ssimon if (valp != NULL) 159160814Ssimon *valp = (db_expr_t)lval.value; 160160814Ssimon } else { 161160814Ssimon if (namep != NULL) 162160814Ssimon *namep = (const char *)symtab->private + 163160814Ssimon ((const Elf_Sym *)sym)->st_name; 164160814Ssimon if (valp != NULL) 165160814Ssimon *valp = (db_expr_t)((const Elf_Sym *)sym)->st_value; 166160814Ssimon } 167160814Ssimon} 168160814Ssimon 169160814Ssimonstatic int 170160814Ssimondb_init(void) 171160814Ssimon{ 172160814Ssimon uintptr_t symtab, strtab; 173160814Ssimon Elf_Size tabsz, strsz; 174160814Ssimon 175160814Ssimon if (ksym_end > ksym_start && ksym_start != 0) { 176160814Ssimon symtab = ksym_start; 177160814Ssimon tabsz = *((Elf_Size*)symtab); 178160814Ssimon symtab += sizeof(Elf_Size); 179160814Ssimon strtab = symtab + tabsz; 180160814Ssimon strsz = *((Elf_Size*)strtab); 181160814Ssimon strtab += sizeof(Elf_Size); 182160814Ssimon if (strtab + strsz <= ksym_end) { 183160814Ssimon db_add_symbol_table((char *)symtab, 184160814Ssimon (char *)(symtab + tabsz), "elf", (char *)strtab); 185160814Ssimon } 186160814Ssimon } 187160814Ssimon db_add_symbol_table(NULL, NULL, "kld", NULL); 188160814Ssimon 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