From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 83083 invoked by alias); 30 Jul 2019 01:51:28 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 83070 invoked by uid 89); 30 Jul 2019 01:51:28 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-23.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= X-HELO: mail-qk1-f202.google.com Received: from mail-qk1-f202.google.com (HELO mail-qk1-f202.google.com) (209.85.222.202) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 30 Jul 2019 01:51:26 +0000 Received: by mail-qk1-f202.google.com with SMTP id r200so53684145qke.19 for ; Mon, 29 Jul 2019 18:51:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=6qX4X6Y8bzGTv2mghh8bFOCt/wWaGQQuvOk2sYYG8ys=; b=pBzkrsRUq8AK4VCo5vJ27JwtphkxoMg/0UUZyzDT00uioxQwiDCYnnjjwZ/yF65/zy CP/9Oi0uo/Qi1qEn03Ih6EAWv07fLMtYXMbQ9YoWQJvT43+TUunzENsRor6c9eCanpeF lrRlWQQsGhRWfr0tx/lMEkTLNQY4r71vE2apVMDzjrOAMvZrnT0CCuPP7jdXCkuho0QL MqU17I2OSDy20CBQMCtu64nbtm+mv7S0c2JWeV6IKNqMthN6NIBNITr+344viIDbLSbY 2RJXdLYoTwbmTEvO7hARDwj2xKVEznsCdJ8eebCTrQzq5NNWkBJimuoJHdwFAFS+x8eJ 86PA== Date: Tue, 30 Jul 2019 01:51:00 -0000 In-Reply-To: <39420ba4-a5f2-783b-5a8a-2a10e08cb05a@simark.ca> Message-Id: <20190730015121.205269-1-cbiesinger@google.com> Mime-Version: 1.0 References: <39420ba4-a5f2-783b-5a8a-2a10e08cb05a@simark.ca> Subject: [PATCH v5] Add Objfile.lookup_{global,static}_symbol functions From: "Christian Biesinger via gdb-patches" Reply-To: Christian Biesinger To: gdb-patches@sourceware.org Cc: Christian Biesinger Content-Type: text/plain; charset="UTF-8" X-SW-Source: 2019-07/txt/msg00649.txt.bz2 Thanks Simon; fixed. I pushed the below version. 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 * 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 * python.texi (Objfiles In Python): Document new functions Objfile.lookup_{global,static}_symbol. gdb/testsuite/ChangeLog: 2019-07-29 Christian Biesinger * 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. --- gdb/ChangeLog | 7 +++ gdb/NEWS | 3 + gdb/doc/ChangeLog | 5 ++ gdb/doc/python.texi | 17 ++++++ gdb/python/py-objfile.c | 78 +++++++++++++++++++++++++ gdb/testsuite/ChangeLog | 6 ++ gdb/testsuite/gdb.python/py-objfile.c | 3 + gdb/testsuite/gdb.python/py-objfile.exp | 14 +++++ 8 files changed, 133 insertions(+) diff --git a/gdb/ChangeLog b/gdb/ChangeLog index dcc6a91b60..59fb8002e8 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,10 @@ +2019-07-29 Christian Biesinger + + * 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. + 2019-07-29 Philippe Waroquiers * NEWS: Mention 'set|show print frame-info'. Mention new diff --git a/gdb/NEWS b/gdb/NEWS index a08265cae7..4e821eab4c 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -41,6 +41,9 @@ there are no filters, or when the 'backtrace' '-no-filters' option is given. + ** gdb.Objfile has new methods 'lookup_global_symbol' and + 'lookup_static_symbol' to lookup a symbol from this objfile only. + * New commands | [COMMAND] | SHELL_COMMAND diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog index c409fe867b..9b91b998bd 100644 --- a/gdb/doc/ChangeLog +++ b/gdb/doc/ChangeLog @@ -1,3 +1,8 @@ +2019-07-29 Christian Biesinger + + * python.texi (Objfiles In Python): Document new functions + Objfile.lookup_{global,static}_symbol. + 2019-07-29 Philippe Waroquiers * gdb.texinfo (Backtrace): Document the new '-frame-info' diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 034623513b..bbba519ffc 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4441,6 +4441,23 @@ 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{]}) +Search for a global symbol named @var{name} in this objfile. Optionally, the +search scope can be restricted with the @var{domain} argument. +The @var{domain} argument must be a domain constant defined in the @code{gdb} +module and described in @ref{Symbols In Python}. 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 + +@defun Objfile.lookup_static_symbol (name @r{[}, domain@r{]}) +Like @code{Objfile.lookup_global_symbol}, but searches for a global +symbol with static linkage named @var{name} in this objfile. +@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 15735c8501..2c548450b4 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -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 } }; diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index a0b5862dd8..b2551063fe 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2019-07-29 Christian Biesinger + + * 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. + 2019-07-29 Tom Tromey * lib/tuiterm.exp (Term::_csi_@): New proc. 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..261f605445 100644 --- a/gdb/testsuite/gdb.python/py-objfile.exp +++ b/gdb/testsuite/gdb.python/py-objfile.exp @@ -55,6 +55,20 @@ 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 finds a valid symbol" +gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"static_var\") is None)" \ + "True" "lookup_global_symbol does not find static symbol" +gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_global_symbol (\"stdout\"))" \ + "None" "lookup_global_symbol doesn't find symbol in other objfile" + +gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"static_var\").name)" \ + "static_var" "lookup_static_symbol finds a valid symbol" +gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"global_var\") is None)" \ + "True" "lookup_static_symbol does not find global symbol" +gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").lookup_static_symbol (\"nonexistant\"))" \ + "None" "lookup_static_symbol can handle nonexistant symbol" + set binfile_build_id [get_build_id $binfile] if [string compare $binfile_build_id ""] { verbose -log "binfile_build_id = $binfile_build_id" -- 2.22.0.709.g102302147b-goog