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

Message ID ac00f967-3a7d-bd6a-88de-bcb98ff357ca@gmail.com
State New, archived
Headers

Commit Message

Matthew Malcomson Feb. 6, 2017, 5:01 p.m. UTC
  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.

  Return the newest frame object." },
  

Comments

Yao Qi Feb. 8, 2017, 5:15 p.m. UTC | #1
On Mon, Feb 6, 2017 at 5:01 PM, Matthew Malcomson
<hardenedapple@gmail.com> wrote:
>
> 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.
>
Architecture.disassemble doesn't work?
https://sourceware.org/gdb/current/onlinedocs/gdb/Architectures-In-Python.html
  
Matthew Malcomson Feb. 8, 2017, 5:46 p.m. UTC | #2
The problem is that without this patch I can't get an Architecture 
object without first having a Frame object, and I can't get a Frame 
object without starting my target program.

Adding a gdb.current_arch() function is in order to get the Architecture 
object I need without starting the target program.


On 08/02/17 17:15, Yao Qi wrote:
> On Mon, Feb 6, 2017 at 5:01 PM, Matthew Malcomson
> <hardenedapple@gmail.com> wrote:
>> 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.
>>
> Architecture.disassemble doesn't work?
> https://sourceware.org/gdb/current/onlinedocs/gdb/Architectures-In-Python.html
>
  
Yao Qi Feb. 8, 2017, 10:15 p.m. UTC | #3
On Wed, Feb 8, 2017 at 5:46 PM, Matthew Malcomson
<hardenedapple@gmail.com> wrote:
> The problem is that without this patch I can't get an Architecture object
> without first having a Frame object, and I can't get a Frame object without
> starting my target program.
>
> Adding a gdb.current_arch() function is in order to get the Architecture
> object I need without starting the target program.
>

IMO, "current" arch is unclear as an extension language interface.  Yes, we
do have function get_current_arch, but it is just a c function, we are free to
change it in the future.
It would be better to add function Inferior.architecture.  I don't have much
GDB python expertise, so I'd like to hear what other people think.
  
Matthew Malcomson Feb. 9, 2017, 10:45 a.m. UTC | #4
FWIW I've nothing against your suggestion (other than it's sometimes a 
pain to get one object just to get access to a method to obtain a 
different object), but thought I should mention that "current" arch is 
already part of an extension language interface.

https://sourceware.org/gdb/current/onlinedocs/gdb/Architectures-In-Guile.html


On 08/02/17 22:15, Yao Qi wrote:
> On Wed, Feb 8, 2017 at 5:46 PM, Matthew Malcomson
> <hardenedapple@gmail.com> wrote:
>> The problem is that without this patch I can't get an Architecture object
>> without first having a Frame object, and I can't get a Frame object without
>> starting my target program.
>>
>> Adding a gdb.current_arch() function is in order to get the Architecture
>> object I need without starting the target program.
>>
> IMO, "current" arch is unclear as an extension language interface.  Yes, we
> do have function get_current_arch, but it is just a c function, we are free to
> change it in the future.
> It would be better to add function Inferior.architecture.  I don't have much
> GDB python expertise, so I'd like to hear what other people think.
>
  

Patch

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\