[1/3] python: Add Inferior.progspace property

Message ID 20180912193617.16523-1-simon.marchi@ericsson.com
State New, archived
Headers

Commit Message

Simon Marchi Sept. 12, 2018, 7:36 p.m. UTC
  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(+)
  

Comments

Tom Tromey Sept. 12, 2018, 9:42 p.m. UTC | #1
>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:

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

I did this a long time ago, plus a bit more.
I think it would be good to compare approaches.

Old thread here:
https://sourceware.org/ml/gdb-patches/2014-06/msg00616.html

I rebaesd it - see the python/progspace-submit branch in my github.
I haven't re-tested it yet.

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

This could go in immediately if you were so inclined.

Tom
  
Tom Tromey Sept. 12, 2018, 9:43 p.m. UTC | #2
>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:

Simon> +Program space associated to this inferrior, as a @code{gdb.Progspace}.

Forgot to mention that earlier I noticed a typo in "inferrior".

Tom
  
Simon Marchi Sept. 12, 2018, 10:22 p.m. UTC | #3
On 2018-09-12 17:42, Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi <simon.marchi@ericsson.com> writes:
> 
> Simon> This patch adds a progspace property to the gdb.Inferior type, 
> which
> Simon> allows getting the gdb.Progspace object associated to that 
> inferior.
> Simon> In conjunction with the following patch, this will allow scripts 
> iterate
> Simon> on objfiles associated with a particular inferior.
> 
> I did this a long time ago, plus a bit more.
> I think it would be good to compare approaches.
> 
> Old thread here:
> https://sourceware.org/ml/gdb-patches/2014-06/msg00616.html
> 
> I rebaesd it - see the python/progspace-submit branch in my github.
> I haven't re-tested it yet.

Oh wow, that's much more complete than what I have.  I also spotted some 
other areas of improvement, but I know that if I try to bite too much in 
one go I never finish and submit what I have.  That's why I sent just 
these two by themselves.

If you intend on finishing and submitting that patch I'll drop mine.  
Otherwise, I'll go ahead and maybe submit yours chunk by chunk, adding 
tests in the process.

As far as the gdb.Inferior.progspace part is concerned, our two versions 
are essentially identical.

> Simon> While modifying py-inferior.exp, I added some checks for the 
> other
> Simon> Inferior properties, when the Inferior is no longer valid.  This 
> doesn't
> Simon> seem tested at the moment.
> 
> This could go in immediately if you were so inclined.

Ok, I'll push an obvious patch for that.

Simon
  
Eli Zaretskii Sept. 13, 2018, 2:37 a.m. UTC | #4
> From: Simon Marchi <simon.marchi@ericsson.com>
> CC: Simon Marchi <simon.marchi@ericsson.com>
> Date: Wed, 12 Sep 2018 15:36:15 -0400
> 
> 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.

OK for the documentation parts.

Thanks.
  

Patch

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()