[gdb/syscalls] Sync with strace v6.13
[binutils-gdb.git] / gdb / testsuite / gdb.base / foll-exec.exp
blob206e713a6b02f454f4afd5ec227853b334d6450b
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"
34      return -1
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"
43      return -1
46 proc do_exec_tests {} {
47    global binfile srcfile srcfile2 testfile testfile2
48    global gdb_prompt
50    # Start the program running, and stop at main.
51    #
52    if {![runto_main]} {
53      return
54    }
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"
62      }
63      -re ".*Catchpoint.*$gdb_prompt $" {
64        set has_exec_catchpoints 1
65        pass "continue to first exec catchpoint"
66      }
67    }
69    if {$has_exec_catchpoints == 0} {
70      unsupported "exec catchpoints"
71      return
72    }
74    clean_restart $binfile
76    # Start the program running, and stop at main.
77    #
78    if {![runto_main]} {
79      return
80    }
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 \\(.*"
87    }
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
92    # exec in a moment.
93    #
95    send_gdb "print global_i\n"
96    gdb_expect {
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"}
101    }
102    send_gdb "print local_j\n"
103    gdb_expect {
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"}
108    }
109    send_gdb "print local_k\n"
110    gdb_expect {
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"}
115    }
117    # Try stepping through an execlp call, without catching it.
118    # We should stop in execd-program, at its first statement.
119    #
120    set execd_line [gdb_get_line_number "after-exec" $srcfile2]
121    send_gdb "next\n"
122    gdb_expect {
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"}
127    }
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.
131    #
132    send_gdb "next\n"
133    gdb_expect {
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"}
138    }
139    send_gdb "print global_i\n"
140    gdb_expect {
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"}
145    }
146    send_gdb "print local_j\n"
147    gdb_expect {
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"}
152    }
153    send_gdb "print local_k\n"
154    gdb_expect {
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"}
159    }
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.
166    #
167    if {![runto_main]} {
168      return
169    }
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.)
175    #
176    send_gdb "catch exec\n"
177    gdb_expect {
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"}
182    }
184    # Verify that the catchpoint is mentioned in an "info breakpoints",
185    # and further that the catchpoint mentions no program name.
186    #
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 $" {
190            pass $msg
191        }
192    }
194    send_gdb "continue\n"
195    gdb_expect {
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"}
200    }
202    # DTS CLLbs16760
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
209    # program's name.
210    #
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 $" {
214            pass $msg
215        }
216    }
218    # Verify that we can continue from the catchpoint, and land in the
219    # main of the newly-exec'd program.
220    #
221    send_gdb "continue\n"
222    gdb_expect {
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"}
227    }
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.
234    #
235    if {![runto_main]} {
236      return
237    }
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 \\(.*"
244    }
246    # Verify that we can follow through follow an execl()
247    # call.  (We must jump around earlier exec* calls.)
248    #
249    set tbreak_line [gdb_get_line_number " tbreak-execl " $srcfile]
250    send_gdb "tbreak ${tbreak_line}\n"
251    gdb_expect {
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"}
256    }
257    send_gdb "jump ${tbreak_line}\n"
258    gdb_expect {
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"}
263    }
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
268    # reaches zero.
269    #
270    send_gdb "next 2\n"
271    gdb_expect {
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"}
276    }
277    send_gdb "next\n"
278    gdb_expect {
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"}
283    }
285    # Verify that we can print a local variable (which happens to be
286    # assigned the value of main's argc).
287    #
288    send_gdb "print local_j\n"
289    gdb_expect {
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"}
294    }
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.
301    #
302    if {![runto_main]} {
303      return
304    }
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 \\(.*"
311    }
313    # Verify that we can follow through follow an execv()
314    # call.  (We must jump around earlier exec* calls.)
315    #
316    set tbreak_line [gdb_get_line_number "tbreak-execv"]
317    send_gdb "tbreak ${tbreak_line}\n"
318    gdb_expect {
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"}
323    }
324    send_gdb "jump ${tbreak_line}\n"
325    gdb_expect {
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"}
330    }
331    send_gdb "next\n"
332    gdb_expect {
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"}
337    }
338    send_gdb "next\n"
339    gdb_expect {
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"}
344    }
346    # Verify that we can print a local variable (which happens to be
347    # assigned the value of main's argc).
348    #
349    send_gdb "print local_j\n"
350    gdb_expect {
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"}
355    }
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.
362    #
363    if {![runto_main]} {
364      return
365    }
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.)
370    #
371    send_gdb "continue\n"
372    gdb_expect {
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"}
377    }
380 clean_restart $binfile
382 do_exec_tests