This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [RFA] choose symbol from given block's objfile first.
> Attached are two patches, the first being the code change, and the
> second a testcase for it.
>
> gdb/ChangeLog:
>
> * findvar.c (default_read_var_value): For LOC_UNRESOLVED symbols,
> try locating the symbol in the symbol's own objfile first, before
> extending the search to all objfiles.
> * symtab.c (lookup_symbol_aux_objfile): New function, extracted
> out of lookup_symbol_aux_symtabs.
> (lookup_symbol_aux_symtabs): Add new parameter "exclude_objfile".
> Replace extracted-out code by call to lookup_symbol_aux_objfile.
> Do not search EXCLUDE_OBJFILE.
> (lookup_static_symbol_aux): Update call to lookup_symbol_aux_symtabs.
> (lookup_symbol_global): Search for matches in the block's objfile
> first, before searching all other objfiles.
>
> gdb/testsuite/ChangeLog:
>
> * gdb.base/ctxobj-f.c, gdb.base/ctxobj-m.c, gdb.base/ctxobj-v.c,
> gdb.base/ctxobj.exp: New files.
>
> Tested on x86_64-linux. No regression.
ENOPATCH.
--
Joel
>From 60b3578a23eef7b06e0a2e6a38191a2bcb09651e Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Mon, 7 May 2012 14:43:28 -0700
Subject: [PATCH 1/2] Search global symbols from the expression's block objfile first.
gdb/ChangeLog:
* findvar.c (default_read_var_value): For LOC_UNRESOLVED symbols,
try locating the symbol in the symbol's own objfile first, before
extending the search to all objfiles.
* symtab.c (lookup_symbol_aux_objfile): New function, extracted
out of lookup_symbol_aux_symtabs.
(lookup_symbol_aux_symtabs): Add new parameter "exclude_objfile".
Replace extracted-out code by call to lookup_symbol_aux_objfile.
Do not search EXCLUDE_OBJFILE.
(lookup_static_symbol_aux): Update call to lookup_symbol_aux_symtabs.
(lookup_symbol_global): Search for matches in the block's objfile
first, before searching all other objfiles.
---
gdb/findvar.c | 10 +++++-
gdb/symtab.c | 108 ++++++++++++++++++++++++++++++++++++++++-----------------
2 files changed, 85 insertions(+), 33 deletions(-)
diff --git a/gdb/findvar.c b/gdb/findvar.c
index 9009e6f..ed7903c 100644
--- a/gdb/findvar.c
+++ b/gdb/findvar.c
@@ -562,7 +562,15 @@ default_read_var_value (struct symbol *var, struct frame_info *frame)
struct minimal_symbol *msym;
struct obj_section *obj_section;
- msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
+ /* First, try locating the associated minimal symbol within
+ the same objfile. This prevents us from selecting another
+ symbol with the same name but located in a different objfile. */
+ msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL,
+ SYMBOL_SYMTAB (var)->objfile);
+ /* If the lookup failed, try expanding the search to all
+ objfiles. */
+ if (msym == NULL)
+ msym = lookup_minimal_symbol (SYMBOL_LINKAGE_NAME (var), NULL, NULL);
if (msym == NULL)
error (_("No global symbol \"%s\"."), SYMBOL_LINKAGE_NAME (var));
if (overlay_debugging)
diff --git a/gdb/symtab.c b/gdb/symtab.c
index d68e542..3fce349 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -95,7 +95,8 @@ struct symbol *lookup_symbol_aux_local (const char *name,
static
struct symbol *lookup_symbol_aux_symtabs (int block_index,
const char *name,
- const domain_enum domain);
+ const domain_enum domain,
+ struct objfile *exclude_objfile);
static
struct symbol *lookup_symbol_aux_quick (struct objfile *objfile,
@@ -1361,7 +1362,7 @@ lookup_static_symbol_aux (const char *name, const domain_enum domain)
struct objfile *objfile;
struct symbol *sym;
- sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, domain);
+ sym = lookup_symbol_aux_symtabs (STATIC_BLOCK, name, domain, NULL);
if (sym != NULL)
return sym;
@@ -1500,40 +1501,61 @@ lookup_global_symbol_from_objfile (const struct objfile *main_objfile,
return NULL;
}
-/* Check to see if the symbol is defined in one of the symtabs.
- BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
+/* Check to see if the symbol is defined in one of the OBJFILE's
+ symtabs. BLOCK_INDEX should be either GLOBAL_BLOCK or STATIC_BLOCK,
depending on whether or not we want to search global symbols or
static symbols. */
static struct symbol *
-lookup_symbol_aux_symtabs (int block_index, const char *name,
- const domain_enum domain)
+lookup_symbol_aux_objfile (struct objfile *objfile, int block_index,
+ const char *name, const domain_enum domain)
{
- struct symbol *sym;
- struct objfile *objfile;
+ struct symbol *sym = NULL;
struct blockvector *bv;
const struct block *block;
struct symtab *s;
+ if (objfile->sf)
+ objfile->sf->qf->pre_expand_symtabs_matching (objfile, block_index,
+ name, domain);
+
+ ALL_OBJFILE_SYMTABS (objfile, s)
+ if (s->primary)
+ {
+ bv = BLOCKVECTOR (s);
+ block = BLOCKVECTOR_BLOCK (bv, block_index);
+ sym = lookup_block_symbol (block, name, domain);
+ if (sym)
+ {
+ block_found = block;
+ return fixup_symbol_section (sym, objfile);
+ }
+ }
+
+ return NULL;
+}
+
+/* Same as lookup_symbol_aux_objfile, except that it searches all
+ objfiles except for EXCLUDE_OBJFILE. Return the first match found.
+
+ If EXCLUDE_OBJFILE is NULL, then all objfiles are searched. */
+
+static struct symbol *
+lookup_symbol_aux_symtabs (int block_index, const char *name,
+ const domain_enum domain,
+ struct objfile *exclude_objfile)
+{
+ struct symbol *sym;
+ struct objfile *objfile;
+
ALL_OBJFILES (objfile)
{
- if (objfile->sf)
- objfile->sf->qf->pre_expand_symtabs_matching (objfile,
- block_index,
- name, domain);
-
- ALL_OBJFILE_SYMTABS (objfile, s)
- if (s->primary)
- {
- bv = BLOCKVECTOR (s);
- block = BLOCKVECTOR_BLOCK (bv, block_index);
- sym = lookup_block_symbol (block, name, domain);
- if (sym)
- {
- block_found = block;
- return fixup_symbol_section (sym, objfile);
- }
- }
+ if (objfile != exclude_objfile)
+ {
+ sym = lookup_symbol_aux_objfile (objfile, block_index, name, domain);
+ if (sym)
+ return sym;
+ }
}
return NULL;
@@ -1659,24 +1681,46 @@ lookup_symbol_global (const char *name,
const domain_enum domain)
{
struct symbol *sym = NULL;
+ struct objfile *block_objfile = NULL;
struct objfile *objfile = NULL;
/* Call library-specific lookup procedure. */
- objfile = lookup_objfile_from_block (block);
- if (objfile != NULL)
- sym = solib_global_lookup (objfile, name, domain);
+ block_objfile = lookup_objfile_from_block (block);
+ if (block_objfile != NULL)
+ sym = solib_global_lookup (block_objfile, name, domain);
if (sym != NULL)
return sym;
- sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, domain);
+ /* If BLOCK_OBJFILE is not NULL, then search this objfile first.
+ In case the global symbol is defined in multiple objfiles,
+ we have a better chance of finding the most relevant symbol. */
+
+ if (block_objfile != NULL)
+ {
+ sym = lookup_symbol_aux_objfile (block_objfile, GLOBAL_BLOCK,
+ name, domain);
+ if (sym == NULL)
+ sym = lookup_symbol_aux_quick (block_objfile, GLOBAL_BLOCK,
+ name, domain);
+ if (sym != NULL)
+ return sym;
+ }
+
+ /* Symbol not found in the BLOCK_OBJFILE, so try all the other
+ objfiles, starting with symtabs first, and then partial symtabs. */
+
+ sym = lookup_symbol_aux_symtabs (GLOBAL_BLOCK, name, domain, block_objfile);
if (sym != NULL)
return sym;
ALL_OBJFILES (objfile)
{
- sym = lookup_symbol_aux_quick (objfile, GLOBAL_BLOCK, name, domain);
- if (sym)
- return sym;
+ if (objfile != block_objfile)
+ {
+ sym = lookup_symbol_aux_quick (objfile, GLOBAL_BLOCK, name, domain);
+ if (sym)
+ return sym;
+ }
}
return NULL;
--
1.7.1
>From c3b79f2dfd48d1052d012ce1c390fdb271bad2dc Mon Sep 17 00:00:00 2001
From: Joel Brobecker <brobecker@adacore.com>
Date: Wed, 9 May 2012 10:35:31 -0700
Subject: [PATCH 2/2] New testcase: gdb.base/ctxobj.exp
gdb/testsuite/ChangeLog:
* gdb.base/ctxobj-f.c, gdb.base/ctxobj-m.c, gdb.base/ctxobj-v.c,
gdb.base/ctxobj.exp: New files.
---
gdb/testsuite/gdb.base/ctxobj-f.c | 27 +++++++++++
gdb/testsuite/gdb.base/ctxobj-m.c | 31 ++++++++++++
gdb/testsuite/gdb.base/ctxobj-v.c | 21 ++++++++
gdb/testsuite/gdb.base/ctxobj.exp | 94 +++++++++++++++++++++++++++++++++++++
4 files changed, 173 insertions(+), 0 deletions(-)
create mode 100644 gdb/testsuite/gdb.base/ctxobj-f.c
create mode 100644 gdb/testsuite/gdb.base/ctxobj-m.c
create mode 100644 gdb/testsuite/gdb.base/ctxobj-v.c
create mode 100644 gdb/testsuite/gdb.base/ctxobj.exp
diff --git a/gdb/testsuite/gdb.base/ctxobj-f.c b/gdb/testsuite/gdb.base/ctxobj-f.c
new file mode 100644
index 0000000..43cc328
--- /dev/null
+++ b/gdb/testsuite/gdb.base/ctxobj-f.c
@@ -0,0 +1,27 @@
+/* This testcase is part of GDB, the GNU debugger.
+ Copyright 2012 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+extern int this_version_num;
+
+#ifndef GET_VERSION
+#error GET_VERSION macro is undefined
+#endif
+
+int
+GET_VERSION (void)
+{
+ return this_version_num;
+}
diff --git a/gdb/testsuite/gdb.base/ctxobj-m.c b/gdb/testsuite/gdb.base/ctxobj-m.c
new file mode 100644
index 0000000..203f838
--- /dev/null
+++ b/gdb/testsuite/gdb.base/ctxobj-m.c
@@ -0,0 +1,31 @@
+/* This testcase is part of GDB, the GNU debugger.
+ Copyright 2012 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+extern int get_version_1 (void);
+extern int get_version_2 (void);
+
+int
+main (void)
+{
+ if (get_version_1 () != 104)
+ return 1;
+
+ if (get_version_2 () != 203)
+ return 2;
+
+ return 0;
+}
+
diff --git a/gdb/testsuite/gdb.base/ctxobj-v.c b/gdb/testsuite/gdb.base/ctxobj-v.c
new file mode 100644
index 0000000..6d3a8d0
--- /dev/null
+++ b/gdb/testsuite/gdb.base/ctxobj-v.c
@@ -0,0 +1,21 @@
+/* This testcase is part of GDB, the GNU debugger.
+ Copyright 2012 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef VERSION
+#error VERSION macro is not defined.
+#endif
+
+int this_version_num = VERSION;
diff --git a/gdb/testsuite/gdb.base/ctxobj.exp b/gdb/testsuite/gdb.base/ctxobj.exp
new file mode 100644
index 0000000..57ed4c1
--- /dev/null
+++ b/gdb/testsuite/gdb.base/ctxobj.exp
@@ -0,0 +1,94 @@
+# Copyright 2012 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+set executable ctxobj-m
+
+# The sources used to build two shared libraries (SO). We use the exact
+# same sources to build both SOs, but differentiate them through the use
+# of macros defined when calling the compiler.
+#
+# We need two source files per SO, because we need to test the situation
+# where we are trying to print the value of a global variable defined
+# in that SO while the variable's associated symtab has not been created
+# yet.
+set libsrc [list "${srcdir}/${subdir}/ctxobj-v.c" \
+ "${srcdir}/${subdir}/ctxobj-f.c"]
+
+set libobj1 "${objdir}/${subdir}/libctxobj1.so"
+set libobj2 "${objdir}/${subdir}/libctxobj2.so"
+
+set libobj1_opts { debug additional_flags=-fPIC
+ additional_flags=-DVERSION=104
+ additional_flags=-DGET_VERSION=get_version_1 }
+set libobj2_opts { debug additional_flags=-fPIC
+ additional_flags=-DVERSION=203
+ additional_flags=-DGET_VERSION=get_version_2 }
+
+if { [gdb_compile_shlib $libsrc $libobj1 $libobj1_opts ] != "" } {
+ return -1
+}
+if { [gdb_compile_shlib $libsrc $libobj2 $libobj2_opts ] != "" } {
+ return -1
+}
+if { [gdb_compile "${srcdir}/${subdir}/${executable}.c" \
+ "${objdir}/${subdir}/${executable}" \
+ executable \
+ [list debug shlib=${libobj1} shlib=${libobj2}]]
+ != ""} {
+ return -1
+}
+
+clean_restart $executable
+
+if ![runto_main] {
+ untested "could not run to main"
+ return -1
+}
+
+gdb_breakpoint "get_version_1"
+gdb_test "continue" \
+ "Breakpoint $decimal, get_version_1 \\(\\).*" \
+ "continue to get_version_1"
+
+# Try printing "this_version_num". There are two global variables
+# with that name, but we should pick the one in the shared library
+# we are currently debugging. We will know we picked the correct one
+# if the value printed is 104. The first print test verifies that
+# we're doing things right when the partial symtab hasn't been
+# expanded. And the second print test will do the same, but after
+# the partial symtab has been expanded.
+
+gdb_test "print this_version_num" \
+ " = 104" \
+ "print libctxobj1's this_version_num from partial symtab"
+
+gdb_test "print this_version_num" \
+ " = 104" \
+ "print libctxobj1's this_version_num from symtab"
+
+# Do the same, but from get_version_2.
+
+gdb_breakpoint "get_version_2"
+gdb_test "continue" \
+ "Breakpoint $decimal, get_version_2 \\(\\).*" \
+ "continue to get_version_2"
+
+gdb_test "print this_version_num" \
+ " = 203" \
+ "print libctxobj2's this_version_num from partial symtab"
+
+gdb_test "print this_version_num" \
+ " = 203" \
+ "print libctxobj2's this_version_num from symtab"
--
1.7.1