1 # Copyright (C) 2015-2024 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 \"start\""
143 append EXTRA_GDBFLAGS " --args \"$file_arg\""
144 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $EXTRA_GDBFLAGS [host_info gdb_opts]\n"
146 -re "Don't know how to run.*$shell_prompt_re$" {
149 -re "$gdb_prompt $" {
150 # -batch implies no GDB prompt.
153 -re "Temporary breakpoint .*$shell_prompt_re$" {
157 fail "$test (timeout)"
164 set test "echo test_echo"
165 send_gdb "echo test_echo\n"
167 -re "^echo test_echo\r\ntest_echo\r\n.*$shell_prompt_re$" {
171 fail "$test (timeout)"
178 set test "terminal settings preserved"
180 run_stty "stty after" stty_after
182 gdb_assert [string equal $stty_before $stty_after] $test
184 unsupported "$test (no stty)"
190 # Send the quit command to GDB and make sure it exits.
192 proc send_quit_command { test_name } {
193 global shell_prompt_re
202 -re ".*$shell_prompt_re$" {
207 fail "$test (timeout)"
217 # Check that quitting from the CLI via the "quit" command does not leave the
218 # terminal in the wrong state. The GDB commands CMDS are executed before
221 proc test_terminal_settings_preserved_after_cli_exit { cmds } {
223 global GDB INTERNAL_GDBFLAGS GDBFLAGS
225 global shell_prompt_re
231 set stty_supported [run_stty "stty before" stty_before]
234 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts] --args \"$file_arg\"\n"
236 -re "$gdb_prompt $" {
240 fail "$test (timeout)"
248 set test "run command $cmd"
251 -re "$gdb_prompt $" {
255 fail "$test (timeout)"
263 send_quit_command "quit gdb"
265 set test "terminal settings preserved"
267 run_stty "stty after" stty_after
269 gdb_assert [string equal $stty_before $stty_after] $test
271 unsupported "$test (no stty)"
277 # Check that sending SIGTERM kills GDB and does not leave the terminal in the
280 proc test_terminal_settings_preserved_after_sigterm { } {
282 global GDB INTERNAL_GDBFLAGS GDBFLAGS
284 global shell_prompt_re
286 # On Windows, GDB's "shell" command spawns cmd.exe, which does not
287 # understand PPID. So we're out of luck even if the test harness
288 # uses a remote_exec shell with a working "kill" command.
289 if [ishost *-*-mingw*] {
297 set stty_supported [run_stty "stty before" stty_before]
300 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]\n"
302 -re "$gdb_prompt $" {
306 fail "$test (timeout)"
313 # Retrieve the pid of gdb with the gdb command "shell echo $PPID"
315 set test "run shell echo \$PPID"
316 send_gdb "shell echo \$PPID\n"
318 -re ".*\r\n(\\d+)\r\n$gdb_prompt $" {
319 set gdb_pid $expect_out(1,string)
322 -re ".*\r\n\r\n$gdb_prompt $" {
323 fail "$test (no \$PPID)"
326 fail "$test (timeout)"
333 set test "kill gdb with SIGTERM"
334 if { $gdb_pid == -1 } {
335 fail "$test (no pid)"
336 send_quit_command "quit gdb"
338 remote_exec host "kill -TERM $gdb_pid"
341 -re ".*$shell_prompt_re$" {
346 fail "$test (did not quit)"
351 send_quit_command "quit gdb"
355 set test "terminal settings preserved"
357 run_stty "stty after" stty_after
359 gdb_assert [string equal $stty_before $stty_after] $test
361 unsupported "$test (no stty)"
367 with_test_prefix "batch run" {
368 test_terminal_settings_preserved
371 with_test_prefix "cli exit" {
372 test_terminal_settings_preserved_after_cli_exit { }
375 with_test_prefix "cli exit after start cmd" {
376 test_terminal_settings_preserved_after_cli_exit { "start" }
379 with_test_prefix "cli exit after run cmd" {
380 test_terminal_settings_preserved_after_cli_exit { "run" }
383 with_test_prefix "cli exit after SIGTERM" {
384 test_terminal_settings_preserved_after_sigterm