Skip to content

Commit ee3dffe

Browse files
committed
debugger: fix memory issues caused by overlayed debug symbols
1 parent 4013302 commit ee3dffe

4 files changed

Lines changed: 46 additions & 11 deletions

File tree

src/Cafe/GraphicPack/GraphicPack2PatchesApply.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ void PatchEntryInstruction::undoPatch()
420420
uint8* patchAddr = (uint8*)memory_base + addr;
421421
memcpy(patchAddr, m_dataBackup, m_length);
422422
PPCRecompiler_invalidateRange(addr, addr + m_length);
423-
rplSymbolStorage_removeRange(addr, m_length);
423+
rplSymbolStorage_removeRange(addr, m_length, RPL_STORED_SYMBOL_PATCH);
424424
DebugSymbolStorage::ClearRange(addr, m_length);
425425
}
426426

@@ -434,7 +434,7 @@ bool registerU32Variable(PatchContext_t& ctx, std::string& name, uint32 value, P
434434
}
435435
ctx.map_values[name] = value;
436436
// keep track of address symbols for the debugger
437-
rplSymbolStorage_store(ctx.graphicPack->GetName().data(), name.data(), value);
437+
rplSymbolStorage_store(ctx.graphicPack->GetName().data(), name.data(), value, RPL_STORED_SYMBOL_PATCH);
438438

439439
return true;
440440
}

src/Cafe/OS/RPL/rpl_symbol_storage.cpp

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ char* rplSymbolStorage_storeLibname(const char* libName)
6363
return libEntry->libName;
6464
}
6565

66-
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address)
66+
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address, uint32 type)
6767
{
6868
std::unique_lock<std::mutex> lck(rplSymbolStorage.m_symbolStorageMutex);
6969
char* libNameStorage = rplSymbolStorage_storeLibname(libName);
@@ -72,7 +72,7 @@ RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolN
7272
storedSymbol->address = address;
7373
storedSymbol->libName = libNameStorage;
7474
storedSymbol->symbolName = symbolNameStorage;
75-
storedSymbol->flags = 0;
75+
storedSymbol->flags = type;
7676
storedSymbol->previous = nullptr;
7777
auto it = rplSymbolStorage.map_symbolByAddress.find(address);
7878
if (it != rplSymbolStorage.map_symbolByAddress.end())
@@ -130,13 +130,41 @@ void rplSymbolStorage_remove(RPLStoredSymbol* storedSymbol)
130130
delete storedSymbol;
131131
}
132132

133-
void rplSymbolStorage_removeRange(MPTR address, sint32 length)
133+
134+
void rplSymbolStorage_removeRange(MPTR address, sint32 length, uint32 type)
134135
{
136+
std::unique_lock<std::mutex> lck(rplSymbolStorage.m_symbolStorageMutex);
135137
while (length > 0)
136138
{
137-
RPLStoredSymbol* symbol = rplSymbolStorage_getByAddress(address);
138-
if (symbol)
139-
rplSymbolStorage_remove(symbol);
139+
auto it = rplSymbolStorage.map_symbolByAddress.find(address);
140+
if (it != rplSymbolStorage.map_symbolByAddress.end())
141+
{
142+
RPLStoredSymbol* current = it->second;
143+
RPLStoredSymbol* newHead = current;
144+
RPLStoredSymbol* previousKept = nullptr;
145+
while (current)
146+
{
147+
RPLStoredSymbol* next = current->previous;
148+
if ((current->flags & type) == type)
149+
{
150+
if (previousKept)
151+
previousKept->previous = next;
152+
else
153+
newHead = next;
154+
delete current;
155+
}
156+
else
157+
{
158+
previousKept = current;
159+
}
160+
current = next;
161+
}
162+
163+
if (newHead)
164+
it->second = newHead;
165+
else
166+
rplSymbolStorage.map_symbolByAddress.erase(it);
167+
}
140168
address += 4;
141169
length -= 4;
142170
}

src/Cafe/OS/RPL/rpl_symbol_storage.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
enum RPLStoredSymbolType : uint32
2+
{
3+
RPL_STORED_SYMBOL_NONE = 0,
4+
RPL_STORED_SYMBOL_MAP = 1 << 0,
5+
RPL_STORED_SYMBOL_PATCH = 1 << 1,
6+
};
7+
18
struct RPLStoredSymbol
29
{
310
MPTR address;
@@ -9,9 +16,9 @@ struct RPLStoredSymbol
916

1017
void rplSymbolStorage_init();
1118
void rplSymbolStorage_unloadAll();
12-
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address);
19+
RPLStoredSymbol* rplSymbolStorage_store(const char* libName, const char* symbolName, MPTR address, uint32 type = RPL_STORED_SYMBOL_NONE);
1320
void rplSymbolStorage_remove(RPLStoredSymbol* storedSymbol);
14-
void rplSymbolStorage_removeRange(MPTR address, sint32 length);
21+
void rplSymbolStorage_removeRange(MPTR address, sint32 length, uint32 type = RPL_STORED_SYMBOL_NONE);
1522
RPLStoredSymbol* rplSymbolStorage_getByAddress(MPTR address);
1623
RPLStoredSymbol* rplSymbolStorage_getByClosestAddress(MPTR address);
1724
void rplSymbolStorage_createJumpProxySymbol(MPTR jumpAddress, MPTR destAddress);

src/gui/wxgui/debugger/DebuggerWindow2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ void DebuggerWindow2::LoadModuleMap(DebuggerModuleStorage& moduleStorage)
401401
const auto relocatedAddress = GetRelocatedMapAddress(moduleStorage.moduleInfo, mapAddress);
402402
if (relocatedAddress)
403403
{
404-
moduleStorage.loaded_map_symbols.emplace_back(rplSymbolStorage_store(moduleStorage.moduleInfo.moduleName.c_str(), symbolName.c_str(), *relocatedAddress));
404+
moduleStorage.loaded_map_symbols.emplace_back(rplSymbolStorage_store(moduleStorage.moduleInfo.moduleName.c_str(), symbolName.c_str(), *relocatedAddress, RPL_STORED_SYMBOL_MAP));
405405
}
406406
}
407407
}

0 commit comments

Comments
 (0)