aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gdb/NEWS4
-rw-r--r--gdb/doc/python.texi7
-rw-r--r--gdb/python/python.c28
-rw-r--r--gdb/testsuite/gdb.python/py-value.c3
-rw-r--r--gdb/testsuite/gdb.python/py-value.exp5
5 files changed, 40 insertions, 7 deletions
diff --git a/gdb/NEWS b/gdb/NEWS
index 3ea7237..f963ab3 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -195,6 +195,10 @@ info main
** New function gdb.execute_mi(COMMAND, [ARG]...), that invokes a
GDB/MI command and returns the output as a Python dictionary.
+ ** gdb.parse_and_eval now has a new "global_context" parameter.
+ This can be used to request that the parse only examine global
+ symbols.
+
*** Changes in GDB 13
* MI version 1 is deprecated, and will be removed in GDB 14.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index d1f9311..38f756e 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -419,11 +419,16 @@ using the @code{gdb.Value} constructor.
@end defun
@findex gdb.parse_and_eval
-@defun gdb.parse_and_eval (expression)
+@defun gdb.parse_and_eval (expression @r{[}, global_context@r{]})
Parse @var{expression}, which must be a string, as an expression in
the current language, evaluate it, and return the result as a
@code{gdb.Value}.
+@var{global_context}, if provided, is a boolean indicating whether the
+parsing should be done in the global context. The default is
+@samp{False}, meaning that the current frame or current static context
+should be used.
+
This function can be useful when implementing a new command
(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
as it provides a way to parse the
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9703d6e..517cea7 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -969,19 +969,34 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
/* Parse a string and evaluate it as an expression. */
static PyObject *
-gdbpy_parse_and_eval (PyObject *self, PyObject *args)
+gdbpy_parse_and_eval (PyObject *self, PyObject *args, PyObject *kw)
{
+ static const char *keywords[] = { "expression", "global_context", nullptr };
+
const char *expr_str;
+ PyObject *global_context_obj = nullptr;
- if (!PyArg_ParseTuple (args, "s", &expr_str))
- return NULL;
+ if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "s|O!", keywords,
+ &expr_str,
+ &PyBool_Type, &global_context_obj))
+ return nullptr;
+
+ parser_flags flags = 0;
+ if (global_context_obj != NULL)
+ {
+ int cmp = PyObject_IsTrue (global_context_obj);
+ if (cmp < 0)
+ return nullptr;
+ if (cmp)
+ flags |= PARSER_LEAVE_BLOCK_ALONE;
+ }
PyObject *result = nullptr;
try
{
gdbpy_allow_threads allow_threads;
scoped_value_mark free_values;
- struct value *val = parse_and_eval (expr_str);
+ struct value *val = parse_and_eval (expr_str, flags);
result = value_to_value_object (val);
}
catch (const gdb_exception &except)
@@ -2563,8 +2578,9 @@ The first element contains any unparsed portion of the String parameter\n\
(or None if the string was fully parsed). The second element contains\n\
a tuple that contains all the locations that match, represented as\n\
gdb.Symtab_and_line objects (or None)."},
- { "parse_and_eval", gdbpy_parse_and_eval, METH_VARARGS,
- "parse_and_eval (String) -> Value.\n\
+ { "parse_and_eval", (PyCFunction) gdbpy_parse_and_eval,
+ METH_VARARGS | METH_KEYWORDS,
+ "parse_and_eval (String, [Boolean]) -> Value.\n\
Parse String as an expression, evaluate it, and return the result as a Value."
},
diff --git a/gdb/testsuite/gdb.python/py-value.c b/gdb/testsuite/gdb.python/py-value.c
index 5a578de..f0b1f5f 100644
--- a/gdb/testsuite/gdb.python/py-value.c
+++ b/gdb/testsuite/gdb.python/py-value.c
@@ -79,6 +79,8 @@ int func2 (int arg1, int arg2)
char **save_argv;
+int shadowed = 23;
+
int
main (int argc, char *argv[])
{
@@ -96,6 +98,7 @@ main (int argc, char *argv[])
int i = 2;
int *ptr_i = &i;
struct str *xstr;
+ int shadowed = 97;
/* Prevent gcc from optimizing argv[] out. */
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 898208b..9fc2581 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -338,6 +338,11 @@ proc test_value_in_inferior {} {
gdb_py_test_silent_cmd "python str = '\"str\"'" "set up str variable" 1
gdb_test "python print (gdb.parse_and_eval (str).string (length = 10))" \
"gdb.error: Attempt to take address of value not located in memory.\r\nError while executing Python code."
+
+ gdb_test "python print (gdb.parse_and_eval ('shadowed'))" \
+ 97 "shadowed local value"
+ gdb_test "python print (gdb.parse_and_eval ('shadowed', global_context=True))" \
+ 23 "shadowed global value"
}
proc test_inferior_function_call {} {