]>
Commit | Line | Data |
---|---|---|
4a94e368 | 1 | # Copyright 1997-2022 Free Software Foundation, Inc. |
74cf1395 JM |
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 | |
e22f8b7c | 5 | # the Free Software Foundation; either version 3 of the License, or |
74cf1395 | 6 | # (at your option) any later version. |
e22f8b7c | 7 | # |
74cf1395 JM |
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. | |
e22f8b7c | 12 | # |
74cf1395 | 13 | # You should have received a copy of the GNU General Public License |
e85a822c | 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
74cf1395 | 15 | |
2fd33e94 DB |
16 | # This is a test of gdb's ability to follow a process through a |
17 | # Unix exec() system call. | |
18 | ||
2d4e0376 YQ |
19 | # Until "catch exec" is implemented on other targets... |
20 | # | |
3ca22649 | 21 | if { ![istarget "*-linux*"] } then { |
cdd42066 | 22 | return |
2d4e0376 | 23 | } |
74cf1395 | 24 | |
289f9037 TT |
25 | standard_testfile foll-exec.c |
26 | ||
74cf1395 | 27 | set testfile2 "execd-prog" |
74cf1395 | 28 | set srcfile2 ${testfile2}.c |
289f9037 | 29 | set binfile2 [standard_output_file ${testfile2}] |
74cf1395 | 30 | |
3fef966c | 31 | set compile_options debug |
3fef966c | 32 | |
74cf1395 | 33 | # build the first test case |
3fef966c | 34 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable $compile_options] != "" } { |
5b362f04 | 35 | untested "failed to compile" |
b60f0898 | 36 | return -1 |
74cf1395 JM |
37 | } |
38 | ||
3fef966c | 39 | if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $compile_options] != "" } { |
5b362f04 | 40 | untested "failed to compile" |
b60f0898 | 41 | return -1 |
74cf1395 JM |
42 | } |
43 | ||
74cf1395 | 44 | proc do_exec_tests {} { |
2fd33e94 | 45 | global binfile srcfile srcfile2 testfile testfile2 |
74cf1395 | 46 | global gdb_prompt |
74cf1395 JM |
47 | |
48 | # Start the program running, and stop at main. | |
49 | # | |
50 | if ![runto_main] then { | |
74cf1395 JM |
51 | return |
52 | } | |
53 | ||
e85a822c DJ |
54 | # Verify that the system supports "catch exec". |
55 | gdb_test "catch exec" "Catchpoint \[0-9\]* \\(exec\\)" "insert first exec catchpoint" | |
56 | set has_exec_catchpoints 0 | |
57 | gdb_test_multiple "continue" "continue to first exec catchpoint" { | |
77b06cd7 | 58 | -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { |
e85a822c DJ |
59 | unsupported "continue to first exec catchpoint" |
60 | } | |
61 | -re ".*Catchpoint.*$gdb_prompt $" { | |
62 | set has_exec_catchpoints 1 | |
63 | pass "continue to first exec catchpoint" | |
64 | } | |
65 | } | |
66 | ||
67 | if {$has_exec_catchpoints == 0} { | |
68 | unsupported "exec catchpoints" | |
69 | return | |
70 | } | |
71 | ||
2fd33e94 | 72 | clean_restart $binfile |
e85a822c DJ |
73 | |
74 | # Start the program running, and stop at main. | |
75 | # | |
76 | if ![runto_main] then { | |
e85a822c DJ |
77 | return |
78 | } | |
c2b75043 LM |
79 | |
80 | with_test_prefix "1st run" { | |
81 | # Execute the code setting up variable PROG. | |
82 | set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile] | |
83 | gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint" | |
84 | gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*" | |
85 | } | |
e85a822c | 86 | |
74cf1395 JM |
87 | # Verify that we can see various global and local variables |
88 | # in this program, and that they have expected values. Some | |
89 | # of these variables are also declared in the program we'll | |
90 | # exec in a moment. | |
91 | # | |
31d913c7 | 92 | |
74cf1395 JM |
93 | send_gdb "print global_i\n" |
94 | gdb_expect { | |
95 | -re ".* = 100.*$gdb_prompt $"\ | |
96 | {pass "print follow-exec/global_i"} | |
97 | -re "$gdb_prompt $" {fail "print follow-exec/global_i"} | |
98 | timeout {fail "(timeout) print follow-exec/global_i"} | |
99 | } | |
100 | send_gdb "print local_j\n" | |
101 | gdb_expect { | |
102 | -re ".* = 101.*$gdb_prompt $"\ | |
103 | {pass "print follow-exec/local_j"} | |
104 | -re "$gdb_prompt $" {fail "print follow-exec/local_j"} | |
105 | timeout {fail "(timeout) print follow-exec/local_j"} | |
106 | } | |
107 | send_gdb "print local_k\n" | |
108 | gdb_expect { | |
109 | -re ".* = 102.*$gdb_prompt $"\ | |
110 | {pass "print follow-exec/local_k"} | |
111 | -re "$gdb_prompt $" {fail "print follow-exec/local_k"} | |
112 | timeout {fail "(timeout) print follow-exec/local_k"} | |
113 | } | |
114 | ||
115 | # Try stepping through an execlp call, without catching it. | |
116 | # We should stop in execd-program, at its first statement. | |
117 | # | |
58fa2af0 | 118 | set execd_line [gdb_get_line_number "after-exec" $srcfile2] |
74cf1395 JM |
119 | send_gdb "next\n" |
120 | gdb_expect { | |
58fa2af0 | 121 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
122 | {pass "step through execlp call"} |
123 | -re "$gdb_prompt $" {fail "step through execlp call"} | |
124 | timeout {fail "(timeout) step through execlp call"} | |
125 | } | |
126 | ||
127 | # Verify that we can see the variables defined in the newly-exec'd | |
128 | # program, and CANNOT see those defined in the exec'ing program. | |
129 | # | |
130 | send_gdb "next\n" | |
131 | gdb_expect { | |
58fa2af0 | 132 | -re "printf \\(.Hello .*$gdb_prompt $"\ |
74cf1395 JM |
133 | {pass "step after execlp call"} |
134 | -re "$gdb_prompt $" {fail "step after execlp call"} | |
135 | timeout {fail "(timeout) step after execlp call"} | |
136 | } | |
137 | send_gdb "print global_i\n" | |
138 | gdb_expect { | |
139 | -re ".* = 0.*$gdb_prompt $"\ | |
140 | {pass "print execd-program/global_i (after execlp)"} | |
141 | -re "$gdb_prompt $" {fail "print execd-program/global_i (after execlp)"} | |
142 | timeout {fail "(timeout) print execd-program/global_i (after execlp)"} | |
143 | } | |
144 | send_gdb "print local_j\n" | |
145 | gdb_expect { | |
146 | -re ".* = 2.*$gdb_prompt $"\ | |
147 | {pass "print execd-program/local_j (after execlp)"} | |
148 | -re "$gdb_prompt $" {fail "print execd-program/local_j (after execlp)"} | |
149 | timeout {fail "(timeout) print execd-program/local_j (after execlp)"} | |
150 | } | |
151 | send_gdb "print local_k\n" | |
152 | gdb_expect { | |
153 | -re "No symbol \"local_k\" in current context.*$gdb_prompt $"\ | |
154 | {pass "print follow-exec/local_k (after execlp)"} | |
155 | -re "$gdb_prompt $" {fail "print follow-exec/local_k (after execlp)"} | |
156 | timeout {fail "(timeout) print follow-exec/local_k (after execlp)"} | |
157 | } | |
158 | ||
159 | # Explicitly kill this program, or a subsequent rerun actually runs | |
160 | # the exec'd program, not the original program... | |
2fd33e94 | 161 | clean_restart $binfile |
74cf1395 JM |
162 | |
163 | # Start the program running, and stop at main. | |
164 | # | |
165 | if ![runto_main] then { | |
74cf1395 JM |
166 | return |
167 | } | |
168 | ||
169 | # Verify that we can catch an exec event, and then continue | |
170 | # to follow through the exec. (Since there's a breakpoint on | |
171 | # "main", it'll also be transferred to the exec'd program, | |
172 | # and we expect to stop there.) | |
173 | # | |
174 | send_gdb "catch exec\n" | |
175 | gdb_expect { | |
176 | -re "Catchpoint .*(exec).*$gdb_prompt $"\ | |
177 | {pass "set catch exec"} | |
178 | -re "$gdb_prompt $" {fail "set catch exec"} | |
179 | timeout {fail "(timeout) set catch exec"} | |
180 | } | |
181 | ||
182 | # Verify that the catchpoint is mentioned in an "info breakpoints", | |
183 | # and further that the catchpoint mentions no program name. | |
184 | # | |
462f405c JB |
185 | set msg "info shows catchpoint without exec pathname" |
186 | gdb_test_multiple "info breakpoints" $msg { | |
187 | -re ".*catchpoint.*keep y.*exec\[\n\r\]+$gdb_prompt $" { | |
188 | pass $msg | |
189 | } | |
74cf1395 JM |
190 | } |
191 | ||
74cf1395 JM |
192 | send_gdb "continue\n" |
193 | gdb_expect { | |
b129b0ca | 194 | -re ".*xecuting new program:.*${testfile2}.*Catchpoint .*(exec\'d .*${testfile2}).*$gdb_prompt $"\ |
74cf1395 JM |
195 | {pass "hit catch exec"} |
196 | -re "$gdb_prompt $" {fail "hit catch exec"} | |
197 | timeout {fail "(timeout) hit catch exec"} | |
198 | } | |
199 | ||
200 | # DTS CLLbs16760 | |
201 | # test gets out of sync if previous test fails. | |
202 | gdb_test "bt" ".*" "sync up after possible failure 1" | |
203 | gdb_test "bt" "#0.*" "sync up after possible failure 2" | |
204 | ||
205 | # Verify that the catchpoint is mentioned in an "info breakpoints", | |
206 | # and further that the catchpoint managed to capture the exec'd | |
207 | # program's name. | |
208 | # | |
462f405c JB |
209 | set msg "info shows catchpoint exec pathname" |
210 | gdb_test_multiple "info breakpoints" $msg { | |
211 | -re ".*catchpoint.*keep y.*exec, program \".*${testfile2}\".*$gdb_prompt $" { | |
212 | pass $msg | |
213 | } | |
74cf1395 JM |
214 | } |
215 | ||
216 | # Verify that we can continue from the catchpoint, and land in the | |
217 | # main of the newly-exec'd program. | |
218 | # | |
219 | send_gdb "continue\n" | |
220 | gdb_expect { | |
58fa2af0 | 221 | -re ".*${srcfile2}:${execd_line}.*$gdb_prompt $"\ |
74cf1395 JM |
222 | {pass "continue after hit catch exec"} |
223 | -re "$gdb_prompt $" {fail "continue after hit catch exec"} | |
224 | timeout {fail "(timeout) continue after hit catch exec"} | |
225 | } | |
226 | ||
227 | # Explicitly kill this program, or a subsequent rerun actually runs | |
228 | # the exec'd program, not the original program... | |
2fd33e94 | 229 | clean_restart $binfile |
74cf1395 JM |
230 | |
231 | # Start the program running, and stop at main. | |
232 | # | |
233 | if ![runto_main] then { | |
74cf1395 JM |
234 | return |
235 | } | |
c2b75043 LM |
236 | |
237 | with_test_prefix "2nd run" { | |
238 | # Execute the code setting up variable PROG. | |
239 | set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile] | |
240 | gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint" | |
241 | gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*" | |
242 | } | |
74cf1395 JM |
243 | |
244 | # Verify that we can follow through follow an execl() | |
245 | # call. (We must jump around earlier exec* calls.) | |
246 | # | |
31d913c7 | 247 | set tbreak_line [gdb_get_line_number " tbreak-execl " $srcfile] |
58fa2af0 | 248 | send_gdb "tbreak ${tbreak_line}\n" |
74cf1395 | 249 | gdb_expect { |
58fa2af0 | 250 | -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
251 | {pass "prepare to jump to execl call"} |
252 | -re "$gdb_prompt $" {fail "prepare to jump to execl call"} | |
253 | timeout {fail "(timeout) prepare to jump to execl call"} | |
254 | } | |
58fa2af0 | 255 | send_gdb "jump ${tbreak_line}\n" |
74cf1395 | 256 | gdb_expect { |
58fa2af0 | 257 | -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
258 | {pass "jump to execl call"} |
259 | -re "$gdb_prompt $" {fail "jump to execl call"} | |
260 | timeout {fail "(timeout) jump to execl call"} | |
261 | } | |
262 | # Note that stepping through an exec call causes the step-count | |
263 | # to be reset to zero. I.e.: you may specify "next 2" at the | |
264 | # call, but you'll actually stop at the first breakpoint set in | |
265 | # the newly-exec'd program, not after the remaining step-count | |
266 | # reaches zero. | |
267 | # | |
268 | send_gdb "next 2\n" | |
269 | gdb_expect { | |
58fa2af0 | 270 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
271 | {pass "step through execl call"} |
272 | -re "$gdb_prompt $" {fail "step through execl call"} | |
273 | timeout {fail "(timeout) step through execl call"} | |
274 | } | |
275 | send_gdb "next\n" | |
276 | gdb_expect { | |
58fa2af0 | 277 | -re "printf \\(.Hello .*$gdb_prompt $"\ |
74cf1395 JM |
278 | {pass "step after execl call"} |
279 | -re "$gdb_prompt $" {fail "step after execl call"} | |
280 | timeout {fail "(timeout) step after execl call"} | |
281 | } | |
282 | ||
283 | # Verify that we can print a local variable (which happens to be | |
284 | # assigned the value of main's argc). | |
285 | # | |
286 | send_gdb "print local_j\n" | |
287 | gdb_expect { | |
288 | -re ".* = 3.*$gdb_prompt $"\ | |
289 | {pass "print execd-program/local_j (after execl)"} | |
290 | -re "$gdb_prompt $" {fail "print execd-program/local_j (after execl)"} | |
291 | timeout {fail "(timeout) print execd-program/local_j (after execl)"} | |
292 | } | |
293 | ||
294 | # Explicitly kill this program, or a subsequent rerun actually runs | |
295 | # the exec'd program, not the original program... | |
2fd33e94 | 296 | clean_restart $binfile |
74cf1395 JM |
297 | |
298 | # Start the program running, and stop at main. | |
299 | # | |
300 | if ![runto_main] then { | |
74cf1395 JM |
301 | return |
302 | } | |
c2b75043 LM |
303 | |
304 | with_test_prefix "3rd run" { | |
305 | # Execute the code setting up variable PROG. | |
306 | set tbreak_line [gdb_get_line_number " tbreak-execlp " $srcfile] | |
307 | gdb_test "tbreak ${tbreak_line}" "" "insert breakpoint" | |
308 | gdb_continue_to_breakpoint "line tbreak-execlp" ".*execlp \\(.*" | |
309 | } | |
74cf1395 JM |
310 | |
311 | # Verify that we can follow through follow an execv() | |
312 | # call. (We must jump around earlier exec* calls.) | |
313 | # | |
58fa2af0 AA |
314 | set tbreak_line [gdb_get_line_number "tbreak-execv"] |
315 | send_gdb "tbreak ${tbreak_line}\n" | |
74cf1395 | 316 | gdb_expect { |
58fa2af0 | 317 | -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
318 | {pass "prepare to jump to execv call"} |
319 | -re "$gdb_prompt $" {fail "prepare to jump to execv call"} | |
320 | timeout {fail "(timeout) prepare to jump to execv call"} | |
321 | } | |
58fa2af0 | 322 | send_gdb "jump ${tbreak_line}\n" |
74cf1395 | 323 | gdb_expect { |
58fa2af0 | 324 | -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
325 | {pass "jump to execv call"} |
326 | -re "$gdb_prompt $" {fail "jump to execv call"} | |
327 | timeout {fail "(timeout) jump to execv call"} | |
328 | } | |
329 | send_gdb "next\n" | |
330 | gdb_expect { | |
58fa2af0 | 331 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
332 | {pass "step through execv call"} |
333 | -re "$gdb_prompt $" {fail "step through execv call"} | |
334 | timeout {fail "(timeout) step through execv call"} | |
335 | } | |
336 | send_gdb "next\n" | |
337 | gdb_expect { | |
58fa2af0 | 338 | -re "printf \\(.Hello .*$gdb_prompt $"\ |
74cf1395 JM |
339 | {pass "step after execv call"} |
340 | -re "$gdb_prompt $" {fail "step after execv call"} | |
341 | timeout {fail "(timeout) step after execv call"} | |
342 | } | |
343 | ||
344 | # Verify that we can print a local variable (which happens to be | |
345 | # assigned the value of main's argc). | |
346 | # | |
347 | send_gdb "print local_j\n" | |
348 | gdb_expect { | |
349 | -re ".* = 2.*$gdb_prompt $"\ | |
350 | {pass "print execd-program/local_j (after execv)"} | |
351 | -re "$gdb_prompt $" {fail "print execd-program/local_j (after execv)"} | |
352 | timeout {fail "(timeout) print execd-program/local_j (after execv)"} | |
353 | } | |
354 | ||
355 | # Explicitly kill this program, or a subsequent rerun actually runs | |
356 | # the exec'd program, not the original program... | |
2fd33e94 | 357 | clean_restart $binfile |
74cf1395 JM |
358 | |
359 | # Start the program running, and stop at main. | |
360 | # | |
361 | if ![runto_main] then { | |
74cf1395 JM |
362 | return |
363 | } | |
364 | ||
365 | # Verify that we can just continue and thereby follow through an | |
366 | # exec call. (Since the breakpoint on "main" is reset, we should | |
367 | # just stop in main of the newly-exec'd program.) | |
368 | # | |
369 | send_gdb "continue\n" | |
370 | gdb_expect { | |
58fa2af0 | 371 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
372 | {pass "continue through exec"} |
373 | -re "$gdb_prompt $" {fail "continue through exec"} | |
374 | timeout {fail "(timeout) continue through exec"} | |
375 | } | |
376 | } | |
377 | ||
378 | # Start with a fresh gdb | |
379 | ||
380 | gdb_exit | |
2fd33e94 | 381 | clean_restart $binfile |
74cf1395 | 382 | |
74cf1395 JM |
383 | do_exec_tests |
384 | ||
385 | return 0 |