1 /* $NetBSD: db_sym.c,v 1.61 2011/04/11 04:26:18 mrg 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.61 2011/04/11 04:26:18 mrg Exp $"); 31 32 #ifdef _KERNEL_OPT 33 #include "opt_ddbparam.h" 34 #endif 35 36 #include <sys/param.h> 37 #include <sys/proc.h> 38 #include <sys/systm.h> 39 #include <sys/ksyms.h> 40 41 #include <ddb/ddb.h> 42 43 static void db_symsplit(char *, char **, char **); 44 45 46 #ifndef _KERNEL 47 #define TBLNAME "netbsd" 48 49 #define use_ksyms 0 50 51 const db_symformat_t *db_symformat; 52 static db_forall_func_t db_sift; 53 extern db_symformat_t db_symformat_aout; 54 extern db_symformat_t db_symformat_elf; 55 #endif 56 57 58 /* 59 * Initialize the kernel debugger by initializing the master symbol 60 * table. Note that if initializing the master symbol table fails, 61 * no other symbol tables can be loaded. 62 */ 63 void 64 ddb_init(int symsize, void *vss, void *vse) 65 { 66 #ifdef _KERNEL 67 ksyms_addsyms_elf(symsize, vss, vse); /* Will complain if necessary */ 68 #else /* _KERNEL */ 69 db_symformat = &db_symformat_elf; 70 if ((*db_symformat->sym_init)(symsize, vss, vse, TBLNAME) != true) 71 printf("sym_init failed"); 72 #endif /* _KERNEL */ 73 } 74 75 bool 76 db_eqname(const char *src, const char *dst, int c) 77 { 78 79 if (!strcmp(src, dst)) 80 return (true); 81 if (src[0] == c) 82 return (!strcmp(src+1,dst)); 83 return (false); 84 } 85 86 bool 87 db_value_of_name(const char *name, db_expr_t *valuep) 88 { 89 char symbol[128]; 90 char *mod, *sym; 91 #ifdef _KERNEL 92 unsigned long uval; 93 long val; 94 #endif 95 96 #ifndef _KERNEL 97 if (!use_ksyms) { 98 db_sym_t ssym; 99 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 112 (void)strlcpy(symbol, name, sizeof(symbol)); 113 db_symsplit(symbol, &mod, &sym); 114 #ifdef _KERNEL 115 if (ksyms_getval_unlocked(mod, sym, &uval, KSYMS_EXTERN) == 0) { 116 val = (long) uval; 117 *valuep = (db_expr_t)val; 118 return true; 119 } 120 if (ksyms_getval_unlocked(mod, sym, &uval, KSYMS_ANY) == 0) { 121 val = (long) uval; 122 *valuep = (db_expr_t)val; 123 return true; 124 } 125 #endif 126 return false; 127 } 128 129 #ifndef _KERNEL 130 /* Private structure for passing args to db_sift() from db_sifting(). */ 131 struct db_sift_args { 132 char *symstr; 133 int mode; 134 }; 135 136 /* 137 * Does the work of db_sifting(), called once for each 138 * symbol via db_forall(), prints out symbols matching 139 * criteria. 140 */ 141 static void 142 db_sift(db_symtab_t *stab, db_sym_t sym, char *name, 143 char *suffix, int prefix, void *arg) 144 { 145 char c, sc; 146 char *find, *p; 147 size_t len; 148 struct db_sift_args *dsa; 149 150 dsa = (struct db_sift_args*)arg; 151 152 find = dsa->symstr; /* String we're looking for. */ 153 p = name; /* String we're searching within. */ 154 155 /* Matching algorithm cribbed from strstr(), which is not 156 in the kernel. */ 157 if ((c = *find++) != 0) { 158 len = strlen(find); 159 do { 160 do { 161 if ((sc = *p++) == 0) 162 return; 163 } while (sc != c); 164 } while (strncmp(p, find, len) != 0); 165 } 166 if (dsa->mode=='F') /* ala ls -F */ 167 db_printf("%s%s ", name, suffix); 168 else 169 db_printf("%s ", name); 170 } 171 #endif 172 173 /* 174 * "Sift" for a partial symbol. 175 * Named for the Sun OpenPROM command ("sifting"). 176 * If the symbol has a qualifier (e.g., ux:vm_map), 177 * then only the specified symbol table will be searched; 178 * otherwise, all symbol tables will be searched.. 179 * 180 * "mode" is how-to-display, set from modifiers. 181 */ 182 void 183 db_sifting(char *symstr, int mode) 184 { 185 #ifdef _KERNEL 186 char *mod, *sym; 187 #endif 188 189 #ifndef _KERNEL 190 struct db_sift_args dsa; 191 192 if (!use_ksyms) { 193 dsa.symstr = symstr; 194 dsa.mode = mode; 195 (*db_symformat->sym_forall)(NULL, db_sift, &dsa); 196 db_printf("\n"); 197 return; 198 } 199 #endif 200 201 #ifdef _KERNEL 202 db_symsplit(symstr, &mod, &sym); 203 if (ksyms_sift(mod, sym, mode) == ENODEV) 204 db_error("invalid symbol table name"); 205 #endif 206 } 207 208 /* 209 * Find the closest symbol to val, and return its name 210 * and the difference between val and the symbol found. 211 */ 212 db_sym_t 213 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp) 214 { 215 unsigned int diff; 216 db_sym_t ret = DB_SYM_NULL; 217 #ifdef _KERNEL 218 unsigned long naddr; 219 const char *mod; 220 const char *sym; 221 #endif 222 223 #ifndef _KERNEL 224 if (!use_ksyms) { 225 db_expr_t newdiff; 226 db_sym_t ssym; 227 228 newdiff = diff = ~0; 229 ssym = (*db_symformat->sym_search) 230 (NULL, val, strategy, &newdiff); 231 if ((unsigned int) newdiff < diff) { 232 diff = newdiff; 233 ret = ssym; 234 } 235 *offp = diff; 236 return ret; 237 } 238 #endif 239 240 #ifdef _KERNEL 241 if (ksyms_getname(&mod, &sym, (vaddr_t)val, strategy) == 0) { 242 (void)ksyms_getval_unlocked(mod, sym, &naddr, KSYMS_ANY); 243 diff = val - (db_addr_t)naddr; 244 ret = (db_sym_t)naddr; 245 } else 246 #endif 247 diff = 0; 248 *offp = diff; 249 return ret; 250 } 251 252 /* 253 * Return name and value of a symbol 254 */ 255 void 256 db_symbol_values(db_sym_t sym, const char **namep, db_expr_t *valuep) 257 { 258 #ifdef _KERNEL 259 const char *mod; 260 #endif 261 262 if (sym == DB_SYM_NULL) { 263 *namep = 0; 264 return; 265 } 266 267 #ifndef _KERNEL 268 if (!use_ksyms) { 269 db_expr_t value; 270 271 (*db_symformat->sym_value)(NULL, sym, namep, &value); 272 if (valuep) 273 *valuep = value; 274 return; 275 } 276 #endif 277 278 #ifdef _KERNEL 279 if (ksyms_getname(&mod, namep, (vaddr_t)sym, 280 KSYMS_ANY|KSYMS_EXACT) == 0) { 281 if (valuep) 282 *valuep = sym; 283 } else 284 #endif 285 *namep = NULL; 286 } 287 288 289 /* 290 * Print a the closest symbol to value 291 * 292 * After matching the symbol according to the given strategy 293 * we print it in the name+offset format, provided the symbol's 294 * value is close enough (eg smaller than db_maxoff). 295 * We also attempt to print [filename:linenum] when applicable 296 * (eg for procedure names). 297 * 298 * If we could not find a reasonable name+offset representation, 299 * then we just print the value in hex. Small values might get 300 * bogus symbol associations, e.g. 3 might get some absolute 301 * value like _INCLUDE_VERSION or something, therefore we do 302 * not accept symbols whose value is zero (and use plain hex). 303 * Also, avoid printing as "end+0x????" which is useless. 304 * The variable db_lastsym is used instead of "end" in case we 305 * add support for symbols in loadable driver modules. 306 */ 307 extern char end[]; 308 unsigned long db_lastsym = (unsigned long)end; 309 unsigned int db_maxoff = 0x100000; 310 311 void 312 db_symstr(char *buf, size_t buflen, db_expr_t off, db_strategy_t strategy) 313 { 314 const char *name; 315 #ifdef _KERNEL 316 const char *mod; 317 unsigned long val; 318 #endif 319 320 #ifndef _KERNEL 321 if (!use_ksyms) { 322 db_expr_t d; 323 char *filename; 324 db_expr_t value; 325 int linenum; 326 db_sym_t cursym; 327 328 if ((unsigned long) off <= db_lastsym) { 329 cursym = db_search_symbol(off, strategy, &d); 330 db_symbol_values(cursym, &name, &value); 331 if (name != NULL && 332 ((unsigned int) d < db_maxoff) && 333 value != 0) { 334 strlcpy(buf, name, buflen); 335 if (d) { 336 strlcat(buf, "+", buflen); 337 db_format_radix(buf+strlen(buf), 338 24, d, true); 339 } 340 if (strategy == DB_STGY_PROC) { 341 if ((*db_symformat->sym_line_at_pc) 342 (NULL, cursym, &filename, 343 &linenum, off)) 344 snprintf(buf + strlen(buf), 345 buflen - strlen(buf), 346 " [%s:%d]", 347 filename, linenum); 348 } 349 return; 350 } 351 } 352 strlcpy(buf, db_num_to_str(off), buflen); 353 return; 354 } 355 #endif 356 #ifdef _KERNEL 357 if (ksyms_getname(&mod, &name, (vaddr_t)off, 358 strategy|KSYMS_CLOSEST) == 0) { 359 (void)ksyms_getval_unlocked(mod, name, &val, KSYMS_ANY); 360 if (((off - val) < db_maxoff) && val) { 361 snprintf(buf, buflen, "%s:%s", mod, name); 362 if (off - val) { 363 strlcat(buf, "+", buflen); 364 db_format_radix(buf+strlen(buf), 365 24, off - val, true); 366 } 367 #ifdef notyet 368 if (strategy & KSYMS_PROC) { 369 if (ksyms_fmaddr(off, &filename, &linenum) == 0) 370 snprintf(buf + strlen(buf), 371 buflen - strlen(buf), 372 " [%s:%d]", filename, linenum); 373 } 374 #endif 375 return; 376 } 377 } 378 strlcpy(buf, db_num_to_str(off), buflen); 379 #endif 380 } 381 382 void 383 db_printsym(db_expr_t off, db_strategy_t strategy, 384 void (*pr)(const char *, ...)) 385 { 386 const char *name; 387 #ifdef _KERNEL 388 const char *mod; 389 unsigned long uval; 390 long val; 391 #endif 392 #ifdef notyet 393 char *filename; 394 int linenum; 395 #endif 396 397 #ifndef _KERNEL 398 if (!use_ksyms) { 399 db_expr_t d; 400 char *filename; 401 db_expr_t value; 402 int linenum; 403 db_sym_t cursym; 404 405 if ((unsigned long) off <= db_lastsym) { 406 cursym = db_search_symbol(off, strategy, &d); 407 db_symbol_values(cursym, &name, &value); 408 if (name != NULL && 409 ((unsigned int) d < db_maxoff) && 410 value != 0) { 411 (*pr)("%s", name); 412 if (d) { 413 char tbuf[24]; 414 415 db_format_radix(tbuf, 24, d, true); 416 (*pr)("+%s", tbuf); 417 } 418 if (strategy == DB_STGY_PROC) { 419 if ((*db_symformat->sym_line_at_pc) 420 (NULL, cursym, &filename, 421 &linenum, off)) 422 (*pr)(" [%s:%d]", 423 filename, linenum); 424 } 425 return; 426 } 427 } 428 (*pr)(db_num_to_str(off)); 429 return; 430 } 431 #endif 432 #ifdef _KERNEL 433 if (ksyms_getname(&mod, &name, (vaddr_t)off, 434 strategy|KSYMS_CLOSEST) == 0) { 435 (void)ksyms_getval_unlocked(mod, name, &uval, KSYMS_ANY); 436 val = (long) uval; 437 if (((off - val) < db_maxoff) && val) { 438 (*pr)("%s:%s", mod, name); 439 if (off - val) { 440 char tbuf[24]; 441 442 db_format_radix(tbuf, 24, off - val, true); 443 (*pr)("+%s", tbuf); 444 } 445 #ifdef notyet 446 if (strategy & KSYMS_PROC) { 447 if (ksyms_fmaddr(off, &filename, &linenum) == 0) 448 (*pr)(" [%s:%d]", filename, linenum); 449 } 450 #endif 451 return; 452 } 453 } 454 #endif 455 (*pr)(db_num_to_str(off)); 456 return; 457 } 458 459 /* 460 * Splits a string in the form "mod:sym" to two strings. 461 */ 462 static void 463 db_symsplit(char *str, char **mod, char **sym) 464 { 465 char *cp; 466 467 if ((cp = strchr(str, ':')) != NULL) { 468 *cp++ = '\0'; 469 *mod = str; 470 *sym = cp; 471 } else { 472 *mod = NULL; 473 *sym = str; 474 } 475 } 476 477 bool 478 db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep) 479 { 480 #ifndef _KERNEL 481 if (!use_ksyms) 482 return ((*db_symformat->sym_numargs)(NULL, cursym, nargp, 483 argnamep)); 484 #endif 485 return (false); 486 } 487 488