xref: /freebsd/sys/ddb/db_sym.c (revision dd3cb56845afea8d0480ed47caf7e5e415ab19e6)
1dd3cb568SWarner Losh /*-
25b81b6b3SRodney W. Grimes  * Mach Operating System
35b81b6b3SRodney W. Grimes  * Copyright (c) 1991,1990 Carnegie Mellon University
45b81b6b3SRodney W. Grimes  * All Rights Reserved.
55b81b6b3SRodney W. Grimes  *
65b81b6b3SRodney W. Grimes  * Permission to use, copy, modify and distribute this software and its
75b81b6b3SRodney W. Grimes  * documentation is hereby granted, provided that both the copyright
85b81b6b3SRodney W. Grimes  * notice and this permission notice appear in all copies of the
95b81b6b3SRodney W. Grimes  * software, derivative works or modified versions, and any portions
105b81b6b3SRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
115b81b6b3SRodney W. Grimes  *
125b81b6b3SRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
135b81b6b3SRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
145b81b6b3SRodney W. Grimes  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
155b81b6b3SRodney W. Grimes  *
165b81b6b3SRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
175b81b6b3SRodney W. Grimes  *
185b81b6b3SRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
195b81b6b3SRodney W. Grimes  *  School of Computer Science
205b81b6b3SRodney W. Grimes  *  Carnegie Mellon University
215b81b6b3SRodney W. Grimes  *  Pittsburgh PA 15213-3890
225b81b6b3SRodney W. Grimes  *
235b81b6b3SRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
245b81b6b3SRodney W. Grimes  * rights to redistribute these changes.
255b81b6b3SRodney W. Grimes  */
265b81b6b3SRodney W. Grimes /*
275b81b6b3SRodney W. Grimes  * 	Author: David B. Golub, Carnegie Mellon University
285b81b6b3SRodney W. Grimes  *	Date:	7/90
295b81b6b3SRodney W. Grimes  */
30753960f7SDavid E. O'Brien 
31753960f7SDavid E. O'Brien #include <sys/cdefs.h>
32753960f7SDavid E. O'Brien __FBSDID("$FreeBSD$");
33753960f7SDavid E. O'Brien 
34f540b106SGarrett Wollman #include <sys/param.h>
35f540b106SGarrett Wollman #include <sys/systm.h>
365ccbc3ccSBruce Evans 
37f540b106SGarrett Wollman #include <ddb/ddb.h>
385b81b6b3SRodney W. Grimes #include <ddb/db_sym.h>
395b81b6b3SRodney W. Grimes 
409aece96fSPoul-Henning Kamp #include <opt_ddb.h>
419aece96fSPoul-Henning Kamp 
425b81b6b3SRodney W. Grimes /*
435b81b6b3SRodney W. Grimes  * Multiple symbol tables
445b81b6b3SRodney W. Grimes  */
45f7d75744SDavid Greenman #ifndef MAXNOSYMTABS
465b81b6b3SRodney W. Grimes #define	MAXNOSYMTABS	3	/* mach, ux, emulator */
47f7d75744SDavid Greenman #endif
485b81b6b3SRodney W. Grimes 
49f73a856dSPoul-Henning Kamp static db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
50f73a856dSPoul-Henning Kamp static int db_nsymtab = 0;
515b81b6b3SRodney W. Grimes 
5294e24bf0SBruce Evans static db_symtab_t	*db_last_symtab; /* where last symbol was found */
535b81b6b3SRodney W. Grimes 
5414e10f99SAlfred Perlstein static c_db_sym_t	db_lookup( const char *symstr);
5514e10f99SAlfred Perlstein static char		*db_qualify(c_db_sym_t sym, char *symtabname);
5614e10f99SAlfred Perlstein static boolean_t	db_symbol_is_ambiguous(c_db_sym_t sym);
5714e10f99SAlfred Perlstein static boolean_t	db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
585b81b6b3SRodney W. Grimes 
595b81b6b3SRodney W. Grimes /*
605b81b6b3SRodney W. Grimes  * Add symbol table, with given name, to list of symbol tables.
615b81b6b3SRodney W. Grimes  */
625b81b6b3SRodney W. Grimes void
635b81b6b3SRodney W. Grimes db_add_symbol_table(start, end, name, ref)
645b81b6b3SRodney W. Grimes 	char *start;
655b81b6b3SRodney W. Grimes 	char *end;
665b81b6b3SRodney W. Grimes 	char *name;
675b81b6b3SRodney W. Grimes 	char *ref;
685b81b6b3SRodney W. Grimes {
695b81b6b3SRodney W. Grimes 	if (db_nsymtab >= MAXNOSYMTABS) {
705b81b6b3SRodney W. Grimes 		printf ("No slots left for %s symbol table", name);
715b81b6b3SRodney W. Grimes 		panic ("db_sym.c: db_add_symbol_table");
725b81b6b3SRodney W. Grimes 	}
735b81b6b3SRodney W. Grimes 
745b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].start = start;
755b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].end = end;
765b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].name = name;
775b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].private = ref;
785b81b6b3SRodney W. Grimes 	db_nsymtab++;
795b81b6b3SRodney W. Grimes }
805b81b6b3SRodney W. Grimes 
815b81b6b3SRodney W. Grimes /*
825b81b6b3SRodney W. Grimes  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
835b81b6b3SRodney W. Grimes  *
845b81b6b3SRodney W. Grimes  *  Note: return value points to static data whose content is
855b81b6b3SRodney W. Grimes  *  overwritten by each call... but in practice this seems okay.
865b81b6b3SRodney W. Grimes  */
875b81b6b3SRodney W. Grimes static char *
885b81b6b3SRodney W. Grimes db_qualify(sym, symtabname)
89fe08c21aSMatthew Dillon 	c_db_sym_t	sym;
905b81b6b3SRodney W. Grimes 	register char	*symtabname;
915b81b6b3SRodney W. Grimes {
92a1c1e16aSMatthew Dillon 	const char	*symname;
935b81b6b3SRodney W. Grimes 	static char     tmp[256];
945b81b6b3SRodney W. Grimes 
955b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &symname, 0);
962127f260SArchie Cobbs 	snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
975b81b6b3SRodney W. Grimes 	return tmp;
985b81b6b3SRodney W. Grimes }
995b81b6b3SRodney W. Grimes 
1005b81b6b3SRodney W. Grimes 
1015b81b6b3SRodney W. Grimes boolean_t
1025b81b6b3SRodney W. Grimes db_eqname(src, dst, c)
1031e66367cSBruce Evans 	const char *src;
1041e66367cSBruce Evans 	const char *dst;
105b2b392c4SBruce Evans 	int c;
1065b81b6b3SRodney W. Grimes {
1075b81b6b3SRodney W. Grimes 	if (!strcmp(src, dst))
1085b81b6b3SRodney W. Grimes 	    return (TRUE);
1095b81b6b3SRodney W. Grimes 	if (src[0] == c)
1105b81b6b3SRodney W. Grimes 	    return (!strcmp(src+1,dst));
1115b81b6b3SRodney W. Grimes 	return (FALSE);
1125b81b6b3SRodney W. Grimes }
1135b81b6b3SRodney W. Grimes 
1145b81b6b3SRodney W. Grimes boolean_t
1155b81b6b3SRodney W. Grimes db_value_of_name(name, valuep)
116a1c1e16aSMatthew Dillon 	const char	*name;
1175b81b6b3SRodney W. Grimes 	db_expr_t	*valuep;
1185b81b6b3SRodney W. Grimes {
119fe08c21aSMatthew Dillon 	c_db_sym_t	sym;
1205b81b6b3SRodney W. Grimes 
1215b81b6b3SRodney W. Grimes 	sym = db_lookup(name);
122fe08c21aSMatthew Dillon 	if (sym == C_DB_SYM_NULL)
1235b81b6b3SRodney W. Grimes 	    return (FALSE);
1245b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &name, valuep);
1255b81b6b3SRodney W. Grimes 	return (TRUE);
1265b81b6b3SRodney W. Grimes }
1275b81b6b3SRodney W. Grimes 
1285b81b6b3SRodney W. Grimes 
1295b81b6b3SRodney W. Grimes /*
1305b81b6b3SRodney W. Grimes  * Lookup a symbol.
1315b81b6b3SRodney W. Grimes  * If the symbol has a qualifier (e.g., ux:vm_map),
1325b81b6b3SRodney W. Grimes  * then only the specified symbol table will be searched;
1335b81b6b3SRodney W. Grimes  * otherwise, all symbol tables will be searched.
1345b81b6b3SRodney W. Grimes  */
135fe08c21aSMatthew Dillon static c_db_sym_t
1365b81b6b3SRodney W. Grimes db_lookup(symstr)
137a1c1e16aSMatthew Dillon 	const char *symstr;
1385b81b6b3SRodney W. Grimes {
139fe08c21aSMatthew Dillon 	c_db_sym_t sp;
1405b81b6b3SRodney W. Grimes 	register int i;
1415b81b6b3SRodney W. Grimes 	int symtab_start = 0;
1425b81b6b3SRodney W. Grimes 	int symtab_end = db_nsymtab;
143a1c1e16aSMatthew Dillon 	register const char *cp;
1445b81b6b3SRodney W. Grimes 
1455b81b6b3SRodney W. Grimes 	/*
1465b81b6b3SRodney W. Grimes 	 * Look for, remove, and remember any symbol table specifier.
1475b81b6b3SRodney W. Grimes 	 */
1485b81b6b3SRodney W. Grimes 	for (cp = symstr; *cp; cp++) {
1495b81b6b3SRodney W. Grimes 		if (*cp == ':') {
1505b81b6b3SRodney W. Grimes 			for (i = 0; i < db_nsymtab; i++) {
151a1c1e16aSMatthew Dillon 				int n = strlen(db_symtabs[i].name);
152a1c1e16aSMatthew Dillon 
153a1c1e16aSMatthew Dillon 				if (
154a1c1e16aSMatthew Dillon 				    n == (cp - symstr) &&
155a1c1e16aSMatthew Dillon 				    strncmp(symstr, db_symtabs[i].name, n) == 0
156a1c1e16aSMatthew Dillon 				) {
1575b81b6b3SRodney W. Grimes 					symtab_start = i;
1585b81b6b3SRodney W. Grimes 					symtab_end = i + 1;
1595b81b6b3SRodney W. Grimes 					break;
1605b81b6b3SRodney W. Grimes 				}
1615b81b6b3SRodney W. Grimes 			}
1625b81b6b3SRodney W. Grimes 			if (i == db_nsymtab) {
1635b81b6b3SRodney W. Grimes 				db_error("invalid symbol table name");
1645b81b6b3SRodney W. Grimes 			}
1655b81b6b3SRodney W. Grimes 			symstr = cp+1;
1665b81b6b3SRodney W. Grimes 		}
1675b81b6b3SRodney W. Grimes 	}
1685b81b6b3SRodney W. Grimes 
1695b81b6b3SRodney W. Grimes 	/*
1705b81b6b3SRodney W. Grimes 	 * Look in the specified set of symbol tables.
1715b81b6b3SRodney W. Grimes 	 * Return on first match.
1725b81b6b3SRodney W. Grimes 	 */
1735b81b6b3SRodney W. Grimes 	for (i = symtab_start; i < symtab_end; i++) {
174169cd910SPoul-Henning Kamp 		sp = X_db_lookup(&db_symtabs[i], symstr);
175169cd910SPoul-Henning Kamp 		if (sp) {
1765b81b6b3SRodney W. Grimes 			db_last_symtab = &db_symtabs[i];
1775b81b6b3SRodney W. Grimes 			return sp;
1785b81b6b3SRodney W. Grimes 		}
1795b81b6b3SRodney W. Grimes 	}
1805b81b6b3SRodney W. Grimes 	return 0;
1815b81b6b3SRodney W. Grimes }
1825b81b6b3SRodney W. Grimes 
1835b81b6b3SRodney W. Grimes /*
18494e24bf0SBruce Evans  * If TRUE, check across symbol tables for multiple occurrences
18594e24bf0SBruce Evans  * of a name.  Might slow things down quite a bit.
18694e24bf0SBruce Evans  */
18794e24bf0SBruce Evans static volatile boolean_t db_qualify_ambiguous_names = FALSE;
18894e24bf0SBruce Evans 
18994e24bf0SBruce Evans /*
1905b81b6b3SRodney W. Grimes  * Does this symbol name appear in more than one symbol table?
1915b81b6b3SRodney W. Grimes  * Used by db_symbol_values to decide whether to qualify a symbol.
1925b81b6b3SRodney W. Grimes  */
193f73a856dSPoul-Henning Kamp static boolean_t
1945b81b6b3SRodney W. Grimes db_symbol_is_ambiguous(sym)
195fe08c21aSMatthew Dillon 	c_db_sym_t	sym;
1965b81b6b3SRodney W. Grimes {
197a1c1e16aSMatthew Dillon 	const char	*sym_name;
1985b81b6b3SRodney W. Grimes 	register int	i;
1995b81b6b3SRodney W. Grimes 	register
2005b81b6b3SRodney W. Grimes 	boolean_t	found_once = FALSE;
2015b81b6b3SRodney W. Grimes 
2025b81b6b3SRodney W. Grimes 	if (!db_qualify_ambiguous_names)
2035b81b6b3SRodney W. Grimes 		return FALSE;
2045b81b6b3SRodney W. Grimes 
2055b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &sym_name, 0);
2065b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
2075b81b6b3SRodney W. Grimes 		if (X_db_lookup(&db_symtabs[i], sym_name)) {
2085b81b6b3SRodney W. Grimes 			if (found_once)
2095b81b6b3SRodney W. Grimes 				return TRUE;
2105b81b6b3SRodney W. Grimes 			found_once = TRUE;
2115b81b6b3SRodney W. Grimes 		}
2125b81b6b3SRodney W. Grimes 	}
2135b81b6b3SRodney W. Grimes 	return FALSE;
2145b81b6b3SRodney W. Grimes }
2155b81b6b3SRodney W. Grimes 
2165b81b6b3SRodney W. Grimes /*
2175b81b6b3SRodney W. Grimes  * Find the closest symbol to val, and return its name
2185b81b6b3SRodney W. Grimes  * and the difference between val and the symbol found.
2195b81b6b3SRodney W. Grimes  */
220fe08c21aSMatthew Dillon c_db_sym_t
2215b81b6b3SRodney W. Grimes db_search_symbol( val, strategy, offp)
2225b81b6b3SRodney W. Grimes 	register db_addr_t	val;
2235b81b6b3SRodney W. Grimes 	db_strategy_t		strategy;
2245b81b6b3SRodney W. Grimes 	db_expr_t		*offp;
2255b81b6b3SRodney W. Grimes {
2265b81b6b3SRodney W. Grimes 	register
2275b81b6b3SRodney W. Grimes 	unsigned int	diff;
228ecbb00a2SDoug Rabson 	size_t		newdiff;
2295b81b6b3SRodney W. Grimes 	register int	i;
230fe08c21aSMatthew Dillon 	c_db_sym_t	ret = C_DB_SYM_NULL, sym;
2315b81b6b3SRodney W. Grimes 
2325b81b6b3SRodney W. Grimes 	newdiff = diff = ~0;
2335b81b6b3SRodney W. Grimes 	db_last_symtab = 0;
2345b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
2355b81b6b3SRodney W. Grimes 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
2365b81b6b3SRodney W. Grimes 	    if (newdiff < diff) {
2375b81b6b3SRodney W. Grimes 		db_last_symtab = &db_symtabs[i];
2385b81b6b3SRodney W. Grimes 		diff = newdiff;
2395b81b6b3SRodney W. Grimes 		ret = sym;
2405b81b6b3SRodney W. Grimes 	    }
2415b81b6b3SRodney W. Grimes 	}
2425b81b6b3SRodney W. Grimes 	*offp = diff;
2435b81b6b3SRodney W. Grimes 	return ret;
2445b81b6b3SRodney W. Grimes }
2455b81b6b3SRodney W. Grimes 
2465b81b6b3SRodney W. Grimes /*
2475b81b6b3SRodney W. Grimes  * Return name and value of a symbol
2485b81b6b3SRodney W. Grimes  */
2495b81b6b3SRodney W. Grimes void
2505b81b6b3SRodney W. Grimes db_symbol_values(sym, namep, valuep)
251fe08c21aSMatthew Dillon 	c_db_sym_t	sym;
252a1c1e16aSMatthew Dillon 	const char	**namep;
2535b81b6b3SRodney W. Grimes 	db_expr_t	*valuep;
2545b81b6b3SRodney W. Grimes {
2555b81b6b3SRodney W. Grimes 	db_expr_t	value;
2565b81b6b3SRodney W. Grimes 
2575b81b6b3SRodney W. Grimes 	if (sym == DB_SYM_NULL) {
2585b81b6b3SRodney W. Grimes 		*namep = 0;
2595b81b6b3SRodney W. Grimes 		return;
2605b81b6b3SRodney W. Grimes 	}
2615b81b6b3SRodney W. Grimes 
2626edf3d91SDoug Rabson 	X_db_symbol_values(db_last_symtab, sym, namep, &value);
2635b81b6b3SRodney W. Grimes 
2645b81b6b3SRodney W. Grimes 	if (db_symbol_is_ambiguous(sym))
2655b81b6b3SRodney W. Grimes 		*namep = db_qualify(sym, db_last_symtab->name);
2665b81b6b3SRodney W. Grimes 	if (valuep)
2675b81b6b3SRodney W. Grimes 		*valuep = value;
2685b81b6b3SRodney W. Grimes }
2695b81b6b3SRodney W. Grimes 
2705b81b6b3SRodney W. Grimes 
2715b81b6b3SRodney W. Grimes /*
2725b81b6b3SRodney W. Grimes  * Print a the closest symbol to value
2735b81b6b3SRodney W. Grimes  *
2745b81b6b3SRodney W. Grimes  * After matching the symbol according to the given strategy
2755b81b6b3SRodney W. Grimes  * we print it in the name+offset format, provided the symbol's
2765b81b6b3SRodney W. Grimes  * value is close enough (eg smaller than db_maxoff).
2775b81b6b3SRodney W. Grimes  * We also attempt to print [filename:linenum] when applicable
2785b81b6b3SRodney W. Grimes  * (eg for procedure names).
2795b81b6b3SRodney W. Grimes  *
2805b81b6b3SRodney W. Grimes  * If we could not find a reasonable name+offset representation,
2815b81b6b3SRodney W. Grimes  * then we just print the value in hex.  Small values might get
2825b81b6b3SRodney W. Grimes  * bogus symbol associations, e.g. 3 might get some absolute
2835b81b6b3SRodney W. Grimes  * value like _INCLUDE_VERSION or something, therefore we do
2847d350e72SBruce Evans  * not accept symbols whose value is "small" (and use plain hex).
2855b81b6b3SRodney W. Grimes  */
2865b81b6b3SRodney W. Grimes 
2873da6ef3cSBruce Evans db_expr_t	db_maxoff = 0x10000;
2885b81b6b3SRodney W. Grimes 
2895b81b6b3SRodney W. Grimes void
2905b81b6b3SRodney W. Grimes db_printsym(off, strategy)
2915b81b6b3SRodney W. Grimes 	db_expr_t	off;
2925b81b6b3SRodney W. Grimes 	db_strategy_t	strategy;
2935b81b6b3SRodney W. Grimes {
2945b81b6b3SRodney W. Grimes 	db_expr_t	d;
2955b81b6b3SRodney W. Grimes 	char 		*filename;
296a1c1e16aSMatthew Dillon 	const char	*name;
2975b81b6b3SRodney W. Grimes 	db_expr_t	value;
2985b81b6b3SRodney W. Grimes 	int 		linenum;
299fe08c21aSMatthew Dillon 	c_db_sym_t	cursym;
3005b81b6b3SRodney W. Grimes 
3015b81b6b3SRodney W. Grimes 	cursym = db_search_symbol(off, strategy, &d);
3025b81b6b3SRodney W. Grimes 	db_symbol_values(cursym, &name, &value);
3037d350e72SBruce Evans 	if (name == 0)
3047d350e72SBruce Evans 		value = off;
3057d350e72SBruce Evans 	if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
306596dfc04SBruce Evans 		db_printf("%+#lr", (long)off);
3077d350e72SBruce Evans 		return;
3087d350e72SBruce Evans 	}
3093da6ef3cSBruce Evans 	if (name == 0 || d >= (unsigned long)db_maxoff) {
310596dfc04SBruce Evans 		db_printf("%#lr", (unsigned long)off);
3115b81b6b3SRodney W. Grimes 		return;
3125b81b6b3SRodney W. Grimes 	}
3139aece96fSPoul-Henning Kamp #ifdef DDB_NUMSYM
3149aece96fSPoul-Henning Kamp 	db_printf("%#lr = %s", (unsigned long)off, name);
3159aece96fSPoul-Henning Kamp #else
3165b81b6b3SRodney W. Grimes 	db_printf("%s", name);
3179aece96fSPoul-Henning Kamp #endif
3185b81b6b3SRodney W. Grimes 	if (d)
3191c6989faSPeter Wemm 		db_printf("+%+#lr", (long)d);
3205b81b6b3SRodney W. Grimes 	if (strategy == DB_STGY_PROC) {
3215b81b6b3SRodney W. Grimes 		if (db_line_at_pc(cursym, &filename, &linenum, off))
3225b81b6b3SRodney W. Grimes 			db_printf(" [%s:%d]", filename, linenum);
3235b81b6b3SRodney W. Grimes 	}
3245b81b6b3SRodney W. Grimes }
3255b81b6b3SRodney W. Grimes 
326f73a856dSPoul-Henning Kamp static boolean_t
3275b81b6b3SRodney W. Grimes db_line_at_pc( sym, filename, linenum, pc)
328fe08c21aSMatthew Dillon 	c_db_sym_t	sym;
3297b42c960SDavid Greenman 	char		**filename;
3307b42c960SDavid Greenman 	int		*linenum;
3317b42c960SDavid Greenman 	db_expr_t	pc;
3325b81b6b3SRodney W. Grimes {
3335b81b6b3SRodney W. Grimes 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
3345b81b6b3SRodney W. Grimes }
335f7d75744SDavid Greenman 
336f7d75744SDavid Greenman int
337f7d75744SDavid Greenman db_sym_numargs(sym, nargp, argnames)
338fe08c21aSMatthew Dillon 	c_db_sym_t	sym;
339f7d75744SDavid Greenman 	int		*nargp;
340f7d75744SDavid Greenman 	char		**argnames;
341f7d75744SDavid Greenman {
342f7d75744SDavid Greenman 	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
343f7d75744SDavid Greenman }
344