summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.threads/wp-replication.exp
blob: c7a6c594f96a2e3bec7c05ab77726322d92d999b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# This testcase is part of GDB, the GNU debugger.

# Copyright 2009-2014 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Check that hardware watchpoints get correctly replicated to all
# existing threads when hardware watchpoints are created.  This test
# creates one hardware watchpoint per thread until a maximum is
# reached.  It originally addresses a deficiency seen on embedded
# powerpc targets with slotted hardware *point designs.

set NR_THREADS 10
set NR_TRIGGERS_PER_THREAD 2

# This test verifies that a hardware watchpoint gets replicated to
# every existing thread and is detected properly.  This test is
# only meaningful on a target with hardware watchpoint support.
if {[skip_hw_watchpoint_tests]} {
    return 0
}

standard_testfile
if {[gdb_compile_pthreads "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable [list debug "additional_flags=-DNR_THREADS=$NR_THREADS -DNR_TRIGGERS_PER_THREAD=$NR_TRIGGERS_PER_THREAD"]] != "" } {
    return -1
}

clean_restart ${binfile}

# Force hardware watchpoints to be used.
gdb_test_no_output "set can-use-hw-watchpoints 1" ""

# Run to `main' where we begin our tests.
if ![runto_main] then {
    fail "Failed to run to main"
    return 0
}

# First, break at empty_cycle.
gdb_test "break empty_cycle" \
  "Breakpoint 2 at .*: file .*${srcfile}, line .*" \
  "Breakpoint on empty_cycle"

# Set some default values.
set hwatch_count 0
set done 0

# Count the number of hardware watchpoints available on
# this target.
while { $done == 0 } {

  gdb_test "continue" \
    ".*Breakpoint 2, empty_cycle \\(\\) at .*${srcfile}.*" \
    "Continue to empty_cycle to insert watchpoint $hwatch_count"

  # Some targets do resource counting as we insert watchpoints.
  # Such targets won't cause a watchpoint insertion failure, but
  # will switch to software watchpoints silently.  We check for
  # both cases here.
  gdb_test_multiple "watch watched_data\[$hwatch_count\]" \
       "watch watched_data\[$hwatch_count\]" {
    -re "Hardware watchpoint .*$gdb_prompt $" {
    }
    -re "Watchpoint .*$gdb_prompt $" {
      set done 1
      break
    }
  }

  gdb_test_multiple "continue" "watchpoint created successfully" {
    -re ".*Breakpoint 2, empty_cycle \\(\\).*$gdb_prompt $" {
      incr hwatch_count

      # Some targets (like S/390) behave as though supporting
      # unlimited hardware watchpoints.  In this case we just take a
      # safe exit out of the loop.
      if { $hwatch_count == $NR_THREADS } {
	set done 1
	break
      }
    }
    -re ".*Could not insert hardware watchpoint.*$gdb_prompt $" {
      set done 1
      break
    }
  }
}

# Target cannot insert hardware watchpoints.  It should have reported
# (through board settings) that it did not support them in the first place.
# Just exit.
if { $hwatch_count == 0} {
  fail "No hardware watchpoints available"
  return 0
}

# Set the testcase's internal variable indicating the number of
# hardware watchpoints the target supports.
gdb_test_no_output "set var hw_watch_count=${hwatch_count}" \
		   "set var hw_watch_count=${hwatch_count}"

# At this point, we know how many hardware watchpoints
# the target supports.  Use that to do further testing.
delete_breakpoints

# Break out of the empty_cycle loop by changing the
# controlling variable.
gdb_test_no_output "set var watch_count_done=1" \
		   "set var watch_count_done=1"

# Prepare to create all the threads.
gdb_test "break thread_started" \
	 "Breakpoint \[0-9\]+ at .*: file .*${srcfile}, line .*" \
	 "Breakpoint on thread_started"

# Move all threads to where they're supposed to be for testing.
for { set i 0 } { $i < $NR_THREADS } { incr i } {

    # We want to set the maximum number of hardware watchpoints
    # and make sure the target can handle that without an error.
    # That will show us the watchpoints got replicated to all the
    # threads correctly, and that no new watchpoints got created
    # in the background for a specific thread.
    if {$i < $hwatch_count} {
      gdb_test "watch watched_data\[$i\]" \
	"Hardware watchpoint .*" \
	"watch watched_data\[$i\]"
    } else {
      verbose -log "Not setting watchpoint for watched_data\[$i\]\n"
    }

    gdb_test continue "Continuing\\..*Breakpoint \[0-9\]+, thread_started \\(\\) at .*$srcfile.*" \
    "Thread $i hit breakpoint at thread_started"
}

# Let the threads run and change the watched data, leading
# to watchpoint triggers.
gdb_test_no_output "set var test_ready=1" \
      "set var test_ready=1"

# Set the number of expected watchpoint triggers.
set TRIGGERS [expr "$NR_THREADS * $hwatch_count * $NR_TRIGGERS_PER_THREAD"]

# Move the threads and hit the watchpoints TRIGGERS times.
for { set i 1 } { $i <= $TRIGGERS } { incr i } {
    gdb_test continue "Continuing\\..*Hardware watchpoint \[0-9\]+: watched_data\[\[0-9\]+\].*Old value = \[0-9\]+.*New value = \[0-9\]+.*thread_function \\(arg=$hex\\) at .*$srcfile.*" \
    "Continue to watchpoint trigger $i out of ${TRIGGERS} on watched_data"
}