Annotation of 43BSDReno/contrib/emacs-18.55/gdb/symtab.c, revision 1.1.1.1

1.1       root        1: /* Symbol table lookup for the GNU debugger, GDB.
                      2:    Copyright (C) 1986, 1987, 1988 Free Software Foundation, Inc.
                      3: 
                      4: GDB is distributed in the hope that it will be useful, but WITHOUT ANY
                      5: WARRANTY.  No author or distributor accepts responsibility to anyone
                      6: for the consequences of using it or for whether it serves any
                      7: particular purpose or works at all, unless he says so in writing.
                      8: Refer to the GDB General Public License for full details.
                      9: 
                     10: Everyone is granted permission to copy, modify and redistribute GDB,
                     11: but only under the conditions described in the GDB General Public
                     12: License.  A copy of this license is supposed to have been given to you
                     13: along with GDB so you can know your rights and responsibilities.  It
                     14: should be in a file named COPYING.  Among other things, the copyright
                     15: notice and this notice must be preserved on all copies.
                     16: 
                     17: In other words, go ahead and share GDB, but don't try to stop
                     18: anyone else from sharing it farther.  Help stamp out software hoarding!
                     19: */
                     20: 
                     21: #include "defs.h"
                     22: #include "initialize.h"
                     23: #include "symtab.h"
                     24: #include "param.h"
                     25: 
                     26: #include <stdio.h>
                     27: #include <obstack.h>
                     28: 
                     29: START_FILE
                     30: 
                     31: /* Allocate an obstack to hold objects that should be freed
                     32:    when we load a new symbol table.
                     33:    This includes the symbols made by dbxread
                     34:    and the types that are not permanent.  */
                     35: 
                     36: struct obstack obstack1;
                     37: 
                     38: struct obstack *symbol_obstack = &obstack1;
                     39: 
                     40: /* These variables point to the objects
                     41:    representing the predefined C data types.  */
                     42: 
                     43: struct type *builtin_type_void;
                     44: struct type *builtin_type_char;
                     45: struct type *builtin_type_short;
                     46: struct type *builtin_type_int;
                     47: struct type *builtin_type_long;
                     48: struct type *builtin_type_unsigned_char;
                     49: struct type *builtin_type_unsigned_short;
                     50: struct type *builtin_type_unsigned_int;
                     51: struct type *builtin_type_unsigned_long;
                     52: struct type *builtin_type_float;
                     53: struct type *builtin_type_double;
                     54: 
                     55: /* Lookup the symbol table of a source file named NAME.  */
                     56: 
                     57: struct symtab *
                     58: lookup_symtab (name)
                     59:      char *name;
                     60: {
                     61:   register struct symtab *s;
                     62:   register char *copy;
                     63: 
                     64:   for (s = symtab_list; s; s = s->next)
                     65:     if (!strcmp (name, s->filename))
                     66:       return s;
                     67: 
                     68:   /* If name not found as specified, see if adding ".c" helps.  */
                     69: 
                     70:   copy = (char *) alloca (strlen (name) + 3);
                     71:   strcpy (copy, name);
                     72:   strcat (copy, ".c");
                     73:   for (s = symtab_list; s; s = s->next)
                     74:     if (!strcmp (copy, s->filename))
                     75:       return s;
                     76: 
                     77:   return 0;
                     78: }
                     79: 
                     80: /* Lookup a typedef or primitive type named NAME,
                     81:    visible in lexical block BLOCK.
                     82:    If NOERR is nonzero, return zero if NAME is not suitably defined.  */
                     83: 
                     84: struct type *
                     85: lookup_typename (name, block, noerr)
                     86:      char *name;
                     87:      struct block *block;
                     88:      int noerr;
                     89: {
                     90:   register struct symbol *sym = lookup_symbol (name, block, VAR_NAMESPACE);
                     91:   if (sym == 0 || SYMBOL_CLASS (sym) != LOC_TYPEDEF)
                     92:     {
                     93:       if (!strcmp (name, "int"))
                     94:        return builtin_type_int;
                     95:       if (!strcmp (name, "long"))
                     96:        return builtin_type_long;
                     97:       if (!strcmp (name, "short"))
                     98:        return builtin_type_short;
                     99:       if (!strcmp (name, "char"))
                    100:        return builtin_type_char;
                    101:       if (!strcmp (name, "float"))
                    102:        return builtin_type_float;
                    103:       if (!strcmp (name, "double"))
                    104:        return builtin_type_double;
                    105:       if (!strcmp (name, "void"))
                    106:        return builtin_type_void;
                    107: 
                    108:       if (noerr)
                    109:        return 0;
                    110:       error ("No type named %s.", name);
                    111:     }
                    112:   return SYMBOL_TYPE (sym);
                    113: }
                    114: 
                    115: struct type *
                    116: lookup_unsigned_typename (name)
                    117:      char *name;
                    118: {
                    119:   if (!strcmp (name, "int"))
                    120:     return builtin_type_unsigned_int;
                    121:   if (!strcmp (name, "long"))
                    122:     return builtin_type_unsigned_long;
                    123:   if (!strcmp (name, "short"))
                    124:     return builtin_type_unsigned_short;
                    125:   if (!strcmp (name, "char"))
                    126:     return builtin_type_unsigned_char;
                    127:   error ("No type named unsigned %s.", name);
                    128: }
                    129: 
                    130: /* Lookup a structure type named "struct NAME",
                    131:    visible in lexical block BLOCK.  */
                    132: 
                    133: struct type *
                    134: lookup_struct (name, block)
                    135:      char *name;
                    136:      struct block *block;
                    137: {
                    138:   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
                    139:   if (sym == 0)
                    140:     error ("No struct type named %s.", name);
                    141:   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_STRUCT)
                    142:     error ("This context has union or enum %s, not a struct.", name);
                    143:   return SYMBOL_TYPE (sym);
                    144: }
                    145: 
                    146: /* Lookup a union type named "union NAME",
                    147:    visible in lexical block BLOCK.  */
                    148: 
                    149: struct type *
                    150: lookup_union (name, block)
                    151:      char *name;
                    152:      struct block *block;
                    153: {
                    154:   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
                    155:   if (sym == 0)
                    156:     error ("No union type named %s.", name);
                    157:   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_UNION)
                    158:     error ("This context has struct or enum %s, not a union.", name);
                    159:   return SYMBOL_TYPE (sym);
                    160: }
                    161: 
                    162: /* Lookup an enum type named "enum NAME",
                    163:    visible in lexical block BLOCK.  */
                    164: 
                    165: struct type *
                    166: lookup_enum (name, block)
                    167:      char *name;
                    168:      struct block *block;
                    169: {
                    170:   register struct symbol *sym = lookup_symbol (name, block, STRUCT_NAMESPACE);
                    171:   if (sym == 0)
                    172:     error ("No enum type named %s.", name);
                    173:   if (TYPE_CODE (SYMBOL_TYPE (sym)) != TYPE_CODE_ENUM)
                    174:     error ("This context has struct or union %s, not an enum.", name);
                    175:   return SYMBOL_TYPE (sym);
                    176: }
                    177: 
                    178: /* Given a type TYPE, return a type of pointers to that type.
                    179:    May need to construct such a type if this is the first use.  */
                    180: 
                    181: struct type *
                    182: lookup_pointer_type (type)
                    183:      struct type *type;
                    184: {
                    185:   register struct type *ptype = TYPE_POINTER_TYPE (type);
                    186:   if (ptype) return ptype;
                    187: 
                    188:   /* This is the first time anyone wanted a pointer to a TYPE.  */
                    189:   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
                    190:     ptype  = (struct type *) xmalloc (sizeof (struct type));
                    191:   else
                    192:     ptype  = (struct type *) obstack_alloc (symbol_obstack,
                    193:                                            sizeof (struct type));
                    194: 
                    195:   bzero (ptype, sizeof (struct type));
                    196:   TYPE_TARGET_TYPE (ptype) = type;
                    197:   TYPE_POINTER_TYPE (type) = ptype;
                    198:   /* New type is permanent if type pointed to is permanent.  */
                    199:   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
                    200:     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
                    201:   /* We assume the machine has only one representation for pointers!  */
                    202:   TYPE_LENGTH (ptype) = sizeof (char *);
                    203:   TYPE_CODE (ptype) = TYPE_CODE_PTR;
                    204:   return ptype;
                    205: }
                    206: 
                    207: /* Given a type TYPE, return a type of functions that return that type.
                    208:    May need to construct such a type if this is the first use.  */
                    209: 
                    210: struct type *
                    211: lookup_function_type (type)
                    212:      struct type *type;
                    213: {
                    214:   register struct type *ptype = TYPE_FUNCTION_TYPE (type);
                    215:   if (ptype) return ptype;
                    216: 
                    217:   /* This is the first time anyone wanted a function returning a TYPE.  */
                    218:   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
                    219:     ptype  = (struct type *) xmalloc (sizeof (struct type));
                    220:   else
                    221:     ptype  = (struct type *) obstack_alloc (symbol_obstack,
                    222:                                            sizeof (struct type));
                    223: 
                    224:   bzero (ptype, sizeof (struct type));
                    225:   TYPE_TARGET_TYPE (ptype) = type;
                    226:   TYPE_FUNCTION_TYPE (type) = ptype;
                    227:   /* New type is permanent if type returned is permanent.  */
                    228:   if (TYPE_FLAGS (type) & TYPE_FLAG_PERM)
                    229:     TYPE_FLAGS (ptype) |= TYPE_FLAG_PERM;
                    230:   TYPE_LENGTH (ptype) = 1;
                    231:   TYPE_CODE (ptype) = TYPE_CODE_FUNC;
                    232:   TYPE_NFIELDS (ptype) = 0;
                    233:   return ptype;
                    234: }
                    235: 
                    236: /* Smash TYPE to be a type of pointers to TO_TYPE.
                    237:    If TO_TYPE is not permanent and has no pointer-type yet,
                    238:    record TYPE as its pointer-type.  */
                    239: 
                    240: void
                    241: smash_to_pointer_type (type, to_type)
                    242:      struct type *type, *to_type;
                    243: {
                    244:   bzero (type, sizeof (struct type));
                    245:   TYPE_TARGET_TYPE (type) = to_type;
                    246:   /* We assume the machine has only one representation for pointers!  */
                    247:   TYPE_LENGTH (type) = sizeof (char *);
                    248:   TYPE_CODE (type) = TYPE_CODE_PTR;
                    249: 
                    250:   if (TYPE_POINTER_TYPE (to_type) == 0
                    251:       && !(TYPE_FLAGS (type) & TYPE_FLAG_PERM))
                    252:     {
                    253:       TYPE_POINTER_TYPE (to_type) = type;
                    254:     }
                    255: }
                    256: 
                    257: /* Smash TYPE to be a type of functions returning TO_TYPE.
                    258:    If TO_TYPE is not permanent and has no function-type yet,
                    259:    record TYPE as its function-type.  */
                    260: 
                    261: void
                    262: smash_to_function_type (type, to_type)
                    263:      struct type *type, *to_type;
                    264: {
                    265:   bzero (type, sizeof (struct type));
                    266:   TYPE_TARGET_TYPE (type) = to_type;
                    267:   TYPE_LENGTH (type) = 1;
                    268:   TYPE_CODE (type) = TYPE_CODE_FUNC;
                    269:   TYPE_NFIELDS (type) = 0;
                    270: 
                    271:   if (TYPE_FUNCTION_TYPE (to_type) == 0
                    272:       && !(TYPE_FLAGS (type) & TYPE_FLAG_PERM))
                    273:     {
                    274:       TYPE_FUNCTION_TYPE (to_type) = type;
                    275:     }
                    276: }
                    277: 
                    278: static struct symbol *lookup_block_symbol ();
                    279: 
                    280: /* Find the definition for a specified symbol name NAME
                    281:    in namespace NAMESPACE, visible from lexical block BLOCK.
                    282:    Returns the struct symbol pointer, or zero if no symbol is found.  */
                    283: 
                    284: struct symbol *
                    285: lookup_symbol (name, block, namespace)
                    286:      char *name;
                    287:      register struct block *block;
                    288:      enum namespace namespace;
                    289: {
                    290:   register int i, n;
                    291:   register struct symbol *sym;
                    292:   register struct symtab *s;
                    293:   struct blockvector *bv;
                    294: 
                    295:   /* Search specified block and its superiors.  */
                    296: 
                    297:   while (block != 0)
                    298:     {
                    299:       sym = lookup_block_symbol (block, name, namespace);
                    300:       if (sym) return sym;
                    301:       block = BLOCK_SUPERBLOCK (block);
                    302:     }
                    303: 
                    304:   /* Now search all symtabs' global blocks.  */
                    305: 
                    306:   for (s = symtab_list; s; s = s->next)
                    307:     {
                    308:       bv = BLOCKVECTOR (s);
                    309:       block = BLOCKVECTOR_BLOCK (bv, 0);
                    310:       sym = lookup_block_symbol (block, name, namespace);
                    311:       if (sym) return sym;
                    312:     }
                    313: 
                    314:   /* Now search all symtabs' per-file blocks.
                    315:      Not strictly correct, but more useful than an error.  */
                    316: 
                    317:   for (s = symtab_list; s; s = s->next)
                    318:     {
                    319:       bv = BLOCKVECTOR (s);
                    320:       block = BLOCKVECTOR_BLOCK (bv, 1);
                    321:       sym = lookup_block_symbol (block, name, namespace);
                    322:       if (sym) return sym;
                    323:     }
                    324:   return 0;
                    325: }
                    326: 
                    327: /* Look for a symbol in block BLOCK.  */
                    328: 
                    329: static struct symbol *
                    330: lookup_block_symbol (block, name, namespace)
                    331:      register struct block *block;
                    332:      char *name;
                    333:      enum namespace namespace;
                    334: {
                    335:   register int bot, top, inc;
                    336:   register struct symbol *sym;
                    337: 
                    338:   top = BLOCK_NSYMS (block);
                    339:   bot = 0;
                    340: 
                    341:   /* If the blocks's symbols were sorted, start with a binary search.  */
                    342: 
                    343:   if (BLOCK_SHOULD_SORT (block))
                    344:     {
                    345:       /* First, advance BOT to not far before
                    346:         the first symbol whose name is NAME.  */
                    347: 
                    348:       while (1)
                    349:        {
                    350:          inc = (top - bot + 1);
                    351:          /* No need to keep binary searching for the last few bits worth.  */
                    352:          if (inc < 4)
                    353:            break;
                    354:          inc = (inc >> 1) + bot;
                    355:          sym = BLOCK_SYM (block, inc);
                    356:          if (SYMBOL_NAME (sym)[0] < name[0])
                    357:            bot = inc;
                    358:          else if (SYMBOL_NAME (sym)[0] > name[0])
                    359:            top = inc;
                    360:          else if (strcmp (SYMBOL_NAME (sym), name) < 0)
                    361:            bot = inc;
                    362:          else
                    363:            top = inc;
                    364:        }
                    365: 
                    366:       /* Now scan forward until we run out of symbols,
                    367:         find one whose name is greater than NAME,
                    368:         or find one we want.
                    369:         If there is more than one symbol with the right name and namespace,
                    370:         we return the first one.  dbxread.c is careful to make sure
                    371:         that if one is a register then it comes first.  */
                    372: 
                    373:       top = BLOCK_NSYMS (block);
                    374:       while (bot < top)
                    375:        {
                    376:          sym = BLOCK_SYM (block, bot);
                    377:          inc = SYMBOL_NAME (sym)[0] - name[0];
                    378:          if (inc == 0)
                    379:            inc = strcmp (SYMBOL_NAME (sym), name);
                    380:          if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
                    381:            return sym;
                    382:          if (inc > 0)
                    383:            return 0;
                    384:          bot++;
                    385:        }
                    386:       return 0;
                    387:     }
                    388: 
                    389:   /* Here if block isn't sorted.
                    390:      This loop is equivalent to the loop above,
                    391:      but hacked greatly for speed.  */
                    392: 
                    393:   top = BLOCK_NSYMS (block);
                    394:   inc = name[0];
                    395:   while (bot < top)
                    396:     {
                    397:       sym = BLOCK_SYM (block, bot);
                    398:       if (SYMBOL_NAME (sym)[0] == inc
                    399:          && !strcmp (SYMBOL_NAME (sym), name)
                    400:          && SYMBOL_NAMESPACE (sym) == namespace)
                    401:        return sym;
                    402:       bot++;
                    403:     }
                    404:   return 0;
                    405: }
                    406: 
                    407: /* Return the symbol for the function which contains a specified
                    408:    lexical block, described by a struct block BL.  */
                    409: 
                    410: struct symbol *
                    411: block_function (bl)
                    412:      struct block *bl;
                    413: {
                    414:   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
                    415:     bl = BLOCK_SUPERBLOCK (bl);
                    416: 
                    417:   return BLOCK_FUNCTION (bl);
                    418: }
                    419: 
                    420: /* Subroutine of find_pc_line */
                    421: 
                    422: static struct symtab *
                    423: find_pc_symtab (pc)
                    424:      register CORE_ADDR pc;
                    425: {
                    426:   register struct block *b;
                    427:   struct blockvector *bv;
                    428:   register struct symtab *s;
                    429: 
                    430:   /* Search all symtabs for one whose file contains our pc */
                    431: 
                    432:   for (s = symtab_list; s; s = s->next)
                    433:     {
                    434:       bv = BLOCKVECTOR (s);
                    435:       b = BLOCKVECTOR_BLOCK (bv, 0);
                    436:       if (BLOCK_START (b) <= pc
                    437:          && BLOCK_END (b) > pc)
                    438:        break;
                    439:     }
                    440: 
                    441:   return s;
                    442: }
                    443: 
                    444: /* Find the source file and line number for a given PC value.
                    445:    Return a structure containing a symtab pointer, a line number,
                    446:    and a pc range for the entire source line.
                    447:    The value's .pc field is NOT the specified pc.
                    448:    NOTCURRENT nonzero means, if specified pc is on a line boundary,
                    449:    use the line that ends there.  Otherwise, in that case, the line
                    450:    that begins there is used.  */
                    451: 
                    452: struct symtab_and_line
                    453: find_pc_line (pc, notcurrent)
                    454:      CORE_ADDR pc;
                    455:      int notcurrent;
                    456: {
                    457:   struct symtab *s;
                    458:   register struct linetable *l;
                    459:   register int len;
                    460:   register int i, item;
                    461:   int line;
                    462:   struct symtab_and_line value;
                    463:   struct blockvector *bv;
                    464: 
                    465:   /* Info on best line seen so far, and where it starts, and its file.  */
                    466: 
                    467:   int best_line = 0;
                    468:   CORE_ADDR best_pc = 0;
                    469:   CORE_ADDR best_end = 0;
                    470:   struct symtab *best_symtab = 0;
                    471: 
                    472:   /* Store here the first line number
                    473:      of a file which contains the line at the smallest pc after PC.
                    474:      If we don't find a line whose range contains PC,
                    475:      we will use a line one less than this,
                    476:      with a range from the start of that file to the first line's pc.  */
                    477:   int alt_line = 0;
                    478:   CORE_ADDR alt_pc = 0;
                    479:   struct symtab *alt_symtab = 0;
                    480: 
                    481:   /* Info on best line seen in this file.  */
                    482: 
                    483:   int prev_line;
                    484:   CORE_ADDR prev_pc;
                    485: 
                    486:   /* Info on first line of this file.  */
                    487: 
                    488:   int first_line;
                    489:   CORE_ADDR first_pc;
                    490: 
                    491:   /* If this pc is not from the current frame,
                    492:      it is the address of the end of a call instruction.
                    493:      Quite likely that is the start of the following statement.
                    494:      But what we want is the statement containing the instruction.
                    495:      Fudge the pc to make sure we get that.  */
                    496: 
                    497:   if (notcurrent) pc -= 1;
                    498: 
                    499:   s = find_pc_symtab (pc);
                    500:   if (s == 0)
                    501:     {
                    502:       value.symtab = 0;
                    503:       value.line = 0;
                    504:       value.pc = pc;
                    505:       return value;
                    506:     }
                    507: 
                    508:   bv = BLOCKVECTOR (s);
                    509: 
                    510:   /* Look at all the symtabs that share this blockvector.
                    511:      They all have the same apriori range, that we found was right;
                    512:      but they have different line tables.  */
                    513: 
                    514:   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
                    515:     {
                    516:       /* Find the best line in this symtab.  */
                    517:       l = LINETABLE (s);
                    518:       len = l->nitems;
                    519:       prev_line = -1;
                    520:       first_line = -1;
                    521:       for (i = 0; i < len; i++)
                    522:        {
                    523:          item = l->item[i];
                    524:          if (item < 0)
                    525:            line = - item - 1;
                    526:          else
                    527:            {
                    528:              line++;
                    529:              if (first_line < 0)
                    530:                {
                    531:                  first_line = line;
                    532:                  first_pc = item;
                    533:                }
                    534:              /* Return the last line that did not start after PC.  */
                    535:              if (pc >= item)
                    536:                {
                    537:                  prev_line = line;
                    538:                  prev_pc = item;
                    539:                }
                    540:              else
                    541:                break;
                    542:            }
                    543:        }
                    544: 
                    545:       /* Is this file's best line closer than the best in the other files?
                    546:         If so, record this file, and its best line, as best so far.  */
                    547:       if (prev_line >= 0 && prev_pc > best_pc)
                    548:        {
                    549:          best_pc = prev_pc;
                    550:          best_line = prev_line;
                    551:          best_symtab = s;
                    552:          if (i < len)
                    553:            best_end = item;
                    554:          else
                    555:            best_end = 0;
                    556:        }
                    557:       /* Is this file's first line closer than the first lines of other files?
                    558:         If so, record this file, and its first line, as best alternate.  */
                    559:       if (first_line >= 0 && first_pc > pc
                    560:          && (alt_pc == 0 || first_pc < alt_pc))
                    561:        {
                    562:          alt_pc = first_pc;
                    563:          alt_line = first_line;
                    564:          alt_symtab = s;
                    565:        }
                    566:     }
                    567:   if (best_symtab == 0)
                    568:     {
                    569:       value.symtab = alt_symtab;
                    570:       value.line = alt_line - 1;
                    571:       value.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0));
                    572:       value.end = alt_pc;
                    573:     }
                    574:   else
                    575:     {
                    576:       value.symtab = best_symtab;
                    577:       value.line = best_line;
                    578:       value.pc = best_pc;
                    579:       value.end = (best_end ? best_end
                    580:                   : (alt_pc ? alt_pc
                    581:                      : BLOCK_END (BLOCKVECTOR_BLOCK (bv, 0))));
                    582:     }
                    583:   return value;
                    584: }
                    585: 
                    586: /* Find the PC value for a given source file and line number.
                    587:    Returns zero for invalid line number.
                    588:    The source file is specified with a struct symtab.  */
                    589: 
                    590: CORE_ADDR
                    591: find_line_pc (symtab, line)
                    592:      struct symtab *symtab;
                    593:      int line;
                    594: {
                    595:   register struct linetable *l;
                    596:   register int index;
                    597:   int dummy;
                    598: 
                    599:   if (symtab == 0)
                    600:     return 0;
                    601:   l = LINETABLE (symtab);
                    602:   index = find_line_common(l, line, &dummy);
                    603:   return index ? l->item[index] : 0;
                    604: }
                    605: 
                    606: /* Find the range of pc values in a line.
                    607:    Store the starting pc of the line into *STARTPTR
                    608:    and the ending pc (start of next line) into *ENDPTR.
                    609:    Returns 1 to indicate success.
                    610:    Returns 0 if could not find the specified line.  */
                    611: 
                    612: int
                    613: find_line_pc_range (symtab, thisline, startptr, endptr)
                    614:      struct symtab *symtab;
                    615:      int thisline;
                    616:      CORE_ADDR *startptr, *endptr;
                    617: {
                    618:   register struct linetable *l;
                    619:   register int index;
                    620:   int exact_match;             /* did we get an exact linenumber match */
                    621:   register CORE_ADDR prev_pc;
                    622:   CORE_ADDR last_pc;
                    623: 
                    624:   if (symtab == 0)
                    625:     return 0;
                    626: 
                    627:   l = LINETABLE (symtab);
                    628:   index = find_line_common (l, thisline, &exact_match);
                    629:   if (index)
                    630:     {
                    631:       *startptr = l->item[index];
                    632:       /* If we have not seen an entry for the specified line,
                    633:         assume that means the specified line has zero bytes.  */
                    634:       if (!exact_match || index == l->nitems-1)
                    635:        *endptr = *startptr;
                    636:       else
                    637:        /* Perhaps the following entry is for the following line.
                    638:           It's worth a try.  */
                    639:        if (l->item[index+1] > 0)
                    640:          *endptr = l->item[index+1];
                    641:        else
                    642:          *endptr = find_line_pc (symtab, thisline+1);
                    643:       return 1;
                    644:     }
                    645: 
                    646:   return 0;
                    647: }
                    648: 
                    649: /* Given a line table and a line number, return the index into the line
                    650:    table for the pc of the nearest line whose number is >= the specified one.
                    651:    Return 0 if none is found.  The value is never zero is it is an index.
                    652: 
                    653:    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
                    654: 
                    655: static int
                    656: find_line_common (l, lineno, exact_match)
                    657:      register struct linetable *l;
                    658:      register int lineno;
                    659:      int *exact_match;
                    660: {
                    661:   register int i;
                    662:   register int len;
                    663: 
                    664:   /* BEST is the smallest linenumber > LINENO so far seen,
                    665:      or 0 if none has been seen so far.
                    666:      BEST_INDEX identifies the item for it.  */
                    667: 
                    668:   int best_index = 0;
                    669:   int best = 0;
                    670: 
                    671:   int nextline = -1;
                    672: 
                    673:   if (lineno <= 0)
                    674:     return 0;
                    675: 
                    676:   len = l->nitems;
                    677:   for (i = 0; i < len; i++)
                    678:     {
                    679:       register int item = l->item[i];
                    680: 
                    681:       if (item < 0)
                    682:        nextline = - item - 1;
                    683:       else
                    684:        {
                    685:          nextline++;
                    686:          if (nextline == lineno)
                    687:            {
                    688:              *exact_match = 1;
                    689:              return i;
                    690:            }
                    691: 
                    692:          if (nextline > lineno && (best == 0 || nextline < best))
                    693:            {
                    694:              best = lineno;
                    695:              best_index = i;
                    696:            }
                    697:        }
                    698:     }
                    699: 
                    700:   /* If we got here, we didn't get an exact match.  */
                    701: 
                    702:   *exact_match = 0;
                    703:   return best_index;
                    704: }
                    705: 
                    706: int
                    707: find_pc_line_pc_range (pc, startptr, endptr)
                    708:      CORE_ADDR pc;
                    709:      CORE_ADDR *startptr, *endptr;
                    710: {
                    711:   struct symtab_and_line sal;
                    712:   sal = find_pc_line (pc, 0);
                    713:   *startptr = sal.pc;
                    714:   *endptr = sal.end;
                    715:   return sal.symtab != 0;
                    716: }
                    717: 
                    718: /* Parse a string that specifies a line number.
                    719:    Pass the address of a char * variable; that variable will be
                    720:    advanced over the characters actually parsed.
                    721: 
                    722:    The string can be:
                    723: 
                    724:    LINENUM -- that line number in current file.  PC returned is 0.
                    725:    FILE:LINENUM -- that line in that file.  PC returned is 0.
                    726:    FUNCTION -- line number of openbrace of that function.
                    727:       PC returned is the start of the function.
                    728:    FILE:FUNCTION -- likewise, but prefer functions in that file.
                    729:    *EXPR -- line in which address EXPR appears.
                    730: 
                    731:    FUNCTION may be an undebuggable function found in misc_function_vector.
                    732: 
                    733:    If the argument FUNFIRSTLINE is nonzero, we want the first line
                    734:    of real code inside a function when a function is specified.
                    735: 
                    736:    DEFAULT_SYMTAB specifies the file to use if none is specified.
                    737:    It defaults to current_source_symtab.
                    738:    DEFAULT_LINE specifies the line number to use for relative
                    739:    line numbers (that start with signs).  Defaults to current_source_line.
                    740: 
                    741:    Note that it is possible to return zero for the symtab
                    742:    if no file is validly specified.  Callers must check that.
                    743:    Also, the line number returned may be invalid.  */
                    744: 
                    745: struct symtab_and_line
                    746: decode_line_1 (argptr, funfirstline, default_symtab, default_line)
                    747:      char **argptr;
                    748:      int funfirstline;
                    749:      struct symtab *default_symtab;
                    750:      int default_line;
                    751: {
                    752:   struct symtab_and_line value;
                    753:   register char *p, *p1;
                    754:   register struct symtab *s;
                    755:   register struct symbol *sym;
                    756:   register CORE_ADDR pc;
                    757:   register int i;
                    758:   char *copy;
                    759: 
                    760:   /* Defaults have defaults.  */
                    761: 
                    762:   if (default_symtab == 0)
                    763:     {
                    764:       default_symtab = current_source_symtab;
                    765:       default_line = current_source_line;
                    766:     }
                    767: 
                    768:   /* See if arg is *PC */
                    769: 
                    770:   if (**argptr == '*')
                    771:     {
                    772:       (*argptr)++;
                    773:       pc = parse_and_eval_address_1 (argptr);
                    774:       value = find_pc_line (pc, 0);
                    775:       value.pc = pc;
                    776:       return value;
                    777:     }
                    778: 
                    779:   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
                    780: 
                    781:   s = 0;
                    782: 
                    783:   for (p = *argptr; *p; p++)
                    784:     {
                    785:       if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
                    786:        break;
                    787:     }
                    788:   while (p[0] == ' ' || p[0] == '\t') p++;
                    789: 
                    790:   if (p[0] == ':')
                    791:     {
                    792:       /* Extract the file name.  */
                    793:       p1 = p;
                    794:       while (p != *argptr && p[-1] == ' ') --p;
                    795:       copy = (char *) alloca (p - *argptr + 1);
                    796:       bcopy (*argptr, copy, p - *argptr);
                    797:       copy[p - *argptr] = 0;
                    798: 
                    799:       /* Find that file's data.  */
                    800:       s = lookup_symtab (copy);
                    801:       if (s == 0)
                    802:        {
                    803:          if (symtab_list == 0)
                    804:            error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
                    805:          error ("No source file named %s.", copy);
                    806:        }
                    807: 
                    808:       /* Discard the file name from the arg.  */
                    809:       p = p1 + 1;
                    810:       while (*p == ' ' || *p == '\t') p++;
                    811:       *argptr = p;
                    812:     }
                    813: 
                    814:   /* S is specified file's symtab, or 0 if no file specified.
                    815:      arg no longer contains the file name.  */
                    816: 
                    817:   /* Check whether arg is all digits (and sign) */
                    818: 
                    819:   p = *argptr;
                    820:   if (*p == '-' || *p == '+') p++;
                    821:   while (*p >= '0' && *p <= '9')
                    822:     p++;
                    823: 
                    824:   if (p != *argptr && (*p == 0 || *p == ' ' || *p == '\t' || *p == ','))
                    825:     {
                    826:       /* We found a token consisting of all digits -- at least one digit.  */
                    827:       enum sign {none, plus, minus} sign = none;
                    828: 
                    829:       if (**argptr == '+')
                    830:        sign = plus, (*argptr)++;
                    831:       else if (**argptr == '-')
                    832:        sign = minus, (*argptr)++;
                    833:       value.line = atoi (*argptr);
                    834:       switch (sign)
                    835:        {
                    836:        case plus:
                    837:          if (p == *argptr)
                    838:            value.line = 5;
                    839:          if (s == 0)
                    840:            value.line = default_line + value.line;
                    841:          break;
                    842:        case minus:
                    843:          if (p == *argptr)
                    844:            value.line = 15;
                    845:          if (s == 0)
                    846:            value.line = default_line - value.line;
                    847:          else
                    848:            value.line = 1;
                    849:          break;
                    850:        }
                    851: 
                    852:       while (*p == ' ' || *p == '\t') p++;
                    853:       *argptr = p;
                    854:       if (s == 0)
                    855:        s = default_symtab;
                    856:       value.symtab = s;
                    857:       value.pc = 0;
                    858:       return value;
                    859:     }
                    860: 
                    861:   /* Arg token is not digits => try it as a function name
                    862:      Find the next token (everything up to end or next whitespace).  */
                    863:   p = *argptr;
                    864:   while (*p && *p != ' ' && *p != '\t' && *p != ',') p++;
                    865:   copy = (char *) alloca (p - *argptr + 1);
                    866:   bcopy (*argptr, copy, p - *argptr);
                    867:   copy[p - *argptr] = 0;
                    868:   while (*p == ' ' || *p == '\t') p++;
                    869:   *argptr = p;
                    870: 
                    871:   /* Look up that token as a function.
                    872:      If file specified, use that file's per-file block to start with.  */
                    873: 
                    874:   sym = lookup_symbol (copy, s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), 1) : 0,
                    875:                       VAR_NAMESPACE);
                    876: 
                    877:   if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
                    878:     {
                    879:       /* Arg is the name of a function */
                    880:       pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) + FUNCTION_START_OFFSET;
                    881:       if (funfirstline)
                    882:        SKIP_PROLOGUE (pc);
                    883:       value = find_pc_line (pc, 0);
                    884:       value.pc = (value.end && value.pc != pc) ? value.end : pc;
                    885:       return value;
                    886:     }
                    887: 
                    888:   if (sym)
                    889:     error ("%s is not a function.", copy);
                    890: 
                    891:   if ((i = lookup_misc_func (copy)) < 0)
                    892:     error ("Function %s not defined.", copy);
                    893:   else
                    894:     {
                    895:       value.symtab = 0;
                    896:       value.line = 0;
                    897:       value.pc = misc_function_vector[i].address + FUNCTION_START_OFFSET;
                    898:       if (funfirstline)
                    899:        SKIP_PROLOGUE (value.pc);
                    900:       return value;
                    901:     }
                    902: 
                    903:   if (symtab_list == 0)
                    904:     error ("No symbol table is loaded.  Use the \"symbol-file\" command.");
                    905:   error ("Function %s not defined.", copy);
                    906: }
                    907: 
                    908: struct symtab_and_line
                    909: decode_line_spec (string, funfirstline)
                    910:      char *string;
                    911:      int funfirstline;
                    912: {
                    913:   struct symtab_and_line sal;
                    914:   if (string == 0)
                    915:     error ("Empty line specification.");
                    916:   sal = decode_line_1 (&string, funfirstline,
                    917:                       current_source_symtab, current_source_line);
                    918:   if (*string)
                    919:     error ("Junk at end of line specification: %s", string);
                    920:   return sal;
                    921: }
                    922: 
                    923: /* Return the index of misc function named NAME.  */
                    924: 
                    925: static
                    926: lookup_misc_func (name)
                    927:      register char *name;
                    928: {
                    929:   register int i;
                    930: 
                    931:   for (i = 0; i < misc_function_count; i++)
                    932:     if (!strcmp (misc_function_vector[i].name, name))
                    933:       return i;
                    934:   return -1;           /* not found */
                    935: }
                    936: 
                    937: static void
                    938: sources_info ()
                    939: {
                    940:   register struct symtab *s;
                    941:   register int column = 0;
                    942: 
                    943:   if (symtab_list == 0)
                    944:     {
                    945:       printf ("No symbol table is loaded.\n");
                    946:       return;
                    947:     }
                    948:   printf ("Source files for which symbol table is known:\n");
                    949:   for (s = symtab_list; s; s = s->next)
                    950:     {
                    951:       if (column != 0 && column + strlen (s->filename) >= 70)
                    952:        {
                    953:          printf ("\n");
                    954:          column = 0;
                    955:        }
                    956:       else if (column != 0)
                    957:        {
                    958:          printf (" ");
                    959:          column++;
                    960:        }
                    961:       printf ("%s", s->filename);
                    962:       column += strlen (s->filename);
                    963:       if (s->next)
                    964:        {
                    965:          printf (",");
                    966:          column++;
                    967:        }
                    968:     }
                    969:   printf ("\n");
                    970: }
                    971: 
                    972: /* List all symbols (if REGEXP is 0) or all symbols matching REGEXP.
                    973:    If CLASS is zero, list all symbols except functions and type names.
                    974:    If CLASS is 1, list only functions.
                    975:    If CLASS is 2, list only type names.  */
                    976: 
                    977: #define MORE  \
                    978: { print_count++;               \
                    979:   if (print_count >= 21)       \
                    980:     { printf ("--Type Return to print more--");        \
                    981:       print_count = 0;         \
                    982:       fflush (stdout);         \
                    983:       read_line (); } }
                    984: 
                    985: static void sort_block_syms ();
                    986: 
                    987: static void
                    988: list_symbols (regexp, class)
                    989:      char *regexp;
                    990:      int class;
                    991: {
                    992:   register struct symtab *s;
                    993:   register struct blockvector *bv;
                    994:   struct blockvector *prev_bv = 0;
                    995:   register struct block *b;
                    996:   register int i, j;
                    997:   register struct symbol *sym;
                    998:   char *val;
                    999:   int found_in_file;
                   1000:   static char *classnames[]
                   1001:     = {"variable", "function", "type"};
                   1002:   int print_count = 0;
                   1003: 
                   1004:   if (regexp)
                   1005:     if (val = (char *) re_comp (regexp))
                   1006:       error ("Invalid regexp: %s", val);
                   1007: 
                   1008:   printf (regexp
                   1009:          ? "All %ss matching regular expression \"%s\":\n"
                   1010:          : "All defined %ss:\n",
                   1011:          classnames[class],
                   1012:          regexp);
                   1013: 
                   1014:   for (s = symtab_list; s; s = s->next)
                   1015:     {
                   1016:       found_in_file = 0;
                   1017:       bv = BLOCKVECTOR (s);
                   1018:       /* Often many files share a blockvector.
                   1019:         Scan each blockvector only once so that
                   1020:         we don't get every symbol many times.
                   1021:         It happens that the first symtab in the list
                   1022:         for any given blockvector is the main file.  */
                   1023:       if (bv != prev_bv)
                   1024:        for (i = 0; i < 2; i++)
                   1025:          {
                   1026:            b = BLOCKVECTOR_BLOCK (bv, i);
                   1027:            /* Skip the sort if this block is always sorted.  */
                   1028:            if (!BLOCK_SHOULD_SORT (b))
                   1029:              sort_block_syms (b);
                   1030:            for (j = 0; j < BLOCK_NSYMS (b); j++)
                   1031:              {
                   1032:                QUIT;
                   1033:                sym = BLOCK_SYM (b, j);
                   1034:                if ((regexp == 0 || re_exec (SYMBOL_NAME (sym)))
                   1035:                    && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
                   1036:                         && SYMBOL_CLASS (sym) != LOC_BLOCK)
                   1037:                        || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
                   1038:                        || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)))
                   1039:                  {
                   1040:                    if (!found_in_file)
                   1041:                      {
                   1042:                        printf ("\nFile %s:\n", s->filename);
                   1043:                        print_count += 2;
                   1044:                      }
                   1045:                    found_in_file = 1;
                   1046:                    MORE;
                   1047:                    if (class != 2 && i == 1)
                   1048:                      printf ("static ");
                   1049:                    if (class == 2
                   1050:                        && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
                   1051:                      printf ("typedef ");
                   1052: 
                   1053:                    type_print (SYMBOL_TYPE (sym),
                   1054:                                (SYMBOL_CLASS (sym) == LOC_TYPEDEF
                   1055:                                 ? "" : SYMBOL_NAME (sym)),
                   1056:                                stdout, 0);
                   1057:                    if (class == 2
                   1058:                        && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE
                   1059:                        && (TYPE_NAME ((SYMBOL_TYPE (sym))) == 0
                   1060:                            || 0 != strcmp (TYPE_NAME ((SYMBOL_TYPE (sym))),
                   1061:                                            SYMBOL_NAME (sym))))
                   1062:                      printf (" %s", SYMBOL_NAME (sym));
                   1063:                    printf (";\n");
                   1064:                  }
                   1065:              }
                   1066:          }
                   1067:       prev_bv = bv;
                   1068:     }
                   1069: }
                   1070: 
                   1071: static void
                   1072: variables_info (regexp)
                   1073:      char *regexp;
                   1074: {
                   1075:   list_symbols (regexp, 0);
                   1076: }
                   1077: 
                   1078: static void
                   1079: functions_info (regexp)
                   1080:      char *regexp;
                   1081: {
                   1082:   list_symbols (regexp, 1);
                   1083: }
                   1084: 
                   1085: static void
                   1086: types_info (regexp)
                   1087:      char *regexp;
                   1088: {
                   1089:   list_symbols (regexp, 2);
                   1090: }
                   1091: 
                   1092: /* Call sort_block_syms to sort alphabetically the symbols of one block.  */
                   1093: 
                   1094: static int
                   1095: compare_symbols (s1, s2)
                   1096:      struct symbol **s1, **s2;
                   1097: {
                   1098:   /* Names that are less should come first.  */
                   1099:   register int namediff = strcmp (SYMBOL_NAME (*s1), SYMBOL_NAME (*s2));
                   1100:   if (namediff != 0) return namediff;
                   1101:   /* For symbols of the same name, registers should come first.  */
                   1102:   return ((SYMBOL_CLASS (*s2) == LOC_REGISTER)
                   1103:          - (SYMBOL_CLASS (*s1) == LOC_REGISTER));
                   1104: }
                   1105: 
                   1106: static void
                   1107: sort_block_syms (b)
                   1108:      register struct block *b;
                   1109: {
                   1110:   qsort (&BLOCK_SYM (b, 0), BLOCK_NSYMS (b),
                   1111:         sizeof (struct symbol *), compare_symbols);
                   1112: }
                   1113: 
                   1114: /* Initialize the standard C scalar types.  */
                   1115: 
                   1116: static
                   1117: struct type *
                   1118: init_type (code, length, uns, name)
                   1119:      enum type_code code;
                   1120:      int length, uns;
                   1121:      char *name;
                   1122: {
                   1123:   register struct type *type;
                   1124: 
                   1125:   type = (struct type *) xmalloc (sizeof (struct type));
                   1126:   bzero (type, sizeof *type);
                   1127:   TYPE_CODE (type) = code;
                   1128:   TYPE_LENGTH (type) = length;
                   1129:   TYPE_FLAGS (type) = uns ? TYPE_FLAG_UNSIGNED : 0;
                   1130:   TYPE_FLAGS (type) |= TYPE_FLAG_PERM;
                   1131:   TYPE_NFIELDS (type) = 0;
                   1132:   TYPE_NAME (type) = name;
                   1133: 
                   1134:   return type;
                   1135: }
                   1136: 
                   1137: static
                   1138: initialize ()
                   1139: {
                   1140:   add_info ("variables", variables_info,
                   1141:            "All global and static variable names, or those matching REGEXP.");
                   1142:   add_info ("functions", functions_info,
                   1143:            "All function names, or those matching REGEXP.");
                   1144:   add_info ("types", types_info,
                   1145:            "All types names, or those matching REGEXP.");
                   1146:   add_info ("sources", sources_info,
                   1147:            "Source files in the program.");
                   1148: 
                   1149:   obstack_init (symbol_obstack);
                   1150: 
                   1151:   builtin_type_void = init_type (TYPE_CODE_VOID, 0, 0, "void");
                   1152: 
                   1153:   builtin_type_float = init_type (TYPE_CODE_FLT, sizeof (float), 0, "float");
                   1154:   builtin_type_double = init_type (TYPE_CODE_FLT, sizeof (double), 0, "double");
                   1155: 
                   1156:   builtin_type_char = init_type (TYPE_CODE_INT, sizeof (char), 0, "char");
                   1157:   builtin_type_short = init_type (TYPE_CODE_INT, sizeof (short), 0, "short");
                   1158:   builtin_type_long = init_type (TYPE_CODE_INT, sizeof (long), 0, "long");
                   1159:   builtin_type_int = init_type (TYPE_CODE_INT, sizeof (int), 0, "int");
                   1160: 
                   1161:   builtin_type_unsigned_char = init_type (TYPE_CODE_INT, sizeof (char), 1, "unsigned char");
                   1162:   builtin_type_unsigned_short = init_type (TYPE_CODE_INT, sizeof (short), 1, "unsigned short");
                   1163:   builtin_type_unsigned_long = init_type (TYPE_CODE_INT, sizeof (long), 1, "unsigned long");
                   1164:   builtin_type_unsigned_int = init_type (TYPE_CODE_INT, sizeof (int), 1, "unsigned int");
                   1165: }
                   1166: 
                   1167: END_FILE

unix.superglobalmegacorp.com

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