xref: /freebsd/sys/ddb/db_sym.c (revision cd508278c11d046bf7ce79ca39f6aabeb8c77f36)
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>
35530c0060SRobert Watson #include <sys/pcpu.h>
36eddfbb76SRobert Watson #include <sys/smp.h>
37f540b106SGarrett Wollman #include <sys/systm.h>
38eddfbb76SRobert Watson 
39eddfbb76SRobert Watson #include <net/vnet.h>
405ccbc3ccSBruce Evans 
41f540b106SGarrett Wollman #include <ddb/ddb.h>
425b81b6b3SRodney W. Grimes #include <ddb/db_sym.h>
43eddfbb76SRobert Watson #include <ddb/db_variables.h>
445b81b6b3SRodney W. Grimes 
459aece96fSPoul-Henning Kamp #include <opt_ddb.h>
469aece96fSPoul-Henning Kamp 
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 
54f73a856dSPoul-Henning Kamp static db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
55f73a856dSPoul-Henning Kamp static int db_nsymtab = 0;
565b81b6b3SRodney W. Grimes 
5794e24bf0SBruce Evans static db_symtab_t	*db_last_symtab; /* where last symbol was found */
585b81b6b3SRodney W. Grimes 
5914e10f99SAlfred Perlstein static c_db_sym_t	db_lookup( const char *symstr);
6014e10f99SAlfred Perlstein static char		*db_qualify(c_db_sym_t sym, char *symtabname);
61*cd508278SPedro F. Giffuni static bool		db_symbol_is_ambiguous(c_db_sym_t sym);
62*cd508278SPedro F. Giffuni static bool		db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
635b81b6b3SRodney W. Grimes 
64eddfbb76SRobert Watson static int db_cpu = -1;
65eddfbb76SRobert Watson 
66eddfbb76SRobert Watson #ifdef VIMAGE
67eddfbb76SRobert Watson static void *db_vnet = NULL;
68eddfbb76SRobert Watson #endif
69eddfbb76SRobert Watson 
70eddfbb76SRobert Watson /*
71eddfbb76SRobert Watson  * Validate the CPU number used to interpret per-CPU variables so we can
72eddfbb76SRobert Watson  * avoid later confusion if an invalid CPU is requested.
73eddfbb76SRobert Watson  */
74eddfbb76SRobert Watson int
75eddfbb76SRobert Watson db_var_db_cpu(struct db_variable *vp, db_expr_t *valuep, int op)
76eddfbb76SRobert Watson {
77eddfbb76SRobert Watson 
78eddfbb76SRobert Watson 	switch (op) {
79eddfbb76SRobert Watson 	case DB_VAR_GET:
80eddfbb76SRobert Watson 		*valuep = db_cpu;
81eddfbb76SRobert Watson 		return (1);
82eddfbb76SRobert Watson 
83eddfbb76SRobert Watson 	case DB_VAR_SET:
84eddfbb76SRobert Watson 		if (*(int *)valuep < -1 && *(int *)valuep > mp_maxid) {
85eddfbb76SRobert Watson 			db_printf("Invalid value: %d", *(int*)valuep);
86eddfbb76SRobert Watson 			return (0);
87eddfbb76SRobert Watson 		}
88eddfbb76SRobert Watson 		db_cpu = *(int *)valuep;
89eddfbb76SRobert Watson 		return (1);
90eddfbb76SRobert Watson 
91eddfbb76SRobert Watson 	default:
92eddfbb76SRobert Watson 		db_printf("db_var_db_cpu: unknown operation\n");
93eddfbb76SRobert Watson 		return (0);
94eddfbb76SRobert Watson 	}
95eddfbb76SRobert Watson }
96eddfbb76SRobert Watson 
97eddfbb76SRobert Watson /*
98eddfbb76SRobert Watson  * Read-only variable reporting the current CPU, which is what we use when
99eddfbb76SRobert Watson  * db_cpu is set to -1.
100eddfbb76SRobert Watson  */
101eddfbb76SRobert Watson int
102eddfbb76SRobert Watson db_var_curcpu(struct db_variable *vp, db_expr_t *valuep, int op)
103eddfbb76SRobert Watson {
104eddfbb76SRobert Watson 
105eddfbb76SRobert Watson 	switch (op) {
106eddfbb76SRobert Watson 	case DB_VAR_GET:
107eddfbb76SRobert Watson 		*valuep = curcpu;
108eddfbb76SRobert Watson 		return (1);
109eddfbb76SRobert Watson 
110eddfbb76SRobert Watson 	case DB_VAR_SET:
111eddfbb76SRobert Watson 		db_printf("Read-only variable.\n");
112eddfbb76SRobert Watson 		return (0);
113eddfbb76SRobert Watson 
114eddfbb76SRobert Watson 	default:
115eddfbb76SRobert Watson 		db_printf("db_var_curcpu: unknown operation\n");
116eddfbb76SRobert Watson 		return (0);
117eddfbb76SRobert Watson 	}
118eddfbb76SRobert Watson }
119eddfbb76SRobert Watson 
120eddfbb76SRobert Watson #ifdef VIMAGE
121eddfbb76SRobert Watson /*
122eddfbb76SRobert Watson  * Validate the virtual network pointer used to interpret per-vnet global
123eddfbb76SRobert Watson  * variable expansion.  Right now we don't do much here, really we should
124eddfbb76SRobert Watson  * walk the global vnet list to check it's an OK pointer.
125eddfbb76SRobert Watson  */
126eddfbb76SRobert Watson int
127eddfbb76SRobert Watson db_var_db_vnet(struct db_variable *vp, db_expr_t *valuep, int op)
128eddfbb76SRobert Watson {
129eddfbb76SRobert Watson 
130eddfbb76SRobert Watson 	switch (op) {
131eddfbb76SRobert Watson 	case DB_VAR_GET:
132eddfbb76SRobert Watson 		*valuep = (db_expr_t)db_vnet;
133eddfbb76SRobert Watson 		return (1);
134eddfbb76SRobert Watson 
135eddfbb76SRobert Watson 	case DB_VAR_SET:
136eddfbb76SRobert Watson 		db_vnet = *(void **)valuep;
137eddfbb76SRobert Watson 		return (1);
138eddfbb76SRobert Watson 
139eddfbb76SRobert Watson 	default:
140eddfbb76SRobert Watson 		db_printf("db_var_db_vnet: unknown operation\n");
141eddfbb76SRobert Watson 		return (0);
142eddfbb76SRobert Watson 	}
143eddfbb76SRobert Watson }
144eddfbb76SRobert Watson 
145eddfbb76SRobert Watson /*
146eddfbb76SRobert Watson  * Read-only variable reporting the current vnet, which is what we use when
147eddfbb76SRobert Watson  * db_vnet is set to NULL.
148eddfbb76SRobert Watson  */
149eddfbb76SRobert Watson int
150eddfbb76SRobert Watson db_var_curvnet(struct db_variable *vp, db_expr_t *valuep, int op)
151eddfbb76SRobert Watson {
152eddfbb76SRobert Watson 
153eddfbb76SRobert Watson 	switch (op) {
154eddfbb76SRobert Watson 	case DB_VAR_GET:
155eddfbb76SRobert Watson 		*valuep = (db_expr_t)curvnet;
156eddfbb76SRobert Watson 		return (1);
157eddfbb76SRobert Watson 
158eddfbb76SRobert Watson 	case DB_VAR_SET:
159eddfbb76SRobert Watson 		db_printf("Read-only variable.\n");
160eddfbb76SRobert Watson 		return (0);
161eddfbb76SRobert Watson 
162eddfbb76SRobert Watson 	default:
163eddfbb76SRobert Watson 		db_printf("db_var_curcpu: unknown operation\n");
164eddfbb76SRobert Watson 		return (0);
165eddfbb76SRobert Watson 	}
166eddfbb76SRobert Watson }
167eddfbb76SRobert Watson #endif
168eddfbb76SRobert Watson 
1695b81b6b3SRodney W. Grimes /*
1705b81b6b3SRodney W. Grimes  * Add symbol table, with given name, to list of symbol tables.
1715b81b6b3SRodney W. Grimes  */
1725b81b6b3SRodney W. Grimes void
173a41dd031SPedro F. Giffuni db_add_symbol_table(char *start, char *end, char *name, char *ref)
1745b81b6b3SRodney W. Grimes {
1755b81b6b3SRodney W. Grimes 	if (db_nsymtab >= MAXNOSYMTABS) {
1765b81b6b3SRodney W. Grimes 		printf ("No slots left for %s symbol table", name);
1775b81b6b3SRodney W. Grimes 		panic ("db_sym.c: db_add_symbol_table");
1785b81b6b3SRodney W. Grimes 	}
1795b81b6b3SRodney W. Grimes 
1805b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].start = start;
1815b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].end = end;
1825b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].name = name;
1835b81b6b3SRodney W. Grimes 	db_symtabs[db_nsymtab].private = ref;
1845b81b6b3SRodney W. Grimes 	db_nsymtab++;
1855b81b6b3SRodney W. Grimes }
1865b81b6b3SRodney W. Grimes 
1875b81b6b3SRodney W. Grimes /*
1885b81b6b3SRodney W. Grimes  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
1895b81b6b3SRodney W. Grimes  *
1905b81b6b3SRodney W. Grimes  *  Note: return value points to static data whose content is
1915b81b6b3SRodney W. Grimes  *  overwritten by each call... but in practice this seems okay.
1925b81b6b3SRodney W. Grimes  */
1935b81b6b3SRodney W. Grimes static char *
194a41dd031SPedro F. Giffuni db_qualify(c_db_sym_t sym, char *symtabname)
1955b81b6b3SRodney W. Grimes {
196a1c1e16aSMatthew Dillon 	const char	*symname;
1975b81b6b3SRodney W. Grimes 	static char     tmp[256];
1985b81b6b3SRodney W. Grimes 
1995b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &symname, 0);
2002127f260SArchie Cobbs 	snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
2015b81b6b3SRodney W. Grimes 	return tmp;
2025b81b6b3SRodney W. Grimes }
2035b81b6b3SRodney W. Grimes 
2045b81b6b3SRodney W. Grimes 
205*cd508278SPedro F. Giffuni bool
206a41dd031SPedro F. Giffuni db_eqname(const char *src, const char *dst, int c)
2075b81b6b3SRodney W. Grimes {
2085b81b6b3SRodney W. Grimes 	if (!strcmp(src, dst))
2092b490bc7SPedro F. Giffuni 	    return (true);
2105b81b6b3SRodney W. Grimes 	if (src[0] == c)
2115b81b6b3SRodney W. Grimes 	    return (!strcmp(src+1,dst));
2122b490bc7SPedro F. Giffuni 	return (false);
2135b81b6b3SRodney W. Grimes }
2145b81b6b3SRodney W. Grimes 
215*cd508278SPedro F. Giffuni bool
216a41dd031SPedro F. Giffuni db_value_of_name(const char *name, db_expr_t *valuep)
2175b81b6b3SRodney W. Grimes {
218fe08c21aSMatthew Dillon 	c_db_sym_t	sym;
2195b81b6b3SRodney W. Grimes 
2205b81b6b3SRodney W. Grimes 	sym = db_lookup(name);
221fe08c21aSMatthew Dillon 	if (sym == C_DB_SYM_NULL)
2222b490bc7SPedro F. Giffuni 	    return (false);
2235b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &name, valuep);
2242b490bc7SPedro F. Giffuni 	return (true);
2255b81b6b3SRodney W. Grimes }
2265b81b6b3SRodney W. Grimes 
227*cd508278SPedro F. Giffuni bool
228a41dd031SPedro F. Giffuni db_value_of_name_pcpu(const char *name, db_expr_t *valuep)
229eddfbb76SRobert Watson {
230eddfbb76SRobert Watson 	static char     tmp[256];
231eddfbb76SRobert Watson 	db_expr_t	value;
232eddfbb76SRobert Watson 	c_db_sym_t	sym;
233eddfbb76SRobert Watson 	int		cpu;
234eddfbb76SRobert Watson 
235eddfbb76SRobert Watson 	if (db_cpu != -1)
236eddfbb76SRobert Watson 		cpu = db_cpu;
237eddfbb76SRobert Watson 	else
238eddfbb76SRobert Watson 		cpu = curcpu;
239eddfbb76SRobert Watson 	snprintf(tmp, sizeof(tmp), "pcpu_entry_%s", name);
240eddfbb76SRobert Watson 	sym = db_lookup(tmp);
241eddfbb76SRobert Watson 	if (sym == C_DB_SYM_NULL)
2422b490bc7SPedro F. Giffuni 		return (false);
243eddfbb76SRobert Watson 	db_symbol_values(sym, &name, &value);
244eddfbb76SRobert Watson 	if (value < DPCPU_START || value >= DPCPU_STOP)
2452b490bc7SPedro F. Giffuni 		return (false);
246eddfbb76SRobert Watson 	*valuep = (db_expr_t)((uintptr_t)value + dpcpu_off[cpu]);
2472b490bc7SPedro F. Giffuni 	return (true);
248eddfbb76SRobert Watson }
249eddfbb76SRobert Watson 
250*cd508278SPedro F. Giffuni bool
251a41dd031SPedro F. Giffuni db_value_of_name_vnet(const char *name, db_expr_t *valuep)
252eddfbb76SRobert Watson {
253eddfbb76SRobert Watson #ifdef VIMAGE
254eddfbb76SRobert Watson 	static char     tmp[256];
255eddfbb76SRobert Watson 	db_expr_t	value;
256eddfbb76SRobert Watson 	c_db_sym_t	sym;
257eddfbb76SRobert Watson 	struct vnet	*vnet;
258eddfbb76SRobert Watson 
259eddfbb76SRobert Watson 	if (db_vnet != NULL)
260eddfbb76SRobert Watson 		vnet = db_vnet;
261eddfbb76SRobert Watson 	else
262eddfbb76SRobert Watson 		vnet = curvnet;
263eddfbb76SRobert Watson 	snprintf(tmp, sizeof(tmp), "vnet_entry_%s", name);
264eddfbb76SRobert Watson 	sym = db_lookup(tmp);
265eddfbb76SRobert Watson 	if (sym == C_DB_SYM_NULL)
2662b490bc7SPedro F. Giffuni 		return (false);
267eddfbb76SRobert Watson 	db_symbol_values(sym, &name, &value);
268eddfbb76SRobert Watson 	if (value < VNET_START || value >= VNET_STOP)
2692b490bc7SPedro F. Giffuni 		return (false);
270eddfbb76SRobert Watson 	*valuep = (db_expr_t)((uintptr_t)value + vnet->vnet_data_base);
2712b490bc7SPedro F. Giffuni 	return (true);
272eddfbb76SRobert Watson #else
2732b490bc7SPedro F. Giffuni 	return (false);
274eddfbb76SRobert Watson #endif
275eddfbb76SRobert Watson }
2765b81b6b3SRodney W. Grimes 
2775b81b6b3SRodney W. Grimes /*
2785b81b6b3SRodney W. Grimes  * Lookup a symbol.
2795b81b6b3SRodney W. Grimes  * If the symbol has a qualifier (e.g., ux:vm_map),
2805b81b6b3SRodney W. Grimes  * then only the specified symbol table will be searched;
2815b81b6b3SRodney W. Grimes  * otherwise, all symbol tables will be searched.
2825b81b6b3SRodney W. Grimes  */
283fe08c21aSMatthew Dillon static c_db_sym_t
284a41dd031SPedro F. Giffuni db_lookup(const char *symstr)
2855b81b6b3SRodney W. Grimes {
286fe08c21aSMatthew Dillon 	c_db_sym_t sp;
2875b81b6b3SRodney W. Grimes 	register int i;
2885b81b6b3SRodney W. Grimes 	int symtab_start = 0;
2895b81b6b3SRodney W. Grimes 	int symtab_end = db_nsymtab;
290a1c1e16aSMatthew Dillon 	register const char *cp;
2915b81b6b3SRodney W. Grimes 
2925b81b6b3SRodney W. Grimes 	/*
2935b81b6b3SRodney W. Grimes 	 * Look for, remove, and remember any symbol table specifier.
2945b81b6b3SRodney W. Grimes 	 */
2955b81b6b3SRodney W. Grimes 	for (cp = symstr; *cp; cp++) {
2965b81b6b3SRodney W. Grimes 		if (*cp == ':') {
2975b81b6b3SRodney W. Grimes 			for (i = 0; i < db_nsymtab; i++) {
298a1c1e16aSMatthew Dillon 				int n = strlen(db_symtabs[i].name);
299a1c1e16aSMatthew Dillon 
300a1c1e16aSMatthew Dillon 				if (
301a1c1e16aSMatthew Dillon 				    n == (cp - symstr) &&
302a1c1e16aSMatthew Dillon 				    strncmp(symstr, db_symtabs[i].name, n) == 0
303a1c1e16aSMatthew Dillon 				) {
3045b81b6b3SRodney W. Grimes 					symtab_start = i;
3055b81b6b3SRodney W. Grimes 					symtab_end = i + 1;
3065b81b6b3SRodney W. Grimes 					break;
3075b81b6b3SRodney W. Grimes 				}
3085b81b6b3SRodney W. Grimes 			}
3095b81b6b3SRodney W. Grimes 			if (i == db_nsymtab) {
3105b81b6b3SRodney W. Grimes 				db_error("invalid symbol table name");
3115b81b6b3SRodney W. Grimes 			}
3125b81b6b3SRodney W. Grimes 			symstr = cp+1;
3135b81b6b3SRodney W. Grimes 		}
3145b81b6b3SRodney W. Grimes 	}
3155b81b6b3SRodney W. Grimes 
3165b81b6b3SRodney W. Grimes 	/*
3175b81b6b3SRodney W. Grimes 	 * Look in the specified set of symbol tables.
3185b81b6b3SRodney W. Grimes 	 * Return on first match.
3195b81b6b3SRodney W. Grimes 	 */
3205b81b6b3SRodney W. Grimes 	for (i = symtab_start; i < symtab_end; i++) {
321169cd910SPoul-Henning Kamp 		sp = X_db_lookup(&db_symtabs[i], symstr);
322169cd910SPoul-Henning Kamp 		if (sp) {
3235b81b6b3SRodney W. Grimes 			db_last_symtab = &db_symtabs[i];
3245b81b6b3SRodney W. Grimes 			return sp;
3255b81b6b3SRodney W. Grimes 		}
3265b81b6b3SRodney W. Grimes 	}
3275b81b6b3SRodney W. Grimes 	return 0;
3285b81b6b3SRodney W. Grimes }
3295b81b6b3SRodney W. Grimes 
3305b81b6b3SRodney W. Grimes /*
3312b490bc7SPedro F. Giffuni  * If true, check across symbol tables for multiple occurrences
33294e24bf0SBruce Evans  * of a name.  Might slow things down quite a bit.
33394e24bf0SBruce Evans  */
334*cd508278SPedro F. Giffuni static volatile bool db_qualify_ambiguous_names = false;
33594e24bf0SBruce Evans 
33694e24bf0SBruce Evans /*
3375b81b6b3SRodney W. Grimes  * Does this symbol name appear in more than one symbol table?
3385b81b6b3SRodney W. Grimes  * Used by db_symbol_values to decide whether to qualify a symbol.
3395b81b6b3SRodney W. Grimes  */
340*cd508278SPedro F. Giffuni static bool
341a41dd031SPedro F. Giffuni db_symbol_is_ambiguous(c_db_sym_t sym)
3425b81b6b3SRodney W. Grimes {
343a1c1e16aSMatthew Dillon 	const char	*sym_name;
3445b81b6b3SRodney W. Grimes 	register int	i;
345*cd508278SPedro F. Giffuni 	register bool	found_once = false;
3465b81b6b3SRodney W. Grimes 
3475b81b6b3SRodney W. Grimes 	if (!db_qualify_ambiguous_names)
3482b490bc7SPedro F. Giffuni 		return (false);
3495b81b6b3SRodney W. Grimes 
3505b81b6b3SRodney W. Grimes 	db_symbol_values(sym, &sym_name, 0);
3515b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
3525b81b6b3SRodney W. Grimes 		if (X_db_lookup(&db_symtabs[i], sym_name)) {
3535b81b6b3SRodney W. Grimes 			if (found_once)
354*cd508278SPedro F. Giffuni 				return (true);
3552b490bc7SPedro F. Giffuni 			found_once = true;
3565b81b6b3SRodney W. Grimes 		}
3575b81b6b3SRodney W. Grimes 	}
3582b490bc7SPedro F. Giffuni 	return (false);
3595b81b6b3SRodney W. Grimes }
3605b81b6b3SRodney W. Grimes 
3615b81b6b3SRodney W. Grimes /*
3625b81b6b3SRodney W. Grimes  * Find the closest symbol to val, and return its name
3635b81b6b3SRodney W. Grimes  * and the difference between val and the symbol found.
3645b81b6b3SRodney W. Grimes  */
365fe08c21aSMatthew Dillon c_db_sym_t
366a41dd031SPedro F. Giffuni db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
3675b81b6b3SRodney W. Grimes {
3685b81b6b3SRodney W. Grimes 	register
3695b81b6b3SRodney W. Grimes 	unsigned int	diff;
370ecbb00a2SDoug Rabson 	size_t		newdiff;
3715b81b6b3SRodney W. Grimes 	register int	i;
372fe08c21aSMatthew Dillon 	c_db_sym_t	ret = C_DB_SYM_NULL, sym;
3735b81b6b3SRodney W. Grimes 
3745b81b6b3SRodney W. Grimes 	newdiff = diff = ~0;
3755b81b6b3SRodney W. Grimes 	for (i = 0; i < db_nsymtab; i++) {
3765b81b6b3SRodney W. Grimes 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
3775b81b6b3SRodney W. Grimes 	    if (newdiff < diff) {
3785b81b6b3SRodney W. Grimes 		db_last_symtab = &db_symtabs[i];
3795b81b6b3SRodney W. Grimes 		diff = newdiff;
3805b81b6b3SRodney W. Grimes 		ret = sym;
3815b81b6b3SRodney W. Grimes 	    }
3825b81b6b3SRodney W. Grimes 	}
3835b81b6b3SRodney W. Grimes 	*offp = diff;
3845b81b6b3SRodney W. Grimes 	return ret;
3855b81b6b3SRodney W. Grimes }
3865b81b6b3SRodney W. Grimes 
3875b81b6b3SRodney W. Grimes /*
3885b81b6b3SRodney W. Grimes  * Return name and value of a symbol
3895b81b6b3SRodney W. Grimes  */
3905b81b6b3SRodney W. Grimes void
391a41dd031SPedro F. Giffuni db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
3925b81b6b3SRodney W. Grimes {
3935b81b6b3SRodney W. Grimes 	db_expr_t	value;
3945b81b6b3SRodney W. Grimes 
3955b81b6b3SRodney W. Grimes 	if (sym == DB_SYM_NULL) {
3965b81b6b3SRodney W. Grimes 		*namep = 0;
3975b81b6b3SRodney W. Grimes 		return;
3985b81b6b3SRodney W. Grimes 	}
3995b81b6b3SRodney W. Grimes 
4006edf3d91SDoug Rabson 	X_db_symbol_values(db_last_symtab, sym, namep, &value);
4015b81b6b3SRodney W. Grimes 
4025b81b6b3SRodney W. Grimes 	if (db_symbol_is_ambiguous(sym))
4035b81b6b3SRodney W. Grimes 		*namep = db_qualify(sym, db_last_symtab->name);
4045b81b6b3SRodney W. Grimes 	if (valuep)
4055b81b6b3SRodney W. Grimes 		*valuep = value;
4065b81b6b3SRodney W. Grimes }
4075b81b6b3SRodney W. Grimes 
4085b81b6b3SRodney W. Grimes 
4095b81b6b3SRodney W. Grimes /*
4105b81b6b3SRodney W. Grimes  * Print a the closest symbol to value
4115b81b6b3SRodney W. Grimes  *
4125b81b6b3SRodney W. Grimes  * After matching the symbol according to the given strategy
4135b81b6b3SRodney W. Grimes  * we print it in the name+offset format, provided the symbol's
4145b81b6b3SRodney W. Grimes  * value is close enough (eg smaller than db_maxoff).
4155b81b6b3SRodney W. Grimes  * We also attempt to print [filename:linenum] when applicable
4165b81b6b3SRodney W. Grimes  * (eg for procedure names).
4175b81b6b3SRodney W. Grimes  *
4185b81b6b3SRodney W. Grimes  * If we could not find a reasonable name+offset representation,
4195b81b6b3SRodney W. Grimes  * then we just print the value in hex.  Small values might get
4205b81b6b3SRodney W. Grimes  * bogus symbol associations, e.g. 3 might get some absolute
4215b81b6b3SRodney W. Grimes  * value like _INCLUDE_VERSION or something, therefore we do
4227d350e72SBruce Evans  * not accept symbols whose value is "small" (and use plain hex).
4235b81b6b3SRodney W. Grimes  */
4245b81b6b3SRodney W. Grimes 
4253da6ef3cSBruce Evans db_expr_t	db_maxoff = 0x10000;
4265b81b6b3SRodney W. Grimes 
4275b81b6b3SRodney W. Grimes void
428a41dd031SPedro F. Giffuni db_printsym(db_expr_t off, db_strategy_t strategy)
4295b81b6b3SRodney W. Grimes {
4305b81b6b3SRodney W. Grimes 	db_expr_t	d;
4315b81b6b3SRodney W. Grimes 	char 		*filename;
432a1c1e16aSMatthew Dillon 	const char	*name;
4335b81b6b3SRodney W. Grimes 	db_expr_t	value;
4345b81b6b3SRodney W. Grimes 	int 		linenum;
435fe08c21aSMatthew Dillon 	c_db_sym_t	cursym;
4365b81b6b3SRodney W. Grimes 
4375b81b6b3SRodney W. Grimes 	cursym = db_search_symbol(off, strategy, &d);
4385b81b6b3SRodney W. Grimes 	db_symbol_values(cursym, &name, &value);
4397d350e72SBruce Evans 	if (name == 0)
4407d350e72SBruce Evans 		value = off;
4417d350e72SBruce Evans 	if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
442596dfc04SBruce Evans 		db_printf("%+#lr", (long)off);
4437d350e72SBruce Evans 		return;
4447d350e72SBruce Evans 	}
4453da6ef3cSBruce Evans 	if (name == 0 || d >= (unsigned long)db_maxoff) {
446596dfc04SBruce Evans 		db_printf("%#lr", (unsigned long)off);
4475b81b6b3SRodney W. Grimes 		return;
4485b81b6b3SRodney W. Grimes 	}
4499aece96fSPoul-Henning Kamp #ifdef DDB_NUMSYM
4509aece96fSPoul-Henning Kamp 	db_printf("%#lr = %s", (unsigned long)off, name);
4519aece96fSPoul-Henning Kamp #else
4525b81b6b3SRodney W. Grimes 	db_printf("%s", name);
4539aece96fSPoul-Henning Kamp #endif
4545b81b6b3SRodney W. Grimes 	if (d)
4551c6989faSPeter Wemm 		db_printf("+%+#lr", (long)d);
4565b81b6b3SRodney W. Grimes 	if (strategy == DB_STGY_PROC) {
4575b81b6b3SRodney W. Grimes 		if (db_line_at_pc(cursym, &filename, &linenum, off))
4585b81b6b3SRodney W. Grimes 			db_printf(" [%s:%d]", filename, linenum);
4595b81b6b3SRodney W. Grimes 	}
4605b81b6b3SRodney W. Grimes }
4615b81b6b3SRodney W. Grimes 
462*cd508278SPedro F. Giffuni static bool
463a41dd031SPedro F. Giffuni db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
4645b81b6b3SRodney W. Grimes {
465*cd508278SPedro F. Giffuni 	return (X_db_line_at_pc(db_last_symtab, sym, filename, linenum, pc));
4665b81b6b3SRodney W. Grimes }
467f7d75744SDavid Greenman 
468*cd508278SPedro F. Giffuni bool
469a41dd031SPedro F. Giffuni db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
470f7d75744SDavid Greenman {
471*cd508278SPedro F. Giffuni 	return (X_db_sym_numargs(db_last_symtab, sym, nargp, argnames));
472f7d75744SDavid Greenman }
473