Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@ericsson.com>
To: <gdb-patches@sourceware.org>
Cc: Simon Marchi <simon.marchi@ericsson.com>
Subject: [PATCH 1/3] python: Add Inferior.progspace property
Date: Wed, 12 Sep 2018 19:37:00 -0000	[thread overview]
Message-ID: <20180912193617.16523-1-simon.marchi@ericsson.com> (raw)

This patch adds a progspace property to the gdb.Inferior type, which
allows getting the gdb.Progspace object associated to that inferior.
In conjunction with the following patch, this will allow scripts iterate
on objfiles associated with a particular inferior.

While modifying py-inferior.exp, I added some checks for the other
Inferior properties, when the Inferior is no longer valid.  This doesn't
seem tested at the moment.

gdb/ChangeLog:

	* python/py-inferior.c (infpy_get_progspace): New function.
	(inferior_object_getset): Add progspace property.
	* NEWS: Mention the new property.

gdb/doc/ChangeLog:

	* python.texi (Inferiors In Python): Document
	Inferior.progspace.

gdb/testsuite/ChangeLog:

	* gdb.python/py-inferior.exp: Add tests for Inferior.progspace
	and a few other Inferior properties when the Inferior is no
	longer valid.
---
 gdb/NEWS                                 |  5 +++++
 gdb/doc/python.texi                      |  4 ++++
 gdb/python/py-inferior.c                 | 18 ++++++++++++++++++
 gdb/testsuite/gdb.python/py-inferior.exp | 18 ++++++++++++++++++
 4 files changed, 45 insertions(+)

diff --git a/gdb/NEWS b/gdb/NEWS
index 4e4f12d8d13..21da6ae4ba8 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -84,6 +84,11 @@ GNU/Linux/RISC-V		riscv*-*-linux*
 CSKY ELF			csky*-*-elf
 CSKY GNU/LINUX			csky*-*-linux
 
+* Python API
+
+  ** The program space associated to an inferior is now accessible through a
+     new "progspace" attribute of gdb.Inferior.
+
 *** 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 5e2ab76d2bc..d4f03439f10 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -2832,6 +2832,10 @@ Boolean signaling whether the inferior was created using `attach', or
 started by @value{GDBN} itself.
 @end defvar
 
+@defvar Inferior.progspace
+Program space associated to this inferrior, as a @code{gdb.Progspace}.
+@end defvar
+
 A @code{gdb.Inferior} object has the following methods:
 
 @defun Inferior.is_valid ()
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 8e6f148b17a..e770585184b 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -459,6 +459,23 @@ infpy_get_was_attached (PyObject *self, void *closure)
   Py_RETURN_FALSE;
 }
 
+/* Getter of gdb.Inferior.progspace.  */
+
+static PyObject *
+infpy_get_progspace (PyObject *self, void *closure)
+{
+  inferior_object *inf = (inferior_object *) self;
+
+  INFPY_REQUIRE_VALID (inf);
+
+  program_space *pspace = inf->inferior->pspace;
+  gdb_assert (pspace != nullptr);
+
+  PyObject *py_pspace = pspace_to_pspace_object (pspace);
+  Py_XINCREF (py_pspace);
+  return py_pspace;
+}
+
 static int
 build_inferior_list (struct inferior *inf, void *arg)
 {
@@ -963,6 +980,7 @@ static gdb_PyGetSetDef inferior_object_getset[] =
     NULL },
   { "was_attached", infpy_get_was_attached, NULL,
     "True if the inferior was created using 'attach'.", NULL },
+  { "progspace", infpy_get_progspace, NULL, "Program space of this inferior" },
   { NULL }
 };
 
diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp
index 6f4a3146684..01307531412 100644
--- a/gdb/testsuite/gdb.python/py-inferior.exp
+++ b/gdb/testsuite/gdb.python/py-inferior.exp
@@ -54,6 +54,9 @@ gdb_test "python print ('result = %s' % i0.pid)" " = \[0-9\]+" "test Inferior.pi
 gdb_test "python print ('result = %s' % i0.was_attached)" " = False" "test Inferior.was_attached"
 gdb_test "python print (i0.threads ())" "\\(<gdb.InferiorThread object at 0x\[\[:xdigit:\]\]+>,\\)" "test Inferior.threads"
 
+gdb_test "python print (i0.progspace)" "<gdb.Progspace object at $hex>"
+gdb_test "python print (i0.progspace == gdb.progspaces()\[0\])" "True"
+
 # Test the number of inferior threads.
 
 gdb_breakpoint check_threads
@@ -253,6 +256,21 @@ with_test_prefix "is_valid" {
 
     gdb_test "python print (my_inferior_count)" "1" \
 	"test inferior-deleted event handler"
+	
+    # Test that other properties and methods handle the removed inferior
+    # correctly.
+    gdb_test "python print (inf_list\[1\].num)" \
+	"RuntimeError: Inferior no longer exists.*"
+    gdb_test "python print (inf_list\[1\].pid)" \
+	"RuntimeError: Inferior no longer exists.*"
+    gdb_test "python print (inf_list\[1\].was_attached)" \
+	"RuntimeError: Inferior no longer exists.*"
+    gdb_test "python print (inf_list\[1\].progspace)" \
+	"RuntimeError: Inferior no longer exists.*"
+    gdb_test "python print (inf_list\[1\].threads ())" \
+	"RuntimeError: Inferior no longer exists.*"
+    gdb_test "python print (inf_list\[1\].thread_from_thread_handle (1))" \
+	"RuntimeError: Inferior no longer exists.*"
 }
 
 # Test gdb.selected_inferior()
-- 
2.19.0


             reply	other threads:[~2018-09-12 19:37 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-09-12 19:37 Simon Marchi [this message]
2018-09-12 19:36 ` [PATCH 3/3] python: Fix erroneous doc about gdb.objfiles() Simon Marchi
2018-09-12 21:46   ` Tom Tromey
2018-09-13  2:37   ` Eli Zaretskii
2018-09-13  2:42     ` Simon Marchi
2018-09-12 19:37 ` [PATCH 2/3] python: Add Progspace.objfiles method Simon Marchi
2018-09-12 22:01   ` Tom Tromey
2018-09-12 22:58     ` Simon Marchi
2018-09-13  4:50       ` Tom Tromey
2018-09-13  2:38   ` Eli Zaretskii
2018-09-12 21:42 ` [PATCH 1/3] python: Add Inferior.progspace property Tom Tromey
2018-09-12 22:22   ` Simon Marchi
2018-09-13  4:38     ` Tom Tromey
2018-09-13 22:16       ` Tom Tromey
2018-09-12 21:43 ` Tom Tromey
2018-09-13  2:37 ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180912193617.16523-1-simon.marchi@ericsson.com \
    --to=simon.marchi@ericsson.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox