From patchwork Tue Jun 25 22:08:32 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Terekhov, Mikhail via Gdb-patches" X-Patchwork-Id: 33401 Received: (qmail 38778 invoked by alias); 25 Jun 2019 22:09:58 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 38691 invoked by uid 89); 25 Jun 2019 22:09:55 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.9 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=Christian, HX-Google-Smtp-Source:APXvYqw, sk:lookup_, H*MI:google X-HELO: mail-ua1-f73.google.com Received: from mail-ua1-f73.google.com (HELO mail-ua1-f73.google.com) (209.85.222.73) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 25 Jun 2019 22:09:53 +0000 Received: by mail-ua1-f73.google.com with SMTP id t2so36753uao.7 for ; Tue, 25 Jun 2019 15:09:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=VvIzCKJ0dd272LEHtL0AlLgt4N0+A4XWm/RM+4hIq5o=; b=hwgsCuDu6iAeZymUs6gJ5p1z0KAjpd4w8e7o5qO1YYbMIbHFLNToy42nO88Ry4ocV7 T8ZdMVSGIrwpnTqvySAbyzUlcqEwSNedHuzLfqfS2e+9F4h+JeUkOYh72msxLlLamU4V IhKKSGokFKgLGKC47ooKz+XCZqXkOoa9tEZavk7328QbSKnujPNjzhEQCSQiaGPjJetG tpaZrlIqc5j+1yqqxISEOZAEen3QXbKVow9D1vB5r6UeuoyYjv8FWFUf00N7WYRSWH1X 2VSeNDdwF+RG5U0nI2lyX8j88/Oky8ie/y1ZEmJNcwMQgaq2POp/IqrZzl0gD5qggxSL cRMQ== Date: Tue, 25 Jun 2019 17:08:32 -0500 Message-Id: <20190625220832.247935-1-cbiesinger@google.com> Mime-Version: 1.0 Subject: [PATCH] Add an Objfile.lookup_global_symbol function X-Patchwork-Original-From: "Christian Biesinger via gdb-patches" From: "Terekhov, Mikhail via Gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger 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-06-25 Christian Biesinger * NEWS: Mention new function Objfile.lookup_global_symbol. * compile/compile-object-load.c (compile_object_load): Pass GLOBAL_SCOPE. * python/py-objfile.c (objfpy_lookup_global_symbol): New function Objfile.lookup_global_symbol. * solib-spu.c (spu_lookup_lib_symbol): Pass GLOBAL_SCOPE. * solib-svr4.c (elf_lookup_lib_symbol): Pass GLOBAL_SCOPE. * symtab.c (lookup_global_symbol_from_objfile): Add a scope parameter. * symtab.h: Add a scope parameter to lookup_global_symbol_from_objfile. gdb/doc/ChangeLog: 2019-06-25 Christian Biesinger * python.texi: Document new function Objfile.lookup_global_symbol. gdb/testsuite/ChangeLog: 2019-06-25 Christian Biesinger * gdb.python/py-objfile.c: Add global and static vars. * gdb.python/py-objfile.exp: Test new function Objfile. lookup_global_symbol. --- gdb/NEWS | 3 ++ gdb/compile/compile-object-load.c | 1 + gdb/doc/python.texi | 12 +++++++ gdb/python/py-objfile.c | 45 ++++++++++++++++++++++++- gdb/solib-spu.c | 3 +- gdb/solib-svr4.c | 3 +- gdb/symtab.c | 3 +- gdb/symtab.h | 2 ++ gdb/testsuite/gdb.python/py-objfile.c | 3 ++ gdb/testsuite/gdb.python/py-objfile.exp | 5 +++ 10 files changed, 76 insertions(+), 4 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 2cc82e8656..e52af20748 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -36,6 +36,9 @@ ** gdb.Type has a new property 'objfile' which returns the objfile the type was defined in. + ** gdb.Objfile has a new method 'lookup_global_symbol' to lookup a symbol + from this objfile only. + * New commands | [COMMAND] | SHELL_COMMAND diff --git a/gdb/compile/compile-object-load.c b/gdb/compile/compile-object-load.c index 4e70205195..3a765a345b 100644 --- a/gdb/compile/compile-object-load.c +++ b/gdb/compile/compile-object-load.c @@ -639,6 +639,7 @@ compile_object_load (const compile_file_names &file_names, objfile = objfile_holder.get (); func_sym = lookup_global_symbol_from_objfile (objfile, + GLOBAL_BLOCK, GCC_FE_WRAPPER_FUNCTION, VAR_DOMAIN).symbol; if (func_sym == NULL) diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index f769ad03a2..01b5813d73 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4443,6 +4443,18 @@ searches then this function can be used to add a debug info file from a different place. @end defun +@defun Objfile.lookup_global_symbol (name @r{[}, domain@r{]}) +Searches for a global symbol by name in this objfile. Optionally, the +search scope can be restricted with the domain argument. +The @var{domain} argument must be a domain constant defined in the @code{gdb} +module and described in the @xref{Symbols In Python} section. This function is +similar to @code{gdb.lookup_global_symbol}, except that the search is limited +to this objfile. + +The result is a @code{gdb.Symbol} object or @code{None} if the symbol +is not found. +@end defun + @node Frames In Python @subsubsection Accessing inferior stack frames from Python diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index 199c567a04..eeb087eb3a 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -406,7 +406,7 @@ objfpy_is_valid (PyObject *self, PyObject *args) Py_RETURN_TRUE; } -/* Implementation of gdb.Objfile.add_separate_debug_file (self) -> Boolean. */ +/* Implementation of gdb.Objfile.add_separate_debug_file (self, string) -> None. */ static PyObject * objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw) @@ -434,6 +434,44 @@ objfpy_add_separate_debug_file (PyObject *self, PyObject *args, PyObject *kw) Py_RETURN_NONE; } +/* Implementation of gdb.Objfile.lookup_global_symbol (self, string [, int]) -> 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)) + Py_RETURN_NONE; + + try + { + struct symbol *sym = lookup_global_symbol_from_objfile + (obj->objfile, GLOBAL_BLOCK, symbol_name, (domain_enum) domain).symbol; + if (!sym) { + sym = lookup_global_symbol_from_objfile + (obj->objfile, STATIC_BLOCK, symbol_name, (domain_enum) domain) + .symbol; + if (!sym) + 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 +690,11 @@ 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\ +Looks up a global symbol in this objfile and returns it." }, + { NULL } }; diff --git a/gdb/solib-spu.c b/gdb/solib-spu.c index c5dc8639f1..faa4ac3956 100644 --- a/gdb/solib-spu.c +++ b/gdb/solib-spu.c @@ -392,7 +392,8 @@ spu_lookup_lib_symbol (struct objfile *objfile, const domain_enum domain) { if (bfd_get_arch (objfile->obfd) == bfd_arch_spu) - return lookup_global_symbol_from_objfile (objfile, name, domain); + return lookup_global_symbol_from_objfile (objfile, GLOBAL_BLOCK, name, + domain); if (svr4_so_ops.lookup_lib_global_symbol != NULL) return svr4_so_ops.lookup_lib_global_symbol (objfile, name, domain); diff --git a/gdb/solib-svr4.c b/gdb/solib-svr4.c index 8cd5b7d8e7..c0c505acaa 100644 --- a/gdb/solib-svr4.c +++ b/gdb/solib-svr4.c @@ -3226,7 +3226,8 @@ elf_lookup_lib_symbol (struct objfile *objfile, if (abfd == NULL || scan_dyntag (DT_SYMBOLIC, abfd, NULL, NULL) != 1) return {}; - return lookup_global_symbol_from_objfile (objfile, name, domain); + return lookup_global_symbol_from_objfile (objfile, GLOBAL_BLOCK, name, + domain); } void diff --git a/gdb/symtab.c b/gdb/symtab.c index 4920d94a24..7ab2f8f473 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -2223,13 +2223,14 @@ lookup_symbol_in_block (const char *name, symbol_name_match_type match_type, struct block_symbol lookup_global_symbol_from_objfile (struct objfile *main_objfile, + int block_index, const char *name, const domain_enum domain) { for (objfile *objfile : main_objfile->separate_debug_objfiles ()) { struct block_symbol result - = lookup_symbol_in_objfile (objfile, GLOBAL_BLOCK, name, domain); + = lookup_symbol_in_objfile (objfile, block_index, name, domain); if (result.symbol != NULL) return result; diff --git a/gdb/symtab.h b/gdb/symtab.h index e4ee7271a1..1df25fb9b5 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -2051,10 +2051,12 @@ extern enum language main_language (void); /* Lookup symbol NAME from DOMAIN in MAIN_OBJFILE's global blocks. This searches MAIN_OBJFILE as well as any associated separate debug info objfiles of MAIN_OBJFILE. + block_index can be GLOBAL_BLOCK, STATIC_BLOCK, etc. Upon success fixes up the symbol's section if necessary. */ extern struct block_symbol lookup_global_symbol_from_objfile (struct objfile *main_objfile, + int block_index, const char *name, const domain_enum domain); diff --git a/gdb/testsuite/gdb.python/py-objfile.c b/gdb/testsuite/gdb.python/py-objfile.c index ac41491234..6d751bddae 100644 --- a/gdb/testsuite/gdb.python/py-objfile.c +++ b/gdb/testsuite/gdb.python/py-objfile.c @@ -15,6 +15,9 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +int global_var = 42; +static int static_var = 50; + int main () { diff --git a/gdb/testsuite/gdb.python/py-objfile.exp b/gdb/testsuite/gdb.python/py-objfile.exp index b5e0c5e760..f90c2f881b 100644 --- a/gdb/testsuite/gdb.python/py-objfile.exp +++ b/gdb/testsuite/gdb.python/py-objfile.exp @@ -55,6 +55,11 @@ gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").filename)" \ gdb_test "python print (gdb.lookup_objfile (\"junk\"))" \ "Objfile not found\\.\r\n${python_error_text}" +gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"global_var\").name)" \ + "global_var" "lookup_global_symbol find a valid symbol" +gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"static_var\").name)" \ + "static_var" "lookup_global_symbol find a valid static symbol" + set binfile_build_id [get_build_id $binfile] if [string compare $binfile_build_id ""] { verbose -log "binfile_build_id = $binfile_build_id"