1 # Copyright 1997-2024 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 require {istarget "*-linux*"}
23 standard_testfile foll-exec.c
25 set testfile2 "execd-prog"
26 set srcfile2 ${testfile2}.c
27 set binfile2 [standard_output_file ${testfile2}]
29 set compile_options debug
31 # build the first test case
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } {
33 untested "failed to compile"
37 if { [is_remote target] } {
38 gdb_remote_download target $binfile2
41 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } {
42 untested "failed to compile"
46 proc do_exec_tests {} {
47 global binfile srcfile srcfile2 testfile testfile2
50 # Start the program running, and stop at main.
56 # Verify that the system supports "catch exec".
57 gdb_test "catch exec" "Catchpoint \[0-9\]* \\(exec\\)" "insert first exec catchpoint"
58 set has_exec_catchpoints 0
59 gdb_test_multiple "continue" "continue to first exec catchpoint" {
60 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
61 unsupported "continue to first exec catchpoint"
63 -re ".*Catchpoint.*$gdb_prompt $" {
64 set has_exec_catchpoints 1
65 pass "continue to first exec catchpoint"
69 if {$has_exec_catchpoints == 0} {
70 unsupported "exec catchpoints"
74 clean_restart $binfile
76 # Start the program running, and stop at main.
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.
171 # Verify that we can catch an exec event, and then continue
172 # to follow through the exec. (Since there's a breakpoint on
173 # "main", it'll also be transferred to the exec'd program,
174 # and we expect to stop there.)
176 send_gdb "catch exec\n"
178 -re "Catchpoint .*(exec).*$gdb_prompt $"\
179 {pass "set catch exec"}
180 -re "$gdb_prompt $" {fail "set catch exec"}
181 timeout {fail "(timeout) set catch exec"}
184 # Verify that the catchpoint is mentioned in an "info breakpoints",
185 # and further that the catchpoint mentions no program name.
187 set msg "info shows catchpoint without exec pathname"
188 gdb_test_multiple "info breakpoints" $msg {
189 -re ".*catchpoint.*keep y.*exec\[\n\r\]+$gdb_prompt $" {
194 send_gdb "continue\n"
196 -re ".*xecuting new program:.*${testfile2}.*Catchpoint .*(exec\'d .*${testfile2}).*$gdb_prompt $"\
197 {pass "hit catch exec"}
198 -re "$gdb_prompt $" {fail "hit catch exec"}
199 timeout {fail "(timeout) hit catch exec"}
203 # test gets out of sync if previous test fails.
204 gdb_test "bt" ".*" "sync up after possible failure 1"
205 gdb_test "bt" "#0.*" "sync up after possible failure 2"
207 # Verify that the catchpoint is mentioned in an "info breakpoints",
208 # and further that the catchpoint managed to capture the exec'd
211 set msg "info shows catchpoint exec pathname"
212 gdb_test_multiple "info breakpoints" $msg {
213 -re ".*catchpoint.*keep y.*exec, program \".*${testfile2}\".*$gdb_prompt $" {
218 # Verify that we can continue from the catchpoint, and land in the
219 # main of the newly-exec'd program.
221 send_gdb "continue\n"
223 -re ".*${srcfile2}:${execd_line}.*$gdb_prompt $"\
224 {pass "continue after hit catch exec"}
225 -re "$gdb_prompt $" {fail "continue after hit catch exec"}
226 timeout {fail "(timeout) continue after hit catch exec"}
229 # Explicitly kill this program, or a subsequent rerun actually runs
230 # the exec'd program, not the original program...
231 clean_restart $binfile
233 # Start the program running, and stop at main.
239 with_test_prefix "2nd run" {
240 # Execute the code setting up variable PROG.
241 set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile]
242 gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint"
243 gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*"
246 # Verify that we can follow through follow an execl()
247 # call. (We must jump around earlier exec* calls.)
249 set tbreak_line [gdb_get_line_number " tbreak-execl " $srcfile]
250 send_gdb "tbreak ${tbreak_line}\n"
252 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
253 {pass "prepare to jump to execl call"}
254 -re "$gdb_prompt $" {fail "prepare to jump to execl call"}
255 timeout {fail "(timeout) prepare to jump to execl call"}
257 send_gdb "jump ${tbreak_line}\n"
259 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
260 {pass "jump to execl call"}
261 -re "$gdb_prompt $" {fail "jump to execl call"}
262 timeout {fail "(timeout) jump to execl call"}
264 # Note that stepping through an exec call causes the step-count
265 # to be reset to zero. I.e.: you may specify "next 2" at the
266 # call, but you'll actually stop at the first breakpoint set in
267 # the newly-exec'd program, not after the remaining step-count
272 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
273 {pass "step through execl call"}
274 -re "$gdb_prompt $" {fail "step through execl call"}
275 timeout {fail "(timeout) step through execl call"}
279 -re "printf \\(.Hello .*$gdb_prompt $"\
280 {pass "step after execl call"}
281 -re "$gdb_prompt $" {fail "step after execl call"}
282 timeout {fail "(timeout) step after execl call"}
285 # Verify that we can print a local variable (which happens to be
286 # assigned the value of main's argc).
288 send_gdb "print local_j\n"
290 -re ".* = 3.*$gdb_prompt $"\
291 {pass "print execd-program/local_j, after execl"}
292 -re "$gdb_prompt $" {fail "print execd-program/local_j, after execl"}
293 timeout {fail "(timeout) print execd-program/local_j, after execl"}
296 # Explicitly kill this program, or a subsequent rerun actually runs
297 # the exec'd program, not the original program...
298 clean_restart $binfile
300 # Start the program running, and stop at main.
306 with_test_prefix "3rd run" {
307 # Execute the code setting up variable PROG.
308 set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile]
309 gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint"
310 gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*"
313 # Verify that we can follow through follow an execv()
314 # call. (We must jump around earlier exec* calls.)
316 set tbreak_line [gdb_get_line_number "tbreak-execv"]
317 send_gdb "tbreak ${tbreak_line}\n"
319 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
320 {pass "prepare to jump to execv call"}
321 -re "$gdb_prompt $" {fail "prepare to jump to execv call"}
322 timeout {fail "(timeout) prepare to jump to execv call"}
324 send_gdb "jump ${tbreak_line}\n"
326 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
327 {pass "jump to execv call"}
328 -re "$gdb_prompt $" {fail "jump to execv call"}
329 timeout {fail "(timeout) jump to execv call"}
333 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
334 {pass "step through execv call"}
335 -re "$gdb_prompt $" {fail "step through execv call"}
336 timeout {fail "(timeout) step through execv call"}
340 -re "printf \\(.Hello .*$gdb_prompt $"\
341 {pass "step after execv call"}
342 -re "$gdb_prompt $" {fail "step after execv call"}
343 timeout {fail "(timeout) step after execv call"}
346 # Verify that we can print a local variable (which happens to be
347 # assigned the value of main's argc).
349 send_gdb "print local_j\n"
351 -re ".* = 2.*$gdb_prompt $"\
352 {pass "print execd-program/local_j, after execv"}
353 -re "$gdb_prompt $" {fail "print execd-program/local_j, after execv"}
354 timeout {fail "(timeout) print execd-program/local_j, after execv"}
357 # Explicitly kill this program, or a subsequent rerun actually runs
358 # the exec'd program, not the original program...
359 clean_restart $binfile
361 # Start the program running, and stop at main.
367 # Verify that we can just continue and thereby follow through an
368 # exec call. (Since the breakpoint on "main" is reset, we should
369 # just stop in main of the newly-exec'd program.)
371 send_gdb "continue\n"
373 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
374 {pass "continue through exec"}
375 -re "$gdb_prompt $" {fail "continue through exec"}
376 timeout {fail "(timeout) continue through exec"}
380 clean_restart $binfile