CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/WebSocket/MemorySubscriber.cpp
Views: 1401
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 <algorithm>
19
#include <cstring>
20
#include <mutex>
21
#include "Common/Data/Encoding/Base64.h"
22
#include "Common/StringUtils.h"
23
#include "Core/Core.h"
24
#include "Core/Debugger/WebSocket/MemorySubscriber.h"
25
#include "Core/Debugger/WebSocket/WebSocketUtils.h"
26
#include "Core/HLE/ReplaceTables.h"
27
#include "Core/MemMap.h"
28
#include "Core/MIPS/MIPSDebugInterface.h"
29
#include "Core/Reporting.h"
30
#include "Core/System.h"
31
32
DebuggerSubscriber *WebSocketMemoryInit(DebuggerEventHandlerMap &map) {
33
// No need to bind or alloc state, these are all global.
34
map["memory.read_u8"] = &WebSocketMemoryReadU8;
35
map["memory.read_u16"] = &WebSocketMemoryReadU16;
36
map["memory.read_u32"] = &WebSocketMemoryReadU32;
37
map["memory.read"] = &WebSocketMemoryRead;
38
map["memory.readString"] = &WebSocketMemoryReadString;
39
map["memory.write_u8"] = &WebSocketMemoryWriteU8;
40
map["memory.write_u16"] = &WebSocketMemoryWriteU16;
41
map["memory.write_u32"] = &WebSocketMemoryWriteU32;
42
map["memory.write"] = &WebSocketMemoryWrite;
43
44
return nullptr;
45
}
46
47
struct AutoDisabledReplacements {
48
AutoDisabledReplacements() {}
49
AutoDisabledReplacements(AutoDisabledReplacements &&other);
50
AutoDisabledReplacements(const AutoDisabledReplacements &) = delete;
51
AutoDisabledReplacements &operator =(const AutoDisabledReplacements &) = delete;
52
~AutoDisabledReplacements();
53
54
Memory::MemoryInitedLock *lock = nullptr;
55
std::map<u32, u32> replacements;
56
std::vector<u32> emuhacks;
57
bool saved = false;
58
bool wasStepping = false;
59
};
60
61
// Important: Only use keepReplacements when reading, not writing.
62
static AutoDisabledReplacements LockMemoryAndCPU(uint32_t addr, bool keepReplacements) {
63
AutoDisabledReplacements result;
64
if (Core_IsStepping()) {
65
result.wasStepping = true;
66
} else {
67
Core_EnableStepping(true, "memory.access", addr);
68
Core_WaitInactive();
69
}
70
71
result.lock = new Memory::MemoryInitedLock();
72
if (!keepReplacements) {
73
result.saved = true;
74
// Okay, save so we can restore later.
75
result.replacements = SaveAndClearReplacements();
76
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
77
if (MIPSComp::jit)
78
result.emuhacks = MIPSComp::jit->SaveAndClearEmuHackOps();
79
}
80
return result;
81
}
82
83
AutoDisabledReplacements::AutoDisabledReplacements(AutoDisabledReplacements &&other) {
84
lock = other.lock;
85
other.lock = nullptr;
86
replacements = std::move(other.replacements);
87
emuhacks = std::move(other.emuhacks);
88
saved = other.saved;
89
other.saved = false;
90
wasStepping = other.wasStepping;
91
other.wasStepping = true;
92
}
93
94
AutoDisabledReplacements::~AutoDisabledReplacements() {
95
if (saved) {
96
std::lock_guard<std::recursive_mutex> guard(MIPSComp::jitLock);
97
if (MIPSComp::jit)
98
MIPSComp::jit->RestoreSavedEmuHackOps(emuhacks);
99
RestoreSavedReplacements(replacements);
100
}
101
if (!wasStepping)
102
Core_EnableStepping(false);
103
delete lock;
104
}
105
106
// Read a byte from memory (memory.read_u8)
107
//
108
// Parameters:
109
// - address: unsigned integer
110
//
111
// Response (same event name):
112
// - value: unsigned integer
113
void WebSocketMemoryReadU8(DebuggerRequest &req) {
114
uint32_t addr;
115
if (!req.ParamU32("address", &addr, false)) {
116
return;
117
}
118
119
auto memLock = LockMemoryAndCPU(addr, true);
120
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
121
return req.Fail("CPU not started");
122
123
if (!Memory::IsValidAddress(addr)) {
124
req.Fail("Invalid address");
125
return;
126
}
127
128
JsonWriter &json = req.Respond();
129
json.writeUint("value", Memory::Read_U8(addr));
130
}
131
132
// Read two bytes from memory (memory.read_u16)
133
//
134
// Parameters:
135
// - address: unsigned integer
136
//
137
// Response (same event name):
138
// - value: unsigned integer
139
void WebSocketMemoryReadU16(DebuggerRequest &req) {
140
uint32_t addr;
141
if (!req.ParamU32("address", &addr, false)) {
142
return;
143
}
144
145
auto memLock = LockMemoryAndCPU(addr, true);
146
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
147
return req.Fail("CPU not started");
148
149
if (!Memory::IsValidAddress(addr)) {
150
req.Fail("Invalid address");
151
return;
152
}
153
154
JsonWriter &json = req.Respond();
155
json.writeUint("value", Memory::Read_U16(addr));
156
}
157
158
// Read four bytes from memory (memory.read_u32)
159
//
160
// Parameters:
161
// - address: unsigned integer
162
//
163
// Response (same event name):
164
// - value: unsigned integer
165
void WebSocketMemoryReadU32(DebuggerRequest &req) {
166
uint32_t addr;
167
if (!req.ParamU32("address", &addr, false)) {
168
return;
169
}
170
171
auto memLock = LockMemoryAndCPU(addr, true);
172
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
173
return req.Fail("CPU not started");
174
175
if (!Memory::IsValidAddress(addr)) {
176
req.Fail("Invalid address");
177
return;
178
}
179
180
JsonWriter &json = req.Respond();
181
json.writeUint("value", Memory::Read_U32(addr));
182
}
183
184
// Read bytes from memory (memory.read)
185
//
186
// Parameters:
187
// - address: unsigned integer address for the start of the memory range.
188
// - size: unsigned integer specifying size of memory range.
189
// - replacements: optional, false to ignore PPSSPP replacements in MIPS code.
190
//
191
// Response (same event name):
192
// - base64: base64 encode of binary data.
193
void WebSocketMemoryRead(DebuggerRequest &req) {
194
uint32_t addr;
195
if (!req.ParamU32("address", &addr))
196
return;
197
uint32_t size;
198
if (!req.ParamU32("size", &size))
199
return;
200
bool replacements = true;
201
if (!req.ParamBool("replacements", &replacements, DebuggerParamType::OPTIONAL))
202
return;
203
204
auto memLock = LockMemoryAndCPU(addr, replacements);
205
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
206
return req.Fail("CPU not started");
207
208
if (!Memory::IsValidAddress(addr))
209
return req.Fail("Invalid address");
210
else if (!Memory::IsValidRange(addr, size))
211
return req.Fail("Invalid size");
212
213
JsonWriter &json = req.Respond();
214
// Start a value without any actual data yet...
215
json.writeRaw("base64", "");
216
req.Flush();
217
218
// Now we'll write it directly to the stream.
219
req.ws->AddFragment(false, "\"");
220
// 65535 is an "even" number of base64 characters.
221
static const size_t CHUNK_SIZE = 65535;
222
for (size_t i = 0; i < size; i += CHUNK_SIZE) {
223
size_t left = std::min(size - i, CHUNK_SIZE);
224
req.ws->AddFragment(false, Base64Encode(Memory::GetPointerUnchecked(addr) + i, left));
225
}
226
req.ws->AddFragment(false, "\"");
227
}
228
229
// Read a NUL terminated string from memory (memory.readString)
230
//
231
// Parameters:
232
// - address: unsigned integer address for the start of the memory range.
233
// - type: optional, 'utf-8' (default) or 'base64'.
234
//
235
// Response (same event name) for 'utf8':
236
// - value: string value read.
237
//
238
// Response (same event name) for 'base64':
239
// - base64: base64 encode of binary data, not including NUL.
240
void WebSocketMemoryReadString(DebuggerRequest &req) {
241
uint32_t addr;
242
if (!req.ParamU32("address", &addr))
243
return;
244
245
auto memLock = LockMemoryAndCPU(addr, true);
246
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
247
return req.Fail("CPU not started");
248
249
std::string type = "utf-8";
250
if (!req.ParamString("type", &type, DebuggerParamType::OPTIONAL))
251
return;
252
if (type != "utf-8" && type != "base64")
253
return req.Fail("Invalid type, must be either utf-8 or base64");
254
255
if (!Memory::IsValidAddress(addr))
256
return req.Fail("Invalid address");
257
258
// Let's try to avoid crashing and get a safe length.
259
const uint8_t *p = Memory::GetPointerUnchecked(addr);
260
size_t longest = Memory::ValidSize(addr, Memory::g_MemorySize);
261
size_t len = strnlen((const char *)p, longest);
262
263
JsonWriter &json = req.Respond();
264
if (type == "utf-8") {
265
json.writeString("value", std::string((const char *)p, len));
266
} else if (type == "base64") {
267
json.writeString("base64", Base64Encode(p, len));
268
}
269
}
270
271
// Write a byte to memory (memory.write_u8)
272
//
273
// Parameters:
274
// - address: unsigned integer
275
// - value: unsigned integer
276
//
277
// Response (same event name):
278
// - value: new value, unsigned integer
279
void WebSocketMemoryWriteU8(DebuggerRequest &req) {
280
uint32_t addr, val;
281
if (!req.ParamU32("address", &addr, false)) {
282
return;
283
}
284
if (!req.ParamU32("value", &val, false)) {
285
return;
286
}
287
288
auto memLock = LockMemoryAndCPU(addr, true);
289
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
290
return req.Fail("CPU not started");
291
292
if (!Memory::IsValidAddress(addr)) {
293
req.Fail("Invalid address");
294
return;
295
}
296
currentMIPS->InvalidateICache(addr, 1);
297
Memory::Write_U8(val, addr);
298
Reporting::NotifyDebugger();
299
300
JsonWriter &json = req.Respond();
301
json.writeUint("value", Memory::Read_U8(addr));
302
}
303
304
// Write two bytes to memory (memory.write_u16)
305
//
306
// Parameters:
307
// - address: unsigned integer
308
// - value: unsigned integer
309
//
310
// Response (same event name):
311
// - value: new value, unsigned integer
312
void WebSocketMemoryWriteU16(DebuggerRequest &req) {
313
uint32_t addr, val;
314
if (!req.ParamU32("address", &addr, false)) {
315
return;
316
}
317
if (!req.ParamU32("value", &val, false)) {
318
return;
319
}
320
321
auto memLock = LockMemoryAndCPU(addr, true);
322
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
323
return req.Fail("CPU not started");
324
325
if (!Memory::IsValidAddress(addr)) {
326
req.Fail("Invalid address");
327
return;
328
}
329
currentMIPS->InvalidateICache(addr, 2);
330
Memory::Write_U16(val, addr);
331
Reporting::NotifyDebugger();
332
333
JsonWriter &json = req.Respond();
334
json.writeUint("value", Memory::Read_U16(addr));
335
}
336
337
// Write four bytes to memory (memory.write_u32)
338
//
339
// Parameters:
340
// - address: unsigned integer
341
// - value: unsigned integer
342
//
343
// Response (same event name):
344
// - value: new value, unsigned integer
345
void WebSocketMemoryWriteU32(DebuggerRequest &req) {
346
uint32_t addr, val;
347
if (!req.ParamU32("address", &addr, false)) {
348
return;
349
}
350
if (!req.ParamU32("value", &val, false)) {
351
return;
352
}
353
354
auto memLock = LockMemoryAndCPU(addr, true);
355
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
356
return req.Fail("CPU not started");
357
358
if (!Memory::IsValidAddress(addr)) {
359
req.Fail("Invalid address");
360
return;
361
}
362
currentMIPS->InvalidateICache(addr, 4);
363
Memory::Write_U32(val, addr);
364
Reporting::NotifyDebugger();
365
366
JsonWriter &json = req.Respond();
367
json.writeUint("value", Memory::Read_U32(addr));
368
}
369
370
// Write bytes to memory (memory.write)
371
//
372
// Parameters:
373
// - address: unsigned integer address for the start of the memory range.
374
// - base64: data to write, encoded as base64 string
375
//
376
// Response (same event name) with no extra data.
377
void WebSocketMemoryWrite(DebuggerRequest &req) {
378
uint32_t addr;
379
if (!req.ParamU32("address", &addr))
380
return;
381
std::string encoded;
382
if (!req.ParamString("base64", &encoded))
383
return;
384
385
auto memLock = LockMemoryAndCPU(addr, true);
386
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
387
return req.Fail("CPU not started");
388
389
std::vector<uint8_t> value = Base64Decode(&encoded[0], encoded.size());
390
uint32_t size = (uint32_t)value.size();
391
392
if (!Memory::IsValidAddress(addr))
393
return req.Fail("Invalid address");
394
else if (value.size() != (size_t)size || !Memory::IsValidRange(addr, size))
395
return req.Fail("Invalid size");
396
397
currentMIPS->InvalidateICache(addr, size);
398
Memory::MemcpyUnchecked(addr, &value[0], size);
399
Reporting::NotifyDebugger();
400
req.Respond();
401
}
402
403