db_sym.c revision 159668
1314564Sdim/*-
2314564Sdim * Mach Operating System
3254721Semaste * Copyright (c) 1991,1990 Carnegie Mellon University
4353358Sdim * All Rights Reserved.
5353358Sdim *
6353358Sdim * Permission to use, copy, modify and distribute this software and its
7254721Semaste * documentation is hereby granted, provided that both the copyright
8254721Semaste * notice and this permission notice appear in all copies of the
9254721Semaste * software, derivative works or modified versions, and any portions
10254721Semaste * thereof, and that both notices appear in supporting documentation.
11254721Semaste *
12254721Semaste * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13254721Semaste * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14254721Semaste * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15254721Semaste *
16314564Sdim * Carnegie Mellon requests users of this software to return to
17314564Sdim *
18314564Sdim *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19280031Sdim *  School of Computer Science
20314564Sdim *  Carnegie Mellon University
21296417Sdim *  Pittsburgh PA 15213-3890
22314564Sdim *
23296417Sdim * any improvements or extensions that they make and grant Carnegie the
24314564Sdim * rights to redistribute these changes.
25254721Semaste */
26353358Sdim/*
27353358Sdim * 	Author: David B. Golub, Carnegie Mellon University
28314564Sdim *	Date:	7/90
29314564Sdim */
30314564Sdim
31314564Sdim#include <sys/cdefs.h>
32314564Sdim__FBSDID("$FreeBSD: head/sys/ddb/db_sym.c 159668 2006-06-16 16:17:52Z kib $");
33314564Sdim
34314564Sdim#include <sys/param.h>
35314564Sdim#include <sys/systm.h>
36314564Sdim
37314564Sdim#include <ddb/ddb.h>
38314564Sdim#include <ddb/db_sym.h>
39314564Sdim
40314564Sdim#include <opt_ddb.h>
41314564Sdim
42314564Sdim/*
43314564Sdim * Multiple symbol tables
44314564Sdim */
45314564Sdim#ifndef MAXNOSYMTABS
46314564Sdim#define	MAXNOSYMTABS	3	/* mach, ux, emulator */
47314564Sdim#endif
48314564Sdim
49314564Sdimstatic db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
50314564Sdimstatic int db_nsymtab = 0;
51314564Sdim
52314564Sdimstatic db_symtab_t	*db_last_symtab; /* where last symbol was found */
53314564Sdim
54353358Sdimstatic c_db_sym_t	db_lookup( const char *symstr);
55314564Sdimstatic char		*db_qualify(c_db_sym_t sym, char *symtabname);
56314564Sdimstatic boolean_t	db_symbol_is_ambiguous(c_db_sym_t sym);
57314564Sdimstatic boolean_t	db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
58314564Sdim
59314564Sdim/*
60314564Sdim * Add symbol table, with given name, to list of symbol tables.
61314564Sdim */
62314564Sdimvoid
63314564Sdimdb_add_symbol_table(start, end, name, ref)
64314564Sdim	char *start;
65314564Sdim	char *end;
66314564Sdim	char *name;
67314564Sdim	char *ref;
68314564Sdim{
69314564Sdim	if (db_nsymtab >= MAXNOSYMTABS) {
70314564Sdim		printf ("No slots left for %s symbol table", name);
71314564Sdim		panic ("db_sym.c: db_add_symbol_table");
72314564Sdim	}
73314564Sdim
74314564Sdim	db_symtabs[db_nsymtab].start = start;
75314564Sdim	db_symtabs[db_nsymtab].end = end;
76314564Sdim	db_symtabs[db_nsymtab].name = name;
77314564Sdim	db_symtabs[db_nsymtab].private = ref;
78314564Sdim	db_nsymtab++;
79314564Sdim}
80314564Sdim
81314564Sdim/*
82314564Sdim *  db_qualify("vm_map", "ux") returns "unix:vm_map".
83314564Sdim *
84353358Sdim *  Note: return value points to static data whose content is
85353358Sdim *  overwritten by each call... but in practice this seems okay.
86314564Sdim */
87314564Sdimstatic char *
88314564Sdimdb_qualify(sym, symtabname)
89314564Sdim	c_db_sym_t	sym;
90314564Sdim	register char	*symtabname;
91314564Sdim{
92314564Sdim	const char	*symname;
93314564Sdim	static char     tmp[256];
94314564Sdim
95314564Sdim	db_symbol_values(sym, &symname, 0);
96314564Sdim	snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
97314564Sdim	return tmp;
98314564Sdim}
99314564Sdim
100314564Sdim
101314564Sdimboolean_t
102314564Sdimdb_eqname(src, dst, c)
103314564Sdim	const char *src;
104314564Sdim	const char *dst;
105314564Sdim	int c;
106314564Sdim{
107314564Sdim	if (!strcmp(src, dst))
108314564Sdim	    return (TRUE);
109314564Sdim	if (src[0] == c)
110314564Sdim	    return (!strcmp(src+1,dst));
111314564Sdim	return (FALSE);
112314564Sdim}
113314564Sdim
114314564Sdimboolean_t
115314564Sdimdb_value_of_name(name, valuep)
116314564Sdim	const char	*name;
117314564Sdim	db_expr_t	*valuep;
118314564Sdim{
119314564Sdim	c_db_sym_t	sym;
120314564Sdim
121314564Sdim	sym = db_lookup(name);
122314564Sdim	if (sym == C_DB_SYM_NULL)
123314564Sdim	    return (FALSE);
124314564Sdim	db_symbol_values(sym, &name, valuep);
125314564Sdim	return (TRUE);
126314564Sdim}
127314564Sdim
128314564Sdim
129314564Sdim/*
130314564Sdim * Lookup a symbol.
131314564Sdim * If the symbol has a qualifier (e.g., ux:vm_map),
132314564Sdim * then only the specified symbol table will be searched;
133314564Sdim * otherwise, all symbol tables will be searched.
134314564Sdim */
135314564Sdimstatic c_db_sym_t
136314564Sdimdb_lookup(symstr)
137254721Semaste	const char *symstr;
138254721Semaste{
139254721Semaste	c_db_sym_t sp;
140	register int i;
141	int symtab_start = 0;
142	int symtab_end = db_nsymtab;
143	register const char *cp;
144
145	/*
146	 * Look for, remove, and remember any symbol table specifier.
147	 */
148	for (cp = symstr; *cp; cp++) {
149		if (*cp == ':') {
150			for (i = 0; i < db_nsymtab; i++) {
151				int n = strlen(db_symtabs[i].name);
152
153				if (
154				    n == (cp - symstr) &&
155				    strncmp(symstr, db_symtabs[i].name, n) == 0
156				) {
157					symtab_start = i;
158					symtab_end = i + 1;
159					break;
160				}
161			}
162			if (i == db_nsymtab) {
163				db_error("invalid symbol table name");
164			}
165			symstr = cp+1;
166		}
167	}
168
169	/*
170	 * Look in the specified set of symbol tables.
171	 * Return on first match.
172	 */
173	for (i = symtab_start; i < symtab_end; i++) {
174		sp = X_db_lookup(&db_symtabs[i], symstr);
175		if (sp) {
176			db_last_symtab = &db_symtabs[i];
177			return sp;
178		}
179	}
180	return 0;
181}
182
183/*
184 * If TRUE, check across symbol tables for multiple occurrences
185 * of a name.  Might slow things down quite a bit.
186 */
187static volatile boolean_t db_qualify_ambiguous_names = FALSE;
188
189/*
190 * Does this symbol name appear in more than one symbol table?
191 * Used by db_symbol_values to decide whether to qualify a symbol.
192 */
193static boolean_t
194db_symbol_is_ambiguous(sym)
195	c_db_sym_t	sym;
196{
197	const char	*sym_name;
198	register int	i;
199	register
200	boolean_t	found_once = FALSE;
201
202	if (!db_qualify_ambiguous_names)
203		return FALSE;
204
205	db_symbol_values(sym, &sym_name, 0);
206	for (i = 0; i < db_nsymtab; i++) {
207		if (X_db_lookup(&db_symtabs[i], sym_name)) {
208			if (found_once)
209				return TRUE;
210			found_once = TRUE;
211		}
212	}
213	return FALSE;
214}
215
216/*
217 * Find the closest symbol to val, and return its name
218 * and the difference between val and the symbol found.
219 */
220c_db_sym_t
221db_search_symbol( val, strategy, offp)
222	register db_addr_t	val;
223	db_strategy_t		strategy;
224	db_expr_t		*offp;
225{
226	register
227	unsigned int	diff;
228	size_t		newdiff;
229	register int	i;
230	c_db_sym_t	ret = C_DB_SYM_NULL, sym;
231
232	newdiff = diff = ~0;
233	for (i = 0; i < db_nsymtab; i++) {
234	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
235	    if (newdiff < diff) {
236		db_last_symtab = &db_symtabs[i];
237		diff = newdiff;
238		ret = sym;
239	    }
240	}
241	*offp = diff;
242	return ret;
243}
244
245/*
246 * Return name and value of a symbol
247 */
248void
249db_symbol_values(sym, namep, valuep)
250	c_db_sym_t	sym;
251	const char	**namep;
252	db_expr_t	*valuep;
253{
254	db_expr_t	value;
255
256	if (sym == DB_SYM_NULL) {
257		*namep = 0;
258		return;
259	}
260
261	X_db_symbol_values(db_last_symtab, sym, namep, &value);
262
263	if (db_symbol_is_ambiguous(sym))
264		*namep = db_qualify(sym, db_last_symtab->name);
265	if (valuep)
266		*valuep = value;
267}
268
269
270/*
271 * Print a the closest symbol to value
272 *
273 * After matching the symbol according to the given strategy
274 * we print it in the name+offset format, provided the symbol's
275 * value is close enough (eg smaller than db_maxoff).
276 * We also attempt to print [filename:linenum] when applicable
277 * (eg for procedure names).
278 *
279 * If we could not find a reasonable name+offset representation,
280 * then we just print the value in hex.  Small values might get
281 * bogus symbol associations, e.g. 3 might get some absolute
282 * value like _INCLUDE_VERSION or something, therefore we do
283 * not accept symbols whose value is "small" (and use plain hex).
284 */
285
286db_expr_t	db_maxoff = 0x10000;
287
288void
289db_printsym(off, strategy)
290	db_expr_t	off;
291	db_strategy_t	strategy;
292{
293	db_expr_t	d;
294	char 		*filename;
295	const char	*name;
296	db_expr_t	value;
297	int 		linenum;
298	c_db_sym_t	cursym;
299
300	cursym = db_search_symbol(off, strategy, &d);
301	db_symbol_values(cursym, &name, &value);
302	if (name == 0)
303		value = off;
304	if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
305		db_printf("%+#lr", (long)off);
306		return;
307	}
308	if (name == 0 || d >= (unsigned long)db_maxoff) {
309		db_printf("%#lr", (unsigned long)off);
310		return;
311	}
312#ifdef DDB_NUMSYM
313	db_printf("%#lr = %s", (unsigned long)off, name);
314#else
315	db_printf("%s", name);
316#endif
317	if (d)
318		db_printf("+%+#lr", (long)d);
319	if (strategy == DB_STGY_PROC) {
320		if (db_line_at_pc(cursym, &filename, &linenum, off))
321			db_printf(" [%s:%d]", filename, linenum);
322	}
323}
324
325static boolean_t
326db_line_at_pc( sym, filename, linenum, pc)
327	c_db_sym_t	sym;
328	char		**filename;
329	int		*linenum;
330	db_expr_t	pc;
331{
332	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
333}
334
335int
336db_sym_numargs(sym, nargp, argnames)
337	c_db_sym_t	sym;
338	int		*nargp;
339	char		**argnames;
340{
341	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
342}
343