xref: /freebsd/sys/ddb/db_sym.c (revision f7d757443b29d74d51678eb5cb6acccfb0db6416)
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  *
26f7d75744SDavid Greenman  *	$Id: db_sym.c,v 1.3 1993/11/25 01:30:12 wollman 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  */
335b81b6b3SRodney W. Grimes #include "param.h"
34381fe1aaSGarrett Wollman #include "systm.h"
355b81b6b3SRodney W. Grimes #include "proc.h"
36381fe1aaSGarrett Wollman #include "ddb/ddb.h"
375b81b6b3SRodney W. Grimes #include <ddb/db_sym.h>
385b81b6b3SRodney W. Grimes 
395b81b6b3SRodney W. Grimes /*
405b81b6b3SRodney W. Grimes  * We import from the symbol-table dependent routines:
415b81b6b3SRodney W. Grimes  */
425b81b6b3SRodney W. Grimes extern db_sym_t	X_db_lookup();
435b81b6b3SRodney W. Grimes extern db_sym_t	X_db_search_symbol();
445b81b6b3SRodney W. Grimes extern boolean_t X_db_line_at_pc();
455b81b6b3SRodney W. Grimes extern void	X_db_symbol_values();
465b81b6b3SRodney W. Grimes 
475b81b6b3SRodney W. Grimes /*
485b81b6b3SRodney W. Grimes  * Multiple symbol tables
495b81b6b3SRodney W. Grimes  */
50f7d75744SDavid Greenman #ifndef MAXNOSYMTABS
515b81b6b3SRodney W. Grimes #define	MAXNOSYMTABS	3	/* mach, ux, emulator */
52f7d75744SDavid Greenman #endif
535b81b6b3SRodney W. Grimes 
545b81b6b3SRodney W. Grimes db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
555b81b6b3SRodney W. Grimes int db_nsymtab = 0;
565b81b6b3SRodney W. Grimes 
575b81b6b3SRodney W. Grimes db_symtab_t	*db_last_symtab;
585b81b6b3SRodney W. Grimes 
595b81b6b3SRodney W. Grimes db_sym_t	db_lookup();	/* forward */
605b81b6b3SRodney W. Grimes 
615b81b6b3SRodney W. Grimes /*
625b81b6b3SRodney W. Grimes  * Add symbol table, with given name, to list of symbol tables.
635b81b6b3SRodney W. Grimes  */
645b81b6b3SRodney W. Grimes void
655b81b6b3SRodney W. Grimes db_add_symbol_table(start, end, name, ref)
665b81b6b3SRodney W. Grimes 	char *start;
675b81b6b3SRodney W. Grimes 	char *end;
685b81b6b3SRodney W. Grimes 	char *name;
695b81b6b3SRodney W. Grimes 	char *ref;
705b81b6b3SRodney W. Grimes {
715b81b6b3SRodney W. Grimes 	if (db_nsymtab >= MAXNOSYMTABS) {
725b81b6b3SRodney W. Grimes 		printf ("No slots left for %s symbol table", name);
735b81b6b3SRodney W. Grimes 		panic ("db_sym.c: db_add_symbol_table");
745b81b6b3SRodney W. Grimes 	}
755b81b6b3SRodney W. Grimes 
765b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].start = start;
775b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].end = end;
785b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].name = name;
795b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].private = ref;
805b81b6b3SRodney W. Grimes 	db_nsymtab++;
815b81b6b3SRodney W. Grimes }
825b81b6b3SRodney W. Grimes 
835b81b6b3SRodney W. Grimes /*
845b81b6b3SRodney W. Grimes  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
855b81b6b3SRodney W. Grimes  *
865b81b6b3SRodney W. Grimes  *  Note: return value points to static data whose content is
875b81b6b3SRodney W. Grimes  *  overwritten by each call... but in practice this seems okay.
885b81b6b3SRodney W. Grimes  */
895b81b6b3SRodney W. Grimes static char *
905b81b6b3SRodney W. Grimes db_qualify(sym, symtabname)
915b81b6b3SRodney W. Grimes 	db_sym_t	sym;
925b81b6b3SRodney W. Grimes 	register char	*symtabname;
935b81b6b3SRodney W. Grimes {
945b81b6b3SRodney W. Grimes 	char		*symname;
955b81b6b3SRodney W. Grimes 	static char     tmp[256];
965b81b6b3SRodney W. Grimes 	register char	*s;
975b81b6b3SRodney W. Grimes 
985b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &symname, 0);
995b81b6b3SRodney W. Grimes 	s = tmp;
1005b81b6b3SRodney W. Grimes 	while (*s++ = *symtabname++) {
1015b81b6b3SRodney W. Grimes 	}
1025b81b6b3SRodney W. Grimes 	s[-1] = ':';
1035b81b6b3SRodney W. Grimes 	while (*s++ = *symname++) {
1045b81b6b3SRodney W. Grimes 	}
1055b81b6b3SRodney W. Grimes 	return tmp;
1065b81b6b3SRodney W. Grimes }
1075b81b6b3SRodney W. Grimes 
1085b81b6b3SRodney W. Grimes 
1095b81b6b3SRodney W. Grimes boolean_t
1105b81b6b3SRodney W. Grimes db_eqname(src, dst, c)
1115b81b6b3SRodney W. Grimes 	char *src;
1125b81b6b3SRodney W. Grimes 	char *dst;
1135b81b6b3SRodney W. Grimes 	char c;
1145b81b6b3SRodney W. Grimes {
1155b81b6b3SRodney W. Grimes 	if (!strcmp(src, dst))
1165b81b6b3SRodney W. Grimes 	    return (TRUE);
1175b81b6b3SRodney W. Grimes 	if (src[0] == c)
1185b81b6b3SRodney W. Grimes 	    return (!strcmp(src+1,dst));
1195b81b6b3SRodney W. Grimes 	return (FALSE);
1205b81b6b3SRodney W. Grimes }
1215b81b6b3SRodney W. Grimes 
1225b81b6b3SRodney W. Grimes boolean_t
1235b81b6b3SRodney W. Grimes db_value_of_name(name, valuep)
1245b81b6b3SRodney W. Grimes 	char		*name;
1255b81b6b3SRodney W. Grimes 	db_expr_t	*valuep;
1265b81b6b3SRodney W. Grimes {
1275b81b6b3SRodney W. Grimes 	db_sym_t	sym;
1285b81b6b3SRodney W. Grimes 
1295b81b6b3SRodney W. Grimes 	sym = db_lookup(name);
1305b81b6b3SRodney W. Grimes 	if (sym == DB_SYM_NULL)
1315b81b6b3SRodney W. Grimes 	    return (FALSE);
1325b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &name, valuep);
1335b81b6b3SRodney W. Grimes 	return (TRUE);
1345b81b6b3SRodney W. Grimes }
1355b81b6b3SRodney W. Grimes 
1365b81b6b3SRodney W. Grimes 
1375b81b6b3SRodney W. Grimes /*
1385b81b6b3SRodney W. Grimes  * Lookup a symbol.
1395b81b6b3SRodney W. Grimes  * If the symbol has a qualifier (e.g., ux:vm_map),
1405b81b6b3SRodney W. Grimes  * then only the specified symbol table will be searched;
1415b81b6b3SRodney W. Grimes  * otherwise, all symbol tables will be searched.
1425b81b6b3SRodney W. Grimes  */
1435b81b6b3SRodney W. Grimes db_sym_t
1445b81b6b3SRodney W. Grimes db_lookup(symstr)
1455b81b6b3SRodney W. Grimes 	char *symstr;
1465b81b6b3SRodney W. Grimes {
1475b81b6b3SRodney W. Grimes 	db_sym_t sp;
1485b81b6b3SRodney W. Grimes 	register int i;
1495b81b6b3SRodney W. Grimes 	int symtab_start = 0;
1505b81b6b3SRodney W. Grimes 	int symtab_end = db_nsymtab;
1515b81b6b3SRodney W. Grimes 	register char *cp;
1525b81b6b3SRodney W. Grimes 
1535b81b6b3SRodney W. Grimes 	/*
1545b81b6b3SRodney W. Grimes 	 * Look for, remove, and remember any symbol table specifier.
1555b81b6b3SRodney W. Grimes 	 */
1565b81b6b3SRodney W. Grimes 	for (cp = symstr; *cp; cp++) {
1575b81b6b3SRodney W. Grimes 		if (*cp == ':') {
1585b81b6b3SRodney W. Grimes 			*cp = '\0';
1595b81b6b3SRodney W. Grimes 			for (i = 0; i < db_nsymtab; i++) {
1605b81b6b3SRodney W. Grimes 				if (! strcmp(symstr, db_symtabs[i].name)) {
1615b81b6b3SRodney W. Grimes 					symtab_start = i;
1625b81b6b3SRodney W. Grimes 					symtab_end = i + 1;
1635b81b6b3SRodney W. Grimes 					break;
1645b81b6b3SRodney W. Grimes 				}
1655b81b6b3SRodney W. Grimes 			}
1665b81b6b3SRodney W. Grimes 			*cp = ':';
1675b81b6b3SRodney W. Grimes 			if (i == db_nsymtab) {
1685b81b6b3SRodney W. Grimes 				db_error("invalid symbol table name");
1695b81b6b3SRodney W. Grimes 			}
1705b81b6b3SRodney W. Grimes 			symstr = cp+1;
1715b81b6b3SRodney W. Grimes 		}
1725b81b6b3SRodney W. Grimes 	}
1735b81b6b3SRodney W. Grimes 
1745b81b6b3SRodney W. Grimes 	/*
1755b81b6b3SRodney W. Grimes 	 * Look in the specified set of symbol tables.
1765b81b6b3SRodney W. Grimes 	 * Return on first match.
1775b81b6b3SRodney W. Grimes 	 */
1785b81b6b3SRodney W. Grimes 	for (i = symtab_start; i < symtab_end; i++) {
1795b81b6b3SRodney W. Grimes 		if (sp = X_db_lookup(&db_symtabs[i], symstr)) {
1805b81b6b3SRodney W. Grimes 			db_last_symtab = &db_symtabs[i];
1815b81b6b3SRodney W. Grimes 			return sp;
1825b81b6b3SRodney W. Grimes 		}
1835b81b6b3SRodney W. Grimes 	}
1845b81b6b3SRodney W. Grimes 	return 0;
1855b81b6b3SRodney W. Grimes }
1865b81b6b3SRodney W. Grimes 
1875b81b6b3SRodney W. Grimes /*
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  */
1915b81b6b3SRodney W. Grimes boolean_t db_qualify_ambiguous_names = FALSE;
1925b81b6b3SRodney W. Grimes 
1935b81b6b3SRodney W. Grimes boolean_t
1945b81b6b3SRodney W. Grimes db_symbol_is_ambiguous(sym)
1955b81b6b3SRodney W. Grimes 	db_sym_t	sym;
1965b81b6b3SRodney W. Grimes {
1975b81b6b3SRodney W. Grimes 	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  */
2205b81b6b3SRodney W. Grimes 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;
2285b81b6b3SRodney W. Grimes 	unsigned int	newdiff;
2295b81b6b3SRodney W. Grimes 	register int	i;
2305b81b6b3SRodney W. Grimes 	db_sym_t	ret = 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)
2515b81b6b3SRodney W. Grimes 	db_sym_t	sym;
2525b81b6b3SRodney W. Grimes 	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 
2625b81b6b3SRodney W. Grimes 	X_db_symbol_values(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
2845b81b6b3SRodney W. Grimes  * not accept symbols whose value is zero (and use plain hex).
2855b81b6b3SRodney W. Grimes  */
2865b81b6b3SRodney W. Grimes 
2875b81b6b3SRodney W. Grimes unsigned int	db_maxoff = 0x10000000;
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;
2965b81b6b3SRodney W. Grimes 	char		*name;
2975b81b6b3SRodney W. Grimes 	db_expr_t	value;
2985b81b6b3SRodney W. Grimes 	int 		linenum;
2995b81b6b3SRodney W. Grimes 	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);
3035b81b6b3SRodney W. Grimes 	if (name == 0 || d >= db_maxoff || value == 0) {
3045b81b6b3SRodney W. Grimes 		db_printf("%#n", off);
3055b81b6b3SRodney W. Grimes 		return;
3065b81b6b3SRodney W. Grimes 	}
3075b81b6b3SRodney W. Grimes 	db_printf("%s", name);
3085b81b6b3SRodney W. Grimes 	if (d)
3095b81b6b3SRodney W. Grimes 		db_printf("+%#r", d);
3105b81b6b3SRodney W. Grimes 	if (strategy == DB_STGY_PROC) {
3115b81b6b3SRodney W. Grimes 		if (db_line_at_pc(cursym, &filename, &linenum, off))
3125b81b6b3SRodney W. Grimes 			db_printf(" [%s:%d]", filename, linenum);
3135b81b6b3SRodney W. Grimes 	}
3145b81b6b3SRodney W. Grimes }
3155b81b6b3SRodney W. Grimes 
3165b81b6b3SRodney W. Grimes boolean_t
3175b81b6b3SRodney W. Grimes db_line_at_pc( sym, filename, linenum, pc)
318381fe1aaSGarrett Wollman 	int sym;
319381fe1aaSGarrett Wollman 	int filename;
320381fe1aaSGarrett Wollman 	int linenum;
321381fe1aaSGarrett Wollman 	int pc;
3225b81b6b3SRodney W. Grimes {
3235b81b6b3SRodney W. Grimes 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
3245b81b6b3SRodney W. Grimes }
325f7d75744SDavid Greenman 
326f7d75744SDavid Greenman int
327f7d75744SDavid Greenman db_sym_numargs(sym, nargp, argnames)
328f7d75744SDavid Greenman 	db_sym_t	sym;
329f7d75744SDavid Greenman 	int		*nargp;
330f7d75744SDavid Greenman 	char		**argnames;
331f7d75744SDavid Greenman {
332f7d75744SDavid Greenman 	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
333f7d75744SDavid Greenman }
334