From c0afd99439fea90c1e93e5add7eac06cf533bb3e Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Wed, 26 Jul 2023 12:29:28 +0200 Subject: [PATCH] [gdb/tui] Fix assert in ~gdbpy_tui_window_maker In gdb/tui/tui-layout.c, we have: ... static window_types_map known_window_types; ... and in gdb/python/py-tui.c: ... /* A global list of all gdbpy_tui_window_maker objects. */ static intrusive_list m_window_maker_list; }; /* See comment in class declaration above. */ intrusive_list gdbpy_tui_window_maker::m_window_maker_list; ... With a gdb build with -O0 or -O2, the static destructor calling order seems to be: - first gdb/tui/tui-layout.c, - then gdb/python/py-tui.c. So when running test-case gdb.python/tui-window-factory.exp, we see the following order of events: - the destructor for known_window_types is called, which triggers calling the destructor for the only element E of m_window_maker_list. The destructor destroys E, and also removes E from m_window_maker_list, leaving it empty. - the destructor for m_window_maker_list is called. It's empty, so it's a nop. However, when building gdb with -O2 -flto=auto, the static destructor calling order seems to be reversed. Instead, we have these events: - the destructor for m_window_maker_list is called. This doesn't destroy it's only element E, but it does make m_window_maker_list empty. - the destructor for known_window_types is called, which triggers calling the destructor for E. An attempt is done to remove E from m_window_maker_list, but we run into an assertion failure, because the list is empty. Fix this by checking is_linked () before attempting to remove from m_window_maker_list, similar to how things were addressed in commit 995a34b1772 ("Guard against frame.c destructors running before frame-info.c's"). Tested on x86_64-linux. PR tui/30646 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30646 --- gdb/python/py-tui.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/gdb/python/py-tui.c b/gdb/python/py-tui.c index f47f2278dda..64f22dbb462 100644 --- a/gdb/python/py-tui.c +++ b/gdb/python/py-tui.c @@ -339,7 +339,8 @@ intrusive_list gdbpy_tui_window_maker::~gdbpy_tui_window_maker () { /* Remove this gdbpy_tui_window_maker from the global list. */ - m_window_maker_list.erase (m_window_maker_list.iterator_to (*this)); + if (is_linked ()) + m_window_maker_list.erase (m_window_maker_list.iterator_to (*this)); if (m_constr != nullptr) { -- 2.30.2