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