From patchwork Mon Jun 9 21:16:20 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Smundak X-Patchwork-Id: 1407 Received: (qmail 9263 invoked by alias); 9 Jun 2014 21:16:24 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 9250 invoked by uid 89); 9 Jun 2014 21:16:23 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.7 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, RP_MATCHES_RCVD, SPF_PASS autolearn=ham version=3.3.2 X-HELO: mail-ob0-f170.google.com Received: from mail-ob0-f170.google.com (HELO mail-ob0-f170.google.com) (209.85.214.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES128-SHA encrypted) ESMTPS; Mon, 09 Jun 2014 21:16:22 +0000 Received: by mail-ob0-f170.google.com with SMTP id uz6so1744100obc.1 for ; Mon, 09 Jun 2014 14:16:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=ltDgea5Ec00Qanqj1C56EBsRBsh69sv1oSdKqgaimgQ=; b=I9wu4FFVMYmQsHs/D0JXnrnS7mVDWiwxGMijd8YTUb2zAHxFuN0Q/A7wNjfIHXXwuy /tLd2/BZW/HWSAJzmx8/6+RXcXt8dBqM2vyGGatWizJ/gVjFPyrRzuoSbTEdX1sDWEv0 6uS+dE/ka77Rs7DhDFqE8vNQMjqBf/9BZ9bxKaT2A+7IjVuxo1uuUkPTUr0hH7kG+eB4 x1eKnKgm8aShtWFPnbFaLXSqo62pq8Z86hz4gUqkNBYACpa/t0REBYEXYEx+Yqi6r0+g /ZwQ7uFnknnrFxBklzmW4Muqk1dfqEePhiiQlWsItlVtctrfuc1y50J8dBNV0qz10rTn A3rQ== X-Gm-Message-State: ALoCoQncAoywDSFSfx2/Z0L9Hy3UocAz9lvW4t7WFguh3vjUJBUIJxTDR0RWrUBylR5C334t4Wa8 MIME-Version: 1.0 X-Received: by 10.60.175.163 with SMTP id cb3mr28513588oec.29.1402348580545; Mon, 09 Jun 2014 14:16:20 -0700 (PDT) Received: by 10.182.144.133 with HTTP; Mon, 9 Jun 2014 14:16:20 -0700 (PDT) In-Reply-To: <83oay128ca.fsf@gnu.org> References: <83oay128ca.fsf@gnu.org> Date: Mon, 9 Jun 2014 14:16:20 -0700 Message-ID: Subject: Re: [PATCH] Add Frame.read_register to Python API From: Alexander Smundak To: Eli Zaretskii Cc: gdb-patches On Mon, Jun 9, 2014 at 12:25 PM, Eli Zaretskii wrote: > When 2 changes are in the same node, write them one after the other, > and have only one "(Node Name)" thing. Done. >> +* Python Scripting >> + Access frame registers > > This is an incomplete sentence. I guess you meant something like > You can now access frame registers from Python scripts. Done. >> +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. > ^^^^^^^^^^^^^ > These should be in @code instead of quotes. Added the code but retained the quotes, because it's string literal. >> +Returns @code{Gdb.Value} object. > ^ > "a" missing here. Done. Here it is again: 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 * python/py-frame.c (frapy_read_register): New function. 2014-06-09 Sasha Smundak * python.texi (Writing a Frame Filter): Fix example code. (Frames in Python): Fix reference in find_sal description. Add read_register description. 2014-06-09 Sasha Smundak * gdb.python/py-frame.exp: Test Frame.read_register. diff --git a/gdb/NEWS b/gdb/NEWS index 1397e8b..3db458b 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -3,6 +3,9 @@ *** Changes since GDB 7.7 +* Python Scripting + You can now access frame registers from Python scripts. + * New command line options -D data-directory diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi index 4688783..14564eb 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., @code{'rsp'} or @code{'r1'}), or a +register number. Returns a @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", ®num)) + { + const char *regnum_str; + PyErr_Clear(); /* Clear PyArg_ParseTuple failure above. */ + if (PyArg_ParseTuple (args, "s", ®num_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..6034fff 100644 --- a/gdb/testsuite/gdb.python/py-frame.exp +++ b/gdb/testsuite/gdb.python/py-frame.exp @@ -94,3 +94,16 @@ 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)"