public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Matthew Malcomson <hardenedapple@gmail.com>
To: gdb-patches@sourceware.org
Subject: [PATCH] Add gdb.current_arch() function to python module -- equivalent of guile (current-arch)
Date: Mon, 06 Feb 2017 17:02:00 -0000	[thread overview]
Message-ID: <ac00f967-3a7d-bd6a-88de-bcb98ff357ca@gmail.com> (raw)

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." },

             reply	other threads:[~2017-02-06 17:02 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-06 17:02 Matthew Malcomson [this message]
2017-02-08 17:15 ` Yao Qi
2017-02-08 17:46   ` Matthew Malcomson
2017-02-08 22:15     ` Yao Qi
2017-02-09 10:46       ` Matthew Malcomson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ac00f967-3a7d-bd6a-88de-bcb98ff357ca@gmail.com \
    --to=hardenedapple@gmail.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).