/* Symbol table lookup for the GNU debugger, GDB.
- Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
- 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
+ Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
+ 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2007
Free Software Foundation, Inc.
This file is part of GDB.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place - Suite 330,
- Boston, MA 02111-1307, USA. */
+ Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ Boston, MA 02110-1301, USA. */
#include "defs.h"
#include "symtab.h"
#include "gdb_stat.h"
#include <ctype.h>
#include "cp-abi.h"
+#include "observer.h"
+#include "gdb_assert.h"
/* Prototypes for local functions */
}
\f
+/* Return 1 if the two sections are the same, or if they could
+ plausibly be copies of each other, one in an original object
+ file and another in a separated debug file. */
+
+int
+matching_bfd_sections (asection *first, asection *second)
+{
+ struct objfile *obj;
+
+ /* If they're the same section, then they match. */
+ if (first == second)
+ return 1;
+
+ /* If either is NULL, give up. */
+ if (first == NULL || second == NULL)
+ return 0;
+
+ /* This doesn't apply to absolute symbols. */
+ if (first->owner == NULL || second->owner == NULL)
+ return 0;
+
+ /* If they're in the same object file, they must be different sections. */
+ if (first->owner == second->owner)
+ return 0;
+
+ /* Check whether the two sections are potentially corresponding. They must
+ have the same size, address, and name. We can't compare section indexes,
+ which would be more reliable, because some sections may have been
+ stripped. */
+ if (bfd_get_section_size (first) != bfd_get_section_size (second))
+ return 0;
+
+ /* In-memory addresses may start at a different offset, relativize them. */
+ if (bfd_get_section_vma (first->owner, first)
+ - bfd_get_start_address (first->owner)
+ != bfd_get_section_vma (second->owner, second)
+ - bfd_get_start_address (second->owner))
+ return 0;
+
+ if (bfd_get_section_name (first->owner, first) == NULL
+ || bfd_get_section_name (second->owner, second) == NULL
+ || strcmp (bfd_get_section_name (first->owner, first),
+ bfd_get_section_name (second->owner, second)) != 0)
+ return 0;
+
+ /* Otherwise check that they are in corresponding objfiles. */
+
+ ALL_OBJFILES (obj)
+ if (obj->obfd == first->owner)
+ break;
+ gdb_assert (obj != NULL);
+
+ if (obj->separate_debug_objfile != NULL
+ && obj->separate_debug_objfile->obfd == second->owner)
+ return 1;
+ if (obj->separate_debug_objfile_backlink != NULL
+ && obj->separate_debug_objfile_backlink->obfd == second->owner)
+ return 1;
+
+ return 0;
+}
/* Find which partial symtab contains PC and SECTION. Return 0 if
none. We return the psymtab that contains a symbol whose address
{
struct partial_symtab *tpst;
struct partial_symtab *best_pst = pst;
- struct partial_symbol *best_psym = NULL;
+ CORE_ADDR best_addr = pst->textlow;
/* An objfile that has its functions reordered might have
many partial symbol tables containing the PC, but
if (pc >= tpst->textlow && pc < tpst->texthigh)
{
struct partial_symbol *p;
+ CORE_ADDR this_addr;
+ /* NOTE: This assumes that every psymbol has a
+ corresponding msymbol, which is not necessarily
+ true; the debug info might be much richer than the
+ object's symbol table. */
p = find_pc_sect_psymbol (tpst, pc, section);
if (p != NULL
&& SYMBOL_VALUE_ADDRESS (p)
== SYMBOL_VALUE_ADDRESS (msymbol))
return (tpst);
+
+ /* Also accept the textlow value of a psymtab as a
+ "symbol", to provide some support for partial
+ symbol tables with line information but no debug
+ symbols (e.g. those produced by an assembler). */
if (p != NULL)
+ this_addr = SYMBOL_VALUE_ADDRESS (p);
+ else
+ this_addr = tpst->textlow;
+
+ /* Check whether it is closer than our current
+ BEST_ADDR. Since this symbol address is
+ necessarily lower or equal to PC, the symbol closer
+ to PC is the symbol which address is the highest.
+ This way we return the psymtab which contains such
+ best match symbol. This can help in cases where the
+ symbol information/debuginfo is not complete, like
+ for instance on IRIX6 with gcc, where no debug info
+ is emitted for statics. (See also the nodebug.exp
+ testcase.) */
+ if (this_addr > best_addr)
{
- /* We found a symbol in this partial symtab which
- matches (or is closest to) PC, check whether it
- is closer than our current BEST_PSYM. Since
- this symbol address is necessarily lower or
- equal to PC, the symbol closer to PC is the
- symbol which address is the highest. */
- /* This way we return the psymtab which contains
- such best match symbol. This can help in cases
- where the symbol information/debuginfo is not
- complete, like for instance on IRIX6 with gcc,
- where no debug info is emitted for
- statics. (See also the nodebug.exp
- testcase.) */
- if (best_psym == NULL
- || SYMBOL_VALUE_ADDRESS (p)
- > SYMBOL_VALUE_ADDRESS (best_psym))
- {
- best_psym = p;
- best_pst = tpst;
- }
+ best_addr = this_addr;
+ best_pst = tpst;
}
-
}
}
return (best_pst);
if (section) /* match on a specific section */
{
fixup_psymbol_section (p, psymtab->objfile);
- if (SYMBOL_BFD_SECTION (p) != section)
+ if (!matching_bfd_sections (SYMBOL_BFD_SECTION (p), section))
continue;
}
best_pc = SYMBOL_VALUE_ADDRESS (p);
if (section) /* match on a specific section */
{
fixup_psymbol_section (p, psymtab->objfile);
- if (SYMBOL_BFD_SECTION (p) != section)
+ if (!matching_bfd_sections (SYMBOL_BFD_SECTION (p), section))
continue;
}
best_pc = SYMBOL_VALUE_ADDRESS (p);
if (needtofreename)
xfree (demangled_name);
+ /* Override the returned symtab with the symbol's specific one. */
+ if (returnval != NULL && symtab != NULL)
+ *symtab = SYMBOL_SYMTAB (returnval);
+
return returnval;
}
{
/* Search the list of symtabs for one which contains the
address of the start of this block. */
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
bv = BLOCKVECTOR (s);
b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
const struct block *block;
struct symtab *s;
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
bv = BLOCKVECTOR (s);
block = BLOCKVECTOR_BLOCK (bv, block_index);
of the desired name as a global, then do psymtab-to-symtab
conversion on the fly and return the found symbol. */
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
bv = BLOCKVECTOR (s);
block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
conversion on the fly and return the found symbol.
*/
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
bv = BLOCKVECTOR (s);
block = BLOCKVECTOR_BLOCK (bv, STATIC_BLOCK);
It also happens for objfiles that have their functions reordered.
For these, the symtab we are looking for is not necessarily read in. */
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
bv = BLOCKVECTOR (s);
b = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
ALL_BLOCK_SYMBOLS (b, iter, sym)
{
fixup_symbol_section (sym, objfile);
- if (section == SYMBOL_BFD_SECTION (sym))
+ if (matching_bfd_sections (SYMBOL_BFD_SECTION (sym), section))
break;
}
if (sym == NULL)
if (!best_symtab)
{
- if (!alt_symtab)
- { /* If we didn't find any line # info, just
- return zeros. */
- val.pc = pc;
- }
- else
- {
- val.symtab = alt_symtab;
- val.line = alt->line - 1;
-
- /* Don't return line 0, that means that we didn't find the line. */
- if (val.line == 0)
- ++val.line;
-
- val.pc = BLOCK_END (BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK));
- val.end = alt->pc;
- }
+ /* If we didn't find any line number info, just return zeros.
+ We used to return alt->line - 1 here, but that could be
+ anywhere; if we don't have line number info for this PC,
+ don't make some up. */
+ val.pc = pc;
}
else if (best->line == 0)
{
struct symtab *s;
struct partial_symtab *ps;
struct blockvector *bv;
- struct blockvector *prev_bv = 0;
struct block *b;
int i = 0;
struct dict_iterator iter;
QUIT;
/* If it would match (logic taken from loop below)
- load the file and go on to the next one */
+ load the file and go on to the next one. We check the
+ filename here, but that's a bit bogus: we don't know
+ what file it really comes from until we have full
+ symtabs. The symbol might be in a header file included by
+ this psymtab. This only affects Insight. */
if (file_matches (ps->filename, files, nfiles)
&& ((regexp == NULL
|| re_exec (SYMBOL_NATURAL_NAME (*psym)) != 0)
}
}
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
bv = BLOCKVECTOR (s);
- /* Often many files share a blockvector.
- Scan each blockvector only once so that
- we don't get every symbol many times.
- It happens that the first symtab in the list
- for any given blockvector is the main file. */
- if (bv != prev_bv)
for (i = GLOBAL_BLOCK; i <= STATIC_BLOCK; i++)
{
struct symbol_search *prevtail = tail;
b = BLOCKVECTOR_BLOCK (bv, i);
ALL_BLOCK_SYMBOLS (b, iter, sym)
{
+ struct symtab *real_symtab = SYMBOL_SYMTAB (sym);
QUIT;
- if (file_matches (s->filename, files, nfiles)
+
+ if (file_matches (real_symtab->filename, files, nfiles)
&& ((regexp == NULL
|| re_exec (SYMBOL_NATURAL_NAME (sym)) != 0)
&& ((kind == VARIABLES_DOMAIN && SYMBOL_CLASS (sym) != LOC_TYPEDEF
/* match */
psr = (struct symbol_search *) xmalloc (sizeof (struct symbol_search));
psr->block = i;
- psr->symtab = s;
+ psr->symtab = real_symtab;
psr->symbol = sym;
psr->msymbol = NULL;
psr->next = NULL;
tail = sort_search_symbols (prevtail, nfound);
}
}
- prev_bv = bv;
}
/* If there are no eyes, avoid all contact. I mean, if there are
/* Go through the symtabs and check the externs and statics for
symbols which match. */
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
QUIT;
b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), GLOBAL_BLOCK);
}
}
- ALL_SYMTABS (objfile, s)
+ ALL_PRIMARY_SYMTABS (objfile, s)
{
QUIT;
b = BLOCKVECTOR_BLOCK (BLOCKVECTOR (s), STATIC_BLOCK);
prologue_sal = find_pc_line (start_pc, 0);
if (prologue_sal.line != 0)
{
+ /* If there is only one sal that covers the entire function,
+ then it is probably a single line function, like
+ "foo(){}". */
+ if (prologue_sal.end == end_pc)
+ return 0;
while (prologue_sal.end < end_pc)
{
struct symtab_and_line sal;
}
}
+/* Deduce the name of the main procedure, and set NAME_OF_MAIN
+ accordingly. */
+
+static void
+find_main_name (void)
+{
+ char *new_main_name;
+
+ /* Try to see if the main procedure is in Ada. */
+ /* FIXME: brobecker/2005-03-07: Another way of doing this would
+ be to add a new method in the language vector, and call this
+ method for each language until one of them returns a non-empty
+ name. This would allow us to remove this hard-coded call to
+ an Ada function. It is not clear that this is a better approach
+ at this point, because all methods need to be written in a way
+ such that false positives never be returned. For instance, it is
+ important that a method does not return a wrong name for the main
+ procedure if the main procedure is actually written in a different
+ language. It is easy to guaranty this with Ada, since we use a
+ special symbol generated only when the main in Ada to find the name
+ of the main procedure. It is difficult however to see how this can
+ be guarantied for languages such as C, for instance. This suggests
+ that order of call for these methods becomes important, which means
+ a more complicated approach. */
+ new_main_name = ada_main_name ();
+ if (new_main_name != NULL)
+ {
+ set_main_name (new_main_name);
+ return;
+ }
+
+ /* The languages above didn't identify the name of the main procedure.
+ Fallback to "main". */
+ set_main_name ("main");
+}
+
char *
main_name (void)
{
- if (name_of_main != NULL)
- return name_of_main;
- else
- return "main";
+ if (name_of_main == NULL)
+ find_main_name ();
+
+ return name_of_main;
}
+/* Handle ``executable_changed'' events for the symtab module. */
+
+static void
+symtab_observer_executable_changed (void *unused)
+{
+ /* NAME_OF_MAIN may no longer be the same, so reset it for now. */
+ set_main_name (NULL);
+}
void
_initialize_symtab (void)
{
- add_info ("variables", variables_info,
- "All global and static variable names, or those matching REGEXP.");
+ add_info ("variables", variables_info, _("\
+All global and static variable names, or those matching REGEXP."));
if (dbx_commands)
- add_com ("whereis", class_info, variables_info,
- "All global and static variable names, or those matching REGEXP.");
+ add_com ("whereis", class_info, variables_info, _("\
+All global and static variable names, or those matching REGEXP."));
add_info ("functions", functions_info,
- "All function names, or those matching REGEXP.");
+ _("All function names, or those matching REGEXP."));
/* FIXME: This command has at least the following problems:
I also think "ptype" or "whatis" is more likely to be useful (but if
there is much disagreement "info types" can be fixed). */
add_info ("types", types_info,
- "All type names, or those matching REGEXP.");
+ _("All type names, or those matching REGEXP."));
add_info ("sources", sources_info,
- "Source files in the program.");
+ _("Source files in the program."));
add_com ("rbreak", class_breakpoint, rbreak_command,
- "Set a breakpoint for all functions matching REGEXP.");
+ _("Set a breakpoint for all functions matching REGEXP."));
if (xdb_commands)
{
- add_com ("lf", class_info, sources_info, "Source files in the program");
- add_com ("lg", class_info, variables_info,
- "All global and static variable names, or those matching REGEXP.");
+ add_com ("lf", class_info, sources_info,
+ _("Source files in the program"));
+ add_com ("lg", class_info, variables_info, _("\
+All global and static variable names, or those matching REGEXP."));
}
/* Initialize the one built-in type that isn't language dependent... */
builtin_type_error = init_type (TYPE_CODE_ERROR, 0, 0,
"<unknown type>", (struct objfile *) NULL);
+
+ observer_attach_executable_changed (symtab_observer_executable_changed);
}