Add Inferior.architecture method
authorTom Tromey <tom@tromey.com>
Sun, 7 Oct 2018 04:57:19 +0000 (22:57 -0600)
committerTom Tromey <tom@tromey.com>
Sun, 7 Oct 2018 05:20:55 +0000 (23:20 -0600)
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-10-06  Tom Tromey  <tom@tromey.com>

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

gdb/doc/ChangeLog
2018-10-06  Tom Tromey  <tom@tromey.com>

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

gdb/testsuite/ChangeLog
2018-10-06  Tom Tromey  <tom@tromey.com>

PR python/19399:
* gdb.python/py-inferior.exp: Add architecture test.

gdb/ChangeLog
gdb/doc/ChangeLog
gdb/doc/python.texi
gdb/python/py-inferior.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-inferior.exp

index aac1c6d6ce70369d44b1e2e5be336e37d0d30888..d896bf48e1c1df26c5379da7dac847f88f65feb2 100644 (file)
@@ -1,3 +1,9 @@
+2018-10-06  Tom Tromey  <tom@tromey.com>
+
+       PR python/19399:
+       * python/py-inferior.c: Add "architecture" entry.
+       (infpy_architecture): New function.
+
 2018-10-06  Tom Tromey  <tom@tromey.com>
 
        PR python/21765:
index 2e2894904c9f97182df19781a3f01436c56aaa1c..acf68ceb34d746d5c423c8a8ac6c9958fb79f89f 100644 (file)
@@ -1,3 +1,9 @@
+2018-10-06  Tom Tromey  <tom@tromey.com>
+
+       PR python/19399:
+       * python.texi (Inferiors In Python): Document
+       Inferior.Architecture.
+
 2018-10-06  Tom Tromey  <tom@tromey.com>
 
        PR python/21765:
index 0a8f7a1dc9a78b8ffaae194f44644ae7d3224b45..dc53cfad7c220f274318486b16cf02782a387ed5 100644 (file)
@@ -2860,6 +2860,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
index 5bba676478c6086de4305729a0cbd0f3f947c281..e987cfed225642ba48efae50716033240d30a3a4 100644 (file)
@@ -855,6 +855,18 @@ infpy_thread_from_thread_handle (PyObject *self, PyObject *args, PyObject *kw)
   Py_RETURN_NONE;
 }
 
+/* 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 *
@@ -988,6 +1000,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 }
 };
 
index f43936b8094f592322930feeb9989a6460a199b5..9473646aa0560e88155a740c13fa0c84325519ef 100644 (file)
@@ -1,3 +1,8 @@
+2018-10-06  Tom Tromey  <tom@tromey.com>
+
+       PR python/19399:
+       * gdb.python/py-inferior.exp: Add architecture test.
+
 2018-10-06  Tom Tromey  <tom@tromey.com>
 
        * gdb.base/gnu-ifunc.exp (build): Use standard_output_file.
index 38f52573b190fb4bdddb99fa525c8760c5565b2a..7b1a01b3ea6f55f51e1bf5fbbba16d3d1530fc37 100644 (file)
@@ -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"
+}
This page took 0.043873 seconds and 4 git commands to generate.