db_sym.c revision 1.68
1/*	$NetBSD: db_sym.c,v 1.68 2021/12/13 01:25:29 chs 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.68 2021/12/13 01:25:29 chs 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
43static void		db_symsplit(char *, char **, char **);
44
45
46#ifndef _KERNEL
47#define	TBLNAME	"netbsd"
48
49#define use_ksyms 0
50
51const db_symformat_t *db_symformat;
52static db_forall_func_t db_sift;
53extern 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 */
62void
63ddb_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
74bool
75db_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
85bool
86db_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(). */
130struct 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 */
140static void
141db_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 */
181void
182db_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 */
211db_sym_t
212db_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 */
255void
256db_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 */
304unsigned int	db_maxoff = 0x100000;
305
306void
307db_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 (strategy & KSYMS_PROC && val == off) {
351			if (ksyms_getname(&mod, &name, (vaddr_t)off - 1,
352					  strategy|KSYMS_CLOSEST) != 0)
353				goto out;
354			(void)ksyms_getval_unlocked(mod, name, NULL, &val, KSYMS_ANY);
355		}
356		if (((off - val) < db_maxoff) && val) {
357			snprintf(buf, buflen, "%s:%s", mod, name);
358			if (off - val) {
359				strlcat(buf, "+", buflen);
360				db_format_radix(buf+strlen(buf),
361				    24, off - val, true);
362			}
363#ifdef notyet
364			if (strategy & KSYMS_PROC) {
365				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
366					snprintf(buf + strlen(buf),
367					    buflen - strlen(buf),
368					    " [%s:%d]", filename, linenum);
369			}
370#endif
371			return;
372		}
373	}
374out:
375	strlcpy(buf, db_num_to_str(off), buflen);
376#endif
377}
378
379void
380db_printsym(db_expr_t off, db_strategy_t strategy,
381    void (*pr)(const char *, ...))
382{
383	const char  *name;
384#ifdef _KERNEL
385	const char *mod;
386	unsigned long uval;
387	long val;
388#endif
389#ifdef notyet
390	char *filename;
391	int  linenum;
392#endif
393
394#ifndef _KERNEL
395	if (!use_ksyms) {
396		db_expr_t	d;
397		char 		*filename;
398		db_expr_t	value;
399		int 		linenum;
400		db_sym_t	cursym;
401
402		cursym = db_search_symbol(off, strategy, &d);
403		db_symbol_values(cursym, &name, &value);
404		if (name != NULL && ((unsigned int)d < db_maxoff) &&
405		    value != 0) {
406			(*pr)("%s", name);
407			if (d) {
408				char tbuf[24];
409
410				db_format_radix(tbuf, 24, d, true);
411				(*pr)("+%s", tbuf);
412			}
413			if (strategy == DB_STGY_PROC) {
414				if ((*db_symformat->sym_line_at_pc)
415				    (NULL, cursym, &filename, &linenum, off))
416					(*pr)(" [%s:%d]", filename, linenum);
417			}
418			return;
419		}
420		(*pr)("%s", db_num_to_str(off));
421		return;
422	}
423#endif
424#ifdef _KERNEL
425	if (ksyms_getname(&mod, &name, (vaddr_t)off,
426	    strategy|KSYMS_CLOSEST) == 0) {
427		(void)ksyms_getval_unlocked(mod, name, NULL, &uval, KSYMS_ANY);
428		if (strategy & KSYMS_PROC && uval == off) {
429			if (ksyms_getname(&mod, &name, (vaddr_t)off - 1,
430					  strategy|KSYMS_CLOSEST) != 0)
431				goto out;
432			(void)ksyms_getval_unlocked(mod, name, NULL, &uval, KSYMS_ANY);
433		}
434		val = (long) uval;
435		if (((off - val) < db_maxoff) && val) {
436			(*pr)("%s:%s", mod, name);
437			if (off - val) {
438				char tbuf[24];
439
440				db_format_radix(tbuf, 24, off - val, true);
441				(*pr)("+%s", tbuf);
442			}
443#ifdef notyet
444			if (strategy & KSYMS_PROC) {
445				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
446					(*pr)(" [%s:%d]", filename, linenum);
447			}
448#endif
449			return;
450		}
451	}
452#endif
453out:
454	(*pr)("%s", db_num_to_str(off));
455	return;
456}
457
458/*
459 * Splits a string in the form "mod:sym" to two strings.
460 */
461static void
462db_symsplit(char *str, char **mod, char **sym)
463{
464	char *cp;
465
466	if ((cp = strchr(str, ':')) != NULL) {
467		*cp++ = '\0';
468		*mod = str;
469		*sym = cp;
470	} else {
471		*mod = NULL;
472		*sym = str;
473	}
474}
475
476bool
477db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep)
478{
479#ifndef _KERNEL
480	if (!use_ksyms)
481		return ((*db_symformat->sym_numargs)(NULL, cursym, nargp,
482		    argnamep));
483#endif
484	return (false);
485}
486
487