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