1 # Copyright (C) 2015-2021 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 # Check that "gdb -batch -ex run" does not leave the terminal in the
21 if {[build_executable "failed to prepare" $testfile $srcfile debug] == -1} {
27 set file_arg [remote_download host $file_arg]
31 set shell_prompt_ps1 "gdb-subshell$ "
32 set shell_prompt_re [string_to_regexp $shell_prompt_ps1]
34 # Spawn shell. Returns true on success, false otherwise.
37 global shell_prompt_ps1 shell_prompt_re
39 set res [remote_spawn host "/bin/sh"]
40 if { $res < 0 || $res == "" } {
41 unsupported "spawning shell failed."
45 send_gdb "PS1=\"$shell_prompt_ps1\"\n"
48 # PS1="gdb-subshell$ "^M
51 # PS1="gdb-subshell$ "^M
52 # sh-4.4$ PS1="gdb-subshell$ "^M
55 set test "spawn shell"
57 -re "PS1=\"$shell_prompt_re" {
60 -re "$shell_prompt_re$" {
65 fail "$test (timeout)"
78 global shell_prompt_re
84 fail "$test (timeout)"
91 if ![is_remote host] {
96 # Run "stty" and store the output in $result. Returns true on
97 # success, false otherwise.
99 proc run_stty {message result} {
100 global shell_prompt_re
104 send_gdb "stty || echo \"not found\"\n"
107 -re "not found.*not found.*$shell_prompt_re$" {
108 pass "$message (not found)"
110 -re "(.*)$shell_prompt_re$" {
111 set output $expect_out(1,string)
116 fail "$message (timeout)"
119 fail "$message (eof)"
125 # Check that "gdb -batch -ex run" does not leave the terminal in the
128 proc test_terminal_settings_preserved {} {
130 global GDB INTERNAL_GDBFLAGS GDBFLAGS
132 global shell_prompt_re
138 set stty_supported [run_stty "stty before" stty_before]
140 set test "gdb -batch -ex run"
141 append EXTRA_GDBFLAGS "-batch"
142 append EXTRA_GDBFLAGS " -ex \"set height unlimited\""
143 append EXTRA_GDBFLAGS " -ex \"start\""
144 append EXTRA_GDBFLAGS " --args \"$file_arg\""
145 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $EXTRA_GDBFLAGS [host_info gdb_opts]\n"
147 -re "Don't know how to run.*$shell_prompt_re$" {
150 -re "$gdb_prompt $" {
151 # -batch implies no GDB prompt.
154 -re "Temporary breakpoint .*$shell_prompt_re$" {
158 fail "$test (timeout)"
165 set test "echo test_echo"
166 send_gdb "echo test_echo\n"
168 -re "^echo test_echo\r\ntest_echo\r\n.*$shell_prompt_re$" {
172 fail "$test (timeout)"
179 set test "terminal settings preserved"
181 run_stty "stty after" stty_after
183 gdb_assert [string equal $stty_before $stty_after] $test
185 unsupported "$test (no stty)"
191 # Send the quit command to GDB and make sure it exits.
193 proc send_quit_command { test_name } {
194 global shell_prompt_re
203 -re ".*$shell_prompt_re$" {
208 fail "$test (timeout)"
218 # Check that quitting from the CLI via the "quit" command does not leave the
219 # terminal in the wrong state. The GDB commands CMDS are executed before
222 proc test_terminal_settings_preserved_after_cli_exit { cmds } {
224 global GDB INTERNAL_GDBFLAGS GDBFLAGS
226 global shell_prompt_re
232 set saved_gdbflags $GDBFLAGS
234 set stty_supported [run_stty "stty before" stty_before]
237 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts] --args \"$file_arg\"\n"
239 -re "$gdb_prompt $" {
243 fail "$test (timeout)"
251 set test "run command $cmd"
254 -re "$gdb_prompt $" {
258 fail "$test (timeout)"
266 send_quit_command "quit gdb"
268 set test "terminal settings preserved"
270 run_stty "stty after" stty_after
272 gdb_assert [string equal $stty_before $stty_after] $test
274 unsupported "$test (no stty)"
280 # Check that sending SIGTERM kills GDB and does not leave the terminal in the
283 proc test_terminal_settings_preserved_after_sigterm { } {
285 global GDB INTERNAL_GDBFLAGS GDBFLAGS
287 global shell_prompt_re
289 # On Windows, GDB's "shell" command spawns cmd.exe, which does not
290 # understand PPID. So we're out of luck even if the test harness
291 # uses a remote_exec shell with a working "kill" command.
292 if [ishost *-*-mingw*] {
300 set saved_gdbflags $GDBFLAGS
302 set stty_supported [run_stty "stty before" stty_before]
305 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]\n"
307 -re "$gdb_prompt $" {
311 fail "$test (timeout)"
318 # Retrieve the pid of gdb with the gdb command "shell echo $PPID"
320 set test "run shell echo \$PPID"
321 send_gdb "shell echo \$PPID\n"
323 -re ".*\r\n(\\d+)\r\n$gdb_prompt $" {
324 set gdb_pid $expect_out(1,string)
327 -re ".*\r\n\r\n$gdb_prompt $" {
328 fail "$test (no \$PPID)"
331 fail "$test (timeout)"
338 set test "kill gdb with SIGTERM"
339 if { $gdb_pid == -1 } {
340 fail "$test (no pid)"
341 send_quit_command "quit gdb"
343 remote_exec host "kill -TERM $gdb_pid"
346 -re ".*$shell_prompt_re$" {
351 fail "$test (did not quit)"
356 send_quit_command "quit gdb"
360 set test "terminal settings preserved"
362 run_stty "stty after" stty_after
364 gdb_assert [string equal $stty_before $stty_after] $test
366 unsupported "$test (no stty)"
372 with_test_prefix "batch run" {
373 test_terminal_settings_preserved
376 with_test_prefix "cli exit" {
377 test_terminal_settings_preserved_after_cli_exit { }
380 with_test_prefix "cli exit after start cmd" {
381 test_terminal_settings_preserved_after_cli_exit { "start" }
384 with_test_prefix "cli exit after run cmd" {
385 test_terminal_settings_preserved_after_cli_exit { "run" }
388 with_test_prefix "cli exit after SIGTERM" {
389 test_terminal_settings_preserved_after_sigterm