aboutsummaryrefslogtreecommitdiff
path: root/gdb
diff options
context:
space:
mode:
authorTom Tromey <tromey@redhat.com>2009-12-03 18:30:02 +0000
committerTom Tromey <tromey@redhat.com>2009-12-03 18:30:02 +0000
commit57a1d736959fb92e39abab59511a903b7ec59547 (patch)
tree5ec223f0d33bfe64a7583c5993325705b1fad1cc /gdb
parentfb169834478e0db3ffe865300c0cf4556160c7bd (diff)
downloadgdb-57a1d736959fb92e39abab59511a903b7ec59547.zip
gdb-57a1d736959fb92e39abab59511a903b7ec59547.tar.gz
gdb-57a1d736959fb92e39abab59511a903b7ec59547.tar.bz2
gdb
* python/python.c (gdbpy_parse_and_eval): New function. (GdbMethods): Add "parse_and_eval". gdb/testsuite * gdb.python/py-value.exp (test_parse_and_eval): New function. gdb/doc * gdb.texinfo (Basic Python): Document gdb.parse_and_eval.
Diffstat (limited to 'gdb')
-rw-r--r--gdb/ChangeLog5
-rw-r--r--gdb/doc/ChangeLog4
-rw-r--r--gdb/doc/gdb.texinfo13
-rw-r--r--gdb/python/python.c25
-rw-r--r--gdb/testsuite/ChangeLog5
-rw-r--r--gdb/testsuite/gdb.python/py-value.exp12
6 files changed, 64 insertions, 0 deletions
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
index a36834e..70da313 100644
--- a/gdb/ChangeLog
+++ b/gdb/ChangeLog
@@ -1,3 +1,8 @@
+2009-12-03 Tom Tromey <tromey@redhat.com>
+
+ * python/python.c (gdbpy_parse_and_eval): New function.
+ (GdbMethods): Add "parse_and_eval".
+
2009-12-03 Jan Kratochvil <jan.kratochvil@redhat.com>
* linux-thread-db.c (find_new_threads_once): Change errp and err types
diff --git a/gdb/doc/ChangeLog b/gdb/doc/ChangeLog
index 73a640d..553abae 100644
--- a/gdb/doc/ChangeLog
+++ b/gdb/doc/ChangeLog
@@ -1,3 +1,7 @@
+2009-12-03 Tom Tromey <tromey@redhat.com>
+
+ * gdb.texinfo (Basic Python): Document gdb.parse_and_eval.
+
2009-12-02 Paul Pluzhnikov <ppluzhnikov@google.com>
* observer.texi: New memory_changed observer.
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 39107ec..8cdab8f 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/ChangeLog b/gdb/testsuite/ChangeLog
index 9c95f3f..4ad760e 100644
--- a/gdb/testsuite/ChangeLog
+++ b/gdb/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2009-12-03 Tom Tromey <tromey@redhat.com>
+
+ * gdb.python/py-value.exp (test_parse_and_eval): New
+ function.
+
2009-12-03 Paul Pluzhnikov <ppluzhnikov@google.com>
PR gdb/11022
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.