1 # Copyright (C) 2021-2022 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 # Create a TUI window in Python that responds to GDB event. Each
17 # event will trigger the TUI window to redraw itself.
19 # This test is checking how GDB behaves if the user first displays a
20 # Python based tui window, and then does 'tui disable'. At one point
21 # it was possible that GDB would try to redraw the tui window even
22 # though the tui should be disabled.
24 load_lib gdb-python.exp
29 if {[build_executable "failed to prepare" ${testfile} ${srcfile}] == -1} {
34 if {[skip_tui_tests]} {
38 # Copy the Python script to where the tests are being run.
39 set remote_python_file [gdb_remote_download host \
40 ${srcdir}/${subdir}/${testfile}.py]
42 proc clean_restart_and_setup { prefix } {
44 global remote_python_file
46 with_test_prefix $prefix {
48 Term::clean_restart 24 80 $testfile
50 # Skip all tests if Python scripting is not enabled.
51 if { [skip_python_tests] } { return 0 }
53 # Now source the python script.
54 gdb_test_no_output "source ${remote_python_file}" \
55 "source ${testfile}.py"
57 # Create a new layout making use of our new event window.
58 gdb_test_no_output "tui new-layout test events 1 cmd 1"
60 # Disable source code highlighting.
61 gdb_test_no_output "set style sources off"
64 perror "test suppressed"
72 # Run the test. CLEANUP_PROPERLY is either true or false. This is
73 # used to set a flag in the Python code which controls whether the
74 # Python TUI window cleans up properly or not.
76 # When the Python window does not cleanup properly then it retains a
77 # cyclic reference to itself, this means that it is still possible for
78 # the object to try and redraw itself even when the tui is disabled.
79 proc run_test { cleanup_properly } {
81 if { ![clean_restart_and_setup "initial restart"] } {
82 unsupported "couldn't restart GDB"
86 if { $cleanup_properly } {
87 gdb_test_no_output "python cleanup_properly = True"
89 gdb_test_no_output "python cleanup_properly = False"
92 if {![Term::enter_tui]} {
93 unsupported "TUI not supported"
97 Term::command "layout test"
99 # Confirm that the events box is initially empty, then perform two
100 # actions that will add two events to the window.
101 Term::check_box_contents "no events yet" 0 0 80 16 ""
103 Term::check_box_contents "single stop event" 0 0 80 16 "stop"
105 Term::check_box_contents "two stop events" 0 0 80 16 \
108 # Now disable the tui, we should end up back at a standard GDB prompt.
109 Term::command "tui disable"
111 # Do something just so we know that the CLI is working.
112 gdb_test "print 1 + 1 + 1" " = 3"
114 # Now perform an action that would trigger an event. At one point
115 # there was a bug where the TUI window might try to redraw itself.
116 # This is why we use GDB_TEST_MULTIPLE here, so we can spot the tui
117 # window title and know that things have gone wrong.
118 gdb_test_multiple "next" "next at cli" {
119 -re -wrap "func \\(3\\);" {
123 -re "This Is The Event Window" {
128 # Do something just so we know that the CLI is still working. This
129 # also serves to drain the expect buffer if the above test failed.
130 gdb_test "print 2 + 2 + 2" " = 6"
132 # Now tell the Python code not to check the window is valid before
133 # calling rerended. The result is the Python code will try to draw to
134 # the screen. This should throw a Python exception.
135 gdb_test_no_output "python perform_valid_check = False"
136 set exception_pattern "\r\nPython Exception\[^\n\r\]+TUI window is invalid\[^\n\r\]+"
137 gdb_test_multiple "next" "next at cli, with an exception" {
138 -re -wrap "func \\(4\\);${exception_pattern}" {
142 -re "This Is The Event Window" {
147 # Do something just so we know that the CLI is still working. This
148 # also serves to drain the expect buffer if the above test failed.
149 gdb_test "print 3 + 3 + 3" " = 9"
151 # Set 'update_title' to True. The Python script will now try to set
152 # the window title when an event occurs (instead of trying to redraw
153 # the window). As the window is still not displayed this will again
154 # through an exception.
155 gdb_test_no_output "python update_title = True"
156 gdb_test_multiple "next" "next at cli, with an exception for setting the title" {
157 -re -wrap "func \\(5\\);${exception_pattern}" {
161 -re "This Is The Event Window" {
166 # We need to perform a restart here as the TUI library we use for
167 # testing doesn't seem to handle output in the command window
168 # correctly, and gets really upset as we approach the bottom of
171 # Restart GDB, enable tui mode, select the new layout. Then
172 # disable tui and re-enable again.
173 if { ![clean_restart_and_setup "second restart"] } {
174 unsupported "couldn't restart GDB"
178 with_test_prefix "enter tui again" {
179 if {![Term::enter_tui]} {
180 unsupported "TUI not supported"
185 Term::command "layout test"
186 Term::command "tui disable"
187 send_gdb "tui enable\n"
188 Term::check_box "check for python window" 0 0 80 16
191 # Run the tests in both cleanup modes.
192 foreach_with_prefix cleanup_properly { True False } {
193 run_test $cleanup_properly