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