db_sym.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 * Author: David B. Golub, Carnegie Mellon University 28 * Date: 7/90 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/sys/ddb/db_sym.c 139747 2005-01-06 01:34:41Z imp $"); 33 34#include <sys/param.h> 35#include <sys/systm.h> 36 37#include <ddb/ddb.h> 38#include <ddb/db_sym.h> 39 40#include <opt_ddb.h> 41 42/* 43 * Multiple symbol tables 44 */ 45#ifndef MAXNOSYMTABS 46#define MAXNOSYMTABS 3 /* mach, ux, emulator */ 47#endif 48 49static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},}; 50static int db_nsymtab = 0; 51 52static db_symtab_t *db_last_symtab; /* where last symbol was found */ 53 54static c_db_sym_t db_lookup( const char *symstr); 55static char *db_qualify(c_db_sym_t sym, char *symtabname); 56static boolean_t db_symbol_is_ambiguous(c_db_sym_t sym); 57static boolean_t db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t); 58 59/* 60 * Add symbol table, with given name, to list of symbol tables. 61 */ 62void 63db_add_symbol_table(start, end, name, ref) 64 char *start; 65 char *end; 66 char *name; 67 char *ref; 68{ 69 if (db_nsymtab >= MAXNOSYMTABS) { 70 printf ("No slots left for %s symbol table", name); 71 panic ("db_sym.c: db_add_symbol_table"); 72 } 73 74 db_symtabs[db_nsymtab].start = start; 75 db_symtabs[db_nsymtab].end = end; 76 db_symtabs[db_nsymtab].name = name; 77 db_symtabs[db_nsymtab].private = ref; 78 db_nsymtab++; 79} 80 81/* 82 * db_qualify("vm_map", "ux") returns "unix:vm_map". 83 * 84 * Note: return value points to static data whose content is 85 * overwritten by each call... but in practice this seems okay. 86 */ 87static char * 88db_qualify(sym, symtabname) 89 c_db_sym_t sym; 90 register char *symtabname; 91{ 92 const char *symname; 93 static char tmp[256]; 94 95 db_symbol_values(sym, &symname, 0); 96 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname); 97 return tmp; 98} 99 100 101boolean_t 102db_eqname(src, dst, c) 103 const char *src; 104 const char *dst; 105 int c; 106{ 107 if (!strcmp(src, dst)) 108 return (TRUE); 109 if (src[0] == c) 110 return (!strcmp(src+1,dst)); 111 return (FALSE); 112} 113 114boolean_t 115db_value_of_name(name, valuep) 116 const char *name; 117 db_expr_t *valuep; 118{ 119 c_db_sym_t sym; 120 121 sym = db_lookup(name); 122 if (sym == C_DB_SYM_NULL) 123 return (FALSE); 124 db_symbol_values(sym, &name, valuep); 125 return (TRUE); 126} 127 128 129/* 130 * Lookup a symbol. 131 * If the symbol has a qualifier (e.g., ux:vm_map), 132 * then only the specified symbol table will be searched; 133 * otherwise, all symbol tables will be searched. 134 */ 135static c_db_sym_t 136db_lookup(symstr) 137 const char *symstr; 138{ 139 c_db_sym_t sp; 140 register int i; 141 int symtab_start = 0; 142 int symtab_end = db_nsymtab; 143 register const char *cp; 144 145 /* 146 * Look for, remove, and remember any symbol table specifier. 147 */ 148 for (cp = symstr; *cp; cp++) { 149 if (*cp == ':') { 150 for (i = 0; i < db_nsymtab; i++) { 151 int n = strlen(db_symtabs[i].name); 152 153 if ( 154 n == (cp - symstr) && 155 strncmp(symstr, db_symtabs[i].name, n) == 0 156 ) { 157 symtab_start = i; 158 symtab_end = i + 1; 159 break; 160 } 161 } 162 if (i == db_nsymtab) { 163 db_error("invalid symbol table name"); 164 } 165 symstr = cp+1; 166 } 167 } 168 169 /* 170 * Look in the specified set of symbol tables. 171 * Return on first match. 172 */ 173 for (i = symtab_start; i < symtab_end; i++) { 174 sp = X_db_lookup(&db_symtabs[i], symstr); 175 if (sp) { 176 db_last_symtab = &db_symtabs[i]; 177 return sp; 178 } 179 } 180 return 0; 181} 182 183/* 184 * If TRUE, check across symbol tables for multiple occurrences 185 * of a name. Might slow things down quite a bit. 186 */ 187static volatile boolean_t db_qualify_ambiguous_names = FALSE; 188 189/* 190 * Does this symbol name appear in more than one symbol table? 191 * Used by db_symbol_values to decide whether to qualify a symbol. 192 */ 193static boolean_t 194db_symbol_is_ambiguous(sym) 195 c_db_sym_t sym; 196{ 197 const char *sym_name; 198 register int i; 199 register 200 boolean_t found_once = FALSE; 201 202 if (!db_qualify_ambiguous_names) 203 return FALSE; 204 205 db_symbol_values(sym, &sym_name, 0); 206 for (i = 0; i < db_nsymtab; i++) { 207 if (X_db_lookup(&db_symtabs[i], sym_name)) { 208 if (found_once) 209 return TRUE; 210 found_once = TRUE; 211 } 212 } 213 return FALSE; 214} 215 216/* 217 * Find the closest symbol to val, and return its name 218 * and the difference between val and the symbol found. 219 */ 220c_db_sym_t 221db_search_symbol( val, strategy, offp) 222 register db_addr_t val; 223 db_strategy_t strategy; 224 db_expr_t *offp; 225{ 226 register 227 unsigned int diff; 228 size_t newdiff; 229 register int i; 230 c_db_sym_t ret = C_DB_SYM_NULL, sym; 231 232 newdiff = diff = ~0; 233 db_last_symtab = 0; 234 for (i = 0; i < db_nsymtab; i++) { 235 sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff); 236 if (newdiff < diff) { 237 db_last_symtab = &db_symtabs[i]; 238 diff = newdiff; 239 ret = sym; 240 } 241 } 242 *offp = diff; 243 return ret; 244} 245 246/* 247 * Return name and value of a symbol 248 */ 249void 250db_symbol_values(sym, namep, valuep) 251 c_db_sym_t sym; 252 const char **namep; 253 db_expr_t *valuep; 254{ 255 db_expr_t value; 256 257 if (sym == DB_SYM_NULL) { 258 *namep = 0; 259 return; 260 } 261 262 X_db_symbol_values(db_last_symtab, sym, namep, &value); 263 264 if (db_symbol_is_ambiguous(sym)) 265 *namep = db_qualify(sym, db_last_symtab->name); 266 if (valuep) 267 *valuep = value; 268} 269 270 271/* 272 * Print a the closest symbol to value 273 * 274 * After matching the symbol according to the given strategy 275 * we print it in the name+offset format, provided the symbol's 276 * value is close enough (eg smaller than db_maxoff). 277 * We also attempt to print [filename:linenum] when applicable 278 * (eg for procedure names). 279 * 280 * If we could not find a reasonable name+offset representation, 281 * then we just print the value in hex. Small values might get 282 * bogus symbol associations, e.g. 3 might get some absolute 283 * value like _INCLUDE_VERSION or something, therefore we do 284 * not accept symbols whose value is "small" (and use plain hex). 285 */ 286 287db_expr_t db_maxoff = 0x10000; 288 289void 290db_printsym(off, strategy) 291 db_expr_t off; 292 db_strategy_t strategy; 293{ 294 db_expr_t d; 295 char *filename; 296 const char *name; 297 db_expr_t value; 298 int linenum; 299 c_db_sym_t cursym; 300 301 cursym = db_search_symbol(off, strategy, &d); 302 db_symbol_values(cursym, &name, &value); 303 if (name == 0) 304 value = off; 305 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) { 306 db_printf("%+#lr", (long)off); 307 return; 308 } 309 if (name == 0 || d >= (unsigned long)db_maxoff) { 310 db_printf("%#lr", (unsigned long)off); 311 return; 312 } 313#ifdef DDB_NUMSYM 314 db_printf("%#lr = %s", (unsigned long)off, name); 315#else 316 db_printf("%s", name); 317#endif 318 if (d) 319 db_printf("+%+#lr", (long)d); 320 if (strategy == DB_STGY_PROC) { 321 if (db_line_at_pc(cursym, &filename, &linenum, off)) 322 db_printf(" [%s:%d]", filename, linenum); 323 } 324} 325 326static boolean_t 327db_line_at_pc( sym, filename, linenum, pc) 328 c_db_sym_t sym; 329 char **filename; 330 int *linenum; 331 db_expr_t pc; 332{ 333 return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc); 334} 335 336int 337db_sym_numargs(sym, nargp, argnames) 338 c_db_sym_t sym; 339 int *nargp; 340 char **argnames; 341{ 342 return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames); 343} 344