From patchwork Thu Sep 13 02:36:26 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Simon Marchi X-Patchwork-Id: 29355 Received: (qmail 34054 invoked by alias); 13 Sep 2018 02:37:02 -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 33913 invoked by uid 89); 13 Sep 2018 02:37:01 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-26.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3 autolearn=ham version=3.3.2 spammy= X-HELO: sesbmg23.ericsson.net Received: from sesbmg23.ericsson.net (HELO sesbmg23.ericsson.net) (193.180.251.37) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 13 Sep 2018 02:36:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; d=ericsson.com; s=mailgw201801; c=relaxed/simple; q=dns/txt; i=@ericsson.com; t=1536806216; h=From:Sender:Reply-To:Subject:Date:Message-Id:To:CC:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=IuWUU6PXFyMegJgmvmca4Kq/V1A8Y19el66RFQlC/zI=; b=WfgJcdhN0+8KtaR/WZ1cmUtZm8WLfUy7zJh8rVZcyMpw+xpLG3hMrBZ5IQtAC6jR ZmL4pVZgXDBQPz04B/z8J6u9EmvJY7sA5c3eWgeXyegOs4IiA/ZrqDXSXt7QZ9K3 hXnFpQRrsN6ICZGd3v9e8xvte/BVmnQPOminEqCZyuc=; Received: from ESESSMB504.ericsson.se (Unknown_Domain [153.88.183.122]) by sesbmg23.ericsson.net (Symantec Mail Security) with SMTP id BD.0E.05037.84DC99B5; Thu, 13 Sep 2018 04:36:56 +0200 (CEST) Received: from ESESSMR505.ericsson.se (153.88.183.127) by ESESSMB504.ericsson.se (153.88.183.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Thu, 13 Sep 2018 04:36:55 +0200 Received: from ESESBMB502.ericsson.se (153.88.183.169) by ESESSMR505.ericsson.se (153.88.183.127) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3; Thu, 13 Sep 2018 04:36:55 +0200 Received: from NAM01-BY2-obe.outbound.protection.outlook.com (153.88.183.157) by ESESBMB502.ericsson.se (153.88.183.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1466.3 via Frontend Transport; Thu, 13 Sep 2018 04:36:55 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericsson.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=3Si6AP/Ec+FPXexMWMtg4J0TWN/ST2N/4P6QZmHv3hA=; b=EagUFfZ6atZBRnEaReAZRc9ExY/4MsxBnS4Gma2+G3NwT+Ws2I9WhdIHW+qVNE2yPAzM3nfQ8ZuU7mXgn2zuwFCug1iM6wUtv1ihTTwc+UWqHwDNwAMcL9e8mVRvmekQ1Yc49E54N2C2ZOTBkbfY1abLCkr5VjBQq4tiY6Xa50I= Authentication-Results: spf=none (sender IP is ) smtp.mailfrom=simon.marchi@ericsson.com; Received: from elxacz23q12.ericsson.se (192.176.1.81) by BN7PR15MB2385.namprd15.prod.outlook.com (2603:10b6:406:8c::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.1122.17; Thu, 13 Sep 2018 02:36:51 +0000 From: Simon Marchi To: CC: Simon Marchi Subject: [PATCH v2 2/3] python: Add Progspace.objfiles method Date: Wed, 12 Sep 2018 22:36:26 -0400 Message-Id: <20180913023627.18855-2-simon.marchi@ericsson.com> In-Reply-To: <20180913023627.18855-1-simon.marchi@ericsson.com> References: <20180913023627.18855-1-simon.marchi@ericsson.com> MIME-Version: 1.0 Return-Path: simon.marchi@ericsson.com Received-SPF: None (protection.outlook.com: ericsson.com does not designate permitted sender hosts) X-IsSubscribed: yes New in v2: * throw an exception when using the method on an invalid Progspace. * Add the bit in the gdb.objfiles() doc. I have stolen these bits from Tom Tromey's 2014 patch, so I will include him in the relevant ChangeLog entries. This patch adds an objfiles method to the Progspace object, which returns a sequence of the objfiles associated to that program space. I chose a method rather than a property for symmetry with gdb.objfiles(). gdb/ChangeLog: * python/py-progspace.c (PSPY_REQUIRE_VALID): New macro. (pspy_get_objfiles): New function. (progspace_object_methods): New. (pspace_object_type): Add tp_methods callback. * python/python-internal.h (build_objfiles_list): New declaration. * python/python.c (build_objfiles_list): New function. (gdbpy_objfiles): Implement using build_objfiles_list. * NEWS: Mention the Progspace.objfiles method. gdb/doc/ChangeLog: * python.texi (Program Spaces In Python): Document the Progspace.objfiles method. (Objfiles In Python): Mention that gdb.objfiles() is identical to gdb.selected_inferior().progspace.objfiles(). gdb/testsuite/ChangeLog: * gdb.python/py-progspace.exp: Test the Progspace.objfiles method. --- gdb/NEWS | 3 +++ gdb/doc/python.texi | 12 ++++++++- gdb/python/py-progspace.c | 32 ++++++++++++++++++++--- gdb/python/python-internal.h | 4 +++ gdb/python/python.c | 28 +++++++++++++------- gdb/testsuite/gdb.python/py-progspace.exp | 31 ++++++++++++++++++++++ 6 files changed, 96 insertions(+), 14 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index 73d95f2a559..0b5bd2f1f2f 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -89,6 +89,9 @@ CSKY GNU/LINUX csky*-*-linux ** The gdb.Inferior type has a new progspace property, which is the program space associated to that inferior. + ** The gdb.Progspace type has a new objfiles method, which returns the list + of objfiles associated to that program space. + *** Changes in GDB 8.2 * The 'set disassembler-options' command now supports specifying options diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index ca3e651098f..6b3b598039d 100644 --- a/gdb/doc/python.texi +++ b/gdb/doc/python.texi @@ -4079,6 +4079,14 @@ Hello. [Inferior 1 (process 4242) exited normally] @end smallexample +A @code{gdb.Progspace} object has the following methods: + +@findex Progspace.objfiles +@defun Progspace.objfiles () +Return a sequence of all the objfiles referenced by this program +space. @xref{Objfiles In Python}. +@end defun + @node Objfiles In Python @subsubsection Objfiles In Python @@ -4105,7 +4113,9 @@ this function returns @code{None}. @findex gdb.objfiles @defun gdb.objfiles () Return a sequence of all the objfiles current known to @value{GDBN}. -@xref{Objfiles In Python}. +@xref{Objfiles In Python}. This is identical to +@code{gdb.selected_inferior().progspace.objfiles()} (@pxref{Progspaces In +Python}) and is included for historical compatibility. @end defun @findex gdb.lookup_objfile diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 3eaa466666d..e01338f43d3 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -58,7 +58,16 @@ extern PyTypeObject pspace_object_type static const struct program_space_data *pspy_pspace_data_key; - +/* Require that PSPACE_OBJ be a valid program space ID. */ +#define PSPY_REQUIRE_VALID(pspace_obj) \ + do { \ + if (pspace_obj->pspace == nullptr) \ + { \ + PyErr_SetString (PyExc_RuntimeError, \ + _("Program space no longer exists.")); \ + return NULL; \ + } \ + } while (0) /* An Objfile method which returns the objfile's file name, or None. */ @@ -314,7 +323,17 @@ pspy_set_type_printers (PyObject *o, PyObject *value, void *ignore) return 0; } - +/* Implement the objfiles method. */ + +static PyObject * +pspy_get_objfiles (PyObject *self_, PyObject *args) +{ + pspace_object *self = (pspace_object *) self_; + + PSPY_REQUIRE_VALID (self); + + return build_objfiles_list (self->pspace).release (); +} /* Clear the PSPACE pointer in a Pspace object and remove the reference. */ @@ -397,6 +416,13 @@ static gdb_PyGetSetDef pspace_getset[] = { NULL } }; +static PyMethodDef progspace_object_methods[] = +{ + { "objfiles", pspy_get_objfiles, METH_NOARGS, + "Return a sequence of objfiles associated to this program space." }, + { NULL } +}; + PyTypeObject pspace_object_type = { PyVarObject_HEAD_INIT (NULL, 0) @@ -426,7 +452,7 @@ PyTypeObject pspace_object_type = 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ - 0, /* tp_methods */ + progspace_object_methods, /* tp_methods */ 0, /* tp_members */ pspace_getset, /* tp_getset */ 0, /* tp_base */ diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 3874fdc5e79..785ad171511 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -549,6 +549,10 @@ struct symtab_and_line *sal_object_to_symtab_and_line (PyObject *obj); struct frame_info *frame_object_to_frame_info (PyObject *frame_obj); struct gdbarch *arch_object_to_gdbarch (PyObject *obj); +/* Return a Python list containing an Objfile object for each objfile in + PSPACE. */ +gdbpy_ref<> build_objfiles_list (program_space *pspace); + void gdbpy_initialize_gdb_readline (void); int gdbpy_initialize_auto_load (void) CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION; diff --git a/gdb/python/python.c b/gdb/python/python.c index 6f798a0e461..371f4a57529 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1437,10 +1437,10 @@ gdbpy_get_current_objfile (PyObject *unused1, PyObject *unused2) return result; } -/* Return a sequence holding all the Objfiles. */ +/* See python-internal.h. */ -static PyObject * -gdbpy_objfiles (PyObject *unused1, PyObject *unused2) +gdbpy_ref<> +build_objfiles_list (program_space *pspace) { struct objfile *objf; @@ -1448,15 +1448,23 @@ gdbpy_objfiles (PyObject *unused1, PyObject *unused2) if (list == NULL) return NULL; - ALL_OBJFILES (objf) - { - PyObject *item = objfile_to_objfile_object (objf); + ALL_PSPACE_OBJFILES (pspace, objf) + { + PyObject *item = objfile_to_objfile_object (objf); - if (!item || PyList_Append (list.get (), item) == -1) - return NULL; - } + if (item == nullptr || PyList_Append (list.get (), item) == -1) + return NULL; + } - return list.release (); + return list; +} + +/* Return a sequence holding all the Objfiles. */ + +static PyObject * +gdbpy_objfiles (PyObject *unused1, PyObject *unused2) +{ + return build_objfiles_list (current_program_space).release (); } /* Compute the list of active python type printers and store them in diff --git a/gdb/testsuite/gdb.python/py-progspace.exp b/gdb/testsuite/gdb.python/py-progspace.exp index f0cafa834e0..fa1ffd9e283 100644 --- a/gdb/testsuite/gdb.python/py-progspace.exp +++ b/gdb/testsuite/gdb.python/py-progspace.exp @@ -51,3 +51,34 @@ gdb_py_test_silent_cmd "python progspace.random_attribute = 42" \ "Set random attribute in progspace" 1 gdb_test "python print (progspace.random_attribute)" "42" \ "Verify set of random attribute in progspace" + +if {![runto_main]} { + fail "can't run to main" + return +} + +# With a single inferior, progspace.objfiles () and gdb.objfiles () should +# be identical. +gdb_test "python print (progspace.objfiles () == gdb.objfiles ())" "True" + +gdb_test "add-inferior" +gdb_test "inferior 2" + +gdb_load ${binfile} + +# With a second (non-started) inferior, we should have a single objfile - the +# main one. +gdb_test "python print (len (gdb.objfiles ())) == 1" + +# And the gdb.objfiles() list should now be different from the objfiles of the +# prog space of inferior 1. +gdb_test "python print (progspace.objfiles () != gdb.objfiles ())" "True" + +# Delete inferior 2 (and therefore the second progspace), check that the Python +# object reacts sensibly. +gdb_py_test_silent_cmd "python progspace2 = gdb.current_progspace()" \ + "save progspace 2" 1 +gdb_test "inferior 1" "Switching to inferior 1.*" +gdb_test_no_output "remove-inferiors 2" +gdb_test "python print (progspace2.objfiles ())" \ + "RuntimeError: Program space no longer exists.*"