db_sym.c revision 1.19 1 1.19 augustss /* $NetBSD: db_sym.c,v 1.19 2000/03/30 11:31:27 augustss Exp $ */
2 1.7 cgd
3 1.1 cgd /*
4 1.1 cgd * Mach Operating System
5 1.1 cgd * Copyright (c) 1991,1990 Carnegie Mellon University
6 1.1 cgd * All Rights Reserved.
7 1.1 cgd *
8 1.1 cgd * Permission to use, copy, modify and distribute this software and its
9 1.1 cgd * documentation is hereby granted, provided that both the copyright
10 1.1 cgd * notice and this permission notice appear in all copies of the
11 1.1 cgd * software, derivative works or modified versions, and any portions
12 1.1 cgd * thereof, and that both notices appear in supporting documentation.
13 1.1 cgd *
14 1.17 pk * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15 1.1 cgd * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 1.1 cgd * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 1.1 cgd *
18 1.1 cgd * Carnegie Mellon requests users of this software to return to
19 1.1 cgd *
20 1.1 cgd * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
21 1.1 cgd * School of Computer Science
22 1.1 cgd * Carnegie Mellon University
23 1.1 cgd * Pittsburgh PA 15213-3890
24 1.1 cgd *
25 1.1 cgd * any improvements or extensions that they make and grant Carnegie the
26 1.1 cgd * rights to redistribute these changes.
27 1.1 cgd */
28 1.5 mycroft
29 1.6 mycroft #include <sys/param.h>
30 1.6 mycroft #include <sys/proc.h>
31 1.15 thorpej #include <sys/systm.h>
32 1.6 mycroft
33 1.1 cgd #include <machine/db_machdep.h>
34 1.6 mycroft
35 1.1 cgd #include <ddb/db_sym.h>
36 1.12 christos #include <ddb/db_output.h>
37 1.12 christos #include <ddb/db_extern.h>
38 1.12 christos #include <ddb/db_command.h>
39 1.1 cgd
40 1.1 cgd /*
41 1.1 cgd * Multiple symbol tables
42 1.1 cgd */
43 1.4 brezak #ifndef MAXLKMS
44 1.4 brezak #define MAXLKMS 20
45 1.4 brezak #endif
46 1.4 brezak
47 1.3 brezak #ifndef MAXNOSYMTABS
48 1.4 brezak #define MAXNOSYMTABS MAXLKMS+1 /* Room for kernel + LKM's */
49 1.3 brezak #endif
50 1.1 cgd
51 1.1 cgd db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
52 1.1 cgd
53 1.1 cgd db_symtab_t *db_last_symtab;
54 1.1 cgd
55 1.15 thorpej static char *db_qualify __P((db_sym_t, const char *));
56 1.15 thorpej
57 1.15 thorpej /*
58 1.15 thorpej * Put the most picky symbol table formats at the top!
59 1.15 thorpej */
60 1.15 thorpej const db_symformat_t *db_symformats[] = {
61 1.15 thorpej #ifdef DB_ELF_SYMBOLS
62 1.15 thorpej &db_symformat_elf,
63 1.15 thorpej #endif
64 1.15 thorpej #ifdef DB_AOUT_SYMBOLS
65 1.15 thorpej &db_symformat_aout,
66 1.15 thorpej #endif
67 1.15 thorpej NULL,
68 1.15 thorpej };
69 1.15 thorpej
70 1.15 thorpej const db_symformat_t *db_symformat;
71 1.15 thorpej
72 1.15 thorpej boolean_t X_db_sym_init __P((int, void *, void *, const char *));
73 1.15 thorpej db_sym_t X_db_lookup __P((db_symtab_t *, char *));
74 1.15 thorpej db_sym_t X_db_search_symbol __P((db_symtab_t *, db_addr_t,
75 1.15 thorpej db_strategy_t, db_expr_t *));
76 1.15 thorpej void X_db_symbol_values __P((db_symtab_t *, db_sym_t, char **,
77 1.15 thorpej db_expr_t *));
78 1.15 thorpej boolean_t X_db_line_at_pc __P((db_symtab_t *, db_sym_t, char **,
79 1.15 thorpej int *, db_expr_t));
80 1.15 thorpej int X_db_sym_numargs __P((db_symtab_t *, db_sym_t, int *,
81 1.15 thorpej char **));
82 1.15 thorpej
83 1.15 thorpej /*
84 1.15 thorpej * Initialize the kernel debugger by initializing the master symbol
85 1.15 thorpej * table. Note that if initializing the master symbol table fails,
86 1.15 thorpej * no other symbol tables can be loaded.
87 1.15 thorpej */
88 1.15 thorpej void
89 1.15 thorpej ddb_init(symsize, vss, vse)
90 1.15 thorpej int symsize;
91 1.15 thorpej void *vss, *vse;
92 1.15 thorpej {
93 1.15 thorpej const db_symformat_t **symf;
94 1.15 thorpej const char *name = "netbsd";
95 1.15 thorpej
96 1.15 thorpej if (symsize <= 0) {
97 1.15 thorpej printf(" [ no symbols available ]\n");
98 1.15 thorpej return;
99 1.15 thorpej }
100 1.15 thorpej
101 1.15 thorpej /*
102 1.15 thorpej * Do this check now for the master symbol table to avoid printing
103 1.15 thorpej * the message N times.
104 1.15 thorpej */
105 1.15 thorpej if (ALIGNED_POINTER(vss, long) == 0) {
106 1.18 simonb printf("[ %s symbol table has bad start address %p ]\n",
107 1.15 thorpej name, vss);
108 1.15 thorpej return;
109 1.15 thorpej }
110 1.15 thorpej
111 1.15 thorpej for (symf = db_symformats; *symf != NULL; symf++) {
112 1.15 thorpej db_symformat = *symf;
113 1.15 thorpej if (X_db_sym_init(symsize, vss, vse, name) == TRUE)
114 1.15 thorpej return;
115 1.15 thorpej }
116 1.15 thorpej
117 1.15 thorpej db_symformat = NULL;
118 1.15 thorpej printf("[ no symbol table formats found ]\n");
119 1.15 thorpej }
120 1.1 cgd
121 1.1 cgd /*
122 1.1 cgd * Add symbol table, with given name, to list of symbol tables.
123 1.1 cgd */
124 1.4 brezak int
125 1.1 cgd db_add_symbol_table(start, end, name, ref)
126 1.1 cgd char *start;
127 1.1 cgd char *end;
128 1.15 thorpej const char *name;
129 1.1 cgd char *ref;
130 1.1 cgd {
131 1.4 brezak int slot;
132 1.4 brezak
133 1.4 brezak for (slot = 0; slot < MAXNOSYMTABS; slot++) {
134 1.4 brezak if (db_symtabs[slot].name == NULL)
135 1.4 brezak break;
136 1.4 brezak }
137 1.4 brezak if (slot >= MAXNOSYMTABS) {
138 1.12 christos db_printf("No slots left for %s symbol table", name);
139 1.4 brezak return(-1);
140 1.1 cgd }
141 1.1 cgd
142 1.4 brezak db_symtabs[slot].start = start;
143 1.4 brezak db_symtabs[slot].end = end;
144 1.4 brezak db_symtabs[slot].name = name;
145 1.4 brezak db_symtabs[slot].private = ref;
146 1.4 brezak
147 1.4 brezak return(slot);
148 1.4 brezak }
149 1.4 brezak
150 1.4 brezak /*
151 1.4 brezak * Delete a symbol table. Caller is responsible for freeing storage.
152 1.4 brezak */
153 1.4 brezak void
154 1.4 brezak db_del_symbol_table(name)
155 1.4 brezak char *name;
156 1.4 brezak {
157 1.4 brezak int slot;
158 1.4 brezak
159 1.4 brezak for (slot = 0; slot < MAXNOSYMTABS; slot++) {
160 1.4 brezak if (db_symtabs[slot].name &&
161 1.4 brezak ! strcmp(db_symtabs[slot].name, name))
162 1.4 brezak break;
163 1.4 brezak }
164 1.4 brezak if (slot >= MAXNOSYMTABS) {
165 1.12 christos db_printf("Unable to find symbol table slot for %s.", name);
166 1.4 brezak return;
167 1.4 brezak }
168 1.4 brezak
169 1.4 brezak db_symtabs[slot].start = 0;
170 1.4 brezak db_symtabs[slot].end = 0;
171 1.4 brezak db_symtabs[slot].name = 0;
172 1.4 brezak db_symtabs[slot].private = 0;
173 1.1 cgd }
174 1.1 cgd
175 1.1 cgd /*
176 1.4 brezak * db_qualify("vm_map", "netbsd") returns "netbsd:vm_map".
177 1.1 cgd *
178 1.1 cgd * Note: return value points to static data whose content is
179 1.1 cgd * overwritten by each call... but in practice this seems okay.
180 1.1 cgd */
181 1.1 cgd static char *
182 1.1 cgd db_qualify(sym, symtabname)
183 1.1 cgd db_sym_t sym;
184 1.15 thorpej const char *symtabname;
185 1.1 cgd {
186 1.1 cgd char *symname;
187 1.1 cgd static char tmp[256];
188 1.19 augustss char *s;
189 1.1 cgd
190 1.1 cgd db_symbol_values(sym, &symname, 0);
191 1.1 cgd s = tmp;
192 1.12 christos while ((*s++ = *symtabname++) != '\0')
193 1.12 christos ;
194 1.1 cgd s[-1] = ':';
195 1.12 christos while ((*s++ = *symname++) != '\0')
196 1.12 christos ;
197 1.1 cgd return tmp;
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd
201 1.1 cgd boolean_t
202 1.1 cgd db_eqname(src, dst, c)
203 1.1 cgd char *src;
204 1.1 cgd char *dst;
205 1.12 christos int c;
206 1.1 cgd {
207 1.1 cgd if (!strcmp(src, dst))
208 1.1 cgd return (TRUE);
209 1.1 cgd if (src[0] == c)
210 1.1 cgd return (!strcmp(src+1,dst));
211 1.1 cgd return (FALSE);
212 1.1 cgd }
213 1.1 cgd
214 1.1 cgd boolean_t
215 1.1 cgd db_value_of_name(name, valuep)
216 1.1 cgd char *name;
217 1.1 cgd db_expr_t *valuep;
218 1.1 cgd {
219 1.1 cgd db_sym_t sym;
220 1.1 cgd
221 1.1 cgd sym = db_lookup(name);
222 1.1 cgd if (sym == DB_SYM_NULL)
223 1.1 cgd return (FALSE);
224 1.1 cgd db_symbol_values(sym, &name, valuep);
225 1.1 cgd return (TRUE);
226 1.1 cgd }
227 1.1 cgd
228 1.1 cgd
229 1.1 cgd /*
230 1.1 cgd * Lookup a symbol.
231 1.1 cgd * If the symbol has a qualifier (e.g., ux:vm_map),
232 1.1 cgd * then only the specified symbol table will be searched;
233 1.1 cgd * otherwise, all symbol tables will be searched.
234 1.1 cgd */
235 1.1 cgd db_sym_t
236 1.1 cgd db_lookup(symstr)
237 1.1 cgd char *symstr;
238 1.1 cgd {
239 1.1 cgd db_sym_t sp;
240 1.19 augustss int i;
241 1.1 cgd int symtab_start = 0;
242 1.4 brezak int symtab_end = MAXNOSYMTABS;
243 1.19 augustss char *cp;
244 1.1 cgd
245 1.1 cgd /*
246 1.1 cgd * Look for, remove, and remember any symbol table specifier.
247 1.1 cgd */
248 1.1 cgd for (cp = symstr; *cp; cp++) {
249 1.1 cgd if (*cp == ':') {
250 1.1 cgd *cp = '\0';
251 1.4 brezak for (i = 0; i < MAXNOSYMTABS; i++) {
252 1.4 brezak if (db_symtabs[i].name &&
253 1.4 brezak ! strcmp(symstr, db_symtabs[i].name)) {
254 1.1 cgd symtab_start = i;
255 1.1 cgd symtab_end = i + 1;
256 1.1 cgd break;
257 1.1 cgd }
258 1.1 cgd }
259 1.1 cgd *cp = ':';
260 1.4 brezak if (i == MAXNOSYMTABS) {
261 1.1 cgd db_error("invalid symbol table name");
262 1.8 mycroft /*NOTREACHED*/
263 1.1 cgd }
264 1.1 cgd symstr = cp+1;
265 1.1 cgd }
266 1.1 cgd }
267 1.1 cgd
268 1.1 cgd /*
269 1.1 cgd * Look in the specified set of symbol tables.
270 1.1 cgd * Return on first match.
271 1.1 cgd */
272 1.1 cgd for (i = symtab_start; i < symtab_end; i++) {
273 1.4 brezak if (db_symtabs[i].name &&
274 1.4 brezak (sp = X_db_lookup(&db_symtabs[i], symstr))) {
275 1.1 cgd db_last_symtab = &db_symtabs[i];
276 1.1 cgd return sp;
277 1.1 cgd }
278 1.1 cgd }
279 1.1 cgd return 0;
280 1.1 cgd }
281 1.1 cgd
282 1.1 cgd /*
283 1.1 cgd * Does this symbol name appear in more than one symbol table?
284 1.1 cgd * Used by db_symbol_values to decide whether to qualify a symbol.
285 1.1 cgd */
286 1.1 cgd boolean_t db_qualify_ambiguous_names = FALSE;
287 1.1 cgd
288 1.1 cgd boolean_t
289 1.1 cgd db_symbol_is_ambiguous(sym)
290 1.1 cgd db_sym_t sym;
291 1.1 cgd {
292 1.1 cgd char *sym_name;
293 1.19 augustss int i;
294 1.1 cgd boolean_t found_once = FALSE;
295 1.1 cgd
296 1.1 cgd if (!db_qualify_ambiguous_names)
297 1.1 cgd return FALSE;
298 1.1 cgd
299 1.1 cgd db_symbol_values(sym, &sym_name, 0);
300 1.4 brezak for (i = 0; i < MAXNOSYMTABS; i++) {
301 1.4 brezak if (db_symtabs[i].name &&
302 1.4 brezak X_db_lookup(&db_symtabs[i], sym_name)) {
303 1.1 cgd if (found_once)
304 1.1 cgd return TRUE;
305 1.1 cgd found_once = TRUE;
306 1.1 cgd }
307 1.1 cgd }
308 1.1 cgd return FALSE;
309 1.1 cgd }
310 1.1 cgd
311 1.1 cgd /*
312 1.1 cgd * Find the closest symbol to val, and return its name
313 1.1 cgd * and the difference between val and the symbol found.
314 1.1 cgd */
315 1.1 cgd db_sym_t
316 1.1 cgd db_search_symbol( val, strategy, offp)
317 1.19 augustss db_addr_t val;
318 1.1 cgd db_strategy_t strategy;
319 1.1 cgd db_expr_t *offp;
320 1.1 cgd {
321 1.1 cgd unsigned int diff;
322 1.13 cgd db_expr_t newdiff;
323 1.19 augustss int i;
324 1.1 cgd db_sym_t ret = DB_SYM_NULL, sym;
325 1.1 cgd
326 1.1 cgd newdiff = diff = ~0;
327 1.1 cgd db_last_symtab = 0;
328 1.4 brezak for (i = 0; i < MAXNOSYMTABS; i++) {
329 1.4 brezak if (!db_symtabs[i].name)
330 1.4 brezak continue;
331 1.1 cgd sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
332 1.1 cgd if (newdiff < diff) {
333 1.1 cgd db_last_symtab = &db_symtabs[i];
334 1.1 cgd diff = newdiff;
335 1.1 cgd ret = sym;
336 1.1 cgd }
337 1.1 cgd }
338 1.1 cgd *offp = diff;
339 1.1 cgd return ret;
340 1.1 cgd }
341 1.1 cgd
342 1.1 cgd /*
343 1.1 cgd * Return name and value of a symbol
344 1.1 cgd */
345 1.1 cgd void
346 1.1 cgd db_symbol_values(sym, namep, valuep)
347 1.1 cgd db_sym_t sym;
348 1.1 cgd char **namep;
349 1.1 cgd db_expr_t *valuep;
350 1.1 cgd {
351 1.1 cgd db_expr_t value;
352 1.1 cgd
353 1.1 cgd if (sym == DB_SYM_NULL) {
354 1.1 cgd *namep = 0;
355 1.1 cgd return;
356 1.1 cgd }
357 1.1 cgd
358 1.14 thorpej X_db_symbol_values(db_last_symtab, sym, namep, &value);
359 1.1 cgd
360 1.1 cgd if (db_symbol_is_ambiguous(sym))
361 1.1 cgd *namep = db_qualify(sym, db_last_symtab->name);
362 1.1 cgd if (valuep)
363 1.1 cgd *valuep = value;
364 1.1 cgd }
365 1.1 cgd
366 1.1 cgd
367 1.1 cgd /*
368 1.1 cgd * Print a the closest symbol to value
369 1.1 cgd *
370 1.1 cgd * After matching the symbol according to the given strategy
371 1.1 cgd * we print it in the name+offset format, provided the symbol's
372 1.1 cgd * value is close enough (eg smaller than db_maxoff).
373 1.1 cgd * We also attempt to print [filename:linenum] when applicable
374 1.1 cgd * (eg for procedure names).
375 1.1 cgd *
376 1.1 cgd * If we could not find a reasonable name+offset representation,
377 1.1 cgd * then we just print the value in hex. Small values might get
378 1.1 cgd * bogus symbol associations, e.g. 3 might get some absolute
379 1.1 cgd * value like _INCLUDE_VERSION or something, therefore we do
380 1.1 cgd * not accept symbols whose value is zero (and use plain hex).
381 1.9 gwr * Also, avoid printing as "end+0x????" which is useless.
382 1.9 gwr * The variable db_lastsym is used instead of "end" in case we
383 1.9 gwr * add support for symbols in loadable driver modules.
384 1.1 cgd */
385 1.9 gwr extern char end[];
386 1.13 cgd unsigned long db_lastsym = (unsigned long)end;
387 1.13 cgd unsigned int db_maxoff = 0x10000000;
388 1.1 cgd
389 1.9 gwr
390 1.1 cgd void
391 1.1 cgd db_printsym(off, strategy)
392 1.1 cgd db_expr_t off;
393 1.1 cgd db_strategy_t strategy;
394 1.1 cgd {
395 1.1 cgd db_expr_t d;
396 1.1 cgd char *filename;
397 1.1 cgd char *name;
398 1.1 cgd db_expr_t value;
399 1.1 cgd int linenum;
400 1.1 cgd db_sym_t cursym;
401 1.1 cgd
402 1.9 gwr if (off <= db_lastsym) {
403 1.9 gwr cursym = db_search_symbol(off, strategy, &d);
404 1.9 gwr db_symbol_values(cursym, &name, &value);
405 1.9 gwr if (name && (d < db_maxoff) && value) {
406 1.9 gwr db_printf("%s", name);
407 1.9 gwr if (d)
408 1.13 cgd db_printf("+%#lr", d);
409 1.9 gwr if (strategy == DB_STGY_PROC) {
410 1.9 gwr if (db_line_at_pc(cursym, &filename, &linenum, off))
411 1.9 gwr db_printf(" [%s:%d]", filename, linenum);
412 1.9 gwr }
413 1.9 gwr return;
414 1.9 gwr }
415 1.1 cgd }
416 1.13 cgd db_printf("%#ln", off);
417 1.9 gwr return;
418 1.1 cgd }
419 1.1 cgd
420 1.1 cgd
421 1.1 cgd boolean_t
422 1.1 cgd db_line_at_pc( sym, filename, linenum, pc)
423 1.3 brezak db_sym_t sym;
424 1.3 brezak char **filename;
425 1.3 brezak int *linenum;
426 1.3 brezak db_expr_t pc;
427 1.1 cgd {
428 1.1 cgd return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
429 1.3 brezak }
430 1.3 brezak
431 1.3 brezak int
432 1.3 brezak db_sym_numargs(sym, nargp, argnames)
433 1.3 brezak db_sym_t sym;
434 1.3 brezak int *nargp;
435 1.3 brezak char **argnames;
436 1.3 brezak {
437 1.3 brezak return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
438 1.15 thorpej }
439 1.15 thorpej
440 1.15 thorpej boolean_t
441 1.15 thorpej X_db_sym_init(symsize, vss, vse, name)
442 1.15 thorpej int symsize;
443 1.15 thorpej void *vss, *vse;
444 1.15 thorpej const char *name;
445 1.15 thorpej {
446 1.15 thorpej
447 1.15 thorpej if (db_symformat != NULL)
448 1.15 thorpej return ((*db_symformat->sym_init)(symsize, vss, vse, name));
449 1.15 thorpej return (FALSE);
450 1.15 thorpej }
451 1.15 thorpej
452 1.15 thorpej db_sym_t
453 1.15 thorpej X_db_lookup(stab, symstr)
454 1.15 thorpej db_symtab_t *stab;
455 1.15 thorpej char *symstr;
456 1.15 thorpej {
457 1.15 thorpej
458 1.15 thorpej if (db_symformat != NULL)
459 1.15 thorpej return ((*db_symformat->sym_lookup)(stab, symstr));
460 1.15 thorpej return ((db_sym_t)0);
461 1.15 thorpej }
462 1.15 thorpej
463 1.15 thorpej db_sym_t
464 1.15 thorpej X_db_search_symbol(stab, off, strategy, diffp)
465 1.15 thorpej db_symtab_t *stab;
466 1.15 thorpej db_addr_t off;
467 1.15 thorpej db_strategy_t strategy;
468 1.15 thorpej db_expr_t *diffp;
469 1.15 thorpej {
470 1.15 thorpej
471 1.15 thorpej if (db_symformat != NULL)
472 1.15 thorpej return ((*db_symformat->sym_search)(stab, off, strategy,
473 1.15 thorpej diffp));
474 1.15 thorpej return ((db_sym_t)0);
475 1.15 thorpej }
476 1.15 thorpej
477 1.15 thorpej void
478 1.15 thorpej X_db_symbol_values(stab, sym, namep, valuep)
479 1.15 thorpej db_symtab_t *stab;
480 1.15 thorpej db_sym_t sym;
481 1.15 thorpej char **namep;
482 1.15 thorpej db_expr_t *valuep;
483 1.15 thorpej {
484 1.15 thorpej
485 1.15 thorpej if (db_symformat != NULL)
486 1.15 thorpej (*db_symformat->sym_value)(stab, sym, namep, valuep);
487 1.15 thorpej }
488 1.15 thorpej
489 1.15 thorpej boolean_t
490 1.15 thorpej X_db_line_at_pc(stab, cursym, filename, linenum, off)
491 1.15 thorpej db_symtab_t *stab;
492 1.15 thorpej db_sym_t cursym;
493 1.15 thorpej char **filename;
494 1.15 thorpej int *linenum;
495 1.15 thorpej db_expr_t off;
496 1.15 thorpej {
497 1.15 thorpej
498 1.15 thorpej if (db_symformat != NULL)
499 1.15 thorpej return ((*db_symformat->sym_line_at_pc)(stab, cursym,
500 1.15 thorpej filename, linenum, off));
501 1.15 thorpej return (FALSE);
502 1.15 thorpej }
503 1.15 thorpej
504 1.15 thorpej boolean_t
505 1.15 thorpej X_db_sym_numargs(stab, cursym, nargp, argnamep)
506 1.15 thorpej db_symtab_t *stab;
507 1.15 thorpej db_sym_t cursym;
508 1.15 thorpej int *nargp;
509 1.15 thorpej char **argnamep;
510 1.15 thorpej {
511 1.15 thorpej
512 1.15 thorpej if (db_symformat != NULL)
513 1.15 thorpej return ((*db_symformat->sym_numargs)(stab, cursym, nargp,
514 1.15 thorpej argnamep));
515 1.15 thorpej return (FALSE);
516 1.1 cgd }
517