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