diff --git a/Core/Debugger/MemBlockInfo.cpp b/Core/Debugger/MemBlockInfo.cpp index 9377599918..5750c6735f 100644 --- a/Core/Debugger/MemBlockInfo.cpp +++ b/Core/Debugger/MemBlockInfo.cpp @@ -395,10 +395,12 @@ void NotifyMemInfoPC(MemBlockFlags flags, uint32_t start, uint32_t size, uint32_ FlushPendingMemInfo(); } - if (flags & MemBlockFlags::WRITE) { - CBreakPoints::ExecMemCheck(start, true, size, pc, tagStr); - } else if (flags & MemBlockFlags::READ) { - CBreakPoints::ExecMemCheck(start, false, size, pc, tagStr); + if (!(flags & MemBlockFlags::SKIP_MEMCHECK)) { + if (flags & MemBlockFlags::WRITE) { + CBreakPoints::ExecMemCheck(start, true, size, pc, tagStr); + } else if (flags & MemBlockFlags::READ) { + CBreakPoints::ExecMemCheck(start, false, size, pc, tagStr); + } } } diff --git a/Core/Debugger/MemBlockInfo.h b/Core/Debugger/MemBlockInfo.h index aec9d03725..786e713ba0 100644 --- a/Core/Debugger/MemBlockInfo.h +++ b/Core/Debugger/MemBlockInfo.h @@ -34,6 +34,8 @@ enum class MemBlockFlags { READ = 0x0800, FREE = 0x1000, SUB_FREE = 0x2000, + + SKIP_MEMCHECK = 0x00010000, }; ENUM_CLASS_BITOPS(MemBlockFlags); @@ -47,7 +49,8 @@ struct MemBlockInfo { bool allocated; }; -void NotifyMemInfo(MemBlockFlags flags, uint32_t start, uint32_t size, const char *str, size_t strLength); +void NotifyMemInfo(MemBlockFlags flags, uint32_t start, uint32_t size, const char *tag, size_t tagLength); +void NotifyMemInfoPC(MemBlockFlags flags, uint32_t start, uint32_t size, uint32_t pc, const char *tag, size_t tagLength); // This lets us avoid calling strlen on string constants, instead the string length (including null, // so we have to subtract 1) is computed at compile time. diff --git a/Core/Debugger/WebSocket/DisasmSubscriber.cpp b/Core/Debugger/WebSocket/DisasmSubscriber.cpp index 5f4848ea8a..fdebe9a892 100644 --- a/Core/Debugger/WebSocket/DisasmSubscriber.cpp +++ b/Core/Debugger/WebSocket/DisasmSubscriber.cpp @@ -47,7 +47,6 @@ public: protected: void WriteDisasmLine(JsonWriter &json, const DisassemblyLineInfo &l); void WriteBranchGuide(JsonWriter &json, const BranchLine &l); - uint32_t RoundAddressUp(uint32_t addr); DisassemblyManager disasm_; }; @@ -248,18 +247,6 @@ void WebSocketDisasmState::WriteBranchGuide(JsonWriter &json, const BranchLine & json.pop(); } -uint32_t WebSocketDisasmState::RoundAddressUp(uint32_t addr) { - if (addr < PSP_GetScratchpadMemoryBase()) - return PSP_GetScratchpadMemoryBase(); - else if (addr >= PSP_GetScratchpadMemoryEnd() && addr < PSP_GetVidMemBase()) - return PSP_GetVidMemBase(); - else if (addr >= PSP_GetVidMemEnd() && addr < PSP_GetKernelMemoryBase()) - return PSP_GetKernelMemoryBase(); - else if (addr >= PSP_GetUserMemoryEnd()) - return PSP_GetScratchpadMemoryBase(); - return addr; -} - // Request the current PSP memory base address (memory.base) // // WARNING: Avoid this unless you have a good reason. Uses PPSSPP's address space. @@ -383,7 +370,7 @@ void WebSocketDisasmState::Disasm(DebuggerRequest &req) { // Parameters: // - thread: optional number indicating the thread id (may not affect search much.) // - address: starting address as a number. -// - end: optional end address as a number (otherwise uses start.) +// - end: optional end address as a number (otherwise uses start address.) // - match: string to search for. // - displaySymbols: optional, specify false to hide symbols in the searched parameters. // @@ -410,7 +397,7 @@ void WebSocketDisasmState::SearchDisasm(DebuggerRequest &req) { return; bool loopSearch = end <= start; - start = RoundAddressUp(start); + start = RoundMemAddressUp(start); if ((end <= start) != loopSearch) { // We must've passed end by rounding up. JsonWriter &json = req.Respond(); @@ -419,7 +406,7 @@ void WebSocketDisasmState::SearchDisasm(DebuggerRequest &req) { } // We do this after the check in case both were in unused memory. - end = RoundAddressUp(end); + end = RoundMemAddressUp(end); std::transform(match.begin(), match.end(), match.begin(), ::tolower); @@ -450,7 +437,7 @@ void WebSocketDisasmState::SearchDisasm(DebuggerRequest &req) { break; } - addr = RoundAddressUp(addr + line.totalSize); + addr = RoundMemAddressUp(addr + line.totalSize); } while (addr != end); JsonWriter &json = req.Respond(); diff --git a/Core/Debugger/WebSocket/MemoryInfoSubscriber.cpp b/Core/Debugger/WebSocket/MemoryInfoSubscriber.cpp index ee2f34cd75..30eb8cdd02 100644 --- a/Core/Debugger/WebSocket/MemoryInfoSubscriber.cpp +++ b/Core/Debugger/WebSocket/MemoryInfoSubscriber.cpp @@ -15,6 +15,9 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. +#include +#include "Core/MIPS/MIPS.h" +#include "Core/MIPS/MIPSDebugInterface.h" #include "Core/Debugger/MemBlockInfo.h" #include "Core/Debugger/WebSocket/MemoryInfoSubscriber.h" #include "Core/Debugger/WebSocket/WebSocketUtils.h" @@ -30,6 +33,9 @@ public: void Mapping(DebuggerRequest &req); void Config(DebuggerRequest &req); + void Set(DebuggerRequest &req); + void List(DebuggerRequest &req); + void Search(DebuggerRequest &req); protected: void UpdateOverride(bool flag); @@ -41,6 +47,9 @@ DebuggerSubscriber *WebSocketMemoryInfoInit(DebuggerEventHandlerMap &map) { auto p = new WebSocketMemoryInfoState(); map["memory.mapping"] = std::bind(&WebSocketMemoryInfoState::Mapping, p, std::placeholders::_1); map["memory.info.config"] = std::bind(&WebSocketMemoryInfoState::Config, p, std::placeholders::_1); + map["memory.info.set"] = std::bind(&WebSocketMemoryInfoState::Set, p, std::placeholders::_1); + map["memory.info.list"] = std::bind(&WebSocketMemoryInfoState::List, p, std::placeholders::_1); + map["memory.info.search"] = std::bind(&WebSocketMemoryInfoState::Search, p, std::placeholders::_1); return p; } @@ -140,3 +149,246 @@ void WebSocketMemoryInfoState::Config(DebuggerRequest &req) { if (setDetailed) UpdateOverride(detailed); } + +static MemBlockFlags FlagFromType(const std::string &type) { + if (type == "write") + return MemBlockFlags::WRITE; + if (type == "texture") + return MemBlockFlags::TEXTURE; + if (type == "alloc") + return MemBlockFlags::ALLOC; + if (type == "suballoc") + return MemBlockFlags::SUB_ALLOC; + if (type == "free") + return MemBlockFlags::FREE; + if (type == "subfree") + return MemBlockFlags::SUB_FREE; + return MemBlockFlags::SKIP_MEMCHECK; +} + +static std::string TypeFromFlag(const MemBlockFlags &flag) { + if (flag & MemBlockFlags::WRITE) + return "write"; + else if (flag & MemBlockFlags::TEXTURE) + return "texture"; + else if (flag & MemBlockFlags::ALLOC) + return "alloc"; + else if (flag & MemBlockFlags::SUB_ALLOC) + return "suballoc"; + return "error"; +} + +// Update memory info tagging (memory.info.set) +// +// Parameters: +// - address: number representing start address of the range to modify. +// - size: number, bytes from start address. +// - type: string, one of: +// - "write" for last modification information. +// - "texture" for last texture usage information. +// - "alloc" for allocation information. +// - "suballoc" for allocations within an existing allocation. +// - "free" to mark a previous allocation and its suballocations freed (ignores tag.) +// - "subfree" to mark a previous suballocation freed (ignores tag.) +// - tag: string label to give the memory. Optional if type if free or subfree. +// - pc: optional, number indicating PC address for this tag. +// +// Response (same event name) with no extra data. +// +// Note: Only one tag per type is maintained for any given memory address. +// Small extent info may be ignored unless detailed tracking enabled (see memory.info.config.) +void WebSocketMemoryInfoState::Set(DebuggerRequest &req) { + if (!currentDebugMIPS->isAlive() || !Memory::IsActive()) + return req.Fail("CPU not started"); + + std::string type; + if (!req.ParamString("type", &type)) + return; + std::string tag; + if (type != "free" && type != "subfree") { + if (!req.ParamString("tag", &tag)) + return; + } + uint32_t addr; + if (!req.ParamU32("address", &addr)) + return; + uint32_t size; + if (!req.ParamU32("size", &size)) + return; + uint32_t pc = currentMIPS->pc; + if (!req.ParamU32("pc", &pc, false, DebuggerParamType::OPTIONAL)) + return; + + MemBlockFlags flags = MemBlockFlags::SKIP_MEMCHECK | FlagFromType(type); + if (flags == MemBlockFlags::SKIP_MEMCHECK) + return req.Fail("Invaid type - expecting write, texture, alloc, suballoc, free, or subfree"); + + if (!Memory::IsValidAddress(addr)) + return req.Fail("Invalid address"); + else if (!Memory::IsValidRange(addr, size)) + return req.Fail("Invalid size"); + + NotifyMemInfoPC(flags, addr, size, pc, tag.c_str(), tag.size()); + req.Respond(); +} + +// List memory info tags for address range (memory.info.list) +// +// Parameters: +// - address: number representing start address of the range. +// - size: number, bytes from start address. +// - type: optional string to limit information to one of: +// - "write" for last modification information. +// - "texture" for last texture usage information. +// - "alloc" for allocation information. +// - "suballoc" for allocations within an existing allocation. +// +// Response (same event name): +// - extents: array of objects: +// - type: one of the above type string values. +// - address: number (may be outside requested range if overlapping.) +// - size: number (may be outside requested range if overlapping.) +// - ticks: number indicating tick counter as of last tag. +// - pc: number address of last tag. +// - tag: string tag for this memory extent. +// - allocated: boolean, if this extent is marked as allocated (for alloc/suballoc types.) +void WebSocketMemoryInfoState::List(DebuggerRequest &req) { + if (!currentDebugMIPS->isAlive() || !Memory::IsActive()) + return req.Fail("CPU not started"); + + std::string type; + if (!req.ParamString("type", &type, DebuggerParamType::OPTIONAL)) + return; + uint32_t addr; + if (!req.ParamU32("address", &addr)) + return; + uint32_t size; + if (!req.ParamU32("size", &size)) + return; + + // Allow type to be omitted. + MemBlockFlags flags = MemBlockFlags::SKIP_MEMCHECK | FlagFromType(type); + if (flags == MemBlockFlags::SKIP_MEMCHECK && req.HasParam("type")) + return req.Fail("Invaid type - expecting write, texture, alloc, suballoc, free, or subfree"); + + if (!Memory::IsValidAddress(addr)) + return req.Fail("Invalid address"); + else if (!Memory::IsValidRange(addr, size)) + return req.Fail("Invalid size"); + + std::vector results; + if (flags == MemBlockFlags::SKIP_MEMCHECK) + results = FindMemInfo(addr, size); + else + results = FindMemInfoByFlag(flags, addr, size); + + JsonWriter &json = req.Respond(); + json.pushArray("extents"); + for (const auto &result : results) { + json.pushDict(); + json.writeString("type", TypeFromFlag(result.flags)); + json.writeUint("address", result.start); + json.writeUint("size", result.size); + json.writeFloat("ticks", result.ticks); + json.writeUint("pc", result.pc); + json.writeString("tag", result.tag); + json.writeBool("allocated", result.allocated); + json.pop(); + } + json.pop(); +} + +// Search memory info tags for a string (memory.info.search) +// +// Parameters: +// - address: optional number representing start address of the range. +// - end: optional end address as a number (otherwise uses start address.) +// - match: string to search for within tag. +// - type: optional string to limit information to one of: +// - "write" for last modification information. +// - "texture" for last texture usage information. +// - "alloc" for allocation information. +// - "suballoc" for allocations within an existing allocation. +// +// Response (same event name): +// - extent: null, or matching object containing: +// - type: one of the above type string values. +// - address: number (may be outside requested range if overlapping.) +// - size: number (may be outside requested range if overlapping.) +// - ticks: number indicating tick counter as of last tag. +// - pc: number address of last tag. +// - tag: string tag for this memory extent. +// - allocated: boolean, if this extent is marked as allocated (for alloc/suballoc types.) +// +// Note: may not be fast. +void WebSocketMemoryInfoState::Search(DebuggerRequest &req) { + if (!currentDebugMIPS->isAlive() || !Memory::IsActive()) + return req.Fail("CPU not started"); + + uint32_t start = 0; + if (!req.ParamU32("address", &start, false, DebuggerParamType::OPTIONAL)) + return; + uint32_t end = start; + if (!req.ParamU32("end", &end, false, DebuggerParamType::OPTIONAL)) + return; + std::string type; + if (!req.ParamString("type", &type, DebuggerParamType::OPTIONAL)) + return; + std::string match; + if (!req.ParamString("match", &match)) + return; + + // Allow type to be omitted. + MemBlockFlags flags = MemBlockFlags::SKIP_MEMCHECK | FlagFromType(type); + if (flags == MemBlockFlags::SKIP_MEMCHECK && req.HasParam("type")) + return req.Fail("Invaid type - expecting write, texture, alloc, suballoc, free, or subfree"); + + start = RoundMemAddressUp(start); + end = RoundMemAddressUp(end); + std::transform(match.begin(), match.end(), match.begin(), ::tolower); + + bool found = false; + MemBlockInfo foundResult; + + uint32_t addr = start; + constexpr uint32_t CHUNK_SIZE = 0x1000; + do { + uint32_t chunk_end = addr + CHUNK_SIZE; + if (addr < end && chunk_end >= end) { + chunk_end = end; + } + + std::vector results; + if (flags == MemBlockFlags::SKIP_MEMCHECK) + results = FindMemInfo(addr, chunk_end - addr); + else + results = FindMemInfoByFlag(flags, addr, chunk_end - addr); + + for (const auto &result : results) { + std::string lowercase = result.tag; + std::transform(lowercase.begin(), lowercase.end(), lowercase.begin(), ::tolower); + + if (lowercase.find(match) != lowercase.npos) { + found = true; + foundResult = result; + break; + } + } + addr = RoundMemAddressUp(chunk_end); + } while (!found && addr != end); + + JsonWriter &json = req.Respond(); + if (found) { + json.pushDict("extent"); + json.writeString("type", TypeFromFlag(foundResult.flags)); + json.writeUint("address", foundResult.start); + json.writeUint("size", foundResult.size); + json.writeFloat("ticks", foundResult.ticks); + json.writeUint("pc", foundResult.pc); + json.writeString("tag", foundResult.tag); + json.writeBool("allocated", foundResult.allocated); + json.pop(); + } else { + json.writeNull("extent"); + } +} diff --git a/Core/Debugger/WebSocket/WebSocketUtils.cpp b/Core/Debugger/WebSocket/WebSocketUtils.cpp index a9120b5caa..cc49e33336 100644 --- a/Core/Debugger/WebSocket/WebSocketUtils.cpp +++ b/Core/Debugger/WebSocket/WebSocketUtils.cpp @@ -21,6 +21,7 @@ #include "Common/Data/Text/Parsers.h" #include "Common/StringUtils.h" #include "Core/Debugger/WebSocket/WebSocketUtils.h" +#include "Core/MemMap.h" inline void DebuggerJsonAddTicket(JsonWriter &writer, const JsonGet &data) { const JsonNode *value = data.get("ticket"); @@ -273,3 +274,15 @@ bool DebuggerRequest::ParamString(const char *name, std::string *out, DebuggerPa Fail(StringFromFormat("Invalid '%s' parameter type", name)); return false; } + +uint32_t RoundMemAddressUp(uint32_t addr) { + if (addr < PSP_GetScratchpadMemoryBase()) + return PSP_GetScratchpadMemoryBase(); + else if (addr >= PSP_GetScratchpadMemoryEnd() && addr < PSP_GetVidMemBase()) + return PSP_GetVidMemBase(); + else if (addr >= PSP_GetVidMemEnd() && addr < PSP_GetKernelMemoryBase()) + return PSP_GetKernelMemoryBase(); + else if (addr >= PSP_GetUserMemoryEnd()) + return PSP_GetScratchpadMemoryBase(); + return addr; +} diff --git a/Core/Debugger/WebSocket/WebSocketUtils.h b/Core/Debugger/WebSocket/WebSocketUtils.h index 5caa28adaf..38edc24a68 100644 --- a/Core/Debugger/WebSocket/WebSocketUtils.h +++ b/Core/Debugger/WebSocket/WebSocketUtils.h @@ -109,3 +109,5 @@ public: typedef std::function DebuggerEventHandler; typedef std::unordered_map DebuggerEventHandlerMap; + +uint32_t RoundMemAddressUp(uint32_t addr);