gdb/python: smarter symbol lookup for gdb.lookup_static_symbol

When using gdb.lookup_static_symbol I think that GDB should find
static symbols (global symbol with static linkage) from the current
object file ahead of static symbols from other object files.

This means that if we have two source files f1.c and f2.c, and both
files contains 'static int foo;', then when we are stopped in f1.c a
call to 'gdb.lookup_static_symbol ("foo")' will find f1.c::foo, and if
we are stopped in f2.c we would find 'f2.c::foo'.

Given that gdb.lookup_static_symbol always returns a single symbol,
but there can be multiple static symbols with the same name GDB is
always making a choice about which symbols to return.  I think that it
makes sense for the choice GDB makes in this case to match what a user
would get on the command line if they asked to 'print foo'.

gdb/testsuite/ChangeLog:

	* gdb.python/py-symbol.c: Declare and call function from new
	py-symbol-2.c file.
	* gdb.python/py-symbol.exp: Compile both source files, and add new
	tests for gdb.lookup_static_symbol.
	* gdb.python/py-symbol-2.c: New file.

gdb/doc/ChangeLog:

	* python.texi (Symbols In Python): Extend documentation for
	gdb.lookup_static_symbol.

gdb/ChangeLog:

	* python/py-symbol.c (gdbpy_lookup_static_symbol): Lookup in
	static block of current object file first.  Also fix typo in
	header comment.

Change-Id: Ie55dbeb8806f35577b46015deecde27a0ca2ab64
This commit is contained in:
Andrew Burgess 2019-09-23 16:59:08 +01:00
parent eb2dd8df76
commit 09ff83af3c
8 changed files with 109 additions and 14 deletions

View file

@ -472,7 +472,7 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
}
/* Implementation of
gdb.lookup_static_symbol (name [, domain) -> symbol or None. */
gdb.lookup_static_symbol (name [, domain]) -> symbol or None. */
PyObject *
gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
@ -487,9 +487,32 @@ gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
&domain))
return NULL;
/* In order to find static symbols associated with the "current" object
file ahead of those from other object files, we first need to see if
we can acquire a current block. If this fails however, then we still
want to search all static symbols, so don't throw an exception just
yet. */
const struct block *block = NULL;
try
{
symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
struct frame_info *selected_frame
= get_selected_frame (_("No frame selected."));
block = get_frame_block (selected_frame, NULL);
}
catch (const gdb_exception &except)
{
/* Nothing. */
}
try
{
if (block != nullptr)
symbol
= lookup_symbol_in_static_block (name, block,
(domain_enum) domain).symbol;
if (symbol == nullptr)
symbol = lookup_static_symbol (name, (domain_enum) domain).symbol;
}
catch (const gdb_exception &except)
{