From 1da5d0e664e362857153af8682321a89ebafb7f6 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Tue, 4 Jan 2022 08:02:24 -0700 Subject: Change how Python architecture and language are handled Currently, gdb's Python layer captures the current architecture and language when "entering" Python code. This has some undesirable effects, and so this series changes how this is handled. First, there is code like this: gdbpy_enter enter_py (python_gdbarch, python_language); This is incorrect, because both of these are NULL when not otherwise assigned. This can cause crashes in some cases -- I've added one to the test suite. (Note that this crasher is just an example, other ones along the same lines are possible.) Second, when the language is captured in this way, it means that Python code cannot affect the current language for its own purposes. It's reasonable to want to write code like this: gdb.execute('set language mumble') ... stuff using the current language gdb.execute('set language previous-value') However, this won't actually work, because the language is captured on entry. I've added a test to show this as well. This patch changes gdb to try to avoid capturing the current values. The Python concept of the current gdbarch is only set in those few cases where a non-default value is computed or needed; and the language is not captured at all -- instead, in the cases where it's required, the current language is temporarily changed. --- gdb/python/py-xmethods.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'gdb/python/py-xmethods.c') diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c index fd36730..9d58249 100644 --- a/gdb/python/py-xmethods.c +++ b/gdb/python/py-xmethods.c @@ -69,7 +69,7 @@ private: python_xmethod_worker::~python_xmethod_worker () { /* We don't do much here, but we still need the GIL. */ - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; Py_DECREF (m_py_worker); Py_DECREF (m_this_type); @@ -122,7 +122,7 @@ gdbpy_get_matching_xmethod_workers { gdb_assert (obj_type != NULL && method_name != NULL); - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; gdbpy_ref<> py_type (type_to_type_object (obj_type)); if (py_type == NULL) @@ -294,7 +294,7 @@ python_xmethod_worker::do_get_arg_types (std::vector *arg_types) { /* The gdbpy_enter object needs to be placed first, so that it's the last to be destroyed. */ - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; struct type *obj_type; int i = 1, arg_count; gdbpy_ref<> list_iter; @@ -410,7 +410,7 @@ python_xmethod_worker::do_get_result_type (value *obj, struct type *obj_type, *this_type; int i; - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; /* First see if there is a get_result_type method. If not this could be an old xmethod (pre 7.9.1). */ @@ -502,7 +502,7 @@ struct value * python_xmethod_worker::invoke (struct value *obj, gdb::array_view args) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; int i; struct type *obj_type, *this_type; @@ -580,7 +580,7 @@ python_xmethod_worker::invoke (struct value *obj, } else { - res = allocate_value (lookup_typename (python_language, + res = allocate_value (lookup_typename (current_language, "void", NULL, 0)); } -- cgit v1.1