2010-06-02 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.threads / watchthreads.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2004, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 # Check that GDB can support multiple watchpoints across threads.
19
20 if $tracelevel {
21 strace $tracelevel
22 }
23
24
25 # This test verifies that a watchpoint is detected in the proper thread
26 # so the test is only meaningful on a system with hardware watchpoints.
27 if [target_info exists gdb,no_hardware_watchpoints] {
28 return 0;
29 }
30
31 proc target_no_stopped_data { } {
32 return [istarget s390*-*-*]
33 }
34
35 set testfile "watchthreads"
36 set srcfile ${testfile}.c
37 set binfile ${objdir}/${subdir}/${testfile}
38 if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "incdir=${objdir}"]] != "" } {
39 return -1
40 }
41
42 gdb_exit
43 gdb_start
44 gdb_reinitialize_dir $srcdir/$subdir
45 gdb_load ${binfile}
46
47 gdb_test "set can-use-hw-watchpoints 1" "" ""
48
49 #
50 # Run to `main' where we begin our tests.
51 #
52
53 if ![runto_main] then {
54 gdb_suppress_tests
55 }
56
57 set args_0 0
58 set args_1 0
59
60 # Watch values that will be modified by distinct threads.
61 gdb_test "watch args\[0\]" "Hardware watchpoint 2: args\\\[0\\\]"
62 gdb_test "watch args\[1\]" "Hardware watchpoint 3: args\\\[1\\\]"
63 set hwwp_2_enabled 1
64 set hwwp_3_enabled 1
65
66 set init_line [gdb_get_line_number "Init value"]
67 set inc_line [gdb_get_line_number "Loop increment"]
68 set main_loc "main \\\(\\\) at .*watchthreads.c:$init_line"
69 set thread0_loc "thread_function \\\(arg=0x0\\\) at .*watchthreads.c:$inc_line"
70 set thread1_loc "thread_function \\\(arg=0x1\\\) at .*watchthreads.c:$inc_line"
71
72 # Loop and continue to allow both watchpoints to be triggered.
73 for {set i 0} {$i < 30} {incr i} {
74 set test_flag_0 0
75 set test_flag_1 0
76 set test_flag 0
77 gdb_test_multiple "continue" "threaded watch loop" {
78 -re "(.*Hardware watchpoint.*)$gdb_prompt $" {
79 # At least one hardware watchpoint was hit. Check if both were.
80 set string $expect_out(1,string)
81
82 if [regexp "Hardware watchpoint 2: args\\\[0\\\]\[^\r\]*\r\[^\r\]*\r\[^\r\]*Old value = $args_0\[^\r\]*\r\[^\r\]*New value = [expr $args_0+1]\r" $string] {
83 incr args_0
84 incr test_flag_0
85 }
86 if [regexp "Hardware watchpoint 3: args\\\[1\\\]\[^\r\]*\r\[^\r\]*\r\[^\r\]*Old value = $args_1\[^\r\]*\r\[^\r\]*New value = [expr $args_1+1]\r" $string] {
87 incr args_1
88 incr test_flag_1
89 }
90
91 set expected_loc "bogus location"
92 if { $test_flag_0 == 1 && $test_flag_1 == 0 && $args_0 == 1 } {
93 set expected_loc $main_loc
94 } elseif { $test_flag_0 == 0 && $test_flag_1 == 1 && $args_1 == 1 } {
95 set expected_loc $main_loc
96 } elseif { $test_flag_0 == 1 && $test_flag_1 == 0 } {
97 set expected_loc $thread0_loc
98 } elseif { $test_flag_0 == 0 && $test_flag_1 == 1 } {
99 set expected_loc $thread1_loc
100 } elseif { $test_flag_0 + $test_flag_1 == 2 } {
101 # On S/390, or any other system which can not report the
102 # stopped data address, it is OK to report two watchpoints
103 # at once in this test. Make sure the reported location
104 # corresponds to at least one of the watchpoints (and not,
105 # e.g., __nptl_create_event). On other systems, we should
106 # report the two watchpoints serially.
107 if { [target_no_stopped_data] } {
108 set expected_loc "($main_loc|$thread0_loc|$thread1_loc)"
109 }
110 }
111
112 if [ regexp "$expected_loc" $string ] {
113 set test_flag 1
114 } else {
115 fail "threaded watch loop"
116 }
117
118 # If one of the watchpoints is disabled, we'd better not stop there.
119 if { !$hwwp_2_enabled && $test_flag_0 } {
120 fail "disabled hw watchpoint 2 triggered"
121 }
122 if { !$hwwp_3_enabled && $test_flag_1 } {
123 fail "disabled hw watchpoint 3 triggered"
124 }
125
126 # If we get to 10 in one of the watched locations disable it so we
127 # see some of the other watched location.
128 # 10 is chosen so we're guaranteed to come through here.
129 if { $hwwp_2_enabled && $hwwp_3_enabled } {
130 if { $args_0 >= 10 && $hwwp_2_enabled } {
131 gdb_test_no_output "disable 2" "disable first watchpoint at 10"
132 set hwwp_2_enabled 0
133 } elseif { $args_1 >= 10 && $hwwp_3_enabled } {
134 gdb_test_no_output "disable 3" "disable first watchpoint at 10"
135 set hwwp_3_enabled 0
136 }
137 }
138 }
139 }
140
141 # If we fail above, don't bother continuing loop
142 if { $test_flag == 0 } {
143 set i 30;
144 }
145 }
146
147 # Print success message if loop succeeded.
148 if { $test_flag == 1 } {
149 pass "threaded watch loop"
150 }
151
152 # Verify that we hit first watchpoint in main thread.
153 set message "first watchpoint on args\[0\] hit"
154 if { $args_0 > 0 } {
155 pass $message
156 } else {
157 fail $message
158 }
159
160 # Verify that we hit second watchpoint in main thread.
161 set message "first watchpoint on args\[1\] hit"
162 if { $args_1 > 0 } {
163 pass $message
164 } else {
165 fail $message
166 }
167
168 # Verify that we hit first watchpoint in child thread.
169 set message "watchpoint on args\[0\] hit in thread"
170 if { $args_0 > 1 } {
171 pass $message
172 } else {
173 fail $message
174 }
175
176 # Verify that we hit second watchpoint in child thread.
177 set message "watchpoint on args\[1\] hit in thread"
178 if { $args_1 > 1 } {
179 pass $message
180 } else {
181 fail $message
182 }
183
184 # Verify that all watchpoint hits are accounted for.
185 set message "combination of threaded watchpoints = 30"
186 if { [target_no_stopped_data] } {
187 # See above. If we allow two watchpoints to be hit at once, we
188 # may have more than 30 hits total.
189 set result [expr $args_0 + $args_1 >= 30]
190 } else {
191 set result [expr $args_0 + $args_1 == 30]
192 }
193 if { $result } {
194 pass $message
195 } else {
196 fail $message
197 }
This page took 0.187461 seconds and 5 git commands to generate.