xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.threads/step-over-trips-on-watchpoint.exp (revision eceb233b9bd0dfebb902ed73b531ae6964fa3f9b)
1# Copyright (C) 2014-2019 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# Test that when a step-over trips on a watchpoint, that watchpoint is
17# reported.
18
19standard_testfile
20set executable ${testfile}
21
22# This test verifies that a watchpoint is detected in a multithreaded
23# program so the test is only meaningful on a system with hardware
24# watchpoints.
25if {[skip_hw_watchpoint_tests]} {
26    return 0
27}
28
29if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" \
30	 executable [list debug "incdir=${objdir}"]] != "" } {
31    return -1
32}
33
34# The test proper.  DISPLACED is true if we should try with displaced
35# stepping.  WITH_BP is true if we should try with a thread-specific
36# breakpoint (for the wrong thread) right after the instruction that
37# triggers the watchpoint.
38proc do_test { displaced with_bp } {
39    global executable
40    global gdb_prompt
41    global hex
42
43    if ${with_bp} {
44	set prefix "with thread-specific bp"
45    } else {
46	set prefix "no thread-specific bp"
47    }
48    with_test_prefix "displaced=$displaced: $prefix" {
49	# Cover both stepping and non-stepping execution commands.
50	foreach command {"step" "next" "continue" } {
51	    with_test_prefix $command {
52		clean_restart $executable
53
54		if ![runto_main] {
55		    continue
56		}
57
58		gdb_test_no_output "set displaced-stepping $displaced"
59
60		gdb_breakpoint [gdb_get_line_number "set wait-thread breakpoint here"]
61		gdb_continue_to_breakpoint "run to wait-thread breakpoint"
62		gdb_test "info threads" "\\\* 1 .*  2 .*" "info threads shows all threads"
63
64		gdb_test_no_output "set scheduler-locking on"
65
66		delete_breakpoints
67
68		gdb_breakpoint [gdb_get_line_number "set breakpoint child here"]
69		gdb_test "thread 2" "Switching to .*"
70		gdb_continue_to_breakpoint "run to breakpoint in thread 2"
71
72		set address_triggers_watch "<invalid>"
73		set after_address_triggers_watch "<invalid>"
74
75		# Let the watchpoint trigger once (with the other
76		# thread locked), in order to find both the address of
77		# the instruction that triggers the watchpoint and the
78		# address of the instruction immediately after.
79		with_test_prefix "find addresses" {
80		    gdb_test "p watch_me = 0" " = 0" "clear watch_me"
81		    gdb_test "watch watch_me" "Hardware watchpoint .*"
82
83		    gdb_test "continue" \
84			"Hardware watchpoint.*: watch_me.*New value = 1.*" \
85			"continue to watchpoint"
86
87		    set msg "find addresses"
88		    gdb_test_multiple "disassemble" $msg {
89			-re " ($hex) \[^\r\n\]*\r\n=> ($hex) .*$gdb_prompt $" {
90			    set address_triggers_watch $expect_out(1,string)
91			    set after_address_triggers_watch $expect_out(2,string)
92			    pass $msg
93			}
94		    }
95
96		    delete_breakpoints
97		}
98
99		gdb_test "break *$address_triggers_watch" "Breakpoint .*" \
100		    "set breakpoint at address that triggers watch"
101		gdb_continue_to_breakpoint \
102		    "run to instruction that triggers watch in thread 2"
103
104		gdb_test "p counter = 0" " = 0" "unbreak loop in thread 2"
105		gdb_test "p watch_me = 0" " = 0" "clear watch_me"
106		gdb_test "watch watch_me" "Hardware watchpoint .*"
107
108		if ${with_bp} {
109		    gdb_test "b *$after_address_triggers_watch thread 1" \
110			"Breakpoint .*" \
111			"set breakpoint specific to thread 1"
112		}
113
114		# Switch back to thread 1 and disable scheduler locking.
115		gdb_test "thread 1" "Switching to .*"
116		gdb_test_no_output "set scheduler-locking off"
117
118		# Thread 2 is still stopped at a breakpoint that needs
119		# to be stepped over.  However, the instruction that
120		# is under the breakpoint triggers a watchpoint, which
121		# should trap and be reported to the user.
122		gdb_test "$command" "Hardware watchpoint.*: watch_me.*New value = 1.*"
123	    }
124	}
125    }
126}
127
128foreach displaced { "off" "on" } {
129    if { $displaced != "off" && ![support_displaced_stepping] } {
130	continue
131    }
132
133    foreach with_bp { 0 1 } {
134	do_test $displaced $with_bp
135    }
136}
137