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