Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Debugger/WebSocket/MemoryInfoSubscriber.cpp
5651 views
1
// Copyright (c) 2021- 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 "Core/MIPS/MIPS.h"
20
#include "Core/MIPS/MIPSDebugInterface.h"
21
#include "Core/Debugger/MemBlockInfo.h"
22
#include "Core/Debugger/WebSocket/MemoryInfoSubscriber.h"
23
#include "Core/Debugger/WebSocket/WebSocketUtils.h"
24
#include "Core/MemMap.h"
25
26
class WebSocketMemoryInfoState : public DebuggerSubscriber {
27
public:
28
WebSocketMemoryInfoState() {
29
}
30
~WebSocketMemoryInfoState() {
31
UpdateOverride(false);
32
}
33
34
void Mapping(DebuggerRequest &req);
35
void Config(DebuggerRequest &req);
36
void Set(DebuggerRequest &req);
37
void List(DebuggerRequest &req);
38
void Search(DebuggerRequest &req);
39
40
protected:
41
void UpdateOverride(bool flag);
42
43
bool detailOverride_ = false;
44
};
45
46
DebuggerSubscriber *WebSocketMemoryInfoInit(DebuggerEventHandlerMap &map) {
47
auto p = new WebSocketMemoryInfoState();
48
map["memory.mapping"] = [p](DebuggerRequest &req) { p->Mapping(req); };
49
map["memory.info.config"] = [p](DebuggerRequest &req) { p->Config(req); };
50
map["memory.info.set"] = [p](DebuggerRequest &req) { p->Set(req); };
51
map["memory.info.list"] = [p](DebuggerRequest &req) { p->List(req); };
52
map["memory.info.search"] = [p](DebuggerRequest &req) { p->Search(req); };
53
54
return p;
55
}
56
57
void WebSocketMemoryInfoState::UpdateOverride(bool flag) {
58
if (detailOverride_ && !flag)
59
MemBlockReleaseDetailed();
60
if (!detailOverride_ && flag)
61
MemBlockOverrideDetailed();
62
detailOverride_ = flag;
63
}
64
65
// List memory map data (memory.mapping)
66
//
67
// No parameters.
68
//
69
// Response (same event name):
70
// - ranges: array of objects:
71
// - type: one of "ram", "vram", "sram".
72
// - subtype: "primary" or "mirror".
73
// - name: string, friendly name.
74
// - address: number, start address of range.
75
// - size: number, in bytes.
76
void WebSocketMemoryInfoState::Mapping(DebuggerRequest &req) {
77
struct MemRange {
78
const char *type;
79
const char *subtype;
80
const char *name;
81
const uint32_t address;
82
const uint32_t size;
83
};
84
constexpr uint32_t kernelMemorySize = PSP_GetKernelMemoryEnd() - PSP_GetKernelMemoryBase();
85
constexpr uint32_t volatileMemorySize = PSP_GetVolatileMemoryEnd() - PSP_GetVolatileMemoryStart();
86
static constexpr MemRange ranges[] = {
87
{ "sram", "primary", "Scratchpad", PSP_GetScratchpadMemoryBase(), Memory::SCRATCHPAD_SIZE },
88
{ "vram", "primary", "VRAM", PSP_GetVidMemBase(), Memory::VRAM_SIZE },
89
{ "vram", "mirror", "VRAM (Swizzled)", PSP_GetVidMemBase() + Memory::VRAM_SIZE * 1, Memory::VRAM_SIZE },
90
{ "vram", "mirror", "VRAM (Mirror)", PSP_GetVidMemBase() + Memory::VRAM_SIZE * 2, Memory::VRAM_SIZE },
91
{ "vram", "mirror", "VRAM (Swizzled + Interleaved)", PSP_GetVidMemBase() + Memory::VRAM_SIZE * 3, Memory::VRAM_SIZE },
92
{ "ram", "primary", "Kernel Memory", 0x80000000 | PSP_GetKernelMemoryBase(), kernelMemorySize },
93
{ "ram", "primary", "Volatile Memory", PSP_GetVolatileMemoryStart(), volatileMemorySize },
94
// Size is specially calculated.
95
{ "ram", "primary", "User Memory", PSP_GetUserMemoryBase(), 0 },
96
};
97
98
JsonWriter &json = req.Respond();
99
json.pushArray("ranges");
100
for (auto range : ranges) {
101
uint32_t size = range.size;
102
if (size == 0) {
103
size = Memory::g_MemorySize;
104
if (size == 0) {
105
size = Memory::RAM_NORMAL_SIZE;
106
}
107
size -= kernelMemorySize + volatileMemorySize;
108
}
109
json.pushDict();
110
json.writeString("type", range.type);
111
json.writeString("subtype", range.subtype);
112
json.writeString("name", range.name);
113
json.writeUint("address", range.address);
114
json.writeUint("size", size);
115
json.pop();
116
117
// Also write the uncached range.
118
json.pushDict();
119
json.writeString("type", range.type);
120
json.writeString("subtype", "mirror");
121
json.writeString("name", std::string("Uncached ") + range.name);
122
json.writeUint("address", 0x40000000 | range.address);
123
json.writeUint("size", size);
124
json.pop();
125
}
126
json.pop();
127
}
128
129
// Update memory info tracking config (memory.info.config)
130
//
131
// Parameters:
132
// - detailed: optional, boolean to force enable detailed tracking (perf impact.)
133
//
134
// Response (same event name):
135
// - detailed: boolean state of tracking before any changes.
136
//
137
// Note: Even if you set false, may stay enabled if set by user or another debug session.
138
void WebSocketMemoryInfoState::Config(DebuggerRequest &req) {
139
bool setDetailed = req.HasParam("detailed");
140
bool detailed = false;
141
if (!req.ParamBool("detailed", &detailed, DebuggerParamType::OPTIONAL))
142
return;
143
144
JsonWriter &json = req.Respond();
145
json.writeBool("detailed", MemBlockInfoDetailed());
146
147
if (setDetailed)
148
UpdateOverride(detailed);
149
}
150
151
static MemBlockFlags FlagFromType(const std::string &type) {
152
if (type == "write")
153
return MemBlockFlags::WRITE;
154
if (type == "texture")
155
return MemBlockFlags::TEXTURE;
156
if (type == "alloc")
157
return MemBlockFlags::ALLOC;
158
if (type == "suballoc")
159
return MemBlockFlags::SUB_ALLOC;
160
if (type == "free")
161
return MemBlockFlags::FREE;
162
if (type == "subfree")
163
return MemBlockFlags::SUB_FREE;
164
return MemBlockFlags::SKIP_MEMCHECK;
165
}
166
167
static std::string TypeFromFlag(const MemBlockFlags &flag) {
168
if (flag & MemBlockFlags::WRITE)
169
return "write";
170
else if (flag & MemBlockFlags::TEXTURE)
171
return "texture";
172
else if (flag & MemBlockFlags::ALLOC)
173
return "alloc";
174
else if (flag & MemBlockFlags::SUB_ALLOC)
175
return "suballoc";
176
return "error";
177
}
178
179
// Update memory info tagging (memory.info.set)
180
//
181
// Parameters:
182
// - address: number representing start address of the range to modify.
183
// - size: number, bytes from start address.
184
// - type: string, one of:
185
// - "write" for last modification information.
186
// - "texture" for last texture usage information.
187
// - "alloc" for allocation information.
188
// - "suballoc" for allocations within an existing allocation.
189
// - "free" to mark a previous allocation and its suballocations freed (ignores tag.)
190
// - "subfree" to mark a previous suballocation freed (ignores tag.)
191
// - tag: string label to give the memory. Optional if type if free or subfree.
192
// - pc: optional, number indicating PC address for this tag.
193
//
194
// Response (same event name) with no extra data.
195
//
196
// Note: Only one tag per type is maintained for any given memory address.
197
// Small extent info may be ignored unless detailed tracking enabled (see memory.info.config.)
198
void WebSocketMemoryInfoState::Set(DebuggerRequest &req) {
199
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
200
return req.Fail("CPU not started");
201
202
std::string type;
203
if (!req.ParamString("type", &type))
204
return;
205
std::string tag;
206
if (type != "free" && type != "subfree") {
207
if (!req.ParamString("tag", &tag))
208
return;
209
}
210
uint32_t addr;
211
if (!req.ParamU32("address", &addr))
212
return;
213
uint32_t size;
214
if (!req.ParamU32("size", &size))
215
return;
216
uint32_t pc = currentMIPS->pc;
217
if (!req.ParamU32("pc", &pc, false, DebuggerParamType::OPTIONAL))
218
return;
219
220
MemBlockFlags flags = MemBlockFlags::SKIP_MEMCHECK | FlagFromType(type);
221
if (flags == MemBlockFlags::SKIP_MEMCHECK)
222
return req.Fail("Invaid type - expecting write, texture, alloc, suballoc, free, or subfree");
223
224
if (!Memory::IsValidAddress(addr))
225
return req.Fail("Invalid address");
226
else if (!Memory::IsValidRange(addr, size))
227
return req.Fail("Invalid size");
228
229
NotifyMemInfoPC(flags, addr, size, pc, tag.c_str(), tag.size());
230
req.Respond();
231
}
232
233
// List memory info tags for address range (memory.info.list)
234
//
235
// Parameters:
236
// - address: number representing start address of the range.
237
// - size: number, bytes from start address.
238
// - type: optional string to limit information to one of:
239
// - "write" for last modification information.
240
// - "texture" for last texture usage information.
241
// - "alloc" for allocation information.
242
// - "suballoc" for allocations within an existing allocation.
243
//
244
// Response (same event name):
245
// - extents: array of objects:
246
// - type: one of the above type string values.
247
// - address: number (may be outside requested range if overlapping.)
248
// - size: number (may be outside requested range if overlapping.)
249
// - ticks: number indicating tick counter as of last tag.
250
// - pc: number address of last tag.
251
// - tag: string tag for this memory extent.
252
// - allocated: boolean, if this extent is marked as allocated (for alloc/suballoc types.)
253
void WebSocketMemoryInfoState::List(DebuggerRequest &req) {
254
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
255
return req.Fail("CPU not started");
256
257
std::string type;
258
if (!req.ParamString("type", &type, DebuggerParamType::OPTIONAL))
259
return;
260
uint32_t addr;
261
if (!req.ParamU32("address", &addr))
262
return;
263
uint32_t size;
264
if (!req.ParamU32("size", &size))
265
return;
266
267
// Allow type to be omitted.
268
MemBlockFlags flags = MemBlockFlags::SKIP_MEMCHECK | FlagFromType(type);
269
if (flags == MemBlockFlags::SKIP_MEMCHECK && req.HasParam("type"))
270
return req.Fail("Invaid type - expecting write, texture, alloc, suballoc, free, or subfree");
271
272
if (!Memory::IsValidAddress(addr))
273
return req.Fail("Invalid address");
274
else if (!Memory::IsValidRange(addr, size))
275
return req.Fail("Invalid size");
276
277
std::vector<MemBlockInfo> results;
278
if (flags == MemBlockFlags::SKIP_MEMCHECK)
279
results = FindMemInfo(addr, size);
280
else
281
results = FindMemInfoByFlag(flags, addr, size);
282
283
JsonWriter &json = req.Respond();
284
json.pushArray("extents");
285
for (const auto &result : results) {
286
json.pushDict();
287
json.writeString("type", TypeFromFlag(result.flags));
288
json.writeUint("address", result.start);
289
json.writeUint("size", result.size);
290
json.writeFloat("ticks", result.ticks);
291
json.writeUint("pc", result.pc);
292
json.writeString("tag", result.tag);
293
json.writeBool("allocated", result.allocated);
294
json.pop();
295
}
296
json.pop();
297
}
298
299
// Search memory info tags for a string (memory.info.search)
300
//
301
// Parameters:
302
// - address: optional number representing start address of the range.
303
// - end: optional end address as a number (otherwise uses start address.)
304
// - match: string to search for within tag.
305
// - type: optional string to limit information to one of:
306
// - "write" for last modification information.
307
// - "texture" for last texture usage information.
308
// - "alloc" for allocation information.
309
// - "suballoc" for allocations within an existing allocation.
310
//
311
// Response (same event name):
312
// - extent: null, or matching object containing:
313
// - type: one of the above type string values.
314
// - address: number (may be outside requested range if overlapping.)
315
// - size: number (may be outside requested range if overlapping.)
316
// - ticks: number indicating tick counter as of last tag.
317
// - pc: number address of last tag.
318
// - tag: string tag for this memory extent.
319
// - allocated: boolean, if this extent is marked as allocated (for alloc/suballoc types.)
320
//
321
// Note: may not be fast.
322
void WebSocketMemoryInfoState::Search(DebuggerRequest &req) {
323
if (!currentDebugMIPS->isAlive() || !Memory::IsActive())
324
return req.Fail("CPU not started");
325
326
uint32_t start = 0;
327
if (!req.ParamU32("address", &start, false, DebuggerParamType::OPTIONAL))
328
return;
329
uint32_t end = start;
330
if (!req.ParamU32("end", &end, false, DebuggerParamType::OPTIONAL))
331
return;
332
std::string type;
333
if (!req.ParamString("type", &type, DebuggerParamType::OPTIONAL))
334
return;
335
std::string match;
336
if (!req.ParamString("match", &match))
337
return;
338
339
// Allow type to be omitted.
340
MemBlockFlags flags = MemBlockFlags::SKIP_MEMCHECK | FlagFromType(type);
341
if (flags == MemBlockFlags::SKIP_MEMCHECK && req.HasParam("type"))
342
return req.Fail("Invaid type - expecting write, texture, alloc, suballoc, free, or subfree");
343
344
start = RoundMemAddressUp(start);
345
end = RoundMemAddressUp(end);
346
std::transform(match.begin(), match.end(), match.begin(), ::tolower);
347
348
bool found = false;
349
MemBlockInfo foundResult;
350
351
uint32_t addr = start;
352
constexpr uint32_t CHUNK_SIZE = 0x1000;
353
do {
354
uint32_t chunk_end = addr + CHUNK_SIZE;
355
if (addr < end && chunk_end >= end) {
356
chunk_end = end;
357
}
358
359
std::vector<MemBlockInfo> results;
360
if (flags == MemBlockFlags::SKIP_MEMCHECK)
361
results = FindMemInfo(addr, chunk_end - addr);
362
else
363
results = FindMemInfoByFlag(flags, addr, chunk_end - addr);
364
365
for (const auto &result : results) {
366
std::string lowercase = result.tag;
367
std::transform(lowercase.begin(), lowercase.end(), lowercase.begin(), ::tolower);
368
369
if (lowercase.find(match) != lowercase.npos) {
370
found = true;
371
foundResult = result;
372
break;
373
}
374
}
375
addr = RoundMemAddressUp(chunk_end);
376
} while (!found && addr != end);
377
378
JsonWriter &json = req.Respond();
379
if (found) {
380
json.pushDict("extent");
381
json.writeString("type", TypeFromFlag(foundResult.flags));
382
json.writeUint("address", foundResult.start);
383
json.writeUint("size", foundResult.size);
384
json.writeFloat("ticks", foundResult.ticks);
385
json.writeUint("pc", foundResult.pc);
386
json.writeString("tag", foundResult.tag);
387
json.writeBool("allocated", foundResult.allocated);
388
json.pop();
389
} else {
390
json.writeNull("extent");
391
}
392
}
393
394