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} {
33 # Copy the Python script to where the tests are being run.
34 set remote_python_file [gdb_remote_download host \
35 ${srcdir}/${subdir}/${testfile}.py]
37 proc clean_restart_and_setup { prefix } {
39 global remote_python_file
41 with_test_prefix $prefix {
43 Term::clean_restart 24 80 $testfile
45 # Skip all tests if Python scripting is not enabled.
46 if { [skip_python_tests] } { return 0 }
48 # Now source the python script.
49 gdb_test_no_output "source ${remote_python_file}" \
50 "source ${testfile}.py"
52 # Create a new layout making use of our new event window.
53 gdb_test_no_output "tui new-layout test events 1 cmd 1"
55 # Disable source code highlighting.
56 gdb_test_no_output "set style sources off"
59 perror "test suppressed"
67 # Run the test. CLEANUP_PROPERLY is either true or false. This is
68 # used to set a flag in the Python code which controls whether the
69 # Python TUI window cleans up properly or not.
71 # When the Python window does not cleanup properly then it retains a
72 # cyclic reference to itself, this means that it is still possible for
73 # the object to try and redraw itself even when the tui is disabled.
74 proc run_test { cleanup_properly } {
76 if { ![clean_restart_and_setup "initial restart"] } {
77 unsupported "couldn't restart GDB"
81 if { $cleanup_properly } {
82 gdb_test_no_output "python cleanup_properly = True"
84 gdb_test_no_output "python cleanup_properly = False"
87 if {![Term::enter_tui]} {
88 unsupported "TUI not supported"
92 Term::command "layout test"
94 # Confirm that the events box is initially empty, then perform two
95 # actions that will add two events to the window.
96 Term::check_box_contents "no events yet" 0 0 80 16 ""
98 Term::check_box_contents "single stop event" 0 0 80 16 "stop"
100 Term::check_box_contents "two stop events" 0 0 80 16 \
103 # Now disable the tui, we should end up back at a standard GDB prompt.
104 Term::command "tui disable"
106 # Do something just so we know that the CLI is working.
107 gdb_test "print 1 + 1 + 1" " = 3"
109 # Now perform an action that would trigger an event. At one point
110 # there was a bug where the TUI window might try to redraw itself.
111 # This is why we use GDB_TEST_MULTIPLE here, so we can spot the tui
112 # window title and know that things have gone wrong.
113 gdb_test_multiple "next" "next at cli" {
114 -re -wrap "func \\(3\\);" {
118 -re "This Is The Event Window" {
123 # Do something just so we know that the CLI is still working. This
124 # also serves to drain the expect buffer if the above test failed.
125 gdb_test "print 2 + 2 + 2" " = 6"
127 # Now tell the Python code not to check the window is valid before
128 # calling rerended. The result is the Python code will try to draw to
129 # the screen. This should throw a Python exception.
130 gdb_test_no_output "python perform_valid_check = False"
131 set exception_pattern "\r\nPython Exception\[^\n\r\]+TUI window is invalid\[^\n\r\]+"
132 gdb_test_multiple "next" "next at cli, with an exception" {
133 -re -wrap "func \\(4\\);${exception_pattern}" {
137 -re "This Is The Event Window" {
142 # Do something just so we know that the CLI is still working. This
143 # also serves to drain the expect buffer if the above test failed.
144 gdb_test "print 3 + 3 + 3" " = 9"
146 # Set 'update_title' to True. The Python script will now try to set
147 # the window title when an event occurs (instead of trying to redraw
148 # the window). As the window is still not displayed this will again
149 # through an exception.
150 gdb_test_no_output "python update_title = True"
151 gdb_test_multiple "next" "next at cli, with an exception for setting the title" {
152 -re -wrap "func \\(5\\);${exception_pattern}" {
156 -re "This Is The Event Window" {
161 # We need to perform a restart here as the TUI library we use for
162 # testing doesn't seem to handle output in the command window
163 # correctly, and gets really upset as we approach the bottom of
166 # Restart GDB, enable tui mode, select the new layout. Then
167 # disable tui and re-enable again.
168 if { ![clean_restart_and_setup "second restart"] } {
169 unsupported "couldn't restart GDB"
173 with_test_prefix "enter tui again" {
174 if {![Term::enter_tui]} {
175 unsupported "TUI not supported"
180 Term::command "layout test"
181 Term::command "tui disable"
182 send_gdb "tui enable\n"
183 Term::check_box "check for python window" 0 0 80 16
186 # Run the tests in both cleanup modes.
187 foreach_with_prefix cleanup_properly { True False } {
188 run_test $cleanup_properly