Home | History | Annotate | Line # | Download | only in ddb
db_sym.c revision 1.43
      1 /*	$NetBSD: db_sym.c,v 1.43 2003/10/05 11:17:47 scw 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/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: db_sym.c,v 1.43 2003/10/05 11:17:47 scw 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 	}
    235 	*offp = diff;
    236 	return ret;
    237 }
    238 
    239 /*
    240  * Return name and value of a symbol
    241  */
    242 void
    243 db_symbol_values(db_sym_t sym, char **namep, db_expr_t *valuep)
    244 {
    245 	const char *mod;
    246 
    247 	if (sym == DB_SYM_NULL) {
    248 		*namep = 0;
    249 		return;
    250 	}
    251 
    252 #ifdef DB_AOUT_SYMBOLS
    253 	if (using_aout_symtab) {
    254 		db_expr_t value;
    255 		(*db_symformat->sym_value)(NULL, sym, namep, &value);
    256 		if (valuep)
    257 			*valuep = value;
    258 		return;
    259 	}
    260 #endif
    261 
    262 	if (ksyms_getname(&mod, namep, (vaddr_t)sym,
    263 	    KSYMS_ANY|KSYMS_EXACT) == 0) {
    264 		if (valuep)
    265 			*valuep = sym;
    266 	} else
    267 		*namep = NULL;
    268 }
    269 
    270 
    271 /*
    272  * Print a the closest symbol to value
    273  *
    274  * After matching the symbol according to the given strategy
    275  * we print it in the name+offset format, provided the symbol's
    276  * value is close enough (eg smaller than db_maxoff).
    277  * We also attempt to print [filename:linenum] when applicable
    278  * (eg for procedure names).
    279  *
    280  * If we could not find a reasonable name+offset representation,
    281  * then we just print the value in hex.  Small values might get
    282  * bogus symbol associations, e.g. 3 might get some absolute
    283  * value like _INCLUDE_VERSION or something, therefore we do
    284  * not accept symbols whose value is zero (and use plain hex).
    285  * Also, avoid printing as "end+0x????" which is useless.
    286  * The variable db_lastsym is used instead of "end" in case we
    287  * add support for symbols in loadable driver modules.
    288  */
    289 extern char end[];
    290 unsigned long	db_lastsym = (unsigned long)end;
    291 unsigned int	db_maxoff = 0x10000000;
    292 
    293 void
    294 db_symstr(char *buf, size_t buflen, db_expr_t off, db_strategy_t strategy)
    295 {
    296 	char  *name;
    297 	const char *mod;
    298 	unsigned long val;
    299 
    300 #ifdef DB_AOUT_SYMBOLS
    301 	if (using_aout_symtab) {
    302 		db_expr_t	d;
    303 		char 		*filename;
    304 		char		*name;
    305 		db_expr_t	value;
    306 		int 		linenum;
    307 		db_sym_t	cursym;
    308 
    309 		if ((unsigned long) off <= db_lastsym) {
    310 			cursym = db_search_symbol(off, strategy, &d);
    311 			db_symbol_values(cursym, &name, &value);
    312 			if (name != NULL &&
    313 			    ((unsigned int) d < db_maxoff) &&
    314 			    value != 0) {
    315 				strlcpy(buf, name, buflen);
    316 				if (d) {
    317 					strlcat(buf, "+", buflen);
    318 					db_format_radix(buf+strlen(buf),
    319 					    24, d, TRUE);
    320 				}
    321 				if (strategy == DB_STGY_PROC) {
    322 					if ((*db_symformat->sym_line_at_pc)
    323 					    (NULL, cursym, &filename,
    324 					    &linenum, off))
    325 						sprintf(buf+strlen(buf),
    326 						    " [%s:%d]",
    327 						    filename, linenum);
    328 				}
    329 				return;
    330 			}
    331 		}
    332 		strlcpy(buf, db_num_to_str(off), buflen);
    333 		return;
    334 	}
    335 #endif
    336 	if (ksyms_getname(&mod, &name, (vaddr_t)off,
    337 	    strategy|KSYMS_CLOSEST) == 0) {
    338 		(void)ksyms_getval(mod, name, &val, KSYMS_ANY);
    339 		if (((off - val) < db_maxoff) && val) {
    340 			sprintf(buf, "%s:%s", mod, name);
    341 			if (off - val) {
    342 				strlcat(buf, "+", buflen);
    343 				db_format_radix(buf+strlen(buf),
    344 				    24, off - val, TRUE);
    345 			}
    346 #ifdef notyet
    347 			if (strategy & KSYMS_PROC) {
    348 				if (ksyms_fmaddr(off, &filename, &linenum) == 0)					sprintf(buf+strlen(buf),
    349 					    " [%s:%d]", filename, linenum);
    350 			}
    351 #endif
    352 			return;
    353 		}
    354 	}
    355 	strlcpy(buf, db_num_to_str(off), buflen);
    356 }
    357 
    358 void
    359 db_printsym(db_expr_t off, db_strategy_t strategy,
    360     void (*pr)(const char *, ...))
    361 {
    362 	char  *name;
    363 	const char *mod;
    364 	unsigned long uval;
    365 	long val;
    366 #ifdef notyet
    367 	char *filename;
    368 	int  linenum;
    369 #endif
    370 
    371 #ifdef DB_AOUT_SYMBOLS
    372 	if (using_aout_symtab) {
    373 		db_expr_t	d;
    374 		char 		*filename;
    375 		char		*name;
    376 		db_expr_t	value;
    377 		int 		linenum;
    378 		db_sym_t	cursym;
    379 		if ((unsigned long) off <= db_lastsym) {
    380 			cursym = db_search_symbol(off, strategy, &d);
    381 			db_symbol_values(cursym, &name, &value);
    382 			if (name != NULL &&
    383 			    ((unsigned int) d < db_maxoff) &&
    384 			    value != 0) {
    385 				(*pr)("%s", name);
    386 				if (d) {
    387 					char tbuf[24];
    388 
    389 					db_format_radix(tbuf, 24, d, TRUE);
    390 					(*pr)("+%s", tbuf);
    391 				}
    392 				if (strategy == DB_STGY_PROC) {
    393 					if ((*db_symformat->sym_line_at_pc)
    394 					    (NULL, cursym, &filename,
    395 					    &linenum, off))
    396 						(*pr)(" [%s:%d]",
    397 						    filename, linenum);
    398 				}
    399 				return;
    400 			}
    401 		}
    402 		(*pr)(db_num_to_str(off));
    403 		return;
    404 	}
    405 #endif
    406 	if (ksyms_getname(&mod, &name, (vaddr_t)off,
    407 	    strategy|KSYMS_CLOSEST) == 0) {
    408 		(void)ksyms_getval(mod, name, &uval, KSYMS_ANY);
    409 		val = (long) uval;
    410 		if (((off - val) < db_maxoff) && val) {
    411 			(*pr)("%s:%s", mod, name);
    412 			if (off - val) {
    413 				char tbuf[24];
    414 
    415 				db_format_radix(tbuf, 24, off - val, TRUE);
    416 				(*pr)("+%s", tbuf);
    417 			}
    418 #ifdef notyet
    419 			if (strategy & KSYMS_PROC) {
    420 				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
    421 					(*pr)(" [%s:%d]", filename, linenum);
    422 			}
    423 #endif
    424 			return;
    425 		}
    426 	}
    427 	(*pr)(db_num_to_str(off));
    428 	return;
    429 }
    430 
    431 /*
    432  * Splits a string in the form "mod:sym" to two strings.
    433  */
    434 static void
    435 db_symsplit(char *str, char **mod, char **sym)
    436 {
    437 	char *cp;
    438 
    439 	if ((cp = strchr(str, ':')) != NULL) {
    440 		*cp++ = '\0';
    441 		*mod = str;
    442 		*sym = cp;
    443 	} else {
    444 		*mod = NULL;
    445 		*sym = str;
    446 	}
    447 }
    448 
    449 boolean_t
    450 db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep)
    451 {
    452 #ifdef DB_AOUT_SYMBOLS
    453 	if (using_aout_symtab)
    454 		return ((*db_symformat->sym_numargs)(NULL, cursym, nargp,
    455 		    argnamep));
    456 #endif
    457 	return (FALSE);
    458 }
    459 
    460