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.
30 * Author: David B. Golub, Carnegie Mellon University
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <ddb/db_sym.h>
40 * Multiple symbol tables
43 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
46 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
47 static int db_nsymtab = 0;
49 static db_symtab_t *db_last_symtab; /* where last symbol was found */
51 static c_db_sym_t db_lookup( const char *symstr);
52 static char *db_qualify(c_db_sym_t sym, char *symtabname);
53 static boolean_t db_symbol_is_ambiguous(c_db_sym_t sym);
54 static boolean_t db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
57 * Add symbol table, with given name, to list of symbol tables.
60 db_add_symbol_table(start, end, name, ref)
66 if (db_nsymtab >= MAXNOSYMTABS) {
67 printf ("No slots left for %s symbol table", name);
68 panic ("db_sym.c: db_add_symbol_table");
71 db_symtabs[db_nsymtab].start = start;
72 db_symtabs[db_nsymtab].end = end;
73 db_symtabs[db_nsymtab].name = name;
74 db_symtabs[db_nsymtab].private = ref;
79 * db_qualify("vm_map", "ux") returns "unix:vm_map".
81 * Note: return value points to static data whose content is
82 * overwritten by each call... but in practice this seems okay.
85 db_qualify(sym, symtabname)
87 register char *symtabname;
92 db_symbol_values(sym, &symname, 0);
93 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
99 db_eqname(src, dst, c)
104 if (!strcmp(src, dst))
107 return (!strcmp(src+1,dst));
112 db_value_of_name(name, valuep)
118 sym = db_lookup(name);
119 if (sym == C_DB_SYM_NULL)
121 db_symbol_values(sym, &name, valuep);
128 * If the symbol has a qualifier (e.g., ux:vm_map),
129 * then only the specified symbol table will be searched;
130 * otherwise, all symbol tables will be searched.
138 int symtab_start = 0;
139 int symtab_end = db_nsymtab;
140 register const char *cp;
143 * Look for, remove, and remember any symbol table specifier.
145 for (cp = symstr; *cp; cp++) {
147 for (i = 0; i < db_nsymtab; i++) {
148 int n = strlen(db_symtabs[i].name);
151 n == (cp - symstr) &&
152 strncmp(symstr, db_symtabs[i].name, n) == 0
159 if (i == db_nsymtab) {
160 db_error("invalid symbol table name");
167 * Look in the specified set of symbol tables.
168 * Return on first match.
170 for (i = symtab_start; i < symtab_end; i++) {
171 sp = X_db_lookup(&db_symtabs[i], symstr);
173 db_last_symtab = &db_symtabs[i];
181 * If TRUE, check across symbol tables for multiple occurrences
182 * of a name. Might slow things down quite a bit.
184 static volatile boolean_t db_qualify_ambiguous_names = FALSE;
187 * Does this symbol name appear in more than one symbol table?
188 * Used by db_symbol_values to decide whether to qualify a symbol.
191 db_symbol_is_ambiguous(sym)
194 const char *sym_name;
197 boolean_t found_once = FALSE;
199 if (!db_qualify_ambiguous_names)
202 db_symbol_values(sym, &sym_name, 0);
203 for (i = 0; i < db_nsymtab; i++) {
204 if (X_db_lookup(&db_symtabs[i], sym_name)) {
214 * Find the closest symbol to val, and return its name
215 * and the difference between val and the symbol found.
218 db_search_symbol( val, strategy, offp)
219 register db_addr_t val;
220 db_strategy_t strategy;
227 c_db_sym_t ret = C_DB_SYM_NULL, sym;
231 for (i = 0; i < db_nsymtab; i++) {
232 sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
233 if (newdiff < diff) {
234 db_last_symtab = &db_symtabs[i];
244 * Return name and value of a symbol
247 db_symbol_values(sym, namep, valuep)
254 if (sym == DB_SYM_NULL) {
259 X_db_symbol_values(db_last_symtab, sym, namep, &value);
261 if (db_symbol_is_ambiguous(sym))
262 *namep = db_qualify(sym, db_last_symtab->name);
269 * Print a the closest symbol to value
271 * After matching the symbol according to the given strategy
272 * we print it in the name+offset format, provided the symbol's
273 * value is close enough (eg smaller than db_maxoff).
274 * We also attempt to print [filename:linenum] when applicable
275 * (eg for procedure names).
277 * If we could not find a reasonable name+offset representation,
278 * then we just print the value in hex. Small values might get
279 * bogus symbol associations, e.g. 3 might get some absolute
280 * value like _INCLUDE_VERSION or something, therefore we do
281 * not accept symbols whose value is "small" (and use plain hex).
284 db_expr_t db_maxoff = 0x10000;
287 db_printsym(off, strategy)
289 db_strategy_t strategy;
298 cursym = db_search_symbol(off, strategy, &d);
299 db_symbol_values(cursym, &name, &value);
302 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
303 db_printf("%+#lr", (long)off);
306 if (name == 0 || d >= (unsigned long)db_maxoff) {
307 db_printf("%#lr", (unsigned long)off);
310 db_printf("%s", name);
312 db_printf("+%+#lr", (long)d);
313 if (strategy == DB_STGY_PROC) {
314 if (db_line_at_pc(cursym, &filename, &linenum, off))
315 db_printf(" [%s:%d]", filename, linenum);
320 db_line_at_pc( sym, filename, linenum, pc)
326 return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
330 db_sym_numargs(sym, nargp, argnames)
335 return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);