2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
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.
16 * Carnegie Mellon requests users of this software to return to
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
27 * Author: David B. Golub, Carnegie Mellon University
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
37 #include <sys/systm.h>
42 #include <ddb/db_sym.h>
43 #include <ddb/db_variables.h>
48 * Multiple symbol tables
51 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
54 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
55 static int db_nsymtab = 0;
57 static db_symtab_t *db_last_symtab; /* where last symbol was found */
59 static c_db_sym_t db_lookup( const char *symstr);
60 static char *db_qualify(c_db_sym_t sym, char *symtabname);
61 static boolean_t db_symbol_is_ambiguous(c_db_sym_t sym);
62 static boolean_t db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
64 static int db_cpu = -1;
67 extern uintptr_t *__start_set_vnet;
68 extern uintptr_t *__stop_set_vnet;
70 #define VNET_START (uintptr_t)&__start_set_vnet
71 #define VNET_STOP (uintptr_t)&__stop_set_vnet
73 static void *db_vnet = NULL;
77 * Validate the CPU number used to interpret per-CPU variables so we can
78 * avoid later confusion if an invalid CPU is requested.
81 db_var_db_cpu(struct db_variable *vp, db_expr_t *valuep, int op)
90 if (*(int *)valuep < -1 && *(int *)valuep > mp_maxid) {
91 db_printf("Invalid value: %d", *(int*)valuep);
94 db_cpu = *(int *)valuep;
98 db_printf("db_var_db_cpu: unknown operation\n");
104 * Read-only variable reporting the current CPU, which is what we use when
105 * db_cpu is set to -1.
108 db_var_curcpu(struct db_variable *vp, db_expr_t *valuep, int op)
117 db_printf("Read-only variable.\n");
121 db_printf("db_var_curcpu: unknown operation\n");
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.
133 db_var_db_vnet(struct db_variable *vp, db_expr_t *valuep, int op)
138 *valuep = (db_expr_t)db_vnet;
142 db_vnet = *(void **)valuep;
146 db_printf("db_var_db_vnet: unknown operation\n");
152 * Read-only variable reporting the current vnet, which is what we use when
153 * db_vnet is set to NULL.
156 db_var_curvnet(struct db_variable *vp, db_expr_t *valuep, int op)
161 *valuep = (db_expr_t)curvnet;
165 db_printf("Read-only variable.\n");
169 db_printf("db_var_curcpu: unknown operation\n");
176 * Add symbol table, with given name, to list of symbol tables.
179 db_add_symbol_table(start, end, name, ref)
185 if (db_nsymtab >= MAXNOSYMTABS) {
186 printf ("No slots left for %s symbol table", name);
187 panic ("db_sym.c: db_add_symbol_table");
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;
198 * db_qualify("vm_map", "ux") returns "unix:vm_map".
200 * Note: return value points to static data whose content is
201 * overwritten by each call... but in practice this seems okay.
204 db_qualify(sym, symtabname)
206 register char *symtabname;
209 static char tmp[256];
211 db_symbol_values(sym, &symname, 0);
212 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
218 db_eqname(src, dst, c)
223 if (!strcmp(src, dst))
226 return (!strcmp(src+1,dst));
231 db_value_of_name(name, valuep)
237 sym = db_lookup(name);
238 if (sym == C_DB_SYM_NULL)
240 db_symbol_values(sym, &name, valuep);
245 db_value_of_name_pcpu(name, valuep)
249 static char tmp[256];
258 snprintf(tmp, sizeof(tmp), "pcpu_entry_%s", name);
259 sym = db_lookup(tmp);
260 if (sym == C_DB_SYM_NULL)
262 db_symbol_values(sym, &name, &value);
263 if (value < DPCPU_START || value >= DPCPU_STOP)
265 *valuep = (db_expr_t)((uintptr_t)value + dpcpu_off[cpu]);
270 db_value_of_name_vnet(name, valuep)
275 static char tmp[256];
284 snprintf(tmp, sizeof(tmp), "vnet_entry_%s", name);
285 sym = db_lookup(tmp);
286 if (sym == C_DB_SYM_NULL)
288 db_symbol_values(sym, &name, &value);
289 if (value < VNET_START || value >= VNET_STOP)
291 *valuep = (db_expr_t)((uintptr_t)value + vnet->vnet_data_base);
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.
310 int symtab_start = 0;
311 int symtab_end = db_nsymtab;
312 register const char *cp;
315 * Look for, remove, and remember any symbol table specifier.
317 for (cp = symstr; *cp; cp++) {
319 for (i = 0; i < db_nsymtab; i++) {
320 int n = strlen(db_symtabs[i].name);
323 n == (cp - symstr) &&
324 strncmp(symstr, db_symtabs[i].name, n) == 0
331 if (i == db_nsymtab) {
332 db_error("invalid symbol table name");
339 * Look in the specified set of symbol tables.
340 * Return on first match.
342 for (i = symtab_start; i < symtab_end; i++) {
343 sp = X_db_lookup(&db_symtabs[i], symstr);
345 db_last_symtab = &db_symtabs[i];
353 * If TRUE, check across symbol tables for multiple occurrences
354 * of a name. Might slow things down quite a bit.
356 static volatile boolean_t db_qualify_ambiguous_names = FALSE;
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.
363 db_symbol_is_ambiguous(sym)
366 const char *sym_name;
369 boolean_t found_once = FALSE;
371 if (!db_qualify_ambiguous_names)
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)) {
386 * Find the closest symbol to val, and return its name
387 * and the difference between val and the symbol found.
390 db_search_symbol( val, strategy, offp)
391 register db_addr_t val;
392 db_strategy_t strategy;
399 c_db_sym_t ret = C_DB_SYM_NULL, sym;
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];
415 * Return name and value of a symbol
418 db_symbol_values(sym, namep, valuep)
425 if (sym == DB_SYM_NULL) {
430 X_db_symbol_values(db_last_symtab, sym, namep, &value);
432 if (db_symbol_is_ambiguous(sym))
433 *namep = db_qualify(sym, db_last_symtab->name);
440 * Print a the closest symbol to value
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).
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).
455 db_expr_t db_maxoff = 0x10000;
458 db_printsym(off, strategy)
460 db_strategy_t strategy;
469 cursym = db_search_symbol(off, strategy, &d);
470 db_symbol_values(cursym, &name, &value);
473 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
474 db_printf("%+#lr", (long)off);
477 if (name == 0 || d >= (unsigned long)db_maxoff) {
478 db_printf("%#lr", (unsigned long)off);
482 db_printf("%#lr = %s", (unsigned long)off, name);
484 db_printf("%s", name);
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);
495 db_line_at_pc( sym, filename, linenum, pc)
501 return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
505 db_sym_numargs(sym, nargp, argnames)
510 return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);