xref: /freebsd/sys/ddb/db_sym.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
1 /*-
2  * SPDX-License-Identifier: MIT-CMU
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
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  * 	Author: David B. Golub, Carnegie Mellon University
30  *	Date:	7/90
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_kstack_pages.h"
37 
38 #include <sys/param.h>
39 #include <sys/pcpu.h>
40 #include <sys/smp.h>
41 #include <sys/systm.h>
42 
43 #include <net/vnet.h>
44 
45 #include <ddb/ddb.h>
46 #include <ddb/db_sym.h>
47 #include <ddb/db_variables.h>
48 
49 #include "opt_ddb.h"
50 
51 /*
52  * Multiple symbol tables
53  */
54 #ifndef MAXNOSYMTABS
55 #define	MAXNOSYMTABS	3	/* mach, ux, emulator */
56 #endif
57 
58 static db_symtab_t	db_symtabs[MAXNOSYMTABS] = {{0,},};
59 static int db_nsymtab = 0;
60 
61 static db_symtab_t	*db_last_symtab; /* where last symbol was found */
62 
63 static c_db_sym_t	db_lookup( const char *symstr);
64 static char		*db_qualify(c_db_sym_t sym, char *symtabname);
65 static bool		db_symbol_is_ambiguous(c_db_sym_t sym);
66 static bool		db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
67 
68 static int db_cpu = -1;
69 
70 #ifdef VIMAGE
71 static void *db_vnet = NULL;
72 #endif
73 
74 /*
75  * Validate the CPU number used to interpret per-CPU variables so we can
76  * avoid later confusion if an invalid CPU is requested.
77  */
78 int
79 db_var_db_cpu(struct db_variable *vp, db_expr_t *valuep, int op)
80 {
81 
82 	switch (op) {
83 	case DB_VAR_GET:
84 		*valuep = db_cpu;
85 		return (1);
86 
87 	case DB_VAR_SET:
88 		if (*(int *)valuep < -1 || *(int *)valuep > mp_maxid) {
89 			db_printf("Invalid value: %d\n", *(int*)valuep);
90 			return (0);
91 		}
92 		db_cpu = *(int *)valuep;
93 		return (1);
94 
95 	default:
96 		db_printf("db_var_db_cpu: unknown operation\n");
97 		return (0);
98 	}
99 }
100 
101 /*
102  * Read-only variable reporting the current CPU, which is what we use when
103  * db_cpu is set to -1.
104  */
105 int
106 db_var_curcpu(struct db_variable *vp, db_expr_t *valuep, int op)
107 {
108 
109 	switch (op) {
110 	case DB_VAR_GET:
111 		*valuep = curcpu;
112 		return (1);
113 
114 	case DB_VAR_SET:
115 		db_printf("Read-only variable.\n");
116 		return (0);
117 
118 	default:
119 		db_printf("db_var_curcpu: unknown operation\n");
120 		return (0);
121 	}
122 }
123 
124 #ifdef VIMAGE
125 /*
126  * Validate the virtual network pointer used to interpret per-vnet global
127  * variable expansion.  Right now we don't do much here, really we should
128  * walk the global vnet list to check it's an OK pointer.
129  */
130 int
131 db_var_db_vnet(struct db_variable *vp, db_expr_t *valuep, int op)
132 {
133 
134 	switch (op) {
135 	case DB_VAR_GET:
136 		*valuep = (db_expr_t)db_vnet;
137 		return (1);
138 
139 	case DB_VAR_SET:
140 		db_vnet = *(void **)valuep;
141 		return (1);
142 
143 	default:
144 		db_printf("db_var_db_vnet: unknown operation\n");
145 		return (0);
146 	}
147 }
148 
149 /*
150  * Read-only variable reporting the current vnet, which is what we use when
151  * db_vnet is set to NULL.
152  */
153 int
154 db_var_curvnet(struct db_variable *vp, db_expr_t *valuep, int op)
155 {
156 
157 	switch (op) {
158 	case DB_VAR_GET:
159 		*valuep = (db_expr_t)curvnet;
160 		return (1);
161 
162 	case DB_VAR_SET:
163 		db_printf("Read-only variable.\n");
164 		return (0);
165 
166 	default:
167 		db_printf("db_var_curvnet: unknown operation\n");
168 		return (0);
169 	}
170 }
171 #endif
172 
173 /*
174  * Add symbol table, with given name, to list of symbol tables.
175  */
176 void
177 db_add_symbol_table(char *start, char *end, char *name, char *ref)
178 {
179 	if (db_nsymtab >= MAXNOSYMTABS) {
180 		printf ("No slots left for %s symbol table", name);
181 		panic ("db_sym.c: db_add_symbol_table");
182 	}
183 
184 	db_symtabs[db_nsymtab].start = start;
185 	db_symtabs[db_nsymtab].end = end;
186 	db_symtabs[db_nsymtab].name = name;
187 	db_symtabs[db_nsymtab].private = ref;
188 	db_nsymtab++;
189 }
190 
191 /*
192  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
193  *
194  *  Note: return value points to static data whose content is
195  *  overwritten by each call... but in practice this seems okay.
196  */
197 static char *
198 db_qualify(c_db_sym_t sym, char *symtabname)
199 {
200 	const char	*symname;
201 	static char     tmp[256];
202 
203 	db_symbol_values(sym, &symname, 0);
204 	snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
205 	return tmp;
206 }
207 
208 bool
209 db_eqname(const char *src, const char *dst, int c)
210 {
211 	if (!strcmp(src, dst))
212 	    return (true);
213 	if (src[0] == c)
214 	    return (!strcmp(src+1,dst));
215 	return (false);
216 }
217 
218 bool
219 db_value_of_name(const char *name, db_expr_t *valuep)
220 {
221 	c_db_sym_t	sym;
222 
223 	sym = db_lookup(name);
224 	if (sym == C_DB_SYM_NULL)
225 	    return (false);
226 	db_symbol_values(sym, &name, valuep);
227 	return (true);
228 }
229 
230 bool
231 db_value_of_name_pcpu(const char *name, db_expr_t *valuep)
232 {
233 	static char     tmp[256];
234 	db_expr_t	value;
235 	c_db_sym_t	sym;
236 	int		cpu;
237 
238 	if (db_cpu != -1)
239 		cpu = db_cpu;
240 	else
241 		cpu = curcpu;
242 	snprintf(tmp, sizeof(tmp), "pcpu_entry_%s", name);
243 	sym = db_lookup(tmp);
244 	if (sym == C_DB_SYM_NULL)
245 		return (false);
246 	db_symbol_values(sym, &name, &value);
247 	if (value < DPCPU_START || value >= DPCPU_STOP)
248 		return (false);
249 	*valuep = (db_expr_t)((uintptr_t)value + dpcpu_off[cpu]);
250 	return (true);
251 }
252 
253 bool
254 db_value_of_name_vnet(const char *name, db_expr_t *valuep)
255 {
256 #ifdef VIMAGE
257 	static char     tmp[256];
258 	db_expr_t	value;
259 	c_db_sym_t	sym;
260 	struct vnet	*vnet;
261 
262 	if (db_vnet != NULL)
263 		vnet = db_vnet;
264 	else
265 		vnet = curvnet;
266 	snprintf(tmp, sizeof(tmp), "vnet_entry_%s", name);
267 	sym = db_lookup(tmp);
268 	if (sym == C_DB_SYM_NULL)
269 		return (false);
270 	db_symbol_values(sym, &name, &value);
271 	if (value < VNET_START || value >= VNET_STOP)
272 		return (false);
273 	*valuep = (db_expr_t)((uintptr_t)value + vnet->vnet_data_base);
274 	return (true);
275 #else
276 	return (false);
277 #endif
278 }
279 
280 /*
281  * Lookup a symbol.
282  * If the symbol has a qualifier (e.g., ux:vm_map),
283  * then only the specified symbol table will be searched;
284  * otherwise, all symbol tables will be searched.
285  */
286 static c_db_sym_t
287 db_lookup(const char *symstr)
288 {
289 	c_db_sym_t sp;
290 	int i;
291 	int symtab_start = 0;
292 	int symtab_end = db_nsymtab;
293 	const char *cp;
294 
295 	/*
296 	 * Look for, remove, and remember any symbol table specifier.
297 	 */
298 	for (cp = symstr; *cp; cp++) {
299 		if (*cp == ':') {
300 			for (i = 0; i < db_nsymtab; i++) {
301 				int n = strlen(db_symtabs[i].name);
302 
303 				if (
304 				    n == (cp - symstr) &&
305 				    strncmp(symstr, db_symtabs[i].name, n) == 0
306 				) {
307 					symtab_start = i;
308 					symtab_end = i + 1;
309 					break;
310 				}
311 			}
312 			if (i == db_nsymtab) {
313 				db_error("invalid symbol table name");
314 			}
315 			symstr = cp+1;
316 		}
317 	}
318 
319 	/*
320 	 * Look in the specified set of symbol tables.
321 	 * Return on first match.
322 	 */
323 	for (i = symtab_start; i < symtab_end; i++) {
324 		sp = X_db_lookup(&db_symtabs[i], symstr);
325 		if (sp) {
326 			db_last_symtab = &db_symtabs[i];
327 			return sp;
328 		}
329 	}
330 	return 0;
331 }
332 
333 /*
334  * If true, check across symbol tables for multiple occurrences
335  * of a name.  Might slow things down quite a bit.
336  */
337 static volatile bool db_qualify_ambiguous_names = false;
338 
339 /*
340  * Does this symbol name appear in more than one symbol table?
341  * Used by db_symbol_values to decide whether to qualify a symbol.
342  */
343 static bool
344 db_symbol_is_ambiguous(c_db_sym_t sym)
345 {
346 	const char	*sym_name;
347 	int		i;
348 	bool		found_once = false;
349 
350 	if (!db_qualify_ambiguous_names)
351 		return (false);
352 
353 	db_symbol_values(sym, &sym_name, 0);
354 	for (i = 0; i < db_nsymtab; i++) {
355 		if (X_db_lookup(&db_symtabs[i], sym_name)) {
356 			if (found_once)
357 				return (true);
358 			found_once = true;
359 		}
360 	}
361 	return (false);
362 }
363 
364 /*
365  * Find the closest symbol to val, and return its name
366  * and the difference between val and the symbol found.
367  */
368 c_db_sym_t
369 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
370 {
371 	unsigned int	diff;
372 	size_t		newdiff;
373 	int		i;
374 	c_db_sym_t	ret, sym;
375 
376 	/*
377 	 * The kernel will never map the first page, so any symbols in that
378 	 * range cannot refer to addresses.  Some third-party assembly files
379 	 * define internal constants which appear in their symbol table.
380 	 * Avoiding the lookup for those symbols avoids replacing small offsets
381 	 * with those symbols during disassembly.
382 	 */
383 	if (val < PAGE_SIZE) {
384 		*offp = 0;
385 		return (C_DB_SYM_NULL);
386 	}
387 
388 	ret = C_DB_SYM_NULL;
389 	newdiff = diff = val;
390 	for (i = 0; i < db_nsymtab; i++) {
391 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
392 	    if ((uintmax_t)newdiff < (uintmax_t)diff) {
393 		db_last_symtab = &db_symtabs[i];
394 		diff = newdiff;
395 		ret = sym;
396 	    }
397 	}
398 	*offp = diff;
399 	return ret;
400 }
401 
402 /*
403  * Return name and value of a symbol
404  */
405 void
406 db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
407 {
408 	db_expr_t	value;
409 
410 	if (sym == DB_SYM_NULL) {
411 		*namep = NULL;
412 		return;
413 	}
414 
415 	X_db_symbol_values(db_last_symtab, sym, namep, &value);
416 
417 	if (db_symbol_is_ambiguous(sym))
418 		*namep = db_qualify(sym, db_last_symtab->name);
419 	if (valuep)
420 		*valuep = value;
421 }
422 
423 /*
424  * Print a the closest symbol to value
425  *
426  * After matching the symbol according to the given strategy
427  * we print it in the name+offset format, provided the symbol's
428  * value is close enough (eg smaller than db_maxoff).
429  * We also attempt to print [filename:linenum] when applicable
430  * (eg for procedure names).
431  *
432  * If we could not find a reasonable name+offset representation,
433  * then we just print the value in hex.  Small values might get
434  * bogus symbol associations, e.g. 3 might get some absolute
435  * value like _INCLUDE_VERSION or something, therefore we do
436  * not accept symbols whose value is "small" (and use plain hex).
437  */
438 
439 db_expr_t	db_maxoff = 0x10000;
440 
441 void
442 db_printsym(db_expr_t off, db_strategy_t strategy)
443 {
444 	db_expr_t	d;
445 	char 		*filename;
446 	const char	*name;
447 	int 		linenum;
448 	c_db_sym_t	cursym;
449 
450 	if (off < 0 && off >= -db_maxoff) {
451 		db_printf("%+#lr", (long)off);
452 		return;
453 	}
454 	cursym = db_search_symbol(off, strategy, &d);
455 	db_symbol_values(cursym, &name, NULL);
456 	if (name == NULL || d >= (db_addr_t)db_maxoff) {
457 		db_printf("%#lr", (unsigned long)off);
458 		return;
459 	}
460 #ifdef DDB_NUMSYM
461 	db_printf("%#lr = %s", (unsigned long)off, name);
462 #else
463 	db_printf("%s", name);
464 #endif
465 	if (d)
466 		db_printf("+%+#lr", (long)d);
467 	if (strategy == DB_STGY_PROC) {
468 		if (db_line_at_pc(cursym, &filename, &linenum, off))
469 			db_printf(" [%s:%d]", filename, linenum);
470 	}
471 }
472 
473 static bool
474 db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
475 {
476 	return (X_db_line_at_pc(db_last_symtab, sym, filename, linenum, pc));
477 }
478 
479 bool
480 db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
481 {
482 	return (X_db_sym_numargs(db_last_symtab, sym, nargp, argnames));
483 }
484