aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/python-internal.h
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-internal.h
parent8a782bbf707e19d2da2159781789c4e4491dcdd1 (diff)
downloadbinutils-1da5d0e664e362857153af8682321a89ebafb7f6.zip
binutils-1da5d0e664e362857153af8682321a89ebafb7f6.tar.gz
binutils-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-internal.h')
-rw-r--r--gdb/python/python-internal.h27
1 files changed, 23 insertions, 4 deletions
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 583989c..ccea5c4 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -625,14 +625,36 @@ class gdbpy_enter
{
public:
- gdbpy_enter (struct gdbarch *gdbarch, const struct language_defn *language);
+ /* Set the ambient Python architecture to GDBARCH and the language
+ to LANGUAGE. If GDBARCH is nullptr, then the architecture will
+ be computed, when needed, using get_current_arch; see the
+ get_gdbarch method. If LANGUAGE is not nullptr, then the current
+ language at time of construction will be saved (to be restored on
+ destruction), and the current language will be set to
+ LANGUAGE. */
+ explicit gdbpy_enter (struct gdbarch *gdbarch = nullptr,
+ const struct language_defn *language = nullptr);
~gdbpy_enter ();
DISABLE_COPY_AND_ASSIGN (gdbpy_enter);
+ /* Return the current gdbarch, as known to the Python layer. This
+ is either python_gdbarch (which comes from the most recent call
+ to the gdbpy_enter constructor), or, if that is nullptr, the
+ result of get_current_arch. */
+ static struct gdbarch *get_gdbarch ();
+
+ /* Called only during gdb shutdown. This sets python_gdbarch to an
+ acceptable value. */
+ static void finalize ();
+
private:
+ /* The current gdbarch, according to Python. This can be
+ nullptr. */
+ static struct gdbarch *python_gdbarch;
+
struct active_ext_lang_state *m_previous_active;
PyGILState_STATE m_state;
struct gdbarch *m_gdbarch;
@@ -680,9 +702,6 @@ private:
PyThreadState *m_save;
};
-extern struct gdbarch *python_gdbarch;
-extern const struct language_defn *python_language;
-
/* Use this after a TRY_EXCEPT to throw the appropriate Python
exception. */
#define GDB_PY_HANDLE_EXCEPTION(Exception) \