public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] Add Frame.read_register to Python API
@ 2014-06-09 19:15 Alexander Smundak
  2014-06-09 19:26 ` Eli Zaretskii
  0 siblings, 1 reply; 20+ messages in thread
From: Alexander Smundak @ 2014-06-09 19:15 UTC (permalink / raw)
  To: gdb-patches

The ability to read registers is needed to use Frame Filter API to
display the frames created by JIT compilers.

gdb/Changelog
2014-06-09  Sasha Smundak  <asmundak@google.com>

* python/py-frame.c (frapy_read_register): New function.

2014-06-09  Sasha Smundak  <asmundak@google.com>

* python.texi (Writing a Frame Filter): Fix example code.
(Frames in Python): Add read_register description.
(Frames in Python): Fix reference in find_sal description.

2014-06-09  Sasha Smundak  <asmundak@google.com>

* gdb.python/py-frame.exp: Test Frame.read_register.

diff --git a/gdb/NEWS b/gdb/NEWS
index 1397e8b..b3b7a22 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -3,6 +3,9 @@

 *** Changes since GDB 7.7

+* Python Scripting
+  Access frame registers
+
 * New command line options

 -D data-directory
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 4688783..a7ff162 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -2046,9 +2046,10 @@ class InlinedFrameDecorator(FrameDecorator):

     def __init__(self, fobj):
         super(InlinedFrameDecorator, self).__init__(fobj)
+        self.fobj = fobj

     def function(self):
-        frame = fobj.inferior_frame()
+        frame = self.fobj.inferior_frame()
         name = str(frame.name())

         if frame.type() == gdb.INLINE_FRAME:
@@ -3585,10 +3586,16 @@ Return the frame called by this frame.
 @end defun

 @defun Frame.find_sal ()
-Return the frame's symtab and line object.
+Return the frame's @code{gdb.Symtab_and_line} object.
 @xref{Symbol Tables In Python}.
 @end defun

+@defun Frame.read_register (register)
+Return the value of @var{register} in this frame. The @var{register}
+argument must be a string (e.g., 'rsp' or 'r1'), or a register number.
+Returns @code{Gdb.Value} object.
+@end defun
+
 @defun Frame.read_var (variable @r{[}, block@r{]})
 Return the value of @var{variable} in this frame.  If the optional
 argument @var{block} is provided, search for the variable from that
diff --git a/gdb/python/py-frame.c b/gdb/python/py-frame.c
index 77077d3..4375541 100644
--- a/gdb/python/py-frame.c
+++ b/gdb/python/py-frame.c
@@ -28,6 +28,7 @@
 #include "python-internal.h"
 #include "symfile.h"
 #include "objfiles.h"
+#include "user-regs.h"

 typedef struct {
   PyObject_HEAD
@@ -235,6 +236,38 @@ frapy_pc (PyObject *self, PyObject *args)
   return gdb_py_long_from_ulongest (pc);
 }

+/* Implementation of gdb.Frame.read_register (self, register) -> gdb.Value.
+   Returns the value of a register in this frame.  */
+static PyObject *
+frapy_read_register (PyObject *self, PyObject *args)
+{
+  struct frame_info *frame;
+  volatile struct gdb_exception except;
+  int regnum = -1;
+  struct value *val = NULL;
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      FRAPY_REQUIRE_VALID (self, frame);
+      if (!PyArg_ParseTuple (args, "i", &regnum))
+ {
+  const char *regnum_str;
+  PyErr_Clear();  /* Clear PyArg_ParseTuple failure above.  */
+  if (PyArg_ParseTuple (args, "s", &regnum_str))
+    {
+      regnum = user_reg_map_name_to_regnum (get_frame_arch (frame),
+    regnum_str,
+    strlen (regnum_str));
+    }
+ }
+      if (regnum >= 0)
+ {
+  val = value_of_register (regnum, frame);
+ }
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+  return val ? value_to_value_object (val) : NULL;
+}
+
 /* Implementation of gdb.Frame.block (self) -> gdb.Block.
    Returns the frame's code block.  */

@@ -674,6 +707,9 @@ Return the reason why it's not possible to find
frames older than this." },
   { "pc", frapy_pc, METH_NOARGS,
     "pc () -> Long.\n\
 Return the frame's resume address." },
+  { "read_register", frapy_read_register, METH_VARARGS,
+    "read_register (register) -> gdb.Value\n\
+Return the value of the register in the frame." },
   { "block", frapy_block, METH_NOARGS,
     "block () -> gdb.Block.\n\
 Return the frame's code block." },
diff --git a/gdb/testsuite/gdb.python/py-frame.exp
b/gdb/testsuite/gdb.python/py-frame.exp
index 3517824..9c56d34 100644
--- a/gdb/testsuite/gdb.python/py-frame.exp
+++ b/gdb/testsuite/gdb.python/py-frame.exp
@@ -94,3 +94,10 @@ gdb_test "python print ('result = %s' % f0.read_var
('variable_which_surely_does
 gdb_test "python print ('result = %s' % f0.read_var ('a'))" " = 1"
"test Frame.read_var - success"

 gdb_test "python print ('result = %s' % (gdb.selected_frame () ==
f1))" " = True" "test gdb.selected_frame"
+
+# Can read SP register
+gdb_test "python print ('result = %s' % f0.read_register('sp'))" " =
0x\[0-9a-fA-F\]+" "test Frame.read_register(fp)"
+# PC value obtained via read_register is as expected
+gdb_test "python print ('result = %s' %
(f0.read_register('pc').cast(gdb.lookup_type('unsigned long')) ==
f0.pc()))" "True" "test Frame.read_register(pc)"
+# On x86-64, PC is register 16.
+gdb_test "python print ('result = %s' % ((f0.architecture().name() !=
'i386:x86-64') or f0.read_register('pc') == f0.read_register(16)))"
"True" "test Frame.read_register(regnum)"

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2014-09-03 23:46 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-06-09 19:15 [PATCH] Add Frame.read_register to Python API Alexander Smundak
2014-06-09 19:26 ` Eli Zaretskii
2014-06-09 21:16   ` Alexander Smundak
2014-06-11 19:11     ` Tom Tromey
2014-06-11 23:52       ` Alexander Smundak
2014-06-18 17:18         ` Alexander Smundak
2014-06-18 17:33           ` Eli Zaretskii
2014-06-23 22:46           ` Alexander Smundak
2014-06-30 16:22             ` Alexander Smundak
2014-07-07 20:59               ` Alexander Smundak
2014-07-14 16:24                 ` Alexander Smundak
2014-07-24 17:45                   ` Doug Evans
2014-07-31 18:53                     ` Doug Evans
2014-07-31 20:05                       ` Tom Tromey
2014-08-21 18:44         ` Doug Evans
2014-08-26 20:31           ` Alexander Smundak
2014-08-29 13:39             ` Doug Evans
2014-08-29 23:32               ` Sasha Smundak
2014-08-29 23:36                 ` Doug Evans
2014-09-03 23:46                   ` Doug Evans

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).