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