Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/WebSocket/BreakpointSubscriber.cpp
3187 views
1
// Copyright (c) 2018- PPSSPP Project.
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, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include "Common/StringUtils.h"
19
#include "Core/Debugger/Breakpoints.h"
20
#include "Core/Debugger/DisassemblyManager.h"
21
#include "Core/Debugger/SymbolMap.h"
22
#include "Core/Debugger/WebSocket/BreakpointSubscriber.h"
23
#include "Core/Debugger/WebSocket/WebSocketUtils.h"
24
#include "Core/MIPS/MIPSDebugInterface.h"
25
26
DebuggerSubscriber *WebSocketBreakpointInit(DebuggerEventHandlerMap &map) {
27
// No need to bind or alloc state, these are all global.
28
map["cpu.breakpoint.add"] = &WebSocketCPUBreakpointAdd;
29
map["cpu.breakpoint.update"] = &WebSocketCPUBreakpointUpdate;
30
map["cpu.breakpoint.remove"] = &WebSocketCPUBreakpointRemove;
31
map["cpu.breakpoint.list"] = &WebSocketCPUBreakpointList;
32
33
map["memory.breakpoint.add"] = &WebSocketMemoryBreakpointAdd;
34
map["memory.breakpoint.update"] = &WebSocketMemoryBreakpointUpdate;
35
map["memory.breakpoint.remove"] = &WebSocketMemoryBreakpointRemove;
36
map["memory.breakpoint.list"] = &WebSocketMemoryBreakpointList;
37
38
return nullptr;
39
}
40
41
struct WebSocketCPUBreakpointParams {
42
uint32_t address = 0;
43
bool hasEnabled = false;
44
bool hasLog = false;
45
bool hasCondition = false;
46
bool hasLogFormat = false;
47
48
bool enabled;
49
bool log;
50
std::string condition;
51
PostfixExpression compiledCondition;
52
std::string logFormat;
53
54
bool Parse(DebuggerRequest &req) {
55
if (!currentDebugMIPS->isAlive()) {
56
req.Fail("CPU not started");
57
return false;
58
}
59
60
if (!req.ParamU32("address", &address))
61
return false;
62
63
hasEnabled = req.HasParam("enabled");
64
if (hasEnabled) {
65
if (!req.ParamBool("enabled", &enabled))
66
return false;
67
}
68
hasLog = req.HasParam("log");
69
if (hasLog) {
70
if (!req.ParamBool("log", &log))
71
return false;
72
}
73
hasCondition = req.HasParam("condition");
74
if (hasCondition) {
75
if (!req.ParamString("condition", &condition))
76
return false;
77
if (!initExpression(currentDebugMIPS, condition.c_str(), compiledCondition)) {
78
req.Fail(StringFromFormat("Could not parse expression syntax: %s", getExpressionError()));
79
return false;
80
}
81
}
82
hasLogFormat = req.HasParam("logFormat");
83
if (hasLogFormat) {
84
if (!req.ParamString("logFormat", &logFormat))
85
return false;
86
}
87
88
return true;
89
}
90
91
void Apply() {
92
if (hasCondition && !condition.empty()) {
93
BreakPointCond cond;
94
cond.debug = currentDebugMIPS;
95
cond.expressionString = condition;
96
cond.expression = compiledCondition;
97
g_breakpoints.ChangeBreakPointAddCond(address, cond);
98
} else if (hasCondition && condition.empty()) {
99
g_breakpoints.ChangeBreakPointRemoveCond(address);
100
}
101
102
if (hasLogFormat) {
103
g_breakpoints.ChangeBreakPointLogFormat(address, logFormat);
104
}
105
106
// TODO: Fix this interface.
107
if (hasLog && !hasEnabled) {
108
g_breakpoints.IsAddressBreakPoint(address, &enabled);
109
hasEnabled = true;
110
}
111
if (hasLog && hasEnabled) {
112
BreakAction result = BREAK_ACTION_IGNORE;
113
if (log)
114
result |= BREAK_ACTION_LOG;
115
if (enabled)
116
result |= BREAK_ACTION_PAUSE;
117
g_breakpoints.ChangeBreakPoint(address, result);
118
} else if (hasEnabled) {
119
g_breakpoints.ChangeBreakPoint(address, enabled);
120
}
121
}
122
};
123
124
// Add a new CPU instruction breakpoint (cpu.breakpoint.add)
125
//
126
// Parameters:
127
// - address: unsigned integer address of instruction to break at.
128
// - enabled: optional boolean, whether to actually enter stepping when this breakpoint trips.
129
// - log: optional boolean, whether to log when this breakpoint trips.
130
// - condition: optional string expression to evaluate - breakpoint does not trip if false.
131
// - logFormat: optional string to log when breakpoint trips, may include {expression} parts.
132
//
133
// Response (same event name) with no extra data.
134
//
135
// Note: will replace any breakpoint at the same address.
136
void WebSocketCPUBreakpointAdd(DebuggerRequest &req) {
137
WebSocketCPUBreakpointParams params;
138
if (!params.Parse(req))
139
return;
140
141
g_breakpoints.AddBreakPoint(params.address);
142
params.Apply();
143
req.Respond();
144
}
145
146
// Update a CPU instruction breakpoint (cpu.breakpoint.update)
147
//
148
// Parameters:
149
// - address: unsigned integer address of instruction to break at.
150
// - enabled: optional boolean, whether to actually enter stepping when this breakpoint trips.
151
// - log: optional boolean, whether to log when this breakpoint trips.
152
// - condition: optional string expression to evaluate - breakpoint does not trip if false.
153
// - logFormat: optional string to log when breakpoint trips, may include {expression} parts.
154
//
155
// Response (same event name) with no extra data.
156
void WebSocketCPUBreakpointUpdate(DebuggerRequest &req) {
157
WebSocketCPUBreakpointParams params;
158
if (!params.Parse(req))
159
return;
160
bool enabled;
161
if (!g_breakpoints.IsAddressBreakPoint(params.address, &enabled))
162
return req.Fail("Breakpoint not found");
163
164
params.Apply();
165
req.Respond();
166
}
167
168
// Remove a CPU instruction breakpoint (cpu.breakpoint.remove)
169
//
170
// Parameters:
171
// - address: unsigned integer address of instruction to break at.
172
//
173
// Response (same event name) with no extra data.
174
void WebSocketCPUBreakpointRemove(DebuggerRequest &req) {
175
if (!currentDebugMIPS->isAlive()) {
176
return req.Fail("CPU not started");
177
}
178
179
uint32_t address;
180
if (!req.ParamU32("address", &address))
181
return;
182
183
g_breakpoints.RemoveBreakPoint(address);
184
req.Respond();
185
}
186
187
// List all CPU instruction breakpoints (cpu.breakpoint.list)
188
//
189
// No parameters.
190
//
191
// Response (same event name):
192
// - breakpoints: array of objects, each with properties:
193
// - address: unsigned integer address of instruction to break at.
194
// - enabled: boolean, whether to actually enter stepping when this breakpoint trips.
195
// - log: optional boolean, whether to log when this breakpoint trips.
196
// - condition: null, or string expression to evaluate - breakpoint does not trip if false.
197
// - logFormat: null, or string to log when breakpoint trips, may include {expression} parts.
198
// - symbol: null, or string label or symbol at breakpoint address.
199
// - code: string disassembly of breakpoint address.
200
void WebSocketCPUBreakpointList(DebuggerRequest &req) {
201
if (!currentDebugMIPS->isAlive()) {
202
return req.Fail("CPU not started");
203
}
204
205
JsonWriter &json = req.Respond();
206
json.pushArray("breakpoints");
207
auto bps = g_breakpoints.GetBreakpoints();
208
for (const auto &bp : bps) {
209
if (bp.temporary)
210
continue;
211
212
json.pushDict();
213
json.writeUint("address", bp.addr);
214
json.writeBool("enabled", bp.IsEnabled());
215
json.writeBool("log", (bp.result & BREAK_ACTION_LOG) != 0);
216
if (bp.hasCond)
217
json.writeString("condition", bp.cond.expressionString);
218
else
219
json.writeNull("condition");
220
if (!bp.logFormat.empty())
221
json.writeString("logFormat", bp.logFormat);
222
else
223
json.writeNull("logFormat");
224
std::string symbol = g_symbolMap->GetLabelString(bp.addr);
225
if (symbol.empty())
226
json.writeNull("symbol");
227
else
228
json.writeString("symbol", symbol);
229
230
DisassemblyLineInfo line;
231
g_disassemblyManager.getLine(g_disassemblyManager.getStartAddress(bp.addr), true, line, currentDebugMIPS);
232
json.writeString("code", line.name + " " + line.params);
233
234
json.pop();
235
}
236
json.pop();
237
}
238
239
struct WebSocketMemoryBreakpointParams {
240
uint32_t address = 0;
241
uint32_t end = 0;
242
bool hasEnabled = false;
243
bool hasLog = false;
244
bool hasCond = false;
245
bool hasCondition = false;
246
bool hasLogFormat = false;
247
248
bool enabled = true;
249
bool log = true;
250
MemCheckCondition cond = MEMCHECK_READWRITE;
251
std::string condition;
252
PostfixExpression compiledCondition;
253
std::string logFormat;
254
255
bool Parse(DebuggerRequest &req) {
256
if (!currentDebugMIPS->isAlive()) {
257
req.Fail("CPU not started");
258
return false;
259
}
260
261
if (!req.ParamU32("address", &address))
262
return false;
263
uint32_t size;
264
if (!req.ParamU32("size", &size))
265
return false;
266
if (address + size < address) {
267
req.Fail("Size is too large");
268
return false;
269
}
270
end = size == 0 ? 0 : address + size;
271
272
hasEnabled = req.HasParam("enabled");
273
if (hasEnabled) {
274
if (!req.ParamBool("enabled", &enabled))
275
return false;
276
}
277
hasLog = req.HasParam("log");
278
if (hasLog) {
279
if (!req.ParamBool("log", &log))
280
return false;
281
}
282
hasCond = req.HasParam("read") || req.HasParam("write") || req.HasParam("change");
283
if (hasCond) {
284
bool read = false, write = false, change = false;
285
if (!req.ParamBool("read", &read, DebuggerParamType::OPTIONAL) || !req.ParamBool("write", &write, DebuggerParamType::OPTIONAL) || !req.ParamBool("change", &change, DebuggerParamType::OPTIONAL))
286
return false;
287
int bits = (read ? MEMCHECK_READ : 0) | (write ? MEMCHECK_WRITE : 0) | (change ? MEMCHECK_WRITE_ONCHANGE : 0);
288
cond = MemCheckCondition(bits);
289
}
290
hasCondition = req.HasParam("condition");
291
if (hasCondition) {
292
if (!req.ParamString("condition", &condition))
293
return false;
294
if (!initExpression(currentDebugMIPS, condition.c_str(), compiledCondition)) {
295
req.Fail(StringFromFormat("Could not parse expression syntax: %s", getExpressionError()));
296
return false;
297
}
298
}
299
hasLogFormat = req.HasParam("logFormat");
300
if (hasLogFormat) {
301
if (!req.ParamString("logFormat", &logFormat))
302
return false;
303
}
304
305
return true;
306
}
307
308
BreakAction Result(bool adding) {
309
int bits = MEMCHECK_READWRITE;
310
if (adding || (hasLog && hasEnabled)) {
311
bits = (enabled ? BREAK_ACTION_PAUSE : 0) | (log ? BREAK_ACTION_LOG : 0);
312
} else {
313
MemCheck prev;
314
if (g_breakpoints.GetMemCheck(address, end, &prev))
315
bits = prev.result;
316
317
if (hasEnabled)
318
bits = (bits & ~BREAK_ACTION_PAUSE) | (enabled ? BREAK_ACTION_PAUSE : 0);
319
if (hasLog)
320
bits = (bits & ~BREAK_ACTION_LOG) | (log ? BREAK_ACTION_LOG : 0);
321
}
322
323
return BreakAction(bits);
324
}
325
326
void Apply() {
327
if (hasCondition && !condition.empty()) {
328
BreakPointCond cond;
329
cond.debug = currentDebugMIPS;
330
cond.expressionString = condition;
331
cond.expression = compiledCondition;
332
g_breakpoints.ChangeMemCheckAddCond(address, end, cond);
333
} else if (hasCondition && condition.empty()) {
334
g_breakpoints.ChangeMemCheckRemoveCond(address, end);
335
}
336
if (hasLogFormat) {
337
g_breakpoints.ChangeMemCheckLogFormat(address, end, logFormat);
338
}
339
}
340
};
341
342
// Add a new memory breakpoint (memory.breakpoint.add)
343
//
344
// Parameters:
345
// - address: unsigned integer address for the start of the memory range.
346
// - size: unsigned integer specifying size of memory range.
347
// - enabled: optional boolean, whether to actually enter stepping when this breakpoint trips.
348
// - log: optional boolean, whether to log when this breakpoint trips.
349
// - read: optional boolean, whether to trip on any read to this address.
350
// - write: optional boolean, whether to trip on any write to this address.
351
// - change: optional boolean, whether to trip on a write to this address which modifies data
352
// (or any write that may modify data.)
353
// - condition: optional string expression to evaluate - breakpoint does not trip if false.
354
// - logFormat: optional string to log when breakpoint trips, may include {expression} parts.
355
//
356
// Response (same event name) with no extra data.
357
//
358
// Note: will replace any breakpoint that has the same start address and size.
359
void WebSocketMemoryBreakpointAdd(DebuggerRequest &req) {
360
WebSocketMemoryBreakpointParams params;
361
if (!params.Parse(req))
362
return;
363
364
g_breakpoints.AddMemCheck(params.address, params.end, params.cond, params.Result(true));
365
params.Apply();
366
req.Respond();
367
}
368
369
// Update a memory breakpoint (memory.breakpoint.update)
370
//
371
// Parameters:
372
// - address: unsigned integer address for the start of the memory range.
373
// - size: unsigned integer specifying size of memory range.
374
// - enabled: optional boolean, whether to actually enter stepping when this breakpoint trips.
375
// - log: optional boolean, whether to log when this breakpoint trips.
376
// - read: optional boolean, whether to trip on any read to this address.
377
// - write: optional boolean, whether to trip on any write to this address.
378
// - change: optional boolean, whether to trip on a write to this address which modifies data
379
// (or any write that may modify data.)
380
// - condition: optional string expression to evaluate - breakpoint does not trip if false.
381
// - logFormat: optional string to log when breakpoint trips, may include {expression} parts.
382
//
383
// Response (same event name) with no extra data.
384
void WebSocketMemoryBreakpointUpdate(DebuggerRequest &req) {
385
WebSocketMemoryBreakpointParams params;
386
if (!params.Parse(req))
387
return;
388
389
MemCheck mc;
390
if (!g_breakpoints.GetMemCheck(params.address, params.end, &mc))
391
return req.Fail("Breakpoint not found");
392
393
g_breakpoints.ChangeMemCheck(params.address, params.end, params.cond, params.Result(true));
394
params.Apply();
395
req.Respond();
396
}
397
398
// Remove a memory breakpoint (memory.breakpoint.remove)
399
//
400
// Parameters:
401
// - address: unsigned integer address for the start of the memory range.
402
// - size: unsigned integer specifying size of memory range.
403
//
404
// Response (same event name) with no extra data.
405
void WebSocketMemoryBreakpointRemove(DebuggerRequest &req) {
406
if (!currentDebugMIPS->isAlive()) {
407
return req.Fail("CPU not started");
408
}
409
410
uint32_t address;
411
if (!req.ParamU32("address", &address))
412
return;
413
uint32_t size;
414
if (!req.ParamU32("size", &size))
415
return;
416
417
g_breakpoints.RemoveMemCheck(address, size == 0 ? 0 : address + size);
418
req.Respond();
419
}
420
421
// List all memory breakpoints (memory.breakpoint.list)
422
//
423
// No parameters.
424
//
425
// Response (same event name):
426
// - breakpoints: array of objects, each with properties:
427
// - address: unsigned integer address for the start of the memory range.
428
// - size: unsigned integer specifying size of memory range.
429
// - enabled: boolean, whether to actually enter stepping when this breakpoint trips.
430
// - log: optional boolean, whether to log when this breakpoint trips.
431
// - read: optional boolean, whether to trip on any read to this address.
432
// - write: optional boolean, whether to trip on any write to this address.
433
// - change: optional boolean, whether to trip on a write to this address which modifies data
434
// (or any write that may modify data.)
435
// - condition: null, or string expression to evaluate - breakpoint does not trip if false.
436
// - logFormat: null, or string to log when breakpoint trips, may include {expression} parts.
437
// - symbol: null, or string label or symbol at breakpoint address.
438
void WebSocketMemoryBreakpointList(DebuggerRequest &req) {
439
if (!currentDebugMIPS->isAlive()) {
440
return req.Fail("CPU not started");
441
}
442
443
JsonWriter &json = req.Respond();
444
json.pushArray("breakpoints");
445
auto mcs = g_breakpoints.GetMemChecks();
446
for (const auto &mc : mcs) {
447
json.pushDict();
448
json.writeUint("address", mc.start);
449
json.writeUint("size", mc.end == 0 ? 0 : mc.end - mc.start);
450
json.writeBool("enabled", mc.IsEnabled());
451
json.writeBool("log", (mc.result & BREAK_ACTION_LOG) != 0);
452
json.writeBool("read", (mc.cond & MEMCHECK_READ) != 0);
453
json.writeBool("write", (mc.cond & MEMCHECK_WRITE) != 0);
454
json.writeBool("change", (mc.cond & MEMCHECK_WRITE_ONCHANGE) != 0);
455
json.writeUint("hits", mc.numHits);
456
if (mc.hasCondition)
457
json.writeString("condition", mc.condition.expressionString);
458
else
459
json.writeNull("condition");
460
if (!mc.logFormat.empty())
461
json.writeString("logFormat", mc.logFormat);
462
else
463
json.writeNull("logFormat");
464
std::string symbol = g_symbolMap->GetLabelString(mc.start);
465
if (symbol.empty())
466
json.writeNull("symbol");
467
else
468
json.writeString("symbol", symbol);
469
470
json.pop();
471
}
472
json.pop();
473
}
474
475