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.
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
38 #include <ddb/db_sym.h>
41 * Multiple symbol tables
44 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
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 *,
59 * Add symbol table, with given name, to list of symbol tables.
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
;
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.
83 db_qualify(c_db_sym_t sym
, char *symtabname
)
88 db_symbol_values(sym
, &symname
, 0);
89 ksnprintf(tmp
, sizeof(tmp
), "%s:%s", symtabname
, symname
);
95 db_eqname(const char *src
, const char *dst
, int c
)
97 if (!strcmp(src
, dst
))
100 return (!strcmp(src
+1,dst
));
105 db_value_of_name(const char *name
, db_expr_t
*valuep
)
109 sym
= db_lookup(name
);
110 if (sym
== C_DB_SYM_NULL
)
112 db_symbol_values(sym
, &name
, valuep
);
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.
124 db_lookup(const char *symstr
)
128 int symtab_start
= 0;
129 int symtab_end
= db_nsymtab
;
133 * Look for, remove, and remember any symbol table specifier.
135 for (cp
= symstr
; *cp
; cp
++) {
137 for (i
= 0; i
< db_nsymtab
; i
++) {
138 int n
= strlen(db_symtabs
[i
].name
);
141 n
== (cp
- symstr
) &&
142 strncmp(symstr
, db_symtabs
[i
].name
, n
) == 0
149 if (i
== db_nsymtab
) {
150 db_error("invalid symbol table name");
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
);
163 db_last_symtab
= &db_symtabs
[i
];
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.
181 db_symbol_is_ambiguous(c_db_sym_t sym
)
183 const char *sym_name
;
186 boolean_t found_once
= FALSE
;
188 if (!db_qualify_ambiguous_names
)
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
)) {
203 * Find the closest symbol to val, and return its name
204 * and the difference between val and the symbol found.
207 db_search_symbol(db_addr_t val
, db_strategy_t strategy
, db_expr_t
*offp
)
213 c_db_sym_t ret
= C_DB_SYM_NULL
, sym
;
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
];
230 * Return name and value of a symbol
233 db_symbol_values(c_db_sym_t sym
, const char **namep
, db_expr_t
*valuep
)
237 if (sym
== DB_SYM_NULL
) {
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
);
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;
270 db_printsym(db_expr_t off
, db_strategy_t strategy
)
279 cursym
= db_search_symbol(off
, strategy
, &d
);
280 db_symbol_values(cursym
, &name
, &value
);
283 if (value
>= DB_SMALL_VALUE_MIN
&& value
<= DB_SMALL_VALUE_MAX
) {
284 db_printf("%+#lr", (long)off
);
287 if (name
== 0 || d
>= (unsigned long)db_maxoff
) {
288 db_printf("%#lr", (unsigned long)off
);
291 db_printf("%s", name
);
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
);
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
);