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