Home | History | Annotate | Line # | Download | only in ddb
db_sym.c revision 1.40
      1  1.40    itojun /*	$NetBSD: db_sym.c,v 1.40 2003/05/16 15:02:08 itojun Exp $	*/
      2   1.7       cgd 
      3  1.31    simonb /*
      4   1.1       cgd  * Mach Operating System
      5   1.1       cgd  * Copyright (c) 1991,1990 Carnegie Mellon University
      6   1.1       cgd  * All Rights Reserved.
      7  1.31    simonb  *
      8   1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9   1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10   1.1       cgd  * notice and this permission notice appear in all copies of the
     11   1.1       cgd  * software, derivative works or modified versions, and any portions
     12   1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13  1.31    simonb  *
     14  1.17        pk  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15   1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16   1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.31    simonb  *
     18   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19  1.31    simonb  *
     20   1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1       cgd  *  School of Computer Science
     22   1.1       cgd  *  Carnegie Mellon University
     23   1.1       cgd  *  Pittsburgh PA 15213-3890
     24  1.31    simonb  *
     25   1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26   1.1       cgd  * rights to redistribute these changes.
     27   1.1       cgd  */
     28  1.28     lukem 
     29  1.28     lukem #include <sys/cdefs.h>
     30  1.40    itojun __KERNEL_RCSID(0, "$NetBSD: db_sym.c,v 1.40 2003/05/16 15:02:08 itojun Exp $");
     31   1.5   mycroft 
     32  1.32     itohy #include "opt_ddbparam.h"
     33  1.26    simonb 
     34   1.6   mycroft #include <sys/param.h>
     35   1.6   mycroft #include <sys/proc.h>
     36  1.15   thorpej #include <sys/systm.h>
     37  1.35     ragge #include <sys/ksyms.h>
     38   1.6   mycroft 
     39   1.1       cgd #include <machine/db_machdep.h>
     40   1.6   mycroft 
     41  1.20     jhawk #include <ddb/db_lex.h>
     42   1.1       cgd #include <ddb/db_sym.h>
     43  1.12  christos #include <ddb/db_output.h>
     44  1.12  christos #include <ddb/db_extern.h>
     45  1.12  christos #include <ddb/db_command.h>
     46   1.1       cgd 
     47  1.35     ragge static void		db_symsplit(char *, char **, char **);
     48  1.31    simonb 
     49  1.15   thorpej 
     50  1.15   thorpej #ifdef DB_AOUT_SYMBOLS
     51  1.35     ragge #define	TBLNAME	"netbsd"
     52  1.15   thorpej 
     53  1.35     ragge static int using_aout_symtab;
     54  1.15   thorpej const db_symformat_t *db_symformat;
     55  1.35     ragge static db_forall_func_t db_sift;
     56  1.35     ragge extern db_symformat_t db_symformat_aout;
     57  1.35     ragge #endif
     58  1.15   thorpej 
     59  1.15   thorpej 
     60  1.15   thorpej /*
     61  1.15   thorpej  * Initialize the kernel debugger by initializing the master symbol
     62  1.15   thorpej  * table.  Note that if initializing the master symbol table fails,
     63  1.15   thorpej  * no other symbol tables can be loaded.
     64  1.15   thorpej  */
     65  1.15   thorpej void
     66  1.31    simonb ddb_init(int symsize, void *vss, void *vse)
     67  1.15   thorpej {
     68  1.35     ragge #ifdef DB_AOUT_SYMBOLS
     69  1.35     ragge 	db_symformat = &db_symformat_aout;
     70  1.35     ragge 	if ((*db_symformat->sym_init)(symsize, vss, vse, TBLNAME) == TRUE) {
     71  1.35     ragge 		using_aout_symtab = TRUE;
     72  1.15   thorpej 		return;
     73  1.15   thorpej 	}
     74  1.35     ragge #endif
     75  1.37     ragge 	ksyms_init(symsize, vss, vse);	/* Will complain if necessary */
     76   1.1       cgd }
     77   1.1       cgd 
     78   1.1       cgd boolean_t
     79  1.31    simonb db_eqname(char *src, char *dst, int c)
     80   1.1       cgd {
     81  1.31    simonb 
     82   1.1       cgd 	if (!strcmp(src, dst))
     83  1.31    simonb 		return (TRUE);
     84   1.1       cgd 	if (src[0] == c)
     85  1.31    simonb 		return (!strcmp(src+1,dst));
     86   1.1       cgd 	return (FALSE);
     87   1.1       cgd }
     88   1.1       cgd 
     89   1.1       cgd boolean_t
     90  1.31    simonb db_value_of_name(char *name, db_expr_t *valuep)
     91   1.1       cgd {
     92  1.35     ragge 	char *mod, *sym;
     93   1.1       cgd 
     94  1.35     ragge #ifdef DB_AOUT_SYMBOLS
     95  1.35     ragge 	db_sym_t	ssym;
     96   1.1       cgd 
     97  1.35     ragge 	if (using_aout_symtab) {
     98  1.35     ragge 		/*
     99  1.35     ragge 		 * Cannot load symtabs in a.out kernels, so the ':'
    100  1.35     ragge 		 * style of selecting modules is irrelevant.
    101  1.35     ragge 		 */
    102  1.35     ragge 		ssym = (*db_symformat->sym_lookup)(NULL, name);
    103  1.35     ragge 		if (ssym == DB_SYM_NULL)
    104  1.35     ragge 			return (FALSE);
    105  1.35     ragge 		db_symbol_values(ssym, &name, valuep);
    106  1.35     ragge 		return (TRUE);
    107   1.1       cgd 	}
    108  1.35     ragge #endif
    109  1.35     ragge 	db_symsplit(name, &mod, &sym);
    110  1.35     ragge 	if (ksyms_getval(mod, sym, valuep, KSYMS_EXTERN) == 0)
    111  1.35     ragge 		return TRUE;
    112  1.35     ragge 	if (ksyms_getval(mod, sym, valuep, KSYMS_ANY) == 0)
    113  1.35     ragge 		return TRUE;
    114  1.35     ragge 	return FALSE;
    115   1.1       cgd }
    116   1.1       cgd 
    117  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    118  1.20     jhawk /* Private structure for passing args to db_sift() from db_sifting(). */
    119  1.20     jhawk struct db_sift_args {
    120  1.20     jhawk 	char	*symstr;
    121  1.20     jhawk 	int	mode;
    122  1.20     jhawk };
    123  1.20     jhawk 
    124  1.20     jhawk /*
    125  1.20     jhawk  * Does the work of db_sifting(), called once for each
    126  1.35     ragge  * symbol via db_forall(), prints out symbols matching
    127  1.20     jhawk  * criteria.
    128  1.20     jhawk  */
    129  1.20     jhawk static void
    130  1.31    simonb db_sift(db_symtab_t *stab, db_sym_t sym, char *name, char *suffix, int prefix,
    131  1.31    simonb     void *arg)
    132  1.20     jhawk {
    133  1.20     jhawk 	char c, sc;
    134  1.20     jhawk 	char *find, *p;
    135  1.20     jhawk 	size_t len;
    136  1.20     jhawk 	struct db_sift_args *dsa;
    137  1.20     jhawk 
    138  1.20     jhawk 	dsa = (struct db_sift_args*)arg;
    139  1.20     jhawk 
    140  1.20     jhawk 	find = dsa->symstr;	/* String we're looking for. */
    141  1.20     jhawk 	p = name;		/* String we're searching within. */
    142  1.31    simonb 
    143  1.20     jhawk 	/* Matching algorithm cribbed from strstr(), which is not
    144  1.20     jhawk 	   in the kernel. */
    145  1.20     jhawk 	if ((c = *find++) != 0) {
    146  1.20     jhawk 		len = strlen(find);
    147  1.20     jhawk 		do {
    148  1.20     jhawk 			do {
    149  1.20     jhawk 				if ((sc = *p++) == 0)
    150  1.20     jhawk 					return;
    151  1.20     jhawk 			} while (sc != c);
    152  1.20     jhawk 		} while (strncmp(p, find, len) != 0);
    153  1.20     jhawk 	}
    154  1.20     jhawk 	if (dsa->mode=='F')	/* ala ls -F */
    155  1.20     jhawk 		db_printf("%s%s ", name, suffix);
    156  1.20     jhawk 	else
    157  1.20     jhawk 		db_printf("%s ", name);
    158  1.20     jhawk }
    159  1.35     ragge #endif
    160  1.20     jhawk 
    161  1.20     jhawk /*
    162  1.20     jhawk  * "Sift" for a partial symbol.
    163  1.20     jhawk  * Named for the Sun OpenPROM command ("sifting").
    164  1.20     jhawk  * If the symbol has a qualifier (e.g., ux:vm_map),
    165  1.20     jhawk  * then only the specified symbol table will be searched;
    166  1.20     jhawk  * otherwise, all symbol tables will be searched..
    167  1.20     jhawk  *
    168  1.20     jhawk  * "mode" is how-to-display, set from modifiers.
    169  1.20     jhawk  */
    170  1.20     jhawk void
    171  1.31    simonb db_sifting(char *symstr, int mode)
    172  1.20     jhawk {
    173  1.35     ragge 	char *mod, *sym;
    174  1.35     ragge 
    175  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    176  1.20     jhawk 	struct db_sift_args dsa;
    177  1.20     jhawk 
    178  1.35     ragge 	if (using_aout_symtab) {
    179  1.35     ragge 		dsa.symstr = symstr;
    180  1.35     ragge 		dsa.mode = mode;
    181  1.35     ragge 		(*db_symformat->sym_forall)(NULL, db_sift, &dsa);
    182  1.35     ragge 		db_printf("\n");
    183  1.35     ragge 		return;
    184  1.20     jhawk 	}
    185  1.35     ragge #endif
    186  1.20     jhawk 
    187  1.35     ragge 	db_symsplit(symstr, &mod, &sym);
    188  1.35     ragge 	if (ksyms_sift(mod, sym, mode) == ENODEV)
    189  1.35     ragge 		db_error("invalid symbol table name");
    190   1.1       cgd }
    191   1.1       cgd 
    192   1.1       cgd /*
    193   1.1       cgd  * Find the closest symbol to val, and return its name
    194   1.1       cgd  * and the difference between val and the symbol found.
    195   1.1       cgd  */
    196   1.1       cgd db_sym_t
    197  1.31    simonb db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
    198   1.1       cgd {
    199  1.35     ragge 	unsigned int diff;
    200  1.35     ragge 	db_sym_t ret = DB_SYM_NULL;
    201  1.35     ragge 	db_addr_t naddr;
    202  1.39  jdolecek 	const char *mod;
    203  1.39  jdolecek 	char *sym;
    204  1.35     ragge 
    205  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    206  1.35     ragge 	db_expr_t newdiff;
    207  1.35     ragge 	db_sym_t ssym;
    208  1.35     ragge 
    209  1.35     ragge 	if (using_aout_symtab) {
    210  1.35     ragge 		newdiff = diff = ~0;
    211  1.35     ragge 		ssym = (*db_symformat->sym_search)
    212  1.35     ragge 		    (NULL, val, strategy, &newdiff);
    213  1.33   thorpej 		if ((unsigned int) newdiff < diff) {
    214  1.31    simonb 			diff = newdiff;
    215  1.35     ragge 			ret = ssym;
    216  1.31    simonb 		}
    217  1.35     ragge 		*offp = diff;
    218  1.35     ragge 		return ret;
    219  1.35     ragge 	}
    220  1.35     ragge #endif
    221  1.35     ragge 
    222  1.35     ragge 	if (ksyms_getname(&mod, &sym, val, strategy) == 0) {
    223  1.35     ragge 		(void)ksyms_getval(mod, sym, &naddr, KSYMS_ANY);
    224  1.35     ragge 		diff = val - naddr;
    225  1.35     ragge 		ret = naddr;
    226   1.1       cgd 	}
    227   1.1       cgd 	*offp = diff;
    228   1.1       cgd 	return ret;
    229   1.1       cgd }
    230   1.1       cgd 
    231   1.1       cgd /*
    232   1.1       cgd  * Return name and value of a symbol
    233   1.1       cgd  */
    234   1.1       cgd void
    235  1.31    simonb db_symbol_values(db_sym_t sym, char **namep, db_expr_t *valuep)
    236   1.1       cgd {
    237  1.39  jdolecek 	const char *mod;
    238   1.1       cgd 
    239   1.1       cgd 	if (sym == DB_SYM_NULL) {
    240   1.1       cgd 		*namep = 0;
    241   1.1       cgd 		return;
    242   1.1       cgd 	}
    243   1.1       cgd 
    244  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    245  1.35     ragge 	if (using_aout_symtab) {
    246  1.35     ragge 		db_expr_t value;
    247  1.35     ragge 		(*db_symformat->sym_value)(NULL, sym, namep, &value);
    248  1.35     ragge 		if (valuep)
    249  1.35     ragge 			*valuep = value;
    250  1.35     ragge 		return;
    251  1.35     ragge 	}
    252  1.35     ragge #endif
    253   1.1       cgd 
    254  1.35     ragge 	if (ksyms_getname(&mod, namep, sym, KSYMS_ANY|KSYMS_EXACT) == 0) {
    255  1.35     ragge 		if (valuep)
    256  1.35     ragge 			*valuep = sym;
    257  1.35     ragge 	} else
    258  1.35     ragge 		*namep = NULL;
    259   1.1       cgd }
    260   1.1       cgd 
    261   1.1       cgd 
    262   1.1       cgd /*
    263   1.1       cgd  * Print a the closest symbol to value
    264   1.1       cgd  *
    265   1.1       cgd  * After matching the symbol according to the given strategy
    266   1.1       cgd  * we print it in the name+offset format, provided the symbol's
    267   1.1       cgd  * value is close enough (eg smaller than db_maxoff).
    268   1.1       cgd  * We also attempt to print [filename:linenum] when applicable
    269   1.1       cgd  * (eg for procedure names).
    270   1.1       cgd  *
    271   1.1       cgd  * If we could not find a reasonable name+offset representation,
    272   1.1       cgd  * then we just print the value in hex.  Small values might get
    273   1.1       cgd  * bogus symbol associations, e.g. 3 might get some absolute
    274   1.1       cgd  * value like _INCLUDE_VERSION or something, therefore we do
    275   1.1       cgd  * not accept symbols whose value is zero (and use plain hex).
    276   1.9       gwr  * Also, avoid printing as "end+0x????" which is useless.
    277   1.9       gwr  * The variable db_lastsym is used instead of "end" in case we
    278   1.9       gwr  * add support for symbols in loadable driver modules.
    279   1.1       cgd  */
    280   1.9       gwr extern char end[];
    281  1.13       cgd unsigned long	db_lastsym = (unsigned long)end;
    282  1.13       cgd unsigned int	db_maxoff = 0x10000000;
    283   1.1       cgd 
    284  1.30     jhawk void
    285  1.40    itojun db_symstr(char *buf, size_t buflen, db_expr_t off, db_strategy_t strategy)
    286  1.30     jhawk {
    287  1.39  jdolecek 	char  *name;
    288  1.39  jdolecek 	const char *mod;
    289  1.38     ragge 	long val;
    290  1.38     ragge 
    291  1.38     ragge #ifdef DB_AOUT_SYMBOLS
    292  1.38     ragge 	if (using_aout_symtab) {
    293  1.38     ragge 		db_expr_t	d;
    294  1.38     ragge 		char 		*filename;
    295  1.38     ragge 		char		*name;
    296  1.38     ragge 		db_expr_t	value;
    297  1.38     ragge 		int 		linenum;
    298  1.38     ragge 		db_sym_t	cursym;
    299  1.38     ragge 
    300  1.38     ragge 		if ((unsigned long) off <= db_lastsym) {
    301  1.38     ragge 			cursym = db_search_symbol(off, strategy, &d);
    302  1.38     ragge 			db_symbol_values(cursym, &name, &value);
    303  1.38     ragge 			if (name != NULL &&
    304  1.38     ragge 			    ((unsigned int) d < db_maxoff) &&
    305  1.38     ragge 			    value != 0) {
    306  1.40    itojun 				strlcpy(buf, name, buflen);
    307  1.38     ragge 				if (d) {
    308  1.40    itojun 					strlcat(buf, "+", buflen);
    309  1.38     ragge 					db_format_radix(buf+strlen(buf),
    310  1.38     ragge 					    24, d, TRUE);
    311  1.38     ragge 				}
    312  1.38     ragge 				if (strategy == DB_STGY_PROC) {
    313  1.38     ragge 					if ((*db_symformat->sym_line_at_pc)
    314  1.38     ragge 					    (NULL, cursym, &filename,
    315  1.38     ragge 					    &linenum, off))
    316  1.38     ragge 						sprintf(buf+strlen(buf),
    317  1.38     ragge 						    " [%s:%d]",
    318  1.38     ragge 						    filename, linenum);
    319  1.38     ragge 				}
    320  1.38     ragge 				return;
    321  1.38     ragge 			}
    322  1.38     ragge 		}
    323  1.40    itojun 		strlcpy(buf, db_num_to_str(off), buflen);
    324  1.38     ragge 		return;
    325  1.38     ragge 	}
    326  1.38     ragge #endif
    327  1.38     ragge 	if (ksyms_getname(&mod, &name, off, strategy|KSYMS_CLOSEST) == 0) {
    328  1.38     ragge 		(void)ksyms_getval(mod, name, &val, KSYMS_ANY);
    329  1.38     ragge 		if (((off - val) < db_maxoff) && val) {
    330  1.38     ragge 			sprintf(buf, "%s:%s", mod, name);
    331  1.38     ragge 			if (off - val) {
    332  1.40    itojun 				strlcat(buf, "+", buflen);
    333  1.38     ragge 				db_format_radix(buf+strlen(buf),
    334  1.38     ragge 				    24, off - val, TRUE);
    335  1.30     jhawk 			}
    336  1.38     ragge #ifdef notyet
    337  1.38     ragge 			if (strategy & KSYMS_PROC) {
    338  1.38     ragge 				if (ksyms_fmaddr(off, &filename, &linenum) == 0)					sprintf(buf+strlen(buf),
    339  1.30     jhawk 					    " [%s:%d]", filename, linenum);
    340  1.30     jhawk 			}
    341  1.38     ragge #endif
    342  1.30     jhawk 			return;
    343  1.30     jhawk 		}
    344  1.30     jhawk 	}
    345  1.40    itojun 	strlcpy(buf, db_num_to_str(off), buflen);
    346  1.30     jhawk }
    347   1.9       gwr 
    348   1.1       cgd void
    349  1.31    simonb db_printsym(db_expr_t off, db_strategy_t strategy,
    350  1.31    simonb     void (*pr)(const char *, ...))
    351   1.1       cgd {
    352  1.39  jdolecek 	char  *name;
    353  1.39  jdolecek 	const char *mod;
    354  1.35     ragge 	long val;
    355  1.35     ragge #ifdef notyet
    356  1.35     ragge 	char *filename;
    357  1.35     ragge 	int  linenum;
    358  1.35     ragge #endif
    359   1.1       cgd 
    360  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    361  1.35     ragge 	if (using_aout_symtab) {
    362  1.35     ragge 		db_expr_t	d;
    363  1.35     ragge 		char 		*filename;
    364  1.35     ragge 		char		*name;
    365  1.35     ragge 		db_expr_t	value;
    366  1.35     ragge 		int 		linenum;
    367  1.35     ragge 		db_sym_t	cursym;
    368  1.35     ragge 		if ((unsigned long) off <= db_lastsym) {
    369  1.35     ragge 			cursym = db_search_symbol(off, strategy, &d);
    370  1.35     ragge 			db_symbol_values(cursym, &name, &value);
    371  1.35     ragge 			if (name != NULL &&
    372  1.35     ragge 			    ((unsigned int) d < db_maxoff) &&
    373  1.35     ragge 			    value != 0) {
    374  1.35     ragge 				(*pr)("%s", name);
    375  1.35     ragge 				if (d) {
    376  1.35     ragge 					char tbuf[24];
    377  1.35     ragge 
    378  1.35     ragge 					db_format_radix(tbuf, 24, d, TRUE);
    379  1.35     ragge 					(*pr)("+%s", tbuf);
    380  1.35     ragge 				}
    381  1.35     ragge 				if (strategy == DB_STGY_PROC) {
    382  1.35     ragge 					if ((*db_symformat->sym_line_at_pc)
    383  1.35     ragge 					    (NULL, cursym, &filename,
    384  1.35     ragge 					    &linenum, off))
    385  1.35     ragge 						(*pr)(" [%s:%d]",
    386  1.35     ragge 						    filename, linenum);
    387  1.35     ragge 				}
    388  1.35     ragge 				return;
    389  1.35     ragge 			}
    390  1.35     ragge 		}
    391  1.35     ragge 		(*pr)(db_num_to_str(off));
    392  1.35     ragge 		return;
    393  1.35     ragge 	}
    394  1.35     ragge #endif
    395  1.35     ragge 	if (ksyms_getname(&mod, &name, off, strategy|KSYMS_CLOSEST) == 0) {
    396  1.35     ragge 		(void)ksyms_getval(mod, name, &val, KSYMS_ANY);
    397  1.35     ragge 		if (((off - val) < db_maxoff) && val) {
    398  1.35     ragge 			(*pr)("%s:%s", mod, name);
    399  1.35     ragge 			if (off - val) {
    400  1.23        tv 				char tbuf[24];
    401  1.23        tv 
    402  1.35     ragge 				db_format_radix(tbuf, 24, off - val, TRUE);
    403  1.24        tv 				(*pr)("+%s", tbuf);
    404  1.23        tv 			}
    405  1.35     ragge #ifdef notyet
    406  1.35     ragge 			if (strategy & KSYMS_PROC) {
    407  1.35     ragge 				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
    408  1.21     jhawk 					(*pr)(" [%s:%d]", filename, linenum);
    409   1.9       gwr 			}
    410  1.35     ragge #endif
    411   1.9       gwr 			return;
    412   1.9       gwr 		}
    413   1.1       cgd 	}
    414  1.22     jhawk 	(*pr)(db_num_to_str(off));
    415   1.9       gwr 	return;
    416   1.1       cgd }
    417   1.1       cgd 
    418  1.35     ragge /*
    419  1.35     ragge  * Splits a string in the form "mod:sym" to two strings.
    420  1.35     ragge  */
    421  1.31    simonb static void
    422  1.35     ragge db_symsplit(char *str, char **mod, char **sym)
    423  1.15   thorpej {
    424  1.35     ragge 	char *cp;
    425  1.15   thorpej 
    426  1.35     ragge 	if ((cp = strchr(str, ':')) != NULL) {
    427  1.35     ragge 		*cp++ = '\0';
    428  1.35     ragge 		*mod = str;
    429  1.35     ragge 		*sym = cp;
    430  1.35     ragge 	} else {
    431  1.35     ragge 		*mod = NULL;
    432  1.35     ragge 		*sym = str;
    433  1.35     ragge 	}
    434   1.1       cgd }
    435  1.36     ragge 
    436  1.36     ragge boolean_t
    437  1.36     ragge db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep)
    438  1.36     ragge {
    439  1.36     ragge #ifdef DB_AOUT_SYMBOLS
    440  1.36     ragge 	if (using_aout_symtab)
    441  1.36     ragge 		return ((*db_symformat->sym_numargs)(NULL, cursym, nargp,
    442  1.36     ragge 		    argnamep));
    443  1.36     ragge #endif
    444  1.36     ragge 	return (FALSE);
    445  1.36     ragge }
    446  1.36     ragge 
    447