Move atomic_intr_t to machine/stdint.h and predent __ to reduce
[dragonfly.git] / sys / ddb / db_sym.c
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 $
27  * $DragonFly: src/sys/ddb/db_sym.c,v 1.5 2005/12/23 21:35:44 swildner Exp $
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
47 static db_symtab_t      db_symtabs[MAXNOSYMTABS] = {{0,},};
48 static int db_nsymtab = 0;
49
50 static db_symtab_t      *db_last_symtab; /* where last symbol was found */
51
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);
57
58 /*
59  * Add symbol table, with given name, to list of symbol tables.
60  */
61 void
62 db_add_symbol_table(char *start, char *end, char *name, char *ref)
63 {
64         if (db_nsymtab >= MAXNOSYMTABS) {
65                 printf ("No slots left for %s symbol table", name);
66                 panic ("db_sym.c: db_add_symbol_table");
67         }
68
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++;
74 }
75
76 /*
77  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
78  *
79  *  Note: return value points to static data whose content is
80  *  overwritten by each call... but in practice this seems okay.
81  */
82 static char *
83 db_qualify(c_db_sym_t sym, char *symtabname)
84 {
85         const char      *symname;
86         static char     tmp[256];
87
88         db_symbol_values(sym, &symname, 0);
89         snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
90         return tmp;
91 }
92
93
94 boolean_t
95 db_eqname(const char *src, const char *dst, int c)
96 {
97         if (!strcmp(src, dst))
98             return (TRUE);
99         if (src[0] == c)
100             return (!strcmp(src+1,dst));
101         return (FALSE);
102 }
103
104 boolean_t
105 db_value_of_name(const char *name, db_expr_t *valuep)
106 {
107         c_db_sym_t      sym;
108
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);
114 }
115
116
117 /*
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.
122  */
123 static c_db_sym_t
124 db_lookup(const char *symstr)
125 {
126         c_db_sym_t sp;
127         int i;
128         int symtab_start = 0;
129         int symtab_end = db_nsymtab;
130         const char *cp;
131
132         /*
133          * Look for, remove, and remember any symbol table specifier.
134          */
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);
139
140                                 if (
141                                     n == (cp - symstr) &&
142                                     strncmp(symstr, db_symtabs[i].name, n) == 0
143                                 ) {
144                                         symtab_start = i;
145                                         symtab_end = i + 1;
146                                         break;
147                                 }
148                         }
149                         if (i == db_nsymtab) {
150                                 db_error("invalid symbol table name");
151                         }
152                         symstr = cp+1;
153                 }
154         }
155
156         /*
157          * Look in the specified set of symbol tables.
158          * Return on first match.
159          */
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;
165                 }
166         }
167         return 0;
168 }
169
170 /*
171  * If TRUE, check across symbol tables for multiple occurrences
172  * of a name.  Might slow things down quite a bit.
173  */
174 static volatile boolean_t db_qualify_ambiguous_names = FALSE;
175
176 /*
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.
179  */
180 static boolean_t
181 db_symbol_is_ambiguous(c_db_sym_t sym)
182 {
183         const char      *sym_name;
184         int     i;
185         
186         boolean_t       found_once = FALSE;
187
188         if (!db_qualify_ambiguous_names)
189                 return FALSE;
190
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;
197                 }
198         }
199         return FALSE;
200 }
201
202 /*
203  * Find the closest symbol to val, and return its name
204  * and the difference between val and the symbol found.
205  */
206 c_db_sym_t
207 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
208 {
209         
210         unsigned int    diff;
211         size_t          newdiff;
212         int     i;
213         c_db_sym_t      ret = C_DB_SYM_NULL, sym;
214
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;
223             }
224         }
225         *offp = diff;
226         return ret;
227 }
228
229 /*
230  * Return name and value of a symbol
231  */
232 void
233 db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
234 {
235         db_expr_t       value;
236
237         if (sym == DB_SYM_NULL) {
238                 *namep = 0;
239                 return;
240         }
241
242         X_db_symbol_values(db_last_symtab, sym, namep, &value);
243
244         if (db_symbol_is_ambiguous(sym))
245                 *namep = db_qualify(sym, db_last_symtab->name);
246         if (valuep)
247                 *valuep = value;
248 }
249
250
251 /*
252  * Print a the closest symbol to value
253  *
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).
259  *
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).
265  */
266
267 db_expr_t       db_maxoff = 0x10000;
268
269 void
270 db_printsym(db_expr_t off, db_strategy_t strategy)
271 {
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;
278
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;
286         }
287         if (name == 0 || d >= (unsigned long)db_maxoff) {
288                 db_printf("%#lr", (unsigned long)off);
289                 return;
290         }
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);
297         }
298 }
299
300 static boolean_t
301 db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
302 {
303         return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
304 }
305
306 int
307 db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
308 {
309         return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
310 }