Home | History | Annotate | Line # | Download | only in ddb
db_sym.c revision 1.37
      1  1.37     ragge /*	$NetBSD: db_sym.c,v 1.37 2003/04/25 20:30:58 ragge Exp $	*/
      2   1.7       cgd 
      3  1.31    simonb /*
      4   1.1       cgd  * Mach Operating System
      5   1.1       cgd  * Copyright (c) 1991,1990 Carnegie Mellon University
      6   1.1       cgd  * All Rights Reserved.
      7  1.31    simonb  *
      8   1.1       cgd  * Permission to use, copy, modify and distribute this software and its
      9   1.1       cgd  * documentation is hereby granted, provided that both the copyright
     10   1.1       cgd  * notice and this permission notice appear in all copies of the
     11   1.1       cgd  * software, derivative works or modified versions, and any portions
     12   1.1       cgd  * thereof, and that both notices appear in supporting documentation.
     13  1.31    simonb  *
     14  1.17        pk  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     15   1.1       cgd  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
     16   1.1       cgd  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     17  1.31    simonb  *
     18   1.1       cgd  * Carnegie Mellon requests users of this software to return to
     19  1.31    simonb  *
     20   1.1       cgd  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     21   1.1       cgd  *  School of Computer Science
     22   1.1       cgd  *  Carnegie Mellon University
     23   1.1       cgd  *  Pittsburgh PA 15213-3890
     24  1.31    simonb  *
     25   1.1       cgd  * any improvements or extensions that they make and grant Carnegie the
     26   1.1       cgd  * rights to redistribute these changes.
     27   1.1       cgd  */
     28  1.28     lukem 
     29  1.28     lukem #include <sys/cdefs.h>
     30  1.37     ragge __KERNEL_RCSID(0, "$NetBSD: db_sym.c,v 1.37 2003/04/25 20:30:58 ragge Exp $");
     31   1.5   mycroft 
     32  1.32     itohy #include "opt_ddbparam.h"
     33  1.26    simonb 
     34   1.6   mycroft #include <sys/param.h>
     35   1.6   mycroft #include <sys/proc.h>
     36  1.15   thorpej #include <sys/systm.h>
     37  1.35     ragge #include <sys/ksyms.h>
     38   1.6   mycroft 
     39   1.1       cgd #include <machine/db_machdep.h>
     40   1.6   mycroft 
     41  1.20     jhawk #include <ddb/db_lex.h>
     42   1.1       cgd #include <ddb/db_sym.h>
     43  1.12  christos #include <ddb/db_output.h>
     44  1.12  christos #include <ddb/db_extern.h>
     45  1.12  christos #include <ddb/db_command.h>
     46   1.1       cgd 
     47  1.35     ragge static void		db_symsplit(char *, char **, char **);
     48  1.31    simonb 
     49  1.15   thorpej 
     50  1.15   thorpej #ifdef DB_AOUT_SYMBOLS
     51  1.35     ragge #define	TBLNAME	"netbsd"
     52  1.15   thorpej 
     53  1.35     ragge static int using_aout_symtab;
     54  1.15   thorpej const db_symformat_t *db_symformat;
     55  1.35     ragge static db_forall_func_t db_sift;
     56  1.35     ragge extern db_symformat_t db_symformat_aout;
     57  1.35     ragge #endif
     58  1.15   thorpej 
     59  1.15   thorpej 
     60  1.15   thorpej /*
     61  1.15   thorpej  * Initialize the kernel debugger by initializing the master symbol
     62  1.15   thorpej  * table.  Note that if initializing the master symbol table fails,
     63  1.15   thorpej  * no other symbol tables can be loaded.
     64  1.15   thorpej  */
     65  1.15   thorpej void
     66  1.31    simonb ddb_init(int symsize, void *vss, void *vse)
     67  1.15   thorpej {
     68  1.35     ragge #ifdef DB_AOUT_SYMBOLS
     69  1.35     ragge 	db_symformat = &db_symformat_aout;
     70  1.35     ragge 	if ((*db_symformat->sym_init)(symsize, vss, vse, TBLNAME) == TRUE) {
     71  1.35     ragge 		using_aout_symtab = TRUE;
     72  1.15   thorpej 		return;
     73  1.15   thorpej 	}
     74  1.35     ragge #endif
     75  1.37     ragge 	ksyms_init(symsize, vss, vse);	/* Will complain if necessary */
     76   1.1       cgd }
     77   1.1       cgd 
     78   1.1       cgd boolean_t
     79  1.31    simonb db_eqname(char *src, char *dst, int c)
     80   1.1       cgd {
     81  1.31    simonb 
     82   1.1       cgd 	if (!strcmp(src, dst))
     83  1.31    simonb 		return (TRUE);
     84   1.1       cgd 	if (src[0] == c)
     85  1.31    simonb 		return (!strcmp(src+1,dst));
     86   1.1       cgd 	return (FALSE);
     87   1.1       cgd }
     88   1.1       cgd 
     89   1.1       cgd boolean_t
     90  1.31    simonb db_value_of_name(char *name, db_expr_t *valuep)
     91   1.1       cgd {
     92  1.35     ragge 	char *mod, *sym;
     93   1.1       cgd 
     94  1.35     ragge #ifdef DB_AOUT_SYMBOLS
     95  1.35     ragge 	db_sym_t	ssym;
     96   1.1       cgd 
     97  1.35     ragge 	if (using_aout_symtab) {
     98  1.35     ragge 		/*
     99  1.35     ragge 		 * Cannot load symtabs in a.out kernels, so the ':'
    100  1.35     ragge 		 * style of selecting modules is irrelevant.
    101  1.35     ragge 		 */
    102  1.35     ragge 		ssym = (*db_symformat->sym_lookup)(NULL, name);
    103  1.35     ragge 		if (ssym == DB_SYM_NULL)
    104  1.35     ragge 			return (FALSE);
    105  1.35     ragge 		db_symbol_values(ssym, &name, valuep);
    106  1.35     ragge 		return (TRUE);
    107   1.1       cgd 	}
    108  1.35     ragge #endif
    109  1.35     ragge 	db_symsplit(name, &mod, &sym);
    110  1.35     ragge 	if (ksyms_getval(mod, sym, valuep, KSYMS_EXTERN) == 0)
    111  1.35     ragge 		return TRUE;
    112  1.35     ragge 	if (ksyms_getval(mod, sym, valuep, KSYMS_ANY) == 0)
    113  1.35     ragge 		return TRUE;
    114  1.35     ragge 	return FALSE;
    115   1.1       cgd }
    116   1.1       cgd 
    117  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    118  1.20     jhawk /* Private structure for passing args to db_sift() from db_sifting(). */
    119  1.20     jhawk struct db_sift_args {
    120  1.20     jhawk 	char	*symstr;
    121  1.20     jhawk 	int	mode;
    122  1.20     jhawk };
    123  1.20     jhawk 
    124  1.20     jhawk /*
    125  1.20     jhawk  * Does the work of db_sifting(), called once for each
    126  1.35     ragge  * symbol via db_forall(), prints out symbols matching
    127  1.20     jhawk  * criteria.
    128  1.20     jhawk  */
    129  1.20     jhawk static void
    130  1.31    simonb db_sift(db_symtab_t *stab, db_sym_t sym, char *name, char *suffix, int prefix,
    131  1.31    simonb     void *arg)
    132  1.20     jhawk {
    133  1.20     jhawk 	char c, sc;
    134  1.20     jhawk 	char *find, *p;
    135  1.20     jhawk 	size_t len;
    136  1.20     jhawk 	struct db_sift_args *dsa;
    137  1.20     jhawk 
    138  1.20     jhawk 	dsa = (struct db_sift_args*)arg;
    139  1.20     jhawk 
    140  1.20     jhawk 	find = dsa->symstr;	/* String we're looking for. */
    141  1.20     jhawk 	p = name;		/* String we're searching within. */
    142  1.31    simonb 
    143  1.20     jhawk 	/* Matching algorithm cribbed from strstr(), which is not
    144  1.20     jhawk 	   in the kernel. */
    145  1.20     jhawk 	if ((c = *find++) != 0) {
    146  1.20     jhawk 		len = strlen(find);
    147  1.20     jhawk 		do {
    148  1.20     jhawk 			do {
    149  1.20     jhawk 				if ((sc = *p++) == 0)
    150  1.20     jhawk 					return;
    151  1.20     jhawk 			} while (sc != c);
    152  1.20     jhawk 		} while (strncmp(p, find, len) != 0);
    153  1.20     jhawk 	}
    154  1.20     jhawk 	if (dsa->mode=='F')	/* ala ls -F */
    155  1.20     jhawk 		db_printf("%s%s ", name, suffix);
    156  1.20     jhawk 	else
    157  1.20     jhawk 		db_printf("%s ", name);
    158  1.20     jhawk }
    159  1.35     ragge #endif
    160  1.20     jhawk 
    161  1.20     jhawk /*
    162  1.20     jhawk  * "Sift" for a partial symbol.
    163  1.20     jhawk  * Named for the Sun OpenPROM command ("sifting").
    164  1.20     jhawk  * If the symbol has a qualifier (e.g., ux:vm_map),
    165  1.20     jhawk  * then only the specified symbol table will be searched;
    166  1.20     jhawk  * otherwise, all symbol tables will be searched..
    167  1.20     jhawk  *
    168  1.20     jhawk  * "mode" is how-to-display, set from modifiers.
    169  1.20     jhawk  */
    170  1.20     jhawk void
    171  1.31    simonb db_sifting(char *symstr, int mode)
    172  1.20     jhawk {
    173  1.35     ragge 	char *mod, *sym;
    174  1.35     ragge 
    175  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    176  1.20     jhawk 	struct db_sift_args dsa;
    177  1.20     jhawk 
    178  1.35     ragge 	if (using_aout_symtab) {
    179  1.35     ragge 		dsa.symstr = symstr;
    180  1.35     ragge 		dsa.mode = mode;
    181  1.35     ragge 		(*db_symformat->sym_forall)(NULL, db_sift, &dsa);
    182  1.35     ragge 		db_printf("\n");
    183  1.35     ragge 		return;
    184  1.20     jhawk 	}
    185  1.35     ragge #endif
    186  1.20     jhawk 
    187  1.35     ragge 	db_symsplit(symstr, &mod, &sym);
    188  1.35     ragge 	if (ksyms_sift(mod, sym, mode) == ENODEV)
    189  1.35     ragge 		db_error("invalid symbol table name");
    190   1.1       cgd }
    191   1.1       cgd 
    192   1.1       cgd /*
    193   1.1       cgd  * Find the closest symbol to val, and return its name
    194   1.1       cgd  * and the difference between val and the symbol found.
    195   1.1       cgd  */
    196   1.1       cgd db_sym_t
    197  1.31    simonb db_search_symbol(db_addr_t val, db_strategy_t strategy, db_expr_t *offp)
    198   1.1       cgd {
    199  1.35     ragge 	unsigned int diff;
    200  1.35     ragge 	db_sym_t ret = DB_SYM_NULL;
    201  1.35     ragge 	db_addr_t naddr;
    202  1.35     ragge 	char *mod, *sym;
    203  1.35     ragge 
    204  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    205  1.35     ragge 	db_expr_t newdiff;
    206  1.35     ragge 	db_sym_t ssym;
    207  1.35     ragge 
    208  1.35     ragge 	if (using_aout_symtab) {
    209  1.35     ragge 		newdiff = diff = ~0;
    210  1.35     ragge 		ssym = (*db_symformat->sym_search)
    211  1.35     ragge 		    (NULL, val, strategy, &newdiff);
    212  1.33   thorpej 		if ((unsigned int) newdiff < diff) {
    213  1.31    simonb 			diff = newdiff;
    214  1.35     ragge 			ret = ssym;
    215  1.31    simonb 		}
    216  1.35     ragge 		*offp = diff;
    217  1.35     ragge 		return ret;
    218  1.35     ragge 	}
    219  1.35     ragge #endif
    220  1.35     ragge 
    221  1.35     ragge 	if (ksyms_getname(&mod, &sym, val, strategy) == 0) {
    222  1.35     ragge 		(void)ksyms_getval(mod, sym, &naddr, KSYMS_ANY);
    223  1.35     ragge 		diff = val - naddr;
    224  1.35     ragge 		ret = naddr;
    225   1.1       cgd 	}
    226   1.1       cgd 	*offp = diff;
    227   1.1       cgd 	return ret;
    228   1.1       cgd }
    229   1.1       cgd 
    230   1.1       cgd /*
    231   1.1       cgd  * Return name and value of a symbol
    232   1.1       cgd  */
    233   1.1       cgd void
    234  1.31    simonb db_symbol_values(db_sym_t sym, char **namep, db_expr_t *valuep)
    235   1.1       cgd {
    236  1.35     ragge 	char *mod;
    237   1.1       cgd 
    238   1.1       cgd 	if (sym == DB_SYM_NULL) {
    239   1.1       cgd 		*namep = 0;
    240   1.1       cgd 		return;
    241   1.1       cgd 	}
    242   1.1       cgd 
    243  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    244  1.35     ragge 	if (using_aout_symtab) {
    245  1.35     ragge 		db_expr_t value;
    246  1.35     ragge 		(*db_symformat->sym_value)(NULL, sym, namep, &value);
    247  1.35     ragge 		if (valuep)
    248  1.35     ragge 			*valuep = value;
    249  1.35     ragge 		return;
    250  1.35     ragge 	}
    251  1.35     ragge #endif
    252   1.1       cgd 
    253  1.35     ragge 	if (ksyms_getname(&mod, namep, sym, KSYMS_ANY|KSYMS_EXACT) == 0) {
    254  1.35     ragge 		if (valuep)
    255  1.35     ragge 			*valuep = sym;
    256  1.35     ragge 	} else
    257  1.35     ragge 		*namep = NULL;
    258   1.1       cgd }
    259   1.1       cgd 
    260   1.1       cgd 
    261   1.1       cgd /*
    262   1.1       cgd  * Print a the closest symbol to value
    263   1.1       cgd  *
    264   1.1       cgd  * After matching the symbol according to the given strategy
    265   1.1       cgd  * we print it in the name+offset format, provided the symbol's
    266   1.1       cgd  * value is close enough (eg smaller than db_maxoff).
    267   1.1       cgd  * We also attempt to print [filename:linenum] when applicable
    268   1.1       cgd  * (eg for procedure names).
    269   1.1       cgd  *
    270   1.1       cgd  * If we could not find a reasonable name+offset representation,
    271   1.1       cgd  * then we just print the value in hex.  Small values might get
    272   1.1       cgd  * bogus symbol associations, e.g. 3 might get some absolute
    273   1.1       cgd  * value like _INCLUDE_VERSION or something, therefore we do
    274   1.1       cgd  * not accept symbols whose value is zero (and use plain hex).
    275   1.9       gwr  * Also, avoid printing as "end+0x????" which is useless.
    276   1.9       gwr  * The variable db_lastsym is used instead of "end" in case we
    277   1.9       gwr  * add support for symbols in loadable driver modules.
    278   1.1       cgd  */
    279   1.9       gwr extern char end[];
    280  1.13       cgd unsigned long	db_lastsym = (unsigned long)end;
    281  1.13       cgd unsigned int	db_maxoff = 0x10000000;
    282   1.1       cgd 
    283  1.35     ragge #if 0
    284  1.30     jhawk void
    285  1.31    simonb db_symstr(char *buf, db_expr_t off, db_strategy_t strategy)
    286  1.30     jhawk {
    287  1.30     jhawk 	db_expr_t	d;
    288  1.30     jhawk 	char 		*filename;
    289  1.30     jhawk 	char		*name;
    290  1.30     jhawk 	db_expr_t	value;
    291  1.30     jhawk 	int 		linenum;
    292  1.30     jhawk 	db_sym_t	cursym;
    293  1.30     jhawk 
    294  1.33   thorpej 	if ((unsigned long) off <= db_lastsym) {
    295  1.30     jhawk 		cursym = db_search_symbol(off, strategy, &d);
    296  1.30     jhawk 		db_symbol_values(cursym, &name, &value);
    297  1.33   thorpej 		if (name != NULL &&
    298  1.33   thorpej 		    ((unsigned int) d < db_maxoff) &&
    299  1.33   thorpej 		    value != 0) {
    300  1.30     jhawk 			strcpy(buf, name);
    301  1.30     jhawk 			if (d) {
    302  1.30     jhawk 				strcat(buf, "+");
    303  1.30     jhawk 				db_format_radix(buf+strlen(buf), 24, d, TRUE);
    304  1.30     jhawk 			}
    305  1.30     jhawk 			if (strategy == DB_STGY_PROC) {
    306  1.30     jhawk 				if (db_line_at_pc(cursym, &filename, &linenum,
    307  1.30     jhawk 				    off))
    308  1.30     jhawk 					sprintf(buf+strlen(buf),
    309  1.30     jhawk 					    " [%s:%d]", filename, linenum);
    310  1.30     jhawk 			}
    311  1.30     jhawk 			return;
    312  1.30     jhawk 		}
    313  1.30     jhawk 	}
    314  1.30     jhawk 	strcpy(buf, db_num_to_str(off));
    315  1.30     jhawk 	return;
    316  1.30     jhawk }
    317  1.35     ragge #endif
    318   1.9       gwr 
    319   1.1       cgd void
    320  1.31    simonb db_printsym(db_expr_t off, db_strategy_t strategy,
    321  1.31    simonb     void (*pr)(const char *, ...))
    322   1.1       cgd {
    323  1.35     ragge 	char  *name, *mod;
    324  1.35     ragge 	long val;
    325  1.35     ragge #ifdef notyet
    326  1.35     ragge 	char *filename;
    327  1.35     ragge 	int  linenum;
    328  1.35     ragge #endif
    329   1.1       cgd 
    330  1.35     ragge #ifdef DB_AOUT_SYMBOLS
    331  1.35     ragge 	if (using_aout_symtab) {
    332  1.35     ragge 		db_expr_t	d;
    333  1.35     ragge 		char 		*filename;
    334  1.35     ragge 		char		*name;
    335  1.35     ragge 		db_expr_t	value;
    336  1.35     ragge 		int 		linenum;
    337  1.35     ragge 		db_sym_t	cursym;
    338  1.35     ragge 		if ((unsigned long) off <= db_lastsym) {
    339  1.35     ragge 			cursym = db_search_symbol(off, strategy, &d);
    340  1.35     ragge 			db_symbol_values(cursym, &name, &value);
    341  1.35     ragge 			if (name != NULL &&
    342  1.35     ragge 			    ((unsigned int) d < db_maxoff) &&
    343  1.35     ragge 			    value != 0) {
    344  1.35     ragge 				(*pr)("%s", name);
    345  1.35     ragge 				if (d) {
    346  1.35     ragge 					char tbuf[24];
    347  1.35     ragge 
    348  1.35     ragge 					db_format_radix(tbuf, 24, d, TRUE);
    349  1.35     ragge 					(*pr)("+%s", tbuf);
    350  1.35     ragge 				}
    351  1.35     ragge 				if (strategy == DB_STGY_PROC) {
    352  1.35     ragge 					if ((*db_symformat->sym_line_at_pc)
    353  1.35     ragge 					    (NULL, cursym, &filename,
    354  1.35     ragge 					    &linenum, off))
    355  1.35     ragge 						(*pr)(" [%s:%d]",
    356  1.35     ragge 						    filename, linenum);
    357  1.35     ragge 				}
    358  1.35     ragge 				return;
    359  1.35     ragge 			}
    360  1.35     ragge 		}
    361  1.35     ragge 		(*pr)(db_num_to_str(off));
    362  1.35     ragge 		return;
    363  1.35     ragge 	}
    364  1.35     ragge #endif
    365  1.35     ragge 	if (ksyms_getname(&mod, &name, off, strategy|KSYMS_CLOSEST) == 0) {
    366  1.35     ragge 		(void)ksyms_getval(mod, name, &val, KSYMS_ANY);
    367  1.35     ragge 		if (((off - val) < db_maxoff) && val) {
    368  1.35     ragge 			(*pr)("%s:%s", mod, name);
    369  1.35     ragge 			if (off - val) {
    370  1.23        tv 				char tbuf[24];
    371  1.23        tv 
    372  1.35     ragge 				db_format_radix(tbuf, 24, off - val, TRUE);
    373  1.24        tv 				(*pr)("+%s", tbuf);
    374  1.23        tv 			}
    375  1.35     ragge #ifdef notyet
    376  1.35     ragge 			if (strategy & KSYMS_PROC) {
    377  1.35     ragge 				if (ksyms_fmaddr(off, &filename, &linenum) == 0)
    378  1.21     jhawk 					(*pr)(" [%s:%d]", filename, linenum);
    379   1.9       gwr 			}
    380  1.35     ragge #endif
    381   1.9       gwr 			return;
    382   1.9       gwr 		}
    383   1.1       cgd 	}
    384  1.22     jhawk 	(*pr)(db_num_to_str(off));
    385   1.9       gwr 	return;
    386   1.1       cgd }
    387   1.1       cgd 
    388  1.35     ragge /*
    389  1.35     ragge  * Splits a string in the form "mod:sym" to two strings.
    390  1.35     ragge  */
    391  1.31    simonb static void
    392  1.35     ragge db_symsplit(char *str, char **mod, char **sym)
    393  1.15   thorpej {
    394  1.35     ragge 	char *cp;
    395  1.15   thorpej 
    396  1.35     ragge 	if ((cp = strchr(str, ':')) != NULL) {
    397  1.35     ragge 		*cp++ = '\0';
    398  1.35     ragge 		*mod = str;
    399  1.35     ragge 		*sym = cp;
    400  1.35     ragge 	} else {
    401  1.35     ragge 		*mod = NULL;
    402  1.35     ragge 		*sym = str;
    403  1.35     ragge 	}
    404   1.1       cgd }
    405  1.36     ragge 
    406  1.36     ragge boolean_t
    407  1.36     ragge db_sym_numargs(db_sym_t cursym, int *nargp, char **argnamep)
    408  1.36     ragge {
    409  1.36     ragge #ifdef DB_AOUT_SYMBOLS
    410  1.36     ragge 	if (using_aout_symtab)
    411  1.36     ragge 		return ((*db_symformat->sym_numargs)(NULL, cursym, nargp,
    412  1.36     ragge 		    argnamep));
    413  1.36     ragge #endif
    414  1.36     ragge 	return (FALSE);
    415  1.36     ragge }
    416  1.36     ragge 
    417