Add Objfile.lookup_{global,static}_symbol functions

This is essentially the inverse of Symbol.objfile. This allows
handling different symbols with the same name (but from different
objfiles) and can also be faster if the objfile is known.

gdb/ChangeLog:

2019-07-29  Christian Biesinger  <cbiesinger@google.com>

	* NEWS: Mention new functions Objfile.lookup_{global,static}_symbol.
	* python/py-objfile.c (objfpy_lookup_global_symbol): New function.
	(objfpy_lookup_static_symbol): New function.
	(objfile_object_methods): Add new functions.

gdb/doc/ChangeLog:

2019-07-29  Christian Biesinger  <cbiesinger@google.com>

	* python.texi (Objfiles In Python): Document new functions
	  Objfile.lookup_{global,static}_symbol.

gdb/testsuite/ChangeLog:

2019-07-29  Christian Biesinger  <cbiesinger@google.com>

	* gdb.python/py-objfile.c: Add global and static vars.
	* gdb.python/py-objfile.exp: Test new functions Objfile.
	  lookup_global_symbol and lookup_static_symbol.
This commit is contained in:
Christian Biesinger 2019-06-25 15:45:41 -05:00
parent e48de49be5
commit c620ed8866
8 changed files with 133 additions and 0 deletions

View file

@ -434,6 +434,74 @@ objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw)
Py_RETURN_NONE;
}
/* Implementation of
gdb.Objfile.lookup_global_symbol (self, string [, domain]) -> gdb.Symbol. */
static PyObject *
objfpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw)
{
static const char *keywords[] = { "name", "domain", NULL };
objfile_object *obj = (objfile_object *) self;
const char *symbol_name;
int domain = VAR_DOMAIN;
OBJFPY_REQUIRE_VALID (obj);
if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &symbol_name,
&domain))
return nullptr;
try
{
struct symbol *sym = lookup_global_symbol_from_objfile
(obj->objfile, GLOBAL_BLOCK, symbol_name, (domain_enum) domain).symbol;
if (sym == nullptr)
Py_RETURN_NONE;
return symbol_to_symbol_object (sym);
}
catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
Py_RETURN_NONE;
}
/* Implementation of
gdb.Objfile.lookup_static_symbol (self, string [, domain]) -> gdb.Symbol. */
static PyObject *
objfpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw)
{
static const char *keywords[] = { "name", "domain", NULL };
objfile_object *obj = (objfile_object *) self;
const char *symbol_name;
int domain = VAR_DOMAIN;
OBJFPY_REQUIRE_VALID (obj);
if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &symbol_name,
&domain))
return nullptr;
try
{
struct symbol *sym = lookup_global_symbol_from_objfile
(obj->objfile, STATIC_BLOCK, symbol_name, (domain_enum) domain).symbol;
if (sym == nullptr)
Py_RETURN_NONE;
return symbol_to_symbol_object (sym);
}
catch (const gdb_exception &except)
{
GDB_PY_HANDLE_EXCEPTION (except);
}
Py_RETURN_NONE;
}
/* Implement repr() for gdb.Objfile. */
static PyObject *
@ -652,6 +720,16 @@ Return true if this object file is valid, false if not." },
"add_separate_debug_file (file_name).\n\
Add FILE_NAME to the list of files containing debug info for the objfile." },
{ "lookup_global_symbol", (PyCFunction) objfpy_lookup_global_symbol,
METH_VARARGS | METH_KEYWORDS,
"lookup_global_symbol (name [, domain]).\n\
Look up a global symbol in this objfile and return it." },
{ "lookup_static_symbol", (PyCFunction) objfpy_lookup_static_symbol,
METH_VARARGS | METH_KEYWORDS,
"lookup_static_symbol (name [, domain]).\n\
Look up a static-linkage global symbol in this objfile and return it." },
{ NULL }
};