]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/foll-exec-mode.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-exec-mode.exp
CommitLineData
1d506c26 1# Copyright 1997-2024 Free Software Foundation, Inc.
8d37573b
DB
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# This is a test of gdb's follow-exec-mode.
17#
18# It first checks that exec events are supported by using a catchpoint,
19# then tests multiple scenarios for follow-exec-mode using parameters
20# that test:
21# - each mode
22# - different commands to execute past the exec
23# - re-running both the original and new inferiors.
24#
25# Note that we can't single-step past an exec call. There has to
26# be a breakpoint in order to stop after the exec, even if we use
27# a single-step command to execute past the exec.
28
a8f077dc
DB
29# Remote mode doesn't support the 'run' command, which is
30# required for follow-exec-mode testing.
31if { [target_info exists gdb_protocol]
32 && [target_info gdb_protocol] == "remote" } {
cdd42066 33 return
8d37573b
DB
34}
35
36# Until "catch exec" is implemented on other targets...
37#
24d59b55 38require {istarget "*-linux*"}
8d37573b
DB
39
40standard_testfile foll-exec-mode.c
41
42set testfile2 "execd-prog"
43set srcfile2 ${testfile2}.c
44set binfile2 [standard_output_file ${testfile2}]
45
46set compile_options debug
8d37573b
DB
47
48# build the first test case
49if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } {
5b362f04 50 untested "failed to compile"
8d37573b
DB
51 return -1
52}
53
54if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } {
5b362f04 55 untested "failed to compile"
8d37573b
DB
56 return -1
57}
58
59# Test exec catchpoints to ensure exec events are supported.
60#
61proc do_catch_exec_test { } {
62 global testfile
63 global gdb_prompt
64
65 clean_restart $testfile
66
67 # Start the program running, and stop at main.
68 #
65a33d75 69 if {![runto_main]} {
8d37573b
DB
70 return
71 }
72
73 # Verify that the system supports "catch exec".
74 gdb_test "catch exec" "Catchpoint \[0-9\]* \\(exec\\)" "insert first exec catchpoint"
75 set has_exec_catchpoints 0
76 gdb_test_multiple "continue" "continue to first exec catchpoint" {
77 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
78 unsupported "continue to first exec catchpoint"
79 }
80 -re ".*Catchpoint.*$gdb_prompt $" {
81 set has_exec_catchpoints 1
82 pass "continue to first exec catchpoint"
83 }
84 }
85
86 if {$has_exec_catchpoints == 0} {
87 unsupported "exec catchpoints"
88 return
89 }
90}
91
92# Test follow-exec-mode in the specified scenario.
93# MODE determines whether follow-exec-mode is "same" or "new".
94# CMD determines the command used to execute past the exec call.
95# INFSWITCH is ignored for MODE == "same", and for "new" it is
96# used to determine whether to switch to the original inferior
97# before re-running.
98
99proc do_follow_exec_mode_tests { mode cmd infswitch } {
78805ff8 100 global binfile srcfile srcfile2 testfile testfile2 bkptno_numopt_re
8d37573b
DB
101 global gdb_prompt
102
103 with_test_prefix "$mode,$cmd,$infswitch" {
104 clean_restart $testfile
105
106 # Start the program running, and stop at main.
107 #
65a33d75 108 if {![runto_main]} {
8d37573b
DB
109 return
110 }
111
737358ba
SM
112 set target_is_native [gdb_is_target_native]
113
8d37573b
DB
114 # Set the follow-exec mode.
115 #
116 gdb_test_no_output "set follow-exec-mode $mode"
117
118 # Run to the line of the exec call.
119 #
120 gdb_breakpoint [gdb_get_line_number "Set breakpoint here"]
121 gdb_continue_to_breakpoint "continue to line of exec call"
122
123 # Set up the output we expect to see after we execute past the exec.
124 #
125 set execd_line [gdb_get_line_number "after-exec" $srcfile2]
78805ff8 126 set expected_re ".*xecuting new program: .*${testfile2}.*Breakpoint ${bkptno_numopt_re},.*${srcfile2}:${execd_line}.*$gdb_prompt $"
8d37573b
DB
127
128 # Set a breakpoint after the exec call if we aren't single-stepping
129 # past it.
130 #
131 if {$cmd == "continue"} {
132 gdb_breakpoint "$execd_line"
133 }
134
135 # Execute past the exec call.
136 #
137 set test "$cmd past exec"
138 gdb_test_multiple $cmd $test {
139 -re "$expected_re" {
140 pass $test
141 }
142 }
143
144 # Set expected output, given the test parameters.
145 #
146 if {$mode == "same"} {
147 set expected_re "\\* 1.*process.*"
737358ba
SM
148 } elseif { $mode == "new" } {
149 # If using the native target, we want to specifically check that the
150 # process target, which was automatically pushed when running, was
151 # automatically unpushed from inferior 1 on exec. Use a
152 # different regexp that verifies the Connection field is empty.
153 if { $target_is_native } {
154 set expected_re " 1.*<null> +[string_to_regexp $binfile].*\r\n\\* 2.*process.*$testfile2 .*"
155 } else {
156 set expected_re " 1.*null.*$testfile.*\r\n\\* 2.*process.*$testfile2 .*"
157 }
8d37573b 158 } else {
737358ba 159 error "Invalid mode: $mode"
8d37573b
DB
160 }
161
162 # Check that the inferior list is correct:
163 # - one inferior for MODE == "same"
164 # - two inferiors for MODE == "new", current is execd program
165 #
166 gdb_test "info inferiors" $expected_re "Check inferior list"
167
168 set expected_inf ""
169 if {$mode == "same"} {
170 # One inferior, the execd program.
171 set expected_inf $testfile2
172 } elseif {$infswitch == "infswitch"} {
173 # Two inferiors, we have switched to the original program.
174 set expected_inf $testfile
cdc7edd7 175 gdb_test "inferior 1" "Switching to inferior 1.*$testfile.*" "switch inferiors"
8d37573b
DB
176 } else {
177 # Two inferiors, run the execd program
178 set expected_inf $testfile2
179 }
180
181 # Now check that a 'run' command will run the correct inferior.
182 #
183 set test "use correct executable ($expected_inf) for run after follow exec"
184 gdb_run_cmd
185 gdb_test_multiple "" $test {
186 -re {Start it from the beginning\? \(y or n\) $} {
187 send_gdb "y\n"
188 exp_continue
189 }
78805ff8 190 -re "Starting program: .*$expected_inf.*Breakpoint $bkptno_numopt_re,.*\r\n$gdb_prompt $" {
8d37573b
DB
191 pass $test
192 }
193 }
194 }
195}
196
197do_catch_exec_test
198
199foreach cmd {"next" "continue"} {
200 foreach mode {"same" "new"} {
201 # Test basic follow-exec-mode.
202 do_follow_exec_mode_tests $mode $cmd "no_infswitch"
203 if {$mode == "new"} {
204 # Test that when we do 'run' we get the correct executable.
205 do_follow_exec_mode_tests $mode $cmd "infswitch"
206 }
207 }
208}