-
Notifications
You must be signed in to change notification settings - Fork 20
/
halt_watches.go
281 lines (242 loc) · 7.07 KB
/
halt_watches.go
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// This file is part of Gopher2600.
//
// Gopher2600 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.
//
// Gopher2600 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 Gopher2600. If not, see <https://www.gnu.org/licenses/>.
package debugger
import (
"fmt"
"strconv"
"strings"
"github.com/jetsetilly/gopher2600/debugger/dbgmem"
"github.com/jetsetilly/gopher2600/debugger/terminal"
"github.com/jetsetilly/gopher2600/debugger/terminal/commandline"
)
type watcher struct {
ai dbgmem.AddressInfo
// whether to watch for a specific value. a matchValue of false means the
// watcher will match regardless of the value
matchValue bool
value uint8
// whether the address should be interpreted strictly or whether mirrors
// should be considered too
strict bool
// whether the watcher should match phantom accesses too
phantom bool
}
func (w watcher) String() string {
val := ""
if w.matchValue {
val = fmt.Sprintf(" (value=%#02x)", w.value)
}
event := "write"
if w.ai.Read {
event = "read"
}
strict := ""
if w.strict {
strict = " (strict)"
}
return fmt.Sprintf("%s %s%s%s", w.ai, event, val, strict)
}
// the list of currently defined watches in the system.
type watches struct {
dbg *Debugger
watches []watcher
lastAddressAccessed uint16
lastAddressWrite bool
}
// newWatches is the preferred method of initialisation for the watches type.
func newWatches(dbg *Debugger) *watches {
wtc := &watches{
dbg: dbg,
}
wtc.clear()
return wtc
}
// clear all watches.
func (wtc *watches) clear() {
wtc.watches = make([]watcher, 0, 10)
}
// drop a specific watcher by a position in the list.
func (wtc *watches) drop(num int) error {
if len(wtc.watches)-1 < num {
return fmt.Errorf("watch #%d is not defined", num)
}
h := wtc.watches[:num]
t := wtc.watches[num+1:]
wtc.watches = make([]watcher, len(h)+len(t), cap(wtc.watches))
copy(wtc.watches, h)
copy(wtc.watches[len(h):], t)
return nil
}
// check compares the current state of the emulation with every watch
// condition. returns a string listing every condition that matches (separated
// by \n).
func (wtc *watches) check() string {
if len(wtc.watches) == 0 {
return ""
}
// no check if access address & write flag haven't changed
//
// note that the write flag comparison is required otherwise RMW
// instructions will not be caught on the write signal (which would mean
// that a WRITE watch will never match a RMW instruction)
if wtc.lastAddressAccessed == wtc.dbg.vcs.Mem.LastCPUAddressLiteral && wtc.lastAddressWrite == wtc.dbg.vcs.Mem.LastCPUWrite {
return ""
}
checkString := strings.Builder{}
for _, w := range wtc.watches {
// filter phantom accesses
if !w.phantom && wtc.dbg.vcs.CPU.PhantomMemAccess {
return ""
}
// pick which addresses to compare depending on whether watch is strict
if w.strict {
if wtc.dbg.vcs.Mem.LastCPUAddressLiteral != w.ai.Address {
continue
}
} else {
if wtc.dbg.vcs.Mem.LastCPUAddressMapped != w.ai.MappedAddress {
continue
}
}
if w.matchValue && w.value != wtc.dbg.vcs.Mem.LastCPUData {
continue
}
lai := wtc.dbg.dbgmem.GetAddressInfo(wtc.dbg.vcs.Mem.LastCPUAddressLiteral, !wtc.dbg.vcs.Mem.LastCPUWrite)
if w.ai.Read {
if !wtc.dbg.vcs.Mem.LastCPUWrite {
checkString.WriteString(fmt.Sprintf("watch at %s (read value %#02x)", lai, wtc.dbg.vcs.Mem.LastCPUData))
}
} else {
if wtc.dbg.vcs.Mem.LastCPUWrite {
checkString.WriteString(fmt.Sprintf("watch at %s (written value %#02x)", lai, wtc.dbg.vcs.Mem.LastCPUData))
}
}
if wtc.dbg.vcs.CPU.PhantomMemAccess {
checkString.WriteString(" phantom")
}
if checkString.Len() > 0 {
checkString.WriteRune('\n')
}
}
// note what the last address accessed was
wtc.lastAddressAccessed = wtc.dbg.vcs.Mem.LastCPUAddressLiteral
wtc.lastAddressWrite = wtc.dbg.vcs.Mem.LastCPUWrite
return checkString.String()
}
// list currently defined watches.
func (wtc *watches) list() {
if len(wtc.watches) == 0 {
wtc.dbg.printLine(terminal.StyleFeedback, "no watches")
} else {
wtc.dbg.printLine(terminal.StyleFeedback, "watches:")
for i := range wtc.watches {
wtc.dbg.printLine(terminal.StyleFeedback, "% 2d: %s", i, wtc.watches[i])
}
}
}
// parse tokens and add new watch. unlike breakpoints and traps, only one watch
// at a time can be specified on the command line.
func (wtc *watches) parseCommand(tokens *commandline.Tokens) error {
var read bool
var strict bool
var phantom bool
// event type
arg, _ := tokens.Get()
arg = strings.ToUpper(arg)
switch arg {
case "READ":
read = true
case "WRITE":
read = false
default:
// default watch event is READ
read = true
tokens.Unget()
}
// strict addressing or not
arg, _ = tokens.Get()
arg = strings.ToUpper(arg)
switch arg {
case "STRICT":
strict = true
default:
strict = false
tokens.Unget()
}
// strict addressing or not
arg, _ = tokens.Get()
arg = strings.ToUpper(arg)
switch arg {
case "PHANTOM":
fallthrough
case "GHOST":
phantom = true
default:
phantom = false
tokens.Unget()
}
// get address. required.
a, _ := tokens.Get()
// convert address
var ai *dbgmem.AddressInfo
if read {
ai = wtc.dbg.dbgmem.GetAddressInfo(a, true)
} else {
ai = wtc.dbg.dbgmem.GetAddressInfo(a, false)
}
// mapping of the address was unsuccessful
if ai == nil {
if read {
return fmt.Errorf("invalid watch address (%s) expecting 16-bit address or a read symbol", a)
}
return fmt.Errorf("invalid watch address (%s) expecting 16-bit address or a write symbol", a)
}
// get value if possible
var val uint64
var err error
v, useVal := tokens.Get()
if useVal {
val, err = strconv.ParseUint(v, 0, 8)
if err != nil {
return fmt.Errorf("invalid watch value (%s) expecting 8-bit value", a)
}
}
nw := watcher{
ai: *ai,
matchValue: useVal,
value: uint8(val),
strict: strict,
phantom: phantom,
}
// check to see if watch already exists
for _, w := range wtc.watches {
// the conditions for a watch matching are very specific: both must
// have the same address, be the same /type/ of address (read or
// write), and the same watch value (if applicable)
//
// note that this method means we can add a watch that is a subset of
// an existing watch (or vice-versa) but that's okay, the check()
// function will list all matches. plus, if we combine two watches such
// that only the larger set remains, it may confuse the user
if w.ai.Address == nw.ai.Address &&
w.ai.Read == nw.ai.Read &&
w.matchValue == nw.matchValue && w.value == nw.value {
return fmt.Errorf("already being watched (%s)", w)
}
}
// add watch
wtc.watches = append(wtc.watches, nw)
return nil
}