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>
39 #include <sys/systm.h>
43 #include <sys/sysent.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_variables.h>
54 * Multiple symbol tables
57 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
60 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
61 static int db_nsymtab = 0;
63 static db_symtab_t *db_last_symtab; /* where last symbol was found */
65 static c_db_sym_t db_lookup( const char *symstr);
66 static char *db_qualify(c_db_sym_t sym, char *symtabname);
67 static bool db_symbol_is_ambiguous(c_db_sym_t sym);
68 static bool db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
70 static int db_cpu = -1;
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\n", *(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_curvnet: unknown operation\n");
176 * Add symbol table, with given name, to list of symbol tables.
179 db_add_symbol_table(char *start, char *end, char *name, char *ref)
181 if (db_nsymtab >= MAXNOSYMTABS) {
182 printf ("No slots left for %s symbol table", name);
183 panic ("db_sym.c: db_add_symbol_table");
186 db_symtabs[db_nsymtab].start = start;
187 db_symtabs[db_nsymtab].end = end;
188 db_symtabs[db_nsymtab].name = name;
189 db_symtabs[db_nsymtab].private = ref;
194 * db_qualify("vm_map", "ux") returns "unix:vm_map".
196 * Note: return value points to static data whose content is
197 * overwritten by each call... but in practice this seems okay.
200 db_qualify(c_db_sym_t sym, char *symtabname)
203 static char tmp[256];
205 db_symbol_values(sym, &symname, 0);
206 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
211 db_eqname(const char *src, const char *dst, int c)
213 if (!strcmp(src, dst))
216 return (!strcmp(src+1,dst));
221 db_value_of_name(const char *name, db_expr_t *valuep)
225 sym = db_lookup(name);
226 if (sym == C_DB_SYM_NULL)
228 db_symbol_values(sym, &name, valuep);
233 db_value_of_name_pcpu(const char *name, db_expr_t *valuep)
235 static char tmp[256];
244 snprintf(tmp, sizeof(tmp), "pcpu_entry_%s", name);
245 sym = db_lookup(tmp);
246 if (sym == C_DB_SYM_NULL)
248 db_symbol_values(sym, &name, &value);
249 if (value < DPCPU_START || value >= DPCPU_STOP)
251 *valuep = (db_expr_t)((uintptr_t)value + dpcpu_off[cpu]);
256 db_value_of_name_vnet(const char *name, db_expr_t *valuep)
259 static char tmp[256];
268 snprintf(tmp, sizeof(tmp), "vnet_entry_%s", name);
269 sym = db_lookup(tmp);
270 if (sym == C_DB_SYM_NULL)
272 db_symbol_values(sym, &name, &value);
273 if (value < VNET_START || value >= VNET_STOP)
275 *valuep = (db_expr_t)((uintptr_t)value + vnet->vnet_data_base);
284 * If the symbol has a qualifier (e.g., ux:vm_map),
285 * then only the specified symbol table will be searched;
286 * otherwise, all symbol tables will be searched.
289 db_lookup(const char *symstr)
293 int symtab_start = 0;
294 int symtab_end = db_nsymtab;
298 * Look for, remove, and remember any symbol table specifier.
300 for (cp = symstr; *cp; cp++) {
302 for (i = 0; i < db_nsymtab; i++) {
303 int n = strlen(db_symtabs[i].name);
306 n == (cp - symstr) &&
307 strncmp(symstr, db_symtabs[i].name, n) == 0
314 if (i == db_nsymtab) {
315 db_error("invalid symbol table name");
322 * Look in the specified set of symbol tables.
323 * Return on first match.
325 for (i = symtab_start; i < symtab_end; i++) {
326 sp = X_db_lookup(&db_symtabs[i], symstr);
328 db_last_symtab = &db_symtabs[i];
336 * If true, check across symbol tables for multiple occurrences
337 * of a name. Might slow things down quite a bit.
339 static volatile bool db_qualify_ambiguous_names = false;
342 * Does this symbol name appear in more than one symbol table?
343 * Used by db_symbol_values to decide whether to qualify a symbol.
346 db_symbol_is_ambiguous(c_db_sym_t sym)
348 const char *sym_name;
350 bool found_once = false;
352 if (!db_qualify_ambiguous_names)
355 db_symbol_values(sym, &sym_name, 0);
356 for (i = 0; i < db_nsymtab; i++) {
357 if (X_db_lookup(&db_symtabs[i], sym_name)) {
367 * Find the closest symbol to val, and return its name
368 * and the difference between val and the symbol found.
371 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
379 * The kernel will never map the first page, so any symbols in that
380 * range cannot refer to addresses. Some third-party assembly files
381 * define internal constants which appear in their symbol table.
382 * Avoiding the lookup for those symbols avoids replacing small offsets
383 * with those symbols during disassembly.
385 if (val < PAGE_SIZE) {
387 return (C_DB_SYM_NULL);
391 newdiff = diff = val;
392 for (i = 0; i < db_nsymtab; i++) {
393 sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
394 if ((uintmax_t)newdiff < (uintmax_t)diff) {
395 db_last_symtab = &db_symtabs[i];
405 * Return name and value of a symbol
408 db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
412 if (sym == DB_SYM_NULL) {
417 X_db_symbol_values(db_last_symtab, sym, namep, &value);
419 if (db_symbol_is_ambiguous(sym))
420 *namep = db_qualify(sym, db_last_symtab->name);
426 * Print a the closest symbol to value
428 * After matching the symbol according to the given strategy
429 * we print it in the name+offset format, provided the symbol's
430 * value is close enough (eg smaller than db_maxoff).
431 * We also attempt to print [filename:linenum] when applicable
432 * (eg for procedure names).
434 * If we could not find a reasonable name+offset representation,
435 * then we just print the value in hex. Small values might get
436 * bogus symbol associations, e.g. 3 might get some absolute
437 * value like _INCLUDE_VERSION or something, therefore we do
438 * not accept symbols whose value is "small" (and use plain hex).
441 db_expr_t db_maxoff = 0x10000;
444 db_printsym(db_expr_t off, db_strategy_t strategy)
452 if (off < 0 && off >= -db_maxoff) {
453 db_printf("%+#lr", (long)off);
456 cursym = db_search_symbol(off, strategy, &d);
457 db_symbol_values(cursym, &name, NULL);
458 if (name == NULL || d >= (db_addr_t)db_maxoff) {
459 db_printf("%#lr", (unsigned long)off);
463 db_printf("%#lr = %s", (unsigned long)off, name);
465 db_printf("%s", name);
468 db_printf("+%+#lr", (long)d);
469 if (strategy == DB_STGY_PROC) {
470 if (db_line_at_pc(cursym, &filename, &linenum, off))
471 db_printf(" [%s:%d]", filename, linenum);
476 db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
478 return (X_db_line_at_pc(db_last_symtab, sym, filename, linenum, pc));
482 db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
484 return (X_db_sym_numargs(db_last_symtab, sym, nargp, argnames));
488 db_decode_syscall(struct thread *td, u_int number)
492 db_printf(" (%u", number);
493 p = (td != NULL) ? td->td_proc : NULL;
495 db_printf(", %s, %s", p->p_sysent->sv_name,
496 syscallname(p, number));