1 # Copyright 1997-2016 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
32 set dirname [relative_filename [pwd] [file dirname $binfile]]
33 lappend compile_options "additional_flags=-DBASEDIR=\"$dirname\""
35 # build the first test case
36 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } {
37 untested foll-exec.exp
41 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } {
42 untested foll-exec.exp
46 proc do_exec_tests {} {
47 global binfile srcfile srcfile2 testfile testfile2
50 # Start the program running, and stop at main.
52 if ![runto_main] then {
53 fail "Couldn't run ${testfile}"
57 # Verify that the system supports "catch exec".
58 gdb_test "catch exec" "Catchpoint \[0-9\]* \\(exec\\)" "insert first exec catchpoint"
59 set has_exec_catchpoints 0
60 gdb_test_multiple "continue" "continue to first exec catchpoint" {
61 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
62 unsupported "continue to first exec catchpoint"
64 -re ".*Catchpoint.*$gdb_prompt $" {
65 set has_exec_catchpoints 1
66 pass "continue to first exec catchpoint"
70 if {$has_exec_catchpoints == 0} {
71 unsupported "exec catchpoints"
75 clean_restart $binfile
77 # Start the program running, and stop at main.
79 if ![runto_main] then {
80 fail "Couldn't run ${testfile}"
84 # Verify that we can see various global and local variables
85 # in this program, and that they have expected values. Some
86 # of these variables are also declared in the program we'll
91 -re "execlp \\(.*$gdb_prompt $"\
92 {pass "step to exec call"}
93 -re "$gdb_prompt $" {fail "step to exec call"}
94 timeout {fail "(timeout) step to exec call"}
96 send_gdb "print global_i\n"
98 -re ".* = 100.*$gdb_prompt $"\
99 {pass "print follow-exec/global_i"}
100 -re "$gdb_prompt $" {fail "print follow-exec/global_i"}
101 timeout {fail "(timeout) print follow-exec/global_i"}
103 send_gdb "print local_j\n"
105 -re ".* = 101.*$gdb_prompt $"\
106 {pass "print follow-exec/local_j"}
107 -re "$gdb_prompt $" {fail "print follow-exec/local_j"}
108 timeout {fail "(timeout) print follow-exec/local_j"}
110 send_gdb "print local_k\n"
112 -re ".* = 102.*$gdb_prompt $"\
113 {pass "print follow-exec/local_k"}
114 -re "$gdb_prompt $" {fail "print follow-exec/local_k"}
115 timeout {fail "(timeout) print follow-exec/local_k"}
118 # Try stepping through an execlp call, without catching it.
119 # We should stop in execd-program, at its first statement.
121 set execd_line [gdb_get_line_number "after-exec" $srcfile2]
124 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
125 {pass "step through execlp call"}
126 -re "$gdb_prompt $" {fail "step through execlp call"}
127 timeout {fail "(timeout) step through execlp call"}
130 # Verify that we can see the variables defined in the newly-exec'd
131 # program, and CANNOT see those defined in the exec'ing program.
135 -re "printf \\(.Hello .*$gdb_prompt $"\
136 {pass "step after execlp call"}
137 -re "$gdb_prompt $" {fail "step after execlp call"}
138 timeout {fail "(timeout) step after execlp call"}
140 send_gdb "print global_i\n"
142 -re ".* = 0.*$gdb_prompt $"\
143 {pass "print execd-program/global_i (after execlp)"}
144 -re "$gdb_prompt $" {fail "print execd-program/global_i (after execlp)"}
145 timeout {fail "(timeout) print execd-program/global_i (after execlp)"}
147 send_gdb "print local_j\n"
149 -re ".* = 2.*$gdb_prompt $"\
150 {pass "print execd-program/local_j (after execlp)"}
151 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execlp)"}
152 timeout {fail "(timeout) print execd-program/local_j (after execlp)"}
154 send_gdb "print local_k\n"
156 -re "No symbol \"local_k\" in current context.*$gdb_prompt $"\
157 {pass "print follow-exec/local_k (after execlp)"}
158 -re "$gdb_prompt $" {fail "print follow-exec/local_k (after execlp)"}
159 timeout {fail "(timeout) print follow-exec/local_k (after execlp)"}
162 # Explicitly kill this program, or a subsequent rerun actually runs
163 # the exec'd program, not the original program...
164 clean_restart $binfile
166 # Start the program running, and stop at main.
168 if ![runto_main] then {
169 fail "Couldn't run ${testfile} (2nd try)"
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.)
178 send_gdb "catch exec\n"
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"}
186 # Verify that the catchpoint is mentioned in an "info breakpoints",
187 # and further that the catchpoint mentions no program name.
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 $" {
196 send_gdb "continue\n"
198 -re ".*xecuting new program:.*${testfile2}.*Catchpoint .*(exec\'d .*${testfile2}).*in .*$gdb_prompt $"\
199 {pass "hit catch exec"}
200 -re "$gdb_prompt $" {fail "hit catch exec"}
201 timeout {fail "(timeout) hit catch exec"}
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"
209 # Verify that the catchpoint is mentioned in an "info breakpoints",
210 # and further that the catchpoint managed to capture the exec'd
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 $" {
220 # Verify that we can continue from the catchpoint, and land in the
221 # main of the newly-exec'd program.
223 send_gdb "continue\n"
225 -re ".*${srcfile2}:${execd_line}.*$gdb_prompt $"\
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"}
231 # Explicitly kill this program, or a subsequent rerun actually runs
232 # the exec'd program, not the original program...
233 clean_restart $binfile
235 # Start the program running, and stop at main.
237 if ![runto_main] then {
238 fail "Couldn't run ${testfile} (3rd try)"
242 # Verify that we can follow through follow an execl()
243 # call. (We must jump around earlier exec* calls.)
245 set tbreak_line [gdb_get_line_number "tbreak-execl" $srcfile]
246 send_gdb "tbreak ${tbreak_line}\n"
248 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
249 {pass "prepare to jump to execl call"}
250 -re "$gdb_prompt $" {fail "prepare to jump to execl call"}
251 timeout {fail "(timeout) prepare to jump to execl call"}
253 send_gdb "jump ${tbreak_line}\n"
255 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
256 {pass "jump to execl call"}
257 -re "$gdb_prompt $" {fail "jump to execl call"}
258 timeout {fail "(timeout) jump to execl call"}
260 # Note that stepping through an exec call causes the step-count
261 # to be reset to zero. I.e.: you may specify "next 2" at the
262 # call, but you'll actually stop at the first breakpoint set in
263 # the newly-exec'd program, not after the remaining step-count
268 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
269 {pass "step through execl call"}
270 -re "$gdb_prompt $" {fail "step through execl call"}
271 timeout {fail "(timeout) step through execl call"}
275 -re "printf \\(.Hello .*$gdb_prompt $"\
276 {pass "step after execl call"}
277 -re "$gdb_prompt $" {fail "step after execl call"}
278 timeout {fail "(timeout) step after execl call"}
281 # Verify that we can print a local variable (which happens to be
282 # assigned the value of main's argc).
284 send_gdb "print local_j\n"
286 -re ".* = 3.*$gdb_prompt $"\
287 {pass "print execd-program/local_j (after execl)"}
288 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execl)"}
289 timeout {fail "(timeout) print execd-program/local_j (after execl)"}
292 # Explicitly kill this program, or a subsequent rerun actually runs
293 # the exec'd program, not the original program...
294 clean_restart $binfile
296 # Start the program running, and stop at main.
298 if ![runto_main] then {
299 fail "Couldn't run ${testfile} (4th try)"
303 # Verify that we can follow through follow an execv()
304 # call. (We must jump around earlier exec* calls.)
306 set tbreak_line [gdb_get_line_number "tbreak-execv"]
307 send_gdb "tbreak ${tbreak_line}\n"
309 -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\
310 {pass "prepare to jump to execv call"}
311 -re "$gdb_prompt $" {fail "prepare to jump to execv call"}
312 timeout {fail "(timeout) prepare to jump to execv call"}
314 send_gdb "jump ${tbreak_line}\n"
316 -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\
317 {pass "jump to execv call"}
318 -re "$gdb_prompt $" {fail "jump to execv call"}
319 timeout {fail "(timeout) jump to execv call"}
323 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
324 {pass "step through execv call"}
325 -re "$gdb_prompt $" {fail "step through execv call"}
326 timeout {fail "(timeout) step through execv call"}
330 -re "printf \\(.Hello .*$gdb_prompt $"\
331 {pass "step after execv call"}
332 -re "$gdb_prompt $" {fail "step after execv call"}
333 timeout {fail "(timeout) step after execv call"}
336 # Verify that we can print a local variable (which happens to be
337 # assigned the value of main's argc).
339 send_gdb "print local_j\n"
341 -re ".* = 2.*$gdb_prompt $"\
342 {pass "print execd-program/local_j (after execv)"}
343 -re "$gdb_prompt $" {fail "print execd-program/local_j (after execv)"}
344 timeout {fail "(timeout) print execd-program/local_j (after execv)"}
347 # Explicitly kill this program, or a subsequent rerun actually runs
348 # the exec'd program, not the original program...
349 clean_restart $binfile
351 # Start the program running, and stop at main.
353 if ![runto_main] then {
354 fail "Couldn't run ${testfile} (5th try)"
358 # Verify that we can just continue and thereby follow through an
359 # exec call. (Since the breakpoint on "main" is reset, we should
360 # just stop in main of the newly-exec'd program.)
362 send_gdb "continue\n"
364 -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\
365 {pass "continue through exec"}
366 -re "$gdb_prompt $" {fail "continue through exec"}
367 timeout {fail "(timeout) continue through exec"}
371 # Start with a fresh gdb
374 clean_restart $binfile