db_sym.c revision 1.44 1 1.44 christos /* $NetBSD: db_sym.c,v 1.44 2003/10/25 08:54:01 christos Exp $ */
2 1.7 cgd
3 1.31 simonb /*
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.31 simonb *
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.31 simonb *
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.31 simonb *
18 1.1 cgd * Carnegie Mellon requests users of this software to return to
19 1.31 simonb *
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.31 simonb *
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.28 lukem
29 1.28 lukem #include <sys/cdefs.h>
30 1.44 christos __KERNEL_RCSID(0, "$NetBSD: db_sym.c,v 1.44 2003/10/25 08:54:01 christos Exp $");
31 1.5 mycroft
32 1.32 itohy #include "opt_ddbparam.h"
33 1.26 simonb
34 1.6 mycroft #include <sys/param.h>
35 1.6 mycroft #include <sys/proc.h>
36 1.15 thorpej #include <sys/systm.h>
37 1.35 ragge #include <sys/ksyms.h>
38 1.6 mycroft
39 1.1 cgd #include <machine/db_machdep.h>
40 1.6 mycroft
41 1.20 jhawk #include <ddb/db_lex.h>
42 1.1 cgd #include <ddb/db_sym.h>
43 1.12 christos #include <ddb/db_output.h>
44 1.12 christos #include <ddb/db_extern.h>
45 1.12 christos #include <ddb/db_command.h>
46 1.1 cgd
47 1.35 ragge static void db_symsplit(char *, char **, char **);
48 1.31 simonb
49 1.15 thorpej
50 1.15 thorpej #ifdef DB_AOUT_SYMBOLS
51 1.35 ragge #define TBLNAME "netbsd"
52 1.15 thorpej
53 1.35 ragge static int using_aout_symtab;
54 1.15 thorpej const db_symformat_t *db_symformat;
55 1.35 ragge static db_forall_func_t db_sift;
56 1.35 ragge extern db_symformat_t db_symformat_aout;
57 1.35 ragge #endif
58 1.15 thorpej
59 1.15 thorpej
60 1.15 thorpej /*
61 1.15 thorpej * Initialize the kernel debugger by initializing the master symbol
62 1.15 thorpej * table. Note that if initializing the master symbol table fails,
63 1.15 thorpej * no other symbol tables can be loaded.
64 1.15 thorpej */
65 1.15 thorpej void
66 1.31 simonb ddb_init(int symsize, void *vss, void *vse)
67 1.15 thorpej {
68 1.35 ragge #ifdef DB_AOUT_SYMBOLS
69 1.35 ragge db_symformat = &db_symformat_aout;
70 1.35 ragge if ((*db_symformat->sym_init)(symsize, vss, vse, TBLNAME) == TRUE) {
71 1.35 ragge using_aout_symtab = TRUE;
72 1.15 thorpej return;
73 1.15 thorpej }
74 1.35 ragge #endif
75 1.37 ragge ksyms_init(symsize, vss, vse); /* Will complain if necessary */
76 1.1 cgd }
77 1.1 cgd
78 1.1 cgd boolean_t
79 1.31 simonb db_eqname(char *src, char *dst, int c)
80 1.1 cgd {
81 1.31 simonb
82 1.1 cgd if (!strcmp(src, dst))
83 1.31 simonb return (TRUE);
84 1.1 cgd if (src[0] == c)
85 1.31 simonb return (!strcmp(src+1,dst));
86 1.1 cgd return (FALSE);
87 1.1 cgd }
88 1.1 cgd
89 1.1 cgd boolean_t
90 1.31 simonb db_value_of_name(char *name, db_expr_t *valuep)
91 1.1 cgd {
92 1.35 ragge char *mod, *sym;
93 1.43 scw unsigned long uval;
94 1.43 scw long val;
95 1.1 cgd
96 1.35 ragge #ifdef DB_AOUT_SYMBOLS
97 1.35 ragge db_sym_t ssym;
98 1.1 cgd
99 1.35 ragge if (using_aout_symtab) {
100 1.35 ragge /*
101 1.35 ragge * Cannot load symtabs in a.out kernels, so the ':'
102 1.35 ragge * style of selecting modules is irrelevant.
103 1.35 ragge */
104 1.35 ragge ssym = (*db_symformat->sym_lookup)(NULL, name);
105 1.35 ragge if (ssym == DB_SYM_NULL)
106 1.35 ragge return (FALSE);
107 1.35 ragge db_symbol_values(ssym, &name, valuep);
108 1.35 ragge return (TRUE);
109 1.1 cgd }
110 1.35 ragge #endif
111 1.35 ragge db_symsplit(name, &mod, &sym);
112 1.43 scw if (ksyms_getval(mod, sym, &uval, KSYMS_EXTERN) == 0) {
113 1.43 scw val = (long) uval;
114 1.41 scw *valuep = (db_expr_t)val;
115 1.35 ragge return TRUE;
116 1.41 scw }
117 1.43 scw if (ksyms_getval(mod, sym, &uval, KSYMS_ANY) == 0) {
118 1.43 scw val = (long) uval;
119 1.41 scw *valuep = (db_expr_t)val;
120 1.35 ragge return TRUE;
121 1.41 scw }
122 1.35 ragge return FALSE;
123 1.1 cgd }
124 1.1 cgd
125 1.35 ragge #ifdef DB_AOUT_SYMBOLS
126 1.20 jhawk /* Private structure for passing args to db_sift() from db_sifting(). */
127 1.20 jhawk struct db_sift_args {
128 1.20 jhawk char *symstr;
129 1.20 jhawk int mode;
130 1.20 jhawk };
131 1.20 jhawk
132 1.20 jhawk /*
133 1.20 jhawk * Does the work of db_sifting(), called once for each
134 1.35 ragge * symbol via db_forall(), prints out symbols matching
135 1.20 jhawk * criteria.
136 1.20 jhawk */
137 1.20 jhawk static void
138 1.31 simonb db_sift(db_symtab_t *stab, db_sym_t sym, char *name, char *suffix, int prefix,
139 1.31 simonb void *arg)
140 1.20 jhawk {
141 1.20 jhawk char c, sc;
142 1.20 jhawk char *find, *p;
143 1.20 jhawk size_t len;
144 1.20 jhawk struct db_sift_args *dsa;
145 1.20 jhawk
146 1.20 jhawk dsa = (struct db_sift_args*)arg;
147 1.20 jhawk
148 1.20 jhawk find = dsa->symstr; /* String we're looking for. */
149 1.20 jhawk p = name; /* String we're searching within. */
150 1.31 simonb
151 1.20 jhawk /* Matching algorithm cribbed from strstr(), which is not
152 1.20 jhawk in the kernel. */
153 1.20 jhawk if ((c = *find++) != 0) {
154 1.20 jhawk len = strlen(find);
155 1.20 jhawk do {
156 1.20 jhawk do {
157 1.20 jhawk if ((sc = *p++) == 0)
158 1.20 jhawk return;
159 1.20 jhawk } while (sc != c);
160 1.20 jhawk } while (strncmp(p, find, len) != 0);
161 1.20 jhawk }
162 1.20 jhawk if (dsa->mode=='F') /* ala ls -F */
163 1.20 jhawk db_printf("%s%s ", name, suffix);
164 1.20 jhawk else
165 1.20 jhawk db_printf("%s ", name);
166 1.20 jhawk }
167 1.35 ragge #endif
168 1.20 jhawk
169 1.20 jhawk /*
170 1.20 jhawk * "Sift" for a partial symbol.
171 1.20 jhawk * Named for the Sun OpenPROM command ("sifting").
172 1.20 jhawk * If the symbol has a qualifier (e.g., ux:vm_map),
173 1.20 jhawk * then only the specified symbol table will be searched;
174 1.20 jhawk * otherwise, all symbol tables will be searched..
175 1.20 jhawk *
176 1.20 jhawk * "mode" is how-to-display, set from modifiers.
177 1.20 jhawk */
178 1.20 jhawk void
179 1.31 simonb db_sifting(char *symstr, int mode)
180 1.20 jhawk {
181 1.35 ragge char *mod, *sym;
182 1.35 ragge
183 1.35 ragge #ifdef DB_AOUT_SYMBOLS
184 1.20 jhawk struct db_sift_args dsa;
185 1.20 jhawk
186 1.35 ragge if (using_aout_symtab) {
187 1.35 ragge dsa.symstr = symstr;
188 1.35 ragge dsa.mode = mode;
189 1.35 ragge (*db_symformat->sym_forall)(NULL, db_sift, &dsa);
190 1.35 ragge db_printf("\n");
191 1.35 ragge return;
192 1.20 jhawk }
193 1.35 ragge #endif
194 1.20 jhawk
195 1.35 ragge db_symsplit(symstr, &mod, &sym);
196 1.35 ragge if (ksyms_sift(mod, sym, mode) == ENODEV)
197 1.35 ragge db_error("invalid symbol table name");
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd /*
201 1.1 cgd * Find the closest symbol to val, and return its name
202 1.1 cgd * and the difference between val and the symbol found.
203 1.1 cgd */
204 1.1 cgd db_sym_t
205 1.31 simonb db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
206 1.1 cgd {
207 1.44 christos /*###207 [cc] warning: `diff' might be used uninitialized in this function%%%*/
208 1.35 ragge unsigned int diff;
209 1.41 scw unsigned long naddr;
210 1.35 ragge db_sym_t ret = DB_SYM_NULL;
211 1.39 jdolecek const char *mod;
212 1.39 jdolecek char *sym;
213 1.35 ragge
214 1.35 ragge #ifdef DB_AOUT_SYMBOLS
215 1.35 ragge db_expr_t newdiff;
216 1.35 ragge db_sym_t ssym;
217 1.35 ragge
218 1.35 ragge if (using_aout_symtab) {
219 1.35 ragge newdiff = diff = ~0;
220 1.35 ragge ssym = (*db_symformat->sym_search)
221 1.35 ragge (NULL, val, strategy, &newdiff);
222 1.33 thorpej if ((unsigned int) newdiff < diff) {
223 1.31 simonb diff = newdiff;
224 1.35 ragge ret = ssym;
225 1.31 simonb }
226 1.35 ragge *offp = diff;
227 1.35 ragge return ret;
228 1.35 ragge }
229 1.35 ragge #endif
230 1.35 ragge
231 1.41 scw if (ksyms_getname(&mod, &sym, (vaddr_t)val, strategy) == 0) {
232 1.35 ragge (void)ksyms_getval(mod, sym, &naddr, KSYMS_ANY);
233 1.41 scw diff = val - (db_addr_t)naddr;
234 1.41 scw ret = (db_sym_t)naddr;
235 1.44 christos } else
236 1.44 christos diff = 0;
237 1.1 cgd *offp = diff;
238 1.1 cgd return ret;
239 1.1 cgd }
240 1.1 cgd
241 1.1 cgd /*
242 1.1 cgd * Return name and value of a symbol
243 1.1 cgd */
244 1.1 cgd void
245 1.31 simonb db_symbol_values(db_sym_t sym, char **namep, db_expr_t *valuep)
246 1.1 cgd {
247 1.39 jdolecek const char *mod;
248 1.1 cgd
249 1.1 cgd if (sym == DB_SYM_NULL) {
250 1.1 cgd *namep = 0;
251 1.1 cgd return;
252 1.1 cgd }
253 1.1 cgd
254 1.35 ragge #ifdef DB_AOUT_SYMBOLS
255 1.35 ragge if (using_aout_symtab) {
256 1.35 ragge db_expr_t value;
257 1.35 ragge (*db_symformat->sym_value)(NULL, sym, namep, &value);
258 1.35 ragge if (valuep)
259 1.35 ragge *valuep = value;
260 1.35 ragge return;
261 1.35 ragge }
262 1.35 ragge #endif
263 1.1 cgd
264 1.41 scw if (ksyms_getname(&mod, namep, (vaddr_t)sym,
265 1.41 scw KSYMS_ANY|KSYMS_EXACT) == 0) {
266 1.35 ragge if (valuep)
267 1.35 ragge *valuep = sym;
268 1.35 ragge } else
269 1.35 ragge *namep = NULL;
270 1.1 cgd }
271 1.1 cgd
272 1.1 cgd
273 1.1 cgd /*
274 1.1 cgd * Print a the closest symbol to value
275 1.1 cgd *
276 1.1 cgd * After matching the symbol according to the given strategy
277 1.1 cgd * we print it in the name+offset format, provided the symbol's
278 1.1 cgd * value is close enough (eg smaller than db_maxoff).
279 1.1 cgd * We also attempt to print [filename:linenum] when applicable
280 1.1 cgd * (eg for procedure names).
281 1.1 cgd *
282 1.1 cgd * If we could not find a reasonable name+offset representation,
283 1.1 cgd * then we just print the value in hex. Small values might get
284 1.1 cgd * bogus symbol associations, e.g. 3 might get some absolute
285 1.1 cgd * value like _INCLUDE_VERSION or something, therefore we do
286 1.1 cgd * not accept symbols whose value is zero (and use plain hex).
287 1.9 gwr * Also, avoid printing as "end+0x????" which is useless.
288 1.9 gwr * The variable db_lastsym is used instead of "end" in case we
289 1.9 gwr * add support for symbols in loadable driver modules.
290 1.1 cgd */
291 1.9 gwr extern char end[];
292 1.13 cgd unsigned long db_lastsym = (unsigned long)end;
293 1.13 cgd unsigned int db_maxoff = 0x10000000;
294 1.1 cgd
295 1.30 jhawk void
296 1.40 itojun db_symstr(char *buf, size_t buflen, db_expr_t off, db_strategy_t strategy)
297 1.30 jhawk {
298 1.39 jdolecek char *name;
299 1.39 jdolecek const char *mod;
300 1.42 ragge unsigned long val;
301 1.38 ragge
302 1.38 ragge #ifdef DB_AOUT_SYMBOLS
303 1.38 ragge if (using_aout_symtab) {
304 1.38 ragge db_expr_t d;
305 1.38 ragge char *filename;
306 1.38 ragge char *name;
307 1.38 ragge db_expr_t value;
308 1.38 ragge int linenum;
309 1.38 ragge db_sym_t cursym;
310 1.38 ragge
311 1.38 ragge if ((unsigned long) off <= db_lastsym) {
312 1.38 ragge cursym = db_search_symbol(off, strategy, &d);
313 1.38 ragge db_symbol_values(cursym, &name, &value);
314 1.38 ragge if (name != NULL &&
315 1.38 ragge ((unsigned int) d < db_maxoff) &&
316 1.38 ragge value != 0) {
317 1.40 itojun strlcpy(buf, name, buflen);
318 1.38 ragge if (d) {
319 1.40 itojun strlcat(buf, "+", buflen);
320 1.38 ragge db_format_radix(buf+strlen(buf),
321 1.38 ragge 24, d, TRUE);
322 1.38 ragge }
323 1.38 ragge if (strategy == DB_STGY_PROC) {
324 1.38 ragge if ((*db_symformat->sym_line_at_pc)
325 1.38 ragge (NULL, cursym, &filename,
326 1.38 ragge &linenum, off))
327 1.38 ragge sprintf(buf+strlen(buf),
328 1.38 ragge " [%s:%d]",
329 1.38 ragge filename, linenum);
330 1.38 ragge }
331 1.38 ragge return;
332 1.38 ragge }
333 1.38 ragge }
334 1.40 itojun strlcpy(buf, db_num_to_str(off), buflen);
335 1.38 ragge return;
336 1.38 ragge }
337 1.38 ragge #endif
338 1.41 scw if (ksyms_getname(&mod, &name, (vaddr_t)off,
339 1.41 scw strategy|KSYMS_CLOSEST) == 0) {
340 1.38 ragge (void)ksyms_getval(mod, name, &val, KSYMS_ANY);
341 1.38 ragge if (((off - val) < db_maxoff) && val) {
342 1.38 ragge sprintf(buf, "%s:%s", mod, name);
343 1.38 ragge if (off - val) {
344 1.40 itojun strlcat(buf, "+", buflen);
345 1.38 ragge db_format_radix(buf+strlen(buf),
346 1.38 ragge 24, off - val, TRUE);
347 1.30 jhawk }
348 1.38 ragge #ifdef notyet
349 1.38 ragge if (strategy & KSYMS_PROC) {
350 1.38 ragge if (ksyms_fmaddr(off, &filename, &linenum) == 0) sprintf(buf+strlen(buf),
351 1.30 jhawk " [%s:%d]", filename, linenum);
352 1.30 jhawk }
353 1.38 ragge #endif
354 1.30 jhawk return;
355 1.30 jhawk }
356 1.30 jhawk }
357 1.40 itojun strlcpy(buf, db_num_to_str(off), buflen);
358 1.30 jhawk }
359 1.9 gwr
360 1.1 cgd void
361 1.31 simonb db_printsym(db_expr_t off, db_strategy_t strategy,
362 1.31 simonb void (*pr)(const char *, ...))
363 1.1 cgd {
364 1.39 jdolecek char *name;
365 1.39 jdolecek const char *mod;
366 1.43 scw unsigned long uval;
367 1.43 scw long val;
368 1.35 ragge #ifdef notyet
369 1.35 ragge char *filename;
370 1.35 ragge int linenum;
371 1.35 ragge #endif
372 1.1 cgd
373 1.35 ragge #ifdef DB_AOUT_SYMBOLS
374 1.35 ragge if (using_aout_symtab) {
375 1.35 ragge db_expr_t d;
376 1.35 ragge char *filename;
377 1.35 ragge char *name;
378 1.35 ragge db_expr_t value;
379 1.35 ragge int linenum;
380 1.35 ragge db_sym_t cursym;
381 1.35 ragge if ((unsigned long) off <= db_lastsym) {
382 1.35 ragge cursym = db_search_symbol(off, strategy, &d);
383 1.35 ragge db_symbol_values(cursym, &name, &value);
384 1.35 ragge if (name != NULL &&
385 1.35 ragge ((unsigned int) d < db_maxoff) &&
386 1.35 ragge value != 0) {
387 1.35 ragge (*pr)("%s", name);
388 1.35 ragge if (d) {
389 1.35 ragge char tbuf[24];
390 1.35 ragge
391 1.35 ragge db_format_radix(tbuf, 24, d, TRUE);
392 1.35 ragge (*pr)("+%s", tbuf);
393 1.35 ragge }
394 1.35 ragge if (strategy == DB_STGY_PROC) {
395 1.35 ragge if ((*db_symformat->sym_line_at_pc)
396 1.35 ragge (NULL, cursym, &filename,
397 1.35 ragge &linenum, off))
398 1.35 ragge (*pr)(" [%s:%d]",
399 1.35 ragge filename, linenum);
400 1.35 ragge }
401 1.35 ragge return;
402 1.35 ragge }
403 1.35 ragge }
404 1.35 ragge (*pr)(db_num_to_str(off));
405 1.35 ragge return;
406 1.35 ragge }
407 1.35 ragge #endif
408 1.41 scw if (ksyms_getname(&mod, &name, (vaddr_t)off,
409 1.41 scw strategy|KSYMS_CLOSEST) == 0) {
410 1.43 scw (void)ksyms_getval(mod, name, &uval, KSYMS_ANY);
411 1.43 scw val = (long) uval;
412 1.35 ragge if (((off - val) < db_maxoff) && val) {
413 1.35 ragge (*pr)("%s:%s", mod, name);
414 1.35 ragge if (off - val) {
415 1.23 tv char tbuf[24];
416 1.23 tv
417 1.35 ragge db_format_radix(tbuf, 24, off - val, TRUE);
418 1.24 tv (*pr)("+%s", tbuf);
419 1.23 tv }
420 1.35 ragge #ifdef notyet
421 1.35 ragge if (strategy & KSYMS_PROC) {
422 1.35 ragge if (ksyms_fmaddr(off, &filename, &linenum) == 0)
423 1.21 jhawk (*pr)(" [%s:%d]", filename, linenum);
424 1.9 gwr }
425 1.35 ragge #endif
426 1.9 gwr return;
427 1.9 gwr }
428 1.1 cgd }
429 1.22 jhawk (*pr)(db_num_to_str(off));
430 1.9 gwr return;
431 1.1 cgd }
432 1.1 cgd
433 1.35 ragge /*
434 1.35 ragge * Splits a string in the form "mod:sym" to two strings.
435 1.35 ragge */
436 1.31 simonb static void
437 1.35 ragge db_symsplit(char *str, char **mod, char **sym)
438 1.15 thorpej {
439 1.35 ragge char *cp;
440 1.15 thorpej
441 1.35 ragge if ((cp = strchr(str, ':')) != NULL) {
442 1.35 ragge *cp++ = '\0';
443 1.35 ragge *mod = str;
444 1.35 ragge *sym = cp;
445 1.35 ragge } else {
446 1.35 ragge *mod = NULL;
447 1.35 ragge *sym = str;
448 1.35 ragge }
449 1.1 cgd }
450 1.36 ragge
451 1.36 ragge boolean_t
452 1.36 ragge db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep)
453 1.36 ragge {
454 1.36 ragge #ifdef DB_AOUT_SYMBOLS
455 1.36 ragge if (using_aout_symtab)
456 1.36 ragge return ((*db_symformat->sym_numargs)(NULL, cursym, nargp,
457 1.36 ragge argnamep));
458 1.36 ragge #endif
459 1.36 ragge return (FALSE);
460 1.36 ragge }
461 1.36 ragge
462