From a2fa53d5c45d75f589c54ca7b2cb931b0d8ae73c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Jan 2014 20:59:14 -0800 Subject: [PATCH 1/5] Track modules in the symbol map. This makes it so things don't overlap confusingly, and also so that we can replace funcs more correctly and sanely. This is unfortunately a bunch more complicated... --- Core/Debugger/SymbolMap.cpp | 491 ++++++++++++++++++++++++++++------- Core/Debugger/SymbolMap.h | 49 +++- Core/HLE/sceKernelModule.cpp | 19 +- 3 files changed, 459 insertions(+), 100 deletions(-) diff --git a/Core/Debugger/SymbolMap.cpp b/Core/Debugger/SymbolMap.cpp index da0c84cf54..29349e325d 100644 --- a/Core/Debugger/SymbolMap.cpp +++ b/Core/Debugger/SymbolMap.cpp @@ -42,6 +42,11 @@ void SymbolMap::Clear() { functions.clear(); labels.clear(); data.clear(); + activeFunctions.clear(); + activeLabels.clear(); + activeData.clear(); + activeModuleEnds.clear(); + modules.clear(); } bool SymbolMap::LoadSymbolMap(const char *filename) { @@ -57,7 +62,8 @@ bool SymbolMap::LoadSymbolMap(const char *filename) { //fgets(temp,255,f); // address Size address //fgets(temp,255,f); // ----------------------- - bool started=false; + bool started = false; + bool hasModules = false; while (!feof(f)) { char line[512], temp[256] = {0}; @@ -94,15 +100,39 @@ bool SymbolMap::LoadSymbolMap(const char *filename) { if (!started) continue; - u32 address, size, vaddress; + u32 address = -1, size, vaddress = -1; + int moduleIndex = 0; SymbolType type; char name[128] = {0}; - sscanf(line,"%08x %08x %08x %i %127c", &address, &size, &vaddress, (int*)&type, name); - if (!Memory::IsValidAddress(vaddress)) { - ERROR_LOG(LOADER, "Invalid address in symbol file: %08x (%s)", vaddress, name); + if (sscanf(line, ".module %x %08x %08x %127c", &moduleIndex, &address, &size, name) == 4) { + // Found a module definition. + ModuleEntry mod; + mod.index = moduleIndex; + strcpy(mod.name, name); + mod.start = address; + mod.size = size; + modules.push_back(mod); + hasModules = true; continue; } + + sscanf(line, "%08x %08x %x %i %127c", &address, &size, &vaddress, (int*)&type, name); + if (!hasModules) { + if (!Memory::IsValidAddress(vaddress)) { + ERROR_LOG(LOADER, "Invalid address in symbol file: %08x (%s)", vaddress, name); + continue; + } + } else { + // The 3rd field is now used for the module index. + moduleIndex = vaddress; + vaddress = GetModuleAbsoluteAddr(address, moduleIndex); + if (!Memory::IsValidAddress(vaddress)) { + ERROR_LOG(LOADER, "Invalid address in symbol file: %08x (%s)", vaddress, name); + continue; + } + } + if (type == ST_DATA && size == 0) size = 4; @@ -112,12 +142,12 @@ bool SymbolMap::LoadSymbolMap(const char *filename) { switch (type) { case ST_FUNCTION: - AddFunction(name, vaddress, size); + AddFunction(name, vaddress, size, moduleIndex); break; case ST_DATA: - AddData(vaddress,size,DATATYPE_BYTE); + AddData(vaddress,size,DATATYPE_BYTE, moduleIndex); if (name[0] != 0) - AddLabel(name, vaddress); + AddLabel(name, vaddress, moduleIndex); break; case ST_NONE: case ST_ALL: @@ -145,14 +175,19 @@ void SymbolMap::SaveSymbolMap(const char *filename) const { fprintf(f,".text\n"); + for (auto it = modules.begin(), end = modules.end(); it != end; ++it) { + const ModuleEntry &mod = *it; + fprintf(f, ".module %x %08x %08x %s\n", mod.index, mod.start, mod.size, mod.name); + } + for (auto it = functions.begin(), end = functions.end(); it != end; ++it) { const FunctionEntry& e = it->second; - fprintf(f,"%08x %08x %08x %i %s\n",it->first,e.size,it->first,ST_FUNCTION,GetLabelName(it->first)); + fprintf(f, "%08x %08x %x %i %s\n", e.start, e.size, e.module, ST_FUNCTION, GetLabelNameRel(e.start, e.module)); } for (auto it = data.begin(), end = data.end(); it != end; ++it) { const DataEntry& e = it->second; - fprintf(f,"%08x %08x %08x %i %s\n",it->first,e.size,it->first,ST_DATA,GetLabelName(it->first)); + fprintf(f, "%08x %08x %x %i %s\n", e.start, e.size, e.module, ST_DATA, GetLabelNameRel(e.start, e.module)); } fclose(f); } @@ -186,13 +221,13 @@ bool SymbolMap::LoadNocashSym(const char *filename) { continue; if (strcasecmp(value, ".byt") == 0) { - AddData(address, size, DATATYPE_BYTE); + AddData(address, size, DATATYPE_BYTE, 0); } else if (strcasecmp(value, ".wrd") == 0) { - AddData(address, size, DATATYPE_HALFWORD); + AddData(address, size, DATATYPE_HALFWORD, 0); } else if (strcasecmp(value, ".dbl") == 0) { - AddData(address, size, DATATYPE_WORD); + AddData(address, size, DATATYPE_WORD, 0); } else if (strcasecmp(value, ".asc") == 0) { - AddData(address, size, DATATYPE_ASCII); + AddData(address, size, DATATYPE_ASCII, 0); } } } else { // labels @@ -204,9 +239,9 @@ bool SymbolMap::LoadNocashSym(const char *filename) { } if (size != 1) { - AddFunction(value,address,size); + AddFunction(value, address,size, 0); } else { - AddLabel(value,address); + AddLabel(value, address, 0); } } } @@ -216,9 +251,10 @@ bool SymbolMap::LoadNocashSym(const char *filename) { } SymbolType SymbolMap::GetSymbolType(u32 address) const { - if (functions.find(address) != functions.end()) + lock_guard guard(lock_); + if (activeFunctions.find(address) != activeFunctions.end()) return ST_FUNCTION; - if (data.find(address) != data.end()) + if (activeData.find(address) != activeData.end()) return ST_DATA; return ST_NONE; } @@ -268,14 +304,15 @@ bool SymbolMap::GetSymbolInfo(SymbolInfo *info, u32 address, SymbolType symmask) } u32 SymbolMap::GetNextSymbolAddress(u32 address, SymbolType symmask) { - const auto functionEntry = symmask & ST_FUNCTION ? functions.upper_bound(address) : functions.end(); - const auto dataEntry = symmask & ST_DATA ? data.upper_bound(address) : data.end(); + lock_guard guard(lock_); + const auto functionEntry = symmask & ST_FUNCTION ? activeFunctions.upper_bound(address) : activeFunctions.end(); + const auto dataEntry = symmask & ST_DATA ? activeData.upper_bound(address) : activeData.end(); - if (functionEntry == functions.end() && dataEntry == data.end()) + if (functionEntry == activeFunctions.end() && dataEntry == activeData.end()) return INVALID_ADDRESS; - u32 funcAddress = (functionEntry != functions.end()) ? functionEntry->first : 0xFFFFFFFF; - u32 dataAddress = (dataEntry != data.end()) ? dataEntry->first : 0xFFFFFFFF; + u32 funcAddress = (functionEntry != activeFunctions.end()) ? functionEntry->first : 0xFFFFFFFF; + u32 dataAddress = (dataEntry != activeData.end()) ? dataEntry->first : 0xFFFFFFFF; if (funcAddress <= dataAddress) return funcAddress; @@ -286,6 +323,7 @@ u32 SymbolMap::GetNextSymbolAddress(u32 address, SymbolType symmask) { static char descriptionTemp[256]; const char *SymbolMap::GetDescription(unsigned int address) const { + lock_guard guard(lock_); const char* labelName = NULL; u32 funcStart = GetFunctionStart(address); @@ -308,7 +346,8 @@ std::vector SymbolMap::GetAllSymbols(SymbolType symmask) { std::vector result; if (symmask & ST_FUNCTION) { - for (auto it = functions.begin(); it != functions.end(); it++) { + lock_guard guard(lock_); + for (auto it = activeFunctions.begin(); it != activeFunctions.end(); it++) { SymbolEntry entry; entry.address = it->first; entry.size = GetFunctionSize(entry.address); @@ -320,7 +359,8 @@ std::vector SymbolMap::GetAllSymbols(SymbolType symmask) { } if (symmask & ST_DATA) { - for (auto it = data.begin(); it != data.end(); it++) { + lock_guard guard(lock_); + for (auto it = activeData.begin(); it != activeData.end(); it++) { SymbolEntry entry; entry.address = it->first; entry.size = GetDataSize(entry.address); @@ -334,24 +374,138 @@ std::vector SymbolMap::GetAllSymbols(SymbolType symmask) { return result; } -void SymbolMap::AddFunction(const char* name, u32 address, u32 size) { +void SymbolMap::AddModule(const char *name, u32 address, u32 size) { lock_guard guard(lock_); - FunctionEntry func; - func.size = size; - func.index = (int)functions.size(); - functions[address] = func; + for (auto it = modules.begin(), end = modules.end(); it != end; ++it) { + if (!strcmp(it->name, name)) { + // Just reactivate that one. + it->start = address; + it->size = size; + activeModuleEnds.insert(std::make_pair(it->start + it->size, *it)); + UpdateActiveSymbols(); + return; + } + } - if (GetLabelName(address) == NULL) - AddLabel(name,address); + ModuleEntry mod; + strncpy(mod.name, name, ARRAY_SIZE(mod.name)); + mod.start = address; + mod.size = size; + mod.index = (int)modules.size() + 1; + + modules.push_back(mod); + activeModuleEnds.insert(std::make_pair(mod.start + mod.size, mod)); + UpdateActiveSymbols(); +} + +void SymbolMap::UnloadModule(u32 address, u32 size) { + lock_guard guard(lock_); + activeModuleEnds.erase(address + size); + UpdateActiveSymbols(); +} + +u32 SymbolMap::GetModuleRelativeAddr(u32 address, int moduleIndex) const { + lock_guard guard(lock_); + if (moduleIndex == -1) { + moduleIndex = GetModuleIndex(address); + } + + for (auto it = modules.begin(), end = modules.end(); it != end; ++it) { + if (it->index == moduleIndex) { + return address - it->start; + } + } + return address; +} + +u32 SymbolMap::GetModuleAbsoluteAddr(u32 relative, int moduleIndex) const { + lock_guard guard(lock_); + for (auto it = modules.begin(), end = modules.end(); it != end; ++it) { + if (it->index == moduleIndex) { + return it->start + relative; + } + } + return relative; +} + +int SymbolMap::GetModuleIndex(u32 address) const { + lock_guard guard(lock_); + auto iter = activeModuleEnds.lower_bound(address); + if (iter == activeModuleEnds.end()) + return -1; + return iter->second.index; +} + +bool SymbolMap::IsModuleActive(int moduleIndex) const { + if (moduleIndex == 0) { + return true; + } + + lock_guard guard(lock_); + for (auto it = activeModuleEnds.begin(), end = activeModuleEnds.end(); it != end; ++it) { + if (it->second.index == moduleIndex) { + return true; + } + } + return false; +} + +void SymbolMap::AddFunction(const char* name, u32 address, u32 size, int moduleIndex) { + lock_guard guard(lock_); + + if (moduleIndex == -1) { + moduleIndex = GetModuleIndex(address); + } + + // Is there an existing one? + u32 relAddress = GetModuleRelativeAddr(address, moduleIndex); + auto symbolKey = std::make_pair(moduleIndex, relAddress); + auto existing = functions.find(symbolKey); + if (existing == functions.end()) { + // Fall back: maybe it's got moduleIndex = 0. + existing = functions.find(std::make_pair(0, address)); + } + + if (existing != functions.end()) { + existing->second.size = size; + if (existing->second.module != moduleIndex) { + FunctionEntry func = existing->second; + func.start = relAddress; + func.module = moduleIndex; + functions.erase(existing); + functions[symbolKey] = func; + } + + // Refresh the active item if it exists. + auto active = activeFunctions.find(address); + if (active != activeFunctions.end() && active->second.module == moduleIndex) { + activeFunctions.erase(active); + activeFunctions.insert(std::make_pair(address, existing->second)); + } + } else { + FunctionEntry func; + func.start = relAddress; + func.size = size; + func.index = (int)functions.size(); + func.module = moduleIndex; + functions[symbolKey] = func; + + if (IsModuleActive(moduleIndex)) { + activeFunctions.insert(std::make_pair(address, func)); + } + } + + AddLabel(name, address, moduleIndex); } u32 SymbolMap::GetFunctionStart(u32 address) const { - auto it = functions.upper_bound(address); - if (it == functions.end()) { + lock_guard guard(lock_); + auto it = activeFunctions.upper_bound(address); + if (it == activeFunctions.end()) { // check last element - auto rit = functions.rbegin(); - if (rit != functions.rend()) { + auto rit = activeFunctions.rbegin(); + if (rit != activeFunctions.rend()) { u32 start = rit->first; u32 size = rit->second.size; if (start <= address && start+size > address) @@ -361,7 +515,7 @@ u32 SymbolMap::GetFunctionStart(u32 address) const { return INVALID_ADDRESS; } - if (it != functions.begin()) { + if (it != activeFunctions.begin()) { it--; u32 start = it->first; u32 size = it->second.size; @@ -373,40 +527,93 @@ u32 SymbolMap::GetFunctionStart(u32 address) const { } u32 SymbolMap::GetFunctionSize(u32 startAddress) const { - auto it = functions.find(startAddress); - if (it == functions.end()) + lock_guard guard(lock_); + auto it = activeFunctions.find(startAddress); + if (it == activeFunctions.end()) return INVALID_ADDRESS; return it->second.size; } int SymbolMap::GetFunctionNum(u32 address) const { + lock_guard guard(lock_); u32 start = GetFunctionStart(address); if (start == INVALID_ADDRESS) return INVALID_ADDRESS; - auto it = functions.find(start); - if (it == functions.end()) + auto it = activeFunctions.find(start); + if (it == activeFunctions.end()) return INVALID_ADDRESS; return it->second.index; } void SymbolMap::AssignFunctionIndices() { + lock_guard guard(lock_); int index = 0; - for (auto it = functions.begin(); it != functions.end(); it++) { - it->second.index = index++; + for (auto mod = activeModuleEnds.begin(), modend = activeModuleEnds.end(); mod != modend; ++mod) { + int moduleIndex = mod->second.index; + auto begin = functions.lower_bound(std::make_pair(moduleIndex, 0)); + auto end = functions.upper_bound(std::make_pair(moduleIndex, 0xFFFFFFFF)); + for (auto it = begin; it != end; ++it) { + it->second.index = index++; + } } } +void SymbolMap::UpdateActiveSymbols() { + lock_guard guard(lock_); + std::map activeModuleIndexes; + for (auto it = activeModuleEnds.begin(), end = activeModuleEnds.end(); it != end; ++it) { + activeModuleIndexes[it->second.index] = it->second.start; + } + + activeFunctions.clear(); + activeLabels.clear(); + activeData.clear(); + + for (auto it = functions.begin(), end = functions.end(); it != end; ++it) { + const auto mod = activeModuleIndexes.find(it->second.module); + if (it->second.module == 0) { + activeFunctions.insert(std::make_pair(it->second.start, it->second)); + } else if (mod != activeModuleIndexes.end()) { + activeFunctions.insert(std::make_pair(mod->second + it->second.start, it->second)); + } + } + + for (auto it = labels.begin(), end = labels.end(); it != end; ++it) { + const auto mod = activeModuleIndexes.find(it->second.module); + if (it->second.module == 0) { + activeLabels.insert(std::make_pair(it->second.addr, it->second)); + } else if (mod != activeModuleIndexes.end()) { + activeLabels.insert(std::make_pair(mod->second + it->second.addr, it->second)); + } + } + + for (auto it = data.begin(), end = data.end(); it != end; ++it) { + const auto mod = activeModuleIndexes.find(it->second.module); + if (it->second.module == 0) { + activeData.insert(std::make_pair(it->second.start, it->second)); + } else if (mod != activeModuleIndexes.end()) { + activeData.insert(std::make_pair(mod->second + it->second.start, it->second)); + } + } + + AssignFunctionIndices(); +} + bool SymbolMap::SetFunctionSize(u32 startAddress, u32 newSize) { lock_guard guard(lock_); - auto it = functions.find(startAddress); - if (it == functions.end()) - return false; - - it->second.size = newSize; + auto funcInfo = activeFunctions.find(startAddress); + if (funcInfo != activeFunctions.end()) { + auto symbolKey = std::make_pair(funcInfo->second.module, funcInfo->second.start); + auto func = functions.find(symbolKey); + if (func != functions.end()) { + func->second.size = newSize; + UpdateActiveSymbols(); + } + } // TODO: check for overlaps return true; @@ -415,47 +622,107 @@ bool SymbolMap::SetFunctionSize(u32 startAddress, u32 newSize) { bool SymbolMap::RemoveFunction(u32 startAddress, bool removeName) { lock_guard guard(lock_); - auto it = functions.find(startAddress); - if (it == functions.end()) + auto it = activeFunctions.find(startAddress); + if (it == activeFunctions.end()) return false; - functions.erase(it); + auto symbolKey = std::make_pair(it->second.module, it->second.start); + auto it2 = functions.find(symbolKey); + if (it2 != functions.end()) { + functions.erase(it2); + } + activeFunctions.erase(it); + if (removeName) { - auto labelIt = labels.find(startAddress); - if (labelIt != labels.end()) - labels.erase(labelIt); + auto labelIt = activeLabels.find(startAddress); + if (labelIt != activeLabels.end()) { + symbolKey = std::make_pair(labelIt->second.module, labelIt->second.addr); + auto labelIt2 = labels.find(symbolKey); + if (labelIt2 != labels.end()) { + labels.erase(labelIt2); + } + activeLabels.erase(labelIt); + } } return true; } -void SymbolMap::AddLabel(const char* name, u32 address) { - // keep a label if it already exists - auto it = labels.find(address); - if (it == labels.end()) { +void SymbolMap::AddLabel(const char* name, u32 address, int moduleIndex) { + lock_guard guard(lock_); + + if (moduleIndex == -1) { + moduleIndex = GetModuleIndex(address); + } + + // Is there an existing one? + u32 relAddress = GetModuleRelativeAddr(address, moduleIndex); + auto symbolKey = std::make_pair(moduleIndex, relAddress); + auto existing = labels.find(symbolKey); + if (existing == labels.end()) { + // Fall back: maybe it's got moduleIndex = 0. + existing = labels.find(std::make_pair(0, address)); + } + + if (existing != labels.end()) { + // We leave an existing label alone, rather than overwriting. + // But we'll still upgrade it to the correct module / relative address. + if (existing->second.module != moduleIndex) { + LabelEntry label = existing->second; + label.addr = relAddress; + label.module = moduleIndex; + labels.erase(existing); + labels[symbolKey] = label; + + // Refresh the active item if it exists. + auto active = activeLabels.find(address); + if (active != activeLabels.end() && active->second.module == moduleIndex) { + activeLabels.erase(active); + activeLabels.insert(std::make_pair(address, existing->second)); + } + } + } else { LabelEntry label; + label.addr = relAddress; + label.module = moduleIndex; strncpy(label.name, name, 128); label.name[127] = 0; - labels[address] = label; + + labels[symbolKey] = label; + if (IsModuleActive(moduleIndex)) { + activeLabels.insert(std::make_pair(address, label)); + } } } void SymbolMap::SetLabelName(const char* name, u32 address) { - auto it = labels.find(address); - if (it == labels.end()) { - LabelEntry label; - strcpy(label.name,name); - label.name[127] = 0; - - labels[address] = label; + lock_guard guard(lock_); + auto labelInfo = activeLabels.find(address); + if (labelInfo == activeLabels.end()) { + AddLabel(name, address); } else { - strcpy(it->second.name,name); - it->second.name[127] = 0; + auto symbolKey = std::make_pair(labelInfo->second.module, labelInfo->second.addr); + auto label = labels.find(symbolKey); + if (label != labels.end()) { + strcpy(label->second.name,name); + label->second.name[127] = 0; + UpdateActiveSymbols(); + } } } -const char* SymbolMap::GetLabelName(u32 address) const { - auto it = labels.find(address); +const char *SymbolMap::GetLabelName(u32 address) const { + lock_guard guard(lock_); + auto it = activeLabels.find(address); + if (it == activeLabels.end()) + return NULL; + + return it->second.name; +} + +const char *SymbolMap::GetLabelNameRel(u32 relAddress, int moduleIndex) const { + lock_guard guard(lock_); + auto it = labels.find(std::make_pair(moduleIndex, relAddress)); if (it == labels.end()) return NULL; @@ -463,8 +730,9 @@ const char* SymbolMap::GetLabelName(u32 address) const { } bool SymbolMap::GetLabelValue(const char* name, u32& dest) { - for (auto it = labels.begin(); it != labels.end(); it++) { - if (strcasecmp(name,it->second.name) == 0) { + lock_guard guard(lock_); + for (auto it = activeLabels.begin(); it != activeLabels.end(); it++) { + if (strcasecmp(name, it->second.name) == 0) { dest = it->first; return true; } @@ -473,21 +741,62 @@ bool SymbolMap::GetLabelValue(const char* name, u32& dest) { return false; } -void SymbolMap::AddData(u32 address, u32 size, DataType type) { - DataEntry entry; - entry.size = size; - entry.type = type; - data[address] = entry; +void SymbolMap::AddData(u32 address, u32 size, DataType type, int moduleIndex) { + lock_guard guard(lock_); + + if (moduleIndex == -1) { + moduleIndex = GetModuleIndex(address); + } + + // Is there an existing one? + u32 relAddress = GetModuleRelativeAddr(address, moduleIndex); + auto symbolKey = std::make_pair(moduleIndex, relAddress); + auto existing = data.find(symbolKey); + if (existing == data.end()) { + // Fall back: maybe it's got moduleIndex = 0. + existing = data.find(std::make_pair(0, address)); + } + + if (existing != data.end()) { + existing->second.size = size; + existing->second.type = type; + if (existing->second.module != moduleIndex) { + DataEntry entry = existing->second; + entry.module = moduleIndex; + entry.start = relAddress; + data.erase(existing); + data[symbolKey] = entry; + } + + // Refresh the active item if it exists. + auto active = activeData.find(address); + if (active != activeData.end() && active->second.module == moduleIndex) { + activeData.erase(active); + activeData.insert(std::make_pair(address, existing->second)); + } + } else { + DataEntry entry; + entry.start = relAddress; + entry.size = size; + entry.type = type; + entry.module = moduleIndex; + + data[symbolKey] = entry; + if (IsModuleActive(moduleIndex)) { + activeData.insert(std::make_pair(address, entry)); + } + } } u32 SymbolMap::GetDataStart(u32 address) const { - auto it = data.upper_bound(address); - if (it == data.end()) + lock_guard guard(lock_); + auto it = activeData.upper_bound(address); + if (it == activeData.end()) { // check last element - auto rit = data.rbegin(); + auto rit = activeData.rbegin(); - if (rit != data.rend()) + if (rit != activeData.rend()) { u32 start = rit->first; u32 size = rit->second.size; @@ -498,7 +807,7 @@ u32 SymbolMap::GetDataStart(u32 address) const { return INVALID_ADDRESS; } - if (it != data.begin()) { + if (it != activeData.begin()) { it--; u32 start = it->first; u32 size = it->second.size; @@ -510,15 +819,17 @@ u32 SymbolMap::GetDataStart(u32 address) const { } u32 SymbolMap::GetDataSize(u32 startAddress) const { - auto it = data.find(startAddress); - if (it == data.end()) + lock_guard guard(lock_); + auto it = activeData.find(startAddress); + if (it == activeData.end()) return INVALID_ADDRESS; return it->second.size; } DataType SymbolMap::GetDataType(u32 startAddress) const { - auto it = data.find(startAddress); - if (it == data.end()) + lock_guard guard(lock_); + auto it = activeData.find(startAddress); + if (it == activeData.end()) return DATATYPE_NONE; return it->second.type; } @@ -548,9 +859,9 @@ void SymbolMap::FillSymbolListBox(HWND listbox,SymbolType symType) const { switch (symType) { case ST_FUNCTION: { - SendMessage(listbox, LB_INITSTORAGE, (WPARAM)functions.size(), (LPARAM)functions.size() * 30); + SendMessage(listbox, LB_INITSTORAGE, (WPARAM)activeFunctions.size(), (LPARAM)activeFunctions.size() * 30); - for (auto it = functions.begin(), end = functions.end(); it != end; ++it) { + for (auto it = activeFunctions.begin(), end = activeFunctions.end(); it != end; ++it) { const FunctionEntry& entry = it->second; const char* name = GetLabelName(it->first); if (name != NULL) @@ -565,7 +876,7 @@ void SymbolMap::FillSymbolListBox(HWND listbox,SymbolType symType) const { case ST_DATA: { - int count = ARRAYSIZE(defaultSymbols)+(int)data.size(); + int count = ARRAYSIZE(defaultSymbols)+(int)activeData.size(); SendMessage(listbox, LB_INITSTORAGE, (WPARAM)count, (LPARAM)count * 30); for (int i = 0; i < ARRAYSIZE(defaultSymbols); i++) { @@ -574,7 +885,7 @@ void SymbolMap::FillSymbolListBox(HWND listbox,SymbolType symType) const { ListBox_SetItemData(listbox,index,defaultSymbols[i].address); } - for (auto it = data.begin(), end = data.end(); it != end; ++it) { + for (auto it = activeData.begin(), end = activeData.end(); it != end; ++it) { const DataEntry& entry = it->second; const char* name = GetLabelName(it->first); diff --git a/Core/Debugger/SymbolMap.h b/Core/Debugger/SymbolMap.h index f776324657..23b254fc21 100644 --- a/Core/Debugger/SymbolMap.h +++ b/Core/Debugger/SymbolMap.h @@ -74,19 +74,27 @@ public: void FillSymbolListBox(HWND listbox, SymbolType symType) const; #endif - void AddFunction(const char* name, u32 address, u32 size); + void AddModule(const char *name, u32 address, u32 size); + void UnloadModule(u32 address, u32 size); + u32 GetModuleRelativeAddr(u32 address, int moduleIndex = -1) const; + u32 GetModuleAbsoluteAddr(u32 relative, int moduleIndex) const; + int GetModuleIndex(u32 address) const; + bool IsModuleActive(int moduleIndex) const; + + void AddFunction(const char* name, u32 address, u32 size, int moduleIndex = -1); u32 GetFunctionStart(u32 address) const; int GetFunctionNum(u32 address) const; u32 GetFunctionSize(u32 startAddress) const; bool SetFunctionSize(u32 startAddress, u32 newSize); bool RemoveFunction(u32 startAddress, bool removeName); - void AddLabel(const char* name, u32 address); + void AddLabel(const char* name, u32 address, int moduleIndex = -1); void SetLabelName(const char* name, u32 address); - const char* GetLabelName(u32 address) const; + const char *GetLabelName(u32 address) const; + const char *GetLabelNameRel(u32 relAddress, int moduleIndex) const; bool GetLabelValue(const char* name, u32& dest); - void AddData(u32 address, u32 size, DataType type); + void AddData(u32 address, u32 size, DataType type, int moduleIndex = -1); u32 GetDataStart(u32 address) const; u32 GetDataSize(u32 startAddress) const; DataType GetDataType(u32 startAddress) const; @@ -95,24 +103,51 @@ public: private: void AssignFunctionIndices(); + void UpdateActiveSymbols(); struct FunctionEntry { + u32 start; u32 size; int index; + int module; }; struct LabelEntry { + u32 addr; + int module; char name[128]; }; struct DataEntry { DataType type; + u32 start; u32 size; + int module; }; - std::map functions; - std::map labels; - std::map data; + struct ModuleEntry { + // Note: this index is +1, 0 matches any for backwards-compat. + int index; + u32 start; + u32 size; + char name[128]; + }; + + // These are flattened, read-only copies of the actual data in active modules only. + std::map activeFunctions; + std::map activeLabels; + std::map activeData; + + // This is indexed by the end address of the module. + std::map activeModuleEnds; + + typedef std::pair SymbolKey; + + // These are indexed by the module id and relative address in the module. + std::map functions; + std::map labels; + std::map data; + std::vector modules; mutable recursive_mutex lock_; }; diff --git a/Core/HLE/sceKernelModule.cpp b/Core/HLE/sceKernelModule.cpp index b7f0625419..3fa3bfa78e 100644 --- a/Core/HLE/sceKernelModule.cpp +++ b/Core/HLE/sceKernelModule.cpp @@ -213,6 +213,7 @@ public: ~Module() { if (memoryBlockAddr) { userMemory.Free(memoryBlockAddr); + symbolMap.UnloadModule(memoryBlockAddr, memoryBlockSize); } } const char *GetName() {return nm.name;} @@ -266,6 +267,12 @@ public: VarSymbolImport vsi = {{0}}; p.Do(importedVars, vsi); RebuildImpExpModuleNames(); + + if (p.mode == p.MODE_READ) { + char moduleName[29] = {0}; + strncpy(moduleName, nm.name, ARRAY_SIZE(nm.name)); + symbolMap.AddModule(moduleName, memoryBlockAddr, memoryBlockSize); + } } // We don't do this in the destructor to avoid annoying messages on game shutdown. @@ -745,7 +752,7 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro *error_string = "Missing key"; delete [] newptr; module->isFake = true; - strncpy(module->nm.name, head->modname, 28); + strncpy(module->nm.name, head->modname, ARRAY_SIZE(module->nm.name)); module->nm.entry_addr = -1; module->nm.gp_value = -1; return module; @@ -814,13 +821,17 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro module->nm.data_size += reader.GetSegmentDataSize(i); } module->nm.gp_value = modinfo->gp; - strncpy(module->nm.name, modinfo->name, 28); + strncpy(module->nm.name, modinfo->name, ARRAY_SIZE(module->nm.name)); + + // Let's also get a truncated version. + char moduleName[29] = {0}; + strncpy(moduleName, modinfo->name, ARRAY_SIZE(module->nm.name)); // Check for module blacklist - we don't allow games to load these modules from disc // as we have HLE implementations and the originals won't run in the emu because they // directly access hardware or for other reasons. for (u32 i = 0; i < ARRAY_SIZE(blacklistedModules); i++) { - if (strcmp(modinfo->name, blacklistedModules[i]) == 0) { + if (strncmp(modinfo->name, blacklistedModules[i], ARRAY_SIZE(modinfo->name)) == 0) { *error_string = "Blacklisted"; if (newptr) { @@ -832,6 +843,8 @@ Module *__KernelLoadELFFromPtr(const u8 *ptr, u32 loadAddress, std::string *erro } } + symbolMap.AddModule(moduleName, module->memoryBlockAddr, module->memoryBlockSize); + SectionID textSection = reader.GetSectionByName(".text"); if (textSection != -1) { From b1af4f49113adde4057f38c5a5b0a4e56db510dc Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Jan 2014 21:12:16 -0800 Subject: [PATCH 2/5] Write symbol maps as ".ppmap" instead. This has some benefits: * They are smaller, but zgrep/zcat etc. still work. * Since the format is slightly different, bisecting won't overwrite. * It avoids conflicts with .map file associations. --- Core/Debugger/SymbolMap.cpp | 39 ++++++++++++++++++++++++------------- Windows/WindowsHost.cpp | 7 +++++-- Windows/WndMainWindow.cpp | 4 ++-- 3 files changed, 33 insertions(+), 17 deletions(-) diff --git a/Core/Debugger/SymbolMap.cpp b/Core/Debugger/SymbolMap.cpp index 29349e325d..d4c900ea56 100644 --- a/Core/Debugger/SymbolMap.cpp +++ b/Core/Debugger/SymbolMap.cpp @@ -24,6 +24,8 @@ #include +#include "util/text/utf8.h" +#include "zlib.h" #include "Common/CommonTypes.h" #include "Common/FileUtil.h" #include "Core/MemMap.h" @@ -52,8 +54,14 @@ void SymbolMap::Clear() { bool SymbolMap::LoadSymbolMap(const char *filename) { lock_guard guard(lock_); Clear(); - FILE *f = File::OpenCFile(filename, "r"); - if (!f) + +#if defined(_WIN32) && defined(UNICODE) + gzFile f = gzopen_w(ConvertUTF8ToWString(filename).c_str(), "r"); +#else + gzFile f = gzopen(filename, "r"); +#endif + + if (f == Z_NULL) return false; //char temp[256]; @@ -65,9 +73,9 @@ bool SymbolMap::LoadSymbolMap(const char *filename) { bool started = false; bool hasModules = false; - while (!feof(f)) { + while (!gzeof(f)) { char line[512], temp[256] = {0}; - char *p = fgets(line, 512, f); + char *p = gzgets(f, line, 512); if (p == NULL) break; @@ -156,9 +164,9 @@ bool SymbolMap::LoadSymbolMap(const char *filename) { } } } - fclose(f); + gzclose(f); SortSymbols(); - return true; + return started; } void SymbolMap::SaveSymbolMap(const char *filename) const { @@ -169,27 +177,32 @@ void SymbolMap::SaveSymbolMap(const char *filename) const { return; } - FILE *f = File::OpenCFile(filename, "w"); - if (!f) +#if defined(_WIN32) && defined(UNICODE) + gzFile f = gzopen_w(ConvertUTF8ToWString(filename).c_str(), "w9"); +#else + gzFile f = gzopen(filename, "w9"); +#endif + + if (f == Z_NULL) return; - fprintf(f,".text\n"); + gzprintf(f, ".text\n"); for (auto it = modules.begin(), end = modules.end(); it != end; ++it) { const ModuleEntry &mod = *it; - fprintf(f, ".module %x %08x %08x %s\n", mod.index, mod.start, mod.size, mod.name); + gzprintf(f, ".module %x %08x %08x %s\n", mod.index, mod.start, mod.size, mod.name); } for (auto it = functions.begin(), end = functions.end(); it != end; ++it) { const FunctionEntry& e = it->second; - fprintf(f, "%08x %08x %x %i %s\n", e.start, e.size, e.module, ST_FUNCTION, GetLabelNameRel(e.start, e.module)); + gzprintf(f, "%08x %08x %x %i %s\n", e.start, e.size, e.module, ST_FUNCTION, GetLabelNameRel(e.start, e.module)); } for (auto it = data.begin(), end = data.end(); it != end; ++it) { const DataEntry& e = it->second; - fprintf(f, "%08x %08x %x %i %s\n", e.start, e.size, e.module, ST_DATA, GetLabelNameRel(e.start, e.module)); + gzprintf(f, "%08x %08x %x %i %s\n", e.start, e.size, e.module, ST_DATA, GetLabelNameRel(e.start, e.module)); } - fclose(f); + gzclose(f); } bool SymbolMap::LoadNocashSym(const char *filename) { diff --git a/Windows/WindowsHost.cpp b/Windows/WindowsHost.cpp index b536ae37f7..dd0d443153 100644 --- a/Windows/WindowsHost.cpp +++ b/Windows/WindowsHost.cpp @@ -241,14 +241,17 @@ static std::string SymbolMapFilename(const char *currentFilename, char* ext) bool WindowsHost::AttemptLoadSymbolMap() { - bool result1 = symbolMap.LoadSymbolMap(SymbolMapFilename(PSP_CoreParameter().fileToStart.c_str(),".map").c_str()); + bool result1 = symbolMap.LoadSymbolMap(SymbolMapFilename(PSP_CoreParameter().fileToStart.c_str(),".ppmap").c_str()); + // Load the old-style map file. + if (!result1) + result1 = symbolMap.LoadSymbolMap(SymbolMapFilename(PSP_CoreParameter().fileToStart.c_str(),".map").c_str()); bool result2 = symbolMap.LoadNocashSym(SymbolMapFilename(PSP_CoreParameter().fileToStart.c_str(),".sym").c_str()); return result1 || result2; } void WindowsHost::SaveSymbolMap() { - symbolMap.SaveSymbolMap(SymbolMapFilename(PSP_CoreParameter().fileToStart.c_str(),".map").c_str()); + symbolMap.SaveSymbolMap(SymbolMapFilename(PSP_CoreParameter().fileToStart.c_str(),".ppmap").c_str()); } bool WindowsHost::IsDebuggingEnabled() diff --git a/Windows/WndMainWindow.cpp b/Windows/WndMainWindow.cpp index 40a535401b..a7be71dbce 100644 --- a/Windows/WndMainWindow.cpp +++ b/Windows/WndMainWindow.cpp @@ -1286,7 +1286,7 @@ namespace MainWindow break; case ID_DEBUG_LOADMAPFILE: - if (W32Util::BrowseForFileName(true, hWnd, L"Load .MAP",0,L"Maps\0*.map\0All files\0*.*\0\0",L"map",fn)) { + if (W32Util::BrowseForFileName(true, hWnd, L"Load .ppmap",0,L"Maps\0*.ppmap\0All files\0*.*\0\0",L"ppmap",fn)) { symbolMap.LoadSymbolMap(fn.c_str()); if (disasmWindow[0]) @@ -1298,7 +1298,7 @@ namespace MainWindow break; case ID_DEBUG_SAVEMAPFILE: - if (W32Util::BrowseForFileName(false, hWnd, L"Save .MAP",0,L"Maps\0*.map\0All files\0*.*\0\0",L"map",fn)) + if (W32Util::BrowseForFileName(false, hWnd, L"Save .ppmap",0,L"Maps\0*.ppmap\0All files\0*.*\0\0",L"ppmap",fn)) symbolMap.SaveSymbolMap(fn.c_str()); break; From 76afb2a8d5eaed855a92b6f13c0d343ab951474c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sat, 25 Jan 2014 21:40:23 -0800 Subject: [PATCH 3/5] Avoid returning points from the symbol map. Now that it uses a lookup, this is even more dangerous. But, the maps could be reordered while it's trying to print the pointer and cause that data to become invalid. This should be safe from race conditions. --- Core/Debugger/Breakpoints.cpp | 2 +- Core/Debugger/DebugInterface.h | 3 ++- Core/Debugger/DisassemblyManager.cpp | 28 +++++++++++++------------- Core/Debugger/SymbolMap.cpp | 13 +++++++++--- Core/Debugger/SymbolMap.h | 7 ++++--- Core/MIPS/MIPSAnalyst.cpp | 17 ++++++++++++---- Core/MIPS/MIPSDebugInterface.cpp | 2 +- Core/MIPS/MIPSDebugInterface.h | 3 ++- Core/MIPS/x86/Jit.cpp | 6 +++--- Windows/Debugger/CtrlDisAsmView.cpp | 30 ++++++++++++++-------------- Windows/Debugger/Debugger_Lists.cpp | 8 ++++---- 11 files changed, 69 insertions(+), 50 deletions(-) diff --git a/Core/Debugger/Breakpoints.cpp b/Core/Debugger/Breakpoints.cpp index fa416b8c3e..7ea1cf1f28 100644 --- a/Core/Debugger/Breakpoints.cpp +++ b/Core/Debugger/Breakpoints.cpp @@ -41,7 +41,7 @@ void MemCheck::Action(u32 addr, bool write, int size, u32 pc) ++numHits; if (result & MEMCHECK_LOG) - NOTICE_LOG(MEMMAP, "CHK %s%i at %08x (%s), PC=%08x (%s)", write ? "Write" : "Read", size * 8, addr, symbolMap.GetDescription(addr), pc, symbolMap.GetDescription(pc)); + NOTICE_LOG(MEMMAP, "CHK %s%i at %08x (%s), PC=%08x (%s)", write ? "Write" : "Read", size * 8, addr, symbolMap.GetDescription(addr).c_str(), pc, symbolMap.GetDescription(pc)); if (result & MEMCHECK_BREAK) { Core_EnableStepping(true); diff --git a/Core/Debugger/DebugInterface.h b/Core/Debugger/DebugInterface.h index 56a89529df..e356194269 100644 --- a/Core/Debugger/DebugInterface.h +++ b/Core/Debugger/DebugInterface.h @@ -16,6 +16,7 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #pragma once +#include #include #include "Common/CommonTypes.h" #include "native/math/expression_parser.h" @@ -45,7 +46,7 @@ public: virtual void step() {} virtual void runToBreakpoint() {} virtual int getColor(unsigned int address){return 0xFFFFFFFF;} - virtual const char *getDescription(unsigned int address) {return "";} + virtual std::string getDescription(unsigned int address) {return "";} virtual bool initExpression(const char* exp, PostfixExpression& dest) { return false; }; virtual bool parseExpression(PostfixExpression& exp, u32& dest) { return false; }; diff --git a/Core/Debugger/DisassemblyManager.cpp b/Core/Debugger/DisassemblyManager.cpp index 57a0f55ac2..96e0d46756 100644 --- a/Core/Debugger/DisassemblyManager.cpp +++ b/Core/Debugger/DisassemblyManager.cpp @@ -15,7 +15,7 @@ // Official git repository and contact information can be found at // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. - +#include #include #include @@ -70,10 +70,10 @@ void parseDisasm(const char* disasm, char* opcode, char* arguments, bool insertS u32 branchTarget; sscanf(disasm+3,"%08x",&branchTarget); - const char* addressSymbol = symbolMap.GetLabelName(branchTarget); - if (addressSymbol != NULL && insertSymbols) + const std::string addressSymbol = symbolMap.GetLabelString(branchTarget); + if (!addressSymbol.empty() && insertSymbols) { - arguments += sprintf(arguments,"%s",addressSymbol); + arguments += sprintf(arguments,"%s",addressSymbol.c_str()); } else { arguments += sprintf(arguments,"0x%08X",branchTarget); } @@ -740,16 +740,16 @@ bool DisassemblyMacro::disassemble(u32 address, DisassemblyLineInfo& dest, bool dest.type = DISTYPE_MACRO; dest.info = MIPSAnalyst::GetOpcodeInfo(DisassemblyManager::getCpu(),address); - const char* addressSymbol; + std::string addressSymbol; switch (type) { case MACRO_LI: dest.name = name; - addressSymbol = symbolMap.GetLabelName(immediate); - if (addressSymbol != NULL && insertSymbols) + addressSymbol = symbolMap.GetLabelString(immediate); + if (!addressSymbol.empty() && insertSymbols) { - sprintf(buffer,"%s,%s",DisassemblyManager::getCpu()->GetRegName(0,rt),addressSymbol); + sprintf(buffer,"%s,%s",DisassemblyManager::getCpu()->GetRegName(0,rt),addressSymbol.c_str()); } else { sprintf(buffer,"%s,0x%08X",DisassemblyManager::getCpu()->GetRegName(0,rt),immediate); } @@ -762,10 +762,10 @@ bool DisassemblyMacro::disassemble(u32 address, DisassemblyLineInfo& dest, bool case MACRO_MEMORYIMM: dest.name = name; - addressSymbol = symbolMap.GetLabelName(immediate); - if (addressSymbol != NULL && insertSymbols) + addressSymbol = symbolMap.GetLabelString(immediate); + if (!addressSymbol.empty() && insertSymbols) { - sprintf(buffer,"%s,%s",DisassemblyManager::getCpu()->GetRegName(0,rt),addressSymbol); + sprintf(buffer,"%s,%s",DisassemblyManager::getCpu()->GetRegName(0,rt),addressSymbol.c_str()); } else { sprintf(buffer,"%s,0x%08X",DisassemblyManager::getCpu()->GetRegName(0,rt),immediate); } @@ -956,9 +956,9 @@ void DisassemblyData::createLines() case DATATYPE_WORD: { value = Memory::Read_U32(pos); - const char* label = symbolMap.GetLabelName(value); - if (label != NULL) - sprintf(buffer,"%s",label); + const std::string label = symbolMap.GetLabelString(value); + if (!label.empty()) + sprintf(buffer,"%s",label.c_str()); else sprintf(buffer,"0x%08X",value); pos += 4; diff --git a/Core/Debugger/SymbolMap.cpp b/Core/Debugger/SymbolMap.cpp index d4c900ea56..4bb51498d8 100644 --- a/Core/Debugger/SymbolMap.cpp +++ b/Core/Debugger/SymbolMap.cpp @@ -333,9 +333,7 @@ u32 SymbolMap::GetNextSymbolAddress(u32 address, SymbolType symmask) { return dataAddress; } -static char descriptionTemp[256]; - -const char *SymbolMap::GetDescription(unsigned int address) const { +std::string SymbolMap::GetDescription(unsigned int address) const { lock_guard guard(lock_); const char* labelName = NULL; @@ -351,6 +349,7 @@ const char *SymbolMap::GetDescription(unsigned int address) const { if (labelName != NULL) return labelName; + char descriptionTemp[256]; sprintf(descriptionTemp, "(%08x)", address); return descriptionTemp; } @@ -742,6 +741,14 @@ const char *SymbolMap::GetLabelNameRel(u32 relAddress, int moduleIndex) const { return it->second.name; } +std::string SymbolMap::GetLabelString(u32 address) const { + lock_guard guard(lock_); + const char *label = GetLabelName(address); + if (label == NULL) + return ""; + return label; +} + bool SymbolMap::GetLabelValue(const char* name, u32& dest) { lock_guard guard(lock_); for (auto it = activeLabels.begin(); it != activeLabels.end(); it++) { diff --git a/Core/Debugger/SymbolMap.h b/Core/Debugger/SymbolMap.h index 23b254fc21..0b7825b356 100644 --- a/Core/Debugger/SymbolMap.h +++ b/Core/Debugger/SymbolMap.h @@ -67,7 +67,7 @@ public: SymbolType GetSymbolType(u32 address) const; bool GetSymbolInfo(SymbolInfo *info, u32 address, SymbolType symmask = ST_FUNCTION) const; u32 GetNextSymbolAddress(u32 address, SymbolType symmask); - const char *GetDescription(unsigned int address) const; + std::string GetDescription(unsigned int address) const; std::vector GetAllSymbols(SymbolType symmask); #ifdef _WIN32 @@ -89,9 +89,8 @@ public: bool RemoveFunction(u32 startAddress, bool removeName); void AddLabel(const char* name, u32 address, int moduleIndex = -1); + std::string GetLabelString(u32 address) const; void SetLabelName(const char* name, u32 address); - const char *GetLabelName(u32 address) const; - const char *GetLabelNameRel(u32 relAddress, int moduleIndex) const; bool GetLabelValue(const char* name, u32& dest); void AddData(u32 address, u32 size, DataType type, int moduleIndex = -1); @@ -104,6 +103,8 @@ public: private: void AssignFunctionIndices(); void UpdateActiveSymbols(); + const char *GetLabelName(u32 address) const; + const char *GetLabelNameRel(u32 relAddress, int moduleIndex) const; struct FunctionEntry { u32 start; diff --git a/Core/MIPS/MIPSAnalyst.cpp b/Core/MIPS/MIPSAnalyst.cpp index d8437d06b9..66b1aca7b0 100644 --- a/Core/MIPS/MIPSAnalyst.cpp +++ b/Core/MIPS/MIPSAnalyst.cpp @@ -264,6 +264,15 @@ skip: return !strncmp(name, "z_un_", strlen("z_un_")) || !strncmp(name, "u_un_", strlen("u_un_")); } + static bool IsDefaultFunction(const std::string &name) { + if (name.empty()) { + // Must be I guess? + return true; + } + + return IsDefaultFunction(name.c_str()); + } + static u32 ScanAheadForJumpback(u32 fromAddr, u32 knownStart, u32 knownEnd) { static const u32 MAX_AHEAD_SCAN = 0x1000; // Maybe a bit high... just to make sure we don't get confused by recursive tail recursion. @@ -516,13 +525,13 @@ skip: continue; } // Functions with default names aren't very interesting either. - const char *name = symbolMap.GetLabelName(f.start); + const std::string name = symbolMap.GetLabelString(f.start); if (IsDefaultFunction(name)) { continue; } HashMapFunc mf = { "", f.hash, f.size }; - strncpy(mf.name, name, sizeof(mf.name) - 1); + strncpy(mf.name, name.c_str(), sizeof(mf.name) - 1); hashMap.insert(mf); } } @@ -584,10 +593,10 @@ skip: if (f.hash == mf->hash && f.size == mf->size) { strncpy(f.name, mf->name, sizeof(mf->name) - 1); - const char *existingLabel = symbolMap.GetLabelName(f.start); + std::string existingLabel = symbolMap.GetLabelString(f.start); char defaultLabel[256]; // If it was renamed, keep it. Only change the name if it's still the default. - if (existingLabel == NULL || !strcmp(existingLabel, DefaultFunctionName(defaultLabel, f.start))) { + if (existingLabel.empty() || !strcmp(existingLabel.c_str(), DefaultFunctionName(defaultLabel, f.start))) { symbolMap.SetLabelName(mf->name, f.start); } } diff --git a/Core/MIPS/MIPSDebugInterface.cpp b/Core/MIPS/MIPSDebugInterface.cpp index c29f2dc8f3..ad7ccd42a8 100644 --- a/Core/MIPS/MIPSDebugInterface.cpp +++ b/Core/MIPS/MIPSDebugInterface.cpp @@ -223,7 +223,7 @@ int MIPSDebugInterface::getColor(unsigned int address) if (n==-1) return 0xFFFFFF; return colors[n%6]; } -const char *MIPSDebugInterface::getDescription(unsigned int address) +std::string MIPSDebugInterface::getDescription(unsigned int address) { return symbolMap.GetDescription(address); } diff --git a/Core/MIPS/MIPSDebugInterface.h b/Core/MIPS/MIPSDebugInterface.h index c83ffa99d0..28665b697f 100644 --- a/Core/MIPS/MIPSDebugInterface.h +++ b/Core/MIPS/MIPSDebugInterface.h @@ -17,6 +17,7 @@ #pragma once +#include #include "MIPS.h" #include "Core/Debugger/DebugInterface.h" @@ -39,7 +40,7 @@ public: virtual void step() {} virtual void runToBreakpoint(); virtual int getColor(unsigned int address); - virtual const char *getDescription(unsigned int address); + virtual std::string getDescription(unsigned int address); virtual bool initExpression(const char* exp, PostfixExpression& dest); virtual bool parseExpression(PostfixExpression& exp, u32& dest); diff --git a/Core/MIPS/x86/Jit.cpp b/Core/MIPS/x86/Jit.cpp index ee9f96f8c4..07a984de53 100644 --- a/Core/MIPS/x86/Jit.cpp +++ b/Core/MIPS/x86/Jit.cpp @@ -374,9 +374,9 @@ bool Jit::DescribeCodePtr(const u8 *ptr, std::string &name) else if (jitAddr != (u32)-1) { char temp[1024]; - const char *label = symbolMap.GetDescription(jitAddr); - if (label != NULL) - snprintf(temp, sizeof(temp), "%08x_%s", jitAddr, label); + const std::string label = symbolMap.GetDescription(jitAddr); + if (!label.empty()) + snprintf(temp, sizeof(temp), "%08x_%s", jitAddr, label.c_str()); else snprintf(temp, sizeof(temp), "%08x", jitAddr); name = temp; diff --git a/Windows/Debugger/CtrlDisAsmView.cpp b/Windows/Debugger/CtrlDisAsmView.cpp index a50716f009..ec54e330b4 100644 --- a/Windows/Debugger/CtrlDisAsmView.cpp +++ b/Windows/Debugger/CtrlDisAsmView.cpp @@ -205,8 +205,8 @@ bool CtrlDisAsmView::getDisasmAddressText(u32 address, char* dest, bool abbrevia { if (displaySymbols) { - const char* addressSymbol = symbolMap.GetLabelName(address); - if (addressSymbol != NULL) + const std::string addressSymbol = symbolMap.GetLabelString(address); + if (!addressSymbol.empty()) { for (int k = 0; addressSymbol[k] != 0; k++) { @@ -915,7 +915,7 @@ void CtrlDisAsmView::onMouseUp(WPARAM wParam, LPARAM lParam, int button) { char name[256]; std::string newname; - strncpy_s(name, symbolMap.GetLabelName(funcBegin),_TRUNCATE); + strncpy_s(name, symbolMap.GetLabelString(funcBegin).c_str(),_TRUNCATE); if (InputBox_GetString(MainWindow::GetHInstance(), MainWindow::GetHWND(), L"New function name", name, newname)) { symbolMap.SetLabelName(newname.c_str(),funcBegin); u32 funcSize = symbolMap.GetFunctionSize(curAddress); @@ -1048,10 +1048,10 @@ void CtrlDisAsmView::updateStatusBarText() // TODO: Could also be a float... { u32 data = Memory::Read_U32(line.info.dataAddress); - const char* addressSymbol = symbolMap.GetLabelName(data); - if (addressSymbol) + const std::string addressSymbol = symbolMap.GetLabelString(data); + if (!addressSymbol.empty()) { - sprintf(text,"[%08X] = %s (%08X)",line.info.dataAddress,addressSymbol,data); + sprintf(text,"[%08X] = %s (%08X)",line.info.dataAddress,addressSymbol.c_str(),data); } else { sprintf(text,"[%08X] = %08X",line.info.dataAddress,data); } @@ -1066,12 +1066,12 @@ void CtrlDisAsmView::updateStatusBarText() if (line.info.isBranch) { - const char* addressSymbol = symbolMap.GetLabelName(line.info.branchTarget); - if (addressSymbol == NULL) + const std::string addressSymbol = symbolMap.GetLabelString(line.info.branchTarget); + if (addressSymbol.empty()) { sprintf(text,"%08X",line.info.branchTarget); } else { - sprintf(text,"%08X = %s",line.info.branchTarget,addressSymbol); + sprintf(text,"%08X = %s",line.info.branchTarget,addressSymbol.c_str()); } } } else if (line.type == DISTYPE_DATA) @@ -1081,14 +1081,14 @@ void CtrlDisAsmView::updateStatusBarText() start = curAddress; u32 diff = curAddress-start; - const char* label = symbolMap.GetLabelName(start); + const std::string label = symbolMap.GetLabelString(start); - if (label != NULL) + if (!label.empty()) { if (diff != 0) - sprintf(text,"%08X (%s) + %08X",start,label,diff); + sprintf(text,"%08X (%s) + %08X",start,label.c_str(),diff); else - sprintf(text,"%08X (%s)",start,label); + sprintf(text,"%08X (%s)",start,label.c_str()); } else { if (diff != 0) sprintf(text,"%08X + %08X",start,diff); @@ -1204,7 +1204,7 @@ std::string CtrlDisAsmView::disassembleRange(u32 start, u32 size) { MIPSAnalyst::MipsOpcodeInfo info = MIPSAnalyst::GetOpcodeInfo(debugger,start+i); - if (info.isBranch && symbolMap.GetLabelName(info.branchTarget) == NULL) + if (info.isBranch && symbolMap.GetLabelString(info.branchTarget).empty()) { if (branchAddresses.find(info.branchTarget) == branchAddresses.end()) { @@ -1236,7 +1236,7 @@ std::string CtrlDisAsmView::disassembleRange(u32 start, u32 size) } if (line.info.isBranch && !line.info.isBranchToRegister - && symbolMap.GetLabelName(line.info.branchTarget) == NULL + && symbolMap.GetLabelString(line.info.branchTarget).empty() && branchAddresses.find(line.info.branchTarget) != branchAddresses.end()) { sprintf(buffer,"pos_%08X",line.info.branchTarget); diff --git a/Windows/Debugger/Debugger_Lists.cpp b/Windows/Debugger/Debugger_Lists.cpp index b73cd1b302..b5f1de0115 100644 --- a/Windows/Debugger/Debugger_Lists.cpp +++ b/Windows/Debugger/Debugger_Lists.cpp @@ -463,8 +463,8 @@ void CtrlBreakpointList::GetColumnText(wchar_t* dest, int row, int col) else wsprintf(dest,L"0x%08X",mc.end-mc.start); } else { - const char* sym = symbolMap.GetLabelName(displayedBreakPoints_[index].addr); - if (sym != NULL) + const std::string sym = symbolMap.GetLabelString(displayedBreakPoints_[index].addr); + if (!sym.empty()) { std::wstring s = ConvertUTF8ToWString(sym); wcscpy(dest,s.c_str()); @@ -635,8 +635,8 @@ void CtrlStackTraceView::GetColumnText(wchar_t* dest, int row, int col) break; case SF_ENTRYNAME: { - const char* sym = symbolMap.GetLabelName(frames[row].entry); - if (sym != NULL) { + const std::string sym = symbolMap.GetLabelString(frames[row].entry); + if (!sym.empty()) { wcscpy(dest, ConvertUTF8ToWString(sym).c_str()); } else { wcscpy(dest,L"-"); From 983e195dd2b8a5f07a8da15b49445ea725650547 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 26 Jan 2014 00:02:01 -0800 Subject: [PATCH 4/5] Fix possible module index overlap. --- Core/Debugger/SymbolMap.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Debugger/SymbolMap.cpp b/Core/Debugger/SymbolMap.cpp index 4bb51498d8..a5c4ada040 100644 --- a/Core/Debugger/SymbolMap.cpp +++ b/Core/Debugger/SymbolMap.cpp @@ -443,7 +443,7 @@ u32 SymbolMap::GetModuleAbsoluteAddr(u32 relative, int moduleIndex) const { int SymbolMap::GetModuleIndex(u32 address) const { lock_guard guard(lock_); - auto iter = activeModuleEnds.lower_bound(address); + auto iter = activeModuleEnds.upper_bound(address); if (iter == activeModuleEnds.end()) return -1; return iter->second.index; From 6b06494940ea9ae61439695800e641b50bb44e81 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 26 Jan 2014 01:56:28 -0800 Subject: [PATCH 5/5] Oops, missed a c_str() conversion. --- Core/Debugger/Breakpoints.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/Debugger/Breakpoints.cpp b/Core/Debugger/Breakpoints.cpp index 7ea1cf1f28..65dd5109a5 100644 --- a/Core/Debugger/Breakpoints.cpp +++ b/Core/Debugger/Breakpoints.cpp @@ -41,7 +41,7 @@ void MemCheck::Action(u32 addr, bool write, int size, u32 pc) ++numHits; if (result & MEMCHECK_LOG) - NOTICE_LOG(MEMMAP, "CHK %s%i at %08x (%s), PC=%08x (%s)", write ? "Write" : "Read", size * 8, addr, symbolMap.GetDescription(addr).c_str(), pc, symbolMap.GetDescription(pc)); + NOTICE_LOG(MEMMAP, "CHK %s%i at %08x (%s), PC=%08x (%s)", write ? "Write" : "Read", size * 8, addr, symbolMap.GetDescription(addr).c_str(), pc, symbolMap.GetDescription(pc).c_str()); if (result & MEMCHECK_BREAK) { Core_EnableStepping(true);