1 # Copyright (C) 2015-2019 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_re "\[$#\] "
33 # Spawn shell. Returns true on success, false otherwise.
36 global shell_prompt_re
38 set res [remote_spawn host "/bin/sh"]
39 if { $res < 0 || $res == "" } {
40 unsupported "spawning shell failed."
45 set test "spawn shell"
47 -re "$shell_prompt_re$" {
52 fail "$test (timeout)"
65 global shell_prompt_re
71 fail "$test (timeout)"
78 if ![is_remote host] {
83 # Run "stty" and store the output in $result. Returns true on
84 # success, false otherwise.
86 proc run_stty {message result} {
87 global shell_prompt_re
91 send_gdb "stty || echo \"not found\"\n"
94 -re "not found.*not found.*$shell_prompt_re$" {
95 pass "$message (not found)"
97 -re "(.*)$shell_prompt_re$" {
98 set output $expect_out(1,string)
103 fail "$message (timeout)"
106 fail "$message (eof)"
112 # Check that "gdb -batch -ex run" does not leave the terminal in the
115 proc test_terminal_settings_preserved {} {
117 global GDB INTERNAL_GDBFLAGS GDBFLAGS
119 global shell_prompt_re
125 set stty_supported [run_stty "stty before" stty_before]
127 set test "gdb -batch -ex run"
128 append EXTRA_GDBFLAGS "-batch"
129 append EXTRA_GDBFLAGS " -ex \"set height unlimited\""
130 append EXTRA_GDBFLAGS " -ex \"start\""
131 append EXTRA_GDBFLAGS " --args \"$file_arg\""
132 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $EXTRA_GDBFLAGS [host_info gdb_opts]\n"
134 -re "Don't know how to run.*$shell_prompt_re$" {
137 -re "$gdb_prompt $" {
138 # -batch implies no GDB prompt.
141 -re "Temporary breakpoint .*$shell_prompt_re$" {
145 fail "$test (timeout)"
152 set test "echo test_echo"
153 send_gdb "echo test_echo\n"
155 -re "^echo test_echo\r\ntest_echo\r\n.*$shell_prompt_re$" {
159 fail "$test (timeout)"
166 set test "terminal settings preserved"
168 run_stty "stty after" stty_after
170 gdb_assert [string equal $stty_before $stty_after] $test
172 unsupported "$test (no stty)"
178 # Send the quit command to GDB and make sure it exits.
180 proc send_quit_command { test_name } {
181 global shell_prompt_re
190 -re ".*$shell_prompt_re$" {
195 fail "$test (timeout)"
205 # Check that quitting from the CLI via the "quit" command does not leave the
206 # terminal in the wrong state. The GDB commands CMDS are executed before
209 proc test_terminal_settings_preserved_after_cli_exit { cmds } {
211 global GDB INTERNAL_GDBFLAGS GDBFLAGS
213 global shell_prompt_re
219 set saved_gdbflags $GDBFLAGS
221 set stty_supported [run_stty "stty before" stty_before]
224 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts] --args \"$file_arg\"\n"
226 -re "$gdb_prompt $" {
230 fail "$test (timeout)"
238 set test "run command $cmd"
241 -re "$gdb_prompt $" {
245 fail "$test (timeout)"
253 send_quit_command "quit gdb"
255 set test "terminal settings preserved"
257 run_stty "stty after" stty_after
259 gdb_assert [string equal $stty_before $stty_after] $test
261 unsupported "$test (no stty)"
267 # Check that sending SIGTERM kills GDB and does not leave the terminal in the
270 proc test_terminal_settings_preserved_after_sigterm { } {
272 global GDB INTERNAL_GDBFLAGS GDBFLAGS
274 global shell_prompt_re
280 set saved_gdbflags $GDBFLAGS
282 set stty_supported [run_stty "stty before" stty_before]
285 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]\n"
287 -re "$gdb_prompt $" {
291 fail "$test (timeout)"
298 # Retrieve the pid of gdb with the gdb command "shell echo $PPID"
300 set test "run shell echo \$PPID"
301 send_gdb "shell echo \$PPID\n"
303 -re ".*\r\n(\\d+)\r\n$gdb_prompt $" {
304 set gdb_pid $expect_out(1,string)
307 -re ".*\r\n\r\n$gdb_prompt $" {
308 fail "$test (no \$PPID)"
311 fail "$test (timeout)"
318 set test "kill gdb with SIGTERM"
319 if { $gdb_pid == -1 } {
320 fail "$test (no pid)"
321 send_quit_command "quit gdb"
323 remote_exec host "kill -TERM $gdb_pid"
326 -re ".*$shell_prompt_re$" {
331 fail "$test (did not quit)"
336 send_quit_command "quit gdb"
340 set test "terminal settings preserved"
342 run_stty "stty after" stty_after
344 gdb_assert [string equal $stty_before $stty_after] $test
346 unsupported "$test (no stty)"
352 with_test_prefix "batch run" {
353 test_terminal_settings_preserved
356 with_test_prefix "cli exit" {
357 test_terminal_settings_preserved_after_cli_exit { }
360 with_test_prefix "cli exit after start cmd" {
361 test_terminal_settings_preserved_after_cli_exit { "start" }
364 with_test_prefix "cli exit after run cmd" {
365 test_terminal_settings_preserved_after_cli_exit { "run" }
368 with_test_prefix "cli exit after SIGTERM" {
369 test_terminal_settings_preserved_after_sigterm