home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / gdb-4.14-src.lha / gdb-4.14 / gdb / symtab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-10  |  91.0 KB  |  3,290 lines

  1. /* Symbol table lookup for the GNU debugger, GDB.
  2.    Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994
  3.              Free Software Foundation, Inc.
  4.  
  5. This file is part of GDB.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "defs.h"
  22. #include "symtab.h"
  23. #include "gdbtypes.h"
  24. #include "gdbcore.h"
  25. #include "frame.h"
  26. #include "target.h"
  27. #include "value.h"
  28. #include "symfile.h"
  29. #include "objfiles.h"
  30. #include "gdbcmd.h"
  31. #include "call-cmds.h"
  32. #include "regex.h"
  33. #include "expression.h"
  34. #include "language.h"
  35. #include "demangle.h"
  36.  
  37. #include <obstack.h>
  38.  
  39. #include <sys/types.h>
  40. #include <fcntl.h>
  41. #include <string.h>
  42. #include <sys/stat.h>
  43. #include <ctype.h>
  44.  
  45. /* Prototypes for local functions */
  46.  
  47. extern int
  48. find_methods PARAMS ((struct type *, char *, struct symbol **));
  49.  
  50. static void
  51. completion_list_add_name PARAMS ((char *, char *, int, char *, char *));
  52.  
  53. static void
  54. build_canonical_line_spec PARAMS ((struct symtab_and_line *, char *, char ***));
  55.  
  56. static struct symtabs_and_lines
  57. decode_line_2 PARAMS ((struct symbol *[], int, int, char ***));
  58.  
  59. static void
  60. rbreak_command PARAMS ((char *, int));
  61.  
  62. static void
  63. types_info PARAMS ((char *, int));
  64.  
  65. static void
  66. functions_info PARAMS ((char *, int));
  67.  
  68. static void
  69. variables_info PARAMS ((char *, int));
  70.  
  71. static void
  72. sources_info PARAMS ((char *, int));
  73.  
  74. static void
  75. list_symbols PARAMS ((char *, int, int, int));
  76.  
  77. static void
  78. output_source_filename PARAMS ((char *, int *));
  79.  
  80. static char *
  81. operator_chars PARAMS ((char *, char **));
  82.  
  83. static int find_line_common PARAMS ((struct linetable *, int, int *));
  84.  
  85. static struct partial_symbol *
  86. lookup_partial_symbol PARAMS ((struct partial_symtab *, const char *,
  87.                    int, enum namespace));
  88.  
  89. static struct symtab *
  90. lookup_symtab_1 PARAMS ((char *));
  91.  
  92. /* */
  93.  
  94. /* The single non-language-specific builtin type */
  95. struct type *builtin_type_error;
  96.  
  97. /* Block in which the most recently searched-for symbol was found.
  98.    Might be better to make this a parameter to lookup_symbol and 
  99.    value_of_this. */
  100.  
  101. const struct block *block_found;
  102.  
  103. char no_symtab_msg[] = "No symbol table is loaded.  Use the \"file\" command.";
  104.  
  105. /* While the C++ support is still in flux, issue a possibly helpful hint on
  106.    using the new command completion feature on single quoted demangled C++
  107.    symbols.  Remove when loose ends are cleaned up.   FIXME -fnf */
  108.  
  109. void
  110. cplusplus_hint (name)
  111.      char *name;
  112. {
  113.   printf_filtered ("Hint: try '%s<TAB> or '%s<ESC-?>\n", name, name);
  114.   printf_filtered ("(Note leading single quote.)\n");
  115. }
  116.  
  117. /* Check for a symtab of a specific name; first in symtabs, then in
  118.    psymtabs.  *If* there is no '/' in the name, a match after a '/'
  119.    in the symtab filename will also work.  */
  120.  
  121. static struct symtab *
  122. lookup_symtab_1 (name)
  123.      char *name;
  124. {
  125.   register struct symtab *s;
  126.   register struct partial_symtab *ps;
  127.   register char *slash;
  128.   register struct objfile *objfile;
  129.  
  130.  got_symtab:
  131.  
  132.   /* First, search for an exact match */
  133.  
  134.   ALL_SYMTABS (objfile, s)
  135.     if (STREQ (name, s->filename))
  136.       return s;
  137.  
  138.   slash = strchr (name, '/');
  139.  
  140.   /* Now, search for a matching tail (only if name doesn't have any dirs) */
  141.  
  142.   if (!slash)
  143.     ALL_SYMTABS (objfile, s)
  144.       {
  145.     char *p = s -> filename;
  146.     char *tail = strrchr (p, '/');
  147.  
  148.     if (tail)
  149.       p = tail + 1;
  150.  
  151.     if (STREQ (p, name))
  152.       return s;
  153.       }
  154.  
  155.   /* Same search rules as above apply here, but now we look thru the
  156.      psymtabs.  */
  157.  
  158.   ps = lookup_partial_symtab (name);
  159.   if (!ps)
  160.     return (NULL);
  161.  
  162.   if (ps -> readin)
  163.     error ("Internal: readin %s pst for `%s' found when no symtab found.",
  164.        ps -> filename, name);
  165.  
  166.   s = PSYMTAB_TO_SYMTAB (ps);
  167.  
  168.   if (s)
  169.     return s;
  170.  
  171.   /* At this point, we have located the psymtab for this file, but
  172.      the conversion to a symtab has failed.  This usually happens
  173.      when we are looking up an include file.  In this case,
  174.      PSYMTAB_TO_SYMTAB doesn't return a symtab, even though one has
  175.      been created.  So, we need to run through the symtabs again in
  176.      order to find the file.
  177.      XXX - This is a crock, and should be fixed inside of the the
  178.      symbol parsing routines. */
  179.   goto got_symtab;
  180. }
  181.  
  182. /* Lookup the symbol table of a source file named NAME.  Try a couple
  183.    of variations if the first lookup doesn't work.  */
  184.  
  185. struct symtab *
  186. lookup_symtab (name)
  187.      char *name;
  188. {
  189.   register struct symtab *s;
  190. #if 0
  191.   register char *copy;
  192. #endif
  193.  
  194.   s = lookup_symtab_1 (name);
  195.   if (s) return s;
  196.  
  197. #if 0
  198.   /* This screws c-exp.y:yylex if there is both a type "tree" and a symtab
  199.      "tree.c".  */
  200.  
  201.   /* If name not found as specified, see if adding ".c" helps.  */
  202.   /* Why is this?  Is it just a user convenience?  (If so, it's pretty
  203.      questionable in the presence of C++, FORTRAN, etc.).  It's not in
  204.      the GDB manual.  */
  205.  
  206.   copy = (char *) alloca (strlen (name) + 3);
  207.   strcpy (copy, name);
  208.   strcat (copy, ".c");
  209.   s = lookup_symtab_1 (copy);
  210.   if (s) return s;
  211. #endif /* 0 */
  212.  
  213.   /* We didn't find anything; die.  */
  214.   return 0;
  215. }
  216.  
  217. /* Lookup the partial symbol table of a source file named NAME.
  218.    *If* there is no '/' in the name, a match after a '/'
  219.    in the psymtab filename will also work.  */
  220.  
  221. struct partial_symtab *
  222. lookup_partial_symtab (name)
  223. char *name;
  224. {
  225.   register struct partial_symtab *pst;
  226.   register struct objfile *objfile;
  227.   
  228.   ALL_PSYMTABS (objfile, pst)
  229.     {
  230.       if (STREQ (name, pst -> filename))
  231.     {
  232.       return (pst);
  233.     }
  234.     }
  235.  
  236.   /* Now, search for a matching tail (only if name doesn't have any dirs) */
  237.  
  238.   if (!strchr (name, '/'))
  239.     ALL_PSYMTABS (objfile, pst)
  240.       {
  241.     char *p = pst -> filename;
  242.     char *tail = strrchr (p, '/');
  243.  
  244.     if (tail)
  245.       p = tail + 1;
  246.  
  247.     if (STREQ (p, name))
  248.       return (pst);
  249.       }
  250.  
  251.   return (NULL);
  252. }
  253.  
  254. /* Demangle a GDB method stub type.
  255.    Note that this function is g++ specific. */
  256.  
  257. char *
  258. gdb_mangle_name (type, i, j)
  259.      struct type *type;
  260.      int i, j;
  261. {
  262.   int mangled_name_len;
  263.   char *mangled_name;
  264.   struct fn_field *f = TYPE_FN_FIELDLIST1 (type, i);
  265.   struct fn_field *method = &f[j];
  266.   char *field_name = TYPE_FN_FIELDLIST_NAME (type, i);
  267.   char *physname = TYPE_FN_FIELD_PHYSNAME (f, j);
  268.   char *newname = type_name_no_tag (type);
  269.  
  270.   /* Does the form of physname indicate that it is the full mangled name
  271.      of a constructor (not just the args)?  */
  272.   int is_full_physname_constructor;
  273.  
  274.   int is_constructor;
  275.   int is_destructor = DESTRUCTOR_PREFIX_P (physname);
  276.   /* Need a new type prefix.  */
  277.   char *const_prefix = method->is_const ? "C" : "";
  278.   char *volatile_prefix = method->is_volatile ? "V" : "";
  279.   char buf[20];
  280.   int len = (newname == NULL ? 0 : strlen (newname));
  281.  
  282.   is_full_physname_constructor = 
  283.     ((physname[0]=='_' && physname[1]=='_' && 
  284.       (isdigit(physname[2]) || physname[2]=='Q' || physname[2]=='t'))
  285.      || (strncmp(physname, "__ct", 4) == 0));
  286.  
  287.   is_constructor =
  288.     is_full_physname_constructor || (newname && STREQ(field_name, newname));
  289.  
  290.   if (!is_destructor)
  291.     is_destructor = (strncmp(physname, "__dt", 4) == 0); 
  292.  
  293.   if (is_destructor || is_full_physname_constructor)
  294.     {
  295.       mangled_name = (char*) xmalloc(strlen(physname)+1);
  296.       strcpy(mangled_name, physname);
  297.       return mangled_name;
  298.     }
  299.  
  300.   if (len == 0)
  301.     {
  302.       sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
  303.       if (strcmp(buf, "__") == 0)
  304.     buf[0] = '\0';
  305.     }
  306.   else if (newname != NULL && strchr (newname, '<') != NULL)
  307.     {
  308.       /* Template methods are fully mangled.  */
  309.       sprintf (buf, "__%s%s", const_prefix, volatile_prefix);
  310.       newname = NULL;
  311.       len = 0;
  312.     }
  313.   else
  314.     {
  315.       sprintf (buf, "__%s%s%d", const_prefix, volatile_prefix, len);
  316.     }
  317.   mangled_name_len = ((is_constructor ? 0 : strlen (field_name))
  318.               + strlen (buf) + len
  319.               + strlen (physname)
  320.               + 1);
  321.  
  322.   /* Only needed for GNU-mangled names.  ANSI-mangled names
  323.      work with the normal mechanisms.  */
  324.   if (OPNAME_PREFIX_P (field_name))
  325.     {
  326.       char *opname = cplus_mangle_opname (field_name + 3, 0);
  327.       if (opname == NULL)
  328.     error ("No mangling for \"%s\"", field_name);
  329.       mangled_name_len += strlen (opname);
  330.       mangled_name = (char *)xmalloc (mangled_name_len);
  331.  
  332.       strncpy (mangled_name, field_name, 3);
  333.       mangled_name[3] = '\0';
  334.       strcat (mangled_name, opname);
  335.     }
  336.   else
  337.     {
  338.       mangled_name = (char *)xmalloc (mangled_name_len);
  339.       if (is_constructor)
  340.     mangled_name[0] = '\0';
  341.       else
  342.     strcpy (mangled_name, field_name);
  343.     }
  344.   strcat (mangled_name, buf);
  345.   /* If the class doesn't have a name, i.e. newname NULL, then we just
  346.      mangle it using 0 for the length of the class.  Thus it gets mangled
  347.      as something starting with `::' rather than `classname::'. */ 
  348.   if (newname != NULL)
  349.     strcat (mangled_name, newname);
  350.  
  351.   strcat (mangled_name, physname);
  352.   return (mangled_name);
  353. }
  354.  
  355.  
  356. /* Find which partial symtab on contains PC.  Return 0 if none.  */
  357.  
  358. struct partial_symtab *
  359. find_pc_psymtab (pc)
  360.      register CORE_ADDR pc;
  361. {
  362.   register struct partial_symtab *pst;
  363.   register struct objfile *objfile;
  364.  
  365.   ALL_PSYMTABS (objfile, pst)
  366.     {
  367.       if (pc >= pst->textlow && pc < pst->texthigh)
  368.     {
  369.       struct minimal_symbol *msymbol;
  370.       struct partial_symtab *tpst;
  371.  
  372.       /* An objfile that has its functions reordered might have
  373.          many partial symbol tables containing the PC, but
  374.          we want the partial symbol table that contains the
  375.          function containing the PC.  */
  376.       if (!(objfile->flags & OBJF_REORDERED))
  377.         return (pst);
  378.  
  379.       msymbol = lookup_minimal_symbol_by_pc (pc);
  380.       if (msymbol == NULL)
  381.         return (pst);
  382.  
  383.       for (tpst = pst; tpst != NULL; tpst = tpst->next)
  384.         {
  385.           if (pc >= tpst->textlow && pc < tpst->texthigh)
  386.         {
  387.           struct partial_symbol *p;
  388.  
  389.           p = find_pc_psymbol (tpst, pc);
  390.           if (p != NULL
  391.               && SYMBOL_VALUE_ADDRESS(p)
  392.              == SYMBOL_VALUE_ADDRESS (msymbol))
  393.             return (tpst);
  394.         }
  395.         }
  396.       return (pst);
  397.     }
  398.     }
  399.   return (NULL);
  400. }
  401.  
  402. /* Find which partial symbol within a psymtab contains PC.  Return 0
  403.    if none.  Check all psymtabs if PSYMTAB is 0.  */
  404. struct partial_symbol *
  405. find_pc_psymbol (psymtab, pc)
  406.      struct partial_symtab *psymtab;
  407.      CORE_ADDR pc;
  408. {
  409.   struct partial_symbol *best = NULL, *p;
  410.   CORE_ADDR best_pc;
  411.   
  412.   if (!psymtab)
  413.     psymtab = find_pc_psymtab (pc);
  414.   if (!psymtab)
  415.     return 0;
  416.  
  417.   best_pc = psymtab->textlow - 1;
  418.  
  419.   /* Search the global symbols as well as the static symbols, so that
  420.      find_pc_partial_function doesn't use a minimal symbol and thus
  421.      cache a bad endaddr.  */
  422.   for (p = psymtab->objfile->global_psymbols.list + psymtab->globals_offset;
  423.        (p - (psymtab->objfile->global_psymbols.list + psymtab->globals_offset)
  424.     < psymtab->n_global_syms);
  425.        p++)
  426.     if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
  427.     && SYMBOL_CLASS (p) == LOC_BLOCK
  428.     && pc >= SYMBOL_VALUE_ADDRESS (p)
  429.     && SYMBOL_VALUE_ADDRESS (p) > best_pc)
  430.       {
  431.     best_pc = SYMBOL_VALUE_ADDRESS (p);
  432.     best = p;
  433.       }
  434.   for (p = psymtab->objfile->static_psymbols.list + psymtab->statics_offset;
  435.        (p - (psymtab->objfile->static_psymbols.list + psymtab->statics_offset)
  436.     < psymtab->n_static_syms);
  437.        p++)
  438.     if (SYMBOL_NAMESPACE (p) == VAR_NAMESPACE
  439.     && SYMBOL_CLASS (p) == LOC_BLOCK
  440.     && pc >= SYMBOL_VALUE_ADDRESS (p)
  441.     && SYMBOL_VALUE_ADDRESS (p) > best_pc)
  442.       {
  443.     best_pc = SYMBOL_VALUE_ADDRESS (p);
  444.     best = p;
  445.       }
  446.   if (best_pc == psymtab->textlow - 1)
  447.     return 0;
  448.   return best;
  449. }
  450.  
  451.  
  452. /* Find the definition for a specified symbol name NAME
  453.    in namespace NAMESPACE, visible from lexical block BLOCK.
  454.    Returns the struct symbol pointer, or zero if no symbol is found.
  455.    If SYMTAB is non-NULL, store the symbol table in which the
  456.    symbol was found there, or NULL if not found.
  457.    C++: if IS_A_FIELD_OF_THIS is nonzero on entry, check to see if
  458.    NAME is a field of the current implied argument `this'.  If so set
  459.    *IS_A_FIELD_OF_THIS to 1, otherwise set it to zero. 
  460.    BLOCK_FOUND is set to the block in which NAME is found (in the case of
  461.    a field of `this', value_of_this sets BLOCK_FOUND to the proper value.) */
  462.  
  463. /* This function has a bunch of loops in it and it would seem to be
  464.    attractive to put in some QUIT's (though I'm not really sure
  465.    whether it can run long enough to be really important).  But there
  466.    are a few calls for which it would appear to be bad news to quit
  467.    out of here: find_proc_desc in alpha-tdep.c and mips-tdep.c, and
  468.    nindy_frame_chain_valid in nindy-tdep.c.  (Note that there is C++
  469.    code below which can error(), but that probably doesn't affect
  470.    these calls since they are looking for a known variable and thus
  471.    can probably assume it will never hit the C++ code).  */
  472.  
  473. struct symbol *
  474. lookup_symbol (name, block, namespace, is_a_field_of_this, symtab)
  475.      const char *name;
  476.      register const struct block *block;
  477.      const enum namespace namespace;
  478.      int *is_a_field_of_this;
  479.      struct symtab **symtab;
  480. {
  481.   register struct symbol *sym;
  482.   register struct symtab *s = NULL;
  483.   register struct partial_symtab *ps;
  484.   struct blockvector *bv;
  485.   register struct objfile *objfile;
  486.   register struct block *b;
  487.   register struct minimal_symbol *msymbol;
  488.  
  489.   /* Search specified block and its superiors.  */
  490.  
  491.   while (block != 0)
  492.     {
  493.       sym = lookup_block_symbol (block, name, namespace);
  494.       if (sym) 
  495.     {
  496.       block_found = block;
  497.       if (symtab != NULL)
  498.         {
  499.           /* Search the list of symtabs for one which contains the
  500.          address of the start of this block.  */
  501.           ALL_SYMTABS (objfile, s)
  502.         {
  503.           bv = BLOCKVECTOR (s);
  504.           b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  505.           if (BLOCK_START (b) <= BLOCK_START (block)
  506.               && BLOCK_END (b) > BLOCK_START (block))
  507.             goto found;
  508.         }
  509. found:
  510.           *symtab = s;
  511.         }
  512.  
  513.       return (sym);
  514.     }
  515.       block = BLOCK_SUPERBLOCK (block);
  516.     }
  517.  
  518.   /* FIXME: this code is never executed--block is always NULL at this
  519.      point.  What is it trying to do, anyway?  We already should have
  520.      checked the STATIC_BLOCK above (it is the superblock of top-level
  521.      blocks).  Why is VAR_NAMESPACE special-cased?  */
  522.   /* Don't need to mess with the psymtabs; if we have a block,
  523.      that file is read in.  If we don't, then we deal later with
  524.      all the psymtab stuff that needs checking.  */
  525.   if (namespace == VAR_NAMESPACE && block != NULL)
  526.     {
  527.       struct block *b;
  528.       /* Find the right symtab.  */
  529.       ALL_SYMTABS (objfile, s)
  530.     {
  531.       bv = BLOCKVECTOR (s);
  532.       b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  533.       if (BLOCK_START (b) <= BLOCK_START (block)
  534.           && BLOCK_END (b) > BLOCK_START (block))
  535.         {
  536.           sym = lookup_block_symbol (b, name, VAR_NAMESPACE);
  537.           if (sym)
  538.         {
  539.           block_found = b;
  540.           if (symtab != NULL)
  541.             *symtab = s;
  542.           return sym;
  543.         }
  544.         }
  545.     }
  546.     }
  547.  
  548.  
  549.   /* C++: If requested to do so by the caller, 
  550.      check to see if NAME is a field of `this'. */
  551.   if (is_a_field_of_this)
  552.     {
  553.       struct value *v = value_of_this (0);
  554.       
  555.       *is_a_field_of_this = 0;
  556.       if (v && check_field (v, name))
  557.     {
  558.       *is_a_field_of_this = 1;
  559.       if (symtab != NULL)
  560.         *symtab = NULL;
  561.       return 0;
  562.     }
  563.     }
  564.  
  565.   /* Now search all global blocks.  Do the symtab's first, then
  566.      check the psymtab's */
  567.   
  568.   ALL_SYMTABS (objfile, s)
  569.     {
  570.       bv = BLOCKVECTOR (s);
  571.       block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  572.       sym = lookup_block_symbol (block, name, namespace);
  573.       if (sym) 
  574.     {
  575.       block_found = block;
  576.       if (symtab != NULL)
  577.         *symtab = s;
  578.       return sym;
  579.     }
  580.     }
  581.  
  582.   /* Check for the possibility of the symbol being a function or
  583.      a mangled variable that is stored in one of the minimal symbol tables.
  584.      Eventually, all global symbols might be resolved in this way.  */
  585.   
  586.   if (namespace == VAR_NAMESPACE)
  587.     {
  588.       msymbol = lookup_minimal_symbol (name, NULL, NULL);
  589.       if (msymbol != NULL)
  590.     {
  591.       s = find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol));
  592.       if (s != NULL)
  593.         {
  594.           /* This is a function which has a symtab for its address.  */
  595.           bv = BLOCKVECTOR (s);
  596.           block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  597.           sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
  598.                      namespace);
  599.               /* We kept static functions in minimal symbol table as well as
  600.          in static scope. We want to find them in the symbol table. */
  601.         if (!sym) {
  602.           block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  603.           sym = lookup_block_symbol (block, SYMBOL_NAME (msymbol),
  604.                          namespace);
  605.         }
  606.  
  607.           /* sym == 0 if symbol was found in the minimal symbol table
  608.          but not in the symtab.
  609.          Return 0 to use the msymbol definition of "foo_".
  610.  
  611.          This happens for Fortran  "foo_" symbols,
  612.          which are "foo" in the symtab.
  613.  
  614.          This can also happen if "asm" is used to make a
  615.          regular symbol but not a debugging symbol, e.g.
  616.          asm(".globl _main");
  617.          asm("_main:");
  618.          */
  619.  
  620.           if (symtab != NULL)
  621.         *symtab = s;
  622.           return sym;
  623.         }
  624.       else if (MSYMBOL_TYPE (msymbol) != mst_text
  625.            && MSYMBOL_TYPE (msymbol) != mst_file_text
  626.            && !STREQ (name, SYMBOL_NAME (msymbol)))
  627.         {
  628.           /* This is a mangled variable, look it up by its
  629.          mangled name.  */
  630.           return lookup_symbol (SYMBOL_NAME (msymbol), block,
  631.                     namespace, is_a_field_of_this, symtab);
  632.         }
  633.       /* There are no debug symbols for this file, or we are looking
  634.          for an unmangled variable.
  635.          Try to find a matching static symbol below. */
  636.     }
  637.     }
  638.       
  639.   ALL_PSYMTABS (objfile, ps)
  640.     {
  641.       if (!ps->readin && lookup_partial_symbol (ps, name, 1, namespace))
  642.     {
  643.       s = PSYMTAB_TO_SYMTAB(ps);
  644.       bv = BLOCKVECTOR (s);
  645.       block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  646.       sym = lookup_block_symbol (block, name, namespace);
  647.       if (!sym)
  648.         error ("Internal: global symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
  649.       if (symtab != NULL)
  650.         *symtab = s;
  651.       return sym;
  652.     }
  653.     }
  654.  
  655.   /* Now search all per-file blocks.
  656.      Not strictly correct, but more useful than an error.
  657.      Do the symtabs first, then check the psymtabs */
  658.  
  659.   ALL_SYMTABS (objfile, s)
  660.     {
  661.       bv = BLOCKVECTOR (s);
  662.       block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  663.       sym = lookup_block_symbol (block, name, namespace);
  664.       if (sym) 
  665.     {
  666.       block_found = block;
  667.       if (symtab != NULL)
  668.         *symtab = s;
  669.       return sym;
  670.     }
  671.     }
  672.  
  673.   ALL_PSYMTABS (objfile, ps)
  674.     {
  675.       if (!ps->readin && lookup_partial_symbol (ps, name, 0, namespace))
  676.     {
  677.       s = PSYMTAB_TO_SYMTAB(ps);
  678.       bv = BLOCKVECTOR (s);
  679.       block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  680.       sym = lookup_block_symbol (block, name, namespace);
  681.       if (!sym)
  682.         error ("Internal: static symbol `%s' found in %s psymtab but not in symtab", name, ps->filename);
  683.       if (symtab != NULL)
  684.         *symtab = s;
  685.       return sym;
  686.     }
  687.     }
  688.  
  689.   if (symtab != NULL)
  690.     *symtab = NULL;
  691.   return 0;
  692. }
  693.  
  694. /* Look, in partial_symtab PST, for symbol NAME.  Check the global
  695.    symbols if GLOBAL, the static symbols if not */
  696.  
  697. static struct partial_symbol *
  698. lookup_partial_symbol (pst, name, global, namespace)
  699.      struct partial_symtab *pst;
  700.      const char *name;
  701.      int global;
  702.      enum namespace namespace;
  703. {
  704.   struct partial_symbol *start, *psym;
  705.   struct partial_symbol *top, *bottom, *center;
  706.   int length = (global ? pst->n_global_syms : pst->n_static_syms);
  707.   int do_linear_search = 1;
  708.  
  709.   if (length == 0)
  710.     {
  711.       return (NULL);
  712.     }
  713.   
  714.   start = (global ?
  715.        pst->objfile->global_psymbols.list + pst->globals_offset :
  716.        pst->objfile->static_psymbols.list + pst->statics_offset  );
  717.  
  718.   if (global)        /* This means we can use a binary search. */
  719.     {
  720.       do_linear_search = 0;
  721.  
  722.       /* Binary search.  This search is guaranteed to end with center
  723.          pointing at the earliest partial symbol with the correct
  724.      name.  At that point *all* partial symbols with that name
  725.      will be checked against the correct namespace. */
  726.  
  727.       bottom = start;
  728.       top = start + length - 1;
  729.       while (top > bottom)
  730.     {
  731.       center = bottom + (top - bottom) / 2;
  732.       if (!(center < top))
  733.         abort ();
  734.       if (!do_linear_search && SYMBOL_LANGUAGE (center) == language_cplus)
  735.         {
  736.           do_linear_search = 1;
  737.         }
  738.       if (STRCMP (SYMBOL_NAME (center), name) >= 0)
  739.         {
  740.           top = center;
  741.         }
  742.       else
  743.         {
  744.           bottom = center + 1;
  745.         }
  746.     }
  747.       if (!(top == bottom))
  748.     abort ();
  749.       while (STREQ (SYMBOL_NAME (top), name))
  750.     {
  751.       if (SYMBOL_NAMESPACE (top) == namespace)
  752.         {
  753.           return top;
  754.         }
  755.       top ++;
  756.     }
  757.     }
  758.  
  759.   /* Can't use a binary search or else we found during the binary search that
  760.      we should also do a linear search. */
  761.  
  762.   if (do_linear_search)
  763.     {
  764.       for (psym = start; psym < start + length; psym++)
  765.     {
  766.       if (namespace == SYMBOL_NAMESPACE (psym))
  767.         {
  768.           if (SYMBOL_MATCHES_NAME (psym, name))
  769.         {
  770.           return (psym);
  771.         }
  772.         }
  773.     }
  774.     }
  775.  
  776.   return (NULL);
  777. }
  778.  
  779. /* Find the psymtab containing main(). */
  780. /* FIXME:  What about languages without main() or specially linked
  781.    executables that have no main() ? */
  782.  
  783. struct partial_symtab *
  784. find_main_psymtab ()
  785. {
  786.   register struct partial_symtab *pst;
  787.   register struct objfile *objfile;
  788.  
  789.   ALL_PSYMTABS (objfile, pst)
  790.     {
  791.       if (lookup_partial_symbol (pst, "main", 1, VAR_NAMESPACE))
  792.     {
  793.       return (pst);
  794.     }
  795.     }
  796.   return (NULL);
  797. }
  798.  
  799. /* Search BLOCK for symbol NAME in NAMESPACE.
  800.  
  801.    Note that if NAME is the demangled form of a C++ symbol, we will fail
  802.    to find a match during the binary search of the non-encoded names, but
  803.    for now we don't worry about the slight inefficiency of looking for
  804.    a match we'll never find, since it will go pretty quick.  Once the
  805.    binary search terminates, we drop through and do a straight linear
  806.    search on the symbols.  Each symbol which is marked as being a C++
  807.    symbol (language_cplus set) has both the encoded and non-encoded names
  808.    tested for a match. */
  809.  
  810. struct symbol *
  811. lookup_block_symbol (block, name, namespace)
  812.      register const struct block *block;
  813.      const char *name;
  814.      const enum namespace namespace;
  815. {
  816.   register int bot, top, inc;
  817.   register struct symbol *sym;
  818.   register struct symbol *sym_found = NULL;
  819.   register int do_linear_search = 1;
  820.  
  821.   /* If the blocks's symbols were sorted, start with a binary search.  */
  822.  
  823.   if (BLOCK_SHOULD_SORT (block))
  824.     {
  825.       /* Reset the linear search flag so if the binary search fails, we
  826.      won't do the linear search once unless we find some reason to
  827.      do so, such as finding a C++ symbol during the binary search.
  828.      Note that for C++ modules, ALL the symbols in a block should
  829.      end up marked as C++ symbols. */
  830.  
  831.       do_linear_search = 0;
  832.       top = BLOCK_NSYMS (block);
  833.       bot = 0;
  834.  
  835.       /* Advance BOT to not far before the first symbol whose name is NAME. */
  836.  
  837.       while (1)
  838.     {
  839.       inc = (top - bot + 1);
  840.       /* No need to keep binary searching for the last few bits worth.  */
  841.       if (inc < 4)
  842.         {
  843.           break;
  844.         }
  845.       inc = (inc >> 1) + bot;
  846.       sym = BLOCK_SYM (block, inc);
  847.       if (!do_linear_search && SYMBOL_LANGUAGE (sym) == language_cplus)
  848.         {
  849.           do_linear_search = 1;
  850.         }
  851.       if (SYMBOL_NAME (sym)[0] < name[0])
  852.         {
  853.           bot = inc;
  854.         }
  855.       else if (SYMBOL_NAME (sym)[0] > name[0])
  856.         {
  857.           top = inc;
  858.         }
  859.       else if (STRCMP (SYMBOL_NAME (sym), name) < 0)
  860.         {
  861.           bot = inc;
  862.         }
  863.       else
  864.         {
  865.           top = inc;
  866.         }
  867.     }
  868.  
  869.       /* Now scan forward until we run out of symbols, find one whose
  870.      name is greater than NAME, or find one we want.  If there is
  871.      more than one symbol with the right name and namespace, we
  872.      return the first one; I believe it is now impossible for us
  873.      to encounter two symbols with the same name and namespace
  874.      here, because blocks containing argument symbols are no
  875.      longer sorted.  */
  876.  
  877.       top = BLOCK_NSYMS (block);
  878.       while (bot < top)
  879.     {
  880.       sym = BLOCK_SYM (block, bot);
  881.       inc = SYMBOL_NAME (sym)[0] - name[0];
  882.       if (inc == 0)
  883.         {
  884.           inc = STRCMP (SYMBOL_NAME (sym), name);
  885.         }
  886.       if (inc == 0 && SYMBOL_NAMESPACE (sym) == namespace)
  887.         {
  888.           return (sym);
  889.         }
  890.       if (inc > 0)
  891.         {
  892.           break;
  893.         }
  894.       bot++;
  895.     }
  896.     }
  897.  
  898.   /* Here if block isn't sorted, or we fail to find a match during the
  899.      binary search above.  If during the binary search above, we find a
  900.      symbol which is a C++ symbol, then we have re-enabled the linear
  901.      search flag which was reset when starting the binary search.
  902.  
  903.      This loop is equivalent to the loop above, but hacked greatly for speed.
  904.  
  905.      Note that parameter symbols do not always show up last in the
  906.      list; this loop makes sure to take anything else other than
  907.      parameter symbols first; it only uses parameter symbols as a
  908.      last resort.  Note that this only takes up extra computation
  909.      time on a match.  */
  910.  
  911.   if (do_linear_search)
  912.     {
  913.       top = BLOCK_NSYMS (block);
  914.       bot = 0;
  915.       while (bot < top)
  916.     {
  917.       sym = BLOCK_SYM (block, bot);
  918.       if (SYMBOL_NAMESPACE (sym) == namespace &&
  919.           SYMBOL_MATCHES_NAME (sym, name))
  920.         {
  921.           sym_found = sym;
  922.           if (SYMBOL_CLASS (sym) != LOC_ARG &&
  923.           SYMBOL_CLASS (sym) != LOC_LOCAL_ARG &&
  924.           SYMBOL_CLASS (sym) != LOC_REF_ARG &&
  925.           SYMBOL_CLASS (sym) != LOC_REGPARM &&
  926.           SYMBOL_CLASS (sym) != LOC_REGPARM_ADDR &&
  927.           SYMBOL_CLASS (sym) != LOC_BASEREG_ARG)
  928.         {
  929.           break;
  930.         }
  931.         }
  932.       bot++;
  933.     }
  934.     }
  935.   return (sym_found);        /* Will be NULL if not found. */
  936. }
  937.  
  938.  
  939. /* Return the symbol for the function which contains a specified
  940.    lexical block, described by a struct block BL.  */
  941.  
  942. struct symbol *
  943. block_function (bl)
  944.      struct block *bl;
  945. {
  946.   while (BLOCK_FUNCTION (bl) == 0 && BLOCK_SUPERBLOCK (bl) != 0)
  947.     bl = BLOCK_SUPERBLOCK (bl);
  948.  
  949.   return BLOCK_FUNCTION (bl);
  950. }
  951.  
  952. /* Find the symtab associated with PC.  Look through the psymtabs and read in
  953.    another symtab if necessary. */
  954.  
  955. struct symtab *
  956. find_pc_symtab (pc)
  957.      register CORE_ADDR pc;
  958. {
  959.   register struct block *b;
  960.   struct blockvector *bv;
  961.   register struct symtab *s = NULL;
  962.   register struct symtab *best_s = NULL;
  963.   register struct partial_symtab *ps;
  964.   register struct objfile *objfile;
  965.   int distance = 0;
  966.  
  967.   /* Search all symtabs for the one whose file contains our address, and which
  968.      is the smallest of all the ones containing the address.  This is designed
  969.      to deal with a case like symtab a is at 0x1000-0x2000 and 0x3000-0x4000
  970.      and symtab b is at 0x2000-0x3000.  So the GLOBAL_BLOCK for a is from
  971.      0x1000-0x4000, but for address 0x2345 we want to return symtab b.
  972.  
  973.      This happens for native ecoff format, where code from included files
  974.      gets its own symtab. The symtab for the included file should have
  975.      been read in already via the dependency mechanism.
  976.      It might be swifter to create several symtabs with the same name
  977.      like xcoff does (I'm not sure).
  978.  
  979.      It also happens for objfiles that have their functions reordered.
  980.      For these, the symtab we are looking for is not necessarily read in.  */
  981.  
  982.   ALL_SYMTABS (objfile, s)
  983.     {
  984.       bv = BLOCKVECTOR (s);
  985.       b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
  986.       if (BLOCK_START (b) <= pc
  987.       && BLOCK_END (b) > pc
  988.       && (distance == 0
  989.           || BLOCK_END (b) - BLOCK_START (b) < distance))
  990.     {
  991.       /* For an objfile that has its functions reordered,
  992.          find_pc_psymtab will find the proper partial symbol table
  993.          and we simply return its corresponding symtab.  */
  994.       if (objfile->flags & OBJF_REORDERED)
  995.         {
  996.           ps = find_pc_psymtab (pc);
  997.           if (ps)
  998.         s = PSYMTAB_TO_SYMTAB (ps);
  999.           else
  1000.             s = NULL;
  1001.           return (s);
  1002.         }
  1003.       distance = BLOCK_END (b) - BLOCK_START (b);
  1004.       best_s = s;
  1005.     }
  1006.     }
  1007.  
  1008.   if (best_s != NULL)
  1009.     return(best_s);
  1010.  
  1011.   s = NULL;
  1012.   ps = find_pc_psymtab (pc);
  1013.   if (ps)
  1014.     {
  1015.       if (ps->readin)
  1016.     /* Might want to error() here (in case symtab is corrupt and
  1017.        will cause a core dump), but maybe we can successfully
  1018.        continue, so let's not.  */
  1019.     /* FIXME-32x64: assumes pc fits in a long */
  1020.     warning ("\
  1021. (Internal error: pc 0x%lx in read in psymtab, but not in symtab.)\n",
  1022.              (unsigned long) pc);
  1023.       s = PSYMTAB_TO_SYMTAB (ps);
  1024.     }
  1025.   return (s);
  1026. }
  1027.  
  1028. #if 0
  1029.  
  1030. /* Find the closest symbol value (of any sort -- function or variable)
  1031.    for a given address value.  Slow but complete.  (currently unused,
  1032.    mainly because it is too slow.  We could fix it if each symtab and
  1033.    psymtab had contained in it the addresses ranges of each of its
  1034.    sections, which also would be required to make things like "info
  1035.    line *0x2345" cause psymtabs to be converted to symtabs).  */
  1036.  
  1037. struct symbol *
  1038. find_addr_symbol (addr, symtabp, symaddrp)
  1039.      CORE_ADDR addr;
  1040.      struct symtab **symtabp;
  1041.      CORE_ADDR *symaddrp;
  1042. {
  1043.   struct symtab *symtab, *best_symtab;
  1044.   struct objfile *objfile;
  1045.   register int bot, top;
  1046.   register struct symbol *sym;
  1047.   register CORE_ADDR sym_addr;
  1048.   struct block *block;
  1049.   int blocknum;
  1050.  
  1051.   /* Info on best symbol seen so far */
  1052.  
  1053.   register CORE_ADDR best_sym_addr = 0;
  1054.   struct symbol *best_sym = 0;
  1055.  
  1056.   /* FIXME -- we should pull in all the psymtabs, too!  */
  1057.   ALL_SYMTABS (objfile, symtab)
  1058.     {
  1059.       /* Search the global and static blocks in this symtab for
  1060.      the closest symbol-address to the desired address.  */
  1061.  
  1062.       for (blocknum = GLOBAL_BLOCK; blocknum <= STATIC_BLOCK; blocknum++)
  1063.     {
  1064.       QUIT;
  1065.       block = BLOCKVECTOR_BLOCK (BLOCKVECTOR (symtab), blocknum);
  1066.       top = BLOCK_NSYMS (block);
  1067.       for (bot = 0; bot < top; bot++)
  1068.         {
  1069.           sym = BLOCK_SYM (block, bot);
  1070.           switch (SYMBOL_CLASS (sym))
  1071.         {
  1072.         case LOC_STATIC:    
  1073.         case LOC_LABEL:    
  1074.           sym_addr = SYMBOL_VALUE_ADDRESS (sym);
  1075.           break;
  1076.  
  1077.         case LOC_BLOCK:
  1078.           sym_addr = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  1079.           break;
  1080.  
  1081.         default:
  1082.           continue;
  1083.         }
  1084.  
  1085.         if (sym_addr <= addr)
  1086.           if (sym_addr > best_sym_addr)
  1087.             {
  1088.               /* Quit if we found an exact match.  */
  1089.               best_sym = sym;
  1090.               best_sym_addr = sym_addr;
  1091.               best_symtab = symtab;
  1092.               if (sym_addr == addr)
  1093.             goto done;
  1094.             }
  1095.         }
  1096.     }
  1097.     }
  1098.  
  1099.  done:
  1100.   if (symtabp)
  1101.     *symtabp = best_symtab;
  1102.   if (symaddrp)
  1103.     *symaddrp = best_sym_addr;
  1104.   return best_sym;
  1105. }
  1106. #endif /* 0 */
  1107.  
  1108. /* Find the source file and line number for a given PC value.
  1109.    Return a structure containing a symtab pointer, a line number,
  1110.    and a pc range for the entire source line.
  1111.    The value's .pc field is NOT the specified pc.
  1112.    NOTCURRENT nonzero means, if specified pc is on a line boundary,
  1113.    use the line that ends there.  Otherwise, in that case, the line
  1114.    that begins there is used.  */
  1115.  
  1116. /* The big complication here is that a line may start in one file, and end just
  1117.    before the start of another file.  This usually occurs when you #include
  1118.    code in the middle of a subroutine.  To properly find the end of a line's PC
  1119.    range, we must search all symtabs associated with this compilation unit, and
  1120.    find the one whose first PC is closer than that of the next line in this
  1121.    symtab.  */
  1122.  
  1123. /* If it's worth the effort, we could be using a binary search.  */
  1124.  
  1125. struct symtab_and_line
  1126. find_pc_line (pc, notcurrent)
  1127.      CORE_ADDR pc;
  1128.      int notcurrent;
  1129. {
  1130.   struct symtab *s;
  1131.   register struct linetable *l;
  1132.   register int len;
  1133.   register int i;
  1134.   register struct linetable_entry *item;
  1135.   struct symtab_and_line val;
  1136.   struct blockvector *bv;
  1137.  
  1138.   /* Info on best line seen so far, and where it starts, and its file.  */
  1139.  
  1140.   struct linetable_entry *best = NULL;
  1141.   CORE_ADDR best_end = 0;
  1142.   struct symtab *best_symtab = 0;
  1143.  
  1144.   /* Store here the first line number
  1145.      of a file which contains the line at the smallest pc after PC.
  1146.      If we don't find a line whose range contains PC,
  1147.      we will use a line one less than this,
  1148.      with a range from the start of that file to the first line's pc.  */
  1149.   struct linetable_entry *alt = NULL;
  1150.   struct symtab *alt_symtab = 0;
  1151.  
  1152.   /* Info on best line seen in this file.  */
  1153.  
  1154.   struct linetable_entry *prev;
  1155.  
  1156.   /* If this pc is not from the current frame,
  1157.      it is the address of the end of a call instruction.
  1158.      Quite likely that is the start of the following statement.
  1159.      But what we want is the statement containing the instruction.
  1160.      Fudge the pc to make sure we get that.  */
  1161.  
  1162.   if (notcurrent) pc -= 1;
  1163.  
  1164.   s = find_pc_symtab (pc);
  1165.   if (!s)
  1166.     {
  1167.       val.symtab = 0;
  1168.       val.line = 0;
  1169.       val.pc = pc;
  1170.       val.end = 0;
  1171.       return val;
  1172.     }
  1173.  
  1174.   bv = BLOCKVECTOR (s);
  1175.  
  1176.   /* Look at all the symtabs that share this blockvector.
  1177.      They all have the same apriori range, that we found was right;
  1178.      but they have different line tables.  */
  1179.  
  1180.   for (; s && BLOCKVECTOR (s) == bv; s = s->next)
  1181.     {
  1182.       /* Find the best line in this symtab.  */
  1183.       l = LINETABLE (s);
  1184.       if (!l)
  1185.         continue;
  1186.       len = l->nitems;
  1187.       if (len <= 0)
  1188.     {
  1189.       /* I think len can be zero if the symtab lacks line numbers
  1190.          (e.g. gcc -g1).  (Either that or the LINETABLE is NULL;
  1191.          I'm not sure which, and maybe it depends on the symbol
  1192.          reader).  */
  1193.       continue;
  1194.     }
  1195.  
  1196.       prev = NULL;
  1197.       item = l->item;        /* Get first line info */
  1198.  
  1199.       /* Is this file's first line closer than the first lines of other files?
  1200.      If so, record this file, and its first line, as best alternate.  */
  1201.       if (item->pc > pc && (!alt || item->pc < alt->pc))
  1202.     {
  1203.       alt = item;
  1204.       alt_symtab = s;
  1205.     }
  1206.  
  1207.       for (i = 0; i < len; i++, item++)
  1208.     {
  1209.       /* Return the last line that did not start after PC.  */
  1210.       if (item->pc > pc)
  1211.         break;
  1212.  
  1213.       prev = item;
  1214.     }
  1215.  
  1216.       /* At this point, prev points at the line whose start addr is <= pc, and
  1217.      item points at the next line.  If we ran off the end of the linetable
  1218.      (pc >= start of the last line), then prev == item.  If pc < start of
  1219.      the first line, prev will not be set.  */
  1220.  
  1221.       /* Is this file's best line closer than the best in the other files?
  1222.      If so, record this file, and its best line, as best so far.  */
  1223.  
  1224.       if (prev && (!best || prev->pc > best->pc))
  1225.     {
  1226.       best = prev;
  1227.       best_symtab = s;
  1228.       /* If another line is in the linetable, and its PC is closer
  1229.          than the best_end we currently have, take it as best_end.  */
  1230.       if (i < len && (best_end == 0 || best_end > item->pc))
  1231.         best_end = item->pc;
  1232.     }
  1233.     }
  1234.  
  1235.   if (!best_symtab)
  1236.     {
  1237.       if (!alt_symtab)
  1238.     {            /* If we didn't find any line # info, just
  1239.                  return zeros.  */
  1240.       val.symtab = 0;
  1241.       val.line = 0;
  1242.       val.pc = pc;
  1243.       val.end = 0;
  1244.     }
  1245.       else
  1246.     {
  1247.       val.symtab = alt_symtab;
  1248.       val.line = alt->line - 1;
  1249.  
  1250.       /* Don't return line 0, that means that we didn't find the line.  */
  1251.       if (val.line == 0) ++val.line;
  1252.  
  1253.       val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
  1254.       val.end = alt->pc;
  1255.     }
  1256.     }
  1257.   else
  1258.     {
  1259.       val.symtab = best_symtab;
  1260.       val.line = best->line;
  1261.       val.pc = best->pc;
  1262.       if (best_end && (!alt || best_end < alt->pc))
  1263.     val.end = best_end;
  1264.       else if (alt)
  1265.     val.end = alt->pc;
  1266.       else
  1267.     val.end = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
  1268.     }
  1269.   return val;
  1270. }
  1271.  
  1272. static int find_line_symtab PARAMS ((struct symtab *, int, struct linetable **,
  1273.                      int *, int *));
  1274.  
  1275. /* Find line number LINE in any symtab whose name is the same as
  1276.    SYMTAB.
  1277.  
  1278.    If found, return 1, set *LINETABLE to the linetable in which it was
  1279.    found, set *INDEX to the index in the linetable of the best entry
  1280.    found, and set *EXACT_MATCH nonzero if the value returned is an
  1281.    exact match.
  1282.  
  1283.    If not found, return 0.  */
  1284.  
  1285. static int
  1286. find_line_symtab (symtab, line, linetable, index, exact_match)
  1287.      struct symtab *symtab;
  1288.      int line;
  1289.      struct linetable **linetable;
  1290.      int *index;
  1291.      int *exact_match;
  1292. {
  1293.   int exact;
  1294.  
  1295.   /* BEST_INDEX and BEST_LINETABLE identify the smallest linenumber > LINE
  1296.      so far seen.  */
  1297.  
  1298.   int best_index;
  1299.   struct linetable *best_linetable;
  1300.  
  1301.   /* First try looking it up in the given symtab.  */
  1302.   best_linetable = LINETABLE (symtab);
  1303.   best_index = find_line_common (best_linetable, line, &exact);
  1304.   if (best_index < 0 || !exact)
  1305.     {
  1306.       /* Didn't find an exact match.  So we better keep looking for
  1307.      another symtab with the same name.  In the case of xcoff,
  1308.      multiple csects for one source file (produced by IBM's FORTRAN
  1309.      compiler) produce multiple symtabs (this is unavoidable
  1310.      assuming csects can be at arbitrary places in memory and that
  1311.      the GLOBAL_BLOCK of a symtab has a begin and end address).  */
  1312.  
  1313.       /* BEST is the smallest linenumber > LINE so far seen,
  1314.      or 0 if none has been seen so far.
  1315.      BEST_INDEX and BEST_LINETABLE identify the item for it.  */
  1316.       int best;
  1317.  
  1318.       struct objfile *objfile;
  1319.       struct symtab *s;
  1320.  
  1321.       if (best_index >= 0)
  1322.     best = best_linetable->item[best_index].line;
  1323.       else
  1324.     best = 0;
  1325.  
  1326.       ALL_SYMTABS (objfile, s)
  1327.     {
  1328.       struct linetable *l;
  1329.       int ind;
  1330.  
  1331.       if (!STREQ (symtab->filename, s->filename))
  1332.         continue;
  1333.       l = LINETABLE (s);
  1334.       ind = find_line_common (l, line, &exact);
  1335.       if (ind >= 0)
  1336.         {
  1337.           if (exact)
  1338.         {
  1339.           best_index = ind;
  1340.           best_linetable = l;
  1341.           goto done;
  1342.         }
  1343.           if (best == 0 || l->item[ind].line < best)
  1344.         {
  1345.           best = l->item[ind].line;
  1346.           best_index = ind;
  1347.           best_linetable = l;
  1348.         }
  1349.         }
  1350.     }
  1351.     }
  1352.  done:
  1353.   if (best_index < 0)
  1354.     return 0;
  1355.  
  1356.   if (index)
  1357.     *index = best_index;
  1358.   if (linetable)
  1359.     *linetable = best_linetable;
  1360.   if (exact_match)
  1361.     *exact_match = exact;
  1362.   return 1;
  1363. }
  1364.  
  1365. /* Find the PC value for a given source file and line number.
  1366.    Returns zero for invalid line number.
  1367.    The source file is specified with a struct symtab.  */
  1368.  
  1369. CORE_ADDR
  1370. find_line_pc (symtab, line)
  1371.      struct symtab *symtab;
  1372.      int line;
  1373. {
  1374.   struct linetable *l;
  1375.   int ind;
  1376.  
  1377.   if (symtab == 0)
  1378.     return 0;
  1379.   if (find_line_symtab (symtab, line, &l, &ind, NULL))
  1380.     return l->item[ind].pc;
  1381.   else
  1382.     return 0;
  1383. }
  1384.  
  1385. /* Find the range of pc values in a line.
  1386.    Store the starting pc of the line into *STARTPTR
  1387.    and the ending pc (start of next line) into *ENDPTR.
  1388.    Returns 1 to indicate success.
  1389.    Returns 0 if could not find the specified line.  */
  1390.  
  1391. int
  1392. find_line_pc_range (sal, startptr, endptr)
  1393.      struct symtab_and_line sal;
  1394.      CORE_ADDR *startptr, *endptr;
  1395. {
  1396.   CORE_ADDR startaddr;
  1397.   struct symtab_and_line found_sal;
  1398.  
  1399.   startaddr = sal.pc;
  1400.   if (startaddr == 0)
  1401.     {
  1402.       startaddr = find_line_pc (sal.symtab, sal.line);
  1403.     }
  1404.   if (startaddr == 0)
  1405.     return 0;
  1406.  
  1407.   /* This whole function is based on address.  For example, if line 10 has
  1408.      two parts, one from 0x100 to 0x200 and one from 0x300 to 0x400, then
  1409.      "info line *0x123" should say the line goes from 0x100 to 0x200
  1410.      and "info line *0x355" should say the line goes from 0x300 to 0x400.
  1411.      This also insures that we never give a range like "starts at 0x134
  1412.      and ends at 0x12c".  */
  1413.  
  1414.   found_sal = find_pc_line (startaddr, 0);
  1415.   if (found_sal.line != sal.line)
  1416.     {
  1417.       /* The specified line (sal) has zero bytes.  */
  1418.       *startptr = found_sal.pc;
  1419.       *endptr = found_sal.pc;
  1420.     }
  1421.   else
  1422.     {
  1423.       *startptr = found_sal.pc;
  1424.       *endptr = found_sal.end;
  1425.     }
  1426.   return 1;
  1427. }
  1428.  
  1429. /* Given a line table and a line number, return the index into the line
  1430.    table for the pc of the nearest line whose number is >= the specified one.
  1431.    Return -1 if none is found.  The value is >= 0 if it is an index.
  1432.  
  1433.    Set *EXACT_MATCH nonzero if the value returned is an exact match.  */
  1434.  
  1435. static int
  1436. find_line_common (l, lineno, exact_match)
  1437.      register struct linetable *l;
  1438.      register int lineno;
  1439.      int *exact_match;
  1440. {
  1441.   register int i;
  1442.   register int len;
  1443.  
  1444.   /* BEST is the smallest linenumber > LINENO so far seen,
  1445.      or 0 if none has been seen so far.
  1446.      BEST_INDEX identifies the item for it.  */
  1447.  
  1448.   int best_index = -1;
  1449.   int best = 0;
  1450.  
  1451.   if (lineno <= 0)
  1452.     return -1;
  1453.   if (l == 0)
  1454.     return -1;
  1455.  
  1456.   len = l->nitems;
  1457.   for (i = 0; i < len; i++)
  1458.     {
  1459.       register struct linetable_entry *item = &(l->item[i]);
  1460.  
  1461.       if (item->line == lineno)
  1462.     {
  1463.       /* Return the first (lowest address) entry which matches.  */
  1464.       *exact_match = 1;
  1465.       return i;
  1466.     }
  1467.  
  1468.       if (item->line > lineno && (best == 0 || item->line < best))
  1469.     {
  1470.       best = item->line;
  1471.       best_index = i;
  1472.     }
  1473.     }
  1474.  
  1475.   /* If we got here, we didn't get an exact match.  */
  1476.  
  1477.   *exact_match = 0;
  1478.   return best_index;
  1479. }
  1480.  
  1481. int
  1482. find_pc_line_pc_range (pc, startptr, endptr)
  1483.      CORE_ADDR pc;
  1484.      CORE_ADDR *startptr, *endptr;
  1485. {
  1486.   struct symtab_and_line sal;
  1487.   sal = find_pc_line (pc, 0);
  1488.   *startptr = sal.pc;
  1489.   *endptr = sal.end;
  1490.   return sal.symtab != 0;
  1491. }
  1492.  
  1493. /* Given a function symbol SYM, find the symtab and line for the start
  1494.    of the function.
  1495.    If the argument FUNFIRSTLINE is nonzero, we want the first line
  1496.    of real code inside the function.  */
  1497.  
  1498. static struct symtab_and_line
  1499. find_function_start_sal PARAMS ((struct symbol *sym, int));
  1500.  
  1501. static struct symtab_and_line
  1502. find_function_start_sal (sym, funfirstline)
  1503.      struct symbol *sym;
  1504.      int funfirstline;
  1505. {
  1506.   CORE_ADDR pc;
  1507.   struct symtab_and_line sal;
  1508.  
  1509.   pc = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  1510.   if (funfirstline)
  1511.     {
  1512.       pc += FUNCTION_START_OFFSET;
  1513.       SKIP_PROLOGUE (pc);
  1514.     }
  1515.   sal = find_pc_line (pc, 0);
  1516.  
  1517. #ifdef PROLOGUE_FIRSTLINE_OVERLAP
  1518.   /* Convex: no need to suppress code on first line, if any */
  1519.   sal.pc = pc;
  1520. #else
  1521.   /* Check if SKIP_PROLOGUE left us in mid-line, and the next
  1522.      line is still part of the same function.  */
  1523.   if (sal.pc != pc
  1524.       && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) <= sal.end
  1525.       && sal.end < BLOCK_END (SYMBOL_BLOCK_VALUE (sym)))
  1526.     {
  1527.       /* First pc of next line */
  1528.       pc = sal.end;
  1529.       /* Recalculate the line number (might not be N+1).  */
  1530.       sal = find_pc_line (pc, 0);
  1531.     }
  1532.   sal.pc = pc;
  1533. #endif
  1534.  
  1535.   return sal;
  1536. }
  1537.  
  1538. /* If P is of the form "operator[ \t]+..." where `...' is
  1539.    some legitimate operator text, return a pointer to the
  1540.    beginning of the substring of the operator text.
  1541.    Otherwise, return "".  */
  1542. static char *
  1543. operator_chars (p, end)
  1544.      char *p;
  1545.      char **end;
  1546. {
  1547.   *end = "";
  1548.   if (strncmp (p, "operator", 8))
  1549.     return *end;
  1550.   p += 8;
  1551.  
  1552.   /* Don't get faked out by `operator' being part of a longer
  1553.      identifier.  */
  1554.   if (isalpha(*p) || *p == '_' || *p == '$' || *p == '\0')
  1555.     return *end;
  1556.  
  1557.   /* Allow some whitespace between `operator' and the operator symbol.  */
  1558.   while (*p == ' ' || *p == '\t')
  1559.     p++;
  1560.  
  1561.   /* Recognize 'operator TYPENAME'. */
  1562.  
  1563.   if (isalpha(*p) || *p == '_' || *p == '$')
  1564.     {
  1565.       register char *q = p+1;
  1566.       while (isalnum(*q) || *q == '_' || *q == '$')
  1567.     q++;
  1568.       *end = q;
  1569.       return p;
  1570.     }
  1571.  
  1572.   switch (*p)
  1573.     {
  1574.     case '!':
  1575.     case '=':
  1576.     case '*':
  1577.     case '/':
  1578.     case '%':
  1579.     case '^':
  1580.       if (p[1] == '=')
  1581.     *end = p+2;
  1582.       else
  1583.     *end = p+1;
  1584.       return p;
  1585.     case '<':
  1586.     case '>':
  1587.     case '+':
  1588.     case '-':
  1589.     case '&':
  1590.     case '|':
  1591.       if (p[1] == '=' || p[1] == p[0])
  1592.     *end = p+2;
  1593.       else
  1594.     *end = p+1;
  1595.       return p;
  1596.     case '~':
  1597.     case ',':
  1598.       *end = p+1;
  1599.       return p;
  1600.     case '(':
  1601.       if (p[1] != ')')
  1602.     error ("`operator ()' must be specified without whitespace in `()'");
  1603.       *end = p+2;
  1604.       return p;
  1605.     case '?':
  1606.       if (p[1] != ':')
  1607.     error ("`operator ?:' must be specified without whitespace in `?:'");
  1608.       *end = p+2;
  1609.       return p;
  1610.     case '[':
  1611.       if (p[1] != ']')
  1612.     error ("`operator []' must be specified without whitespace in `[]'");
  1613.       *end = p+2;
  1614.       return p;
  1615.     default:
  1616.       error ("`operator %s' not supported", p);
  1617.       break;
  1618.     }
  1619.   *end = "";
  1620.   return *end;
  1621. }
  1622.  
  1623. /* Return the number of methods described for TYPE, including the
  1624.    methods from types it derives from. This can't be done in the symbol
  1625.    reader because the type of the baseclass might still be stubbed
  1626.    when the definition of the derived class is parsed.  */
  1627.  
  1628. static int total_number_of_methods PARAMS ((struct type *type));
  1629.  
  1630. static int
  1631. total_number_of_methods (type)
  1632.      struct type *type;
  1633. {
  1634.   int n;
  1635.   int count;
  1636.  
  1637.   check_stub_type (type);
  1638.   count = TYPE_NFN_FIELDS_TOTAL (type);
  1639.  
  1640.   for (n = 0; n < TYPE_N_BASECLASSES (type); n++)
  1641.     count += total_number_of_methods (TYPE_BASECLASS (type, n));
  1642.  
  1643.   return count;
  1644. }
  1645.  
  1646. /* Recursive helper function for decode_line_1.
  1647.    Look for methods named NAME in type T.
  1648.    Return number of matches.
  1649.    Put matches in SYM_ARR, which should have been allocated with
  1650.    a size of total_number_of_methods (T) * sizeof (struct symbol *).
  1651.    Note that this function is g++ specific.  */
  1652.  
  1653. int
  1654. find_methods (t, name, sym_arr)
  1655.      struct type *t;
  1656.      char *name;
  1657.      struct symbol **sym_arr;
  1658. {
  1659.   int i1 = 0;
  1660.   int ibase;
  1661.   struct symbol *sym_class;
  1662.   char *class_name = type_name_no_tag (t);
  1663.   /* Ignore this class if it doesn't have a name.  This is ugly, but
  1664.      unless we figure out how to get the physname without the name of
  1665.      the class, then the loop can't do any good.  */
  1666.   if (class_name
  1667.       && (sym_class = lookup_symbol (class_name,
  1668.                      (struct block *)NULL,
  1669.                      STRUCT_NAMESPACE,
  1670.                      (int *)NULL,
  1671.                      (struct symtab **)NULL)))
  1672.     {
  1673.       int method_counter;
  1674.       /* FIXME: Shouldn't this just be check_stub_type (t)?  */
  1675.       t = SYMBOL_TYPE (sym_class);
  1676.       for (method_counter = TYPE_NFN_FIELDS (t) - 1;
  1677.        method_counter >= 0;
  1678.        --method_counter)
  1679.     {
  1680.       int field_counter;
  1681.       struct fn_field *f = TYPE_FN_FIELDLIST1 (t, method_counter);
  1682.       char *method_name = TYPE_FN_FIELDLIST_NAME (t, method_counter);
  1683.       char dem_opname[64];
  1684.  
  1685.           if (strncmp(method_name, "__", 2)==0 ||
  1686.         strncmp(method_name, "op", 2)==0 ||
  1687.         strncmp(method_name, "type", 4)==0 )
  1688.         {
  1689.           if (cplus_demangle_opname(method_name, dem_opname, DMGL_ANSI))
  1690.             method_name = dem_opname;
  1691.           else if (cplus_demangle_opname(method_name, dem_opname, 0))
  1692.             method_name = dem_opname; 
  1693.         }
  1694.       if (STREQ (name, method_name))
  1695.         /* Find all the fields with that name.  */
  1696.         for (field_counter = TYPE_FN_FIELDLIST_LENGTH (t, method_counter) - 1;
  1697.          field_counter >= 0;
  1698.          --field_counter)
  1699.           {
  1700.         char *phys_name;
  1701.         if (TYPE_FN_FIELD_STUB (f, field_counter))
  1702.           check_stub_method (t, method_counter, field_counter);
  1703.         phys_name = TYPE_FN_FIELD_PHYSNAME (f, field_counter);
  1704.         /* Destructor is handled by caller, dont add it to the list */
  1705.         if (DESTRUCTOR_PREFIX_P (phys_name))
  1706.           continue;
  1707.  
  1708.         /* FIXME: Why are we looking this up in the
  1709.            SYMBOL_BLOCK_VALUE (sym_class)?  It is intended as a hook
  1710.            for nested types?  If so, it should probably hook to the
  1711.            type, not the symbol.  mipsread.c is the only symbol
  1712.            reader which sets the SYMBOL_BLOCK_VALUE for types, and
  1713.            this is not documented in symtab.h.  -26Aug93.  */
  1714.  
  1715.         sym_arr[i1] = lookup_symbol (phys_name,
  1716.                          SYMBOL_BLOCK_VALUE (sym_class),
  1717.                          VAR_NAMESPACE,
  1718.                          (int *) NULL,
  1719.                          (struct symtab **) NULL);
  1720.         if (sym_arr[i1]) i1++;
  1721.         else
  1722.           {
  1723.             fputs_filtered("(Cannot find method ", gdb_stdout);
  1724.             fprintf_symbol_filtered (gdb_stdout, phys_name,
  1725.                          language_cplus,
  1726.                          DMGL_PARAMS | DMGL_ANSI);
  1727.             fputs_filtered(" - possibly inlined.)\n", gdb_stdout);
  1728.           }
  1729.           }
  1730.     }
  1731.     }
  1732.  
  1733.   /* Only search baseclasses if there is no match yet, since names in
  1734.      derived classes override those in baseclasses.
  1735.  
  1736.      FIXME: The above is not true; it is only true of member functions
  1737.      if they have the same number of arguments (??? - section 13.1 of the
  1738.      ARM says the function members are not in the same scope but doesn't
  1739.      really spell out the rules in a way I understand.  In any case, if
  1740.      the number of arguments differ this is a case in which we can overload
  1741.      rather than hiding without any problem, and gcc 2.4.5 does overload
  1742.      rather than hiding in this case).  */
  1743.  
  1744.   if (i1)
  1745.     return i1;
  1746.   for (ibase = 0; ibase < TYPE_N_BASECLASSES (t); ibase++)
  1747.     i1 += find_methods(TYPE_BASECLASS(t, ibase), name,
  1748.                sym_arr + i1);
  1749.   return i1;
  1750. }
  1751.  
  1752. /* Helper function for decode_line_1.
  1753.    Build a canonical line spec in CANONICAL if it is non-NULL and if
  1754.    the SAL has a symtab.
  1755.    If SYMNAME is non-NULL the canonical line spec is `filename:symname'.
  1756.    If SYMNAME is NULL the line number from SAL is used and the canonical
  1757.    line spec is `filename:linenum'.  */
  1758.  
  1759. static void
  1760. build_canonical_line_spec (sal, symname, canonical)
  1761.      struct symtab_and_line *sal;
  1762.      char *symname;
  1763.      char ***canonical;
  1764. {
  1765.   char **canonical_arr;
  1766.   char *canonical_name;
  1767.   char *filename;
  1768.   struct symtab *s = sal->symtab;
  1769.  
  1770.   if (s == (struct symtab *)NULL
  1771.       || s->filename == (char *)NULL
  1772.       || canonical == (char ***)NULL)
  1773.     return;
  1774.  
  1775.   canonical_arr = (char **) xmalloc (sizeof (char *));
  1776.   *canonical = canonical_arr;
  1777.  
  1778.   filename = s->filename;
  1779.   if (symname != NULL)
  1780.     {
  1781.       canonical_name = xmalloc (strlen (filename) + strlen (symname) + 2);
  1782.       sprintf (canonical_name, "%s:%s", filename, symname);
  1783.     }
  1784.   else
  1785.     {
  1786.       canonical_name = xmalloc (strlen (filename) + 30);
  1787.       sprintf (canonical_name, "%s:%d", filename, sal->line);
  1788.     }
  1789.   canonical_arr[0] = canonical_name;
  1790. }
  1791.  
  1792. /* Parse a string that specifies a line number.
  1793.    Pass the address of a char * variable; that variable will be
  1794.    advanced over the characters actually parsed.
  1795.  
  1796.    The string can be:
  1797.  
  1798.    LINENUM -- that line number in current file.  PC returned is 0.
  1799.    FILE:LINENUM -- that line in that file.  PC returned is 0.
  1800.    FUNCTION -- line number of openbrace of that function.
  1801.       PC returned is the start of the function.
  1802.    VARIABLE -- line number of definition of that variable.
  1803.       PC returned is 0.
  1804.    FILE:FUNCTION -- likewise, but prefer functions in that file.
  1805.    *EXPR -- line in which address EXPR appears.
  1806.  
  1807.    FUNCTION may be an undebuggable function found in minimal symbol table.
  1808.  
  1809.    If the argument FUNFIRSTLINE is nonzero, we want the first line
  1810.    of real code inside a function when a function is specified, and it is
  1811.    not OK to specify a variable or type to get its line number.
  1812.  
  1813.    DEFAULT_SYMTAB specifies the file to use if none is specified.
  1814.    It defaults to current_source_symtab.
  1815.    DEFAULT_LINE specifies the line number to use for relative
  1816.    line numbers (that start with signs).  Defaults to current_source_line.
  1817.    If CANONICAL is non-NULL, store an array of strings containing the canonical
  1818.    line specs there if necessary. Currently overloaded member functions and
  1819.    line numbers or static functions without a filename yield a canonical
  1820.    line spec. The array and the line spec strings are allocated on the heap,
  1821.    it is the callers responsibility to free them.
  1822.  
  1823.    Note that it is possible to return zero for the symtab
  1824.    if no file is validly specified.  Callers must check that.
  1825.    Also, the line number returned may be invalid.  */
  1826.  
  1827. /* We allow single quotes in various places.  This is a hideous
  1828.    kludge, which exists because the completer can't yet deal with the
  1829.    lack of single quotes.  FIXME: write a linespec_completer which we
  1830.    can use as appropriate instead of make_symbol_completion_list.  */
  1831.  
  1832. struct symtabs_and_lines
  1833. decode_line_1 (argptr, funfirstline, default_symtab, default_line, canonical)
  1834.      char **argptr;
  1835.      int funfirstline;
  1836.      struct symtab *default_symtab;
  1837.      int default_line;
  1838.      char ***canonical;
  1839. {
  1840.   struct symtabs_and_lines values;
  1841. #ifdef HPPA_COMPILER_BUG
  1842.   /* FIXME: The native HP 9000/700 compiler has a bug which appears
  1843.      when optimizing this file with target i960-vxworks.  I haven't
  1844.      been able to construct a simple test case.  The problem is that
  1845.      in the second call to SKIP_PROLOGUE below, the compiler somehow
  1846.      does not realize that the statement val = find_pc_line (...) will
  1847.      change the values of the fields of val.  It extracts the elements
  1848.      into registers at the top of the block, and does not update the
  1849.      registers after the call to find_pc_line.  You can check this by
  1850.      inserting a printf at the end of find_pc_line to show what values
  1851.      it is returning for val.pc and val.end and another printf after
  1852.      the call to see what values the function actually got (remember,
  1853.      this is compiling with cc -O, with this patch removed).  You can
  1854.      also examine the assembly listing: search for the second call to
  1855.      skip_prologue; the LDO statement before the next call to
  1856.      find_pc_line loads the address of the structure which
  1857.      find_pc_line will return; if there is a LDW just before the LDO,
  1858.      which fetches an element of the structure, then the compiler
  1859.      still has the bug.
  1860.  
  1861.      Setting val to volatile avoids the problem.  We must undef
  1862.      volatile, because the HPPA native compiler does not define
  1863.      __STDC__, although it does understand volatile, and so volatile
  1864.      will have been defined away in defs.h.  */
  1865. #undef volatile
  1866.   volatile struct symtab_and_line val;
  1867. #define volatile /*nothing*/
  1868. #else
  1869.   struct symtab_and_line val;
  1870. #endif
  1871.   register char *p, *p1;
  1872.   char *q, *pp;
  1873. #if 0
  1874.   char *q1;
  1875. #endif
  1876.   register struct symtab *s;
  1877.  
  1878.   register struct symbol *sym;
  1879.   /* The symtab that SYM was found in.  */
  1880.   struct symtab *sym_symtab;
  1881.  
  1882.   register CORE_ADDR pc;
  1883.   register struct minimal_symbol *msymbol;
  1884.   char *copy;
  1885.   struct symbol *sym_class;
  1886.   int i1;
  1887.   int is_quoted, has_parens;
  1888.   struct symbol **sym_arr;
  1889.   struct type *t;
  1890.   char *saved_arg = *argptr;
  1891.   extern char *gdb_completer_quote_characters;
  1892.   
  1893.   /* Defaults have defaults.  */
  1894.  
  1895.   if (default_symtab == 0)
  1896.     {
  1897.       default_symtab = current_source_symtab;
  1898.       default_line = current_source_line;
  1899.     }
  1900.  
  1901.   /* See if arg is *PC */
  1902.  
  1903.   if (**argptr == '*')
  1904.     {
  1905.       (*argptr)++;
  1906.       pc = parse_and_eval_address_1 (argptr);
  1907.       values.sals = (struct symtab_and_line *)
  1908.     xmalloc (sizeof (struct symtab_and_line));
  1909.       values.nelts = 1;
  1910.       values.sals[0] = find_pc_line (pc, 0);
  1911.       values.sals[0].pc = pc;
  1912.       return values;
  1913.     }
  1914.  
  1915.   /* Maybe arg is FILE : LINENUM or FILE : FUNCTION */
  1916.  
  1917.   s = NULL;
  1918.   is_quoted = (strchr(gdb_completer_quote_characters, **argptr) != NULL);
  1919.   has_parens = (( pp = strchr(*argptr, '(')) != NULL  &&
  1920.          (pp = strchr(pp, ')')) != NULL);
  1921.  
  1922.   for (p = *argptr; *p; p++)
  1923.     {
  1924.       if (p[0] == '<') 
  1925.     {
  1926.       while(++p && *p != '>');
  1927.       if (!p)
  1928.         {
  1929.           error ("non-matching '<' and '>' in command");
  1930.         }
  1931.     }
  1932.       if (p[0] == ':' || p[0] == ' ' || p[0] == '\t')
  1933.     break;
  1934.     }
  1935.   while (p[0] == ' ' || p[0] == '\t') p++;
  1936.  
  1937.   if ((p[0] == ':') && !has_parens)
  1938.     {
  1939.  
  1940.       /*  C++  */
  1941.       if (is_quoted) *argptr = *argptr+1;
  1942.       if (p[1] ==':')
  1943.     {
  1944.       /* Extract the class name.  */
  1945.       p1 = p;
  1946.       while (p != *argptr && p[-1] == ' ') --p;
  1947.       copy = (char *) alloca (p - *argptr + 1);
  1948.       memcpy (copy, *argptr, p - *argptr);
  1949.       copy[p - *argptr] = 0;
  1950.  
  1951.       /* Discard the class name from the arg.  */
  1952.       p = p1 + 2;
  1953.       while (*p == ' ' || *p == '\t') p++;
  1954.       *argptr = p;
  1955.  
  1956.       sym_class = lookup_symbol (copy, 0, STRUCT_NAMESPACE, 0, 
  1957.                      (struct symtab **)NULL);
  1958.        
  1959.       if (sym_class &&
  1960.           (   TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_STRUCT
  1961.            || TYPE_CODE (SYMBOL_TYPE (sym_class)) == TYPE_CODE_UNION))
  1962.         {
  1963.           /* Arg token is not digits => try it as a function name
  1964.          Find the next token(everything up to end or next blank). */
  1965.           if (strchr(gdb_completer_quote_characters, **argptr) != NULL)
  1966.         {
  1967.           p = skip_quoted(*argptr);
  1968.           *argptr = *argptr + 1;
  1969.         }
  1970.           else
  1971.         {
  1972.               p = *argptr;
  1973.               while (*p && *p!=' ' && *p!='\t' && *p!=',' && *p!=':') p++;
  1974.         }
  1975. /*
  1976.           q = operator_chars (*argptr, &q1);
  1977.           if (q1 - q)
  1978.         {
  1979.           char *opname;
  1980.           char *tmp = alloca (q1 - q + 1);
  1981.           memcpy (tmp, q, q1 - q);
  1982.           tmp[q1 - q] = '\0';
  1983.           opname = cplus_mangle_opname (tmp, DMGL_ANSI);
  1984.           if (opname == NULL)
  1985.             {
  1986.               error_begin ();
  1987.               printf_filtered ("no mangling for \"%s\"\n", tmp);
  1988.               cplusplus_hint (saved_arg);
  1989.               return_to_top_level (RETURN_ERROR);
  1990.             }
  1991.           copy = (char*) alloca (3 + strlen(opname));
  1992.           sprintf (copy, "__%s", opname);
  1993.           p = q1;
  1994.         }
  1995.           else
  1996. */
  1997.         {
  1998.           copy = (char *) alloca (p - *argptr + 1 );
  1999.           memcpy (copy, *argptr, p - *argptr);
  2000.           copy[p - *argptr] = '\0';
  2001.           if (strchr(gdb_completer_quote_characters, copy[p-*argptr-1]) != NULL)
  2002.             copy[p - *argptr -1] = '\0';
  2003.         }
  2004.  
  2005.           /* no line number may be specified */
  2006.           while (*p == ' ' || *p == '\t') p++;
  2007.           *argptr = p;
  2008.  
  2009.           sym = 0;
  2010.           i1 = 0;        /*  counter for the symbol array */
  2011.           t = SYMBOL_TYPE (sym_class);
  2012.           sym_arr = (struct symbol **) alloca(total_number_of_methods (t)
  2013.                           * sizeof(struct symbol *));
  2014.  
  2015.           /* Cfront objects don't have fieldlists.  */
  2016.           if (destructor_name_p (copy, t) && TYPE_FN_FIELDLISTS (t) != NULL)
  2017.         {
  2018.           /* destructors are a special case.  */
  2019.           struct fn_field *f = TYPE_FN_FIELDLIST1 (t, 0);
  2020.           int len = TYPE_FN_FIELDLIST_LENGTH (t, 0) - 1;
  2021.           /* gcc 1.x puts destructor in last field,
  2022.              gcc 2.x puts destructor in first field.  */
  2023.           char *phys_name = TYPE_FN_FIELD_PHYSNAME (f, len);
  2024.           if (!DESTRUCTOR_PREFIX_P (phys_name))
  2025.             {
  2026.               phys_name = TYPE_FN_FIELD_PHYSNAME (f, 0);
  2027.               if (!DESTRUCTOR_PREFIX_P (phys_name))
  2028.             phys_name = "";
  2029.             }
  2030.           sym_arr[i1] =
  2031.             lookup_symbol (phys_name, SYMBOL_BLOCK_VALUE (sym_class),
  2032.                    VAR_NAMESPACE, 0, (struct symtab **)NULL);
  2033.           if (sym_arr[i1]) i1++;
  2034.         }
  2035.           else
  2036.         i1 = find_methods (t, copy, sym_arr);
  2037.           if (i1 == 1)
  2038.         {
  2039.           /* There is exactly one field with that name.  */
  2040.           sym = sym_arr[0];
  2041.  
  2042.           if (sym && SYMBOL_CLASS (sym) == LOC_BLOCK)
  2043.             {
  2044.               values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  2045.               values.nelts = 1;
  2046.               values.sals[0] = find_function_start_sal (sym,
  2047.                                 funfirstline);
  2048.             }
  2049.           else
  2050.             {
  2051.               values.nelts = 0;
  2052.             }
  2053.           return values;
  2054.         }
  2055.           if (i1 > 0)
  2056.         {
  2057.           /* There is more than one field with that name
  2058.              (overloaded).  Ask the user which one to use.  */
  2059.           return decode_line_2 (sym_arr, i1, funfirstline, canonical);
  2060.         }
  2061.           else
  2062.         {
  2063.           char *tmp;
  2064.  
  2065.           if (OPNAME_PREFIX_P (copy))
  2066.             {
  2067.               tmp = (char *)alloca (strlen (copy+3) + 9);
  2068.               strcpy (tmp, "operator ");
  2069.               strcat (tmp, copy+3);
  2070.             }
  2071.           else
  2072.             tmp = copy;
  2073.           error_begin ();
  2074.           if (tmp[0] == '~')
  2075.             printf_filtered
  2076.               ("the class `%s' does not have destructor defined\n",
  2077.                SYMBOL_SOURCE_NAME(sym_class));
  2078.           else
  2079.             printf_filtered
  2080.               ("the class %s does not have any method named %s\n",
  2081.                SYMBOL_SOURCE_NAME(sym_class), tmp);
  2082.           cplusplus_hint (saved_arg);
  2083.           return_to_top_level (RETURN_ERROR);
  2084.         }
  2085.         }
  2086.       else
  2087.         {
  2088.           error_begin ();
  2089.           /* The quotes are important if copy is empty.  */
  2090.           printf_filtered
  2091.         ("can't find class, struct, or union named \"%s\"\n", copy);
  2092.           cplusplus_hint (saved_arg);
  2093.           return_to_top_level (RETURN_ERROR);
  2094.         }
  2095.     }
  2096.       /*  end of C++  */
  2097.  
  2098.  
  2099.       /* Extract the file name.  */
  2100.       p1 = p;
  2101.       while (p != *argptr && p[-1] == ' ') --p;
  2102.       copy = (char *) alloca (p - *argptr + 1);
  2103.       memcpy (copy, *argptr, p - *argptr);
  2104.       copy[p - *argptr] = 0;
  2105.  
  2106.       /* Find that file's data.  */
  2107.       s = lookup_symtab (copy);
  2108.       if (s == 0)
  2109.     {
  2110.       if (!have_full_symbols () && !have_partial_symbols ())
  2111.         error (no_symtab_msg);
  2112.       error ("No source file named %s.", copy);
  2113.     }
  2114.  
  2115.       /* Discard the file name from the arg.  */
  2116.       p = p1 + 1;
  2117.       while (*p == ' ' || *p == '\t') p++;
  2118.       *argptr = p;
  2119.     }
  2120.  
  2121.   /* S is specified file's symtab, or 0 if no file specified.
  2122.      arg no longer contains the file name.  */
  2123.  
  2124.   /* Check whether arg is all digits (and sign) */
  2125.  
  2126.   q = *argptr;
  2127.   if (*q == '-' || *q == '+') q++;
  2128.   while (*q >= '0' && *q <= '9')
  2129.     q++;
  2130.  
  2131.   if (q != *argptr && (*q == 0 || *q == ' ' || *q == '\t' || *q == ','))
  2132.     {
  2133.       /* We found a token consisting of all digits -- at least one digit.  */
  2134.       enum sign {none, plus, minus} sign = none;
  2135.  
  2136.       /* We might need a canonical line spec if no file was specified.  */
  2137.       int need_canonical = (s == 0) ? 1 : 0;
  2138.  
  2139.       /* This is where we need to make sure that we have good defaults.
  2140.      We must guarantee that this section of code is never executed
  2141.      when we are called with just a function name, since
  2142.      select_source_symtab calls us with such an argument  */
  2143.  
  2144.       if (s == 0 && default_symtab == 0)
  2145.     {
  2146.       select_source_symtab (0);
  2147.       default_symtab = current_source_symtab;
  2148.       default_line = current_source_line;
  2149.     }
  2150.  
  2151.       if (**argptr == '+')
  2152.     sign = plus, (*argptr)++;
  2153.       else if (**argptr == '-')
  2154.     sign = minus, (*argptr)++;
  2155.       val.line = atoi (*argptr);
  2156.       switch (sign)
  2157.     {
  2158.     case plus:
  2159.       if (q == *argptr)
  2160.         val.line = 5;
  2161.       if (s == 0)
  2162.         val.line = default_line + val.line;
  2163.       break;
  2164.     case minus:
  2165.       if (q == *argptr)
  2166.         val.line = 15;
  2167.       if (s == 0)
  2168.         val.line = default_line - val.line;
  2169.       else
  2170.         val.line = 1;
  2171.       break;
  2172.     case none:
  2173.       break;    /* No need to adjust val.line.  */
  2174.     }
  2175.  
  2176.       while (*q == ' ' || *q == '\t') q++;
  2177.       *argptr = q;
  2178.       if (s == 0)
  2179.     s = default_symtab;
  2180.       val.symtab = s;
  2181.       val.pc = 0;
  2182.       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  2183.       values.sals[0] = val;
  2184.       values.nelts = 1;
  2185.       if (need_canonical)
  2186.     build_canonical_line_spec (values.sals, NULL, canonical);
  2187.       return values;
  2188.     }
  2189.  
  2190.   /* Arg token is not digits => try it as a variable name
  2191.      Find the next token (everything up to end or next whitespace).  */
  2192.  
  2193.   if (is_quoted)
  2194.     {
  2195.       p = skip_quoted (*argptr);
  2196.       if (p[-1] != '\'')
  2197.         error ("Unmatched single quote.");
  2198.     }
  2199.   else if (has_parens)
  2200.     {
  2201.       p = pp+1;
  2202.     }
  2203.   else 
  2204.     {
  2205.       p = skip_quoted(*argptr);
  2206.     }
  2207.  
  2208.   copy = (char *) alloca (p - *argptr + 1);
  2209.   memcpy (copy, *argptr, p - *argptr);
  2210.   copy[p - *argptr] = '\0';
  2211.   if (p != *argptr
  2212.       && (copy[0] == copy [p - *argptr - 1])
  2213.       && strchr (gdb_completer_quote_characters, copy[0]) != NULL)
  2214.     {
  2215.       copy [p - *argptr - 1] = '\0';
  2216.       copy++;
  2217.     }
  2218.   while (*p == ' ' || *p == '\t') p++;
  2219.   *argptr = p;
  2220.  
  2221.   /* Look up that token as a variable.
  2222.      If file specified, use that file's per-file block to start with.  */
  2223.  
  2224.   sym = lookup_symbol (copy,
  2225.                (s ? BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK)
  2226.             : get_selected_block ()),
  2227.                VAR_NAMESPACE, 0, &sym_symtab);
  2228.  
  2229.   if (sym != NULL)
  2230.     {
  2231.       if (SYMBOL_CLASS (sym) == LOC_BLOCK)
  2232.     {
  2233.       /* Arg is the name of a function */
  2234.       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  2235.       values.sals[0] = find_function_start_sal (sym, funfirstline);
  2236.       values.nelts = 1;
  2237.  
  2238.       /* Don't use the SYMBOL_LINE; if used at all it points to
  2239.          the line containing the parameters or thereabouts, not
  2240.          the first line of code.  */
  2241.  
  2242.       /* We might need a canonical line spec if it is a static
  2243.          function.  */
  2244.       if (s == 0)
  2245.         {
  2246.           struct blockvector *bv = BLOCKVECTOR (sym_symtab);
  2247.           struct block *b = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
  2248.           if (lookup_block_symbol (b, copy, VAR_NAMESPACE) != NULL)
  2249.         build_canonical_line_spec (values.sals, copy, canonical);
  2250.         }
  2251.       return values;
  2252.     }
  2253.       else
  2254.     {
  2255.       if (funfirstline)
  2256.         error ("\"%s\" is not a function", copy);
  2257.       else if (SYMBOL_LINE (sym) != 0)
  2258.         {
  2259.           /* We know its line number.  */
  2260.           values.sals = (struct symtab_and_line *)
  2261.         xmalloc (sizeof (struct symtab_and_line));
  2262.           values.nelts = 1;
  2263.           memset (&values.sals[0], 0, sizeof (values.sals[0]));
  2264.           values.sals[0].symtab = sym_symtab;
  2265.           values.sals[0].line = SYMBOL_LINE (sym);
  2266.           return values;
  2267.         }
  2268.       else
  2269.         /* This can happen if it is compiled with a compiler which doesn't
  2270.            put out line numbers for variables.  */
  2271.         /* FIXME: Shouldn't we just set .line and .symtab to zero
  2272.            and return?  For example, "info line foo" could print
  2273.            the address.  */
  2274.         error ("Line number not known for symbol \"%s\"", copy);
  2275.     }
  2276.     }
  2277.  
  2278.   msymbol = lookup_minimal_symbol (copy, NULL, NULL);
  2279.   if (msymbol != NULL)
  2280.     {
  2281.       val.symtab = 0;
  2282.       val.line = 0;
  2283.       val.pc = SYMBOL_VALUE_ADDRESS (msymbol);
  2284.       if (funfirstline)
  2285.     {
  2286.       val.pc += FUNCTION_START_OFFSET;
  2287.       SKIP_PROLOGUE (val.pc);
  2288.     }
  2289.       values.sals = (struct symtab_and_line *)xmalloc (sizeof (struct symtab_and_line));
  2290.       values.sals[0] = val;
  2291.       values.nelts = 1;
  2292.       return values;
  2293.     }
  2294.  
  2295.   if (!have_full_symbols () &&
  2296.       !have_partial_symbols () && !have_minimal_symbols ())
  2297.     error (no_symtab_msg);
  2298.  
  2299.   error ("Function \"%s\" not defined.", copy);
  2300.   return values;    /* for lint */
  2301. }
  2302.  
  2303. struct symtabs_and_lines
  2304. decode_line_spec (string, funfirstline)
  2305.      char *string;
  2306.      int funfirstline;
  2307. {
  2308.   struct symtabs_and_lines sals;
  2309.   if (string == 0)
  2310.     error ("Empty line specification.");
  2311.   sals = decode_line_1 (&string, funfirstline,
  2312.             current_source_symtab, current_source_line,
  2313.             (char ***)NULL);
  2314.   if (*string)
  2315.     error ("Junk at end of line specification: %s", string);
  2316.   return sals;
  2317. }
  2318.  
  2319. /* Given a list of NELTS symbols in SYM_ARR, return a list of lines to
  2320.    operate on (ask user if necessary).
  2321.    If CANONICAL is non-NULL return a corresponding array of mangled names
  2322.    as canonical line specs there.  */
  2323.  
  2324. static struct symtabs_and_lines
  2325. decode_line_2 (sym_arr, nelts, funfirstline, canonical)
  2326.      struct symbol *sym_arr[];
  2327.      int nelts;
  2328.      int funfirstline;
  2329.      char ***canonical;
  2330. {
  2331.   struct symtabs_and_lines values, return_values;
  2332.   char *args, *arg1;
  2333.   int i;
  2334.   char *prompt;
  2335.   char *symname;
  2336.   struct cleanup *old_chain;
  2337.   char **canonical_arr = (char **)NULL;
  2338.  
  2339.   values.sals = (struct symtab_and_line *) alloca (nelts * sizeof(struct symtab_and_line));
  2340.   return_values.sals = (struct symtab_and_line *) xmalloc (nelts * sizeof(struct symtab_and_line));
  2341.   old_chain = make_cleanup (free, return_values.sals);
  2342.  
  2343.   if (canonical)
  2344.     {
  2345.       canonical_arr = (char **) xmalloc (nelts * sizeof (char *));
  2346.       make_cleanup (free, canonical_arr);
  2347.       memset (canonical_arr, 0, nelts * sizeof (char *));
  2348.       *canonical = canonical_arr;
  2349.     }
  2350.  
  2351.   i = 0;
  2352.   printf_unfiltered("[0] cancel\n[1] all\n");
  2353.   while (i < nelts)
  2354.     {
  2355.       if (sym_arr[i] && SYMBOL_CLASS (sym_arr[i]) == LOC_BLOCK)
  2356.     {
  2357.       values.sals[i] = find_function_start_sal (sym_arr[i], funfirstline);
  2358.       printf_unfiltered ("[%d] %s at %s:%d\n",
  2359.                  (i+2),
  2360.                  SYMBOL_SOURCE_NAME (sym_arr[i]),
  2361.                  values.sals[i].symtab->filename,
  2362.                  values.sals[i].line);
  2363.     }
  2364.       else
  2365.     printf_unfiltered ("?HERE\n");
  2366.       i++;
  2367.     }
  2368.   
  2369.   if ((prompt = getenv ("PS2")) == NULL)
  2370.     {
  2371.       prompt = ">";
  2372.     }
  2373.   printf_unfiltered("%s ",prompt);
  2374.   gdb_flush(gdb_stdout);
  2375.  
  2376.   args = command_line_input ((char *) NULL, 0, "overload-choice");
  2377.   
  2378.   if (args == 0 || *args == 0)
  2379.     error_no_arg ("one or more choice numbers");
  2380.  
  2381.   i = 0;
  2382.   while (*args)
  2383.     {
  2384.       int num;
  2385.  
  2386.       arg1 = args;
  2387.       while (*arg1 >= '0' && *arg1 <= '9') arg1++;
  2388.       if (*arg1 && *arg1 != ' ' && *arg1 != '\t')
  2389.     error ("Arguments must be choice numbers.");
  2390.  
  2391.       num = atoi (args);
  2392.  
  2393.       if (num == 0)
  2394.     error ("cancelled");
  2395.       else if (num == 1)
  2396.     {
  2397.       if (canonical_arr)
  2398.         {
  2399.           for (i = 0; i < nelts; i++)
  2400.         {
  2401.               if (canonical_arr[i] == NULL)
  2402.             {
  2403.               symname = SYMBOL_NAME (sym_arr[i]);
  2404.                   canonical_arr[i] = savestring (symname, strlen (symname));
  2405.             }
  2406.         }
  2407.         }
  2408.       memcpy (return_values.sals, values.sals,
  2409.           (nelts * sizeof(struct symtab_and_line)));
  2410.       return_values.nelts = nelts;
  2411.       discard_cleanups (old_chain);
  2412.       return return_values;
  2413.     }
  2414.  
  2415.       if (num > nelts + 2)
  2416.     {
  2417.       printf_unfiltered ("No choice number %d.\n", num);
  2418.     }
  2419.       else
  2420.     {
  2421.       num -= 2;
  2422.       if (values.sals[num].pc)
  2423.         {
  2424.           if (canonical_arr)
  2425.         {
  2426.           symname = SYMBOL_NAME (sym_arr[num]);
  2427.           make_cleanup (free, symname);
  2428.           canonical_arr[i] = savestring (symname, strlen (symname));
  2429.         }
  2430.           return_values.sals[i++] = values.sals[num];
  2431.           values.sals[num].pc = 0;
  2432.         }
  2433.       else
  2434.         {
  2435.           printf_unfiltered ("duplicate request for %d ignored.\n", num);
  2436.         }
  2437.     }
  2438.  
  2439.       args = arg1;
  2440.       while (*args == ' ' || *args == '\t') args++;
  2441.     }
  2442.   return_values.nelts = i;
  2443.   discard_cleanups (old_chain);
  2444.   return return_values;
  2445. }
  2446.  
  2447.  
  2448. /* Slave routine for sources_info.  Force line breaks at ,'s.
  2449.    NAME is the name to print and *FIRST is nonzero if this is the first
  2450.    name printed.  Set *FIRST to zero.  */
  2451. static void
  2452. output_source_filename (name, first)
  2453.      char *name;
  2454.      int *first;
  2455. {
  2456.   /* Table of files printed so far.  Since a single source file can
  2457.      result in several partial symbol tables, we need to avoid printing
  2458.      it more than once.  Note: if some of the psymtabs are read in and
  2459.      some are not, it gets printed both under "Source files for which
  2460.      symbols have been read" and "Source files for which symbols will
  2461.      be read in on demand".  I consider this a reasonable way to deal
  2462.      with the situation.  I'm not sure whether this can also happen for
  2463.      symtabs; it doesn't hurt to check.  */
  2464.   static char **tab = NULL;
  2465.   /* Allocated size of tab in elements.
  2466.      Start with one 256-byte block (when using GNU malloc.c).
  2467.      24 is the malloc overhead when range checking is in effect.  */
  2468.   static int tab_alloc_size = (256 - 24) / sizeof (char *);
  2469.   /* Current size of tab in elements.  */
  2470.   static int tab_cur_size;
  2471.  
  2472.   char **p;
  2473.  
  2474.   if (*first)
  2475.     {
  2476.       if (tab == NULL)
  2477.     tab = (char **) xmalloc (tab_alloc_size * sizeof (*tab));
  2478.       tab_cur_size = 0;
  2479.     }
  2480.  
  2481.   /* Is NAME in tab?  */
  2482.   for (p = tab; p < tab + tab_cur_size; p++)
  2483.     if (STREQ (*p, name))
  2484.       /* Yes; don't print it again.  */
  2485.       return;
  2486.   /* No; add it to tab.  */
  2487.   if (tab_cur_size == tab_alloc_size)
  2488.     {
  2489.       tab_alloc_size *= 2;
  2490.       tab = (char **) xrealloc ((char *) tab, tab_alloc_size * sizeof (*tab));
  2491.     }
  2492.   tab[tab_cur_size++] = name;
  2493.  
  2494.   if (*first)
  2495.     {
  2496.       *first = 0;
  2497.     }
  2498.   else
  2499.     {
  2500.       printf_filtered (", ");
  2501.     }
  2502.  
  2503.   wrap_here ("");
  2504.   fputs_filtered (name, gdb_stdout);
  2505. }  
  2506.  
  2507. static void
  2508. sources_info (ignore, from_tty)
  2509.      char *ignore;
  2510.      int from_tty;
  2511. {
  2512.   register struct symtab *s;
  2513.   register struct partial_symtab *ps;
  2514.   register struct objfile *objfile;
  2515.   int first;
  2516.   
  2517.   if (!have_full_symbols () && !have_partial_symbols ())
  2518.     {
  2519.       error (no_symtab_msg);
  2520.     }
  2521.   
  2522.   printf_filtered ("Source files for which symbols have been read in:\n\n");
  2523.  
  2524.   first = 1;
  2525.   ALL_SYMTABS (objfile, s)
  2526.     {
  2527.       output_source_filename (s -> filename, &first);
  2528.     }
  2529.   printf_filtered ("\n\n");
  2530.   
  2531.   printf_filtered ("Source files for which symbols will be read in on demand:\n\n");
  2532.  
  2533.   first = 1;
  2534.   ALL_PSYMTABS (objfile, ps)
  2535.     {
  2536.       if (!ps->readin)
  2537.     {
  2538.       output_source_filename (ps -> filename, &first);
  2539.     }
  2540.     }
  2541.   printf_filtered ("\n");
  2542. }
  2543.  
  2544. /* List all symbols (if REGEXP is NULL) or all symbols matching REGEXP.
  2545.    If CLASS is zero, list all symbols except functions, type names, and
  2546.              constants (enums).
  2547.    If CLASS is 1, list only functions.
  2548.    If CLASS is 2, list only type names.
  2549.    If CLASS is 3, list only method names.
  2550.  
  2551.    BPT is non-zero if we should set a breakpoint at the functions
  2552.    we find.  */
  2553.  
  2554. static void
  2555. list_symbols (regexp, class, bpt, from_tty)
  2556.      char *regexp;
  2557.      int class;
  2558.      int bpt;
  2559.      int from_tty;
  2560. {
  2561.   register struct symtab *s;
  2562.   register struct partial_symtab *ps;
  2563.   register struct blockvector *bv;
  2564.   struct blockvector *prev_bv = 0;
  2565.   register struct block *b;
  2566.   register int i, j;
  2567.   register struct symbol *sym;
  2568.   struct partial_symbol *psym;
  2569.   struct objfile *objfile;
  2570.   struct minimal_symbol *msymbol;
  2571.   char *val;
  2572.   static char *classnames[]
  2573.     = {"variable", "function", "type", "method"};
  2574.   int found_in_file = 0;
  2575.   int found_misc = 0;
  2576.   static enum minimal_symbol_type types[]
  2577.     = {mst_data, mst_text, mst_abs, mst_unknown};
  2578.   static enum minimal_symbol_type types2[]
  2579.     = {mst_bss,  mst_file_text, mst_abs, mst_unknown};
  2580.   static enum minimal_symbol_type types3[]
  2581.     = {mst_file_data,  mst_solib_trampoline, mst_abs, mst_unknown};
  2582.   static enum minimal_symbol_type types4[]
  2583.     = {mst_file_bss,   mst_text, mst_abs, mst_unknown};
  2584.   enum minimal_symbol_type ourtype = types[class];
  2585.   enum minimal_symbol_type ourtype2 = types2[class];
  2586.   enum minimal_symbol_type ourtype3 = types3[class];
  2587.   enum minimal_symbol_type ourtype4 = types4[class];
  2588.  
  2589.   if (regexp != NULL)
  2590.     {
  2591.       /* Make sure spacing is right for C++ operators.
  2592.      This is just a courtesy to make the matching less sensitive
  2593.      to how many spaces the user leaves between 'operator'
  2594.      and <TYPENAME> or <OPERATOR>. */
  2595.       char *opend;
  2596.       char *opname = operator_chars (regexp, &opend);
  2597.       if (*opname)
  2598.     {
  2599.           int fix = -1; /* -1 means ok; otherwise number of spaces needed. */
  2600.       if (isalpha(*opname) || *opname == '_' || *opname == '$')
  2601.         {
  2602.           /* There should 1 space between 'operator' and 'TYPENAME'. */
  2603.           if (opname[-1] != ' ' || opname[-2] == ' ')
  2604.             fix = 1;
  2605.         }
  2606.       else
  2607.         {
  2608.           /* There should 0 spaces between 'operator' and 'OPERATOR'. */
  2609.           if (opname[-1] == ' ')
  2610.             fix = 0;
  2611.         }
  2612.       /* If wrong number of spaces, fix it. */
  2613.       if (fix >= 0)
  2614.         {
  2615.           char *tmp = (char*) alloca(opend-opname+10);
  2616.           sprintf(tmp, "operator%.*s%s", fix, " ", opname);
  2617.           regexp = tmp;
  2618.         }
  2619.         }
  2620.       
  2621.       if (0 != (val = re_comp (regexp)))
  2622.     error ("Invalid regexp (%s): %s", val, regexp);
  2623.     }
  2624.  
  2625.   /* Search through the partial symtabs *first* for all symbols
  2626.      matching the regexp.  That way we don't have to reproduce all of
  2627.      the machinery below. */
  2628.  
  2629.   ALL_PSYMTABS (objfile, ps)
  2630.     {
  2631.       struct partial_symbol *bound, *gbound, *sbound;
  2632.       int keep_going = 1;
  2633.       
  2634.       if (ps->readin) continue;
  2635.       
  2636.       gbound = objfile->global_psymbols.list + ps->globals_offset + ps->n_global_syms;
  2637.       sbound = objfile->static_psymbols.list + ps->statics_offset + ps->n_static_syms;
  2638.       bound = gbound;
  2639.       
  2640.       /* Go through all of the symbols stored in a partial
  2641.      symtab in one loop. */
  2642.       psym = objfile->global_psymbols.list + ps->globals_offset;
  2643.       while (keep_going)
  2644.     {
  2645.       if (psym >= bound)
  2646.         {
  2647.           if (bound == gbound && ps->n_static_syms != 0)
  2648.         {
  2649.           psym = objfile->static_psymbols.list + ps->statics_offset;
  2650.           bound = sbound;
  2651.         }
  2652.           else
  2653.         keep_going = 0;
  2654.           continue;
  2655.         }
  2656.       else
  2657.         {
  2658.           QUIT;
  2659.  
  2660.           /* If it would match (logic taken from loop below)
  2661.          load the file and go on to the next one */
  2662.           if ((regexp == NULL || SYMBOL_MATCHES_REGEXP (psym))
  2663.           && ((class == 0 && SYMBOL_CLASS (psym) != LOC_TYPEDEF
  2664.                && SYMBOL_CLASS (psym) != LOC_BLOCK)
  2665.               || (class == 1 && SYMBOL_CLASS (psym) == LOC_BLOCK)
  2666.               || (class == 2 && SYMBOL_CLASS (psym) == LOC_TYPEDEF)
  2667.               || (class == 3 && SYMBOL_CLASS (psym) == LOC_BLOCK)))
  2668.         {
  2669.           PSYMTAB_TO_SYMTAB(ps);
  2670.           keep_going = 0;
  2671.         }
  2672.         }
  2673.       psym++;
  2674.     }
  2675.     }
  2676.  
  2677.   /* Here, we search through the minimal symbol tables for functions
  2678.      and variables that match, and force their symbols to be read.
  2679.      This is in particular necessary for demangled variable names,
  2680.      which are no longer put into the partial symbol tables.
  2681.      The symbol will then be found during the scan of symtabs below.
  2682.  
  2683.      For functions, find_pc_symtab should succeed if we have debug info
  2684.      for the function, for variables we have to call lookup_symbol
  2685.      to determine if the variable has debug info.
  2686.      If the lookup fails, set found_misc so that we will rescan to print
  2687.      any matching symbols without debug info.
  2688.   */
  2689.  
  2690.   if (class == 0 || class == 1)
  2691.     {
  2692.       ALL_MSYMBOLS (objfile, msymbol)
  2693.     {
  2694.       if (MSYMBOL_TYPE (msymbol) == ourtype ||
  2695.           MSYMBOL_TYPE (msymbol) == ourtype2 ||
  2696.           MSYMBOL_TYPE (msymbol) == ourtype3 ||
  2697.           MSYMBOL_TYPE (msymbol) == ourtype4)
  2698.         {
  2699.           if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
  2700.         {
  2701.           if (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol)))
  2702.             {
  2703.               if (class == 1
  2704.               || lookup_symbol (SYMBOL_NAME (msymbol), 
  2705.                         (struct block *) NULL,
  2706.                         VAR_NAMESPACE,
  2707.                         0, (struct symtab **) NULL) == NULL)
  2708.                 found_misc = 1;
  2709.             }
  2710.         }
  2711.         }
  2712.     }
  2713.     }
  2714.  
  2715.   /* Printout here so as to get after the "Reading in symbols"
  2716.      messages which will be generated above.  */
  2717.   if (!bpt)
  2718.     printf_filtered (regexp
  2719.       ? "All %ss matching regular expression \"%s\":\n"
  2720.       : "All defined %ss:\n",
  2721.       classnames[class],
  2722.       regexp);
  2723.  
  2724.   ALL_SYMTABS (objfile, s)
  2725.     {
  2726.       found_in_file = 0;
  2727.       bv = BLOCKVECTOR (s);
  2728.       /* Often many files share a blockvector.
  2729.      Scan each blockvector only once so that
  2730.      we don't get every symbol many times.
  2731.      It happens that the first symtab in the list
  2732.      for any given blockvector is the main file.  */
  2733.       if (bv != prev_bv)
  2734.     for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
  2735.       {
  2736.         b = BLOCKVECTOR_BLOCK (bv, i);
  2737.         /* Skip the sort if this block is always sorted.  */
  2738.         if (!BLOCK_SHOULD_SORT (b))
  2739.           sort_block_syms (b);
  2740.         for (j = 0; j < BLOCK_NSYMS (b); j++)
  2741.           {
  2742.         QUIT;
  2743.         sym = BLOCK_SYM (b, j);
  2744.         if ((regexp == NULL || SYMBOL_MATCHES_REGEXP (sym))
  2745.             && ((class == 0 && SYMBOL_CLASS (sym) != LOC_TYPEDEF
  2746.              && SYMBOL_CLASS (sym) != LOC_BLOCK
  2747.              && SYMBOL_CLASS (sym) != LOC_CONST)
  2748.             || (class == 1 && SYMBOL_CLASS (sym) == LOC_BLOCK)
  2749.             || (class == 2 && SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  2750.             || (class == 3 && SYMBOL_CLASS (sym) == LOC_BLOCK)))
  2751.           {
  2752.             if (bpt)
  2753.               {
  2754.             /* Set a breakpoint here, if it's a function */
  2755.             if (class == 1)
  2756.               {
  2757.                 /* There may be more than one function with the
  2758.                    same name but in different files.  In order to
  2759.                    set breakpoints on all of them, we must give
  2760.                    both the file name and the function name to
  2761.                    break_command.
  2762.                    Quoting the symbol name gets rid of problems
  2763.                    with mangled symbol names that contain
  2764.                    CPLUS_MARKER characters.  */
  2765.                 char *string =
  2766.                   (char *) alloca (strlen (s->filename)
  2767.                            + strlen (SYMBOL_NAME(sym))
  2768.                            + 4);
  2769.                 strcpy (string, s->filename);
  2770.                 strcat (string, ":'");
  2771.                 strcat (string, SYMBOL_NAME(sym));
  2772.                 strcat (string, "'");
  2773.                 break_command (string, from_tty);
  2774.               }
  2775.               }
  2776.             else if (!found_in_file)
  2777.               {
  2778.             fputs_filtered ("\nFile ", gdb_stdout);
  2779.             fputs_filtered (s->filename, gdb_stdout);
  2780.             fputs_filtered (":\n", gdb_stdout);
  2781.               }
  2782.             found_in_file = 1;
  2783.             
  2784.             if (class != 2 && i == STATIC_BLOCK)
  2785.               printf_filtered ("static ");
  2786.             
  2787.             /* Typedef that is not a C++ class */
  2788.             if (class == 2
  2789.             && SYMBOL_NAMESPACE (sym) != STRUCT_NAMESPACE)
  2790.               c_typedef_print (SYMBOL_TYPE(sym), sym, gdb_stdout);
  2791.             /* variable, func, or typedef-that-is-c++-class */
  2792.             else if (class < 2 || 
  2793.                  (class == 2 && 
  2794.                   SYMBOL_NAMESPACE(sym) == STRUCT_NAMESPACE))
  2795.               {
  2796.             type_print (SYMBOL_TYPE (sym),
  2797.                     (SYMBOL_CLASS (sym) == LOC_TYPEDEF
  2798.                      ? "" : SYMBOL_SOURCE_NAME (sym)),
  2799.                     gdb_stdout, 0);
  2800.             
  2801.             printf_filtered (";\n");
  2802.               }
  2803.             else
  2804.               {
  2805. # if 0  /* FIXME, why is this zapped out? */
  2806.             char buf[1024];
  2807.             c_type_print_base (TYPE_FN_FIELD_TYPE(t, i),
  2808.                        gdb_stdout, 0, 0); 
  2809.             c_type_print_varspec_prefix (TYPE_FN_FIELD_TYPE(t, i),
  2810.                              gdb_stdout, 0); 
  2811.             sprintf (buf, " %s::", type_name_no_tag (t));
  2812.             cp_type_print_method_args (TYPE_FN_FIELD_ARGS (t, i),
  2813.                            buf, name, gdb_stdout);
  2814. # endif
  2815.               }
  2816.           }
  2817.           }
  2818.       }
  2819.       prev_bv = bv;
  2820.     }
  2821.  
  2822.   /* If there are no eyes, avoid all contact.  I mean, if there are
  2823.      no debug symbols, then print directly from the msymbol_vector.  */
  2824.  
  2825.   if (found_misc || class != 1)
  2826.     {
  2827.       found_in_file = 0;
  2828.       ALL_MSYMBOLS (objfile, msymbol)
  2829.     {
  2830.       if (MSYMBOL_TYPE (msymbol) == ourtype ||
  2831.           MSYMBOL_TYPE (msymbol) == ourtype2 ||
  2832.           MSYMBOL_TYPE (msymbol) == ourtype3 ||
  2833.           MSYMBOL_TYPE (msymbol) == ourtype4)
  2834.         {
  2835.           if (regexp == NULL || SYMBOL_MATCHES_REGEXP (msymbol))
  2836.         {
  2837.           /* Functions:  Look up by address. */
  2838.           if (class != 1 ||
  2839.               (0 == find_pc_symtab (SYMBOL_VALUE_ADDRESS (msymbol))))
  2840.             {
  2841.               /* Variables/Absolutes:  Look up by name */
  2842.               if (lookup_symbol (SYMBOL_NAME (msymbol), 
  2843.                      (struct block *) NULL, VAR_NAMESPACE,
  2844.                      0, (struct symtab **) NULL) == NULL)
  2845.             {
  2846.               if (!found_in_file)
  2847.                 {
  2848.                   printf_filtered ("\nNon-debugging symbols:\n");
  2849.                   found_in_file = 1;
  2850.                 }
  2851.               printf_filtered ("    %08lx  %s\n",
  2852.                        (unsigned long) SYMBOL_VALUE_ADDRESS (msymbol),
  2853.                        SYMBOL_SOURCE_NAME (msymbol));
  2854.             }
  2855.             }
  2856.         }
  2857.         }
  2858.     }
  2859.     }
  2860. }
  2861.  
  2862. static void
  2863. variables_info (regexp, from_tty)
  2864.      char *regexp;
  2865.      int from_tty;
  2866. {
  2867.   list_symbols (regexp, 0, 0, from_tty);
  2868. }
  2869.  
  2870. static void
  2871. functions_info (regexp, from_tty)
  2872.      char *regexp;
  2873.      int from_tty;
  2874. {
  2875.   list_symbols (regexp, 1, 0, from_tty);
  2876. }
  2877.  
  2878. static void
  2879. types_info (regexp, from_tty)
  2880.      char *regexp;
  2881.      int from_tty;
  2882. {
  2883.   list_symbols (regexp, 2, 0, from_tty);
  2884. }
  2885.  
  2886. #if 0
  2887. /* Tiemann says: "info methods was never implemented."  */
  2888. static void
  2889. methods_info (regexp)
  2890.      char *regexp;
  2891. {
  2892.   list_symbols (regexp, 3, 0, from_tty);
  2893. }
  2894. #endif /* 0 */
  2895.  
  2896. /* Breakpoint all functions matching regular expression. */
  2897. static void
  2898. rbreak_command (regexp, from_tty)
  2899.      char *regexp;
  2900.      int from_tty;
  2901. {
  2902.   list_symbols (regexp, 1, 1, from_tty);
  2903. }
  2904.  
  2905.  
  2906. /* Return Nonzero if block a is lexically nested within block b,
  2907.    or if a and b have the same pc range.
  2908.    Return zero otherwise. */
  2909. int
  2910. contained_in (a, b)
  2911.      struct block *a, *b;
  2912. {
  2913.   if (!a || !b)
  2914.     return 0;
  2915.   return BLOCK_START (a) >= BLOCK_START (b)
  2916.       && BLOCK_END (a)   <= BLOCK_END (b);
  2917. }
  2918.  
  2919.  
  2920. /* Helper routine for make_symbol_completion_list.  */
  2921.  
  2922. static int return_val_size;
  2923. static int return_val_index;
  2924. static char **return_val;
  2925.  
  2926. #define COMPLETION_LIST_ADD_SYMBOL(symbol, sym_text, len, text, word) \
  2927.   do { \
  2928.     if (SYMBOL_DEMANGLED_NAME (symbol) != NULL) \
  2929.       /* Put only the mangled name on the list.  */ \
  2930.       /* Advantage:  "b foo<TAB>" completes to "b foo(int, int)" */ \
  2931.       /* Disadvantage:  "b foo__i<TAB>" doesn't complete.  */ \
  2932.       completion_list_add_name \
  2933.     (SYMBOL_DEMANGLED_NAME (symbol), (sym_text), (len), (text), (word)); \
  2934.     else \
  2935.       completion_list_add_name \
  2936.     (SYMBOL_NAME (symbol), (sym_text), (len), (text), (word)); \
  2937.   } while (0)
  2938.  
  2939. /*  Test to see if the symbol specified by SYMNAME (which is already
  2940.     demangled for C++ symbols) matches SYM_TEXT in the first SYM_TEXT_LEN
  2941.     characters.  If so, add it to the current completion list. */
  2942.  
  2943. static void
  2944. completion_list_add_name (symname, sym_text, sym_text_len, text, word)
  2945.      char *symname;
  2946.      char *sym_text;
  2947.      int sym_text_len;
  2948.      char *text;
  2949.      char *word;
  2950. {
  2951.   int newsize;
  2952.   int i;
  2953.  
  2954.   /* clip symbols that cannot match */
  2955.  
  2956.   if (strncmp (symname, sym_text, sym_text_len) != 0)
  2957.     {
  2958.       return;
  2959.     }
  2960.  
  2961.   /* Clip any symbol names that we've already considered.  (This is a
  2962.      time optimization)  */
  2963.  
  2964.   for (i = 0; i < return_val_index; ++i)
  2965.     {
  2966.       if (STREQ (symname, return_val[i]))
  2967.     {
  2968.       return;
  2969.     }
  2970.     }
  2971.   
  2972.   /* We have a match for a completion, so add SYMNAME to the current list
  2973.      of matches. Note that the name is moved to freshly malloc'd space. */
  2974.  
  2975.   {
  2976.     char *new;
  2977.     if (word == sym_text)
  2978.       {
  2979.     new = xmalloc (strlen (symname) + 5);
  2980.     strcpy (new, symname);
  2981.       }
  2982.     else if (word > sym_text)
  2983.       {
  2984.     /* Return some portion of symname.  */
  2985.     new = xmalloc (strlen (symname) + 5);
  2986.     strcpy (new, symname + (word - sym_text));
  2987.       }
  2988.     else
  2989.       {
  2990.     /* Return some of SYM_TEXT plus symname.  */
  2991.     new = xmalloc (strlen (symname) + (sym_text - word) + 5);
  2992.     strncpy (new, word, sym_text - word);
  2993.     new[sym_text - word] = '\0';
  2994.     strcat (new, symname);
  2995.       }
  2996.  
  2997.     /* Recheck for duplicates if we intend to add a modified symbol.  */
  2998.     if (word != sym_text)
  2999.       {
  3000.     for (i = 0; i < return_val_index; ++i)
  3001.       {
  3002.         if (STREQ (new, return_val[i]))
  3003.           {
  3004.         free (new);
  3005.         return;
  3006.           }
  3007.       }
  3008.       }
  3009.  
  3010.     if (return_val_index + 3 > return_val_size)
  3011.       {
  3012.     newsize = (return_val_size *= 2) * sizeof (char *);
  3013.     return_val = (char **) xrealloc ((char *) return_val, newsize);
  3014.       }
  3015.     return_val[return_val_index++] = new;
  3016.     return_val[return_val_index] = NULL;
  3017.   }
  3018. }
  3019.  
  3020. /* Return a NULL terminated array of all symbols (regardless of class) which
  3021.    begin by matching TEXT.  If the answer is no symbols, then the return value
  3022.    is an array which contains only a NULL pointer.
  3023.  
  3024.    Problem: All of the symbols have to be copied because readline frees them.
  3025.    I'm not going to worry about this; hopefully there won't be that many.  */
  3026.  
  3027. char **
  3028. make_symbol_completion_list (text, word)
  3029.      char *text;
  3030.      char *word;
  3031. {
  3032.   register struct symbol *sym;
  3033.   register struct symtab *s;
  3034.   register struct partial_symtab *ps;
  3035.   register struct minimal_symbol *msymbol;
  3036.   register struct objfile *objfile;
  3037.   register struct block *b, *surrounding_static_block = 0;
  3038.   register int i, j;
  3039.   struct partial_symbol *psym;
  3040.   /* The symbol we are completing on.  Points in same buffer as text.  */
  3041.   char *sym_text;
  3042.   /* Length of sym_text.  */
  3043.   int sym_text_len;
  3044.  
  3045.   /* Now look for the symbol we are supposed to complete on.
  3046.      FIXME: This should be language-specific.  */
  3047.   {
  3048.     char *p;
  3049.     char quote_found;
  3050.     char *quote_pos = NULL;
  3051.  
  3052.     /* First see if this is a quoted string.  */
  3053.     quote_found = '\0';
  3054.     for (p = text; *p != '\0'; ++p)
  3055.       {
  3056.     if (quote_found != '\0')
  3057.       {
  3058.         if (*p == quote_found)
  3059.           /* Found close quote.  */
  3060.           quote_found = '\0';
  3061.         else if (*p == '\\' && p[1] == quote_found)
  3062.           /* A backslash followed by the quote character
  3063.          doesn't end the string.  */
  3064.           ++p;
  3065.       }
  3066.     else if (*p == '\'' || *p == '"')
  3067.       {
  3068.         quote_found = *p;
  3069.         quote_pos = p;
  3070.       }
  3071.       }
  3072.     if (quote_found == '\'')
  3073.       /* A string within single quotes can be a symbol, so complete on it.  */
  3074.       sym_text = quote_pos + 1;
  3075.     else if (quote_found == '"')
  3076.       /* A double-quoted string is never a symbol, nor does it make sense
  3077.      to complete it any other way.  */
  3078.       return NULL;
  3079.     else
  3080.       {
  3081.     /* It is not a quoted string.  Break it based on the characters
  3082.        which are in symbols.  */
  3083.     while (p > text)
  3084.       {
  3085.         if (isalnum (p[-1]) || p[-1] == '_' || p[-1] == '\0')
  3086.           --p;
  3087.         else
  3088.           break;
  3089.       }
  3090.     sym_text = p;
  3091.       }
  3092.   }
  3093.  
  3094.   sym_text_len = strlen (sym_text);
  3095.  
  3096.   return_val_size = 100;
  3097.   return_val_index = 0;
  3098.   return_val = (char **) xmalloc ((return_val_size + 1) * sizeof (char *));
  3099.   return_val[0] = NULL;
  3100.  
  3101.   /* Look through the partial symtabs for all symbols which begin
  3102.      by matching SYM_TEXT.  Add each one that you find to the list.  */
  3103.  
  3104.   ALL_PSYMTABS (objfile, ps)
  3105.     {
  3106.       /* If the psymtab's been read in we'll get it when we search
  3107.      through the blockvector.  */
  3108.       if (ps->readin) continue;
  3109.       
  3110.       for (psym = objfile->global_psymbols.list + ps->globals_offset;
  3111.        psym < (objfile->global_psymbols.list + ps->globals_offset
  3112.            + ps->n_global_syms);
  3113.        psym++)
  3114.     {
  3115.       /* If interrupted, then quit. */
  3116.       QUIT;
  3117.       COMPLETION_LIST_ADD_SYMBOL (psym, sym_text, sym_text_len, text, word);
  3118.     }
  3119.       
  3120.       for (psym = objfile->static_psymbols.list + ps->statics_offset;
  3121.        psym < (objfile->static_psymbols.list + ps->statics_offset
  3122.            + ps->n_static_syms);
  3123.        psym++)
  3124.     {
  3125.       QUIT;
  3126.       COMPLETION_LIST_ADD_SYMBOL (psym, sym_text, sym_text_len, text, word);
  3127.     }
  3128.     }
  3129.  
  3130.   /* At this point scan through the misc symbol vectors and add each
  3131.      symbol you find to the list.  Eventually we want to ignore
  3132.      anything that isn't a text symbol (everything else will be
  3133.      handled by the psymtab code above).  */
  3134.  
  3135.   ALL_MSYMBOLS (objfile, msymbol)
  3136.     {
  3137.       QUIT;
  3138.       COMPLETION_LIST_ADD_SYMBOL (msymbol, sym_text, sym_text_len, text, word);
  3139.     }
  3140.  
  3141.   /* Search upwards from currently selected frame (so that we can
  3142.      complete on local vars.  */
  3143.  
  3144.   for (b = get_selected_block (); b != NULL; b = BLOCK_SUPERBLOCK (b))
  3145.     {
  3146.       if (!BLOCK_SUPERBLOCK (b))
  3147.     {
  3148.       surrounding_static_block = b;     /* For elmin of dups */
  3149.     }
  3150.       
  3151.       /* Also catch fields of types defined in this places which match our
  3152.      text string.  Only complete on types visible from current context. */
  3153.  
  3154.       for (i = 0; i < BLOCK_NSYMS (b); i++)
  3155.     {
  3156.       sym = BLOCK_SYM (b, i);
  3157.       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
  3158.       if (SYMBOL_CLASS (sym) == LOC_TYPEDEF)
  3159.         {
  3160.           struct type *t = SYMBOL_TYPE (sym);
  3161.           enum type_code c = TYPE_CODE (t);
  3162.  
  3163.           if (c == TYPE_CODE_UNION || c == TYPE_CODE_STRUCT)
  3164.         {
  3165.           for (j = TYPE_N_BASECLASSES (t); j < TYPE_NFIELDS (t); j++)
  3166.             {
  3167.               if (TYPE_FIELD_NAME (t, j))
  3168.             {
  3169.               completion_list_add_name (TYPE_FIELD_NAME (t, j),
  3170.                               sym_text, sym_text_len, text, word);
  3171.             }
  3172.             }
  3173.         }
  3174.         }
  3175.     }
  3176.     }
  3177.  
  3178.   /* Go through the symtabs and check the externs and statics for
  3179.      symbols which match.  */
  3180.  
  3181.   ALL_SYMTABS (objfile, s)
  3182.     {
  3183.       QUIT;
  3184.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
  3185.       for (i = 0; i < BLOCK_NSYMS (b); i++)
  3186.     {
  3187.       sym = BLOCK_SYM (b, i);
  3188.       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
  3189.     }
  3190.     }
  3191.  
  3192.   ALL_SYMTABS (objfile, s)
  3193.     {
  3194.       QUIT;
  3195.       b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
  3196.       /* Don't do this block twice.  */
  3197.       if (b == surrounding_static_block) continue;
  3198.       for (i = 0; i < BLOCK_NSYMS (b); i++)
  3199.     {
  3200.       sym = BLOCK_SYM (b, i);
  3201.       COMPLETION_LIST_ADD_SYMBOL (sym, sym_text, sym_text_len, text, word);
  3202.     }
  3203.     }
  3204.  
  3205.   return (return_val);
  3206. }
  3207.  
  3208.  
  3209. #if 0
  3210. /* Add the type of the symbol sym to the type of the current
  3211.    function whose block we are in (assumed).  The type of
  3212.    this current function is contained in *TYPE.
  3213.    
  3214.    This basically works as follows:  When we find a function
  3215.    symbol (N_FUNC with a 'f' or 'F' in the symbol name), we record
  3216.    a pointer to its type in the global in_function_type.  Every 
  3217.    time we come across a parameter symbol ('p' in its name), then
  3218.    this procedure adds the name and type of that parameter
  3219.    to the function type pointed to by *TYPE.  (Which should correspond
  3220.    to in_function_type if it was called correctly).
  3221.  
  3222.    Note that since we are modifying a type, the result of 
  3223.    lookup_function_type() should be memcpy()ed before calling
  3224.    this.  When not in strict typing mode, the expression
  3225.    evaluator can choose to ignore this.
  3226.  
  3227.    Assumption:  All of a function's parameter symbols will
  3228.    appear before another function symbol is found.  The parameters 
  3229.    appear in the same order in the argument list as they do in the
  3230.    symbol table. */
  3231.  
  3232. void
  3233. add_param_to_type (type,sym)
  3234.    struct type **type;
  3235.    struct symbol *sym;
  3236. {
  3237.    int num = ++(TYPE_NFIELDS(*type));
  3238.  
  3239.    if(TYPE_NFIELDS(*type)-1)
  3240.       TYPE_FIELDS(*type) = (struct field *)
  3241.       (*current_objfile->xrealloc) ((char *)(TYPE_FIELDS(*type)),
  3242.                     num*sizeof(struct field));
  3243.    else
  3244.       TYPE_FIELDS(*type) = (struct field *)
  3245.       (*current_objfile->xmalloc) (num*sizeof(struct field));
  3246.    
  3247.    TYPE_FIELD_BITPOS(*type,num-1) = num-1;
  3248.    TYPE_FIELD_BITSIZE(*type,num-1) = 0;
  3249.    TYPE_FIELD_TYPE(*type,num-1) = SYMBOL_TYPE(sym);
  3250.    TYPE_FIELD_NAME(*type,num-1) = SYMBOL_NAME(sym);
  3251. }
  3252. #endif 
  3253.  
  3254. void
  3255. _initialize_symtab ()
  3256. {
  3257.   add_info ("variables", variables_info,
  3258.         "All global and static variable names, or those matching REGEXP.");
  3259.   add_info ("functions", functions_info,
  3260.         "All function names, or those matching REGEXP.");
  3261.  
  3262.   /* FIXME:  This command has at least the following problems:
  3263.      1.  It prints builtin types (in a very strange and confusing fashion).
  3264.      2.  It doesn't print right, e.g. with
  3265.          typedef struct foo *FOO
  3266.      type_print prints "FOO" when we want to make it (in this situation)
  3267.      print "struct foo *".
  3268.      I also think "ptype" or "whatis" is more likely to be useful (but if
  3269.      there is much disagreement "info types" can be fixed).  */
  3270.   add_info ("types", types_info,
  3271.         "All type names, or those matching REGEXP.");
  3272.  
  3273. #if 0
  3274.   add_info ("methods", methods_info,
  3275.         "All method names, or those matching REGEXP::REGEXP.\n\
  3276. If the class qualifier is omitted, it is assumed to be the current scope.\n\
  3277. If the first REGEXP is omitted, then all methods matching the second REGEXP\n\
  3278. are listed.");
  3279. #endif
  3280.   add_info ("sources", sources_info,
  3281.         "Source files in the program.");
  3282.  
  3283.   add_com ("rbreak", no_class, rbreak_command,
  3284.         "Set a breakpoint for all functions matching REGEXP.");
  3285.  
  3286.   /* Initialize the one built-in type that isn't language dependent... */
  3287.   builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
  3288.                   "<unknown type>", (struct objfile *) NULL);
  3289. }
  3290.