1 /* $NetBSD: db_sym.c,v 1.50 2005/03/28 01:05:13 christos Exp $ */ 2 3 /* 4 * Mach Operating System 5 * Copyright (c) 1991,1990 Carnegie Mellon University 6 * All Rights Reserved. 7 * 8 * Permission to use, copy, modify and distribute this software and its 9 * documentation is hereby granted, provided that both the copyright 10 * notice and this permission notice appear in all copies of the 11 * software, derivative works or modified versions, and any portions 12 * thereof, and that both notices appear in supporting documentation. 13 * 14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 17 * 18 * Carnegie Mellon requests users of this software to return to 19 * 20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 21 * School of Computer Science 22 * Carnegie Mellon University 23 * Pittsburgh PA 15213-3890 24 * 25 * any improvements or extensions that they make and grant Carnegie the 26 * rights to redistribute these changes. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: db_sym.c,v 1.50 2005/03/28 01:05:13 christos Exp $"); 31 32 #include "opt_ddbparam.h" 33 34 #include <sys/param.h> 35 #include <sys/proc.h> 36 #include <sys/systm.h> 37 #include <sys/ksyms.h> 38 39 #include <machine/db_machdep.h> 40 41 #include <ddb/db_lex.h> 42 #include <ddb/db_sym.h> 43 #include <ddb/db_output.h> 44 #include <ddb/db_extern.h> 45 #include <ddb/db_command.h> 46 47 static void db_symsplit(char *, char **, char **); 48 49 50 #ifdef DB_AOUT_SYMBOLS 51 #define TBLNAME "netbsd" 52 53 static int using_aout_symtab; 54 const db_symformat_t *db_symformat; 55 static db_forall_func_t db_sift; 56 extern db_symformat_t db_symformat_aout; 57 #endif 58 59 60 /* 61 * Initialize the kernel debugger by initializing the master symbol 62 * table. Note that if initializing the master symbol table fails, 63 * no other symbol tables can be loaded. 64 */ 65 void 66 ddb_init(int symsize, void *vss, void *vse) 67 { 68 #ifdef DB_AOUT_SYMBOLS 69 db_symformat = &db_symformat_aout; 70 if ((*db_symformat->sym_init)(symsize, vss, vse, TBLNAME) == TRUE) { 71 using_aout_symtab = TRUE; 72 return; 73 } 74 #endif 75 ksyms_init(symsize, vss, vse); /* Will complain if necessary */ 76 } 77 78 boolean_t 79 db_eqname(char *src, char *dst, int c) 80 { 81 82 if (!strcmp(src, dst)) 83 return (TRUE); 84 if (src[0] == c) 85 return (!strcmp(src+1,dst)); 86 return (FALSE); 87 } 88 89 boolean_t 90 db_value_of_name(char *name, db_expr_t *valuep) 91 { 92 char *mod, *sym; 93 unsigned long uval; 94 long val; 95 96 #ifdef DB_AOUT_SYMBOLS 97 db_sym_t ssym; 98 99 if (using_aout_symtab) { 100 /* 101 * Cannot load symtabs in a.out kernels, so the ':' 102 * style of selecting modules is irrelevant. 103 */ 104 ssym = (*db_symformat->sym_lookup)(NULL, name); 105 if (ssym == DB_SYM_NULL) 106 return (FALSE); 107 db_symbol_values(ssym, &name, valuep); 108 return (TRUE); 109 } 110 #endif 111 db_symsplit(name, &mod, &sym); 112 if (ksyms_getval(mod, sym, &uval, KSYMS_EXTERN) == 0) { 113 val = (long) uval; 114 *valuep = (db_expr_t)val; 115 return TRUE; 116 } 117 if (ksyms_getval(mod, sym, &uval, KSYMS_ANY) == 0) { 118 val = (long) uval; 119 *valuep = (db_expr_t)val; 120 return TRUE; 121 } 122 return FALSE; 123 } 124 125 #ifdef DB_AOUT_SYMBOLS 126 /* Private structure for passing args to db_sift() from db_sifting(). */ 127 struct db_sift_args { 128 char *symstr; 129 int mode; 130 }; 131 132 /* 133 * Does the work of db_sifting(), called once for each 134 * symbol via db_forall(), prints out symbols matching 135 * criteria. 136 */ 137 static void 138 db_sift(db_symtab_t *stab, db_sym_t sym, char *name, char *suffix, int prefix, 139 void *arg) 140 { 141 char c, sc; 142 char *find, *p; 143 size_t len; 144 struct db_sift_args *dsa; 145 146 dsa = (struct db_sift_args*)arg; 147 148 find = dsa->symstr; /* String we're looking for. */ 149 p = name; /* String we're searching within. */ 150 151 /* Matching algorithm cribbed from strstr(), which is not 152 in the kernel. */ 153 if ((c = *find++) != 0) { 154 len = strlen(find); 155 do { 156 do { 157 if ((sc = *p++) == 0) 158 return; 159 } while (sc != c); 160 } while (strncmp(p, find, len) != 0); 161 } 162 if (dsa->mode=='F') /* ala ls -F */ 163 db_printf("%s%s ", name, suffix); 164 else 165 db_printf("%s ", name); 166 } 167 #endif 168 169 /* 170 * "Sift" for a partial symbol. 171 * Named for the Sun OpenPROM command ("sifting"). 172 * If the symbol has a qualifier (e.g., ux:vm_map), 173 * then only the specified symbol table will be searched; 174 * otherwise, all symbol tables will be searched.. 175 * 176 * "mode" is how-to-display, set from modifiers. 177 */ 178 void 179 db_sifting(char *symstr, int mode) 180 { 181 char *mod, *sym; 182 183 #ifdef DB_AOUT_SYMBOLS 184 struct db_sift_args dsa; 185 186 if (using_aout_symtab) { 187 dsa.symstr = symstr; 188 dsa.mode = mode; 189 (*db_symformat->sym_forall)(NULL, db_sift, &dsa); 190 db_printf("\n"); 191 return; 192 } 193 #endif 194 195 db_symsplit(symstr, &mod, &sym); 196 if (ksyms_sift(mod, sym, mode) == ENODEV) 197 db_error("invalid symbol table name"); 198 } 199 200 /* 201 * Find the closest symbol to val, and return its name 202 * and the difference between val and the symbol found. 203 */ 204 db_sym_t 205 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp) 206 { 207 unsigned int diff; 208 unsigned long naddr; 209 db_sym_t ret = DB_SYM_NULL; 210 const char *mod; 211 char *sym; 212 213 #ifdef DB_AOUT_SYMBOLS 214 db_expr_t newdiff; 215 db_sym_t ssym; 216 217 if (using_aout_symtab) { 218 newdiff = diff = ~0; 219 ssym = (*db_symformat->sym_search) 220 (NULL, val, strategy, &newdiff); 221 if ((unsigned int) newdiff < diff) { 222 diff = newdiff; 223 ret = ssym; 224 } 225 *offp = diff; 226 return ret; 227 } 228 #endif 229 230 if (ksyms_getname(&mod, &sym, (vaddr_t)val, strategy) == 0) { 231 (void)ksyms_getval(mod, sym, &naddr, KSYMS_ANY); 232 diff = val - (db_addr_t)naddr; 233 ret = (db_sym_t)naddr; 234 } else 235 diff = 0; 236 *offp = diff; 237 return ret; 238 } 239 240 /* 241 * Return name and value of a symbol 242 */ 243 void 244 db_symbol_values(db_sym_t sym, char **namep, db_expr_t *valuep) 245 { 246 const char *mod; 247 248 if (sym == DB_SYM_NULL) { 249 *namep = 0; 250 return; 251 } 252 253 #ifdef DB_AOUT_SYMBOLS 254 if (using_aout_symtab) { 255 db_expr_t value; 256 (*db_symformat->sym_value)(NULL, sym, namep, &value); 257 if (valuep) 258 *valuep = value; 259 return; 260 } 261 #endif 262 263 if (ksyms_getname(&mod, namep, (vaddr_t)sym, 264 KSYMS_ANY|KSYMS_EXACT) == 0) { 265 if (valuep) 266 *valuep = sym; 267 } else 268 *namep = NULL; 269 } 270 271 272 /* 273 * Print a the closest symbol to value 274 * 275 * After matching the symbol according to the given strategy 276 * we print it in the name+offset format, provided the symbol's 277 * value is close enough (eg smaller than db_maxoff). 278 * We also attempt to print [filename:linenum] when applicable 279 * (eg for procedure names). 280 * 281 * If we could not find a reasonable name+offset representation, 282 * then we just print the value in hex. Small values might get 283 * bogus symbol associations, e.g. 3 might get some absolute 284 * value like _INCLUDE_VERSION or something, therefore we do 285 * not accept symbols whose value is zero (and use plain hex). 286 * Also, avoid printing as "end+0x????" which is useless. 287 * The variable db_lastsym is used instead of "end" in case we 288 * add support for symbols in loadable driver modules. 289 */ 290 extern char end[]; 291 unsigned long db_lastsym = (unsigned long)end; 292 unsigned int db_maxoff = 0x100000; 293 294 void 295 db_symstr(char *buf, size_t buflen, db_expr_t off, db_strategy_t strategy) 296 { 297 char *name; 298 const char *mod; 299 unsigned long val; 300 301 #ifdef DB_AOUT_SYMBOLS 302 if (using_aout_symtab) { 303 db_expr_t d; 304 char *filename; 305 char *name; 306 db_expr_t value; 307 int linenum; 308 db_sym_t cursym; 309 310 if ((unsigned long) off <= db_lastsym) { 311 cursym = db_search_symbol(off, strategy, &d); 312 db_symbol_values(cursym, &name, &value); 313 if (name != NULL && 314 ((unsigned int) d < db_maxoff) && 315 value != 0) { 316 strlcpy(buf, name, buflen); 317 if (d) { 318 strlcat(buf, "+", buflen); 319 db_format_radix(buf+strlen(buf), 320 24, d, TRUE); 321 } 322 if (strategy == DB_STGY_PROC) { 323 if ((*db_symformat->sym_line_at_pc) 324 (NULL, cursym, &filename, 325 &linenum, off)) 326 snprintf(buf + strlen(buf), 327 buflen - strlen(buf), 328 " [%s:%d]", 329 filename, linenum); 330 } 331 return; 332 } 333 } 334 strlcpy(buf, db_num_to_str(off), buflen); 335 return; 336 } 337 #endif 338 if (ksyms_getname(&mod, &name, (vaddr_t)off, 339 strategy|KSYMS_CLOSEST) == 0) { 340 (void)ksyms_getval(mod, name, &val, KSYMS_ANY); 341 if (((off - val) < db_maxoff) && val) { 342 snprintf(buf, buflen, "%s:%s", mod, name); 343 if (off - val) { 344 strlcat(buf, "+", buflen); 345 db_format_radix(buf+strlen(buf), 346 24, off - val, TRUE); 347 } 348 #ifdef notyet 349 if (strategy & KSYMS_PROC) { 350 if (ksyms_fmaddr(off, &filename, &linenum) == 0) 351 snprintf(buf + strlen(buf), 352 buflen - strlen(buf), 353 " [%s:%d]", filename, linenum); 354 } 355 #endif 356 return; 357 } 358 } 359 strlcpy(buf, db_num_to_str(off), buflen); 360 } 361 362 void 363 db_printsym(db_expr_t off, db_strategy_t strategy, 364 void (*pr)(const char *, ...)) 365 { 366 char *name; 367 const char *mod; 368 unsigned long uval; 369 long val; 370 #ifdef notyet 371 char *filename; 372 int linenum; 373 #endif 374 375 #ifdef DB_AOUT_SYMBOLS 376 if (using_aout_symtab) { 377 db_expr_t d; 378 char *filename; 379 char *name; 380 db_expr_t value; 381 int linenum; 382 db_sym_t cursym; 383 if ((unsigned long) off <= db_lastsym) { 384 cursym = db_search_symbol(off, strategy, &d); 385 db_symbol_values(cursym, &name, &value); 386 if (name != NULL && 387 ((unsigned int) d < db_maxoff) && 388 value != 0) { 389 (*pr)("%s", name); 390 if (d) { 391 char tbuf[24]; 392 393 db_format_radix(tbuf, 24, d, TRUE); 394 (*pr)("+%s", tbuf); 395 } 396 if (strategy == DB_STGY_PROC) { 397 if ((*db_symformat->sym_line_at_pc) 398 (NULL, cursym, &filename, 399 &linenum, off)) 400 (*pr)(" [%s:%d]", 401 filename, linenum); 402 } 403 return; 404 } 405 } 406 (*pr)(db_num_to_str(off)); 407 return; 408 } 409 #endif 410 if (ksyms_getname(&mod, &name, (vaddr_t)off, 411 strategy|KSYMS_CLOSEST) == 0) { 412 (void)ksyms_getval(mod, name, &uval, KSYMS_ANY); 413 val = (long) uval; 414 if (((off - val) < db_maxoff) && val) { 415 (*pr)("%s:%s", mod, name); 416 if (off - val) { 417 char tbuf[24]; 418 419 db_format_radix(tbuf, 24, off - val, TRUE); 420 (*pr)("+%s", tbuf); 421 } 422 #ifdef notyet 423 if (strategy & KSYMS_PROC) { 424 if (ksyms_fmaddr(off, &filename, &linenum) == 0) 425 (*pr)(" [%s:%d]", filename, linenum); 426 } 427 #endif 428 return; 429 } 430 } 431 (*pr)(db_num_to_str(off)); 432 return; 433 } 434 435 /* 436 * Splits a string in the form "mod:sym" to two strings. 437 */ 438 static void 439 db_symsplit(char *str, char **mod, char **sym) 440 { 441 char *cp; 442 443 if ((cp = strchr(str, ':')) != NULL) { 444 *cp++ = '\0'; 445 *mod = str; 446 *sym = cp; 447 } else { 448 *mod = NULL; 449 *sym = str; 450 } 451 } 452 453 boolean_t 454 db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep) 455 { 456 #ifdef DB_AOUT_SYMBOLS 457 if (using_aout_symtab) 458 return ((*db_symformat->sym_numargs)(NULL, cursym, nargp, 459 argnamep)); 460 #endif 461 return (FALSE); 462 } 463 464