]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/foll-exec.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-exec.exp
CommitLineData
213516ef 1# Copyright 1997-2023 Free Software Foundation, Inc.
74cf1395
JM
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
74cf1395 6# (at your option) any later version.
e22f8b7c 7#
74cf1395
JM
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.
e22f8b7c 12#
74cf1395 13# You should have received a copy of the GNU General Public License
e85a822c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
74cf1395 15
2fd33e94
DB
16# This is a test of gdb's ability to follow a process through a
17# Unix exec() system call.
18
2d4e0376
YQ
19# Until "catch exec" is implemented on other targets...
20#
65a33d75 21if {![istarget "*-linux*"]} {
cdd42066 22 return
2d4e0376 23}
74cf1395 24
289f9037
TT
25standard_testfile foll-exec.c
26
74cf1395 27set testfile2 "execd-prog"
74cf1395 28set srcfile2 ${testfile2}.c
289f9037 29set binfile2 [standard_output_file ${testfile2}]
74cf1395 30
3fef966c 31set compile_options debug
3fef966c 32
74cf1395 33# build the first test case
3fef966c 34if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } {
5b362f04 35 untested "failed to compile"
b60f0898 36 return -1
74cf1395
JM
37}
38
b108c563
TV
39if { [is_remote target] } {
40 gdb_remote_download target $binfile2
41}
42
3fef966c 43if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } {
5b362f04 44 untested "failed to compile"
b60f0898 45 return -1
74cf1395
JM
46}
47
74cf1395 48proc do_exec_tests {} {
2fd33e94 49 global binfile srcfile srcfile2 testfile testfile2
74cf1395 50 global gdb_prompt
74cf1395
JM
51
52 # Start the program running, and stop at main.
53 #
65a33d75 54 if {![runto_main]} {
74cf1395
JM
55 return
56 }
57
e85a822c
DJ
58 # Verify that the system supports "catch exec".
59 gdb_test "catch exec" "Catchpoint \[0-9\]* \\(exec\\)" "insert first exec catchpoint"
60 set has_exec_catchpoints 0
61 gdb_test_multiple "continue" "continue to first exec catchpoint" {
77b06cd7 62 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
e85a822c
DJ
63 unsupported "continue to first exec catchpoint"
64 }
65 -re ".*Catchpoint.*$gdb_prompt $" {
66 set has_exec_catchpoints 1
67 pass "continue to first exec catchpoint"
68 }
69 }
70
71 if {$has_exec_catchpoints == 0} {
72 unsupported "exec catchpoints"
73 return
74 }
75
2fd33e94 76 clean_restart $binfile
e85a822c
DJ
77
78 # Start the program running, and stop at main.
79 #
65a33d75 80 if {![runto_main]} {
e85a822c
DJ
81 return
82 }
c2b75043
LM
83
84 with_test_prefix "1st run" {
85 # Execute the code setting up variable PROG.
86 set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile]
87 gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint"
88 gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*"
89 }
e85a822c 90
74cf1395
JM
91 # Verify that we can see various global and local variables
92 # in this program, and that they have expected values. Some
93 # of these variables are also declared in the program we'll
94 # exec in a moment.
95 #
31d913c7 96
74cf1395
JM
97 send_gdb "print global_i\n"
98 gdb_expect {
99 -re ".* = 100.*$gdb_prompt $"\
100 {pass "print follow-exec/global_i"}
101 -re "$gdb_prompt $" {fail "print follow-exec/global_i"}
102 timeout {fail "(timeout) print follow-exec/global_i"}
103 }
104 send_gdb "print local_j\n"
105 gdb_expect {
106 -re ".* = 101.*$gdb_prompt $"\
107 {pass "print follow-exec/local_j"}
108 -re "$gdb_prompt $" {fail "print follow-exec/local_j"}
109 timeout {fail "(timeout) print follow-exec/local_j"}
110 }
111 send_gdb "print local_k\n"
112 gdb_expect {
113 -re ".* = 102.*$gdb_prompt $"\
114 {pass "print follow-exec/local_k"}
115 -re "$gdb_prompt $" {fail "print follow-exec/local_k"}
116 timeout {fail "(timeout) print follow-exec/local_k"}
117 }
118
119 # Try stepping through an execlp call, without catching it.
120 # We should stop in execd-program, at its first statement.
121 #
58fa2af0 122 set execd_line [gdb_get_line_number "after-exec" $srcfile2]
74cf1395
JM
123 send_gdb "next\n"
124 gdb_expect {
58fa2af0 125 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
74cf1395
JM
126 {pass "step through execlp call"}
127 -re "$gdb_prompt $" {fail "step through execlp call"}
128 timeout {fail "(timeout) step through execlp call"}
129 }
130
131 # Verify that we can see the variables defined in the newly-exec'd
132 # program, and CANNOT see those defined in the exec'ing program.
133 #
134 send_gdb "next\n"
135 gdb_expect {
58fa2af0 136 -re "printf \\(.Hello .*$gdb_prompt $"\
74cf1395
JM
137 {pass "step after execlp call"}
138 -re "$gdb_prompt $" {fail "step after execlp call"}
139 timeout {fail "(timeout) step after execlp call"}
140 }
141 send_gdb "print global_i\n"
142 gdb_expect {
143 -re ".* = 0.*$gdb_prompt $"\
144 {pass "print execd-program/global_i (after execlp)"}
145 -re "$gdb_prompt $" {fail "print execd-program/global_i (after execlp)"}
146 timeout {fail "(timeout) print execd-program/global_i (after execlp)"}
147 }
148 send_gdb "print local_j\n"
149 gdb_expect {
150 -re ".* = 2.*$gdb_prompt $"\
151 {pass "print execd-program/local_j (after execlp)"}
152 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execlp)"}
153 timeout {fail "(timeout) print execd-program/local_j (after execlp)"}
154 }
155 send_gdb "print local_k\n"
156 gdb_expect {
157 -re "No symbol \"local_k\" in current context.*$gdb_prompt $"\
158 {pass "print follow-exec/local_k (after execlp)"}
159 -re "$gdb_prompt $" {fail "print follow-exec/local_k (after execlp)"}
160 timeout {fail "(timeout) print follow-exec/local_k (after execlp)"}
161 }
162
163 # Explicitly kill this program, or a subsequent rerun actually runs
164 # the exec'd program, not the original program...
2fd33e94 165 clean_restart $binfile
74cf1395
JM
166
167 # Start the program running, and stop at main.
168 #
65a33d75 169 if {![runto_main]} {
74cf1395
JM
170 return
171 }
172
173 # Verify that we can catch an exec event, and then continue
174 # to follow through the exec. (Since there's a breakpoint on
175 # "main", it'll also be transferred to the exec'd program,
176 # and we expect to stop there.)
177 #
178 send_gdb "catch exec\n"
179 gdb_expect {
180 -re "Catchpoint .*(exec).*$gdb_prompt $"\
181 {pass "set catch exec"}
182 -re "$gdb_prompt $" {fail "set catch exec"}
183 timeout {fail "(timeout) set catch exec"}
184 }
185
186 # Verify that the catchpoint is mentioned in an "info breakpoints",
187 # and further that the catchpoint mentions no program name.
188 #
462f405c
JB
189 set msg "info shows catchpoint without exec pathname"
190 gdb_test_multiple "info breakpoints" $msg {
191 -re ".*catchpoint.*keep y.*exec\[\n\r\]+$gdb_prompt $" {
192 pass $msg
193 }
74cf1395
JM
194 }
195
74cf1395
JM
196 send_gdb "continue\n"
197 gdb_expect {
b129b0ca 198 -re ".*xecuting new program:.*${testfile2}.*Catchpoint .*(exec\'d .*${testfile2}).*$gdb_prompt $"\
74cf1395
JM
199 {pass "hit catch exec"}
200 -re "$gdb_prompt $" {fail "hit catch exec"}
201 timeout {fail "(timeout) hit catch exec"}
202 }
203
204 # DTS CLLbs16760
205 # test gets out of sync if previous test fails.
206 gdb_test "bt" ".*" "sync up after possible failure 1"
207 gdb_test "bt" "#0.*" "sync up after possible failure 2"
208
209 # Verify that the catchpoint is mentioned in an "info breakpoints",
210 # and further that the catchpoint managed to capture the exec'd
211 # program's name.
212 #
462f405c
JB
213 set msg "info shows catchpoint exec pathname"
214 gdb_test_multiple "info breakpoints" $msg {
215 -re ".*catchpoint.*keep y.*exec, program \".*${testfile2}\".*$gdb_prompt $" {
216 pass $msg
217 }
74cf1395
JM
218 }
219
220 # Verify that we can continue from the catchpoint, and land in the
221 # main of the newly-exec'd program.
222 #
223 send_gdb "continue\n"
224 gdb_expect {
58fa2af0 225 -re ".*${srcfile2}:${execd_line}.*$gdb_prompt $"\
74cf1395
JM
226 {pass "continue after hit catch exec"}
227 -re "$gdb_prompt $" {fail "continue after hit catch exec"}
228 timeout {fail "(timeout) continue after hit catch exec"}
229 }
230
231 # Explicitly kill this program, or a subsequent rerun actually runs
232 # the exec'd program, not the original program...
2fd33e94 233 clean_restart $binfile
74cf1395
JM
234
235 # Start the program running, and stop at main.
236 #
65a33d75 237 if {![runto_main]} {
74cf1395
JM
238 return
239 }
c2b75043
LM
240
241 with_test_prefix "2nd run" {
242 # Execute the code setting up variable PROG.
243 set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile]
244 gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint"
245 gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*"
246 }
74cf1395
JM
247
248 # Verify that we can follow through follow an execl()
249 # call. (We must jump around earlier exec* calls.)
250 #
31d913c7 251 set tbreak_line [gdb_get_line_number " tbreak-execl " $srcfile]
58fa2af0 252 send_gdb "tbreak ${tbreak_line}\n"
74cf1395 253 gdb_expect {
58fa2af0 254 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
74cf1395
JM
255 {pass "prepare to jump to execl call"}
256 -re "$gdb_prompt $" {fail "prepare to jump to execl call"}
257 timeout {fail "(timeout) prepare to jump to execl call"}
258 }
58fa2af0 259 send_gdb "jump ${tbreak_line}\n"
74cf1395 260 gdb_expect {
58fa2af0 261 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
74cf1395
JM
262 {pass "jump to execl call"}
263 -re "$gdb_prompt $" {fail "jump to execl call"}
264 timeout {fail "(timeout) jump to execl call"}
265 }
266 # Note that stepping through an exec call causes the step-count
267 # to be reset to zero. I.e.: you may specify "next 2" at the
268 # call, but you'll actually stop at the first breakpoint set in
269 # the newly-exec'd program, not after the remaining step-count
270 # reaches zero.
271 #
272 send_gdb "next 2\n"
273 gdb_expect {
58fa2af0 274 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
74cf1395
JM
275 {pass "step through execl call"}
276 -re "$gdb_prompt $" {fail "step through execl call"}
277 timeout {fail "(timeout) step through execl call"}
278 }
279 send_gdb "next\n"
280 gdb_expect {
58fa2af0 281 -re "printf \\(.Hello .*$gdb_prompt $"\
74cf1395
JM
282 {pass "step after execl call"}
283 -re "$gdb_prompt $" {fail "step after execl call"}
284 timeout {fail "(timeout) step after execl call"}
285 }
286
287 # Verify that we can print a local variable (which happens to be
288 # assigned the value of main's argc).
289 #
290 send_gdb "print local_j\n"
291 gdb_expect {
292 -re ".* = 3.*$gdb_prompt $"\
293 {pass "print execd-program/local_j (after execl)"}
294 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execl)"}
295 timeout {fail "(timeout) print execd-program/local_j (after execl)"}
296 }
297
298 # Explicitly kill this program, or a subsequent rerun actually runs
299 # the exec'd program, not the original program...
2fd33e94 300 clean_restart $binfile
74cf1395
JM
301
302 # Start the program running, and stop at main.
303 #
65a33d75 304 if {![runto_main]} {
74cf1395
JM
305 return
306 }
c2b75043
LM
307
308 with_test_prefix "3rd run" {
309 # Execute the code setting up variable PROG.
310 set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile]
311 gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint"
312 gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*"
313 }
74cf1395
JM
314
315 # Verify that we can follow through follow an execv()
316 # call. (We must jump around earlier exec* calls.)
317 #
58fa2af0
AA
318 set tbreak_line [gdb_get_line_number "tbreak-execv"]
319 send_gdb "tbreak ${tbreak_line}\n"
74cf1395 320 gdb_expect {
58fa2af0 321 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
74cf1395
JM
322 {pass "prepare to jump to execv call"}
323 -re "$gdb_prompt $" {fail "prepare to jump to execv call"}
324 timeout {fail "(timeout) prepare to jump to execv call"}
325 }
58fa2af0 326 send_gdb "jump ${tbreak_line}\n"
74cf1395 327 gdb_expect {
58fa2af0 328 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
74cf1395
JM
329 {pass "jump to execv call"}
330 -re "$gdb_prompt $" {fail "jump to execv call"}
331 timeout {fail "(timeout) jump to execv call"}
332 }
333 send_gdb "next\n"
334 gdb_expect {
58fa2af0 335 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
74cf1395
JM
336 {pass "step through execv call"}
337 -re "$gdb_prompt $" {fail "step through execv call"}
338 timeout {fail "(timeout) step through execv call"}
339 }
340 send_gdb "next\n"
341 gdb_expect {
58fa2af0 342 -re "printf \\(.Hello .*$gdb_prompt $"\
74cf1395
JM
343 {pass "step after execv call"}
344 -re "$gdb_prompt $" {fail "step after execv call"}
345 timeout {fail "(timeout) step after execv call"}
346 }
347
348 # Verify that we can print a local variable (which happens to be
349 # assigned the value of main's argc).
350 #
351 send_gdb "print local_j\n"
352 gdb_expect {
353 -re ".* = 2.*$gdb_prompt $"\
354 {pass "print execd-program/local_j (after execv)"}
355 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execv)"}
356 timeout {fail "(timeout) print execd-program/local_j (after execv)"}
357 }
358
359 # Explicitly kill this program, or a subsequent rerun actually runs
360 # the exec'd program, not the original program...
2fd33e94 361 clean_restart $binfile
74cf1395
JM
362
363 # Start the program running, and stop at main.
364 #
65a33d75 365 if {![runto_main]} {
74cf1395
JM
366 return
367 }
368
369 # Verify that we can just continue and thereby follow through an
370 # exec call. (Since the breakpoint on "main" is reset, we should
371 # just stop in main of the newly-exec'd program.)
372 #
373 send_gdb "continue\n"
374 gdb_expect {
58fa2af0 375 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
74cf1395
JM
376 {pass "continue through exec"}
377 -re "$gdb_prompt $" {fail "continue through exec"}
378 timeout {fail "(timeout) continue through exec"}
379 }
380}
381
382# Start with a fresh gdb
383
384gdb_exit
2fd33e94 385clean_restart $binfile
74cf1395 386
74cf1395
JM
387do_exec_tests
388
389return 0