xref: /freebsd/sys/ddb/db_sym.c (revision eb69d1f144a6fcc765d1b9d44a5ae8082353e70b)
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 
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 = C_DB_SYM_NULL, sym;
376 
377 	newdiff = diff = val;
378 	for (i = 0; i < db_nsymtab; i++) {
379 	    sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
380 	    if ((uintmax_t)newdiff < (uintmax_t)diff) {
381 		db_last_symtab = &db_symtabs[i];
382 		diff = newdiff;
383 		ret = sym;
384 	    }
385 	}
386 	*offp = diff;
387 	return ret;
388 }
389 
390 /*
391  * Return name and value of a symbol
392  */
393 void
394 db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
395 {
396 	db_expr_t	value;
397 
398 	if (sym == DB_SYM_NULL) {
399 		*namep = NULL;
400 		return;
401 	}
402 
403 	X_db_symbol_values(db_last_symtab, sym, namep, &value);
404 
405 	if (db_symbol_is_ambiguous(sym))
406 		*namep = db_qualify(sym, db_last_symtab->name);
407 	if (valuep)
408 		*valuep = value;
409 }
410 
411 
412 /*
413  * Print a the closest symbol to value
414  *
415  * After matching the symbol according to the given strategy
416  * we print it in the name+offset format, provided the symbol's
417  * value is close enough (eg smaller than db_maxoff).
418  * We also attempt to print [filename:linenum] when applicable
419  * (eg for procedure names).
420  *
421  * If we could not find a reasonable name+offset representation,
422  * then we just print the value in hex.  Small values might get
423  * bogus symbol associations, e.g. 3 might get some absolute
424  * value like _INCLUDE_VERSION or something, therefore we do
425  * not accept symbols whose value is "small" (and use plain hex).
426  */
427 
428 db_expr_t	db_maxoff = 0x10000;
429 
430 void
431 db_printsym(db_expr_t off, db_strategy_t strategy)
432 {
433 	db_expr_t	d;
434 	char 		*filename;
435 	const char	*name;
436 	int 		linenum;
437 	c_db_sym_t	cursym;
438 
439 	if (off < 0 && off >= -db_maxoff) {
440 		db_printf("%+#lr", (long)off);
441 		return;
442 	}
443 	cursym = db_search_symbol(off, strategy, &d);
444 	db_symbol_values(cursym, &name, NULL);
445 	if (name == NULL || d >= (db_addr_t)db_maxoff) {
446 		db_printf("%#lr", (unsigned long)off);
447 		return;
448 	}
449 #ifdef DDB_NUMSYM
450 	db_printf("%#lr = %s", (unsigned long)off, name);
451 #else
452 	db_printf("%s", name);
453 #endif
454 	if (d)
455 		db_printf("+%+#lr", (long)d);
456 	if (strategy == DB_STGY_PROC) {
457 		if (db_line_at_pc(cursym, &filename, &linenum, off))
458 			db_printf(" [%s:%d]", filename, linenum);
459 	}
460 }
461 
462 static bool
463 db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
464 {
465 	return (X_db_line_at_pc(db_last_symtab, sym, filename, linenum, pc));
466 }
467 
468 bool
469 db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
470 {
471 	return (X_db_sym_numargs(db_last_symtab, sym, nargp, argnames));
472 }
473