forked from Imagelibrary/binutils-gdb
The previous commit relied on spotting when a Python defined TUI
window factory was deleted. I spotted that the window factories are
not deleted when GDB shuts down its Python environment, they are only
deleted when one window factory replaces another. Consider this
example Python script:
class TestWindowFactory:
def __init__(self, msg):
self.msg = msg
print("Entering TestWindowFactory.__init__: %s" % self.msg)
def __call__(self, tui_win):
print("Entering TestWindowFactory.__call__: %s" % self.msg)
return TestWindow(tui_win, self.msg)
def __del__(self):
print("Entering TestWindowFactory.__del__: %s" % self.msg)
gdb.register_window_type("test_window", TestWindowFactory("A"))
gdb.register_window_type("test_window", TestWindowFactory("B"))
And this GDB session:
(gdb) source tui.py
Entering TestWindowFactory.__init__: A
Entering TestWindowFactory.__init__: B
Entering TestWindowFactory.__del__: B
(gdb) quit
Notice that when the 'B' window replaces the 'A' window we see the 'A'
object being deleted. But, when Python is shut down (after the
'quit') the 'B' object is never deleted.
Instead, GDB retains a reference to the window factory object, which
forces the Python object to remain live even after the Python
interpreter itself has been shut down.
The references themselves are held in a dynamically allocated
std::unordered_map (in tui/tui-layout.c) which is never deallocated,
thus the underlying Python references are never decremented to zero,
and so GDB never tries to delete these Python objects.
This commit is the first half of the work to clean up this edge case.
All gdbpy_tui_window_maker objects (the objects that implement the
TUI window factory callback for Python defined TUI windows), are now
linked together into a global list using the intrusive list mechanism.
When GDB shuts down the Python interpreter we can now walk this global
list and release the reference that is held to the underlying Python
object. By releasing this reference the Python object will now be
deleted.
I've added a new assert in gdbpy_tui_window_maker::operator(), this
will catch the case where we somehow end up in here after having
reset the reference to the underlying Python object. I don't think
this should ever happen though as we only clear the references when
shutting down the Python interpreter, and the ::operator() function is
only called when trying to apply a new TUI layout - something that
shouldn't happen while GDB itself is shutting down.
This commit does not update the std::unordered_map in tui-layout.c,
that will be done in the next commit.
Reviewed-By: Tom Tromey <tom@tromey.com>
106 lines
3.3 KiB
Plaintext
106 lines
3.3 KiB
Plaintext
# Copyright (C) 2023 Free Software Foundation, Inc.
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# Test that GDB correctly deallocates the window factory object (a)
|
|
# when a window factory is replaced, and (b) during GDB shutdown.
|
|
#
|
|
# This test also ensures that when a new window is registered (via the
|
|
# Python API) with the same name as an existing window, then the
|
|
# previous window is replaced.
|
|
|
|
load_lib gdb-python.exp
|
|
|
|
tuiterm_env
|
|
|
|
clean_restart
|
|
|
|
require allow_tui_tests allow_python_tests
|
|
|
|
set pyfile [gdb_remote_download host \
|
|
${srcdir}/${subdir}/${gdb_test_file_name}.py]
|
|
|
|
Term::clean_restart 24 80
|
|
Term::prepare_for_tui
|
|
|
|
gdb_test "source ${pyfile}" "Python script imported" \
|
|
"import python scripts"
|
|
|
|
gdb_test "python register_window_factory('msg_1')" \
|
|
"Entering TestWindowFactory\\.__init__: msg_1"
|
|
|
|
gdb_test "python register_window_factory('msg_2')" \
|
|
[multi_line \
|
|
"Entering TestWindowFactory\\.__init__: msg_2" \
|
|
"Entering TestWindowFactory\\.__del__: msg_1"]
|
|
|
|
gdb_test_no_output "tui new-layout test test_window 1 cmd 1 status 1"
|
|
|
|
# Load the custom window layout and ensure that the correct window
|
|
# factory was used.
|
|
with_test_prefix "msg_2" {
|
|
Term::command_no_prompt_prefix "layout test"
|
|
Term::check_box_contents "check test_window box" 0 0 80 15 \
|
|
"TestWindow \\(msg_2\\)"
|
|
}
|
|
|
|
# Replace the existing window factory with a new one, then switch
|
|
# layouts so that GDB recreates the window, and check that the new
|
|
# window factory was used.
|
|
with_test_prefix "msg_3" {
|
|
Term::command "python register_window_factory('msg_3')"
|
|
Term::check_region_contents "check for python output" \
|
|
0 18 80 2 \
|
|
[multi_line \
|
|
"Entering TestWindowFactory.__init__: msg_3\\s+" \
|
|
"Entering TestWindowFactory.__del__: msg_2"]
|
|
Term::command "layout src"
|
|
Term::command "layout test"
|
|
|
|
Term::check_box_contents "check test_window box" 0 0 80 15 \
|
|
"TestWindow \\(msg_3\\)"
|
|
}
|
|
|
|
# Restart GDB, setup a TUI window factory, and then check that the
|
|
# Python object is deallocated when GDB exits.
|
|
with_test_prefix "call __del__ at exit" {
|
|
clean_restart
|
|
|
|
gdb_test "source ${pyfile}" "Python script imported" \
|
|
"import python scripts"
|
|
|
|
gdb_test "python register_window_factory('msg_1')" \
|
|
"Entering TestWindowFactory\\.__init__: msg_1"
|
|
|
|
gdb_test "python register_window_factory('msg_2')" \
|
|
[multi_line \
|
|
"Entering TestWindowFactory\\.__init__: msg_2" \
|
|
"Entering TestWindowFactory\\.__del__: msg_1"]
|
|
|
|
set saw_window_factory_del 0
|
|
gdb_test_multiple "quit" "" {
|
|
-re "^quit\r\n" {
|
|
exp_continue
|
|
}
|
|
-re "^Entering TestWindowFactory.__del__: msg_2\r\n" {
|
|
incr saw_window_factory_del
|
|
exp_continue
|
|
}
|
|
eof {
|
|
gdb_assert { $saw_window_factory_del == 1 }
|
|
pass $gdb_test_name
|
|
}
|
|
}
|
|
}
|