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