[PATCH] Add gdb.current_arch() function to python module -- equivalent of guile (current-arch)

Matthew Malcomson hardenedapple@gmail.com
Mon Feb 6 17:02:00 GMT 2017


Hello there,

I noticed that while there is a guile function to get the current 
architecture
`(current-arch)`, there isn't a python equivalent.

I've been finding that awkward in some extensions I've been writing, mainly
when I want to work with the disassembly of a function before my target 
program
has started.

This patch adds a python equivalent: `gdb.current_arch()` that returns an
architecture object representing the current architecture (taken from the
`get_current_arch ()` function).

NOTES:
     To get gdb to build on my machine, I've applied the patch from bug 
21057 so
     it can work with newer versions of flex and bison. This is the 
reason the
     sha hashes of the patch below don't match any in the main tree.

     I believe this change is small enough to not be legally significant 
(under
     15 lines ignoring comments & documentation). I'm currently requesting a
     copyright assignment form for future changes so if it isn't small 
enough I
     should be able to provide documentation soon.

---------------------

CHANGELOG:

The gdb/CONTRIBUTE file mentions I should include a ChangeLog entry.
I may have gotten the wrong format here -- If so I apologise.

2017-02-06  Matthew Malcomson <hardenedapple@gmail.com> (tiny change)

     * python/python.c (python_GdbMethods): Update.
     python/python-internal.h (gdbpy_current_arch): Update.
     python/py-arch.c (gdbpy_current_arch): New.
     doc/python.texi (gdb.current_arch): Document.

------------------------------------


PATCH:

commit 59048e5c4241b2641815456741eee46be1de035c
Author: Matthew Malcomson <hardenedapple@gmail.com>
Date:   Sun Feb 5 15:40:05 2017 +0000

     Add gdb.current_arch() python function

     This provides the equivalent of the guile (current-arch) function.

diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index fae45aa5d9..5f15746f47 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -4898,6 +4898,14 @@ writable.
  number of its various computations.  An architecture is represented
  by an instance of the @code{gdb.Architecture} class.

+The following architecture-related functions are available in the 
@code{gdb}
+module:
+
+@findex gdb.current_arch
+@defun gdb.current_arch ()
+Return the current architecture object.
+@end defun
+
  A @code{gdb.Architecture} class has the following methods:

  @defun Architecture.name ()
diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c
index 8d0ec3330c..0f86ef80fe 100644
--- a/gdb/python/py-arch.c
+++ b/gdb/python/py-arch.c
@@ -230,6 +230,14 @@ archpy_disassemble (PyObject *self, PyObject *args, 
PyObject *kw)
    return result_list.release ();
  }

+/* Implementation of gdb.current_architecture () -> gdb.Architecture.
+   Returns the current architecture object.  */
+PyObject *
+gdbpy_current_arch (PyObject *self, PyObject *args)
+{
+    return gdbarch_to_arch_object (get_current_arch ());
+}
+
  /* Initializes the Architecture class in the gdb module.  */

  int
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index e2ebc1b8a2..3b9ac65c12 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -411,6 +411,7 @@ PyObject *objfpy_get_frame_unwinders (PyObject *, 
void *);
  PyObject *objfpy_get_xmethods (PyObject *, void *);
  PyObject *gdbpy_lookup_objfile (PyObject *self, PyObject *args, 
PyObject *kw);

+PyObject *gdbpy_current_arch (PyObject *self, PyObject *args);
  PyObject *gdbarch_to_arch_object (struct gdbarch *gdbarch);

  thread_object *create_thread_object (struct thread_info *tp);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index ab5a6a416d..db3461d9c0 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1893,6 +1893,13 @@ set to True." },
    { "objfiles", gdbpy_objfiles, METH_NOARGS,
      "Return a sequence of all loaded objfiles." },

+  { "current_arch", gdbpy_current_arch, METH_NOARGS,
+    "current_arch () -> gdb.Architecture.\n\
+Return the gdb.Architecture object representing the architecture of the\n\
+currently selected stack frame, if there is one, or the architecture of 
the\n\
+current target if there isn't.\n\
+" },
+
    { "newest_frame", gdbpy_newest_frame, METH_NOARGS,
      "newest_frame () -> gdb.Frame.\n\
  Return the newest frame object." },



More information about the Gdb-patches mailing list