aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/python.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2022-01-04 08:02:24 -0700
committerTom Tromey <tromey@adacore.com>2022-01-26 06:49:51 -0700
commit1da5d0e664e362857153af8682321a89ebafb7f6 (patch)
treeb57ed08360a0351d66e4114c1d9b0d2b4cf95683 /gdb/python/python.c
parent8a782bbf707e19d2da2159781789c4e4491dcdd1 (diff)
downloadgdb-1da5d0e664e362857153af8682321a89ebafb7f6.zip
gdb-1da5d0e664e362857153af8682321a89ebafb7f6.tar.gz
gdb-1da5d0e664e362857153af8682321a89ebafb7f6.tar.bz2
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.
Diffstat (limited to 'gdb/python/python.c')
-rw-r--r--gdb/python/python.c62
1 files changed, 38 insertions, 24 deletions
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 4dcda53..7ddc170 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -192,13 +192,12 @@ const struct extension_language_defn extension_language_python =
/* Architecture and language to be used in callbacks from
the Python interpreter. */
-struct gdbarch *python_gdbarch;
-const struct language_defn *python_language;
+struct gdbarch *gdbpy_enter::python_gdbarch;
gdbpy_enter::gdbpy_enter (struct gdbarch *gdbarch,
const struct language_defn *language)
: m_gdbarch (python_gdbarch),
- m_language (python_language)
+ m_language (language == nullptr ? nullptr : current_language)
{
/* We should not ever enter Python unless initialized. */
if (!gdb_python_initialized)
@@ -209,7 +208,8 @@ gdbpy_enter::gdbpy_enter (struct gdbarch *gdbarch,
m_state = PyGILState_Ensure ();
python_gdbarch = gdbarch;
- python_language = language;
+ if (language != nullptr)
+ set_language (language->la_language);
/* Save it and ensure ! PyErr_Occurred () afterwards. */
m_error.emplace ();
@@ -228,12 +228,27 @@ gdbpy_enter::~gdbpy_enter ()
m_error->restore ();
python_gdbarch = m_gdbarch;
- python_language = m_language;
+ if (m_language != nullptr)
+ set_language (m_language->la_language);
restore_active_ext_lang (m_previous_active);
PyGILState_Release (m_state);
}
+struct gdbarch *
+gdbpy_enter::get_gdbarch ()
+{
+ if (python_gdbarch != nullptr)
+ return python_gdbarch;
+ return get_current_arch ();
+}
+
+void
+gdbpy_enter::finalize ()
+{
+ python_gdbarch = target_gdbarch ();
+}
+
/* A helper class to save and restore the GIL, but without touching
the other globals that are handled by gdbpy_enter. */
@@ -318,7 +333,7 @@ python_interactive_command (const char *arg, int from_tty)
arg = skip_spaces (arg);
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
if (arg && *arg)
{
@@ -412,7 +427,7 @@ gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
if (cmd->body_list_1 != nullptr)
error (_("Invalid \"python\" block structure."));
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
std::string script = compute_python_string (cmd->body_list_0.get ());
ret = PyRun_SimpleString (script.c_str ());
@@ -425,7 +440,7 @@ gdbpy_eval_from_control_command (const struct extension_language_defn *extlang,
static void
python_command (const char *arg, int from_tty)
{
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
scoped_restore save_async = make_scoped_restore (&current_ui->async, 0);
@@ -556,7 +571,7 @@ gdbpy_parameter (PyObject *self, PyObject *args)
static PyObject *
gdbpy_target_charset (PyObject *self, PyObject *args)
{
- const char *cset = target_charset (python_gdbarch);
+ const char *cset = target_charset (gdbpy_enter::get_gdbarch ());
return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
}
@@ -566,7 +581,7 @@ gdbpy_target_charset (PyObject *self, PyObject *args)
static PyObject *
gdbpy_target_wide_charset (PyObject *self, PyObject *args)
{
- const char *cset = target_wide_charset (python_gdbarch);
+ const char *cset = target_wide_charset (gdbpy_enter::get_gdbarch ());
return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL);
}
@@ -867,7 +882,7 @@ gdbpy_decode_line (PyObject *self, PyObject *args)
}
if (arg != NULL)
- location = string_to_event_location_basic (&arg, python_language,
+ location = string_to_event_location_basic (&arg, current_language,
symbol_name_match_type::WILD);
std::vector<symtab_and_line> decoded_sals;
@@ -972,7 +987,7 @@ static void
gdbpy_source_script (const struct extension_language_defn *extlang,
FILE *file, const char *filename)
{
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
python_run_simple_file (file, filename);
}
@@ -1011,7 +1026,7 @@ struct gdbpy_event
void operator() ()
{
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
gdbpy_ref<> call_result (PyObject_CallObject (m_func, NULL));
if (call_result == NULL)
@@ -1060,7 +1075,7 @@ gdbpy_before_prompt_hook (const struct extension_language_defn *extlang,
if (!gdb_python_initialized)
return EXT_LANG_RC_NOP;
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
if (!evregpy_no_listeners_p (gdb_py_events.before_prompt)
&& evpy_emit_event (NULL, gdb_py_events.before_prompt) < 0)
@@ -1135,7 +1150,7 @@ gdbpy_colorize (const std::string &filename, const std::string &contents)
if (!gdb_python_initialized)
return {};
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
if (gdb_python_module == nullptr
|| !PyObject_HasAttrString (gdb_python_module, "colorize"))
@@ -1400,7 +1415,7 @@ gdbpy_source_objfile_script (const struct extension_language_defn *extlang,
if (!gdb_python_initialized)
return;
- gdbpy_enter enter_py (objfile->arch (), current_language);
+ gdbpy_enter enter_py (objfile->arch ());
scoped_restore restire_current_objfile
= make_scoped_restore (&gdbpy_current_objfile, objfile);
@@ -1421,7 +1436,7 @@ gdbpy_execute_objfile_script (const struct extension_language_defn *extlang,
if (!gdb_python_initialized)
return;
- gdbpy_enter enter_py (objfile->arch (), current_language);
+ gdbpy_enter enter_py (objfile->arch ());
scoped_restore restire_current_objfile
= make_scoped_restore (&gdbpy_current_objfile, objfile);
@@ -1453,7 +1468,7 @@ gdbpy_start_type_printers (const struct extension_language_defn *extlang,
if (!gdb_python_initialized)
return;
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types"));
if (type_module == NULL)
@@ -1498,7 +1513,7 @@ gdbpy_apply_type_printers (const struct extension_language_defn *extlang,
if (!gdb_python_initialized)
return EXT_LANG_RC_NOP;
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
gdbpy_ref<> type_obj (type_to_type_object (type));
if (type_obj == NULL)
@@ -1561,7 +1576,7 @@ gdbpy_free_type_printers (const struct extension_language_defn *extlang,
if (!gdb_python_initialized)
return;
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
Py_DECREF (printers);
}
@@ -1696,8 +1711,7 @@ finalize_python (void *ignore)
previous_active = set_active_ext_lang (&extension_language_python);
(void) PyGILState_Ensure ();
- python_gdbarch = target_gdbarch ();
- python_language = current_language;
+ gdbpy_enter::finalize ();
Py_Finalize ();
@@ -1756,7 +1770,7 @@ gdbpy_gdb_exiting (int exit_code)
if (!gdb_python_initialized)
return;
- gdbpy_enter enter_py (python_gdbarch, python_language);
+ gdbpy_enter enter_py;
if (emit_exiting_event (exit_code) < 0)
gdbpy_print_stack ();
@@ -2174,7 +2188,7 @@ gdbpy_initialize (const struct extension_language_defn *extlang)
if (!do_start_initialization () && PyErr_Occurred ())
gdbpy_print_stack ();
- gdbpy_enter enter_py (get_current_arch (), current_language);
+ gdbpy_enter enter_py;
if (!do_initialize (extlang))
{