2 * SPDX-License-Identifier: MIT-CMU
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
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.
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.
18 * Carnegie Mellon requests users of this software to return to
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
29 * Author: David B. Golub, Carnegie Mellon University
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "opt_kstack_pages.h"
38 #include <sys/param.h>
41 #include <sys/systm.h>
46 #include <ddb/db_sym.h>
47 #include <ddb/db_variables.h>
52 * Multiple symbol tables
55 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
58 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
59 static int db_nsymtab = 0;
61 static db_symtab_t *db_last_symtab; /* where last symbol was found */
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);
68 static int db_cpu = -1;
71 static void *db_vnet = NULL;
75 * Validate the CPU number used to interpret per-CPU variables so we can
76 * avoid later confusion if an invalid CPU is requested.
79 db_var_db_cpu(struct db_variable *vp, db_expr_t *valuep, int op)
88 if (*(int *)valuep < -1 || *(int *)valuep > mp_maxid) {
89 db_printf("Invalid value: %d\n", *(int*)valuep);
92 db_cpu = *(int *)valuep;
96 db_printf("db_var_db_cpu: unknown operation\n");
102 * Read-only variable reporting the current CPU, which is what we use when
103 * db_cpu is set to -1.
106 db_var_curcpu(struct db_variable *vp, db_expr_t *valuep, int op)
115 db_printf("Read-only variable.\n");
119 db_printf("db_var_curcpu: unknown operation\n");
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.
131 db_var_db_vnet(struct db_variable *vp, db_expr_t *valuep, int op)
136 *valuep = (db_expr_t)db_vnet;
140 db_vnet = *(void **)valuep;
144 db_printf("db_var_db_vnet: unknown operation\n");
150 * Read-only variable reporting the current vnet, which is what we use when
151 * db_vnet is set to NULL.
154 db_var_curvnet(struct db_variable *vp, db_expr_t *valuep, int op)
159 *valuep = (db_expr_t)curvnet;
163 db_printf("Read-only variable.\n");
167 db_printf("db_var_curvnet: unknown operation\n");
174 * Add symbol table, with given name, to list of symbol tables.
177 db_add_symbol_table(char *start, char *end, char *name, char *ref)
179 if (db_nsymtab >= MAXNOSYMTABS) {
180 printf ("No slots left for %s symbol table", name);
181 panic ("db_sym.c: db_add_symbol_table");
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;
192 * db_qualify("vm_map", "ux") returns "unix:vm_map".
194 * Note: return value points to static data whose content is
195 * overwritten by each call... but in practice this seems okay.
198 db_qualify(c_db_sym_t sym, char *symtabname)
201 static char tmp[256];
203 db_symbol_values(sym, &symname, 0);
204 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
209 db_eqname(const char *src, const char *dst, int c)
211 if (!strcmp(src, dst))
214 return (!strcmp(src+1,dst));
219 db_value_of_name(const char *name, db_expr_t *valuep)
223 sym = db_lookup(name);
224 if (sym == C_DB_SYM_NULL)
226 db_symbol_values(sym, &name, valuep);
231 db_value_of_name_pcpu(const char *name, db_expr_t *valuep)
233 static char tmp[256];
242 snprintf(tmp, sizeof(tmp), "pcpu_entry_%s", name);
243 sym = db_lookup(tmp);
244 if (sym == C_DB_SYM_NULL)
246 db_symbol_values(sym, &name, &value);
247 if (value < DPCPU_START || value >= DPCPU_STOP)
249 *valuep = (db_expr_t)((uintptr_t)value + dpcpu_off[cpu]);
254 db_value_of_name_vnet(const char *name, db_expr_t *valuep)
257 static char tmp[256];
266 snprintf(tmp, sizeof(tmp), "vnet_entry_%s", name);
267 sym = db_lookup(tmp);
268 if (sym == C_DB_SYM_NULL)
270 db_symbol_values(sym, &name, &value);
271 if (value < VNET_START || value >= VNET_STOP)
273 *valuep = (db_expr_t)((uintptr_t)value + vnet->vnet_data_base);
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.
287 db_lookup(const char *symstr)
291 int symtab_start = 0;
292 int symtab_end = db_nsymtab;
296 * Look for, remove, and remember any symbol table specifier.
298 for (cp = symstr; *cp; cp++) {
300 for (i = 0; i < db_nsymtab; i++) {
301 int n = strlen(db_symtabs[i].name);
304 n == (cp - symstr) &&
305 strncmp(symstr, db_symtabs[i].name, n) == 0
312 if (i == db_nsymtab) {
313 db_error("invalid symbol table name");
320 * Look in the specified set of symbol tables.
321 * Return on first match.
323 for (i = symtab_start; i < symtab_end; i++) {
324 sp = X_db_lookup(&db_symtabs[i], symstr);
326 db_last_symtab = &db_symtabs[i];
334 * If true, check across symbol tables for multiple occurrences
335 * of a name. Might slow things down quite a bit.
337 static volatile bool db_qualify_ambiguous_names = false;
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.
344 db_symbol_is_ambiguous(c_db_sym_t sym)
346 const char *sym_name;
348 bool found_once = false;
350 if (!db_qualify_ambiguous_names)
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)) {
365 * Find the closest symbol to val, and return its name
366 * and the difference between val and the symbol found.
369 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
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.
383 if (val < PAGE_SIZE) {
385 return (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];
403 * Return name and value of a symbol
406 db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
410 if (sym == DB_SYM_NULL) {
415 X_db_symbol_values(db_last_symtab, sym, namep, &value);
417 if (db_symbol_is_ambiguous(sym))
418 *namep = db_qualify(sym, db_last_symtab->name);
424 * Print a the closest symbol to value
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).
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).
439 db_expr_t db_maxoff = 0x10000;
442 db_printsym(db_expr_t off, db_strategy_t strategy)
450 if (off < 0 && off >= -db_maxoff) {
451 db_printf("%+#lr", (long)off);
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);
461 db_printf("%#lr = %s", (unsigned long)off, name);
463 db_printf("%s", name);
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);
474 db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
476 return (X_db_line_at_pc(db_last_symtab, sym, filename, linenum, pc));
480 db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
482 return (X_db_sym_numargs(db_last_symtab, sym, nargp, argnames));