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