xref: /freebsd/sys/ddb/db_sym.c (revision 381fe1aa)
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.2 1993/10/16 16:47:25 rgrimes Exp $
27  */
28 
29 /*
30  * 	Author: David B. Golub, Carnegie Mellon University
31  *	Date:	7/90
32  */
33 #include "param.h"
34 #include "systm.h"
35 #include "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 #define	MAXNOSYMTABS	3	/* mach, ux, emulator */
51 
52 db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
53 int db_nsymtab = 0;
54 
55 db_symtab_t	*db_last_symtab;
56 
57 db_sym_t	db_lookup();	/* forward */
58 
59 /*
60  * Add symbol table, with given name, to list of symbol tables.
61  */
62 void
63 db_add_symbol_table(start, end, name, ref)
64 	char *start;
65 	char *end;
66 	char *name;
67 	char *ref;
68 {
69 	if (db_nsymtab >= MAXNOSYMTABS) {
70 		printf ("No slots left for %s symbol table", name);
71 		panic ("db_sym.c: db_add_symbol_table");
72 	}
73 
74 	db_symtabs[db_nsymtab].start = start;
75 	db_symtabs[db_nsymtab].end = end;
76 	db_symtabs[db_nsymtab].name = name;
77 	db_symtabs[db_nsymtab].private = ref;
78 	db_nsymtab++;
79 }
80 
81 /*
82  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
83  *
84  *  Note: return value points to static data whose content is
85  *  overwritten by each call... but in practice this seems okay.
86  */
87 static char *
88 db_qualify(sym, symtabname)
89 	db_sym_t	sym;
90 	register char	*symtabname;
91 {
92 	char		*symname;
93 	static char     tmp[256];
94 	register char	*s;
95 
96 	db_symbol_values(sym, &symname, 0);
97 	s = tmp;
98 	while (*s++ = *symtabname++) {
99 	}
100 	s[-1] = ':';
101 	while (*s++ = *symname++) {
102 	}
103 	return tmp;
104 }
105 
106 
107 boolean_t
108 db_eqname(src, dst, c)
109 	char *src;
110 	char *dst;
111 	char c;
112 {
113 	if (!strcmp(src, dst))
114 	    return (TRUE);
115 	if (src[0] == c)
116 	    return (!strcmp(src+1,dst));
117 	return (FALSE);
118 }
119 
120 boolean_t
121 db_value_of_name(name, valuep)
122 	char		*name;
123 	db_expr_t	*valuep;
124 {
125 	db_sym_t	sym;
126 
127 	sym = db_lookup(name);
128 	if (sym == DB_SYM_NULL)
129 	    return (FALSE);
130 	db_symbol_values(sym, &name, valuep);
131 	return (TRUE);
132 }
133 
134 
135 /*
136  * Lookup a symbol.
137  * If the symbol has a qualifier (e.g., ux:vm_map),
138  * then only the specified symbol table will be searched;
139  * otherwise, all symbol tables will be searched.
140  */
141 db_sym_t
142 db_lookup(symstr)
143 	char *symstr;
144 {
145 	db_sym_t sp;
146 	register int i;
147 	int symtab_start = 0;
148 	int symtab_end = db_nsymtab;
149 	register char *cp;
150 
151 	/*
152 	 * Look for, remove, and remember any symbol table specifier.
153 	 */
154 	for (cp = symstr; *cp; cp++) {
155 		if (*cp == ':') {
156 			*cp = '\0';
157 			for (i = 0; i < db_nsymtab; i++) {
158 				if (! strcmp(symstr, db_symtabs[i].name)) {
159 					symtab_start = i;
160 					symtab_end = i + 1;
161 					break;
162 				}
163 			}
164 			*cp = ':';
165 			if (i == db_nsymtab) {
166 				db_error("invalid symbol table name");
167 			}
168 			symstr = cp+1;
169 		}
170 	}
171 
172 	/*
173 	 * Look in the specified set of symbol tables.
174 	 * Return on first match.
175 	 */
176 	for (i = symtab_start; i < symtab_end; i++) {
177 		if (sp = X_db_lookup(&db_symtabs[i], symstr)) {
178 			db_last_symtab = &db_symtabs[i];
179 			return sp;
180 		}
181 	}
182 	return 0;
183 }
184 
185 /*
186  * Does this symbol name appear in more than one symbol table?
187  * Used by db_symbol_values to decide whether to qualify a symbol.
188  */
189 boolean_t db_qualify_ambiguous_names = FALSE;
190 
191 boolean_t
192 db_symbol_is_ambiguous(sym)
193 	db_sym_t	sym;
194 {
195 	char		*sym_name;
196 	register int	i;
197 	register
198 	boolean_t	found_once = FALSE;
199 
200 	if (!db_qualify_ambiguous_names)
201 		return FALSE;
202 
203 	db_symbol_values(sym, &sym_name, 0);
204 	for (i = 0; i < db_nsymtab; i++) {
205 		if (X_db_lookup(&db_symtabs[i], sym_name)) {
206 			if (found_once)
207 				return TRUE;
208 			found_once = TRUE;
209 		}
210 	}
211 	return FALSE;
212 }
213 
214 /*
215  * Find the closest symbol to val, and return its name
216  * and the difference between val and the symbol found.
217  */
218 db_sym_t
219 db_search_symbol( val, strategy, offp)
220 	register db_addr_t	val;
221 	db_strategy_t		strategy;
222 	db_expr_t		*offp;
223 {
224 	register
225 	unsigned int	diff;
226 	unsigned int	newdiff;
227 	register int	i;
228 	db_sym_t	ret = DB_SYM_NULL, sym;
229 
230 	newdiff = diff = ~0;
231 	db_last_symtab = 0;
232 	for (i = 0; i < db_nsymtab; i++) {
233 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
234 	    if (newdiff < diff) {
235 		db_last_symtab = &db_symtabs[i];
236 		diff = newdiff;
237 		ret = sym;
238 	    }
239 	}
240 	*offp = diff;
241 	return ret;
242 }
243 
244 /*
245  * Return name and value of a symbol
246  */
247 void
248 db_symbol_values(sym, namep, valuep)
249 	db_sym_t	sym;
250 	char		**namep;
251 	db_expr_t	*valuep;
252 {
253 	db_expr_t	value;
254 
255 	if (sym == DB_SYM_NULL) {
256 		*namep = 0;
257 		return;
258 	}
259 
260 	X_db_symbol_values(sym, namep, &value);
261 
262 	if (db_symbol_is_ambiguous(sym))
263 		*namep = db_qualify(sym, db_last_symtab->name);
264 	if (valuep)
265 		*valuep = value;
266 }
267 
268 
269 /*
270  * Print a the closest symbol to value
271  *
272  * After matching the symbol according to the given strategy
273  * we print it in the name+offset format, provided the symbol's
274  * value is close enough (eg smaller than db_maxoff).
275  * We also attempt to print [filename:linenum] when applicable
276  * (eg for procedure names).
277  *
278  * If we could not find a reasonable name+offset representation,
279  * then we just print the value in hex.  Small values might get
280  * bogus symbol associations, e.g. 3 might get some absolute
281  * value like _INCLUDE_VERSION or something, therefore we do
282  * not accept symbols whose value is zero (and use plain hex).
283  */
284 
285 unsigned int	db_maxoff = 0x10000000;
286 
287 void
288 db_printsym(off, strategy)
289 	db_expr_t	off;
290 	db_strategy_t	strategy;
291 {
292 	db_expr_t	d;
293 	char 		*filename;
294 	char		*name;
295 	db_expr_t	value;
296 	int 		linenum;
297 	db_sym_t	cursym;
298 
299 	cursym = db_search_symbol(off, strategy, &d);
300 	db_symbol_values(cursym, &name, &value);
301 	if (name == 0 || d >= db_maxoff || value == 0) {
302 		db_printf("%#n", off);
303 		return;
304 	}
305 	db_printf("%s", name);
306 	if (d)
307 		db_printf("+%#r", d);
308 	if (strategy == DB_STGY_PROC) {
309 		if (db_line_at_pc(cursym, &filename, &linenum, off))
310 			db_printf(" [%s:%d]", filename, linenum);
311 	}
312 }
313 
314 
315 boolean_t
316 db_line_at_pc( sym, filename, linenum, pc)
317 	int sym;
318 	int filename;
319 	int linenum;
320 	int pc;
321 {
322 	return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
323 }
324