xref: /freebsd/sys/ddb/db_sym.c (revision a1c1e16ada2a9f4797e3a244543b59c35b6cb479)
15b81b6b3SRodney W. Grimes /*
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.
250edf66ecSRodney W. Grimes  *
26a1c1e16aSMatthew Dillon  *	$Id: db_sym.c,v 1.27 1998/12/04 22:54:43 archie Exp $
275b81b6b3SRodney W. Grimes  */
280edf66ecSRodney W. Grimes 
295b81b6b3SRodney W. Grimes /*
305b81b6b3SRodney W. Grimes  * 	Author: David B. Golub, Carnegie Mellon University
315b81b6b3SRodney W. Grimes  *	Date:	7/90
325b81b6b3SRodney W. Grimes  */
33f540b106SGarrett Wollman #include <sys/param.h>
34f540b106SGarrett Wollman #include <sys/systm.h>
355ccbc3ccSBruce Evans 
36f540b106SGarrett Wollman #include <ddb/ddb.h>
375b81b6b3SRodney W. Grimes #include <ddb/db_sym.h>
385b81b6b3SRodney W. Grimes 
395b81b6b3SRodney W. Grimes /*
405b81b6b3SRodney W. Grimes  * Multiple symbol tables
415b81b6b3SRodney W. Grimes  */
42f7d75744SDavid Greenman #ifndef MAXNOSYMTABS
435b81b6b3SRodney W. Grimes #define	MAXNOSYMTABS	3	/* mach, ux, emulator */
44f7d75744SDavid Greenman #endif
455b81b6b3SRodney W. Grimes 
46f73a856dSPoul-Henning Kamp static db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
47f73a856dSPoul-Henning Kamp static int db_nsymtab = 0;
485b81b6b3SRodney W. Grimes 
4994e24bf0SBruce Evans static db_symtab_t	*db_last_symtab; /* where last symbol was found */
505b81b6b3SRodney W. Grimes 
51a1c1e16aSMatthew Dillon static db_sym_t		db_lookup __P(( const char *symstr));
52058284fcSBruce Evans static char		*db_qualify __P((db_sym_t sym, char *symtabname));
53f73a856dSPoul-Henning Kamp static boolean_t	db_symbol_is_ambiguous __P((db_sym_t sym));
54f73a856dSPoul-Henning Kamp static boolean_t	db_line_at_pc __P((db_sym_t, char **, int *,
55f73a856dSPoul-Henning Kamp 				db_expr_t));
565b81b6b3SRodney W. Grimes 
575b81b6b3SRodney W. Grimes /*
585b81b6b3SRodney W. Grimes  * Add symbol table, with given name, to list of symbol tables.
595b81b6b3SRodney W. Grimes  */
605b81b6b3SRodney W. Grimes void
615b81b6b3SRodney W. Grimes db_add_symbol_table(start, end, name, ref)
625b81b6b3SRodney W. Grimes 	char *start;
635b81b6b3SRodney W. Grimes 	char *end;
645b81b6b3SRodney W. Grimes 	char *name;
655b81b6b3SRodney W. Grimes 	char *ref;
665b81b6b3SRodney W. Grimes {
675b81b6b3SRodney W. Grimes 	if (db_nsymtab >= MAXNOSYMTABS) {
685b81b6b3SRodney W. Grimes 		printf ("No slots left for %s symbol table", name);
695b81b6b3SRodney W. Grimes 		panic ("db_sym.c: db_add_symbol_table");
705b81b6b3SRodney W. Grimes 	}
715b81b6b3SRodney W. Grimes 
725b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].start = start;
735b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].end = end;
745b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].name = name;
755b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].private = ref;
765b81b6b3SRodney W. Grimes 	db_nsymtab++;
775b81b6b3SRodney W. Grimes }
785b81b6b3SRodney W. Grimes 
795b81b6b3SRodney W. Grimes /*
805b81b6b3SRodney W. Grimes  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
815b81b6b3SRodney W. Grimes  *
825b81b6b3SRodney W. Grimes  *  Note: return value points to static data whose content is
835b81b6b3SRodney W. Grimes  *  overwritten by each call... but in practice this seems okay.
845b81b6b3SRodney W. Grimes  */
855b81b6b3SRodney W. Grimes static char *
865b81b6b3SRodney W. Grimes db_qualify(sym, symtabname)
875b81b6b3SRodney W. Grimes 	db_sym_t	sym;
885b81b6b3SRodney W. Grimes 	register char	*symtabname;
895b81b6b3SRodney W. Grimes {
90a1c1e16aSMatthew Dillon 	const char	*symname;
915b81b6b3SRodney W. Grimes 	static char     tmp[256];
925b81b6b3SRodney W. Grimes 
935b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &symname, 0);
942127f260SArchie Cobbs 	snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
955b81b6b3SRodney W. Grimes 	return tmp;
965b81b6b3SRodney W. Grimes }
975b81b6b3SRodney W. Grimes 
985b81b6b3SRodney W. Grimes 
995b81b6b3SRodney W. Grimes boolean_t
1005b81b6b3SRodney W. Grimes db_eqname(src, dst, c)
1015b81b6b3SRodney W. Grimes 	char *src;
1025b81b6b3SRodney W. Grimes 	char *dst;
103b2b392c4SBruce Evans 	int c;
1045b81b6b3SRodney W. Grimes {
1055b81b6b3SRodney W. Grimes 	if (!strcmp(src, dst))
1065b81b6b3SRodney W. Grimes 	    return (TRUE);
1075b81b6b3SRodney W. Grimes 	if (src[0] == c)
1085b81b6b3SRodney W. Grimes 	    return (!strcmp(src+1,dst));
1095b81b6b3SRodney W. Grimes 	return (FALSE);
1105b81b6b3SRodney W. Grimes }
1115b81b6b3SRodney W. Grimes 
1125b81b6b3SRodney W. Grimes boolean_t
1135b81b6b3SRodney W. Grimes db_value_of_name(name, valuep)
114a1c1e16aSMatthew Dillon 	const char	*name;
1155b81b6b3SRodney W. Grimes 	db_expr_t	*valuep;
1165b81b6b3SRodney W. Grimes {
1175b81b6b3SRodney W. Grimes 	db_sym_t	sym;
1185b81b6b3SRodney W. Grimes 
1195b81b6b3SRodney W. Grimes 	sym = db_lookup(name);
1205b81b6b3SRodney W. Grimes 	if (sym == DB_SYM_NULL)
1215b81b6b3SRodney W. Grimes 	    return (FALSE);
1225b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &name, valuep);
1235b81b6b3SRodney W. Grimes 	return (TRUE);
1245b81b6b3SRodney W. Grimes }
1255b81b6b3SRodney W. Grimes 
1265b81b6b3SRodney W. Grimes 
1275b81b6b3SRodney W. Grimes /*
1285b81b6b3SRodney W. Grimes  * Lookup a symbol.
1295b81b6b3SRodney W. Grimes  * If the symbol has a qualifier (e.g., ux:vm_map),
1305b81b6b3SRodney W. Grimes  * then only the specified symbol table will be searched;
1315b81b6b3SRodney W. Grimes  * otherwise, all symbol tables will be searched.
1325b81b6b3SRodney W. Grimes  */
133f73a856dSPoul-Henning Kamp static db_sym_t
1345b81b6b3SRodney W. Grimes db_lookup(symstr)
135a1c1e16aSMatthew Dillon 	const char *symstr;
1365b81b6b3SRodney W. Grimes {
1375b81b6b3SRodney W. Grimes 	db_sym_t sp;
1385b81b6b3SRodney W. Grimes 	register int i;
1395b81b6b3SRodney W. Grimes 	int symtab_start = 0;
1405b81b6b3SRodney W. Grimes 	int symtab_end = db_nsymtab;
141a1c1e16aSMatthew Dillon 	register const char *cp;
1425b81b6b3SRodney W. Grimes 
1435b81b6b3SRodney W. Grimes 	/*
1445b81b6b3SRodney W. Grimes 	 * Look for, remove, and remember any symbol table specifier.
1455b81b6b3SRodney W. Grimes 	 */
1465b81b6b3SRodney W. Grimes 	for (cp = symstr; *cp; cp++) {
1475b81b6b3SRodney W. Grimes 		if (*cp == ':') {
1485b81b6b3SRodney W. Grimes 			for (i = 0; i < db_nsymtab; i++) {
149a1c1e16aSMatthew Dillon 				int n = strlen(db_symtabs[i].name);
150a1c1e16aSMatthew Dillon 
151a1c1e16aSMatthew Dillon 				if (
152a1c1e16aSMatthew Dillon 				    n == (cp - symstr) &&
153a1c1e16aSMatthew Dillon 				    strncmp(symstr, db_symtabs[i].name, n) == 0
154a1c1e16aSMatthew Dillon 				) {
1555b81b6b3SRodney W. Grimes 					symtab_start = i;
1565b81b6b3SRodney W. Grimes 					symtab_end = i + 1;
1575b81b6b3SRodney W. Grimes 					break;
1585b81b6b3SRodney W. Grimes 				}
1595b81b6b3SRodney W. Grimes 			}
1605b81b6b3SRodney W. Grimes 			if (i == db_nsymtab) {
1615b81b6b3SRodney W. Grimes 				db_error("invalid symbol table name");
1625b81b6b3SRodney W. Grimes 			}
1635b81b6b3SRodney W. Grimes 			symstr = cp+1;
1645b81b6b3SRodney W. Grimes 		}
1655b81b6b3SRodney W. Grimes 	}
1665b81b6b3SRodney W. Grimes 
1675b81b6b3SRodney W. Grimes 	/*
1685b81b6b3SRodney W. Grimes 	 * Look in the specified set of symbol tables.
1695b81b6b3SRodney W. Grimes 	 * Return on first match.
1705b81b6b3SRodney W. Grimes 	 */
1715b81b6b3SRodney W. Grimes 	for (i = symtab_start; i < symtab_end; i++) {
172169cd910SPoul-Henning Kamp 		sp = X_db_lookup(&db_symtabs[i], symstr);
173169cd910SPoul-Henning Kamp 		if (sp) {
1745b81b6b3SRodney W. Grimes 			db_last_symtab = &db_symtabs[i];
1755b81b6b3SRodney W. Grimes 			return sp;
1765b81b6b3SRodney W. Grimes 		}
1775b81b6b3SRodney W. Grimes 	}
1785b81b6b3SRodney W. Grimes 	return 0;
1795b81b6b3SRodney W. Grimes }
1805b81b6b3SRodney W. Grimes 
1815b81b6b3SRodney W. Grimes /*
18294e24bf0SBruce Evans  * If TRUE, check across symbol tables for multiple occurrences
18394e24bf0SBruce Evans  * of a name.  Might slow things down quite a bit.
18494e24bf0SBruce Evans  */
18594e24bf0SBruce Evans static volatile boolean_t db_qualify_ambiguous_names = FALSE;
18694e24bf0SBruce Evans 
18794e24bf0SBruce Evans /*
1885b81b6b3SRodney W. Grimes  * Does this symbol name appear in more than one symbol table?
1895b81b6b3SRodney W. Grimes  * Used by db_symbol_values to decide whether to qualify a symbol.
1905b81b6b3SRodney W. Grimes  */
191f73a856dSPoul-Henning Kamp static boolean_t
1925b81b6b3SRodney W. Grimes db_symbol_is_ambiguous(sym)
1935b81b6b3SRodney W. Grimes 	db_sym_t	sym;
1945b81b6b3SRodney W. Grimes {
195a1c1e16aSMatthew Dillon 	const char	*sym_name;
1965b81b6b3SRodney W. Grimes 	register int	i;
1975b81b6b3SRodney W. Grimes 	register
1985b81b6b3SRodney W. Grimes 	boolean_t	found_once = FALSE;
1995b81b6b3SRodney W. Grimes 
2005b81b6b3SRodney W. Grimes 	if (!db_qualify_ambiguous_names)
2015b81b6b3SRodney W. Grimes 		return FALSE;
2025b81b6b3SRodney W. Grimes 
2035b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &sym_name, 0);
2045b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
2055b81b6b3SRodney W. Grimes 		if (X_db_lookup(&db_symtabs[i], sym_name)) {
2065b81b6b3SRodney W. Grimes 			if (found_once)
2075b81b6b3SRodney W. Grimes 				return TRUE;
2085b81b6b3SRodney W. Grimes 			found_once = TRUE;
2095b81b6b3SRodney W. Grimes 		}
2105b81b6b3SRodney W. Grimes 	}
2115b81b6b3SRodney W. Grimes 	return FALSE;
2125b81b6b3SRodney W. Grimes }
2135b81b6b3SRodney W. Grimes 
2145b81b6b3SRodney W. Grimes /*
2155b81b6b3SRodney W. Grimes  * Find the closest symbol to val, and return its name
2165b81b6b3SRodney W. Grimes  * and the difference between val and the symbol found.
2175b81b6b3SRodney W. Grimes  */
2185b81b6b3SRodney W. Grimes db_sym_t
2195b81b6b3SRodney W. Grimes db_search_symbol( val, strategy, offp)
2205b81b6b3SRodney W. Grimes 	register db_addr_t	val;
2215b81b6b3SRodney W. Grimes 	db_strategy_t		strategy;
2225b81b6b3SRodney W. Grimes 	db_expr_t		*offp;
2235b81b6b3SRodney W. Grimes {
2245b81b6b3SRodney W. Grimes 	register
2255b81b6b3SRodney W. Grimes 	unsigned int	diff;
226ecbb00a2SDoug Rabson 	size_t		newdiff;
2275b81b6b3SRodney W. Grimes 	register int	i;
2285b81b6b3SRodney W. Grimes 	db_sym_t	ret = DB_SYM_NULL, sym;
2295b81b6b3SRodney W. Grimes 
2305b81b6b3SRodney W. Grimes 	newdiff = diff = ~0;
2315b81b6b3SRodney W. Grimes 	db_last_symtab = 0;
2325b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
2335b81b6b3SRodney W. Grimes 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
2345b81b6b3SRodney W. Grimes 	    if (newdiff < diff) {
2355b81b6b3SRodney W. Grimes 		db_last_symtab = &db_symtabs[i];
2365b81b6b3SRodney W. Grimes 		diff = newdiff;
2375b81b6b3SRodney W. Grimes 		ret = sym;
2385b81b6b3SRodney W. Grimes 	    }
2395b81b6b3SRodney W. Grimes 	}
2405b81b6b3SRodney W. Grimes 	*offp = diff;
2415b81b6b3SRodney W. Grimes 	return ret;
2425b81b6b3SRodney W. Grimes }
2435b81b6b3SRodney W. Grimes 
2445b81b6b3SRodney W. Grimes /*
2455b81b6b3SRodney W. Grimes  * Return name and value of a symbol
2465b81b6b3SRodney W. Grimes  */
2475b81b6b3SRodney W. Grimes void
2485b81b6b3SRodney W. Grimes db_symbol_values(sym, namep, valuep)
2495b81b6b3SRodney W. Grimes 	db_sym_t	sym;
250a1c1e16aSMatthew Dillon 	const char	**namep;
2515b81b6b3SRodney W. Grimes 	db_expr_t	*valuep;
2525b81b6b3SRodney W. Grimes {
2535b81b6b3SRodney W. Grimes 	db_expr_t	value;
2545b81b6b3SRodney W. Grimes 
2555b81b6b3SRodney W. Grimes 	if (sym == DB_SYM_NULL) {
2565b81b6b3SRodney W. Grimes 		*namep = 0;
2575b81b6b3SRodney W. Grimes 		return;
2585b81b6b3SRodney W. Grimes 	}
2595b81b6b3SRodney W. Grimes 
2606edf3d91SDoug Rabson 	X_db_symbol_values(db_last_symtab, sym, namep, &value);
2615b81b6b3SRodney W. Grimes 
2625b81b6b3SRodney W. Grimes 	if (db_symbol_is_ambiguous(sym))
2635b81b6b3SRodney W. Grimes 		*namep = db_qualify(sym, db_last_symtab->name);
2645b81b6b3SRodney W. Grimes 	if (valuep)
2655b81b6b3SRodney W. Grimes 		*valuep = value;
2665b81b6b3SRodney W. Grimes }
2675b81b6b3SRodney W. Grimes 
2685b81b6b3SRodney W. Grimes 
2695b81b6b3SRodney W. Grimes /*
2705b81b6b3SRodney W. Grimes  * Print a the closest symbol to value
2715b81b6b3SRodney W. Grimes  *
2725b81b6b3SRodney W. Grimes  * After matching the symbol according to the given strategy
2735b81b6b3SRodney W. Grimes  * we print it in the name+offset format, provided the symbol's
2745b81b6b3SRodney W. Grimes  * value is close enough (eg smaller than db_maxoff).
2755b81b6b3SRodney W. Grimes  * We also attempt to print [filename:linenum] when applicable
2765b81b6b3SRodney W. Grimes  * (eg for procedure names).
2775b81b6b3SRodney W. Grimes  *
2785b81b6b3SRodney W. Grimes  * If we could not find a reasonable name+offset representation,
2795b81b6b3SRodney W. Grimes  * then we just print the value in hex.  Small values might get
2805b81b6b3SRodney W. Grimes  * bogus symbol associations, e.g. 3 might get some absolute
2815b81b6b3SRodney W. Grimes  * value like _INCLUDE_VERSION or something, therefore we do
2827d350e72SBruce Evans  * not accept symbols whose value is "small" (and use plain hex).
2835b81b6b3SRodney W. Grimes  */
2845b81b6b3SRodney W. Grimes 
2853da6ef3cSBruce Evans db_expr_t	db_maxoff = 0x10000;
2865b81b6b3SRodney W. Grimes 
2875b81b6b3SRodney W. Grimes void
2885b81b6b3SRodney W. Grimes db_printsym(off, strategy)
2895b81b6b3SRodney W. Grimes 	db_expr_t	off;
2905b81b6b3SRodney W. Grimes 	db_strategy_t	strategy;
2915b81b6b3SRodney W. Grimes {
2925b81b6b3SRodney W. Grimes 	db_expr_t	d;
2935b81b6b3SRodney W. Grimes 	char 		*filename;
294a1c1e16aSMatthew Dillon 	const char	*name;
2955b81b6b3SRodney W. Grimes 	db_expr_t	value;
2965b81b6b3SRodney W. Grimes 	int 		linenum;
2975b81b6b3SRodney W. Grimes 	db_sym_t	cursym;
2985b81b6b3SRodney W. Grimes 
2995b81b6b3SRodney W. Grimes 	cursym = db_search_symbol(off, strategy, &d);
3005b81b6b3SRodney W. Grimes 	db_symbol_values(cursym, &name, &value);
3017d350e72SBruce Evans 	if (name == 0)
3027d350e72SBruce Evans 		value = off;
3037d350e72SBruce Evans 	if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
304596dfc04SBruce Evans 		db_printf("%+#lr", (long)off);
3057d350e72SBruce Evans 		return;
3067d350e72SBruce Evans 	}
3073da6ef3cSBruce Evans 	if (name == 0 || d >= (unsigned long)db_maxoff) {
308596dfc04SBruce Evans 		db_printf("%#lr", (unsigned long)off);
3095b81b6b3SRodney W. Grimes 		return;
3105b81b6b3SRodney W. Grimes 	}
3115b81b6b3SRodney W. Grimes 	db_printf("%s", name);
3125b81b6b3SRodney W. Grimes 	if (d)
313596dfc04SBruce Evans 		db_printf("+%+#r", d);
3145b81b6b3SRodney W. Grimes 	if (strategy == DB_STGY_PROC) {
3155b81b6b3SRodney W. Grimes 		if (db_line_at_pc(cursym, &filename, &linenum, off))
3165b81b6b3SRodney W. Grimes 			db_printf(" [%s:%d]", filename, linenum);
3175b81b6b3SRodney W. Grimes 	}
3185b81b6b3SRodney W. Grimes }
3195b81b6b3SRodney W. Grimes 
320f73a856dSPoul-Henning Kamp static boolean_t
3215b81b6b3SRodney W. Grimes db_line_at_pc( sym, filename, linenum, pc)
3227b42c960SDavid Greenman 	db_sym_t	sym;
3237b42c960SDavid Greenman 	char		**filename;
3247b42c960SDavid Greenman 	int		*linenum;
3257b42c960SDavid Greenman 	db_expr_t	pc;
3265b81b6b3SRodney W. Grimes {
3275b81b6b3SRodney W. Grimes 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
3285b81b6b3SRodney W. Grimes }
329f7d75744SDavid Greenman 
330f7d75744SDavid Greenman int
331f7d75744SDavid Greenman db_sym_numargs(sym, nargp, argnames)
332f7d75744SDavid Greenman 	db_sym_t	sym;
333f7d75744SDavid Greenman 	int		*nargp;
334f7d75744SDavid Greenman 	char		**argnames;
335f7d75744SDavid Greenman {
336f7d75744SDavid Greenman 	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
337f7d75744SDavid Greenman }
338