]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/foll-exec.exp
Fix some duplicate test names
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-exec.exp
1 # Copyright 1997-2020 Free Software Foundation, Inc.
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
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
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.
12 #
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/>.
15
16 # This is a test of gdb's ability to follow a process through a
17 # Unix exec() system call.
18
19 # Until "catch exec" is implemented on other targets...
20 #
21 if { ![istarget "*-linux*"] } then {
22 continue
23 }
24
25 standard_testfile foll-exec.c
26
27 set testfile2 "execd-prog"
28 set srcfile2 ${testfile2}.c
29 set binfile2 [standard_output_file ${testfile2}]
30
31 set compile_options debug
32
33 # build the first test case
34 if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } {
35 untested "failed to compile"
36 return -1
37 }
38
39 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } {
40 untested "failed to compile"
41 return -1
42 }
43
44 proc do_exec_tests {} {
45 global binfile srcfile srcfile2 testfile testfile2
46 global gdb_prompt
47
48 # Start the program running, and stop at main.
49 #
50 if ![runto_main] then {
51 fail "couldn't run ${testfile}"
52 return
53 }
54
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"
61 }
62 -re ".*Catchpoint.*$gdb_prompt $" {
63 set has_exec_catchpoints 1
64 pass "continue to first exec catchpoint"
65 }
66 }
67
68 if {$has_exec_catchpoints == 0} {
69 unsupported "exec catchpoints"
70 return
71 }
72
73 clean_restart $binfile
74
75 # Start the program running, and stop at main.
76 #
77 if ![runto_main] then {
78 fail "couldn't run ${testfile}"
79 return
80 }
81
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 }
88
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 #
94
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 }
116
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 }
128
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 }
160
161 # Explicitly kill this program, or a subsequent rerun actually runs
162 # the exec'd program, not the original program...
163 clean_restart $binfile
164
165 # Start the program running, and stop at main.
166 #
167 if ![runto_main] then {
168 fail "couldn't run ${testfile} (2nd try)"
169 return
170 }
171
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.)
176 #
177 send_gdb "catch exec\n"
178 gdb_expect {
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"}
183 }
184
185 # Verify that the catchpoint is mentioned in an "info breakpoints",
186 # and further that the catchpoint mentions no program name.
187 #
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 $" {
191 pass $msg
192 }
193 }
194
195 send_gdb "continue\n"
196 gdb_expect {
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"}
201 }
202
203 # DTS CLLbs16760
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"
207
208 # Verify that the catchpoint is mentioned in an "info breakpoints",
209 # and further that the catchpoint managed to capture the exec'd
210 # program's name.
211 #
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 $" {
215 pass $msg
216 }
217 }
218
219 # Verify that we can continue from the catchpoint, and land in the
220 # main of the newly-exec'd program.
221 #
222 send_gdb "continue\n"
223 gdb_expect {
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"}
228 }
229
230 # Explicitly kill this program, or a subsequent rerun actually runs
231 # the exec'd program, not the original program...
232 clean_restart $binfile
233
234 # Start the program running, and stop at main.
235 #
236 if ![runto_main] then {
237 fail "couldn't run ${testfile} (3rd try)"
238 return
239 }
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 }
247
248 # Verify that we can follow through follow an execl()
249 # call. (We must jump around earlier exec* calls.)
250 #
251 set tbreak_line [gdb_get_line_number " tbreak-execl " $srcfile]
252 send_gdb "tbreak ${tbreak_line}\n"
253 gdb_expect {
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"}
258 }
259 send_gdb "jump ${tbreak_line}\n"
260 gdb_expect {
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"}
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 {
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"}
278 }
279 send_gdb "next\n"
280 gdb_expect {
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"}
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...
300 clean_restart $binfile
301
302 # Start the program running, and stop at main.
303 #
304 if ![runto_main] then {
305 fail "couldn't run ${testfile} (4th try)"
306 return
307 }
308
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 \\(.*"
314 }
315
316 # Verify that we can follow through follow an execv()
317 # call. (We must jump around earlier exec* calls.)
318 #
319 set tbreak_line [gdb_get_line_number "tbreak-execv"]
320 send_gdb "tbreak ${tbreak_line}\n"
321 gdb_expect {
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"}
326 }
327 send_gdb "jump ${tbreak_line}\n"
328 gdb_expect {
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"}
333 }
334 send_gdb "next\n"
335 gdb_expect {
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"}
340 }
341 send_gdb "next\n"
342 gdb_expect {
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"}
347 }
348
349 # Verify that we can print a local variable (which happens to be
350 # assigned the value of main's argc).
351 #
352 send_gdb "print local_j\n"
353 gdb_expect {
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)"}
358 }
359
360 # Explicitly kill this program, or a subsequent rerun actually runs
361 # the exec'd program, not the original program...
362 clean_restart $binfile
363
364 # Start the program running, and stop at main.
365 #
366 if ![runto_main] then {
367 fail "couldn't run ${testfile} (5th try)"
368 return
369 }
370
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.)
374 #
375 send_gdb "continue\n"
376 gdb_expect {
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"}
381 }
382 }
383
384 # Start with a fresh gdb
385
386 gdb_exit
387 clean_restart $binfile
388
389 do_exec_tests
390
391 return 0