[2/2] Add Inferior.architecture method

Message ID 20180915184530.3657-3-tom@tromey.com
State New, archived
Headers

Commit Message

Tom Tromey Sept. 15, 2018, 6:45 p.m. UTC
  I've written a couple of gdb unwinders in Python, and while doing so,
I wanted to find the architecture of the inferior.  (In an unwinder in
particular, one can't use the frame's architecture, because there is
no frame.)

This patch adds Inferior.architecture to allow this.  Normally I think
I would have chosen an attribute and not a method here, but seeing
that Frame.architecture is a method, I chose a method as well, for
consistency.

gdb/ChangeLog
2018-09-15  Tom Tromey  <tom@tromey.com>

	PR python/19399:
	* python/py-inferior.c: Add "architecture" entry.
	(infpy_architecture): New function.

gdb/doc/ChangeLog
2018-09-15  Tom Tromey  <tom@tromey.com>

	PR python/19399:
	* python.texi (Inferiors In Python): Document
	Inferior.Architecture.

gdb/testsuite/ChangeLog
2018-09-15  Tom Tromey  <tom@tromey.com>

	PR python/19399:
	* gdb.python/py-inferior.exp: Add architecture test.
---
 gdb/ChangeLog                            |  6 ++++++
 gdb/doc/ChangeLog                        |  6 ++++++
 gdb/doc/python.texi                      |  7 +++++++
 gdb/python/py-inferior.c                 | 15 +++++++++++++++
 gdb/testsuite/ChangeLog                  |  5 +++++
 gdb/testsuite/gdb.python/py-inferior.exp |  8 ++++++++
 6 files changed, 47 insertions(+)
  

Comments

Eli Zaretskii Sept. 15, 2018, 7:58 p.m. UTC | #1
> From: Tom Tromey <tom@tromey.com>
> Cc: Tom Tromey <tom@tromey.com>
> Date: Sat, 15 Sep 2018 12:45:30 -0600
> 
> gdb/doc/ChangeLog
> 2018-09-15  Tom Tromey  <tom@tromey.com>
> 
> 	PR python/19399:
> 	* python.texi (Inferiors In Python): Document
> 	Inferior.Architecture.

OK for this part, thanks.
  

Patch

diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index bd444daff5a..751c400bf32 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -2856,6 +2856,13 @@  when it is called.  If there are no valid threads, the method will
 return an empty tuple.
 @end defun
 
+@defun Inferior.architecture ()
+Return the @code{gdb.Architecture} (@pxref{Architectures In Python})
+for this inferior.  This represents the architecture of the inferior
+as a whole.  Some platforms can have multiple architectures in a
+single address space, so this may not match the architecture of a
+particular frame (@pxref{Frames in Python}).
+
 @findex Inferior.read_memory
 @defun Inferior.read_memory (address, length)
 Read @var{length} addressable memory units from the inferior, starting at
diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c
index 6db3df412eb..539c9958ec1 100644
--- a/gdb/python/py-inferior.c
+++ b/gdb/python/py-inferior.c
@@ -877,6 +877,18 @@  infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw)
   return result;
 }
 
+/* Implementation of gdb.Inferior.architecture.  */
+
+static PyObject *
+infpy_architecture (PyObject *self, PyObject *args)
+{
+  inferior_object *inf = (inferior_object *) self;
+
+  INFPY_REQUIRE_VALID (inf);
+
+  return gdbarch_to_arch_object (inf->inferior->gdbarch);
+}
+
 /* Implement repr() for gdb.Inferior.  */
 
 static PyObject *
@@ -1010,6 +1022,9 @@  Return a long with the address of a match, or None." },
     METH_VARARGS | METH_KEYWORDS,
     "thread_from_thread_handle (handle) -> gdb.InferiorThread.\n\
 Return thread object corresponding to thread handle." },
+  { "architecture", (PyCFunction) infpy_architecture, METH_NOARGS,
+    "architecture () -> gdb.Architecture\n\
+Return architecture of this inferior." },
   { NULL }
 };
 
diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.python/py-inferior.exp
index 38f52573b19..7b1a01b3ea6 100644
--- a/gdb/testsuite/gdb.python/py-inferior.exp
+++ b/gdb/testsuite/gdb.python/py-inferior.exp
@@ -299,3 +299,11 @@  with_test_prefix "__repr__" {
 	"\\\(<gdb.Inferior num=1, pid=$decimal>, <gdb.Inferior \\\(invalid\\\)>\\\)" \
 	"print all inferiors 2"
 }
+
+# Test architecture.
+with_test_prefix "architecture" {
+    gdb_test "inferior 1" ".*" "switch to first inferior"
+    gdb_test "python print(gdb.selected_frame().architecture() is gdb.selected_inferior().architecture())" \
+	"True" \
+	"inferior architecture matches frame architecture"
+}