Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.multi / multi-term-settings.exp
bloba69ed5b76eb5ec17a27baf74986cc9a22b6c4742
1 # This testcase is part of GDB, the GNU debugger.
3 # Copyright 2017-2023 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 # This testcase exercises GDB's terminal ownership management
19 # (terminal_ours/terminal_inferior, etc.) when debugging multiple
20 # inferiors.  It tests debugging multiple inferiors started with
21 # different combinations of 'run'/'run+tty'/'attach', and ensures that
22 # the process that is sharing GDB's terminal/session is properly made
23 # the GDB terminal's foregound process group (i.e., "given the
24 # terminal").
26 standard_testfile
28 if ![can_spawn_for_attach] {
29     return 0
32 if [build_executable "failed to prepare" $testfile $srcfile {debug}] {
33     return -1
36 # Start the programs running and then wait for a bit, to be sure that
37 # they can be attached to.  We start these processes upfront in order
38 # to reuse them for the different iterations.  This makes the testcase
39 # faster, compared to calling spawn_wait_for_attach for each iteration
40 # in the testing matrix.
42 set spawn_id_list [spawn_wait_for_attach [list $binfile $binfile]]
43 set attach_spawn_id1 [lindex $spawn_id_list 0]
44 set attach_spawn_id2 [lindex $spawn_id_list 1]
45 set attach_pid1 [spawn_id_get_pid $attach_spawn_id1]
46 set attach_pid2 [spawn_id_get_pid $attach_spawn_id2]
48 # Create inferior WHICH_INF.  INF_HOW is how to create it.  This can
49 # be one of:
51 #  - "run" - for "run" command.
52 #  - "tty" - for "run" + "tty TTY".
53 #  - "attach" - for attaching to an existing process.
54 proc create_inferior {which_inf inf_how} {
55     global binfile
57     # Run to main and delete breakpoints.
58     proc my_runto_main {} {
59         if ![runto_main] {
60             return 0
61         } else {
62             # Delete breakpoints otherwise GDB would try to step over
63             # the breakpoint at 'main' without resuming the other
64             # inferior, possibly masking the issue we're trying to
65             # test.
66             delete_breakpoints
67             return 1
68         }
69     }
71     if {$inf_how == "run"} {
72         if [my_runto_main] {
73             global inferior_spawn_id
74             return $inferior_spawn_id
75         }
76     } elseif {$inf_how == "tty"} {
77         # Create the new PTY for the inferior.
78         spawn -pty
79         set inf_spawn_id $spawn_id
80         set inf_tty_name $spawn_out(slave,name)
81         gdb_test_no_output "tty $inf_tty_name" "tty TTY"
83         if [my_runto_main] {
84             return $inf_spawn_id
85         }
86     } elseif {$inf_how == "attach"} {
88         # Reuse the inferiors spawned at the start of the testcase (to
89         # avoid having to wait the delay imposed by
90         # spawn_wait_for_attach).
91         global attach_spawn_id$which_inf
92         set test_spawn_id [set attach_spawn_id$which_inf]
93         set testpid [spawn_id_get_pid $test_spawn_id]
94         if {[gdb_test "attach $testpid" \
95                  "Attaching to program: .*, process $testpid.*(in|at).*" \
96                  "attach"] == 0} {
98             # The program is now stopped, but if testing against
99             # gdbserver, then the inferior's output emmitted before it
100             # stopped isn't flushed unless we explicitly do so,
101             # because it is on a different spawn_id.  Do it now, to
102             # avoid confusing tests further below.
103             gdb_test_multiple "" "flush inferior output" {
104                 -timeout 1
105                 -i $test_spawn_id -re "pid=" {
106                     exp_continue
107                 }
108                 timeout {
109                     pass $gdb_test_name
110                 }
111             }
113             return $test_spawn_id
114         }
115     } else {
116         error "unhandled inf_how: $inf_how"
117     }
119     return ""
122 # Print debug output.
124 proc send_debug {str} {
125     # Uncomment for debugging.
126     #send_user $str
129 # The core of the testcase.  See intro.  Creates two inferiors that
130 # both loop changing their terminal's settings and printing output,
131 # and checks whether they receive SIGTTOU.  INF1_HOW and INF2_HOW
132 # indicate how inferiors 1 and 2 should be created, respectively.  See
133 # 'create_inferior' above for what arguments these parameters accept.
135 proc coretest {inf1_how inf2_how} {
136     global binfile
137     global inferior_spawn_id
138     global gdb_spawn_id
139     global decimal
141     clean_restart $binfile
143     with_test_prefix "inf1" {
144         set inf1_spawn_id [create_inferior 1 $inf1_how]
145     }
147     set num 2
148     gdb_test "add-inferior" "Added inferior $num.*" \
149         "add empty inferior $num"
150     gdb_test "inferior $num" "Switching to inferior $num.*" \
151         "switch to inferior $num"
152     gdb_file_cmd ${binfile}
154     with_test_prefix "inf2" {
155         set inf2_spawn_id [create_inferior 2 $inf2_how]
156     }
158     gdb_test_no_output "set schedule-multiple on"
160     # "run" makes each inferior be a process group leader.  When we
161     # run both inferiors in GDB's terminal/session, only one can end
162     # up as the terminal's foreground process group, so it's expected
163     # that the other receives a SIGTTOU.
164     set expect_ttou [expr {$inf1_how == "run" && $inf2_how == "run"}]
166     global gdb_prompt
168     set any "\[^\r\n\]*"
170     set pid1 -1
171     set pid2 -1
173     set test "info inferiors"
174     gdb_test_multiple $test $test {
175         -re "1${any}process ($decimal)${any}\r\n" {
176             set pid1 $expect_out(1,string)
177             send_debug "pid1=$pid1\n"
178             exp_continue
179         }
180         -re "2${any}process ($decimal)${any}\r\n" {
181             set pid2 $expect_out(1,string)
182             send_debug "pid2=$pid2\n"
183             exp_continue
184         }
185         -re "$gdb_prompt $" {
186             pass $test
187         }
188     }
190     # Helper for the gdb_test_multiple call below.  Issues a PASS if
191     # we've seen each inferior print at least 3 times, otherwise
192     # continues waiting for inferior output.
193     proc pass_or_exp_continue {} {
194         uplevel 1 {
195             if {$count1 >= 3 && $count2 >= 3} {
196                 if $expect_ttou {
197                     fail "$gdb_test_name (expected SIGTTOU)"
198                 } else {
199                     pass $gdb_test_name
200                 }
201             } else {
202                 exp_continue
203             }
204         }
205     }
207     set infs_spawn_ids [list $inf1_spawn_id $inf2_spawn_id]
208     send_debug "infs_spawn_ids=$infs_spawn_ids\n"
210     set count1 0
211     set count2 0
213     # We're going to interrupt with Ctrl-C.  For this to work we must
214     # be sure to consume the "Continuing." message first, or GDB may
215     # still own the terminal.  Also, note that in the attach case, we
216     # flushed inferior output right after attaching, so that we're
217     # sure that the "pid=" lines we see are emitted by the inferior
218     # after it is continued, instead of having been emitted before it
219     # was attached to.
220     gdb_test_multiple "continue" "continue, hand over terminal" {
221         -re "Continuing" {
222             pass $gdb_test_name
223         }
224     }
226     gdb_test_multiple "" "continue" {
227         -i $infs_spawn_ids -re "pid=$pid1, count=" {
228             incr count1
229             pass_or_exp_continue
230         }
231         -i $infs_spawn_ids -re "pid=$pid2, count=" {
232             incr count2
233             pass_or_exp_continue
234         }
235         -i $gdb_spawn_id -re "received signal SIGTTOU.*$gdb_prompt " {
236             if $expect_ttou {
237                 pass "$gdb_test_name (expected SIGTTOU)"
238             } else {
239                 fail "$gdb_test_name (SIGTTOU)"
240             }
241         }
242     }
244     send_gdb "\003"
245     if {$expect_ttou} {
246         gdb_test "" "Quit" "stop with control-c (Quit)"
247     } else {
248         gdb_test "" "received signal SIGINT.*" "stop with control-c (SIGINT)"
249     }
251     # Useful for debugging in case the Ctrl-C above fails.
252     with_test_prefix "final" {
253         gdb_test "info inferiors"
254         gdb_test "info threads"
255     }
258 set how_modes {"run" "attach" "tty"}
260 foreach_with_prefix inf1_how $how_modes {
261     foreach_with_prefix inf2_how $how_modes {
262         if {($inf1_how == "tty" || $inf2_how == "tty")
263             && [target_info gdb_protocol] == "extended-remote"} {
264             # No way to specify the inferior's tty in the remote
265             # protocol.
266             unsupported "no support for \"tty\" in the RSP"
267             continue
268         }
270         coretest $inf1_how $inf2_how
271     }
274 kill_wait_spawned_process $attach_spawn_id1
275 kill_wait_spawned_process $attach_spawn_id2