Home | History | Annotate | Line # | Download | only in ddb
db_sym.c revision 1.19
      1 /*	$NetBSD: db_sym.c,v 1.19 2000/03/30 11:31:27 augustss 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 (at) 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/param.h>
     30 #include <sys/proc.h>
     31 #include <sys/systm.h>
     32 
     33 #include <machine/db_machdep.h>
     34 
     35 #include <ddb/db_sym.h>
     36 #include <ddb/db_output.h>
     37 #include <ddb/db_extern.h>
     38 #include <ddb/db_command.h>
     39 
     40 /*
     41  * Multiple symbol tables
     42  */
     43 #ifndef MAXLKMS
     44 #define MAXLKMS 20
     45 #endif
     46 
     47 #ifndef MAXNOSYMTABS
     48 #define	MAXNOSYMTABS	MAXLKMS+1	/* Room for kernel + LKM's */
     49 #endif
     50 
     51 db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
     52 
     53 db_symtab_t	*db_last_symtab;
     54 
     55 static char *db_qualify __P((db_sym_t, const char *));
     56 
     57 /*
     58  * Put the most picky symbol table formats at the top!
     59  */
     60 const db_symformat_t *db_symformats[] = {
     61 #ifdef DB_ELF_SYMBOLS
     62 	&db_symformat_elf,
     63 #endif
     64 #ifdef DB_AOUT_SYMBOLS
     65 	&db_symformat_aout,
     66 #endif
     67 	NULL,
     68 };
     69 
     70 const db_symformat_t *db_symformat;
     71 
     72 boolean_t	X_db_sym_init __P((int, void *, void *, const char *));
     73 db_sym_t	X_db_lookup __P((db_symtab_t *, char *));
     74 db_sym_t	X_db_search_symbol __P((db_symtab_t *, db_addr_t,
     75 		    db_strategy_t, db_expr_t *));
     76 void		X_db_symbol_values __P((db_symtab_t *, db_sym_t, char **,
     77 		    db_expr_t *));
     78 boolean_t	X_db_line_at_pc __P((db_symtab_t *, db_sym_t, char **,
     79 		    int *, db_expr_t));
     80 int		X_db_sym_numargs __P((db_symtab_t *, db_sym_t, int *,
     81 		    char **));
     82 
     83 /*
     84  * Initialize the kernel debugger by initializing the master symbol
     85  * table.  Note that if initializing the master symbol table fails,
     86  * no other symbol tables can be loaded.
     87  */
     88 void
     89 ddb_init(symsize, vss, vse)
     90 	int symsize;
     91 	void *vss, *vse;
     92 {
     93 	const db_symformat_t **symf;
     94 	const char *name = "netbsd";
     95 
     96 	if (symsize <= 0) {
     97 		printf(" [ no symbols available ]\n");
     98 		return;
     99 	}
    100 
    101 	/*
    102 	 * Do this check now for the master symbol table to avoid printing
    103 	 * the message N times.
    104 	 */
    105 	if (ALIGNED_POINTER(vss, long) == 0) {
    106 		printf("[ %s symbol table has bad start address %p ]\n",
    107 		    name, vss);
    108 		return;
    109 	}
    110 
    111 	for (symf = db_symformats; *symf != NULL; symf++) {
    112 		db_symformat = *symf;
    113 		if (X_db_sym_init(symsize, vss, vse, name) == TRUE)
    114 			return;
    115 	}
    116 
    117 	db_symformat = NULL;
    118 	printf("[ no symbol table formats found ]\n");
    119 }
    120 
    121 /*
    122  * Add symbol table, with given name, to list of symbol tables.
    123  */
    124 int
    125 db_add_symbol_table(start, end, name, ref)
    126 	char *start;
    127 	char *end;
    128 	const char *name;
    129 	char *ref;
    130 {
    131 	int slot;
    132 
    133 	for (slot = 0; slot < MAXNOSYMTABS; slot++) {
    134 		if (db_symtabs[slot].name == NULL)
    135 			break;
    136 	}
    137 	if (slot >= MAXNOSYMTABS) {
    138 		db_printf("No slots left for %s symbol table", name);
    139 		return(-1);
    140 	}
    141 
    142 	db_symtabs[slot].start = start;
    143 	db_symtabs[slot].end = end;
    144 	db_symtabs[slot].name = name;
    145 	db_symtabs[slot].private = ref;
    146 
    147 	return(slot);
    148 }
    149 
    150 /*
    151  * Delete a symbol table. Caller is responsible for freeing storage.
    152  */
    153 void
    154 db_del_symbol_table(name)
    155 	char *name;
    156 {
    157 	int slot;
    158 
    159 	for (slot = 0; slot < MAXNOSYMTABS; slot++) {
    160 		if (db_symtabs[slot].name &&
    161 		    ! strcmp(db_symtabs[slot].name, name))
    162 			break;
    163 	}
    164 	if (slot >= MAXNOSYMTABS) {
    165 		db_printf("Unable to find symbol table slot for %s.", name);
    166 		return;
    167 	}
    168 
    169 	db_symtabs[slot].start = 0;
    170 	db_symtabs[slot].end = 0;
    171 	db_symtabs[slot].name = 0;
    172 	db_symtabs[slot].private = 0;
    173 }
    174 
    175 /*
    176  *  db_qualify("vm_map", "netbsd") returns "netbsd:vm_map".
    177  *
    178  *  Note: return value points to static data whose content is
    179  *  overwritten by each call... but in practice this seems okay.
    180  */
    181 static char *
    182 db_qualify(sym, symtabname)
    183 	db_sym_t	sym;
    184 	const char	*symtabname;
    185 {
    186 	char		*symname;
    187 	static char     tmp[256];
    188 	char	*s;
    189 
    190 	db_symbol_values(sym, &symname, 0);
    191 	s = tmp;
    192 	while ((*s++ = *symtabname++) != '\0')
    193 		;
    194 	s[-1] = ':';
    195 	while ((*s++ = *symname++) != '\0')
    196 		;
    197 	return tmp;
    198 }
    199 
    200 
    201 boolean_t
    202 db_eqname(src, dst, c)
    203 	char *src;
    204 	char *dst;
    205 	int c;
    206 {
    207 	if (!strcmp(src, dst))
    208 	    return (TRUE);
    209 	if (src[0] == c)
    210 	    return (!strcmp(src+1,dst));
    211 	return (FALSE);
    212 }
    213 
    214 boolean_t
    215 db_value_of_name(name, valuep)
    216 	char		*name;
    217 	db_expr_t	*valuep;
    218 {
    219 	db_sym_t	sym;
    220 
    221 	sym = db_lookup(name);
    222 	if (sym == DB_SYM_NULL)
    223 	    return (FALSE);
    224 	db_symbol_values(sym, &name, valuep);
    225 	return (TRUE);
    226 }
    227 
    228 
    229 /*
    230  * Lookup a symbol.
    231  * If the symbol has a qualifier (e.g., ux:vm_map),
    232  * then only the specified symbol table will be searched;
    233  * otherwise, all symbol tables will be searched.
    234  */
    235 db_sym_t
    236 db_lookup(symstr)
    237 	char *symstr;
    238 {
    239 	db_sym_t sp;
    240 	int i;
    241 	int symtab_start = 0;
    242 	int symtab_end = MAXNOSYMTABS;
    243 	char *cp;
    244 
    245 	/*
    246 	 * Look for, remove, and remember any symbol table specifier.
    247 	 */
    248 	for (cp = symstr; *cp; cp++) {
    249 		if (*cp == ':') {
    250 			*cp = '\0';
    251 			for (i = 0; i < MAXNOSYMTABS; i++) {
    252 				if (db_symtabs[i].name &&
    253 				    ! strcmp(symstr, db_symtabs[i].name)) {
    254 					symtab_start = i;
    255 					symtab_end = i + 1;
    256 					break;
    257 				}
    258 			}
    259 			*cp = ':';
    260 			if (i == MAXNOSYMTABS) {
    261 				db_error("invalid symbol table name");
    262 				/*NOTREACHED*/
    263 			}
    264 			symstr = cp+1;
    265 		}
    266 	}
    267 
    268 	/*
    269 	 * Look in the specified set of symbol tables.
    270 	 * Return on first match.
    271 	 */
    272 	for (i = symtab_start; i < symtab_end; i++) {
    273 		if (db_symtabs[i].name &&
    274 		    (sp = X_db_lookup(&db_symtabs[i], symstr))) {
    275 			db_last_symtab = &db_symtabs[i];
    276 			return sp;
    277 		}
    278 	}
    279 	return 0;
    280 }
    281 
    282 /*
    283  * Does this symbol name appear in more than one symbol table?
    284  * Used by db_symbol_values to decide whether to qualify a symbol.
    285  */
    286 boolean_t db_qualify_ambiguous_names = FALSE;
    287 
    288 boolean_t
    289 db_symbol_is_ambiguous(sym)
    290 	db_sym_t	sym;
    291 {
    292 	char		*sym_name;
    293 	int	i;
    294 	boolean_t	found_once = FALSE;
    295 
    296 	if (!db_qualify_ambiguous_names)
    297 		return FALSE;
    298 
    299 	db_symbol_values(sym, &sym_name, 0);
    300 	for (i = 0; i < MAXNOSYMTABS; i++) {
    301 		if (db_symtabs[i].name &&
    302 		    X_db_lookup(&db_symtabs[i], sym_name)) {
    303 			if (found_once)
    304 				return TRUE;
    305 			found_once = TRUE;
    306 		}
    307 	}
    308 	return FALSE;
    309 }
    310 
    311 /*
    312  * Find the closest symbol to val, and return its name
    313  * and the difference between val and the symbol found.
    314  */
    315 db_sym_t
    316 db_search_symbol( val, strategy, offp)
    317 	db_addr_t		val;
    318 	db_strategy_t		strategy;
    319 	db_expr_t		*offp;
    320 {
    321 	unsigned int	diff;
    322 	db_expr_t	newdiff;
    323 	int		i;
    324 	db_sym_t	ret = DB_SYM_NULL, sym;
    325 
    326 	newdiff = diff = ~0;
    327 	db_last_symtab = 0;
    328 	for (i = 0; i < MAXNOSYMTABS; i++) {
    329 	    if (!db_symtabs[i].name)
    330 	        continue;
    331 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
    332 	    if (newdiff < diff) {
    333 		db_last_symtab = &db_symtabs[i];
    334 		diff = newdiff;
    335 		ret = sym;
    336 	    }
    337 	}
    338 	*offp = diff;
    339 	return ret;
    340 }
    341 
    342 /*
    343  * Return name and value of a symbol
    344  */
    345 void
    346 db_symbol_values(sym, namep, valuep)
    347 	db_sym_t	sym;
    348 	char		**namep;
    349 	db_expr_t	*valuep;
    350 {
    351 	db_expr_t	value;
    352 
    353 	if (sym == DB_SYM_NULL) {
    354 		*namep = 0;
    355 		return;
    356 	}
    357 
    358 	X_db_symbol_values(db_last_symtab, sym, namep, &value);
    359 
    360 	if (db_symbol_is_ambiguous(sym))
    361 		*namep = db_qualify(sym, db_last_symtab->name);
    362 	if (valuep)
    363 		*valuep = value;
    364 }
    365 
    366 
    367 /*
    368  * Print a the closest symbol to value
    369  *
    370  * After matching the symbol according to the given strategy
    371  * we print it in the name+offset format, provided the symbol's
    372  * value is close enough (eg smaller than db_maxoff).
    373  * We also attempt to print [filename:linenum] when applicable
    374  * (eg for procedure names).
    375  *
    376  * If we could not find a reasonable name+offset representation,
    377  * then we just print the value in hex.  Small values might get
    378  * bogus symbol associations, e.g. 3 might get some absolute
    379  * value like _INCLUDE_VERSION or something, therefore we do
    380  * not accept symbols whose value is zero (and use plain hex).
    381  * Also, avoid printing as "end+0x????" which is useless.
    382  * The variable db_lastsym is used instead of "end" in case we
    383  * add support for symbols in loadable driver modules.
    384  */
    385 extern char end[];
    386 unsigned long	db_lastsym = (unsigned long)end;
    387 unsigned int	db_maxoff = 0x10000000;
    388 
    389 
    390 void
    391 db_printsym(off, strategy)
    392 	db_expr_t	off;
    393 	db_strategy_t	strategy;
    394 {
    395 	db_expr_t	d;
    396 	char 		*filename;
    397 	char		*name;
    398 	db_expr_t	value;
    399 	int 		linenum;
    400 	db_sym_t	cursym;
    401 
    402 	if (off <= db_lastsym) {
    403 		cursym = db_search_symbol(off, strategy, &d);
    404 		db_symbol_values(cursym, &name, &value);
    405 		if (name && (d < db_maxoff) && value) {
    406 			db_printf("%s", name);
    407 			if (d)
    408 				db_printf("+%#lr", d);
    409 			if (strategy == DB_STGY_PROC) {
    410 				if (db_line_at_pc(cursym, &filename, &linenum, off))
    411 					db_printf(" [%s:%d]", filename, linenum);
    412 			}
    413 			return;
    414 		}
    415 	}
    416 	db_printf("%#ln", off);
    417 	return;
    418 }
    419 
    420 
    421 boolean_t
    422 db_line_at_pc( sym, filename, linenum, pc)
    423 	db_sym_t	sym;
    424 	char		**filename;
    425 	int		*linenum;
    426 	db_expr_t	pc;
    427 {
    428 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
    429 }
    430 
    431 int
    432 db_sym_numargs(sym, nargp, argnames)
    433 	db_sym_t	sym;
    434 	int		*nargp;
    435 	char		**argnames;
    436 {
    437 	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
    438 }
    439 
    440 boolean_t
    441 X_db_sym_init(symsize, vss, vse, name)
    442 	int symsize;
    443 	void *vss, *vse;
    444 	const char *name;
    445 {
    446 
    447 	if (db_symformat != NULL)
    448 		return ((*db_symformat->sym_init)(symsize, vss, vse, name));
    449 	return (FALSE);
    450 }
    451 
    452 db_sym_t
    453 X_db_lookup(stab, symstr)
    454 	db_symtab_t *stab;
    455 	char *symstr;
    456 {
    457 
    458 	if (db_symformat != NULL)
    459 		return ((*db_symformat->sym_lookup)(stab, symstr));
    460 	return ((db_sym_t)0);
    461 }
    462 
    463 db_sym_t
    464 X_db_search_symbol(stab, off, strategy, diffp)
    465 	db_symtab_t *stab;
    466 	db_addr_t off;
    467 	db_strategy_t strategy;
    468 	db_expr_t *diffp;
    469 {
    470 
    471 	if (db_symformat != NULL)
    472 		return ((*db_symformat->sym_search)(stab, off, strategy,
    473 		    diffp));
    474 	return ((db_sym_t)0);
    475 }
    476 
    477 void
    478 X_db_symbol_values(stab, sym, namep, valuep)
    479 	db_symtab_t *stab;
    480 	db_sym_t sym;
    481 	char **namep;
    482 	db_expr_t *valuep;
    483 {
    484 
    485 	if (db_symformat != NULL)
    486 		(*db_symformat->sym_value)(stab, sym, namep, valuep);
    487 }
    488 
    489 boolean_t
    490 X_db_line_at_pc(stab, cursym, filename, linenum, off)
    491 	db_symtab_t *stab;
    492 	db_sym_t cursym;
    493 	char **filename;
    494 	int *linenum;
    495 	db_expr_t off;
    496 {
    497 
    498 	if (db_symformat != NULL)
    499 		return ((*db_symformat->sym_line_at_pc)(stab, cursym,
    500 		    filename, linenum, off));
    501 	return (FALSE);
    502 }
    503 
    504 boolean_t
    505 X_db_sym_numargs(stab, cursym, nargp, argnamep)
    506 	db_symtab_t *stab;
    507 	db_sym_t cursym;
    508 	int *nargp;
    509 	char **argnamep;
    510 {
    511 
    512 	if (db_symformat != NULL)
    513 		return ((*db_symformat->sym_numargs)(stab, cursym, nargp,
    514 		    argnamep));
    515 	return (FALSE);
    516 }
    517