From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 31778 invoked by alias); 2 Dec 2009 21:57:52 -0000 Received: (qmail 31769 invoked by uid 22791); 2 Dec 2009 21:57:51 -0000 X-SWARE-Spam-Status: No, hits=-2.5 required=5.0 tests=AWL,BAYES_00,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 02 Dec 2009 21:57:47 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id nB2Lvh3Y000413 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 2 Dec 2009 16:57:43 -0500 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id nB2LvhFS025131; Wed, 2 Dec 2009 16:57:43 -0500 Received: from opsy.redhat.com (ovpn01.gateway.prod.ext.phx2.redhat.com [10.5.9.1]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id nB2Lvg2L021245; Wed, 2 Dec 2009 16:57:42 -0500 Received: by opsy.redhat.com (Postfix, from userid 500) id 97398378B83; Wed, 2 Dec 2009 14:57:41 -0700 (MST) From: Tom Tromey To: Eli Zaretskii Cc: gdb-patches@sourceware.org Subject: Re: Patch: add gdb.parse_and_eval References: Reply-To: Tom Tromey Date: Wed, 02 Dec 2009 21:57:00 -0000 In-Reply-To: (Eli Zaretskii's message of "Thu, 11 Jun 2009 18:40:01 -0400") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org X-SW-Source: 2009-12/txt/msg00022.txt.bz2 >>>>> "Eli" == Eli Zaretskii writes: >> +@findex gdb.parse_and_eval >> +@defun parse_and_eval expression >> +Parse @var{expression} as an expression in the current language, >> +evaluate it, and return the result as a @code{gdb.Value}. >> +@var{expression} must be a string. >> +@end defun Eli> Thanks. This is okay, but given the recent discussions which Eli> indicated that this feature is necessary for a broad class of scripts, Eli> I would expect a bit more text in the manual about it, describing when Eli> and how this would be useful. How about this? Tom 2009-12-02 Tom Tromey * python/python.c (gdbpy_parse_and_eval): New function. (GdbMethods): Add "parse_and_eval". 2009-12-02 Tom Tromey * gdb.texinfo (Basic Python): Document gdb.parse_and_eval. 2009-12-02 Tom Tromey * gdb.python/py-value.exp (test_parse_and_eval): New function. diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 74cbedf..7e51690 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -19328,6 +19328,19 @@ If no exception is raised, the return value is always an instance of @code{gdb.Value} (@pxref{Values From Inferior}). @end defun +@findex gdb.parse_and_eval +@defun parse_and_eval expression +Parse @var{expression} as an expression in the current language, +evaluate it, and return the result as a @code{gdb.Value}. +@var{expression} must be a string. + +This function can be useful when implementing a new command +(@pxref{Commands In Python}), as it provides a way to parse the +command's argument as an expression. It is also useful simply to +compute values, for example, it is the only way to get the value of a +convenience variable (@pxref{Convenience Vars}) as a @code{gdb.Value}. +@end defun + @findex gdb.write @defun write string Print a string to @value{GDBN}'s paginated standard output stream. diff --git a/gdb/python/python.c b/gdb/python/python.c index 77a0069..23e94a5 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -323,6 +323,26 @@ execute_gdb_command (PyObject *self, PyObject *args) Py_RETURN_NONE; } +/* Parse a string and evaluate it as an expression. */ +static PyObject * +gdbpy_parse_and_eval (PyObject *self, PyObject *args) +{ + char *expr_str; + struct value *result = NULL; + volatile struct gdb_exception except; + + if (!PyArg_ParseTuple (args, "s", &expr_str)) + return NULL; + + TRY_CATCH (except, RETURN_MASK_ALL) + { + result = parse_and_eval (expr_str); + } + GDB_PY_HANDLE_EXCEPTION (except); + + return value_to_value_object (result); +} + /* Printing. */ @@ -680,6 +700,11 @@ Return a string explaining unwind stop reason." }, "lookup_type (name [, block]) -> type\n\ Return a Type corresponding to the given name." }, + { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS, + "parse_and_eval (String) -> Value.\n\ +Parse String as an expression, evaluate it, and return the result as a Value." + }, + { "write", gdbpy_write, METH_VARARGS, "Write a string using gdb's filtered stream." }, { "flush", gdbpy_flush, METH_NOARGS, diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp index 2958233..01f6023 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -361,6 +361,17 @@ proc test_subscript_regression {lang} { gdb_test "python print marray\[1\]\[2\]" "o." "Test multiple subscript" } +# A few tests of gdb.parse_and_eval. +proc test_parse_and_eval {} { + gdb_test "python print gdb.parse_and_eval ('23')" "23" \ + "parse_and_eval constant test" + gdb_test "python print gdb.parse_and_eval ('5 + 7')" "12" \ + "parse_and_eval simple expression test" + gdb_test "python print type(gdb.parse_and_eval ('5 + 7'))" \ + ".type 'gdb.Value'."\ + "parse_and_eval type test" +} + # Start with a fresh gdb. gdb_exit @@ -381,6 +392,7 @@ test_value_numeric_ops test_value_boolean test_value_compare test_objfiles +test_parse_and_eval # The following tests require execution.