xref: /netbsd-src/sys/ddb/db_sym.c (revision 1ffa7b76c40339c17a0fb2a09fac93f287cfc046)
1 /*	$NetBSD: db_sym.c,v 1.38 2003/04/28 15:55:45 ragge 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.38 2003/04/28 15:55:45 ragge 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 
47 static void		db_symsplit(char *, char **, char **);
48 
49 
50 #ifdef DB_AOUT_SYMBOLS
51 #define	TBLNAME	"netbsd"
52 
53 static int using_aout_symtab;
54 const db_symformat_t *db_symformat;
55 static db_forall_func_t db_sift;
56 extern 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  */
65 void
66 ddb_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 
78 boolean_t
79 db_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 
89 boolean_t
90 db_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(). */
119 struct 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  */
129 static void
130 db_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  */
170 void
171 db_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  */
196 db_sym_t
197 db_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 	char *mod, *sym;
203 
204 #ifdef DB_AOUT_SYMBOLS
205 	db_expr_t newdiff;
206 	db_sym_t ssym;
207 
208 	if (using_aout_symtab) {
209 		newdiff = diff = ~0;
210 		ssym = (*db_symformat->sym_search)
211 		    (NULL, val, strategy, &newdiff);
212 		if ((unsigned int) newdiff < diff) {
213 			diff = newdiff;
214 			ret = ssym;
215 		}
216 		*offp = diff;
217 		return ret;
218 	}
219 #endif
220 
221 	if (ksyms_getname(&mod, &sym, val, strategy) == 0) {
222 		(void)ksyms_getval(mod, sym, &naddr, KSYMS_ANY);
223 		diff = val - naddr;
224 		ret = naddr;
225 	}
226 	*offp = diff;
227 	return ret;
228 }
229 
230 /*
231  * Return name and value of a symbol
232  */
233 void
234 db_symbol_values(db_sym_t sym, char **namep, db_expr_t *valuep)
235 {
236 	char *mod;
237 
238 	if (sym == DB_SYM_NULL) {
239 		*namep = 0;
240 		return;
241 	}
242 
243 #ifdef DB_AOUT_SYMBOLS
244 	if (using_aout_symtab) {
245 		db_expr_t value;
246 		(*db_symformat->sym_value)(NULL, sym, namep, &value);
247 		if (valuep)
248 			*valuep = value;
249 		return;
250 	}
251 #endif
252 
253 	if (ksyms_getname(&mod, namep, sym, KSYMS_ANY|KSYMS_EXACT) == 0) {
254 		if (valuep)
255 			*valuep = sym;
256 	} else
257 		*namep = NULL;
258 }
259 
260 
261 /*
262  * Print a the closest symbol to value
263  *
264  * After matching the symbol according to the given strategy
265  * we print it in the name+offset format, provided the symbol's
266  * value is close enough (eg smaller than db_maxoff).
267  * We also attempt to print [filename:linenum] when applicable
268  * (eg for procedure names).
269  *
270  * If we could not find a reasonable name+offset representation,
271  * then we just print the value in hex.  Small values might get
272  * bogus symbol associations, e.g. 3 might get some absolute
273  * value like _INCLUDE_VERSION or something, therefore we do
274  * not accept symbols whose value is zero (and use plain hex).
275  * Also, avoid printing as "end+0x????" which is useless.
276  * The variable db_lastsym is used instead of "end" in case we
277  * add support for symbols in loadable driver modules.
278  */
279 extern char end[];
280 unsigned long	db_lastsym = (unsigned long)end;
281 unsigned int	db_maxoff = 0x10000000;
282 
283 void
284 db_symstr(char *buf, db_expr_t off, db_strategy_t strategy)
285 {
286 	char  *name, *mod;
287 	long val;
288 
289 #ifdef DB_AOUT_SYMBOLS
290 	if (using_aout_symtab) {
291 		db_expr_t	d;
292 		char 		*filename;
293 		char		*name;
294 		db_expr_t	value;
295 		int 		linenum;
296 		db_sym_t	cursym;
297 
298 		if ((unsigned long) off <= db_lastsym) {
299 			cursym = db_search_symbol(off, strategy, &d);
300 			db_symbol_values(cursym, &name, &value);
301 			if (name != NULL &&
302 			    ((unsigned int) d < db_maxoff) &&
303 			    value != 0) {
304 				strcpy(buf, name);
305 				if (d) {
306 					strcat(buf, "+");
307 					db_format_radix(buf+strlen(buf),
308 					    24, d, TRUE);
309 				}
310 				if (strategy == DB_STGY_PROC) {
311 					if ((*db_symformat->sym_line_at_pc)
312 					    (NULL, cursym, &filename,
313 					    &linenum, off))
314 						sprintf(buf+strlen(buf),
315 						    " [%s:%d]",
316 						    filename, linenum);
317 				}
318 				return;
319 			}
320 		}
321 		strcpy(buf, db_num_to_str(off));
322 		return;
323 	}
324 #endif
325 	if (ksyms_getname(&mod, &name, off, strategy|KSYMS_CLOSEST) == 0) {
326 		(void)ksyms_getval(mod, name, &val, KSYMS_ANY);
327 		if (((off - val) < db_maxoff) && val) {
328 			sprintf(buf, "%s:%s", mod, name);
329 			if (off - val) {
330 				strcat(buf, "+");
331 				db_format_radix(buf+strlen(buf),
332 				    24, off - val, TRUE);
333 			}
334 #ifdef notyet
335 			if (strategy & KSYMS_PROC) {
336 				if (ksyms_fmaddr(off, &filename, &linenum) == 0)					sprintf(buf+strlen(buf),
337 					    " [%s:%d]", filename, linenum);
338 			}
339 #endif
340 			return;
341 		}
342 	}
343 	strcpy(buf, db_num_to_str(off));
344 }
345 
346 void
347 db_printsym(db_expr_t off, db_strategy_t strategy,
348     void (*pr)(const char *, ...))
349 {
350 	char  *name, *mod;
351 	long val;
352 #ifdef notyet
353 	char *filename;
354 	int  linenum;
355 #endif
356 
357 #ifdef DB_AOUT_SYMBOLS
358 	if (using_aout_symtab) {
359 		db_expr_t	d;
360 		char 		*filename;
361 		char		*name;
362 		db_expr_t	value;
363 		int 		linenum;
364 		db_sym_t	cursym;
365 		if ((unsigned long) off <= db_lastsym) {
366 			cursym = db_search_symbol(off, strategy, &d);
367 			db_symbol_values(cursym, &name, &value);
368 			if (name != NULL &&
369 			    ((unsigned int) d < db_maxoff) &&
370 			    value != 0) {
371 				(*pr)("%s", name);
372 				if (d) {
373 					char tbuf[24];
374 
375 					db_format_radix(tbuf, 24, d, TRUE);
376 					(*pr)("+%s", tbuf);
377 				}
378 				if (strategy == DB_STGY_PROC) {
379 					if ((*db_symformat->sym_line_at_pc)
380 					    (NULL, cursym, &filename,
381 					    &linenum, off))
382 						(*pr)(" [%s:%d]",
383 						    filename, linenum);
384 				}
385 				return;
386 			}
387 		}
388 		(*pr)(db_num_to_str(off));
389 		return;
390 	}
391 #endif
392 	if (ksyms_getname(&mod, &name, off, strategy|KSYMS_CLOSEST) == 0) {
393 		(void)ksyms_getval(mod, name, &val, KSYMS_ANY);
394 		if (((off - val) < db_maxoff) && val) {
395 			(*pr)("%s:%s", mod, name);
396 			if (off - val) {
397 				char tbuf[24];
398 
399 				db_format_radix(tbuf, 24, off - val, TRUE);
400 				(*pr)("+%s", tbuf);
401 			}
402 #ifdef notyet
403 			if (strategy & KSYMS_PROC) {
404 				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
405 					(*pr)(" [%s:%d]", filename, linenum);
406 			}
407 #endif
408 			return;
409 		}
410 	}
411 	(*pr)(db_num_to_str(off));
412 	return;
413 }
414 
415 /*
416  * Splits a string in the form "mod:sym" to two strings.
417  */
418 static void
419 db_symsplit(char *str, char **mod, char **sym)
420 {
421 	char *cp;
422 
423 	if ((cp = strchr(str, ':')) != NULL) {
424 		*cp++ = '\0';
425 		*mod = str;
426 		*sym = cp;
427 	} else {
428 		*mod = NULL;
429 		*sym = str;
430 	}
431 }
432 
433 boolean_t
434 db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep)
435 {
436 #ifdef DB_AOUT_SYMBOLS
437 	if (using_aout_symtab)
438 		return ((*db_symformat->sym_numargs)(NULL, cursym, nargp,
439 		    argnamep));
440 #endif
441 	return (FALSE);
442 }
443 
444