[gdb/testsuite] Factor out lib/valgrind.exp
[external/binutils.git] / gdb / testsuite / gdb.base / recurse.exp
1 # Copyright 1992-2018 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 file was written by Jeff Law. (law@cs.utah.edu)
17
18
19 standard_testfile
20
21 if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
22     return -1
23 }
24
25 proc recurse_tests {} {
26
27     # Disable hardware watchpoints if necessary.
28     if [target_info exists gdb,no_hardware_watchpoints] {
29         gdb_test_no_output "set can-use-hw-watchpoints 0" ""
30     }
31
32     if [runto recurse] then {
33         # First we need to step over the assignment of b, so it has a known
34         # value.
35         gdb_test "next" "if \\(a == 1\\)" "next over b = 0 in first instance"
36         gdb_test "watch b" ".*\[Ww\]atchpoint \[0-9]*: b" \
37             "set first instance watchpoint"
38
39         # Continue until initial set of b.
40         if [gdb_test "continue" \
41             "Continuing.*\[Ww\]atchpoint.*: b.*Old value = 0.*New value = 10.*" \
42             "continue to first instance watchpoint, first time"] then {
43             gdb_suppress_tests
44         }
45
46         # Continue inward for a few iterations
47         gdb_test "continue" "Breakpoint.* recurse \\(a=9\\).*" \
48             "continue to recurse (a = 9)"
49         gdb_test "continue" "Breakpoint.* recurse \\(a=8\\).*" \
50             "continue to recurse (a = 8)"
51         gdb_test "continue" "Breakpoint.* recurse \\(a=7\\).*" \
52             "continue to recurse (a = 7)"
53         gdb_test "continue" "Breakpoint.* recurse \\(a=6\\).*" \
54             "continue to recurse (a = 6)"
55         gdb_test "continue" "Breakpoint.* recurse \\(a=5\\).*" \
56             "continue to recurse (a = 5)"
57
58         # Put a watchpoint on another instance of b
59         # First we need to step over the assignment of b, so it has a known
60         # value.
61         gdb_test "next" "if \\(a == 1\\)" "next over b = 0 in second instance"
62         gdb_test "watch b" ".*\[Ww\]atchpoint \[0-9]*: b" \
63             "set second instance watchpoint"
64
65         # Continue until initial set of b (second instance).
66         if [gdb_test "continue" \
67             "Continuing.*\[Ww\]atchpoint.*: b.*Old value = 0.*New value = 5.*"\
68             "continue to second instance watchpoint, first time"] then {
69         gdb_suppress_tests
70         }
71
72         # Continue inward for a few iterations
73         gdb_test "continue" "Breakpoint.* recurse \\(a=4\\).*" \
74             "continue to recurse (a = 4)"
75         gdb_test "continue" "Breakpoint.* recurse \\(a=3\\).*" \
76             "continue to recurse (a = 3)"
77         gdb_test "continue" "Breakpoint.* recurse \\(a=2\\).*" \
78             "continue to recurse (a = 2)"
79         gdb_test "continue" "Breakpoint.* recurse \\(a=1\\).*" \
80             "continue to recurse (a = 1)"
81
82         # Continue until second set of b (second instance).
83         if [gdb_test "continue" \
84             "Continuing.*\[Ww\]atchpoint.*: b.*Old value = 5.*New value = 120.*return.*" \
85             "continue to second instance watchpoint, second time"] then { 
86             gdb_suppress_tests
87         }
88
89         # Continue again.  We should have a watchpoint go out of scope now
90         if [gdb_test "continue" \
91             "Continuing.*\[Ww\]atchpoint.*deleted.*recurse \\(a=6\\) .*" \
92             "second instance watchpoint deleted when leaving scope"] then {
93             gdb_suppress_tests
94         }
95
96         # Continue until second set of b (first instance).
97         # 24320 is allowed as the final value for b as that's the value
98         # b would have on systems with 16bit integers.
99         #
100         # We could fix the test program to deal with this too.
101         if [gdb_test "continue" \
102             "Continuing.*\[Ww\]atchpoint.*b.*Old value = 10.*New value = \(3628800|24320\).*return.*" \
103             "continue to first instance watchpoint, second time"] then {
104             gdb_suppress_tests
105         }
106
107         # Continue again.  We should have a watchpoint go out of scope now.
108         #
109         # The former version expected the test to return to main().
110         # Now it expects the test to return to main or to stop in the
111         # function's epilogue.
112         # 
113         # The problem is that gdb needs to (but doesn't) understand
114         # function epilogues in the same way as for prologues.
115         # 
116         # If there is no hardware watchpoint (such as a x86 debug register),
117         # then watchpoints are done "the hard way" by single-stepping the
118         # target until the value of the watched variable changes.  If you
119         # are single-stepping, you will eventually step into an epilogue.
120         # When you do that, the "top" stack frame may become partially
121         # deconstructed (as when you pop the frame pointer, for instance),
122         # and from that point on, GDB can no longer make sense of the stack.
123         # 
124         # A test which stops in the epilogue is trying to determine when GDB
125         # leaves the stack frame in which the watchpoint was created.  It does
126         # this basically by watching for the frame pointer to change.  When
127         # the frame pointer changes, the test expects to be back in main, but
128         # instead it is still in the epilogue of the callee.
129         if [gdb_test "continue" \
130             "Continuing.*\[Ww\]atchpoint.*deleted.*\(main \\(\\) \|21.*\}\).*" \
131             "first instance watchpoint deleted when leaving scope"] then {
132             gdb_suppress_tests
133         }
134     }
135     gdb_stop_suppressing_tests
136 }
137
138 # Preserve the old timeout, and set a new one that should be
139 # sufficient to avoid timing out during this test.
140 set oldtimeout $timeout
141 set timeout [expr "$timeout + 60"]
142 verbose "Timeout is now $timeout seconds" 2
143
144 recurse_tests
145
146 # Restore the preserved old timeout value.
147 set timeout $oldtimeout
148 verbose "Timeout is now $timeout seconds" 2
149