-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBreakPoints.cpp
348 lines (304 loc) · 11 KB
/
BreakPoints.cpp
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
//----------------------------------------------------------------------
// Copyright (c) 2022, Guy Carver
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Guy Carver may not be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// FILE BreakPoints.cpp
//----------------------------------------------------------------------
#include "BreakPoints.h"
#include "Command.h"
#include "Monitor.h"
#include "Numbers.h"
#include "Response.h"
#include <imgui.h>
#include <memory> // For shared_ptr
#include <unordered_map>
/*{
[ ] Kill all received checkpoints we didn't set
}*/
namespace BreakPoints
{
class BreakPoint;
using BreakPointPtr = std::shared_ptr<BreakPoint>;
using BreakPointContainer = std::unordered_map<uint16_t, BreakPointPtr>;
BreakPointContainer BreakPointA;
//Memory access type
enum CHECKOP : uint8_t
{
LOAD = 0_bit, //The address is loaded
STORE = 1_bit, //The address is Stored to
EXEC = 2_bit //The address is executed (breakpoint)
};
//----------------------------------------------------------------
class BreakPoint
{
public:
BreakPoint( ) = delete;
//----------------------------------------------------------------
explicit BreakPoint( uint16_t aAddress ) : Address(aAddress)
{
//Create a command, don't use an ID so we always process the responses
// without having to look for them.
pCommand = CommandPtr(new Command(COMMAND::CHECKPOINT_SET, Address));
pCommand->Add(Address); //Start address
pCommand->Add(Address); //End address
pCommand->Add(1_u8); //Stop when hit
pCommand->Add(Enabled); //Enabled state
pCommand->Add(CHECKOP::EXEC); //Execution operations
pCommand->Add(0_u16); //Not temporary and memspace 0
Monitor::Send(pCommand); //Send add command
}
//----------------------------------------------------------------
///Constructor for memory checkpoint/breakpoint
explicit BreakPoint( uint16_t aStartAddress, uint16_t aEndAddress, uint8_t aBreak = 0 ) : Address(aStartAddress)
{
//Create a command, don't use an ID so we always process the responses
// without having to look for them.
pCommand = CommandPtr(new Command(COMMAND::CHECKPOINT_SET, Address));
pCommand->Add(Address); //Start address
pCommand->Add(aEndAddress); //End address
pCommand->Add(aBreak); //Stop when hit?
pCommand->Add(Enabled); //Enabled state
pCommand->Add(CHECKOP::STORE); //Execution operations
pCommand->Add(0_u16); //Not temporary and memspace 0
Monitor::Send(pCommand); //Send add command
}
//----------------------------------------------------------------
~BreakPoint( )
{
if (Legit()) {
pCommand->SetCommand(COMMAND::CHECKPOINT_DEL);
pCommand->Reset();
pCommand->Add(Index); //ID of checkpoint to delete
Monitor::Send(pCommand); //Send delete command
}
}
//----------------------------------------------------------------
uint32_t QID( ) const { return pCommand->QID(); }
//----------------------------------------------------------------
///Return true if a VICE checkpoint number has been set, otherwise
/// VICE hasn't replied that it's aware of this breakpoint
bool Legit( ) const { return Index != 0xFFFFFFFF; }
//----------------------------------------------------------------
bool QEnabled( ) const { return Enabled != 0; }
//----------------------------------------------------------------
///If not already desired state then toggle
void Enable( bool abTF ) { if (Enabled != static_cast<uint8_t>(abTF)) Toggle(); }
//----------------------------------------------------------------
///Send VICE command to toggle enable state
void Toggle( )
{
if (Legit()) {
//NOTE: We set the local state here because VICE doesn't
// send a response. So we assume VICE changed the enable state
Enabled = !Enabled;
pCommand->SetCommand(COMMAND::CHECKPOINT_TGL);
pCommand->Reset();
pCommand->Add(Index);
pCommand->Add(Enabled);
Monitor::Send(pCommand);
}
}
CommandPtr pCommand; //Command object used to control this CheckPoint
uint32_t Index = 0xFFFFFFFF; //Index for breakpoint assigned by VICE
uint16_t Address; //Address of the breakpoint
uint8_t Enabled = true; //Enabled state
};
//----------------------------------------------------------------
bool ForEach( BREAKPOINTFN aFunction )
{
bool bres = true;
//Loop for each breakpoint and call the function with the address and enable state
for ( const auto &entry : BreakPointA ) {
auto addr = entry.second->Address;
auto enabled = entry.second->QEnabled();
bres = aFunction(addr, enabled);
if (!bres) {
break;
}
}
return bres;
}
//----------------------------------------------------------------
bool CheckHit( uint16_t aAddress )
{
bool bres = false;
auto entry = BreakPointA.find(aAddress);
if (entry != BreakPointA.end()) {
bres = entry->second->QEnabled();
}
return bres;
}
//----------------------------------------------------------------
void Add( uint16_t aAddress )
{
//If found in map then delete
if (auto entry = BreakPointA.find(aAddress); entry != BreakPointA.end()) {
Remove(aAddress);
}
else {
BreakPointPtr ptr(new BreakPoint(aAddress));
BreakPointA[aAddress] = ptr;
}
}
//----------------------------------------------------------------
void Add( uint16_t aStartAddress, uint16_t aEndAddress, uint8_t aBreak )
{
//If found in map then delete
//NOTE: Code breakpoints and memory checkpoints use the same map with
// the address as the key, so you can't have both for the same memory address.
// A limitation I don't expect to be much of an issue
if (auto entry = BreakPointA.find(aStartAddress); entry != BreakPointA.end()) {
Remove(aStartAddress);
}
else {
BreakPointPtr ptr(new BreakPoint(aStartAddress, aEndAddress, aBreak));
BreakPointA[aStartAddress] = ptr;
}
}
//----------------------------------------------------------------
void Remove( uint16_t aAddress )
{
BreakPointA.erase(aAddress); //Remove BreakPoint from map
}
//----------------------------------------------------------------
void Enable( uint16_t aAddress, bool abTF )
{
if (auto entry = BreakPointA.find(aAddress); entry != BreakPointA.end()) {
entry->second->Enable(abTF);
}
}
//----------------------------------------------------------------
void Toggle( uint16_t aAddress )
{
if (auto entry = BreakPointA.find(aAddress); entry != BreakPointA.end()) {
entry->second->Toggle();
}
}
//----------------------------------------------------------------
void Close( )
{
BreakPointA.clear();
}
//The hope was that we'd get a response from the delete/toggle commands
// and set the state of the breakpoints based on those, but we don't
// get responses to our commands from VICE. So the functions that send
// the commands also update our state
//----------------------------------------------------------------
//void ProcessDelete( const Response &arResponse )
//{
// auto addr = static_cast<uint16_t>(arResponse.QID()); //Command ID will be the address
// BreakPointA.erase(addr);
//}
//
////----------------------------------------------------------------
//void ProcessToggle( const Response &arResponse )
//{
// auto addr = static_cast<uint16_t>(arResponse.QID()); //Command ID will be the address
// if (auto entry = BreakPointA.find(addr); entry != BreakPointA.end()) {
// entry->second->Enabled = !entry->second->Enabled;
// }
//}
//----------------------------------------------------------------
void ProcessInfo( const Response &arResponse )
{
//VICE assigns a unique number to each checkpoint, we use this to associate
// the response with a local checkpoint Instance
auto index = arResponse.Get<uint32_t>(0);
auto addr = arResponse.Get16(5);
if (auto entry = BreakPointA.find(addr); entry != BreakPointA.end()) {
entry->second->Index = index; //Make sure index is set
entry->second->Enabled = arResponse.Get8(10);
}
else {
//This isn't one of our breakpoints so delete it
auto pcmd = CommandPtr(new Command(COMMAND::CHECKPOINT_DEL, addr));
pcmd->Add(index); //ID of checkpoint to delete
Monitor::Send(pcmd); //Send delete command
}
}
bool bDisplayEnabled = false; //Display enabled state
//----------------------------------------------------------------
void Display( )
{
if (bDisplayEnabled) {
ImGui::SetNextWindowSize(ImVec2(160, 190), ImGuiCond_FirstUseEver);
ImGui::Begin("BreakPoints", &bDisplayEnabled, ImGuiWindowFlags_NoResize);
ImGui::Text("Address");
ImGui::SameLine();
ImGui::Text("Enabled");
char addrText[8];
int32_t id = 0; //Id of checkboxes
ImGui::PushItemWidth(-1.0f);
//list box of labels, addresses, enabled
if (ImGui::BeginListBox("##BPList")) {
ForEach([&]( uint16_t aAddress, bool abEnabled) {
Numbers::ToHex(addrText, aAddress);
ImGui::Text(addrText);
ImGui::SameLine();
auto pos = ImGui::GetCursorPos();
pos.x += 16.0f;
pos.y -= 2.0f;
ImGui::SetCursorPos(pos);
ImGui::PushID(id);
static bool bstate = abEnabled;
//Use bstate as checkbox bool, but the actual state
// change on click is done by Toggle()
if (ImGui::Checkbox("", &bstate)) {
Toggle(aAddress);
}
ImGui::PopID();
++id;
return true;
});
ImGui::EndListBox();
}
ImGui::PopItemWidth();
ImGui::End();
}
//TODO: Add ability to delete BreakPoints
}
//----------------------------------------------------------------
void ToJson( nlohmann::json &arData )
{
arData["BreakPoints"] = {
{"On", bDisplayEnabled}
};
}
//----------------------------------------------------------------
void FromJson( nlohmann::json &arData )
{
auto obj = arData["BreakPoints"];
if (!obj.is_null()) {
bDisplayEnabled = obj["On"];
}
}
//----------------------------------------------------------------
void DisplayOn( )
{
bDisplayEnabled = true;
}
} //namespace BreakPoints