HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / ddb / db_sym.c
blob9a9c24285629c572de6dd02938db0740195a6c2f
1 /*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
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.
26 * $FreeBSD: src/sys/ddb/db_sym.c,v 1.32 1999/08/28 00:41:10 peter Exp $
27 * $DragonFly: src/sys/ddb/db_sym.c,v 1.7 2006/12/23 00:27:02 swildner Exp $
31 * Author: David B. Golub, Carnegie Mellon University
32 * Date: 7/90
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <ddb/ddb.h>
38 #include <ddb/db_sym.h>
41 * Multiple symbol tables
43 #ifndef MAXNOSYMTABS
44 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
45 #endif
47 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
48 static int db_nsymtab = 0;
50 static db_symtab_t *db_last_symtab; /* where last symbol was found */
52 static c_db_sym_t db_lookup ( const char *symstr);
53 static char *db_qualify (c_db_sym_t sym, char *symtabname);
54 static boolean_t db_symbol_is_ambiguous (c_db_sym_t sym);
55 static boolean_t db_line_at_pc (c_db_sym_t, char **, int *,
56 db_expr_t);
59 * Add symbol table, with given name, to list of symbol tables.
61 void
62 db_add_symbol_table(char *start, char *end, char *name, char *ref)
64 if (db_nsymtab >= MAXNOSYMTABS) {
65 kprintf ("No slots left for %s symbol table", name);
66 panic ("db_sym.c: db_add_symbol_table");
69 db_symtabs[db_nsymtab].start = start;
70 db_symtabs[db_nsymtab].end = end;
71 db_symtabs[db_nsymtab].name = name;
72 db_symtabs[db_nsymtab].private = ref;
73 db_nsymtab++;
77 * db_qualify("vm_map", "ux") returns "unix:vm_map".
79 * Note: return value points to static data whose content is
80 * overwritten by each call... but in practice this seems okay.
82 static char *
83 db_qualify(c_db_sym_t sym, char *symtabname)
85 const char *symname;
86 static char tmp[256];
88 db_symbol_values(sym, &symname, 0);
89 ksnprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
90 return tmp;
94 boolean_t
95 db_eqname(const char *src, const char *dst, int c)
97 if (!strcmp(src, dst))
98 return (TRUE);
99 if (src[0] == c)
100 return (!strcmp(src+1,dst));
101 return (FALSE);
104 boolean_t
105 db_value_of_name(const char *name, db_expr_t *valuep)
107 c_db_sym_t sym;
109 sym = db_lookup(name);
110 if (sym == C_DB_SYM_NULL)
111 return (FALSE);
112 db_symbol_values(sym, &name, valuep);
113 return (TRUE);
118 * Lookup a symbol.
119 * If the symbol has a qualifier (e.g., ux:vm_map),
120 * then only the specified symbol table will be searched;
121 * otherwise, all symbol tables will be searched.
123 static c_db_sym_t
124 db_lookup(const char *symstr)
126 c_db_sym_t sp;
127 int i;
128 int symtab_start = 0;
129 int symtab_end = db_nsymtab;
130 const char *cp;
133 * Look for, remove, and remember any symbol table specifier.
135 for (cp = symstr; *cp; cp++) {
136 if (*cp == ':') {
137 for (i = 0; i < db_nsymtab; i++) {
138 int n = strlen(db_symtabs[i].name);
140 if (
141 n == (cp - symstr) &&
142 strncmp(symstr, db_symtabs[i].name, n) == 0
144 symtab_start = i;
145 symtab_end = i + 1;
146 break;
149 if (i == db_nsymtab) {
150 db_error("invalid symbol table name");
152 symstr = cp+1;
157 * Look in the specified set of symbol tables.
158 * Return on first match.
160 for (i = symtab_start; i < symtab_end; i++) {
161 sp = X_db_lookup(&db_symtabs[i], symstr);
162 if (sp) {
163 db_last_symtab = &db_symtabs[i];
164 return sp;
167 return 0;
171 * If TRUE, check across symbol tables for multiple occurrences
172 * of a name. Might slow things down quite a bit.
174 static volatile boolean_t db_qualify_ambiguous_names = FALSE;
177 * Does this symbol name appear in more than one symbol table?
178 * Used by db_symbol_values to decide whether to qualify a symbol.
180 static boolean_t
181 db_symbol_is_ambiguous(c_db_sym_t sym)
183 const char *sym_name;
184 int i;
186 boolean_t found_once = FALSE;
188 if (!db_qualify_ambiguous_names)
189 return FALSE;
191 db_symbol_values(sym, &sym_name, 0);
192 for (i = 0; i < db_nsymtab; i++) {
193 if (X_db_lookup(&db_symtabs[i], sym_name)) {
194 if (found_once)
195 return TRUE;
196 found_once = TRUE;
199 return FALSE;
203 * Find the closest symbol to val, and return its name
204 * and the difference between val and the symbol found.
206 c_db_sym_t
207 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
210 unsigned int diff;
211 size_t newdiff;
212 int i;
213 c_db_sym_t ret = C_DB_SYM_NULL, sym;
215 newdiff = diff = ~0;
216 db_last_symtab = 0;
217 for (i = 0; i < db_nsymtab; i++) {
218 sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
219 if (newdiff < diff) {
220 db_last_symtab = &db_symtabs[i];
221 diff = newdiff;
222 ret = sym;
225 *offp = diff;
226 return ret;
230 * Return name and value of a symbol
232 void
233 db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
235 db_expr_t value;
237 if (sym == DB_SYM_NULL) {
238 *namep = 0;
239 return;
242 X_db_symbol_values(db_last_symtab, sym, namep, &value);
244 if (db_symbol_is_ambiguous(sym))
245 *namep = db_qualify(sym, db_last_symtab->name);
246 if (valuep)
247 *valuep = value;
252 * Print a the closest symbol to value
254 * After matching the symbol according to the given strategy
255 * we print it in the name+offset format, provided the symbol's
256 * value is close enough (eg smaller than db_maxoff).
257 * We also attempt to print [filename:linenum] when applicable
258 * (eg for procedure names).
260 * If we could not find a reasonable name+offset representation,
261 * then we just print the value in hex. Small values might get
262 * bogus symbol associations, e.g. 3 might get some absolute
263 * value like _INCLUDE_VERSION or something, therefore we do
264 * not accept symbols whose value is "small" (and use plain hex).
267 db_expr_t db_maxoff = 0x10000;
269 void
270 db_printsym(db_expr_t off, db_strategy_t strategy)
272 db_expr_t d;
273 char *filename;
274 const char *name;
275 db_expr_t value;
276 int linenum;
277 c_db_sym_t cursym;
279 cursym = db_search_symbol(off, strategy, &d);
280 db_symbol_values(cursym, &name, &value);
281 if (name == 0)
282 value = off;
283 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
284 db_printf("%+#lr", (long)off);
285 return;
287 if (name == 0 || d >= (unsigned long)db_maxoff) {
288 db_printf("%#lr", (unsigned long)off);
289 return;
291 db_printf("%s", name);
292 if (d)
293 db_printf("+%+#lr", (long)d);
294 if (strategy == DB_STGY_PROC) {
295 if (db_line_at_pc(cursym, &filename, &linenum, off))
296 db_printf(" [%s:%d]", filename, linenum);
300 static boolean_t
301 db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
303 return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
307 db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
309 return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);