1 # Copyright 1997-2020 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 # This is a test of gdb's ability to follow a process through a
17 # Unix exec() system call.
19 # Until "catch exec" is implemented on other targets...
21 if { ![istarget "*-linux*"] } then {
25 standard_testfile foll-exec.c
27 set testfile2 "execd-prog"
28 set srcfile2 ${testfile2}.c
29 set binfile2 [standard_output_file ${testfile2}]
31 set compile_options debug
33 # build the first test case
34 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } {
35 untested "failed to compile"
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } {
40 untested "failed to compile"
44 proc do_exec_tests {} {
45 global binfile srcfile srcfile2 testfile testfile2
48 # Start the program running, and stop at main.
50 if ![runto_main] then {
51 fail "couldn't run ${testfile}"
55 # Verify that the system supports "catch exec".
56 gdb_test "catch exec" "Catchpoint \[0-9\]* \\(exec\\)" "insert first exec catchpoint"
57 set has_exec_catchpoints 0
58 gdb_test_multiple "continue" "continue to first exec catchpoint" {
59 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
60 unsupported "continue to first exec catchpoint"
62 -re ".*Catchpoint.*$gdb_prompt $" {
63 set has_exec_catchpoints 1
64 pass "continue to first exec catchpoint"
68 if {$has_exec_catchpoints == 0} {
69 unsupported "exec catchpoints"
73 clean_restart $binfile
75 # Start the program running, and stop at main.
77 if ![runto_main] then {
78 fail "couldn't run ${testfile}"
82 with_test_prefix "1st run" {
83 # Execute the code setting up variable PROG.
84 set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile]
85 gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint"
86 gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*"
89 # Verify that we can see various global and local variables
90 # in this program, and that they have expected values. Some
91 # of these variables are also declared in the program we'll
95 send_gdb "print global_i\n"
97 -re ".* = 100.*$gdb_prompt $"\
98 {pass "print follow-exec/global_i"}
99 -re "$gdb_prompt $" {fail "print follow-exec/global_i"}
100 timeout {fail "(timeout) print follow-exec/global_i"}
102 send_gdb "print local_j\n"
104 -re ".* = 101.*$gdb_prompt $"\
105 {pass "print follow-exec/local_j"}
106 -re "$gdb_prompt $" {fail "print follow-exec/local_j"}
107 timeout {fail "(timeout) print follow-exec/local_j"}
109 send_gdb "print local_k\n"
111 -re ".* = 102.*$gdb_prompt $"\
112 {pass "print follow-exec/local_k"}
113 -re "$gdb_prompt $" {fail "print follow-exec/local_k"}
114 timeout {fail "(timeout) print follow-exec/local_k"}
117 # Try stepping through an execlp call, without catching it.
118 # We should stop in execd-program, at its first statement.
120 set execd_line [gdb_get_line_number "after-exec" $srcfile2]
123 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
124 {pass "step through execlp call"}
125 -re "$gdb_prompt $" {fail "step through execlp call"}
126 timeout {fail "(timeout) step through execlp call"}
129 # Verify that we can see the variables defined in the newly-exec'd
130 # program, and CANNOT see those defined in the exec'ing program.
134 -re "printf \\(.Hello .*$gdb_prompt $"\
135 {pass "step after execlp call"}
136 -re "$gdb_prompt $" {fail "step after execlp call"}
137 timeout {fail "(timeout) step after execlp call"}
139 send_gdb "print global_i\n"
141 -re ".* = 0.*$gdb_prompt $"\
142 {pass "print execd-program/global_i (after execlp)"}
143 -re "$gdb_prompt $" {fail "print execd-program/global_i (after execlp)"}
144 timeout {fail "(timeout) print execd-program/global_i (after execlp)"}
146 send_gdb "print local_j\n"
148 -re ".* = 2.*$gdb_prompt $"\
149 {pass "print execd-program/local_j (after execlp)"}
150 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execlp)"}
151 timeout {fail "(timeout) print execd-program/local_j (after execlp)"}
153 send_gdb "print local_k\n"
155 -re "No symbol \"local_k\" in current context.*$gdb_prompt $"\
156 {pass "print follow-exec/local_k (after execlp)"}
157 -re "$gdb_prompt $" {fail "print follow-exec/local_k (after execlp)"}
158 timeout {fail "(timeout) print follow-exec/local_k (after execlp)"}
161 # Explicitly kill this program, or a subsequent rerun actually runs
162 # the exec'd program, not the original program...
163 clean_restart $binfile
165 # Start the program running, and stop at main.
167 if ![runto_main] then {
168 fail "couldn't run ${testfile} (2nd try)"
172 # Verify that we can catch an exec event, and then continue
173 # to follow through the exec. (Since there's a breakpoint on
174 # "main", it'll also be transferred to the exec'd program,
175 # and we expect to stop there.)
177 send_gdb "catch exec\n"
179 -re "Catchpoint .*(exec).*$gdb_prompt $"\
180 {pass "set catch exec"}
181 -re "$gdb_prompt $" {fail "set catch exec"}
182 timeout {fail "(timeout) set catch exec"}
185 # Verify that the catchpoint is mentioned in an "info breakpoints",
186 # and further that the catchpoint mentions no program name.
188 set msg "info shows catchpoint without exec pathname"
189 gdb_test_multiple "info breakpoints" $msg {
190 -re ".*catchpoint.*keep y.*exec\[\n\r\]+$gdb_prompt $" {
195 send_gdb "continue\n"
197 -re ".*xecuting new program:.*${testfile2}.*Catchpoint .*(exec\'d .*${testfile2}).*$gdb_prompt $"\
198 {pass "hit catch exec"}
199 -re "$gdb_prompt $" {fail "hit catch exec"}
200 timeout {fail "(timeout) hit catch exec"}
204 # test gets out of sync if previous test fails.
205 gdb_test "bt" ".*" "sync up after possible failure 1"
206 gdb_test "bt" "#0.*" "sync up after possible failure 2"
208 # Verify that the catchpoint is mentioned in an "info breakpoints",
209 # and further that the catchpoint managed to capture the exec'd
212 set msg "info shows catchpoint exec pathname"
213 gdb_test_multiple "info breakpoints" $msg {
214 -re ".*catchpoint.*keep y.*exec, program \".*${testfile2}\".*$gdb_prompt $" {
219 # Verify that we can continue from the catchpoint, and land in the
220 # main of the newly-exec'd program.
222 send_gdb "continue\n"
224 -re ".*${srcfile2}:${execd_line}.*$gdb_prompt $"\
225 {pass "continue after hit catch exec"}
226 -re "$gdb_prompt $" {fail "continue after hit catch exec"}
227 timeout {fail "(timeout) continue after hit catch exec"}
230 # Explicitly kill this program, or a subsequent rerun actually runs
231 # the exec'd program, not the original program...
232 clean_restart $binfile
234 # Start the program running, and stop at main.
236 if ![runto_main] then {
237 fail "couldn't run ${testfile} (3rd try)"
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 \\(.*"
248 # Verify that we can follow through follow an execl()
249 # call. (We must jump around earlier exec* calls.)
251 set tbreak_line [gdb_get_line_number " tbreak-execl " $srcfile]
252 send_gdb "tbreak ${tbreak_line}\n"
254 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
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"}
259 send_gdb "jump ${tbreak_line}\n"
261 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
262 {pass "jump to execl call"}
263 -re "$gdb_prompt $" {fail "jump to execl call"}
264 timeout {fail "(timeout) jump to execl call"}
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
274 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
275 {pass "step through execl call"}
276 -re "$gdb_prompt $" {fail "step through execl call"}
277 timeout {fail "(timeout) step through execl call"}
281 -re "printf \\(.Hello .*$gdb_prompt $"\
282 {pass "step after execl call"}
283 -re "$gdb_prompt $" {fail "step after execl call"}
284 timeout {fail "(timeout) step after execl call"}
287 # Verify that we can print a local variable (which happens to be
288 # assigned the value of main's argc).
290 send_gdb "print local_j\n"
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)"}
298 # Explicitly kill this program, or a subsequent rerun actually runs
299 # the exec'd program, not the original program...
300 clean_restart $binfile
302 # Start the program running, and stop at main.
304 if ![runto_main] then {
305 fail "couldn't run ${testfile} (4th try)"
309 with_test_prefix "3rd run" {
310 # Execute the code setting up variable PROG.
311 set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile]
312 gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint"
313 gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*"
316 # Verify that we can follow through follow an execv()
317 # call. (We must jump around earlier exec* calls.)
319 set tbreak_line [gdb_get_line_number "tbreak-execv"]
320 send_gdb "tbreak ${tbreak_line}\n"
322 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
323 {pass "prepare to jump to execv call"}
324 -re "$gdb_prompt $" {fail "prepare to jump to execv call"}
325 timeout {fail "(timeout) prepare to jump to execv call"}
327 send_gdb "jump ${tbreak_line}\n"
329 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
330 {pass "jump to execv call"}
331 -re "$gdb_prompt $" {fail "jump to execv call"}
332 timeout {fail "(timeout) jump to execv call"}
336 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
337 {pass "step through execv call"}
338 -re "$gdb_prompt $" {fail "step through execv call"}
339 timeout {fail "(timeout) step through execv call"}
343 -re "printf \\(.Hello .*$gdb_prompt $"\
344 {pass "step after execv call"}
345 -re "$gdb_prompt $" {fail "step after execv call"}
346 timeout {fail "(timeout) step after execv call"}
349 # Verify that we can print a local variable (which happens to be
350 # assigned the value of main's argc).
352 send_gdb "print local_j\n"
354 -re ".* = 2.*$gdb_prompt $"\
355 {pass "print execd-program/local_j (after execv)"}
356 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execv)"}
357 timeout {fail "(timeout) print execd-program/local_j (after execv)"}
360 # Explicitly kill this program, or a subsequent rerun actually runs
361 # the exec'd program, not the original program...
362 clean_restart $binfile
364 # Start the program running, and stop at main.
366 if ![runto_main] then {
367 fail "couldn't run ${testfile} (5th try)"
371 # Verify that we can just continue and thereby follow through an
372 # exec call. (Since the breakpoint on "main" is reset, we should
373 # just stop in main of the newly-exec'd program.)
375 send_gdb "continue\n"
377 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
378 {pass "continue through exec"}
379 -re "$gdb_prompt $" {fail "continue through exec"}
380 timeout {fail "(timeout) continue through exec"}
384 # Start with a fresh gdb
387 clean_restart $binfile