xref: /freebsd/sys/ddb/db_sym.c (revision 94e24bf0f563054d7a4a8ed23a3a0e8c4d5059b9)
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  *
2694e24bf0SBruce Evans  *	$Id: db_sym.c,v 1.19 1997/06/30 23:49:16 bde 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 
51f73a856dSPoul-Henning Kamp static db_sym_t		db_lookup __P(( 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 {
905b81b6b3SRodney W. Grimes 	char		*symname;
915b81b6b3SRodney W. Grimes 	static char     tmp[256];
925b81b6b3SRodney W. Grimes 
935b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &symname, 0);
94169cd910SPoul-Henning Kamp 	strcpy(tmp,symtabname);
95169cd910SPoul-Henning Kamp 	strcat(tmp,":");
96169cd910SPoul-Henning Kamp 	strcat(tmp,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)
1035b81b6b3SRodney W. Grimes 	char *src;
1045b81b6b3SRodney W. Grimes 	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)
1165b81b6b3SRodney W. Grimes 	char		*name;
1175b81b6b3SRodney W. Grimes 	db_expr_t	*valuep;
1185b81b6b3SRodney W. Grimes {
1195b81b6b3SRodney W. Grimes 	db_sym_t	sym;
1205b81b6b3SRodney W. Grimes 
1215b81b6b3SRodney W. Grimes 	sym = db_lookup(name);
1225b81b6b3SRodney W. Grimes 	if (sym == 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  */
135f73a856dSPoul-Henning Kamp static db_sym_t
1365b81b6b3SRodney W. Grimes db_lookup(symstr)
1375b81b6b3SRodney W. Grimes 	char *symstr;
1385b81b6b3SRodney W. Grimes {
1395b81b6b3SRodney W. Grimes 	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;
1435b81b6b3SRodney W. Grimes 	register 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 			*cp = '\0';
1515b81b6b3SRodney W. Grimes 			for (i = 0; i < db_nsymtab; i++) {
1525b81b6b3SRodney W. Grimes 				if (! strcmp(symstr, db_symtabs[i].name)) {
1535b81b6b3SRodney W. Grimes 					symtab_start = i;
1545b81b6b3SRodney W. Grimes 					symtab_end = i + 1;
1555b81b6b3SRodney W. Grimes 					break;
1565b81b6b3SRodney W. Grimes 				}
1575b81b6b3SRodney W. Grimes 			}
1585b81b6b3SRodney W. Grimes 			*cp = ':';
1595b81b6b3SRodney W. Grimes 			if (i == db_nsymtab) {
1605b81b6b3SRodney W. Grimes 				db_error("invalid symbol table name");
1615b81b6b3SRodney W. Grimes 			}
1625b81b6b3SRodney W. Grimes 			symstr = cp+1;
1635b81b6b3SRodney W. Grimes 		}
1645b81b6b3SRodney W. Grimes 	}
1655b81b6b3SRodney W. Grimes 
1665b81b6b3SRodney W. Grimes 	/*
1675b81b6b3SRodney W. Grimes 	 * Look in the specified set of symbol tables.
1685b81b6b3SRodney W. Grimes 	 * Return on first match.
1695b81b6b3SRodney W. Grimes 	 */
1705b81b6b3SRodney W. Grimes 	for (i = symtab_start; i < symtab_end; i++) {
171169cd910SPoul-Henning Kamp 		sp = X_db_lookup(&db_symtabs[i], symstr);
172169cd910SPoul-Henning Kamp 		if (sp) {
1735b81b6b3SRodney W. Grimes 			db_last_symtab = &db_symtabs[i];
1745b81b6b3SRodney W. Grimes 			return sp;
1755b81b6b3SRodney W. Grimes 		}
1765b81b6b3SRodney W. Grimes 	}
1775b81b6b3SRodney W. Grimes 	return 0;
1785b81b6b3SRodney W. Grimes }
1795b81b6b3SRodney W. Grimes 
1805b81b6b3SRodney W. Grimes /*
18194e24bf0SBruce Evans  * If TRUE, check across symbol tables for multiple occurrences
18294e24bf0SBruce Evans  * of a name.  Might slow things down quite a bit.
18394e24bf0SBruce Evans  */
18494e24bf0SBruce Evans static volatile boolean_t db_qualify_ambiguous_names = FALSE;
18594e24bf0SBruce Evans 
18694e24bf0SBruce Evans /*
1875b81b6b3SRodney W. Grimes  * Does this symbol name appear in more than one symbol table?
1885b81b6b3SRodney W. Grimes  * Used by db_symbol_values to decide whether to qualify a symbol.
1895b81b6b3SRodney W. Grimes  */
190f73a856dSPoul-Henning Kamp static boolean_t
1915b81b6b3SRodney W. Grimes db_symbol_is_ambiguous(sym)
1925b81b6b3SRodney W. Grimes 	db_sym_t	sym;
1935b81b6b3SRodney W. Grimes {
1945b81b6b3SRodney W. Grimes 	char		*sym_name;
1955b81b6b3SRodney W. Grimes 	register int	i;
1965b81b6b3SRodney W. Grimes 	register
1975b81b6b3SRodney W. Grimes 	boolean_t	found_once = FALSE;
1985b81b6b3SRodney W. Grimes 
1995b81b6b3SRodney W. Grimes 	if (!db_qualify_ambiguous_names)
2005b81b6b3SRodney W. Grimes 		return FALSE;
2015b81b6b3SRodney W. Grimes 
2025b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &sym_name, 0);
2035b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
2045b81b6b3SRodney W. Grimes 		if (X_db_lookup(&db_symtabs[i], sym_name)) {
2055b81b6b3SRodney W. Grimes 			if (found_once)
2065b81b6b3SRodney W. Grimes 				return TRUE;
2075b81b6b3SRodney W. Grimes 			found_once = TRUE;
2085b81b6b3SRodney W. Grimes 		}
2095b81b6b3SRodney W. Grimes 	}
2105b81b6b3SRodney W. Grimes 	return FALSE;
2115b81b6b3SRodney W. Grimes }
2125b81b6b3SRodney W. Grimes 
2135b81b6b3SRodney W. Grimes /*
2145b81b6b3SRodney W. Grimes  * Find the closest symbol to val, and return its name
2155b81b6b3SRodney W. Grimes  * and the difference between val and the symbol found.
2165b81b6b3SRodney W. Grimes  */
2175b81b6b3SRodney W. Grimes db_sym_t
2185b81b6b3SRodney W. Grimes db_search_symbol( val, strategy, offp)
2195b81b6b3SRodney W. Grimes 	register db_addr_t	val;
2205b81b6b3SRodney W. Grimes 	db_strategy_t		strategy;
2215b81b6b3SRodney W. Grimes 	db_expr_t		*offp;
2225b81b6b3SRodney W. Grimes {
2235b81b6b3SRodney W. Grimes 	register
2245b81b6b3SRodney W. Grimes 	unsigned int	diff;
2255b81b6b3SRodney W. Grimes 	unsigned int	newdiff;
2265b81b6b3SRodney W. Grimes 	register int	i;
2275b81b6b3SRodney W. Grimes 	db_sym_t	ret = DB_SYM_NULL, sym;
2285b81b6b3SRodney W. Grimes 
2295b81b6b3SRodney W. Grimes 	newdiff = diff = ~0;
2305b81b6b3SRodney W. Grimes 	db_last_symtab = 0;
2315b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
2325b81b6b3SRodney W. Grimes 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
2335b81b6b3SRodney W. Grimes 	    if (newdiff < diff) {
2345b81b6b3SRodney W. Grimes 		db_last_symtab = &db_symtabs[i];
2355b81b6b3SRodney W. Grimes 		diff = newdiff;
2365b81b6b3SRodney W. Grimes 		ret = sym;
2375b81b6b3SRodney W. Grimes 	    }
2385b81b6b3SRodney W. Grimes 	}
2395b81b6b3SRodney W. Grimes 	*offp = diff;
2405b81b6b3SRodney W. Grimes 	return ret;
2415b81b6b3SRodney W. Grimes }
2425b81b6b3SRodney W. Grimes 
2435b81b6b3SRodney W. Grimes /*
2445b81b6b3SRodney W. Grimes  * Return name and value of a symbol
2455b81b6b3SRodney W. Grimes  */
2465b81b6b3SRodney W. Grimes void
2475b81b6b3SRodney W. Grimes db_symbol_values(sym, namep, valuep)
2485b81b6b3SRodney W. Grimes 	db_sym_t	sym;
2495b81b6b3SRodney W. Grimes 	char		**namep;
2505b81b6b3SRodney W. Grimes 	db_expr_t	*valuep;
2515b81b6b3SRodney W. Grimes {
2525b81b6b3SRodney W. Grimes 	db_expr_t	value;
2535b81b6b3SRodney W. Grimes 
2545b81b6b3SRodney W. Grimes 	if (sym == DB_SYM_NULL) {
2555b81b6b3SRodney W. Grimes 		*namep = 0;
2565b81b6b3SRodney W. Grimes 		return;
2575b81b6b3SRodney W. Grimes 	}
2585b81b6b3SRodney W. Grimes 
2595b81b6b3SRodney W. Grimes 	X_db_symbol_values(sym, namep, &value);
2605b81b6b3SRodney W. Grimes 
2615b81b6b3SRodney W. Grimes 	if (db_symbol_is_ambiguous(sym))
2625b81b6b3SRodney W. Grimes 		*namep = db_qualify(sym, db_last_symtab->name);
2635b81b6b3SRodney W. Grimes 	if (valuep)
2645b81b6b3SRodney W. Grimes 		*valuep = value;
2655b81b6b3SRodney W. Grimes }
2665b81b6b3SRodney W. Grimes 
2675b81b6b3SRodney W. Grimes 
2685b81b6b3SRodney W. Grimes /*
2695b81b6b3SRodney W. Grimes  * Print a the closest symbol to value
2705b81b6b3SRodney W. Grimes  *
2715b81b6b3SRodney W. Grimes  * After matching the symbol according to the given strategy
2725b81b6b3SRodney W. Grimes  * we print it in the name+offset format, provided the symbol's
2735b81b6b3SRodney W. Grimes  * value is close enough (eg smaller than db_maxoff).
2745b81b6b3SRodney W. Grimes  * We also attempt to print [filename:linenum] when applicable
2755b81b6b3SRodney W. Grimes  * (eg for procedure names).
2765b81b6b3SRodney W. Grimes  *
2775b81b6b3SRodney W. Grimes  * If we could not find a reasonable name+offset representation,
2785b81b6b3SRodney W. Grimes  * then we just print the value in hex.  Small values might get
2795b81b6b3SRodney W. Grimes  * bogus symbol associations, e.g. 3 might get some absolute
2805b81b6b3SRodney W. Grimes  * value like _INCLUDE_VERSION or something, therefore we do
2817d350e72SBruce Evans  * not accept symbols whose value is "small" (and use plain hex).
2825b81b6b3SRodney W. Grimes  */
2835b81b6b3SRodney W. Grimes 
284751b0b8eSDavid Greenman unsigned int	db_maxoff = 0x10000;
2855b81b6b3SRodney W. Grimes 
2865b81b6b3SRodney W. Grimes void
2875b81b6b3SRodney W. Grimes db_printsym(off, strategy)
2885b81b6b3SRodney W. Grimes 	db_expr_t	off;
2895b81b6b3SRodney W. Grimes 	db_strategy_t	strategy;
2905b81b6b3SRodney W. Grimes {
2915b81b6b3SRodney W. Grimes 	db_expr_t	d;
2925b81b6b3SRodney W. Grimes 	char 		*filename;
2935b81b6b3SRodney W. Grimes 	char		*name;
2945b81b6b3SRodney W. Grimes 	db_expr_t	value;
2955b81b6b3SRodney W. Grimes 	int 		linenum;
2965b81b6b3SRodney W. Grimes 	db_sym_t	cursym;
2975b81b6b3SRodney W. Grimes 
2985b81b6b3SRodney W. Grimes 	cursym = db_search_symbol(off, strategy, &d);
2995b81b6b3SRodney W. Grimes 	db_symbol_values(cursym, &name, &value);
3007d350e72SBruce Evans 	if (name == 0)
3017d350e72SBruce Evans 		value = off;
3027d350e72SBruce Evans 	if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
3037d350e72SBruce Evans 		db_printf("%+#n", off);
3047d350e72SBruce Evans 		return;
3057d350e72SBruce Evans 	}
3067d350e72SBruce Evans 	if (name == 0 || d >= db_maxoff) {
3075b81b6b3SRodney W. Grimes 		db_printf("%#n", off);
3085b81b6b3SRodney W. Grimes 		return;
3095b81b6b3SRodney W. Grimes 	}
3105b81b6b3SRodney W. Grimes 	db_printf("%s", name);
3115b81b6b3SRodney W. Grimes 	if (d)
312791d77e0SPoul-Henning Kamp 		db_printf("+%+#n", d);
3135b81b6b3SRodney W. Grimes 	if (strategy == DB_STGY_PROC) {
3145b81b6b3SRodney W. Grimes 		if (db_line_at_pc(cursym, &filename, &linenum, off))
3155b81b6b3SRodney W. Grimes 			db_printf(" [%s:%d]", filename, linenum);
3165b81b6b3SRodney W. Grimes 	}
3175b81b6b3SRodney W. Grimes }
3185b81b6b3SRodney W. Grimes 
319f73a856dSPoul-Henning Kamp static boolean_t
3205b81b6b3SRodney W. Grimes db_line_at_pc( sym, filename, linenum, pc)
3217b42c960SDavid Greenman 	db_sym_t	sym;
3227b42c960SDavid Greenman 	char		**filename;
3237b42c960SDavid Greenman 	int		*linenum;
3247b42c960SDavid Greenman 	db_expr_t	pc;
3255b81b6b3SRodney W. Grimes {
3265b81b6b3SRodney W. Grimes 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
3275b81b6b3SRodney W. Grimes }
328f7d75744SDavid Greenman 
329f7d75744SDavid Greenman int
330f7d75744SDavid Greenman db_sym_numargs(sym, nargp, argnames)
331f7d75744SDavid Greenman 	db_sym_t	sym;
332f7d75744SDavid Greenman 	int		*nargp;
333f7d75744SDavid Greenman 	char		**argnames;
334f7d75744SDavid Greenman {
335f7d75744SDavid Greenman 	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
336f7d75744SDavid Greenman }
337