Remove some duplicate FreeBSD CVS IDs, move some IDs to better places.
[dragonfly.git] / sys / ddb / db_sym.c
CommitLineData
984263bc
MD
1/*
2 * Mach Operating System
3 * Copyright (c) 1991,1990 Carnegie Mellon University
4 * All Rights Reserved.
5 *
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.
11 *
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.
15 *
16 * Carnegie Mellon requests users of this software to return to
17 *
18 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
19 * School of Computer Science
20 * Carnegie Mellon University
21 * Pittsburgh PA 15213-3890
22 *
23 * any improvements or extensions that they make and grant Carnegie the
24 * rights to redistribute these changes.
25 *
26 * $FreeBSD: src/sys/ddb/db_sym.c,v 1.32 1999/08/28 00:41:10 peter Exp $
43c0ece6 27 * $DragonFly: src/sys/ddb/db_sym.c,v 1.4 2003/08/27 10:47:13 rob Exp $
984263bc
MD
28 */
29
30/*
31 * Author: David B. Golub, Carnegie Mellon University
32 * Date: 7/90
33 */
34#include <sys/param.h>
35#include <sys/systm.h>
36
37#include <ddb/ddb.h>
38#include <ddb/db_sym.h>
39
40/*
41 * Multiple symbol tables
42 */
43#ifndef MAXNOSYMTABS
44#define MAXNOSYMTABS 3 /* mach, ux, emulator */
45#endif
46
47static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
48static int db_nsymtab = 0;
49
50static db_symtab_t *db_last_symtab; /* where last symbol was found */
51
43c0ece6
RG
52static c_db_sym_t db_lookup ( const char *symstr);
53static char *db_qualify (c_db_sym_t sym, char *symtabname);
54static boolean_t db_symbol_is_ambiguous (c_db_sym_t sym);
55static boolean_t db_line_at_pc (c_db_sym_t, char **, int *,
56 db_expr_t);
984263bc
MD
57
58/*
59 * Add symbol table, with given name, to list of symbol tables.
60 */
61void
62db_add_symbol_table(start, end, name, ref)
63 char *start;
64 char *end;
65 char *name;
66 char *ref;
67{
68 if (db_nsymtab >= MAXNOSYMTABS) {
69 printf ("No slots left for %s symbol table", name);
70 panic ("db_sym.c: db_add_symbol_table");
71 }
72
73 db_symtabs[db_nsymtab].start = start;
74 db_symtabs[db_nsymtab].end = end;
75 db_symtabs[db_nsymtab].name = name;
76 db_symtabs[db_nsymtab].private = ref;
77 db_nsymtab++;
78}
79
80/*
81 * db_qualify("vm_map", "ux") returns "unix:vm_map".
82 *
83 * Note: return value points to static data whose content is
84 * overwritten by each call... but in practice this seems okay.
85 */
86static char *
87db_qualify(sym, symtabname)
88 c_db_sym_t sym;
ad0290bd 89 char *symtabname;
984263bc
MD
90{
91 const char *symname;
92 static char tmp[256];
93
94 db_symbol_values(sym, &symname, 0);
95 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
96 return tmp;
97}
98
99
100boolean_t
101db_eqname(src, dst, c)
102 const char *src;
103 const char *dst;
104 int c;
105{
106 if (!strcmp(src, dst))
107 return (TRUE);
108 if (src[0] == c)
109 return (!strcmp(src+1,dst));
110 return (FALSE);
111}
112
113boolean_t
114db_value_of_name(name, valuep)
115 const char *name;
116 db_expr_t *valuep;
117{
118 c_db_sym_t sym;
119
120 sym = db_lookup(name);
121 if (sym == C_DB_SYM_NULL)
122 return (FALSE);
123 db_symbol_values(sym, &name, valuep);
124 return (TRUE);
125}
126
127
128/*
129 * Lookup a symbol.
130 * If the symbol has a qualifier (e.g., ux:vm_map),
131 * then only the specified symbol table will be searched;
132 * otherwise, all symbol tables will be searched.
133 */
134static c_db_sym_t
135db_lookup(symstr)
136 const char *symstr;
137{
138 c_db_sym_t sp;
ad0290bd 139 int i;
984263bc
MD
140 int symtab_start = 0;
141 int symtab_end = db_nsymtab;
ad0290bd 142 const char *cp;
984263bc
MD
143
144 /*
145 * Look for, remove, and remember any symbol table specifier.
146 */
147 for (cp = symstr; *cp; cp++) {
148 if (*cp == ':') {
149 for (i = 0; i < db_nsymtab; i++) {
150 int n = strlen(db_symtabs[i].name);
151
152 if (
153 n == (cp - symstr) &&
154 strncmp(symstr, db_symtabs[i].name, n) == 0
155 ) {
156 symtab_start = i;
157 symtab_end = i + 1;
158 break;
159 }
160 }
161 if (i == db_nsymtab) {
162 db_error("invalid symbol table name");
163 }
164 symstr = cp+1;
165 }
166 }
167
168 /*
169 * Look in the specified set of symbol tables.
170 * Return on first match.
171 */
172 for (i = symtab_start; i < symtab_end; i++) {
173 sp = X_db_lookup(&db_symtabs[i], symstr);
174 if (sp) {
175 db_last_symtab = &db_symtabs[i];
176 return sp;
177 }
178 }
179 return 0;
180}
181
182/*
183 * If TRUE, check across symbol tables for multiple occurrences
184 * of a name. Might slow things down quite a bit.
185 */
186static volatile boolean_t db_qualify_ambiguous_names = FALSE;
187
188/*
189 * Does this symbol name appear in more than one symbol table?
190 * Used by db_symbol_values to decide whether to qualify a symbol.
191 */
192static boolean_t
193db_symbol_is_ambiguous(sym)
194 c_db_sym_t sym;
195{
196 const char *sym_name;
ad0290bd
RG
197 int i;
198
984263bc
MD
199 boolean_t found_once = FALSE;
200
201 if (!db_qualify_ambiguous_names)
202 return FALSE;
203
204 db_symbol_values(sym, &sym_name, 0);
205 for (i = 0; i < db_nsymtab; i++) {
206 if (X_db_lookup(&db_symtabs[i], sym_name)) {
207 if (found_once)
208 return TRUE;
209 found_once = TRUE;
210 }
211 }
212 return FALSE;
213}
214
215/*
216 * Find the closest symbol to val, and return its name
217 * and the difference between val and the symbol found.
218 */
219c_db_sym_t
220db_search_symbol( val, strategy, offp)
ad0290bd 221 db_addr_t val;
984263bc
MD
222 db_strategy_t strategy;
223 db_expr_t *offp;
224{
ad0290bd 225
984263bc
MD
226 unsigned int diff;
227 size_t newdiff;
ad0290bd 228 int i;
984263bc
MD
229 c_db_sym_t ret = C_DB_SYM_NULL, sym;
230
231 newdiff = diff = ~0;
232 db_last_symtab = 0;
233 for (i = 0; i < db_nsymtab; i++) {
234 sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
235 if (newdiff < diff) {
236 db_last_symtab = &db_symtabs[i];
237 diff = newdiff;
238 ret = sym;
239 }
240 }
241 *offp = diff;
242 return ret;
243}
244
245/*
246 * Return name and value of a symbol
247 */
248void
249db_symbol_values(sym, namep, valuep)
250 c_db_sym_t sym;
251 const char **namep;
252 db_expr_t *valuep;
253{
254 db_expr_t value;
255
256 if (sym == DB_SYM_NULL) {
257 *namep = 0;
258 return;
259 }
260
261 X_db_symbol_values(db_last_symtab, sym, namep, &value);
262
263 if (db_symbol_is_ambiguous(sym))
264 *namep = db_qualify(sym, db_last_symtab->name);
265 if (valuep)
266 *valuep = value;
267}
268
269
270/*
271 * Print a the closest symbol to value
272 *
273 * After matching the symbol according to the given strategy
274 * we print it in the name+offset format, provided the symbol's
275 * value is close enough (eg smaller than db_maxoff).
276 * We also attempt to print [filename:linenum] when applicable
277 * (eg for procedure names).
278 *
279 * If we could not find a reasonable name+offset representation,
280 * then we just print the value in hex. Small values might get
281 * bogus symbol associations, e.g. 3 might get some absolute
282 * value like _INCLUDE_VERSION or something, therefore we do
283 * not accept symbols whose value is "small" (and use plain hex).
284 */
285
286db_expr_t db_maxoff = 0x10000;
287
288void
289db_printsym(off, strategy)
290 db_expr_t off;
291 db_strategy_t strategy;
292{
293 db_expr_t d;
294 char *filename;
295 const char *name;
296 db_expr_t value;
297 int linenum;
298 c_db_sym_t cursym;
299
300 cursym = db_search_symbol(off, strategy, &d);
301 db_symbol_values(cursym, &name, &value);
302 if (name == 0)
303 value = off;
304 if (value >= DB_SMALL_VALUE_MIN && value <= DB_SMALL_VALUE_MAX) {
305 db_printf("%+#lr", (long)off);
306 return;
307 }
308 if (name == 0 || d >= (unsigned long)db_maxoff) {
309 db_printf("%#lr", (unsigned long)off);
310 return;
311 }
312 db_printf("%s", name);
313 if (d)
314 db_printf("+%+#lr", (long)d);
315 if (strategy == DB_STGY_PROC) {
316 if (db_line_at_pc(cursym, &filename, &linenum, off))
317 db_printf(" [%s:%d]", filename, linenum);
318 }
319}
320
321static boolean_t
322db_line_at_pc( sym, filename, linenum, pc)
323 c_db_sym_t sym;
324 char **filename;
325 int *linenum;
326 db_expr_t pc;
327{
328 return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
329}
330
331int
332db_sym_numargs(sym, nargp, argnames)
333 c_db_sym_t sym;
334 int *nargp;
335 char **argnames;
336{
337 return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
338}