From patchwork Fri Jul 26 22:22:59 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: 33823 Received: (qmail 114466 invoked by alias); 26 Jul 2019 22:23:08 -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 114458 invoked by uid 89); 26 Jul 2019 22:23:08 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-22.8 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.1 spammy=gdb_load, anon, freed X-HELO: mail-qk1-f201.google.com Received: from mail-qk1-f201.google.com (HELO mail-qk1-f201.google.com) (209.85.222.201) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 26 Jul 2019 22:23:06 +0000 Received: by mail-qk1-f201.google.com with SMTP id k13so46393603qkj.4 for ; Fri, 26 Jul 2019 15:23:05 -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=GN8td/raXzr+pikI+eeoPMly+sPIyxrECj3NuLPdcv8=; b=hlSJy2RB3kmkol1AUpTLICqrynaQGEEYFWc3fkrBGvCS5mrIwlbpzhBTHV26hw7Ofm glfiCCgACwQkbsO5Zno8sioriQCQivnOsoieg6QGsMxqUVW46Fsp11M+rakUj+7Drdov JcfGO9+X4+owXEEh5nG8ryirjIj6ILsLI0AJ1wdEVX53cRNklxt+51QPbQMRZDM5wa4m B68Usn8Lt2XscSPWHUCpTdz2kBY/wuUqIaKt+UJVstfUTbl0fol+CoDqNUuioA8g6A6W E/jLh914bC9qH0j80uHjL6s/Cm2vCMZmvtiSIvBkFfBWBIIAUerOVItV+HaXvUwe7zG3 PI3g== Date: Fri, 26 Jul 2019 17:22:59 -0500 In-Reply-To: Message-Id: <20190726222259.12312-1-cbiesinger@google.com> Mime-Version: 1.0 References: Subject: [PATCH v2] [PR/24474] Add gdb.lookup_static_symbol to the python API 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 Similar to lookup_global_symbol, except that it checks the STATIC_SCOPE. gdb/ChangeLog: 2019-07-26 Christian Biesinger PR/24474: Add a function to lookup static variables. * NEWS: Mention this new function. * python/py-symbol.c (gdbpy_lookup_static_symbol): New function. * python/python-internal.h (gdbpy_lookup_static_symbol): New function. * python/python.c (python_GdbMethods): Add new function. gdb/doc/ChangeLog: 2019-07-26 Christian Biesinger * python.texi (Symbols In Python): Document new function gdb.lookup_static_symbol. gdb/testsuite/ChangeLog: 2019-07-26 Christian Biesinger * gdb.python/py-symbol.c: Add a static variable and one in an anonymous namespace. * gdb.python/py-symbol.exp: Test gdb.lookup_static_symbol. --- gdb/NEWS | 3 ++ gdb/doc/python.texi | 19 ++++++++++++ gdb/python/py-symbol.c | 40 ++++++++++++++++++++++++++ gdb/python/python-internal.h | 2 ++ gdb/python/python.c | 4 +++ gdb/testsuite/gdb.python/py-symbol.c | 5 ++++ gdb/testsuite/gdb.python/py-symbol.exp | 21 ++++++++++++++ 7 files changed, 94 insertions(+) diff --git a/gdb/NEWS b/gdb/NEWS index cc1d58520d..383295b3d2 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. + ** The new function gdb.lookup_static_symbol can be used to look up + symbols with static linkage. + * New commands | [COMMAND] | SHELL_COMMAND diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 034623513b..c5da7ad6b6 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4830,6 +4830,25 @@ The result is a @code{gdb.Symbol} object or @code{None} if the symbol is not found. @end defun +@findex gdb.lookup_static_symbol +@defun gdb.lookup_static_symbol (name @r{[}, domain@r{]}) +This function searches for a global symbol with static linkage by name. +The search scope can be restricted to by the domain argument. + +@var{name} is the name of the symbol. It must be a string. +The optional @var{domain} argument restricts the search to the domain type. +The @var{domain} argument must be a domain constant defined in the @code{gdb} +module and described later in this chapter. + +The result is a @code{gdb.Symbol} object or @code{None} if the symbol +is not found. + +Note that this function will not find function-scoped static variables. To look +up such variables, iterate over the variables of the function's +@code{gdb.Block} and check that @code{block.addr_class} is +@code{gdb.SYMBOL_LOC_STATIC}. +@end defun + A @code{gdb.Symbol} object has the following attributes: @defvar Symbol.type diff --git a/gdb/python/py-symbol.c b/gdb/python/py-symbol.c index 8605ae71a2..2b10e21d87 100644 --- a/gdb/python/py-symbol.c +++ b/gdb/python/py-symbol.c @@ -471,6 +471,46 @@ gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw) return sym_obj; } +/* Implementation of + gdb.lookup_static_symbol (name [, domain) -> symbol or None. */ + +PyObject * +gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, PyObject *kw) +{ + const char *name; + int domain = VAR_DOMAIN; + static const char *keywords[] = { "name", "domain", NULL }; + struct symbol *symbol = NULL; + PyObject *sym_obj; + + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|i", keywords, &name, + &domain)) + return NULL; + + try + { + symbol = lookup_static_symbol (name, (domain_enum) domain).symbol; + } + catch (const gdb_exception &except) + { + GDB_PY_HANDLE_EXCEPTION (except); + } + + if (symbol) + { + sym_obj = symbol_to_symbol_object (symbol); + if (!sym_obj) + return NULL; + } + else + { + sym_obj = Py_None; + Py_INCREF (Py_None); + } + + return sym_obj; +} + /* This function is called when an objfile is about to be freed. Invalidate the symbol as further actions on the symbol would result in bad data. All access to obj->symbol should be gated by diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index e6a3fe0ec1..c5578430cf 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -424,6 +424,8 @@ PyObject *gdbpy_frame_stop_reason_string (PyObject *, PyObject *); PyObject *gdbpy_lookup_symbol (PyObject *self, PyObject *args, PyObject *kw); PyObject *gdbpy_lookup_global_symbol (PyObject *self, PyObject *args, PyObject *kw); +PyObject *gdbpy_lookup_static_symbol (PyObject *self, PyObject *args, + PyObject *kw); PyObject *gdbpy_start_recording (PyObject *self, PyObject *args); PyObject *gdbpy_current_recording (PyObject *self, PyObject *args); PyObject *gdbpy_stop_recording (PyObject *self, PyObject *args); diff --git a/gdb/python/python.c b/gdb/python/python.c index 96bee7c3b0..162470dcc0 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1978,6 +1978,10 @@ a boolean indicating if name is a field of the current implied argument\n\ METH_VARARGS | METH_KEYWORDS, "lookup_global_symbol (name [, domain]) -> symbol\n\ Return the symbol corresponding to the given name (or None)." }, + { "lookup_static_symbol", (PyCFunction) gdbpy_lookup_static_symbol, + METH_VARARGS | METH_KEYWORDS, + "lookup_static_symbol (name [, domain]) -> symbol\n\ +Return the static-linkage symbol corresponding to the given name (or None)." }, { "lookup_objfile", (PyCFunction) gdbpy_lookup_objfile, METH_VARARGS | METH_KEYWORDS, diff --git a/gdb/testsuite/gdb.python/py-symbol.c b/gdb/testsuite/gdb.python/py-symbol.c index f77c8c8585..06a931bf5d 100644 --- a/gdb/testsuite/gdb.python/py-symbol.c +++ b/gdb/testsuite/gdb.python/py-symbol.c @@ -32,9 +32,14 @@ class SimpleClass return i; /* Break in class. */ } }; + +namespace { + int anon = 10; +}; #endif int qq = 72; /* line of qq */ +static int rr = 42; /* line of rr */ int func (int arg) { diff --git a/gdb/testsuite/gdb.python/py-symbol.exp b/gdb/testsuite/gdb.python/py-symbol.exp index 5b8a2be7c4..5d45e35da0 100644 --- a/gdb/testsuite/gdb.python/py-symbol.exp +++ b/gdb/testsuite/gdb.python/py-symbol.exp @@ -48,6 +48,22 @@ gdb_test "python print (gdb.lookup_global_symbol('qq').needs_frame)" \ "False" \ "print whether qq needs a frame" +set rr_line [gdb_get_line_number "line of rr"] +gdb_test "python print (gdb.lookup_global_symbol ('rr') is None)" "True" \ + "lookup_global_symbol for static var" + +gdb_test "python print (gdb.lookup_static_symbol ('rr').line)" "$rr_line" \ + "print line number of rr" + +gdb_test "python print (gdb.lookup_static_symbol ('rr').value ())" "42" \ + "print value of rr" + +gdb_test "python print (gdb.lookup_static_symbol ('rr').needs_frame)" \ + "False" \ + "print whether rr needs a frame" + +gdb_test "python print (gdb.lookup_global_symbol ('nonexistant') is None)" \ + "True" "lookup_static_symbol for nonexistant var" if ![runto_main] then { fail "can't run to main" @@ -137,6 +153,11 @@ gdb_start gdb_reinitialize_dir $srcdir/$subdir gdb_load ${binfile}-cxx +gdb_test "python print (gdb.lookup_global_symbol ('(anonymous namespace)::anon') is None)" \ + "True" "anon is None" +gdb_test "python print (gdb.lookup_static_symbol ('(anonymous namespace)::anon').value ())" \ + "10" "print value of anon" + if ![runto_main] then { fail "can't run to main" return 0