[gdb/syscalls] Sync with strace v6.13
[binutils-gdb.git] / gdb / testsuite / gdb.base / batch-preserve-term-settings.exp
blobd4ceba68c95303b99073bb9e0f9e9d1aa0885e85
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
17 # wrong state.
19 standard_testfile
21 if {[build_executable "failed to prepare" $testfile $srcfile debug] == -1} {
22     return -1
25 set file_arg $binfile
26 if [is_remote host] {
27   set file_arg [remote_download host $file_arg]
30 # The shell's prompt.
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.
36 proc spawn_shell {} {
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."
42         return 0
43     }
45     send_gdb "PS1=\"$shell_prompt_ps1\"\n"
47     # Try to match:
48     #   PS1="gdb-subshell$ "^M
49     #   $ gdb-subshell$ 
50     # or:
51     #   PS1="gdb-subshell$ "^M
52     #   sh-4.4$ PS1="gdb-subshell$ "^M
53     #   gdb-subshell$ 
54     set gotit 0
55     set test "spawn shell"
56     gdb_expect {
57         -re "PS1=\"$shell_prompt_re" {
58             exp_continue
59         }
60         -re "$shell_prompt_re$" {
61             pass $test
62             set gotit 1
63         }
64         timeout {
65             fail "$test (timeout)"
66         }
67         eof {
68             fail "$test (eof)"
69         }
70     }
72     return $gotit
75 # Exit the shell.
77 proc exit_shell {} {
78     global shell_prompt_re
80     set test "exit shell"
81     send_gdb "exit\n"
82     gdb_expect {
83         timeout {
84             fail "$test (timeout)"
85             return 0
86         }
87         eof {
88             pass "$test"
89         }
90     }
91     if ![is_remote host] {
92         remote_close host
93     }
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
102     upvar $result output
104     send_gdb "stty || echo \"not found\"\n"
105     set gotit 0
106     gdb_expect {
107         -re "not found.*not found.*$shell_prompt_re$" {
108             pass "$message (not found)"
109         }
110         -re "(.*)$shell_prompt_re$" {
111             set output $expect_out(1,string)
112             set gotit 1
113             pass $message
114         }
115         timeout {
116             fail "$message (timeout)"
117         }
118         eof {
119             fail "$message (eof)"
120         }
121     }
122     return $gotit
125 # Check that "gdb -batch -ex run" does not leave the terminal in the
126 # wrong state.
128 proc test_terminal_settings_preserved {} {
129     global file_arg
130     global GDB INTERNAL_GDBFLAGS GDBFLAGS
131     global gdb_prompt
132     global shell_prompt_re
134     if ![spawn_shell] {
135         return
136     }
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"
145     gdb_expect {
146         -re "Don't know how to run.*$shell_prompt_re$" {
147             unsupported $test
148         }
149         -re "$gdb_prompt $" {
150             # -batch implies no GDB prompt.
151             fail $test
152         }
153         -re "Temporary breakpoint .*$shell_prompt_re$" {
154             pass $test
155         }
156         timeout {
157             fail "$test (timeout)"
158         }
159         eof {
160             fail "$test (eof)"
161         }
162     }
164     set test "echo test_echo"
165     send_gdb "echo test_echo\n"
166     gdb_expect {
167         -re "^echo test_echo\r\ntest_echo\r\n.*$shell_prompt_re$" {
168             pass $test
169         }
170         timeout {
171             fail "$test (timeout)"
172         }
173         eof {
174             fail "$test (eof)"
175         }
176     }
178     set test "terminal settings preserved"
179     if $stty_supported {
180         run_stty "stty after" stty_after
182         gdb_assert [string equal $stty_before $stty_after] $test
183     } else {
184         unsupported "$test (no stty)"
185     }
187     exit_shell
190 # Send the quit command to GDB and make sure it exits.
192 proc send_quit_command { test_name } {
193     global shell_prompt_re
195     set test $test_name
196     send_gdb "quit\n"
197     gdb_expect {
198         -re "(y or n)" {
199             send_gdb "y\n"
200             exp_continue
201         }
202         -re ".*$shell_prompt_re$" {
203             pass $test
204             return
205         }
206         timeout {
207             fail "$test (timeout)"
208             return 0
209         }
210         eof {
211             fail "$test (eof)"
212             return 0
213         }
214     }
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
219 # quitting.
221 proc test_terminal_settings_preserved_after_cli_exit { cmds } {
222     global file_arg
223     global GDB INTERNAL_GDBFLAGS GDBFLAGS
224     global gdb_prompt
225     global shell_prompt_re
227     if ![spawn_shell] {
228         return
229     }
231     set stty_supported [run_stty "stty before" stty_before]
233     set test "start gdb"
234     send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts] --args \"$file_arg\"\n"
235     gdb_expect {
236         -re "$gdb_prompt $" {
237             pass $test
238         }
239         timeout {
240             fail "$test (timeout)"
241         }
242         eof {
243             fail "$test (eof)"
244         }
245     }
247     foreach cmd $cmds {
248         set test "run command $cmd"
249         send_gdb "$cmd\n"
250         gdb_expect {
251             -re "$gdb_prompt $" {
252                 pass $test
253             }
254             timeout {
255                 fail "$test (timeout)"
256             }
257             eof {
258                 fail "$test (eof)"
259             }
260         }
261     }
263     send_quit_command "quit gdb"
265     set test "terminal settings preserved"
266     if $stty_supported {
267         run_stty "stty after" stty_after
269         gdb_assert [string equal $stty_before $stty_after] $test
270     } else {
271         unsupported "$test (no stty)"
272     }
274     exit_shell
277 # Check that sending SIGTERM kills GDB and does not leave the terminal in the
278 # wrong state.
280 proc test_terminal_settings_preserved_after_sigterm { } {
281     global file_arg
282     global GDB INTERNAL_GDBFLAGS GDBFLAGS
283     global gdb_prompt
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*] {
290         return
291     }
293     if ![spawn_shell] {
294         return
295     }
297     set stty_supported [run_stty "stty before" stty_before]
299     set test "start gdb"
300     send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]\n"
301     gdb_expect {
302         -re "$gdb_prompt $" {
303             pass $test
304         }
305         timeout {
306             fail "$test (timeout)"
307         }
308         eof {
309             fail "$test (eof)"
310         }
311     }
313     # Retrieve the pid of gdb with the gdb command "shell echo $PPID"
314     set gdb_pid -1
315     set test "run shell echo \$PPID"
316     send_gdb "shell echo \$PPID\n"
317     gdb_expect {
318         -re ".*\r\n(\\d+)\r\n$gdb_prompt $" {
319             set gdb_pid $expect_out(1,string)
320             pass $test
321         }
322         -re ".*\r\n\r\n$gdb_prompt $" {
323             fail "$test (no \$PPID)"
324         }
325         timeout {
326             fail "$test (timeout)"
327         }
328         eof {
329             fail "$test (eof)"
330         }
331     }
333     set test "kill gdb with SIGTERM"
334     if { $gdb_pid == -1 } {
335         fail "$test (no pid)"
336         send_quit_command "quit gdb"
337     } else {
338         remote_exec host "kill -TERM $gdb_pid"
339         set gdb_killed 0
340         gdb_expect {
341             -re ".*$shell_prompt_re$" {
342                 pass $test
343                 set gdb_killed 1
344             }
345             default {
346                 fail "$test (did not quit)"
347             }
348         }
350         if !$gdb_killed {
351             send_quit_command "quit gdb"
352         }
353     }
355     set test "terminal settings preserved"
356     if $stty_supported {
357         run_stty "stty after" stty_after
359         gdb_assert [string equal $stty_before $stty_after] $test
360     } else {
361         unsupported "$test (no stty)"
362     }
364     exit_shell
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