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