Annotation of Net2/ddb/db_sym.c, revision 1.1.1.3

1.1       root        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  [email protected]
                     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: /*
1.1.1.3 ! root       27:  * db_sym.c,v 1.2 1993/05/20 03:39:31 cgd Exp
        !            28:  *
1.1       root       29:  * HISTORY
1.1.1.3 ! root       30:  * db_sym.c,v
        !            31:  * Revision 1.2  1993/05/20  03:39:31  cgd
        !            32:  * add explicit rcs id
        !            33:  *
        !            34:  * Revision 1.1.1.1  1993/03/21  09:46:27  cgd
1.1.1.2   root       35:  * initial import of 386bsd-0.1 sources
                     36:  *
1.1       root       37:  * Revision 1.1  1992/03/25  21:45:27  pace
                     38:  * Initial revision
                     39:  *
                     40:  * Revision 2.5  91/02/05  17:07:07  mrt
                     41:  *     Changed to new Mach copyright
                     42:  *     [91/01/31  16:19:17  mrt]
                     43:  * 
                     44:  * Revision 2.4  90/10/25  14:44:05  rwd
                     45:  *     Changed db_printsym to print unsigned.
                     46:  *     [90/10/19            rpd]
                     47:  * 
                     48:  * Revision 2.3  90/09/09  23:19:56  rpd
                     49:  *     Avoid totally incorrect guesses of symbol names for small values.
                     50:  *     [90/08/30  17:39:48  af]
                     51:  * 
                     52:  * Revision 2.2  90/08/27  21:52:18  dbg
                     53:  *     Removed nlist.h.  Fixed some type declarations.
                     54:  *     Qualifier character is ':'.
                     55:  *     [90/08/20            dbg]
                     56:  *     Modularized symtab info into a new db_symtab_t type.
                     57:  *     Modified db_add_symbol_table  and others accordingly.
                     58:  *     Defined db_sym_t, a new (opaque) type used to represent
                     59:  *     symbols.  This should support all sort of future symtable
                     60:  *     formats. Functions like db_qualify take a db_sym_t now.
                     61:  *     New db_symbol_values() function to explode the content
                     62:  *     of a db_sym_t.
                     63:  *     db_search_symbol() replaces db_find_sym_and_offset(), which is
                     64:  *     now a macro defined in our (new) header file.  This new
                     65:  *     function accepts more restrictive searches, which are
                     66:  *     entirely delegated to the symtab-specific code.
                     67:  *     Accordingly, db_printsym() accepts a strategy parameter.
                     68:  *     New db_line_at_pc() function.
                     69:  *     Renamed misleading db_eqsym into db_eqname.
                     70:  *     [90/08/20  10:47:06  af]
                     71:  * 
                     72:  *     Created.
                     73:  *     [90/07/25            dbg]
                     74:  * 
                     75:  * Revision 2.1  90/07/26  16:43:52  dbg
                     76:  * Created.
                     77:  * 
                     78:  */
                     79: /*
                     80:  *     Author: David B. Golub, Carnegie Mellon University
                     81:  *     Date:   7/90
                     82:  */
                     83: #include "param.h"
                     84: #include "proc.h"
                     85: #include <machine/db_machdep.h>
                     86: #include <ddb/db_sym.h>
                     87: 
                     88: /*
                     89:  * We import from the symbol-table dependent routines:
                     90:  */
                     91: extern db_sym_t        X_db_lookup();
                     92: extern db_sym_t        X_db_search_symbol();
                     93: extern boolean_t X_db_line_at_pc();
                     94: extern void    X_db_symbol_values();
                     95: 
                     96: /*
                     97:  * Multiple symbol tables
                     98:  */
