Commit | Line | Data |
---|---|---|
b811d2c2 | 1 | # Copyright 1997-2020 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 { |
2d4e0376 YQ |
22 | continue |
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 { | |
bc6c7af4 | 51 | fail "couldn't run ${testfile}" |
74cf1395 JM |
52 | return |
53 | } | |
54 | ||
e85a822c DJ |
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" { | |
77b06cd7 | 59 | -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { |
e85a822c DJ |
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 | ||
2fd33e94 | 73 | clean_restart $binfile |
e85a822c DJ |
74 | |
75 | # Start the program running, and stop at main. | |
76 | # | |
77 | if ![runto_main] then { | |
bc6c7af4 | 78 | fail "couldn't run ${testfile}" |
e85a822c DJ |
79 | return |
80 | } | |
c2b75043 LM |
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 | } | |
e85a822c | 88 | |
74cf1395 JM |
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 | # | |
31d913c7 | 94 | |
74cf1395 JM |
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 | # | |
58fa2af0 | 120 | set execd_line [gdb_get_line_number "after-exec" $srcfile2] |
74cf1395 JM |
121 | send_gdb "next\n" |
122 | gdb_expect { | |
58fa2af0 | 123 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
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 { | |
58fa2af0 | 134 | -re "printf \\(.Hello .*$gdb_prompt $"\ |
74cf1395 JM |
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... | |
2fd33e94 | 163 | clean_restart $binfile |
74cf1395 JM |
164 | |
165 | # Start the program running, and stop at main. | |
166 | # | |
167 | if ![runto_main] then { | |
bc6c7af4 | 168 | fail "couldn't run ${testfile} (2nd try)" |
74cf1395 JM |
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 | # | |
462f405c JB |
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 | } | |
74cf1395 JM |
193 | } |
194 | ||
74cf1395 JM |
195 | send_gdb "continue\n" |
196 | gdb_expect { | |
b129b0ca | 197 | -re ".*xecuting new program:.*${testfile2}.*Catchpoint .*(exec\'d .*${testfile2}).*$gdb_prompt $"\ |
74cf1395 JM |
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 | # | |
462f405c JB |
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 | } | |
74cf1395 JM |
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 { | |
58fa2af0 | 224 | -re ".*${srcfile2}:${execd_line}.*$gdb_prompt $"\ |
74cf1395 JM |
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... | |
2fd33e94 | 232 | clean_restart $binfile |
74cf1395 JM |
233 | |
234 | # Start the program running, and stop at main. | |
235 | # | |
236 | if ![runto_main] then { | |
bc6c7af4 | 237 | fail "couldn't run ${testfile} (3rd try)" |
74cf1395 JM |
238 | return |
239 | } | |
c2b75043 LM |
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 | } | |
74cf1395 JM |
247 | |
248 | # Verify that we can follow through follow an execl() | |
249 | # call. (We must jump around earlier exec* calls.) | |
250 | # | |
31d913c7 | 251 | set tbreak_line [gdb_get_line_number " tbreak-execl " $srcfile] |
58fa2af0 | 252 | send_gdb "tbreak ${tbreak_line}\n" |
74cf1395 | 253 | gdb_expect { |
58fa2af0 | 254 | -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
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 | } | |
58fa2af0 | 259 | send_gdb "jump ${tbreak_line}\n" |
74cf1395 | 260 | gdb_expect { |
58fa2af0 | 261 | -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
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 { | |
58fa2af0 | 274 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
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 { | |
58fa2af0 | 281 | -re "printf \\(.Hello .*$gdb_prompt $"\ |
74cf1395 JM |
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... | |
2fd33e94 | 300 | clean_restart $binfile |
74cf1395 JM |
301 | |
302 | # Start the program running, and stop at main. | |
303 | # | |
304 | if ![runto_main] then { | |
bc6c7af4 | 305 | fail "couldn't run ${testfile} (4th try)" |
74cf1395 JM |
306 | return |
307 | } | |
c2b75043 LM |
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 | } | |
74cf1395 JM |
315 | |
316 | # Verify that we can follow through follow an execv() | |
317 | # call. (We must jump around earlier exec* calls.) | |
318 | # | |
58fa2af0 AA |
319 | set tbreak_line [gdb_get_line_number "tbreak-execv"] |
320 | send_gdb "tbreak ${tbreak_line}\n" | |
74cf1395 | 321 | gdb_expect { |
58fa2af0 | 322 | -re "Temporary breakpoint .*file .*${srcfile}, line ${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
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 | } | |
58fa2af0 | 327 | send_gdb "jump ${tbreak_line}\n" |
74cf1395 | 328 | gdb_expect { |
58fa2af0 | 329 | -re "main.* at .*${srcfile}:${tbreak_line}.*$gdb_prompt $"\ |
74cf1395 JM |
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 { | |
58fa2af0 | 336 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
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 { | |
58fa2af0 | 343 | -re "printf \\(.Hello .*$gdb_prompt $"\ |
74cf1395 JM |
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... | |
2fd33e94 | 362 | clean_restart $binfile |
74cf1395 JM |
363 | |
364 | # Start the program running, and stop at main. | |
365 | # | |
366 | if ![runto_main] then { | |
bc6c7af4 | 367 | fail "couldn't run ${testfile} (5th try)" |
74cf1395 JM |
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 { | |
58fa2af0 | 377 | -re ".*xecuting new program: .*${testfile2}.*${srcfile2}:${execd_line}.*int local_j = argc;.*$gdb_prompt $"\ |
74cf1395 JM |
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 | |
2fd33e94 | 387 | clean_restart $binfile |
74cf1395 | 388 | |
74cf1395 JM |
389 | do_exec_tests |
390 | ||
391 | return 0 |