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