From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id D9CDA385843E; Wed, 26 Jan 2022 14:06:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D9CDA385843E Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Change how Python architecture and language are handled X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: 8a782bbf707e19d2da2159781789c4e4491dcdd1 X-Git-Newrev: 1da5d0e664e362857153af8682321a89ebafb7f6 Message-Id: <20220126140657.D9CDA385843E@sourceware.org> Date: Wed, 26 Jan 2022 14:06:57 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Jan 2022 14:06:58 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3D1da5d0e664e3= 62857153af8682321a89ebafb7f6 commit 1da5d0e664e362857153af8682321a89ebafb7f6 Author: Tom Tromey Date: Tue Jan 4 08:02:24 2022 -0700 Change how Python architecture and language are handled =20 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. =20 First, there is code like this: =20 gdbpy_enter enter_py (python_gdbarch, python_language); =20 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.) =20 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: =20 gdb.execute('set language mumble') ... stuff using the current language gdb.execute('set language previous-value') =20 However, this won't actually work, because the language is captured on entry. I've added a test to show this as well. =20 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. Diff: --- gdb/python/py-breakpoint.c | 19 +++------ gdb/python/py-cmd.c | 8 ++-- gdb/python/py-connection.c | 2 +- gdb/python/py-finishbreakpoint.c | 6 +-- gdb/python/py-framefilter.c | 12 +++--- gdb/python/py-inferior.c | 27 ++++++------- gdb/python/py-membuf.c | 3 +- gdb/python/py-objfile.c | 2 +- gdb/python/py-param.c | 4 +- gdb/python/py-progspace.c | 2 +- gdb/python/py-tui.c | 18 ++++----- gdb/python/py-type.c | 4 +- gdb/python/py-unwind.c | 2 +- gdb/python/py-utils.c | 10 +++-- gdb/python/py-value.c | 21 +++++----- gdb/python/py-xmethods.c | 12 +++--- gdb/python/python-internal.h | 27 +++++++++++-- gdb/python/python.c | 62 ++++++++++++++++++-------= ---- gdb/testsuite/gdb.python/py-inferior.exp | 2 + gdb/testsuite/gdb.python/py-lookup-type.exp | 8 ++++ 20 files changed, 146 insertions(+), 105 deletions(-) diff --git a/gdb/python/py-breakpoint.c b/gdb/python/py-breakpoint.c index f00bd6f78e6..2fc20376af8 100644 --- a/gdb/python/py-breakpoint.c +++ b/gdb/python/py-breakpoint.c @@ -858,7 +858,7 @@ bppy_init (PyObject *self, PyObject *args, PyObject *kw= args) const struct breakpoint_ops *ops =3D breakpoint_ops_for_event_location (location.get (), false); =20 - create_breakpoint (python_gdbarch, + create_breakpoint (gdbpy_enter::get_gdbarch (), location.get (), NULL, -1, NULL, false, 0, temporary_bp, type, @@ -954,15 +954,13 @@ gdbpy_breakpoint_cond_says_stop (const struct extensi= on_language_defn *extlang, int stop; struct gdbpy_breakpoint_object *bp_obj =3D b->py_bp_object; PyObject *py_bp =3D (PyObject *) bp_obj; - struct gdbarch *garch; =20 if (bp_obj =3D=3D NULL) return EXT_LANG_BP_STOP_UNSET; =20 stop =3D -1; - garch =3D b->gdbarch ? b->gdbarch : get_current_arch (); =20 - gdbpy_enter enter_py (garch, current_language); + gdbpy_enter enter_py (b->gdbarch); =20 if (bp_obj->is_finish_bp) bpfinishpy_pre_stop_hook (bp_obj); @@ -1005,15 +1003,13 @@ gdbpy_breakpoint_has_cond (const struct extension_l= anguage_defn *extlang, struct breakpoint *b) { PyObject *py_bp; - struct gdbarch *garch; =20 if (b->py_bp_object =3D=3D NULL) return 0; =20 py_bp =3D (PyObject *) b->py_bp_object; - garch =3D b->gdbarch ? b->gdbarch : get_current_arch (); =20 - gdbpy_enter enter_py (garch, current_language); + gdbpy_enter enter_py (b->gdbarch); return PyObject_HasAttrString (py_bp, stop_func); } =20 @@ -1048,8 +1044,7 @@ gdbpy_breakpoint_created (struct breakpoint *bp) return; } =20 - struct gdbarch *garch =3D bp->gdbarch ? bp->gdbarch : get_current_arch (= ); - gdbpy_enter enter_py (garch, current_language); + gdbpy_enter enter_py (bp->gdbarch); =20 if (bppy_pending_object) { @@ -1099,8 +1094,7 @@ gdbpy_breakpoint_deleted (struct breakpoint *b) bp =3D get_breakpoint (num); if (bp) { - struct gdbarch *garch =3D bp->gdbarch ? bp->gdbarch : get_current_ar= ch (); - gdbpy_enter enter_py (garch, current_language); + gdbpy_enter enter_py (b->gdbarch); =20 gdbpy_ref bp_obj (bp->py_bp_object); if (bp_obj !=3D NULL) @@ -1131,8 +1125,7 @@ gdbpy_breakpoint_modified (struct breakpoint *b) bp =3D get_breakpoint (num); if (bp) { - struct gdbarch *garch =3D bp->gdbarch ? bp->gdbarch : get_current_ar= ch (); - gdbpy_enter enter_py (garch, current_language); + gdbpy_enter enter_py (b->gdbarch); =20 PyObject *bp_obj =3D (PyObject *) bp->py_bp_object; if (bp_obj) diff --git a/gdb/python/py-cmd.c b/gdb/python/py-cmd.c index 3a4e6490cf0..b51b05c95ad 100644 --- a/gdb/python/py-cmd.c +++ b/gdb/python/py-cmd.c @@ -90,7 +90,7 @@ cmdpy_dont_repeat (PyObject *self, PyObject *args) static void cmdpy_destroyer (struct cmd_list_element *self, void *context) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 /* Release our hold on the command object. */ gdbpy_ref cmd ((cmdpy_object *) context); @@ -104,7 +104,7 @@ cmdpy_function (const char *args, int from_tty, cmd_lis= t_element *command) { cmdpy_object *obj =3D (cmdpy_object *) command->context (); =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (! obj) error (_("Invalid invocation of Python command object.")); @@ -223,7 +223,7 @@ cmdpy_completer_handle_brkchars (struct cmd_list_elemen= t *command, completion_tracker &tracker, const char *text, const char *word) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 /* Calling our helper to obtain a reference to the PyObject of the Python function. */ @@ -266,7 +266,7 @@ cmdpy_completer (struct cmd_list_element *command, completion_tracker &tracker, const char *text, const char *word) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 /* Calling our helper to obtain a reference to the PyObject of the Python function. */ diff --git a/gdb/python/py-connection.c b/gdb/python/py-connection.c index 7c33a1f93d4..4cdd6abbf3d 100644 --- a/gdb/python/py-connection.c +++ b/gdb/python/py-connection.c @@ -161,7 +161,7 @@ connpy_connection_removed (process_stratum_target *targ= et) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (!evregpy_no_listeners_p (gdb_py_events.connection_removed)) if (emit_connection_event (target, gdb_py_events.connection_removed) <= 0) diff --git a/gdb/python/py-finishbreakpoint.c b/gdb/python/py-finishbreakpo= int.c index 8d5eb42efc0..03bd4934506 100644 --- a/gdb/python/py-finishbreakpoint.c +++ b/gdb/python/py-finishbreakpoint.c @@ -293,7 +293,7 @@ bpfinishpy_init (PyObject *self, PyObject *args, PyObje= ct *kwargs) /* Set a breakpoint on the return address. */ event_location_up location =3D new_address_location (get_frame_pc (prev_frame), NULL, 0); - create_breakpoint (python_gdbarch, + create_breakpoint (gdbpy_enter::get_gdbarch (), location.get (), NULL, thread, NULL, false, 0, 1 /*temp_flag*/, @@ -380,7 +380,7 @@ bpfinishpy_detect_out_scope_cb (struct breakpoint *b, static void bpfinishpy_handle_stop (struct bpstat *bs, int print_frame) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 for (breakpoint *bp : all_breakpoints_safe ()) bpfinishpy_detect_out_scope_cb (bp, bs =3D=3D NULL ? NULL : bs->breakp= oint_at); @@ -392,7 +392,7 @@ bpfinishpy_handle_stop (struct bpstat *bs, int print_fr= ame) static void bpfinishpy_handle_exit (struct inferior *inf) { - gdbpy_enter enter_py (target_gdbarch (), current_language); + gdbpy_enter enter_py (target_gdbarch ()); =20 for (breakpoint *bp : all_breakpoints_safe ()) bpfinishpy_detect_out_scope_cb (bp, nullptr); diff --git a/gdb/python/py-framefilter.c b/gdb/python/py-framefilter.c index 936354f354c..7870374b3ec 100644 --- a/gdb/python/py-framefilter.c +++ b/gdb/python/py-framefilter.c @@ -74,11 +74,11 @@ extract_sym (PyObject *obj, gdb::unique_xmalloc_ptr *name, if (*name =3D=3D NULL) return EXT_LANG_BT_ERROR; /* If the API returns a string (and not a symbol), then there is - no symbol derived language available and the frame filter has - either overridden the symbol with a string, or supplied a - entirely synthetic symbol/value pairing. In that case, use - python_language. */ - *language =3D python_language; + no symbol derived language available and the frame filter has + either overridden the symbol with a string, or supplied a + entirely synthetic symbol/value pairing. In that case, use + the current language. */ + *language =3D current_language; *sym =3D NULL; *sym_block =3D NULL; } @@ -1157,7 +1157,7 @@ gdbpy_apply_frame_filter (const struct extension_lang= uage_defn *extlang, return EXT_LANG_BT_NO_FILTERS; } =20 - gdbpy_enter enter_py (gdbarch, current_language); + gdbpy_enter enter_py (gdbarch); =20 /* When we're limiting the number of frames, be careful to request one extra frame, so that we can print a message if there are more diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index 45e33f9e28a..46f0c9ffd04 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -86,7 +86,7 @@ python_on_normal_stop (struct bpstat *bs, int print_frame) =20 stop_signal =3D inferior_thread ()->stop_signal (); =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (emit_stop_event (bs, stop_signal) < 0) gdbpy_print_stack (); @@ -98,7 +98,7 @@ python_on_resume (ptid_t ptid) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (target_gdbarch (), current_language); + gdbpy_enter enter_py (target_gdbarch ()); =20 if (emit_continue_event (ptid) < 0) gdbpy_print_stack (); @@ -110,7 +110,7 @@ python_on_resume (ptid_t ptid) static void python_on_inferior_call_pre (ptid_t thread, CORE_ADDR address) { - gdbpy_enter enter_py (target_gdbarch (), current_language); + gdbpy_enter enter_py (target_gdbarch ()); =20 if (emit_inferior_call_event (INFERIOR_CALL_PRE, thread, address) < 0) gdbpy_print_stack (); @@ -122,7 +122,7 @@ python_on_inferior_call_pre (ptid_t thread, CORE_ADDR a= ddress) static void python_on_inferior_call_post (ptid_t thread, CORE_ADDR address) { - gdbpy_enter enter_py (target_gdbarch (), current_language); + gdbpy_enter enter_py (target_gdbarch ()); =20 if (emit_inferior_call_event (INFERIOR_CALL_POST, thread, address) < 0) gdbpy_print_stack (); @@ -135,7 +135,7 @@ python_on_inferior_call_post (ptid_t thread, CORE_ADDR = address) static void python_on_memory_change (struct inferior *inferior, CORE_ADDR addr, ssize_= t len, const bfd_byte *data) { - gdbpy_enter enter_py (target_gdbarch (), current_language); + gdbpy_enter enter_py (target_gdbarch ()); =20 if (emit_memory_changed_event (addr, len) < 0) gdbpy_print_stack (); @@ -148,7 +148,7 @@ python_on_memory_change (struct inferior *inferior, COR= E_ADDR addr, ssize_t len, static void python_on_register_change (struct frame_info *frame, int regnum) { - gdbpy_enter enter_py (target_gdbarch (), current_language); + gdbpy_enter enter_py (target_gdbarch ()); =20 if (emit_register_changed_event (frame, regnum) < 0) gdbpy_print_stack (); @@ -162,7 +162,7 @@ python_inferior_exit (struct inferior *inf) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (target_gdbarch (), current_language); + gdbpy_enter enter_py (target_gdbarch ()); =20 if (inf->has_exit_code) exit_code =3D &inf->exit_code; @@ -183,8 +183,7 @@ python_new_objfile (struct objfile *objfile) =20 gdbpy_enter enter_py (objfile !=3D NULL ? objfile->arch () - : target_gdbarch (), - current_language); + : target_gdbarch ()); =20 if (objfile =3D=3D NULL) { @@ -237,7 +236,7 @@ python_new_inferior (struct inferior *inf) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (python_gdbarch, python_language); + gdbpy_enter enter_py; =20 if (evregpy_no_listeners_p (gdb_py_events.new_inferior)) return; @@ -265,7 +264,7 @@ python_inferior_deleted (struct inferior *inf) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (python_gdbarch, python_language); + gdbpy_enter enter_py; =20 if (evregpy_no_listeners_p (gdb_py_events.inferior_deleted)) return; @@ -312,7 +311,7 @@ add_thread_object (struct thread_info *tp) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (python_gdbarch, python_language); + gdbpy_enter enter_py; =20 gdbpy_ref thread_obj =3D create_thread_object (tp); if (thread_obj =3D=3D NULL) @@ -348,7 +347,7 @@ delete_thread_object (struct thread_info *tp, int ignor= e) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (python_gdbarch, python_language); + gdbpy_enter enter_py; =20 gdbpy_ref inf_obj =3D inferior_to_inferior_object (tp->= inf); if (inf_obj =3D=3D NULL) @@ -792,7 +791,7 @@ py_free_inferior (struct inferior *inf, void *datum) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (python_gdbarch, python_language); + gdbpy_enter enter_py; gdbpy_ref inf_obj ((inferior_object *) datum); =20 inf_obj->inferior =3D NULL; diff --git a/gdb/python/py-membuf.c b/gdb/python/py-membuf.c index e789a78e649..74cc0fb46bd 100644 --- a/gdb/python/py-membuf.c +++ b/gdb/python/py-membuf.c @@ -83,7 +83,8 @@ mbpy_str (PyObject *self) =20 return PyString_FromFormat (_("Memory buffer for address %s, \ which is %s bytes long."), - paddress (python_gdbarch, membuf_obj->addr), + paddress (gdbpy_enter::get_gdbarch (), + membuf_obj->addr), pulongest (membuf_obj->length)); } =20 diff --git a/gdb/python/py-objfile.c b/gdb/python/py-objfile.c index 38052245bde..6055a42260b 100644 --- a/gdb/python/py-objfile.c +++ b/gdb/python/py-objfile.c @@ -661,7 +661,7 @@ gdbpy_lookup_objfile (PyObject *self, PyObject *args, P= yObject *kw) static void py_free_objfile (struct objfile *objfile, void *datum) { - gdbpy_enter enter_py (objfile->arch (), current_language); + gdbpy_enter enter_py (objfile->arch ()); gdbpy_ref object ((objfile_object *) datum); object->objfile =3D NULL; } diff --git a/gdb/python/py-param.c b/gdb/python/py-param.c index a25dad315c9..24678bc8a5e 100644 --- a/gdb/python/py-param.c +++ b/gdb/python/py-param.c @@ -396,7 +396,7 @@ get_set_value (const char *args, int from_tty, PyObject *obj =3D (PyObject *) c->context (); gdb::unique_xmalloc_ptr set_doc_string; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; gdbpy_ref<> set_doc_func (PyString_FromString ("get_set_string")); =20 if (set_doc_func =3D=3D NULL) @@ -431,7 +431,7 @@ get_show_value (struct ui_file *file, int from_tty, PyObject *obj =3D (PyObject *) c->context (); gdb::unique_xmalloc_ptr show_doc_string; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; gdbpy_ref<> show_doc_func (PyString_FromString ("get_show_string")); =20 if (show_doc_func =3D=3D NULL) diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 8131b66d24b..b27ebf4a666 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -472,7 +472,7 @@ py_free_pspace (struct program_space *pspace, void *dat= um) being deleted. */ struct gdbarch *arch =3D target_gdbarch (); =20 - gdbpy_enter enter_py (arch, current_language); + gdbpy_enter enter_py (arch); gdbpy_ref object ((pspace_object *) datum); object->pspace =3D NULL; } diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c index 97fdfe2be96..6a92251a705 100644 --- a/gdb/python/py-tui.c +++ b/gdb/python/py-tui.c @@ -155,7 +155,7 @@ gdbpy_tui_window::is_valid () const =20 tui_py_window::~tui_py_window () { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 /* This can be null if the user-provided Python construction function failed. */ @@ -181,7 +181,7 @@ tui_py_window::rerender () { tui_win_info::rerender (); =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 int h =3D viewport_height (); int w =3D viewport_width (); @@ -206,7 +206,7 @@ tui_py_window::rerender () void tui_py_window::do_scroll_horizontal (int num_to_scroll) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (PyObject_HasAttrString (m_window.get (), "hscroll")) { @@ -220,7 +220,7 @@ tui_py_window::do_scroll_horizontal (int num_to_scroll) void tui_py_window::do_scroll_vertical (int num_to_scroll) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (PyObject_HasAttrString (m_window.get (), "vscroll")) { @@ -234,7 +234,7 @@ tui_py_window::do_scroll_vertical (int num_to_scroll) void tui_py_window::click (int mouse_x, int mouse_y, int mouse_button) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (PyObject_HasAttrString (m_window.get (), "click")) { @@ -285,7 +285,7 @@ public: =20 gdbpy_tui_window_maker (const gdbpy_tui_window_maker &other) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; m_constr =3D other.m_constr; } =20 @@ -297,7 +297,7 @@ public: =20 gdbpy_tui_window_maker &operator=3D (const gdbpy_tui_window_maker &other) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; m_constr =3D other.m_constr; return *this; } @@ -312,14 +312,14 @@ private: =20 gdbpy_tui_window_maker::~gdbpy_tui_window_maker () { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; m_constr.reset (nullptr); } =20 tui_win_info * gdbpy_tui_window_maker::operator() (const char *win_name) { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 gdbpy_ref wrapper (PyObject_New (gdbpy_tui_window, &gdbpy_tui_window_object_type)); diff --git a/gdb/python/py-type.c b/gdb/python/py-type.c index 220b0d46ffb..e71e635ecdf 100644 --- a/gdb/python/py-type.c +++ b/gdb/python/py-type.c @@ -799,7 +799,7 @@ typy_lookup_typename (const char *type_name, const stru= ct block *block) else if (startswith (type_name, "enum ")) type =3D lookup_enum (type_name + 5, NULL); else - type =3D lookup_typename (python_language, + type =3D lookup_typename (current_language, type_name, block, 0); } catch (const gdb_exception &except) @@ -1089,7 +1089,7 @@ save_objfile_types (struct objfile *objfile, void *da= tum) =20 /* This prevents another thread from freeing the objects we're operating on. */ - gdbpy_enter enter_py (objfile->arch (), current_language); + gdbpy_enter enter_py (objfile->arch ()); =20 htab_up copied_types =3D create_copied_types_hash (objfile); =20 diff --git a/gdb/python/py-unwind.c b/gdb/python/py-unwind.c index 4fea9068aad..36f42cbcac3 100644 --- a/gdb/python/py-unwind.c +++ b/gdb/python/py-unwind.c @@ -524,7 +524,7 @@ pyuw_sniffer (const struct frame_unwind *self, struct f= rame_info *this_frame, struct gdbarch *gdbarch =3D (struct gdbarch *) (self->unwind_data); cached_frame_info *cached_frame; =20 - gdbpy_enter enter_py (gdbarch, current_language); + gdbpy_enter enter_py (gdbarch); =20 pyuw_debug_printf ("frame=3D%d, sp=3D%s, pc=3D%s", frame_relative_level (this_frame), diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c index 79e1c013261..73c860bcc96 100644 --- a/gdb/python/py-utils.c +++ b/gdb/python/py-utils.c @@ -93,8 +93,9 @@ unicode_to_encoded_python_string (PyObject *unicode_str, = const char *charset) gdb::unique_xmalloc_ptr unicode_to_target_string (PyObject *unicode_str) { - return unicode_to_encoded_string (unicode_str, - target_charset (python_gdbarch)); + return (unicode_to_encoded_string + (unicode_str, + target_charset (gdbpy_enter::get_gdbarch ()))); } =20 /* Returns a PyObject with the contents of the given unicode string @@ -104,8 +105,9 @@ unicode_to_target_string (PyObject *unicode_str) static gdbpy_ref<> unicode_to_target_python_string (PyObject *unicode_str) { - return unicode_to_encoded_python_string (unicode_str, - target_charset (python_gdbarch)); + return (unicode_to_encoded_python_string + (unicode_str, + target_charset (gdbpy_enter::get_gdbarch ()))); } =20 /* Converts a python string (8-bit or unicode) to a target string in diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index 70b33d5a27b..d6ceb54fed8 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -35,23 +35,26 @@ GDB (which uses target arithmetic). */ =20 /* Python's integer type corresponds to C's long type. */ -#define builtin_type_pyint builtin_type (python_gdbarch)->builtin_long +#define builtin_type_pyint \ + builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long =20 /* Python's float type corresponds to C's double type. */ -#define builtin_type_pyfloat builtin_type (python_gdbarch)->builtin_double +#define builtin_type_pyfloat \ + builtin_type (gdbpy_enter::get_gdbarch ())->builtin_double =20 /* Python's long type corresponds to C's long long type. */ -#define builtin_type_pylong builtin_type (python_gdbarch)->builtin_long_lo= ng +#define builtin_type_pylong \ + builtin_type (gdbpy_enter::get_gdbarch ())->builtin_long_long =20 /* Python's long type corresponds to C's long long type. Unsigned version= . */ #define builtin_type_upylong builtin_type \ - (python_gdbarch)->builtin_unsigned_long_long + (gdbpy_enter::get_gdbarch ())->builtin_unsigned_long_long =20 #define builtin_type_pybool \ - language_bool_type (python_language, python_gdbarch) + language_bool_type (current_language, gdbpy_enter::get_gdbarch ()) =20 #define builtin_type_pychar \ - language_string_char_type (python_language, python_gdbarch) + language_string_char_type (current_language, gdbpy_enter::get_gdbarch ()) =20 struct value_object { PyObject_HEAD @@ -754,7 +757,7 @@ valpy_format_string (PyObject *self, PyObject *args, Py= Object *kw) try { common_val_print (((value_object *) self)->value, &stb, 0, - &opts, python_language); + &opts, current_language); } catch (const gdb_exception &except) { @@ -1160,7 +1163,7 @@ valpy_str (PyObject *self) try { common_val_print (((value_object *) self)->value, &stb, 0, - &opts, python_language); + &opts, current_language); } catch (const gdb_exception &except) { @@ -2025,7 +2028,7 @@ gdbpy_convenience_variable (PyObject *self, PyObject = *args) =20 if (var !=3D NULL) { - res_val =3D value_of_internalvar (python_gdbarch, var); + res_val =3D value_of_internalvar (gdbpy_enter::get_gdbarch (), var); if (value_type (res_val)->code () =3D=3D TYPE_CODE_VOID) res_val =3D NULL; } diff --git a/gdb/python/py-xmethods.c b/gdb/python/py-xmethods.c index fd3673029a0..9d5824962b1 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; =20 Py_DECREF (m_py_worker); Py_DECREF (m_this_type); @@ -122,7 +122,7 @@ gdbpy_get_matching_xmethod_workers { gdb_assert (obj_type !=3D NULL && method_name !=3D NULL); =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 gdbpy_ref<> py_type (type_to_type_object (obj_type)); if (py_type =3D=3D 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 las= t to be destroyed. */ - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; struct type *obj_type; int i =3D 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; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 /* 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; =20 int i; struct type *obj_type, *this_type; @@ -580,7 +580,7 @@ python_xmethod_worker::invoke (struct value *obj, } else { - res =3D allocate_value (lookup_typename (python_language, + res =3D allocate_value (lookup_typename (current_language, "void", NULL, 0)); } =20 diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h index 583989c5a6d..ccea5c4a9cd 100644 --- a/gdb/python/python-internal.h +++ b/gdb/python/python-internal.h @@ -625,14 +625,36 @@ class gdbpy_enter { public: =20 - gdbpy_enter (struct gdbarch *gdbarch, const struct language_defn *langua= ge); + /* 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 =3D nullptr, + const struct language_defn *language =3D nullptr); =20 ~gdbpy_enter (); =20 DISABLE_COPY_AND_ASSIGN (gdbpy_enter); =20 + /* 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: =20 + /* 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; }; =20 -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) \ diff --git a/gdb/python/python.c b/gdb/python/python.c index 4dcda53d9ab..7ddc170904c 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -192,13 +192,12 @@ const struct extension_language_defn extension_langua= ge_python =3D =20 /* 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; =20 gdbpy_enter::gdbpy_enter (struct gdbarch *gdbarch, const struct language_defn *language) : m_gdbarch (python_gdbarch), - m_language (python_language) + m_language (language =3D=3D 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 =3D PyGILState_Ensure (); =20 python_gdbarch =3D gdbarch; - python_language =3D language; + if (language !=3D nullptr) + set_language (language->la_language); =20 /* Save it and ensure ! PyErr_Occurred () afterwards. */ m_error.emplace (); @@ -228,12 +228,27 @@ gdbpy_enter::~gdbpy_enter () m_error->restore (); =20 python_gdbarch =3D m_gdbarch; - python_language =3D m_language; + if (m_language !=3D nullptr) + set_language (m_language->la_language); =20 restore_active_ext_lang (m_previous_active); PyGILState_Release (m_state); } =20 +struct gdbarch * +gdbpy_enter::get_gdbarch () +{ + if (python_gdbarch !=3D nullptr) + return python_gdbarch; + return get_current_arch (); +} + +void +gdbpy_enter::finalize () +{ + python_gdbarch =3D target_gdbarch (); +} + /* A helper class to save and restore the GIL, but without touching the other globals that are handled by gdbpy_enter. */ =20 @@ -318,7 +333,7 @@ python_interactive_command (const char *arg, int from_t= ty) =20 arg =3D skip_spaces (arg); =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (arg && *arg) { @@ -412,7 +427,7 @@ gdbpy_eval_from_control_command (const struct extension= _language_defn *extlang, if (cmd->body_list_1 !=3D nullptr) error (_("Invalid \"python\" block structure.")); =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 std::string script =3D compute_python_string (cmd->body_list_0.get ()); ret =3D 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; =20 scoped_restore save_async =3D make_scoped_restore (¤t_ui->async, 0= ); =20 @@ -556,7 +571,7 @@ gdbpy_parameter (PyObject *self, PyObject *args) static PyObject * gdbpy_target_charset (PyObject *self, PyObject *args) { - const char *cset =3D target_charset (python_gdbarch); + const char *cset =3D target_charset (gdbpy_enter::get_gdbarch ()); =20 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 =3D target_wide_charset (python_gdbarch); + const char *cset =3D target_wide_charset (gdbpy_enter::get_gdbarch ()); =20 return PyUnicode_Decode (cset, strlen (cset), host_charset (), NULL); } @@ -867,7 +882,7 @@ gdbpy_decode_line (PyObject *self, PyObject *args) } =20 if (arg !=3D NULL) - location =3D string_to_event_location_basic (&arg, python_language, + location =3D string_to_event_location_basic (&arg, current_language, symbol_name_match_type::WILD); =20 std::vector 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); } =20 @@ -1011,7 +1026,7 @@ struct gdbpy_event =20 void operator() () { - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 gdbpy_ref<> call_result (PyObject_CallObject (m_func, NULL)); if (call_result =3D=3D NULL) @@ -1060,7 +1075,7 @@ gdbpy_before_prompt_hook (const struct extension_lang= uage_defn *extlang, if (!gdb_python_initialized) return EXT_LANG_RC_NOP; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 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 st= d::string &contents) if (!gdb_python_initialized) return {}; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (gdb_python_module =3D=3D nullptr || !PyObject_HasAttrString (gdb_python_module, "colorize")) @@ -1400,7 +1415,7 @@ gdbpy_source_objfile_script (const struct extension_l= anguage_defn *extlang, if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (objfile->arch (), current_language); + gdbpy_enter enter_py (objfile->arch ()); scoped_restore restire_current_objfile =3D make_scoped_restore (&gdbpy_current_objfile, objfile); =20 @@ -1421,7 +1436,7 @@ gdbpy_execute_objfile_script (const struct extension_= language_defn *extlang, if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (objfile->arch (), current_language); + gdbpy_enter enter_py (objfile->arch ()); scoped_restore restire_current_objfile =3D make_scoped_restore (&gdbpy_current_objfile, objfile); =20 @@ -1453,7 +1468,7 @@ gdbpy_start_type_printers (const struct extension_lan= guage_defn *extlang, if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 gdbpy_ref<> type_module (PyImport_ImportModule ("gdb.types")); if (type_module =3D=3D NULL) @@ -1498,7 +1513,7 @@ gdbpy_apply_type_printers (const struct extension_lan= guage_defn *extlang, if (!gdb_python_initialized) return EXT_LANG_RC_NOP; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 gdbpy_ref<> type_obj (type_to_type_object (type)); if (type_obj =3D=3D NULL) @@ -1561,7 +1576,7 @@ gdbpy_free_type_printers (const struct extension_lang= uage_defn *extlang, if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; Py_DECREF (printers); } =20 @@ -1696,8 +1711,7 @@ finalize_python (void *ignore) previous_active =3D set_active_ext_lang (&extension_language_python); =20 (void) PyGILState_Ensure (); - python_gdbarch =3D target_gdbarch (); - python_language =3D current_language; + gdbpy_enter::finalize (); =20 Py_Finalize (); =20 @@ -1756,7 +1770,7 @@ gdbpy_gdb_exiting (int exit_code) if (!gdb_python_initialized) return; =20 - gdbpy_enter enter_py (python_gdbarch, python_language); + gdbpy_enter enter_py; =20 if (emit_exiting_event (exit_code) < 0) gdbpy_print_stack (); @@ -2174,7 +2188,7 @@ gdbpy_initialize (const struct extension_language_def= n *extlang) if (!do_start_initialization () && PyErr_Occurred ()) gdbpy_print_stack (); =20 - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_enter enter_py; =20 if (!do_initialize (extlang)) { diff --git a/gdb/testsuite/gdb.python/py-inferior.exp b/gdb/testsuite/gdb.p= ython/py-inferior.exp index 5b5f58243e4..aa78a1540cf 100644 --- a/gdb/testsuite/gdb.python/py-inferior.exp +++ b/gdb/testsuite/gdb.python/py-inferior.exp @@ -220,6 +220,7 @@ with_test_prefix "is_valid" { gdb_test "python print (inf_list\[0\].is_valid())" "True" \ "check inferior validity 1" =20 + # The "dummy" line below used to cause a gdb crash. gdb_test_multiline "install new inferior event handler" \ "python" "" \ "my_inferior_count =3D 1" "" \ @@ -227,6 +228,7 @@ with_test_prefix "is_valid" { " global my_inferior_count" "" \ " if evt.inferior is not None:" "" \ " my_inferior_count =3D my_inferior_count + 1" "" \ + " dummy =3D gdb.Value(True)" "" \ "gdb.events.new_inferior.connect(new_inf_handler)" "" \ "end" "" gdb_test_multiline "install inferior deleted event handler" \ diff --git a/gdb/testsuite/gdb.python/py-lookup-type.exp b/gdb/testsuite/gd= b.python/py-lookup-type.exp index 52a1dae20ed..534a5fd418e 100644 --- a/gdb/testsuite/gdb.python/py-lookup-type.exp +++ b/gdb/testsuite/gdb.python/py-lookup-type.exp @@ -54,3 +54,11 @@ test_lookup_type "opencl" "ushort" test_lookup_type "objective-c" "char" =20 test_lookup_type "pascal" "char" + +# Ensure that the language can be changed from within Python and still +# affect the results. +gdb_test_multiline "look up ada type from another language" \ + "python" "" \ + "gdb.execute('set language ada')" "" \ + "print(gdb.lookup_type('character'))" "" \ + "end" "character"