]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/batch-preserve-term-settings.exp
GDB copyright headers update after running GDB's copyright.py script.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / batch-preserve-term-settings.exp
1 # Copyright (C) 2015-2016 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.
15
16 # Check that "gdb -batch -ex run" does not leave the terminal in the
17 # wrong state.
18
19 standard_testfile
20
21 if {[build_executable "failed to prepare" $testfile $srcfile debug] == -1} {
22 return -1
23 }
24
25 set file_arg $binfile
26 if [is_remote host] {
27 set file_arg [remote_download host $file_arg]
28 }
29
30 # The shell's prompt.
31 set shell_prompt "$ "
32 set shell_prompt_re [string_to_regexp $shell_prompt]
33
34 # Spawn shell. Returns true on success, false otherwise.
35
36 proc spawn_shell {} {
37 global shell_prompt_re
38
39 set res [remote_spawn host "/bin/sh"]
40 if { $res < 0 || $res == "" } {
41 unsupported "Spawning shell failed."
42 return 0
43 }
44
45 set gotit 0
46 set test "spawn shell"
47 gdb_expect {
48 -re "$shell_prompt_re$" {
49 pass $test
50 set gotit 1
51 }
52 timeout {
53 fail "$test (timeout)"
54 }
55 eof {
56 fail "$test (eof)"
57 }
58 }
59
60 return $gotit
61 }
62
63 # Exit the shell.
64
65 proc exit_shell {} {
66 global shell_prompt_re
67
68 set test "exit shell"
69 send_gdb "exit\n"
70 gdb_expect {
71 timeout {
72 fail "$test (timeout)"
73 return 0
74 }
75 eof {
76 pass "$test"
77 }
78 }
79 if ![is_remote host] {
80 remote_close host
81 }
82 }
83
84 # Run "stty" and store the output in $result. Returns true on
85 # success, false otherwise.
86
87 proc run_stty {message result} {
88 global shell_prompt_re
89
90 upvar $result output
91
92 send_gdb "stty || echo \"not found\"\n"
93 set gotit 0
94 gdb_expect {
95 -re "not found.*not found.*$shell_prompt_re$" {
96 pass "$message (not found)"
97 }
98 -re "(.*)$shell_prompt_re$" {
99 set output $expect_out(1,string)
100 set gotit 1
101 pass $message
102 }
103 timeout {
104 fail "$message (timeout)"
105 }
106 eof {
107 fail "$message (eof)"
108 }
109 }
110 return $gotit
111 }
112
113 # Check that "gdb -batch -ex run" does not leave the terminal in the
114 # wrong state.
115
116 proc test_terminal_settings_preserved {} {
117 global file_arg
118 global GDB INTERNAL_GDBFLAGS GDBFLAGS
119 global gdb_prompt
120 global shell_prompt_re
121
122 if ![spawn_shell] {
123 return
124 }
125
126 set stty_supported [run_stty "stty before" stty_before]
127
128 set test "gdb -batch -ex run"
129 append EXTRA_GDBFLAGS "-batch"
130 append EXTRA_GDBFLAGS " -ex \"set height unlimited\""
131 append EXTRA_GDBFLAGS " -ex \"start\""
132 append EXTRA_GDBFLAGS " --args \"$file_arg\""
133 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS $EXTRA_GDBFLAGS [host_info gdb_opts]\n"
134 gdb_expect {
135 -re "Don't know how to run.*$shell_prompt_re$" {
136 unsupported $test
137 }
138 -re "$gdb_prompt $" {
139 # -batch implies no GDB prompt.
140 fail $test
141 }
142 -re "Temporary breakpoint .*$shell_prompt_re$" {
143 pass $test
144 }
145 timeout {
146 fail "$test (timeout)"
147 }
148 eof {
149 fail "$test (eof)"
150 }
151 }
152
153 set test "echo test_echo"
154 send_gdb "echo test_echo\n"
155 gdb_expect {
156 -re "^echo test_echo\r\ntest_echo\r\n.*$shell_prompt_re$" {
157 pass $test
158 }
159 timeout {
160 fail "$test (timeout)"
161 }
162 eof {
163 fail "$test (eof)"
164 }
165 }
166
167 set test "terminal settings preserved"
168 if $stty_supported {
169 run_stty "stty after" stty_after
170
171 gdb_assert [string equal $stty_before $stty_after] $test
172 } else {
173 unsupported "$test (no stty)"
174 }
175
176 exit_shell
177 }
178
179 # Send the quit command to GDB and make sure it exits.
180
181 proc send_quit_command { test_name } {
182 global shell_prompt_re
183
184 set test $test_name
185 send_gdb "quit\n"
186 gdb_expect {
187 -re "(y or n)" {
188 send_gdb "y\n"
189 exp_continue
190 }
191 -re ".*$shell_prompt_re$" {
192 pass $test
193 return
194 }
195 timeout {
196 fail "$test (timeout)"
197 return 0
198 }
199 eof {
200 fail "$test (eof)"
201 return 0
202 }
203 }
204 }
205
206 # Check that quitting from the CLI via the "quit" command does not leave the
207 # terminal in the wrong state. The GDB commands CMDS are executed before
208 # quitting.
209
210 proc test_terminal_settings_preserved_after_cli_exit { cmds } {
211 global file_arg
212 global GDB INTERNAL_GDBFLAGS GDBFLAGS
213 global gdb_prompt
214 global shell_prompt_re
215
216 if ![spawn_shell] {
217 return
218 }
219
220 set saved_gdbflags $GDBFLAGS
221
222 set stty_supported [run_stty "stty before" stty_before]
223
224 set test "start gdb"
225 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts] --args \"$file_arg\"\n"
226 gdb_expect {
227 -re "$gdb_prompt $" {
228 pass $test
229 }
230 timeout {
231 fail "$test (timeout)"
232 }
233 eof {
234 fail "$test (eof)"
235 }
236 }
237
238 foreach cmd $cmds {
239 set test "run command $cmd"
240 send_gdb "$cmd\n"
241 gdb_expect {
242 -re "$gdb_prompt $" {
243 pass $test
244 }
245 timeout {
246 fail "$test (timeout)"
247 }
248 eof {
249 fail "$test (eof)"
250 }
251 }
252 }
253
254 send_quit_command "quit gdb"
255
256 set test "terminal settings preserved"
257 if $stty_supported {
258 run_stty "stty after" stty_after
259
260 gdb_assert [string equal $stty_before $stty_after] $test
261 } else {
262 unsupported "$test (no stty)"
263 }
264
265 exit_shell
266 }
267
268 # Check that sending SIGTERM kills GDB and does not leave the terminal in the
269 # wrong state.
270
271 proc test_terminal_settings_preserved_after_sigterm { } {
272 global file_arg
273 global GDB INTERNAL_GDBFLAGS GDBFLAGS
274 global gdb_prompt
275 global shell_prompt_re
276
277 if ![spawn_shell] {
278 return
279 }
280
281 set saved_gdbflags $GDBFLAGS
282
283 set stty_supported [run_stty "stty before" stty_before]
284
285 set test "start gdb"
286 send_gdb "$GDB $INTERNAL_GDBFLAGS $GDBFLAGS [host_info gdb_opts]\n"
287 gdb_expect {
288 -re "$gdb_prompt $" {
289 pass $test
290 }
291 timeout {
292 fail "$test (timeout)"
293 }
294 eof {
295 fail "$test (eof)"
296 }
297 }
298
299 # Retrieve the pid of gdb with the gdb command "shell echo $PPID"
300 set gdb_pid -1
301 set test "run shell echo \$PPID"
302 send_gdb "shell echo \$PPID\n"
303 gdb_expect {
304 -re ".*\r\n(\\d+)\r\n$gdb_prompt $" {
305 set gdb_pid $expect_out(1,string)
306 pass $test
307 }
308 -re ".*\r\n\r\n$gdb_prompt $" {
309 fail "$test (no \$PPID)"
310 }
311 timeout {
312 fail "$test (timeout)"
313 }
314 eof {
315 fail "$test (eof)"
316 }
317 }
318
319 set test "kill gdb with SIGTERM"
320 if { $gdb_pid == -1 } {
321 fail "$test (no pid)"
322 send_quit_command "quit gdb"
323 } else {
324 remote_exec host "kill -TERM $gdb_pid"
325 set gdb_killed 0
326 gdb_expect {
327 -re ".*$shell_prompt_re$" {
328 pass $test
329 set gdb_killed 1
330 }
331 default {
332 fail "$test (did not quit)"
333 }
334 }
335
336 if !$gdb_killed {
337 send_quit_command "quit gdb"
338 }
339 }
340
341 set test "terminal settings preserved"
342 if $stty_supported {
343 run_stty "stty after" stty_after
344
345 gdb_assert [string equal $stty_before $stty_after] $test
346 } else {
347 unsupported "$test (no stty)"
348 }
349
350 exit_shell
351 }
352
353 with_test_prefix "batch run" {
354 test_terminal_settings_preserved
355 }
356
357 with_test_prefix "cli exit" {
358 test_terminal_settings_preserved_after_cli_exit { }
359 }
360
361 with_test_prefix "cli exit after start cmd" {
362 test_terminal_settings_preserved_after_cli_exit { "start" }
363 }
364
365 with_test_prefix "cli exit after run cmd" {
366 test_terminal_settings_preserved_after_cli_exit { "run" }
367 }
368
369 with_test_prefix "cli exit after SIGTERM" {
370 test_terminal_settings_preserved_after_sigterm
371 }