Home | History | Annotate | Line # | Download | only in ddb
db_sym.c revision 1.21
      1  1.21     jhawk /*	$NetBSD: db_sym.c,v 1.21 2000/05/25 19:57:36 jhawk Exp $	*/
      2   1.7       cgd 
      3   1.1       cgd /*
      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.1       cgd  *
      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.1       cgd  *
     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.1       cgd  *
     18   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19   1.1       cgd  *
     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.1       cgd  *
     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.5   mycroft 
     29   1.6   mycroft #include <sys/param.h>
     30   1.6   mycroft #include <sys/proc.h>
     31  1.15   thorpej #include <sys/systm.h>
     32   1.6   mycroft 
     33   1.1       cgd #include <machine/db_machdep.h>
     34   1.6   mycroft 
     35  1.20     jhawk #include <ddb/db_lex.h>
     36   1.1       cgd #include <ddb/db_sym.h>
     37  1.12  christos #include <ddb/db_output.h>
     38  1.12  christos #include <ddb/db_extern.h>
     39  1.12  christos #include <ddb/db_command.h>
     40   1.1       cgd 
     41   1.1       cgd /*
     42   1.1       cgd  * Multiple symbol tables
     43   1.1       cgd  */
     44   1.4    brezak #ifndef MAXLKMS
     45   1.4    brezak #define MAXLKMS 20
     46   1.4    brezak #endif
     47   1.4    brezak 
     48   1.3    brezak #ifndef MAXNOSYMTABS
     49   1.4    brezak #define	MAXNOSYMTABS	MAXLKMS+1	/* Room for kernel + LKM's */
     50   1.3    brezak #endif
     51   1.1       cgd 
     52   1.1       cgd db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
     53   1.1       cgd 
     54   1.1       cgd db_symtab_t	*db_last_symtab;
     55   1.1       cgd 
     56  1.15   thorpej static char *db_qualify __P((db_sym_t, const char *));
     57  1.20     jhawk static db_forall_func_t db_sift;
     58  1.15   thorpej 
     59  1.15   thorpej /*
     60  1.15   thorpej  * Put the most picky symbol table formats at the top!
     61  1.15   thorpej  */
     62  1.15   thorpej const db_symformat_t *db_symformats[] = {
     63  1.15   thorpej #ifdef DB_ELF_SYMBOLS
     64  1.15   thorpej 	&db_symformat_elf,
     65  1.15   thorpej #endif
     66  1.15   thorpej #ifdef DB_AOUT_SYMBOLS
     67  1.15   thorpej 	&db_symformat_aout,
     68  1.15   thorpej #endif
     69  1.15   thorpej 	NULL,
     70  1.15   thorpej };
     71  1.15   thorpej 
     72  1.15   thorpej const db_symformat_t *db_symformat;
     73  1.15   thorpej 
     74  1.15   thorpej boolean_t	X_db_sym_init __P((int, void *, void *, const char *));
     75  1.15   thorpej db_sym_t	X_db_lookup __P((db_symtab_t *, char *));
     76  1.15   thorpej db_sym_t	X_db_search_symbol __P((db_symtab_t *, db_addr_t,
     77  1.15   thorpej 		    db_strategy_t, db_expr_t *));
     78  1.15   thorpej void		X_db_symbol_values __P((db_symtab_t *, db_sym_t, char **,
     79  1.15   thorpej 		    db_expr_t *));
     80  1.15   thorpej boolean_t	X_db_line_at_pc __P((db_symtab_t *, db_sym_t, char **,
     81  1.15   thorpej 		    int *, db_expr_t));
     82  1.15   thorpej int		X_db_sym_numargs __P((db_symtab_t *, db_sym_t, int *,
     83  1.15   thorpej 		    char **));
     84  1.20     jhawk void		X_db_forall __P((db_symtab_t *,
     85  1.20     jhawk 		    db_forall_func_t db_forall_func, void *));
     86  1.15   thorpej 
     87  1.15   thorpej /*
     88  1.15   thorpej  * Initialize the kernel debugger by initializing the master symbol
     89  1.15   thorpej  * table.  Note that if initializing the master symbol table fails,
     90  1.15   thorpej  * no other symbol tables can be loaded.
     91  1.15   thorpej  */
     92  1.15   thorpej void
     93  1.15   thorpej ddb_init(symsize, vss, vse)
     94  1.15   thorpej 	int symsize;
     95  1.15   thorpej 	void *vss, *vse;
     96  1.15   thorpej {
     97  1.15   thorpej 	const db_symformat_t **symf;
     98  1.15   thorpej 	const char *name = "netbsd";
     99  1.15   thorpej 
    100  1.15   thorpej 	if (symsize <= 0) {
    101  1.15   thorpej 		printf(" [ no symbols available ]\n");
    102  1.15   thorpej 		return;
    103  1.15   thorpej 	}
    104  1.15   thorpej 
    105  1.15   thorpej 	/*
    106  1.15   thorpej 	 * Do this check now for the master symbol table to avoid printing
    107  1.15   thorpej 	 * the message N times.
    108  1.15   thorpej 	 */
    109  1.15   thorpej 	if (ALIGNED_POINTER(vss, long) == 0) {
    110  1.18    simonb 		printf("[ %s symbol table has bad start address %p ]\n",
    111  1.15   thorpej 		    name, vss);
    112  1.15   thorpej 		return;
    113  1.15   thorpej 	}
    114  1.15   thorpej 
    115  1.15   thorpej 	for (symf = db_symformats; *symf != NULL; symf++) {
    116  1.15   thorpej 		db_symformat = *symf;
    117  1.15   thorpej 		if (X_db_sym_init(symsize, vss, vse, name) == TRUE)
    118  1.15   thorpej 			return;
    119  1.15   thorpej 	}
    120  1.15   thorpej 
    121  1.15   thorpej 	db_symformat = NULL;
    122  1.15   thorpej 	printf("[ no symbol table formats found ]\n");
    123  1.15   thorpej }
    124   1.1       cgd 
    125   1.1       cgd /*
    126   1.1       cgd  * Add symbol table, with given name, to list of symbol tables.
    127   1.1       cgd  */
    128   1.4    brezak int
    129   1.1       cgd db_add_symbol_table(start, end, name, ref)
    130   1.1       cgd 	char *start;
    131   1.1       cgd 	char *end;
    132  1.15   thorpej 	const char *name;
    133   1.1       cgd 	char *ref;
    134   1.1       cgd {
    135   1.4    brezak 	int slot;
    136   1.4    brezak 
    137   1.4    brezak 	for (slot = 0; slot < MAXNOSYMTABS; slot++) {
    138   1.4    brezak 		if (db_symtabs[slot].name == NULL)
    139   1.4    brezak 			break;
    140   1.4    brezak 	}
    141   1.4    brezak 	if (slot >= MAXNOSYMTABS) {
    142  1.12  christos 		db_printf("No slots left for %s symbol table", name);
    143   1.4    brezak 		return(-1);
    144   1.1       cgd 	}
    145   1.1       cgd 
    146   1.4    brezak 	db_symtabs[slot].start = start;
    147   1.4    brezak 	db_symtabs[slot].end = end;
    148   1.4    brezak 	db_symtabs[slot].name = name;
    149   1.4    brezak 	db_symtabs[slot].private = ref;
    150   1.4    brezak 
    151   1.4    brezak 	return(slot);
    152   1.4    brezak }
    153   1.4    brezak 
    154   1.4    brezak /*
    155   1.4    brezak  * Delete a symbol table. Caller is responsible for freeing storage.
    156   1.4    brezak  */
    157   1.4    brezak void
    158   1.4    brezak db_del_symbol_table(name)
    159   1.4    brezak 	char *name;
    160   1.4    brezak {
    161   1.4    brezak 	int slot;
    162   1.4    brezak 
    163   1.4    brezak 	for (slot = 0; slot < MAXNOSYMTABS; slot++) {
    164   1.4    brezak 		if (db_symtabs[slot].name &&
    165   1.4    brezak 		    ! strcmp(db_symtabs[slot].name, name))
    166   1.4    brezak 			break;
    167   1.4    brezak 	}
    168   1.4    brezak 	if (slot >= MAXNOSYMTABS) {
    169  1.12  christos 		db_printf("Unable to find symbol table slot for %s.", name);
    170   1.4    brezak 		return;
    171   1.4    brezak 	}
    172   1.4    brezak 
    173   1.4    brezak 	db_symtabs[slot].start = 0;
    174   1.4    brezak 	db_symtabs[slot].end = 0;
    175   1.4    brezak 	db_symtabs[slot].name = 0;
    176   1.4    brezak 	db_symtabs[slot].private = 0;
    177   1.1       cgd }
    178   1.1       cgd 
    179   1.1       cgd /*
    180   1.4    brezak  *  db_qualify("vm_map", "netbsd") returns "netbsd:vm_map".
    181   1.1       cgd  *
    182   1.1       cgd  *  Note: return value points to static data whose content is
    183   1.1       cgd  *  overwritten by each call... but in practice this seems okay.
    184   1.1       cgd  */
    185   1.1       cgd static char *
    186   1.1       cgd db_qualify(sym, symtabname)
    187   1.1       cgd 	db_sym_t	sym;
    188  1.15   thorpej 	const char	*symtabname;
    189   1.1       cgd {
    190   1.1       cgd 	char		*symname;
    191   1.1       cgd 	static char     tmp[256];
    192  1.19  augustss 	char	*s;
    193   1.1       cgd 
    194   1.1       cgd 	db_symbol_values(sym, &symname, 0);
    195   1.1       cgd 	s = tmp;
    196  1.12  christos 	while ((*s++ = *symtabname++) != '\0')
    197  1.12  christos 		;
    198   1.1       cgd 	s[-1] = ':';
    199  1.12  christos 	while ((*s++ = *symname++) != '\0')
    200  1.12  christos 		;
    201   1.1       cgd 	return tmp;
    202   1.1       cgd }
    203   1.1       cgd 
    204   1.1       cgd 
    205   1.1       cgd boolean_t
    206   1.1       cgd db_eqname(src, dst, c)
    207   1.1       cgd 	char *src;
    208   1.1       cgd 	char *dst;
    209  1.12  christos 	int c;
    210   1.1       cgd {
    211   1.1       cgd 	if (!strcmp(src, dst))
    212   1.1       cgd 	    return (TRUE);
    213   1.1       cgd 	if (src[0] == c)
    214   1.1       cgd 	    return (!strcmp(src+1,dst));
    215   1.1       cgd 	return (FALSE);
    216   1.1       cgd }
    217   1.1       cgd 
    218   1.1       cgd boolean_t
    219   1.1       cgd db_value_of_name(name, valuep)
    220   1.1       cgd 	char		*name;
    221   1.1       cgd 	db_expr_t	*valuep;
    222   1.1       cgd {
    223   1.1       cgd 	db_sym_t	sym;
    224   1.1       cgd 
    225   1.1       cgd 	sym = db_lookup(name);
    226   1.1       cgd 	if (sym == DB_SYM_NULL)
    227   1.1       cgd 	    return (FALSE);
    228   1.1       cgd 	db_symbol_values(sym, &name, valuep);
    229   1.1       cgd 	return (TRUE);
    230   1.1       cgd }
    231   1.1       cgd 
    232   1.1       cgd 
    233   1.1       cgd /*
    234   1.1       cgd  * Lookup a symbol.
    235   1.1       cgd  * If the symbol has a qualifier (e.g., ux:vm_map),
    236   1.1       cgd  * then only the specified symbol table will be searched;
    237   1.1       cgd  * otherwise, all symbol tables will be searched.
    238   1.1       cgd  */
    239   1.1       cgd db_sym_t
    240   1.1       cgd db_lookup(symstr)
    241   1.1       cgd 	char *symstr;
    242   1.1       cgd {
    243   1.1       cgd 	db_sym_t sp;
    244  1.19  augustss 	int i;
    245   1.1       cgd 	int symtab_start = 0;
    246   1.4    brezak 	int symtab_end = MAXNOSYMTABS;
    247  1.19  augustss 	char *cp;
    248   1.1       cgd 
    249   1.1       cgd 	/*
    250   1.1       cgd 	 * Look for, remove, and remember any symbol table specifier.
    251   1.1       cgd 	 */
    252   1.1       cgd 	for (cp = symstr; *cp; cp++) {
    253   1.1       cgd 		if (*cp == ':') {
    254   1.1       cgd 			*cp = '\0';
    255   1.4    brezak 			for (i = 0; i < MAXNOSYMTABS; i++) {
    256   1.4    brezak 				if (db_symtabs[i].name &&
    257   1.4    brezak 				    ! strcmp(symstr, db_symtabs[i].name)) {
    258   1.1       cgd 					symtab_start = i;
    259   1.1       cgd 					symtab_end = i + 1;
    260   1.1       cgd 					break;
    261   1.1       cgd 				}
    262   1.1       cgd 			}
    263   1.1       cgd 			*cp = ':';
    264   1.4    brezak 			if (i == MAXNOSYMTABS) {
    265   1.1       cgd 				db_error("invalid symbol table name");
    266   1.8   mycroft 				/*NOTREACHED*/
    267   1.1       cgd 			}
    268   1.1       cgd 			symstr = cp+1;
    269   1.1       cgd 		}
    270   1.1       cgd 	}
    271   1.1       cgd 
    272   1.1       cgd 	/*
    273   1.1       cgd 	 * Look in the specified set of symbol tables.
    274   1.1       cgd 	 * Return on first match.
    275   1.1       cgd 	 */
    276   1.1       cgd 	for (i = symtab_start; i < symtab_end; i++) {
    277   1.4    brezak 		if (db_symtabs[i].name &&
    278   1.4    brezak 		    (sp = X_db_lookup(&db_symtabs[i], symstr))) {
    279   1.1       cgd 			db_last_symtab = &db_symtabs[i];
    280   1.1       cgd 			return sp;
    281   1.1       cgd 		}
    282   1.1       cgd 	}
    283   1.1       cgd 	return 0;
    284   1.1       cgd }
    285   1.1       cgd 
    286  1.20     jhawk /* Private structure for passing args to db_sift() from db_sifting(). */
    287  1.20     jhawk struct db_sift_args {
    288  1.20     jhawk 	char	*symstr;
    289  1.20     jhawk 	int	mode;
    290  1.20     jhawk };
    291  1.20     jhawk 
    292  1.20     jhawk /*
    293  1.20     jhawk  * Does the work of db_sifting(), called once for each
    294  1.20     jhawk  * symbol via X_db_forall(), prints out symbols matching
    295  1.20     jhawk  * criteria.
    296  1.20     jhawk  */
    297  1.20     jhawk static void
    298  1.20     jhawk db_sift(stab, sym, name, suffix, prefix, arg)
    299  1.20     jhawk 	db_symtab_t	*stab;
    300  1.20     jhawk 	db_sym_t	sym;
    301  1.20     jhawk 	char		*name;
    302  1.20     jhawk 	char		*suffix;
    303  1.20     jhawk 	int		prefix;
    304  1.20     jhawk 	void		*arg;
    305  1.20     jhawk {
    306  1.20     jhawk 	char c, sc;
    307  1.20     jhawk 	char *find, *p;
    308  1.20     jhawk 	size_t len;
    309  1.20     jhawk 	struct db_sift_args *dsa;
    310  1.20     jhawk 
    311  1.20     jhawk 	dsa = (struct db_sift_args*)arg;
    312  1.20     jhawk 
    313  1.20     jhawk 	find = dsa->symstr;	/* String we're looking for. */
    314  1.20     jhawk 	p = name;		/* String we're searching within. */
    315  1.20     jhawk 
    316  1.20     jhawk 	/* Matching algorithm cribbed from strstr(), which is not
    317  1.20     jhawk 	   in the kernel. */
    318  1.20     jhawk 	if ((c = *find++) != 0) {
    319  1.20     jhawk 		len = strlen(find);
    320  1.20     jhawk 		do {
    321  1.20     jhawk 			do {
    322  1.20     jhawk 				if ((sc = *p++) == 0)
    323  1.20     jhawk 					return;
    324  1.20     jhawk 			} while (sc != c);
    325  1.20     jhawk 		} while (strncmp(p, find, len) != 0);
    326  1.20     jhawk 	}
    327  1.20     jhawk 	if (dsa->mode=='F')	/* ala ls -F */
    328  1.20     jhawk 		db_printf("%s%s ", name, suffix);
    329  1.20     jhawk 	else
    330  1.20     jhawk 		db_printf("%s ", name);
    331  1.20     jhawk }
    332  1.20     jhawk 
    333  1.20     jhawk /*
    334  1.20     jhawk  * "Sift" for a partial symbol.
    335  1.20     jhawk  * Named for the Sun OpenPROM command ("sifting").
    336  1.20     jhawk  * If the symbol has a qualifier (e.g., ux:vm_map),
    337  1.20     jhawk  * then only the specified symbol table will be searched;
    338  1.20     jhawk  * otherwise, all symbol tables will be searched..
    339  1.20     jhawk  *
    340  1.20     jhawk  * "mode" is how-to-display, set from modifiers.
    341  1.20     jhawk  */
    342  1.20     jhawk void
    343  1.20     jhawk db_sifting(symstr, mode)
    344  1.20     jhawk 	char	*symstr;
    345  1.20     jhawk 	int	mode;
    346  1.20     jhawk {
    347  1.20     jhawk 	char *cp;
    348  1.20     jhawk 	int i;
    349  1.20     jhawk 	int symtab_start = 0;
    350  1.20     jhawk 	int symtab_end = MAXNOSYMTABS;
    351  1.20     jhawk 	struct db_sift_args dsa;
    352  1.20     jhawk 
    353  1.20     jhawk 	/*
    354  1.20     jhawk 	 * Look for, remove, and remember any symbol table specifier.
    355  1.20     jhawk 	 */
    356  1.20     jhawk 	for (cp = symstr; *cp; cp++) {
    357  1.20     jhawk 		if (*cp == ':') {
    358  1.20     jhawk 			*cp = '\0';
    359  1.20     jhawk 			for (i = 0; i < MAXNOSYMTABS; i++) {
    360  1.20     jhawk 				if (db_symtabs[i].name &&
    361  1.20     jhawk 				    ! strcmp(symstr, db_symtabs[i].name)) {
    362  1.20     jhawk 					symtab_start = i;
    363  1.20     jhawk 					symtab_end = i + 1;
    364  1.20     jhawk 					break;
    365  1.20     jhawk 				}
    366  1.20     jhawk 			}
    367  1.20     jhawk 			*cp = ':';
    368  1.20     jhawk 			if (i == MAXNOSYMTABS) {
    369  1.20     jhawk 				db_error("invalid symbol table name");
    370  1.20     jhawk 				/*NOTREACHED*/
    371  1.20     jhawk 			}
    372  1.20     jhawk 			symstr = cp+1;
    373  1.20     jhawk 		}
    374  1.20     jhawk 	}
    375  1.20     jhawk 
    376  1.20     jhawk 	/* Pass args to db_sift(). */
    377  1.20     jhawk 	dsa.symstr = symstr;
    378  1.20     jhawk 	dsa.mode = mode;
    379  1.20     jhawk 
    380  1.20     jhawk 	/*
    381  1.20     jhawk 	 * Look in the specified set of symbol tables.
    382  1.20     jhawk 	 */
    383  1.20     jhawk 	for (i = symtab_start; i < symtab_end; i++)
    384  1.20     jhawk 		if (db_symtabs[i].name) {
    385  1.20     jhawk 			db_printf("Sifting table %s:\n", db_symtabs[i].name);
    386  1.20     jhawk 			X_db_forall(&db_symtabs[i], db_sift, &dsa);
    387  1.20     jhawk 		}
    388  1.20     jhawk 
    389  1.20     jhawk 	return;
    390  1.20     jhawk }
    391  1.20     jhawk 
    392  1.20     jhawk 
    393   1.1       cgd /*
    394   1.1       cgd  * Does this symbol name appear in more than one symbol table?
    395   1.1       cgd  * Used by db_symbol_values to decide whether to qualify a symbol.
    396   1.1       cgd  */
    397   1.1       cgd boolean_t db_qualify_ambiguous_names = FALSE;
    398   1.1       cgd 
    399   1.1       cgd boolean_t
    400   1.1       cgd db_symbol_is_ambiguous(sym)
    401   1.1       cgd 	db_sym_t	sym;
    402   1.1       cgd {
    403   1.1       cgd 	char		*sym_name;
    404  1.19  augustss 	int	i;
    405   1.1       cgd 	boolean_t	found_once = FALSE;
    406   1.1       cgd 
    407   1.1       cgd 	if (!db_qualify_ambiguous_names)
    408   1.1       cgd 		return FALSE;
    409   1.1       cgd 
    410   1.1       cgd 	db_symbol_values(sym, &sym_name, 0);
    411   1.4    brezak 	for (i = 0; i < MAXNOSYMTABS; i++) {
    412   1.4    brezak 		if (db_symtabs[i].name &&
    413   1.4    brezak 		    X_db_lookup(&db_symtabs[i], sym_name)) {
    414   1.1       cgd 			if (found_once)
    415   1.1       cgd 				return TRUE;
    416   1.1       cgd 			found_once = TRUE;
    417   1.1       cgd 		}
    418   1.1       cgd 	}
    419   1.1       cgd 	return FALSE;
    420   1.1       cgd }
    421   1.1       cgd 
    422   1.1       cgd /*
    423   1.1       cgd  * Find the closest symbol to val, and return its name
    424   1.1       cgd  * and the difference between val and the symbol found.
    425   1.1       cgd  */
    426   1.1       cgd db_sym_t
    427   1.1       cgd db_search_symbol( val, strategy, offp)
    428  1.19  augustss 	db_addr_t		val;
    429   1.1       cgd 	db_strategy_t		strategy;
    430   1.1       cgd 	db_expr_t		*offp;
    431   1.1       cgd {
    432   1.1       cgd 	unsigned int	diff;
    433  1.13       cgd 	db_expr_t	newdiff;
    434  1.19  augustss 	int		i;
    435   1.1       cgd 	db_sym_t	ret = DB_SYM_NULL, sym;
    436   1.1       cgd 
    437   1.1       cgd 	newdiff = diff = ~0;
    438   1.1       cgd 	db_last_symtab = 0;
    439   1.4    brezak 	for (i = 0; i < MAXNOSYMTABS; i++) {
    440   1.4    brezak 	    if (!db_symtabs[i].name)
    441   1.4    brezak 	        continue;
    442   1.1       cgd 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
    443   1.1       cgd 	    if (newdiff < diff) {
    444   1.1       cgd 		db_last_symtab = &db_symtabs[i];
    445   1.1       cgd 		diff = newdiff;
    446   1.1       cgd 		ret = sym;
    447   1.1       cgd 	    }
    448   1.1       cgd 	}
    449   1.1       cgd 	*offp = diff;
    450   1.1       cgd 	return ret;
    451   1.1       cgd }
    452   1.1       cgd 
    453   1.1       cgd /*
    454   1.1       cgd  * Return name and value of a symbol
    455   1.1       cgd  */
    456   1.1       cgd void
    457   1.1       cgd db_symbol_values(sym, namep, valuep)
    458   1.1       cgd 	db_sym_t	sym;
    459   1.1       cgd 	char		**namep;
    460   1.1       cgd 	db_expr_t	*valuep;
    461   1.1       cgd {
    462   1.1       cgd 	db_expr_t	value;
    463   1.1       cgd 
    464   1.1       cgd 	if (sym == DB_SYM_NULL) {
    465   1.1       cgd 		*namep = 0;
    466   1.1       cgd 		return;
    467   1.1       cgd 	}
    468   1.1       cgd 
    469  1.14   thorpej 	X_db_symbol_values(db_last_symtab, sym, namep, &value);
    470   1.1       cgd 
    471   1.1       cgd 	if (db_symbol_is_ambiguous(sym))
    472   1.1       cgd 		*namep = db_qualify(sym, db_last_symtab->name);
    473   1.1       cgd 	if (valuep)
    474   1.1       cgd 		*valuep = value;
    475   1.1       cgd }
    476   1.1       cgd 
    477   1.1       cgd 
    478   1.1       cgd /*
    479   1.1       cgd  * Print a the closest symbol to value
    480   1.1       cgd  *
    481   1.1       cgd  * After matching the symbol according to the given strategy
    482   1.1       cgd  * we print it in the name+offset format, provided the symbol's
    483   1.1       cgd  * value is close enough (eg smaller than db_maxoff).
    484   1.1       cgd  * We also attempt to print [filename:linenum] when applicable
    485   1.1       cgd  * (eg for procedure names).
    486   1.1       cgd  *
    487   1.1       cgd  * If we could not find a reasonable name+offset representation,
    488   1.1       cgd  * then we just print the value in hex.  Small values might get
    489   1.1       cgd  * bogus symbol associations, e.g. 3 might get some absolute
    490   1.1       cgd  * value like _INCLUDE_VERSION or something, therefore we do
    491   1.1       cgd  * not accept symbols whose value is zero (and use plain hex).
    492   1.9       gwr  * Also, avoid printing as "end+0x????" which is useless.
    493   1.9       gwr  * The variable db_lastsym is used instead of "end" in case we
    494   1.9       gwr  * add support for symbols in loadable driver modules.
    495   1.1       cgd  */
    496   1.9       gwr extern char end[];
    497  1.13       cgd unsigned long	db_lastsym = (unsigned long)end;
    498  1.13       cgd unsigned int	db_maxoff = 0x10000000;
    499   1.1       cgd 
    500   1.9       gwr 
    501   1.1       cgd void
    502  1.21     jhawk db_printsym(off, strategy, pr)
    503   1.1       cgd 	db_expr_t	off;
    504   1.1       cgd 	db_strategy_t	strategy;
    505  1.21     jhawk 	void (*pr) __P((const char *, ...));
    506   1.1       cgd {
    507   1.1       cgd 	db_expr_t	d;
    508   1.1       cgd 	char 		*filename;
    509   1.1       cgd 	char		*name;
    510   1.1       cgd 	db_expr_t	value;
    511   1.1       cgd 	int 		linenum;
    512   1.1       cgd 	db_sym_t	cursym;
    513   1.1       cgd 
    514   1.9       gwr 	if (off <= db_lastsym) {
    515   1.9       gwr 		cursym = db_search_symbol(off, strategy, &d);
    516   1.9       gwr 		db_symbol_values(cursym, &name, &value);
    517   1.9       gwr 		if (name && (d < db_maxoff) && value) {
    518  1.21     jhawk 			(*pr)("%s", name);
    519   1.9       gwr 			if (d)
    520  1.21     jhawk 				(*pr)("+%#lr", d);
    521   1.9       gwr 			if (strategy == DB_STGY_PROC) {
    522   1.9       gwr 				if (db_line_at_pc(cursym, &filename, &linenum, off))
    523  1.21     jhawk 					(*pr)(" [%s:%d]", filename, linenum);
    524   1.9       gwr 			}
    525   1.9       gwr 			return;
    526   1.9       gwr 		}
    527   1.1       cgd 	}
    528  1.21     jhawk 	(*pr)("%#ln", off);
    529   1.9       gwr 	return;
    530   1.1       cgd }
    531   1.1       cgd 
    532   1.1       cgd 
    533   1.1       cgd boolean_t
    534   1.1       cgd db_line_at_pc( sym, filename, linenum, pc)
    535   1.3    brezak 	db_sym_t	sym;
    536   1.3    brezak 	char		**filename;
    537   1.3    brezak 	int		*linenum;
    538   1.3    brezak 	db_expr_t	pc;
    539   1.1       cgd {
    540   1.1       cgd 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
    541   1.3    brezak }
    542   1.3    brezak 
    543   1.3    brezak int
    544   1.3    brezak db_sym_numargs(sym, nargp, argnames)
    545   1.3    brezak 	db_sym_t	sym;
    546   1.3    brezak 	int		*nargp;
    547   1.3    brezak 	char		**argnames;
    548   1.3    brezak {
    549   1.3    brezak 	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
    550  1.15   thorpej }
    551  1.15   thorpej 
    552  1.15   thorpej boolean_t
    553  1.15   thorpej X_db_sym_init(symsize, vss, vse, name)
    554  1.15   thorpej 	int symsize;
    555  1.15   thorpej 	void *vss, *vse;
    556  1.15   thorpej 	const char *name;
    557  1.15   thorpej {
    558  1.15   thorpej 
    559  1.15   thorpej 	if (db_symformat != NULL)
    560  1.15   thorpej 		return ((*db_symformat->sym_init)(symsize, vss, vse, name));
    561  1.15   thorpej 	return (FALSE);
    562  1.15   thorpej }
    563  1.15   thorpej 
    564  1.15   thorpej db_sym_t
    565  1.15   thorpej X_db_lookup(stab, symstr)
    566  1.15   thorpej 	db_symtab_t *stab;
    567  1.15   thorpej 	char *symstr;
    568  1.15   thorpej {
    569  1.15   thorpej 
    570  1.15   thorpej 	if (db_symformat != NULL)
    571  1.15   thorpej 		return ((*db_symformat->sym_lookup)(stab, symstr));
    572  1.15   thorpej 	return ((db_sym_t)0);
    573  1.15   thorpej }
    574  1.15   thorpej 
    575  1.15   thorpej db_sym_t
    576  1.15   thorpej X_db_search_symbol(stab, off, strategy, diffp)
    577  1.15   thorpej 	db_symtab_t *stab;
    578  1.15   thorpej 	db_addr_t off;
    579  1.15   thorpej 	db_strategy_t strategy;
    580  1.15   thorpej 	db_expr_t *diffp;
    581  1.15   thorpej {
    582  1.15   thorpej 
    583  1.15   thorpej 	if (db_symformat != NULL)
    584  1.15   thorpej 		return ((*db_symformat->sym_search)(stab, off, strategy,
    585  1.15   thorpej 		    diffp));
    586  1.15   thorpej 	return ((db_sym_t)0);
    587  1.15   thorpej }
    588  1.15   thorpej 
    589  1.15   thorpej void
    590  1.15   thorpej X_db_symbol_values(stab, sym, namep, valuep)
    591  1.15   thorpej 	db_symtab_t *stab;
    592  1.15   thorpej 	db_sym_t sym;
    593  1.15   thorpej 	char **namep;
    594  1.15   thorpej 	db_expr_t *valuep;
    595  1.15   thorpej {
    596  1.15   thorpej 
    597  1.15   thorpej 	if (db_symformat != NULL)
    598  1.15   thorpej 		(*db_symformat->sym_value)(stab, sym, namep, valuep);
    599  1.15   thorpej }
    600  1.15   thorpej 
    601  1.15   thorpej boolean_t
    602  1.15   thorpej X_db_line_at_pc(stab, cursym, filename, linenum, off)
    603  1.15   thorpej 	db_symtab_t *stab;
    604  1.15   thorpej 	db_sym_t cursym;
    605  1.15   thorpej 	char **filename;
    606  1.15   thorpej 	int *linenum;
    607  1.15   thorpej 	db_expr_t off;
    608  1.15   thorpej {
    609  1.15   thorpej 
    610  1.15   thorpej 	if (db_symformat != NULL)
    611  1.15   thorpej 		return ((*db_symformat->sym_line_at_pc)(stab, cursym,
    612  1.15   thorpej 		    filename, linenum, off));
    613  1.15   thorpej 	return (FALSE);
    614  1.15   thorpej }
    615  1.15   thorpej 
    616  1.15   thorpej boolean_t
    617  1.15   thorpej X_db_sym_numargs(stab, cursym, nargp, argnamep)
    618  1.15   thorpej 	db_symtab_t *stab;
    619  1.15   thorpej 	db_sym_t cursym;
    620  1.15   thorpej 	int *nargp;
    621  1.15   thorpej 	char **argnamep;
    622  1.15   thorpej {
    623  1.15   thorpej 
    624  1.15   thorpej 	if (db_symformat != NULL)
    625  1.15   thorpej 		return ((*db_symformat->sym_numargs)(stab, cursym, nargp,
    626  1.15   thorpej 		    argnamep));
    627  1.15   thorpej 	return (FALSE);
    628  1.20     jhawk }
    629  1.20     jhawk 
    630  1.20     jhawk void
    631  1.20     jhawk X_db_forall(stab, db_forall_func, arg)
    632  1.20     jhawk 	db_symtab_t	*stab;
    633  1.20     jhawk 	db_forall_func_t db_forall_func;
    634  1.20     jhawk 	void *arg;
    635  1.20     jhawk {
    636  1.20     jhawk 	if (db_symformat != NULL)
    637  1.20     jhawk 		(*db_symformat->sym_forall)(stab, db_forall_func, arg);
    638   1.1       cgd }
    639