FreeBSD/Linux Kernel Cross Reference
sys/ddb/db_sym.c
1 /*-
2 * SPDX-License-Identifier: MIT-CMU
3 *
4 * Mach Operating System
5 * Copyright (c) 1991,1990 Carnegie Mellon University
6 * All Rights Reserved.
7 *
8 * Permission to use, copy, modify and distribute this software and its
9 * documentation is hereby granted, provided that both the copyright
10 * notice and this permission notice appear in all copies of the
11 * software, derivative works or modified versions, and any portions
12 * thereof, and that both notices appear in supporting documentation.
13 *
14 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS
15 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17 *
18 * Carnegie Mellon requests users of this software to return to
19 *
20 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
21 * School of Computer Science
22 * Carnegie Mellon University
23 * Pittsburgh PA 15213-3890
24 *
25 * any improvements or extensions that they make and grant Carnegie the
26 * rights to redistribute these changes.
27 */
28 /*
29 * Author: David B. Golub, Carnegie Mellon University
30 * Date: 7/90
31 */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "opt_kstack_pages.h"
37
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/pcpu.h>
41 #include <sys/proc.h>
42 #include <sys/smp.h>
43 #include <sys/sysent.h>
44
45 #include <net/vnet.h>
46
47 #include <ddb/ddb.h>
48 #include <ddb/db_sym.h>
49 #include <ddb/db_variables.h>
50
51 #include "opt_ddb.h"
52
53 /*
54 * Multiple symbol tables
55 */
56 #ifndef MAXNOSYMTABS
57 #define MAXNOSYMTABS 3 /* mach, ux, emulator */
58 #endif
59
60 static db_symtab_t db_symtabs[MAXNOSYMTABS] = {{0,},};
61 static int db_nsymtab = 0;
62
63 static db_symtab_t *db_last_symtab; /* where last symbol was found */
64
65 static c_db_sym_t db_lookup( const char *symstr);
66 static char *db_qualify(c_db_sym_t sym, char *symtabname);
67 static bool db_symbol_is_ambiguous(c_db_sym_t sym);
68 static bool db_line_at_pc(c_db_sym_t, char **, int *, db_expr_t);
69
70 static int db_cpu = -1;
71
72 #ifdef VIMAGE
73 static void *db_vnet = NULL;
74 #endif
75
76 /*
77 * Validate the CPU number used to interpret per-CPU variables so we can
78 * avoid later confusion if an invalid CPU is requested.
79 */
80 int
81 db_var_db_cpu(struct db_variable *vp, db_expr_t *valuep, int op)
82 {
83
84 switch (op) {
85 case DB_VAR_GET:
86 *valuep = db_cpu;
87 return (1);
88
89 case DB_VAR_SET:
90 if (*(int *)valuep < -1 || *(int *)valuep > mp_maxid) {
91 db_printf("Invalid value: %d\n", *(int*)valuep);
92 return (0);
93 }
94 db_cpu = *(int *)valuep;
95 return (1);
96
97 default:
98 db_printf("db_var_db_cpu: unknown operation\n");
99 return (0);
100 }
101 }
102
103 /*
104 * Read-only variable reporting the current CPU, which is what we use when
105 * db_cpu is set to -1.
106 */
107 int
108 db_var_curcpu(struct db_variable *vp, db_expr_t *valuep, int op)
109 {
110
111 switch (op) {
112 case DB_VAR_GET:
113 *valuep = curcpu;
114 return (1);
115
116 case DB_VAR_SET:
117 db_printf("Read-only variable.\n");
118 return (0);
119
120 default:
121 db_printf("db_var_curcpu: unknown operation\n");
122 return (0);
123 }
124 }
125
126 #ifdef VIMAGE
127 /*
128 * Validate the virtual network pointer used to interpret per-vnet global
129 * variable expansion. Right now we don't do much here, really we should
130 * walk the global vnet list to check it's an OK pointer.
131 */
132 int
133 db_var_db_vnet(struct db_variable *vp, db_expr_t *valuep, int op)
134 {
135
136 switch (op) {
137 case DB_VAR_GET:
138 *valuep = (db_expr_t)db_vnet;
139 return (1);
140
141 case DB_VAR_SET:
142 db_vnet = *(void **)valuep;
143 return (1);
144
145 default:
146 db_printf("db_var_db_vnet: unknown operation\n");
147 return (0);
148 }
149 }
150
151 /*
152 * Read-only variable reporting the current vnet, which is what we use when
153 * db_vnet is set to NULL.
154 */
155 int
156 db_var_curvnet(struct db_variable *vp, db_expr_t *valuep, int op)
157 {
158
159 switch (op) {
160 case DB_VAR_GET:
161 *valuep = (db_expr_t)curvnet;
162 return (1);
163
164 case DB_VAR_SET:
165 db_printf("Read-only variable.\n");
166 return (0);
167
168 default:
169 db_printf("db_var_curvnet: unknown operation\n");
170 return (0);
171 }
172 }
173 #endif
174
175 /*
176 * Add symbol table, with given name, to list of symbol tables.
177 */
178 void
179 db_add_symbol_table(char *start, char *end, char *name, char *ref)
180 {
181 if (db_nsymtab >= MAXNOSYMTABS) {
182 printf ("No slots left for %s symbol table", name);
183 panic ("db_sym.c: db_add_symbol_table");
184 }
185
186 db_symtabs[db_nsymtab].start = start;
187 db_symtabs[db_nsymtab].end = end;
188 db_symtabs[db_nsymtab].name = name;
189 db_symtabs[db_nsymtab].private = ref;
190 db_nsymtab++;
191 }
192
193 /*
194 * db_qualify("vm_map", "ux") returns "unix:vm_map".
195 *
196 * Note: return value points to static data whose content is
197 * overwritten by each call... but in practice this seems okay.
198 */
199 static char *
200 db_qualify(c_db_sym_t sym, char *symtabname)
201 {
202 const char *symname;
203 static char tmp[256];
204
205 db_symbol_values(sym, &symname, 0);
206 snprintf(tmp, sizeof(tmp), "%s:%s", symtabname, symname);
207 return tmp;
208 }
209
210 bool
211 db_eqname(const char *src, const char *dst, int c)
212 {
213 if (!strcmp(src, dst))
214 return (true);
215 if (src[0] == c)
216 return (!strcmp(src+1,dst));
217 return (false);
218 }
219
220 bool
221 db_value_of_name(const char *name, db_expr_t *valuep)
222 {
223 c_db_sym_t sym;
224
225 sym = db_lookup(name);
226 if (sym == C_DB_SYM_NULL)
227 return (false);
228 db_symbol_values(sym, &name, valuep);
229 return (true);
230 }
231
232 bool
233 db_value_of_name_pcpu(const char *name, db_expr_t *valuep)
234 {
235 static char tmp[256];
236 db_expr_t value;
237 c_db_sym_t sym;
238 int cpu;
239
240 if (db_cpu != -1)
241 cpu = db_cpu;
242 else
243 cpu = curcpu;
244 snprintf(tmp, sizeof(tmp), "pcpu_entry_%s", name);
245 sym = db_lookup(tmp);
246 if (sym == C_DB_SYM_NULL)
247 return (false);
248 db_symbol_values(sym, &name, &value);
249 if (value < DPCPU_START || value >= DPCPU_STOP)
250 return (false);
251 *valuep = (db_expr_t)((uintptr_t)value + dpcpu_off[cpu]);
252 return (true);
253 }
254
255 bool
256 db_value_of_name_vnet(const char *name, db_expr_t *valuep)
257 {
258 #ifdef VIMAGE
259 static char tmp[256];
260 db_expr_t value;
261 c_db_sym_t sym;
262 struct vnet *vnet;
263
264 if (db_vnet != NULL)
265 vnet = db_vnet;
266 else
267 vnet = curvnet;
268 snprintf(tmp, sizeof(tmp), "vnet_entry_%s", name);
269 sym = db_lookup(tmp);
270 if (sym == C_DB_SYM_NULL)
271 return (false);
272 db_symbol_values(sym, &name, &value);
273 if (value < VNET_START || value >= VNET_STOP)
274 return (false);
275 *valuep = (db_expr_t)((uintptr_t)value + vnet->vnet_data_base);
276 return (true);
277 #else
278 return (false);
279 #endif
280 }
281
282 /*
283 * Lookup a symbol.
284 * If the symbol has a qualifier (e.g., ux:vm_map),
285 * then only the specified symbol table will be searched;
286 * otherwise, all symbol tables will be searched.
287 */
288 static c_db_sym_t
289 db_lookup(const char *symstr)
290 {
291 c_db_sym_t sp;
292 int i;
293 int symtab_start = 0;
294 int symtab_end = db_nsymtab;
295 const char *cp;
296
297 /*
298 * Look for, remove, and remember any symbol table specifier.
299 */
300 for (cp = symstr; *cp; cp++) {
301 if (*cp == ':') {
302 for (i = 0; i < db_nsymtab; i++) {
303 int n = strlen(db_symtabs[i].name);
304
305 if (
306 n == (cp - symstr) &&
307 strncmp(symstr, db_symtabs[i].name, n) == 0
308 ) {
309 symtab_start = i;
310 symtab_end = i + 1;
311 break;
312 }
313 }
314 if (i == db_nsymtab) {
315 db_error("invalid symbol table name");
316 }
317 symstr = cp+1;
318 }
319 }
320
321 /*
322 * Look in the specified set of symbol tables.
323 * Return on first match.
324 */
325 for (i = symtab_start; i < symtab_end; i++) {
326 sp = X_db_lookup(&db_symtabs[i], symstr);
327 if (sp) {
328 db_last_symtab = &db_symtabs[i];
329 return sp;
330 }
331 }
332 return 0;
333 }
334
335 /*
336 * If true, check across symbol tables for multiple occurrences
337 * of a name. Might slow things down quite a bit.
338 */
339 static volatile bool db_qualify_ambiguous_names = false;
340
341 /*
342 * Does this symbol name appear in more than one symbol table?
343 * Used by db_symbol_values to decide whether to qualify a symbol.
344 */
345 static bool
346 db_symbol_is_ambiguous(c_db_sym_t sym)
347 {
348 const char *sym_name;
349 int i;
350 bool found_once = false;
351
352 if (!db_qualify_ambiguous_names)
353 return (false);
354
355 db_symbol_values(sym, &sym_name, 0);
356 for (i = 0; i < db_nsymtab; i++) {
357 if (X_db_lookup(&db_symtabs[i], sym_name)) {
358 if (found_once)
359 return (true);
360 found_once = true;
361 }
362 }
363 return (false);
364 }
365
366 /*
367 * Find the closest symbol to val, and return its name
368 * and the difference between val and the symbol found.
369 */
370 c_db_sym_t
371 db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
372 {
373 unsigned int diff;
374 size_t newdiff;
375 int i;
376 c_db_sym_t ret, sym;
377
378 /*
379 * The kernel will never map the first page, so any symbols in that
380 * range cannot refer to addresses. Some third-party assembly files
381 * define internal constants which appear in their symbol table.
382 * Avoiding the lookup for those symbols avoids replacing small offsets
383 * with those symbols during disassembly.
384 */
385 if (val < PAGE_SIZE) {
386 *offp = 0;
387 return (C_DB_SYM_NULL);
388 }
389
390 ret = C_DB_SYM_NULL;
391 newdiff = diff = val;
392 for (i = 0; i < db_nsymtab; i++) {
393 sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
394 if ((uintmax_t)newdiff < (uintmax_t)diff) {
395 db_last_symtab = &db_symtabs[i];
396 diff = newdiff;
397 ret = sym;
398 }
399 }
400 *offp = diff;
401 return ret;
402 }
403
404 /*
405 * Return name and value of a symbol
406 */
407 void
408 db_symbol_values(c_db_sym_t sym, const char **namep, db_expr_t *valuep)
409 {
410 db_expr_t value;
411
412 if (sym == DB_SYM_NULL) {
413 *namep = NULL;
414 return;
415 }
416
417 X_db_symbol_values(db_last_symtab, sym, namep, &value);
418
419 if (db_symbol_is_ambiguous(sym))
420 *namep = db_qualify(sym, db_last_symtab->name);
421 if (valuep)
422 *valuep = value;
423 }
424
425 /*
426 * Print a the closest symbol to value
427 *
428 * After matching the symbol according to the given strategy
429 * we print it in the name+offset format, provided the symbol's
430 * value is close enough (eg smaller than db_maxoff).
431 * We also attempt to print [filename:linenum] when applicable
432 * (eg for procedure names).
433 *
434 * If we could not find a reasonable name+offset representation,
435 * then we just print the value in hex. Small values might get
436 * bogus symbol associations, e.g. 3 might get some absolute
437 * value like _INCLUDE_VERSION or something, therefore we do
438 * not accept symbols whose value is "small" (and use plain hex).
439 */
440
441 db_expr_t db_maxoff = 0x10000;
442
443 void
444 db_printsym(db_expr_t off, db_strategy_t strategy)
445 {
446 db_expr_t d;
447 char *filename;
448 const char *name;
449 int linenum;
450 c_db_sym_t cursym;
451
452 if (off < 0 && off >= -db_maxoff) {
453 db_printf("%+#lr", (long)off);
454 return;
455 }
456 cursym = db_search_symbol(off, strategy, &d);
457 db_symbol_values(cursym, &name, NULL);
458 if (name == NULL || d >= (db_addr_t)db_maxoff) {
459 db_printf("%#lr", (unsigned long)off);
460 return;
461 }
462 #ifdef DDB_NUMSYM
463 db_printf("%#lr = %s", (unsigned long)off, name);
464 #else
465 db_printf("%s", name);
466 #endif
467 if (d)
468 db_printf("+%+#lr", (long)d);
469 if (strategy == DB_STGY_PROC) {
470 if (db_line_at_pc(cursym, &filename, &linenum, off))
471 db_printf(" [%s:%d]", filename, linenum);
472 }
473 }
474
475 static bool
476 db_line_at_pc(c_db_sym_t sym, char **filename, int *linenum, db_expr_t pc)
477 {
478 return (X_db_line_at_pc(db_last_symtab, sym, filename, linenum, pc));
479 }
480
481 bool
482 db_sym_numargs(c_db_sym_t sym, int *nargp, char **argnames)
483 {
484 return (X_db_sym_numargs(db_last_symtab, sym, nargp, argnames));
485 }
486
487 void
488 db_decode_syscall(struct thread *td, u_int number)
489 {
490 struct proc *p;
491
492 db_printf(" (%u", number);
493 p = (td != NULL) ? td->td_proc : NULL;
494 if (p != NULL) {
495 db_printf(", %s, %s", p->p_sysent->sv_name,
496 syscallname(p, number));
497 }
498 db_printf(")");
499 }
Cache object: aeadbd0aed9584e95994fbd02e474785
|