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