From 3c9c6fa17bee86ee81bb6f6cdefa759787431d09 Mon Sep 17 00:00:00 2001 From: "Mr. eXoDia" Date: Sat, 19 Jul 2014 04:34:22 +0200 Subject: [PATCH] DBG: performance improvement with the memory map --- x64_dbg_dbg/_exports.cpp | 1 + x64_dbg_dbg/debugger.cpp | 24 ++++++++++++--------- x64_dbg_dbg/instruction.cpp | 4 ++-- x64_dbg_dbg/memory.cpp | 7 +++++- x64_dbg_dbg/memory.h | 2 +- x64_dbg_dbg/reference.cpp | 2 +- x64_dbg_dbg/threading.cpp | 43 +++++++++++++++++++++++++++++++++++++ x64_dbg_dbg/threading.h | 20 +++++++++++++++++ x64_dbg_dbg/x64_dbg.cpp | 1 + 9 files changed, 89 insertions(+), 15 deletions(-) diff --git a/x64_dbg_dbg/_exports.cpp b/x64_dbg_dbg/_exports.cpp index f99d78eb..458a7c9a 100644 --- a/x64_dbg_dbg/_exports.cpp +++ b/x64_dbg_dbg/_exports.cpp @@ -33,6 +33,7 @@ extern "C" DLL_EXPORT bool _dbg_memwrite(duint addr, const unsigned char* src, d extern "C" DLL_EXPORT bool _dbg_memmap(MEMMAP* memmap) { + CriticalSectionLocker locker(LockMemoryPages); int pagecount=(int)memoryPages.size(); memset(memmap, 0, sizeof(MEMMAP)); memmap->count=pagecount; diff --git a/x64_dbg_dbg/debugger.cpp b/x64_dbg_dbg/debugger.cpp index 8744ad32..8436500f 100644 --- a/x64_dbg_dbg/debugger.cpp +++ b/x64_dbg_dbg/debugger.cpp @@ -104,6 +104,9 @@ void dbginit() exceptionNames.insert(std::make_pair(0xC0000409, "STATUS_STACK_BUFFER_OVERRUN")); exceptionNames.insert(std::make_pair(0xC0000417, "STATUS_INVALID_CRUNTIME_PARAMETER")); exceptionNames.insert(std::make_pair(0xC0000420, "STATUS_ASSERTION_FAILURE")); + exceptionNames.insert(std::make_pair(0x04242420, "CLRDBG_NOTIFICATION_EXCEPTION_CODE")); + exceptionNames.insert(std::make_pair(0xE0434352, "CLR_EXCEPTION")); + exceptionNames.insert(std::make_pair(0xE06D7363, "CPP_EH_EXCEPTION")); } void dbgdisablebpx() @@ -180,7 +183,6 @@ bool dbgcmddel(const char* name) void DebugUpdateGui(uint disasm_addr, bool stack) { - memupdatemap(fdProcessInfo->hProcess); //update memory map uint cip=GetContextData(UE_CIP); if(memisvalidreadptr(fdProcessInfo->hProcess, disasm_addr)) GuiDisasmAt(disasm_addr, cip); @@ -190,12 +192,13 @@ void DebugUpdateGui(uint disasm_addr, bool stack) GuiStackDumpAt(csp, csp); } char modname[MAX_MODULE_SIZE]=""; + char modtext[MAX_MODULE_SIZE*2]=""; if(!modnamefromaddr(disasm_addr, modname, true)) *modname=0; - else - sprintf(modname, "Module: %s - ", modname); + else + sprintf(modtext, "Module: %s - ", modname); char title[1024]=""; - sprintf(title, "File: %s - PID: %X - %sThread: %X", szBaseFileName, fdProcessInfo->dwProcessId, modname, ((DEBUG_EVENT*)GetDebugData())->dwThreadId); + sprintf(title, "File: %s - PID: %X - %sThread: %X", szBaseFileName, fdProcessInfo->dwProcessId, modtext, ((DEBUG_EVENT*)GetDebugData())->dwThreadId); GuiUpdateWindowTitle(title); GuiUpdateAllViews(); } @@ -327,7 +330,7 @@ static void cbMemoryBreakpoint(void* ExceptionAddress) { uint cip=GetContextData(UE_CIP); uint size; - uint base=memfindbaseaddr((uint)ExceptionAddress, &size); + uint base=memfindbaseaddr((uint)ExceptionAddress, &size, true); BREAKPOINT bp; BRIDGEBP pluginBp; PLUG_CB_BREAKPOINT bpInfo; @@ -598,7 +601,7 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo) modInfo.SizeOfStruct=sizeof(modInfo); if(SymGetModuleInfo64(fdProcessInfo->hProcess, (DWORD64)base, &modInfo)) modload((uint)base, modInfo.ImageSize, modInfo.ImageName); - //bpenumall(0); //update breakpoint list + memupdatemap(fdProcessInfo->hProcess); //update memory map char modname[256]=""; if(modnamefromaddr((uint)base, modname, true)) bpenumall(cbSetModuleBreakpoints, modname); @@ -684,6 +687,7 @@ static void cbCreateThread(CREATE_THREAD_DEBUG_INFO* CreateThread) if(settingboolget("Events", "ThreadStart")) { + memupdatemap(fdProcessInfo->hProcess); //update memory map //update GUI DebugUpdateGui(GetContextData(UE_CIP), true); GuiSetDebugState(paused); @@ -728,7 +732,7 @@ static void cbSystemBreakpoint(void* ExceptionData) dputs("system breakpoint reached!"); bSkipExceptions=false; //we are not skipping first-chance exceptions uint cip=GetContextData(UE_CIP); - GuiDumpAt(memfindbaseaddr(cip, 0)); //dump somewhere + GuiDumpAt(memfindbaseaddr(cip, 0, true)); //dump somewhere //plugin callbacks PLUG_CB_SYSTEMBREAKPOINT callbackInfo; @@ -765,7 +769,7 @@ static void cbLoadDll(LOAD_DLL_DEBUG_INFO* LoadDll) modInfo.SizeOfStruct=sizeof(IMAGEHLP_MODULE64); if(SymGetModuleInfo64(fdProcessInfo->hProcess, (DWORD64)base, &modInfo)) modload((uint)base, modInfo.ImageSize, modInfo.ImageName); - //bpenumall(0); //update breakpoint list + memupdatemap(fdProcessInfo->hProcess); //update memory map char modname[256]=""; if(modnamefromaddr((uint)base, modname, true)) bpenumall(cbSetModuleBreakpoints, modname); @@ -1670,7 +1674,7 @@ CMDRESULT cbDebugSetMemoryBpx(int argc, char* argv[]) } } uint size=0; - uint base=memfindbaseaddr(addr, &size); + uint base=memfindbaseaddr(addr, &size, true); bool singleshoot=false; if(!restore) singleshoot=true; @@ -1958,7 +1962,7 @@ CMDRESULT cbDebugMemset(int argc, char* argv[]) } else { - uint base=memfindbaseaddr(addr, &size); + uint base=memfindbaseaddr(addr, &size, true); if(!base) { dputs("invalid address specified"); diff --git a/x64_dbg_dbg/instruction.cpp b/x64_dbg_dbg/instruction.cpp index 443d143b..6ae30260 100644 --- a/x64_dbg_dbg/instruction.cpp +++ b/x64_dbg_dbg/instruction.cpp @@ -1004,7 +1004,7 @@ CMDRESULT cbInstrFind(int argc, char* argv[]) if(pattern[len-1]=='#') pattern[len-1]='\0'; uint size=0; - uint base=memfindbaseaddr(addr, &size); + uint base=memfindbaseaddr(addr, &size, true); if(!base) { dprintf("invalid memory address "fhex"!\n", addr); @@ -1058,7 +1058,7 @@ CMDRESULT cbInstrFindAll(int argc, char* argv[]) if(pattern[len-1]=='#') pattern[len-1]='\0'; uint size=0; - uint base=memfindbaseaddr(addr, &size); + uint base=memfindbaseaddr(addr, &size, true); if(!base) { dprintf("invalid memory address "fhex"!\n", addr); diff --git a/x64_dbg_dbg/memory.cpp b/x64_dbg_dbg/memory.cpp index 9a5ebbd0..c8ada109 100644 --- a/x64_dbg_dbg/memory.cpp +++ b/x64_dbg_dbg/memory.cpp @@ -2,11 +2,13 @@ #include "debugger.h" #include "patches.h" #include "console.h" +#include "threading.h" MemoryMap memoryPages; void memupdatemap(HANDLE hProcess) { + CriticalSectionLocker locker(LockMemoryPages); MEMORY_BASIC_INFORMATION mbi; SIZE_T numBytes; uint MyAddress=0, newAddress=0; @@ -157,8 +159,11 @@ void memupdatemap(HANDLE hProcess) } } -uint memfindbaseaddr(uint addr, uint* size) +uint memfindbaseaddr(uint addr, uint* size, bool refresh) { + if(refresh) + memupdatemap(fdProcessInfo->hProcess); //update memory map + CriticalSectionLocker locker(LockMemoryPages); MemoryMap::iterator found=memoryPages.find(std::make_pair(addr, addr)); if(found==memoryPages.end()) return 0; diff --git a/x64_dbg_dbg/memory.h b/x64_dbg_dbg/memory.h index e1ef4b5d..59be3432 100644 --- a/x64_dbg_dbg/memory.h +++ b/x64_dbg_dbg/memory.h @@ -22,7 +22,7 @@ struct PATTERNBYTE }; void memupdatemap(HANDLE hProcess); -uint memfindbaseaddr(uint addr, uint* size); +uint memfindbaseaddr(uint addr, uint* size, bool refresh = false); bool memread(HANDLE hProcess, const void* lpBaseAddress, void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesRead); bool memwrite(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten); bool mempatch(HANDLE hProcess, void* lpBaseAddress, const void* lpBuffer, SIZE_T nSize, SIZE_T* lpNumberOfBytesWritten); diff --git a/x64_dbg_dbg/reference.cpp b/x64_dbg_dbg/reference.cpp index 426f7f90..cabf0963 100644 --- a/x64_dbg_dbg/reference.cpp +++ b/x64_dbg_dbg/reference.cpp @@ -9,7 +9,7 @@ int reffind(uint addr, uint size, CBREF cbRef, void* userinfo, bool silent) uint start_size; uint base; uint base_size; - base=memfindbaseaddr(addr, &base_size); + base=memfindbaseaddr(addr, &base_size, true); if(!base or !base_size) { if(!silent) diff --git a/x64_dbg_dbg/threading.cpp b/x64_dbg_dbg/threading.cpp index 6ff4910e..2b74c692 100644 --- a/x64_dbg_dbg/threading.cpp +++ b/x64_dbg_dbg/threading.cpp @@ -27,3 +27,46 @@ bool waitislocked(WAIT_ID id) { return waitarray[id]; } + +static CRITICAL_SECTION locks[LockLast] = {}; +static bool bInitDone = false; + +static void CriticalSectionInitializeLocks() +{ + if(bInitDone) + return; + for(int i=0; i