xref: /freebsd/sys/ddb/db_sym.c (revision 17ee9d00bc1ae1e598c38f25826f861e4bc6c3ce)
1 /*
2  * Mach Operating System
3  * Copyright (c) 1991,1990 Carnegie Mellon University
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify and distribute this software and its
7  * documentation is hereby granted, provided that both the copyright
8  * notice and this permission notice appear in all copies of the
9  * software, derivative works or modified versions, and any portions
10  * thereof, and that both notices appear in supporting documentation.
11  *
12  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
13  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
14  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
15  *
16  * Carnegie Mellon requests users of this software to return to
17  *
18  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
19  *  School of Computer Science
20  *  Carnegie Mellon University
21  *  Pittsburgh PA 15213-3890
22  *
23  * any improvements or extensions that they make and grant Carnegie the
24  * rights to redistribute these changes.
25  *
26  *	$Id: db_sym.c,v 1.6 1994/08/20 03:48:26 davidg Exp $
27  */
28 
29 /*
30  * 	Author: David B. Golub, Carnegie Mellon University
31  *	Date:	7/90
32  */
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/proc.h>
36 #include <ddb/ddb.h>
37 #include <ddb/db_sym.h>
38 
39 /*
40  * We import from the symbol-table dependent routines:
41  */
42 extern db_sym_t	X_db_lookup();
43 extern db_sym_t	X_db_search_symbol();
44 extern boolean_t X_db_line_at_pc();
45 extern void	X_db_symbol_values();
46 
47 /*
48  * Multiple symbol tables
49  */
50 #ifndef MAXNOSYMTABS
51 #define	MAXNOSYMTABS	3	/* mach, ux, emulator */
52 #endif
53 
54 db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
55 int db_nsymtab = 0;
56 
57 db_symtab_t	*db_last_symtab;
58 
59 db_sym_t	db_lookup();	/* forward */
60 
61 /*
62  * Add symbol table, with given name, to list of symbol tables.
63  */
64 void
65 db_add_symbol_table(start, end, name, ref)
66 	char *start;
67 	char *end;
68 	char *name;
69 	char *ref;
70 {
71 	if (db_nsymtab >= MAXNOSYMTABS) {
72 		printf ("No slots left for %s symbol table", name);
73 		panic ("db_sym.c: db_add_symbol_table");
74 	}
75 
76 	db_symtabs[db_nsymtab].start = start;
77 	db_symtabs[db_nsymtab].end = end;
78 	db_symtabs[db_nsymtab].name = name;
79 	db_symtabs[db_nsymtab].private = ref;
80 	db_nsymtab++;
81 }
82 
83 /*
84  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
85  *
86  *  Note: return value points to static data whose content is
87  *  overwritten by each call... but in practice this seems okay.
88  */
89 static char *
90 db_qualify(sym, symtabname)
91 	db_sym_t	sym;
92 	register char	*symtabname;
93 {
94 	char		*symname;
95 	static char     tmp[256];
96 
97 	db_symbol_values(sym, &symname, 0);
98 	strcpy(tmp,symtabname);
99 	strcat(tmp,":");
100 	strcat(tmp,symname);
101 	return tmp;
102 }
103 
104 
105 boolean_t
106 db_eqname(src, dst, c)
107 	char *src;
108 	char *dst;
109 	char c;
110 {
111 	if (!strcmp(src, dst))
112 	    return (TRUE);
113 	if (src[0] == c)
114 	    return (!strcmp(src+1,dst));
115 	return (FALSE);
116 }
117 
118 boolean_t
119 db_value_of_name(name, valuep)
120 	char		*name;
121 	db_expr_t	*valuep;
122 {
123 	db_sym_t	sym;
124 
125 	sym = db_lookup(name);
126 	if (sym == DB_SYM_NULL)
127 	    return (FALSE);
128 	db_symbol_values(sym, &name, valuep);
129 	return (TRUE);
130 }
131 
132 
133 /*
134  * Lookup a symbol.
135  * If the symbol has a qualifier (e.g., ux:vm_map),
136  * then only the specified symbol table will be searched;
137  * otherwise, all symbol tables will be searched.
138  */
139 db_sym_t
140 db_lookup(symstr)
141 	char *symstr;
142 {
143 	db_sym_t sp;
144 	register int i;
145 	int symtab_start = 0;
146 	int symtab_end = db_nsymtab;
147 	register char *cp;
148 
149 	/*
150 	 * Look for, remove, and remember any symbol table specifier.
151 	 */
152 	for (cp = symstr; *cp; cp++) {
153 		if (*cp == ':') {
154 			*cp = '\0';
155 			for (i = 0; i < db_nsymtab; i++) {
156 				if (! strcmp(symstr, db_symtabs[i].name)) {
157 					symtab_start = i;
158 					symtab_end = i + 1;
159 					break;
160 				}
161 			}
162 			*cp = ':';
163 			if (i == db_nsymtab) {
164 				db_error("invalid symbol table name");
165 			}
166 			symstr = cp+1;
167 		}
168 	}
169 
170 	/*
171 	 * Look in the specified set of symbol tables.
172 	 * Return on first match.
173 	 */
174 	for (i = symtab_start; i < symtab_end; i++) {
175 		sp = X_db_lookup(&db_symtabs[i], symstr);
176 		if (sp) {
177 			db_last_symtab = &db_symtabs[i];
178 			return sp;
179 		}
180 	}
181 	return 0;
182 }
183 
184 /*
185  * Does this symbol name appear in more than one symbol table?
186  * Used by db_symbol_values to decide whether to qualify a symbol.
187  */
188 boolean_t db_qualify_ambiguous_names = FALSE;
189 
190 boolean_t
191 db_symbol_is_ambiguous(sym)
192 	db_sym_t	sym;
193 {
194 	char		*sym_name;
195 	register int	i;
196 	register
197 	boolean_t	found_once = FALSE;
198 
199 	if (!db_qualify_ambiguous_names)
200 		return FALSE;
201 
202 	db_symbol_values(sym, &sym_name, 0);
203 	for (i = 0; i < db_nsymtab; i++) {
204 		if (X_db_lookup(&db_symtabs[i], sym_name)) {
205 			if (found_once)
206 				return TRUE;
207 			found_once = TRUE;
208 		}
209 	}
210 	return FALSE;
211 }
212 
213 /*
214  * Find the closest symbol to val, and return its name
215  * and the difference between val and the symbol found.
216  */
217 db_sym_t
218 db_search_symbol( val, strategy, offp)
219 	register db_addr_t	val;
220 	db_strategy_t		strategy;
221 	db_expr_t		*offp;
222 {
223 	register
224 	unsigned int	diff;
225 	unsigned int	newdiff;
226 	register int	i;
227 	db_sym_t	ret = DB_SYM_NULL, sym;
228 
229 	newdiff = diff = ~0;
230 	db_last_symtab = 0;
231 	for (i = 0; i < db_nsymtab; i++) {
232 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
233 	    if (newdiff < diff) {
234 		db_last_symtab = &db_symtabs[i];
235 		diff = newdiff;
236 		ret = sym;
237 	    }
238 	}
239 	*offp = diff;
240 	return ret;
241 }
242 
243 /*
244  * Return name and value of a symbol
245  */
246 void
247 db_symbol_values(sym, namep, valuep)
248 	db_sym_t	sym;
249 	char		**namep;
250 	db_expr_t	*valuep;
251 {
252 	db_expr_t	value;
253 
254 	if (sym == DB_SYM_NULL) {
255 		*namep = 0;
256 		return;
257 	}
258 
259 	X_db_symbol_values(sym, namep, &value);
260 
261 	if (db_symbol_is_ambiguous(sym))
262 		*namep = db_qualify(sym, db_last_symtab->name);
263 	if (valuep)
264 		*valuep = value;
265 }
266 
267 
268 /*
269  * Print a the closest symbol to value
270  *
271  * After matching the symbol according to the given strategy
272  * we print it in the name+offset format, provided the symbol's
273  * value is close enough (eg smaller than db_maxoff).
274  * We also attempt to print [filename:linenum] when applicable
275  * (eg for procedure names).
276  *
277  * If we could not find a reasonable name+offset representation,
278  * then we just print the value in hex.  Small values might get
279  * bogus symbol associations, e.g. 3 might get some absolute
280  * value like _INCLUDE_VERSION or something, therefore we do
281  * not accept symbols whose value is zero (and use plain hex).
282  */
283 
284 unsigned int	db_maxoff = 0x10000000;
285 
286 void
287 db_printsym(off, strategy)
288 	db_expr_t	off;
289 	db_strategy_t	strategy;
290 {
291 	db_expr_t	d;
292 	char 		*filename;
293 	char		*name;
294 	db_expr_t	value;
295 	int 		linenum;
296 	db_sym_t	cursym;
297 
298 	cursym = db_search_symbol(off, strategy, &d);
299 	db_symbol_values(cursym, &name, &value);
300 	if (name == 0 || d >= db_maxoff || value == 0) {
301 		db_printf("%#n", off);
302 		return;
303 	}
304 	db_printf("%s", name);
305 	if (d)
306 		db_printf("+%#r", d);
307 	if (strategy == DB_STGY_PROC) {
308 		if (db_line_at_pc(cursym, &filename, &linenum, off))
309 			db_printf(" [%s:%d]", filename, linenum);
310 	}
311 }
312 
313 boolean_t
314 db_line_at_pc( sym, filename, linenum, pc)
315 	db_sym_t	sym;
316 	char		**filename;
317 	int		*linenum;
318 	db_expr_t	pc;
319 {
320 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
321 }
322 
323 int
324 db_sym_numargs(sym, nargp, argnames)
325 	db_sym_t	sym;
326 	int		*nargp;
327 	char		**argnames;
328 {
329 	return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
330 }
331