1.1.1.3 ! root       99: #ifndef MAXNOSYMTABS
1.1       root      100: #define        MAXNOSYMTABS    3       /* mach, ux, emulator */
1.1.1.3 ! root      101: #endif
1.1       root      102: 
                    103: db_symtab_t    db_symtabs[MAXNOSYMTABS] = {{0,},};
                    104: int db_nsymtab = 0;
                    105: 
                    106: db_symtab_t    *db_last_symtab;
                    107: 
                    108: db_sym_t       db_lookup();    /* forward */
                    109: 
                    110: /*
                    111:  * Add symbol table, with given name, to list of symbol tables.
                    112:  */
                    113: void
                    114: db_add_symbol_table(start, end, name, ref)
                    115:        char *start;
                    116:        char *end;
                    117:        char *name;
                    118:        char *ref;
                    119: {
                    120:        if (db_nsymtab >= MAXNOSYMTABS) {
                    121:                printf ("No slots left for %s symbol table", name);
                    122:                panic ("db_sym.c: db_add_symbol_table");
                    123:        }
                    124: 
                    125:        db_symtabs[db_nsymtab].start = start;
                    126:        db_symtabs[db_nsymtab].end = end;
                    127:        db_symtabs[db_nsymtab].name = name;
                    128:        db_symtabs[db_nsymtab].private = ref;
                    129:        db_nsymtab++;
                    130: }
                    131: 
                    132: /*
                    133:  *  db_qualify("vm_map", "ux") returns "unix:vm_map".
                    134:  *
                    135:  *  Note: return value points to static data whose content is
                    136:  *  overwritten by each call... but in practice this seems okay.
                    137:  */
                    138: static char *
                    139: db_qualify(sym, symtabname)
                    140:        db_sym_t        sym;
                    141:        register char   *symtabname;
                    142: {
                    143:        char            *symname;
                    144:        static char     tmp[256];
                    145:        register char   *s;
                    146: 
                    147:        db_symbol_values(sym, &symname, 0);
                    148:        s = tmp;
                    149:        while (*s++ = *symtabname++) {
                    150:        }
                    151:        s[-1] = ':';
                    152:        while (*s++ = *symname++) {
                    153:        }
                    154:        return tmp;
                    155: }
                    156: 
                    157: 
                    158: boolean_t
                    159: db_eqname(src, dst, c)
                    160:        char *src;
                    161:        char *dst;
                    162:        char c;
                    163: {
                    164:        if (!strcmp(src, dst))
                    165:            return (TRUE);
                    166:        if (src[0] == c)
                    167:            return (!strcmp(src+1,dst));
                    168:        return (FALSE);
                    169: }
                    170: 
                    171: boolean_t
                    172: db_value_of_name(name, valuep)
                    173:        char            *name;
                    174:        db_expr_t       *valuep;
                    175: {
                    176:        db_sym_t        sym;
                    177: 
                    178:        sym = db_lookup(name);
                    179:        if (sym == DB_SYM_NULL)
                    180:            return (FALSE);
                    181:        db_symbol_values(sym, &name, valuep);
                    182:        return (TRUE);
                    183: }
                    184: 
                    185: 
                    186: /*
                    187:  * Lookup a symbol.
                    188:  * If the symbol has a qualifier (e.g., ux:vm_map),
                    189:  * then only the specified symbol table will be searched;
                    190:  * otherwise, all symbol tables will be searched.
                    191:  */
                    192: db_sym_t
                    193: db_lookup(symstr)
                    194:        char *symstr;
                    195: {
                    196:        db_sym_t sp;
                    197:        register int i;
                    198:        int symtab_start = 0;
                    199:        int symtab_end = db_nsymtab;
                    200:        register char *cp;
                    201: 
                    202:        /*
                    203:         * Look for, remove, and remember any symbol table specifier.
                    204:         */
                    205:        for (cp = symstr; *cp; cp++) {
                    206:                if (*cp == ':') {
                    207:                        *cp = '\0';
                    208:                        for (i = 0; i < db_nsymtab; i++) {
                    209:                                if (! strcmp(symstr, db_symtabs[i].name)) {
                    210:                                        symtab_start = i;
                    211:                                        symtab_end = i + 1;
                    212:                                        break;
                    213:                                }
                    214:                        }
                    215:                        *cp = ':';
                    216:                        if (i == db_nsymtab) {
                    217:                                db_error("invalid symbol table name");
                    218:                        }
                    219:                        symstr = cp+1;
                    220:                }
                    221:        }
                    222: 
                    223:        /*
                    224:         * Look in the specified set of symbol tables.
                    225:         * Return on first match.
                    226:         */
                    227:        for (i = symtab_start; i < symtab_end; i++) {
                    228:                if (sp = X_db_lookup(&db_symtabs[i], symstr)) {
                    229:                        db_last_symtab = &db_symtabs[i];
                    230:                        return sp;
                    231:                }
                    232:        }
                    233:        return 0;
                    234: }
                    235: 
                    236: /*
                    237:  * Does this symbol name appear in more than one symbol table?
                    238:  * Used by db_symbol_values to decide whether to qualify a symbol.
                    239:  */
                    240: boolean_t db_qualify_ambiguous_names = FALSE;
                    241: 
                    242: boolean_t
                    243: db_symbol_is_ambiguous(sym)
                    244:        db_sym_t        sym;
                    245: {
                    246:        char            *sym_name;
                    247:        register int    i;
                    248:        register
                    249:        boolean_t       found_once = FALSE;
                    250: 
                    251:        if (!db_qualify_ambiguous_names)
                    252:                return FALSE;
                    253: 
                    254:        db_symbol_values(sym, &sym_name, 0);
                    255:        for (i = 0; i < db_nsymtab; i++) {
                    256:                if (X_db_lookup(&db_symtabs[i], sym_name)) {
                    257:                        if (found_once)
                    258:                                return TRUE;
                    259:                        found_once = TRUE;
                    260:                }
                    261:        }
                    262:        return FALSE;
                    263: }
                    264: 
                    265: /*
                    266:  * Find the closest symbol to val, and return its name
                    267:  * and the difference between val and the symbol found.
                    268:  */
                    269: db_sym_t
                    270: db_search_symbol( val, strategy, offp)
                    271:        register db_addr_t      val;
                    272:        db_strategy_t           strategy;
                    273:        db_expr_t               *offp;
                    274: {
                    275:        register
                    276:        unsigned int    diff;
                    277:        unsigned int    newdiff;
                    278:        register int    i;
                    279:        db_sym_t        ret = DB_SYM_NULL, sym;
                    280: 
                    281:        newdiff = diff = ~0;
                    282:        db_last_symtab = 0;
                    283:        for (i = 0; i < db_nsymtab; i++) {
                    284:            sym = X_db_search_symbol(&db_symtabs[i], val, strategy, &newdiff);
                    285:            if (newdiff < diff) {
                    286:                db_last_symtab = &db_symtabs[i];
                    287:                diff = newdiff;
                    288:                ret = sym;
                    289:            }
                    290:        }
                    291:        *offp = diff;
                    292:        return ret;
                    293: }
                    294: 
                    295: /*
                    296:  * Return name and value of a symbol
                    297:  */
                    298: void
                    299: db_symbol_values(sym, namep, valuep)
                    300:        db_sym_t        sym;
                    301:        char            **namep;
                    302:        db_expr_t       *valuep;
                    303: {
                    304:        db_expr_t       value;
                    305: 
                    306:        if (sym == DB_SYM_NULL) {
                    307:                *namep = 0;
                    308:                return;
                    309:        }
                    310: 
                    311:        X_db_symbol_values(sym, namep, &value);
                    312: 
                    313:        if (db_symbol_is_ambiguous(sym))
                    314:                *namep = db_qualify(sym, db_last_symtab->name);
                    315:        if (valuep)
                    316:                *valuep = value;
                    317: }
                    318: 
                    319: 
                    320: /*
                    321:  * Print a the closest symbol to value
                    322:  *
                    323:  * After matching the symbol according to the given strategy
                    324:  * we print it in the name+offset format, provided the symbol's
                    325:  * value is close enough (eg smaller than db_maxoff).
                    326:  * We also attempt to print [filename:linenum] when applicable
                    327:  * (eg for procedure names).
                    328:  *
                    329:  * If we could not find a reasonable name+offset representation,
                    330:  * then we just print the value in hex.  Small values might get
                    331:  * bogus symbol associations, e.g. 3 might get some absolute
                    332:  * value like _INCLUDE_VERSION or something, therefore we do
                    333:  * not accept symbols whose value is zero (and use plain hex).
                    334:  */
                    335: 
                    336: unsigned int   db_maxoff = 0x10000000;
                    337: 
                    338: void
                    339: db_printsym(off, strategy)
                    340:        db_expr_t       off;
                    341:        db_strategy_t   strategy;
                    342: {
                    343:        db_expr_t       d;
                    344:        char            *filename;
                    345:        char            *name;
                    346:        db_expr_t       value;
                    347:        int             linenum;
                    348:        db_sym_t        cursym;
                    349: 
                    350:        cursym = db_search_symbol(off, strategy, &d);
                    351:        db_symbol_values(cursym, &name, &value);
                    352:        if (name == 0 || d >= db_maxoff || value == 0) {
                    353:                db_printf("%#n", off);
                    354:                return;
                    355:        }
                    356:        db_printf("%s", name);
                    357:        if (d)
                    358:                db_printf("+%#r", d);
                    359:        if (strategy == DB_STGY_PROC) {
                    360:                if (db_line_at_pc(cursym, &filename, &linenum, off))
                    361:                        db_printf(" [%s:%d]", filename, linenum);
                    362:        }
                    363: }
                    364: 
                    365: 
                    366: boolean_t
                    367: db_line_at_pc( sym, filename, linenum, pc)
1.1.1.3 ! root      368:        db_sym_t        sym;
        !           369:        char            **filename;
        !           370:        int             *linenum;
        !           371:        db_expr_t       pc;
1.1       root      372: {
                    373:        return X_db_line_at_pc( db_last_symtab, sym, filename, linenum, pc);
                    374: }
1.1.1.3 ! root      375: 
        !           376: int
        !           377: db_sym_numargs(sym, nargp, argnames)
        !           378:        db_sym_t        sym;
        !           379:        int             *nargp;
        !           380:        char            **argnames;
        !           381: {
        !           382:        return X_db_sym_numargs(db_last_symtab, sym, nargp, argnames);
        !           383: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.