FreeBSD/Linux Kernel Cross Reference
sys/ddb/db_sym.c
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 /*
27 * Author: David B. Golub, Carnegie Mellon University
28 * Date: 7/90
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD: releng/5.2/sys/ddb/db_sym.c 116176 2003-06-10 22:09:23Z obrien $");
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36
37 #include <ddb/ddb.h>
38 #include <ddb/db_sym.h>
39
40 /*
41 * Multiple symbol tables
42 */
43 #ifndef MAXNOSYMTABS
44 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
45 #endif
46
47 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
48 static int db_nsymtab = 0;
49
50 static db_symtab_t *db_last_symtab; /* where last symbol was found */
51
52 static c_db_sym_t db_lookup( const char *symstr);
53 static char *db_qualify(c_db_sym_t sym, char *symtabname);
54 static boolean_t db_symbol_is_ambiguous(c_db_sym_t sym);
55 static boolean_t db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
56
57 /*
58 * Add symbol table, with given name, to list of symbol tables.
59 */
60 void
61 db_add_symbol_table(start, end, name, ref)
62 char *start;
63 char *end;
64 char *name;
65 char *ref;
66 {
67 if (db_nsymtab >= MAXNOSYMTABS) {
68 printf ("No slots left for %s symbol table", name);
69 panic ("db_sym.c: db_add_symbol_table");
70 }
71
72 db_symtabs[db_nsymtab].start = start;
73 db_symtabs[db_nsymtab].end = end;
74 db_symtabs[db_nsymtab].name = name;
75 db_symtabs[db_nsymtab].private = ref;
76 db_nsymtab++;
77 }
78
79 /*
80 * db_qualify("vm_map", "ux") returns "unix:vm_map".
81 *
82 * Note: return value points to static data whose content is
83 * overwritten by each call... but in practice this seems okay.
84 */
85 static char *
86 db_qualify(sym, symtabname)
87 c_db_sym_t sym;
88 register char *symtabname;
89 {
90 const char *symname;
91 static char tmp[256];
92
93 db_symbol_values(sym, &symname, 0);
94 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
95 return tmp;
96 }
97
98
99 boolean_t
100 db_eqname(src, dst, c)
101 const char *src;
102 const char *dst;
103 int c;
104 {
105 if (!strcmp(src, dst))
106 return (TRUE);
107 if (src[0] == c)
108 return (!strcmp(src+1,dst));
109 return (FALSE);
110 }
111
112 boolean_t
113 db_value_of_name(name, valuep)
114 const char *name;
115 db_expr_t *valuep;
116 {
117 c_db_sym_t sym;
118
119 sym = db_lookup(name);
120 if (sym == C_DB_SYM_NULL)
121 return (FALSE);
122 db_symbol_values(sym, &name, valuep);
123 return (TRUE);
124 }
125
126
127 /*
128 * Lookup a symbol.
129 * If the symbol has a qualifier (e.g., ux:vm_map),
130 * then only the specified symbol table will be searched;
131 * otherwise, all symbol tables will be searched.
132 */
133 static c_db_sym_t
134 db_lookup(symstr)
135 const char *symstr;
136 {
137 c_db_sym_t sp;
138 register int i;
139 int symtab_start = 0;
140 int symtab_end = db_nsymtab;
141 register const char *cp;
142
143 /*
144 * Look for, remove, and remember any symbol table specifier.
145 */
146 for (cp = symstr; *cp; cp++) {
147 if (*cp == ':') {
148 for (i = 0; i < db_nsymtab; i++) {
149 int n = strlen(db_symtabs[i].name);
150
151 if (
152 n == (cp - symstr) &&
153 strncmp(symstr, db_symtabs[i].name, n) == 0
154 ) {
155 symtab_start = i;
156 symtab_end = i + 1;
157 break;
158 }
159 }
160 if (i == db_nsymtab) {
161 db_error("invalid symbol table name");
162 }
163 symstr = cp+1;
164 }
165 }
166
167 /*
168 * Look in the specified set of symbol tables.
169 * Return on first match.
170 */
171 for (i = symtab_start; i < symtab_end; i++) {
172 sp = X_db_lookup(&db_symtabs[i], symstr);
173 if (sp) {
174 db_last_symtab = &db_symtabs[i];
175 return sp;
176 }
177 }
178 return 0;
179 }
180
181 /*
182 * If TRUE, check across symbol tables for multiple occurrences
183 * of a name. Might slow things down quite a bit.
184 */
185 static volatile boolean_t db_qualify_ambiguous_names = FALSE;
186
187 /*
188 * Does this symbol name appear in more than one symbol table?
189 * Used by db_symbol_values to decide whether to qualify a symbol.
190 */
191 static boolean_t
192 db_symbol_is_ambiguous(sym)
193 c_db_sym_t sym;
194 {
195 const 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 c_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 size_t newdiff;
227 register int i;
228 c_db_sym_t ret = C_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 c_db_sym_t sym;
250 const 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(db_last_symtab, 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 "small" (and use plain hex).
283 */
284
285 db_expr_t db_maxoff = 0x10000;
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 const char *name;
295 db_expr_t value;
296 int linenum;
297 c_db_sym_t cursym;
298
299 cursym = db_search_symbol(off, strategy, &d);
300 db_symbol_values(cursym, &name, &value);
301 if (name == 0)
302 value = off;
303 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
304 db_printf("%+#lr", (long)off);
305 return;
306 }
307 if (name == 0 || d >= (unsigned long)db_maxoff) {
308 db_printf("%#lr", (unsigned long)off);
309 return;
310 }
311 db_printf("%s", name);
312 if (d)
313 db_printf("+%+#lr", (long)d);
314 if (strategy == DB_STGY_PROC) {
315 if (db_line_at_pc(cursym, &filename, &linenum, off))
316 db_printf(" [%s:%d]", filename, linenum);
317 }
318 }
319
320 static boolean_t
321 db_line_at_pc( sym, filename, linenum, pc)
322 c_db_sym_t sym;
323 char **filename;
324 int *linenum;
325 db_expr_t pc;
326 {
327 return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
328 }
329
330 int
331 db_sym_numargs(sym, nargp, argnames)
332 c_db_sym_t sym;
333 int *nargp;
334 char **argnames;
335 {
336 return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
337 }
Cache object: 800eaa3760fb53aa694335c66b6786d7
|