diff --git a/x64_dbg_dbg/_dbgfunctions.cpp b/x64_dbg_dbg/_dbgfunctions.cpp index 57bc9934..a7c75063 100644 --- a/x64_dbg_dbg/_dbgfunctions.cpp +++ b/x64_dbg_dbg/_dbgfunctions.cpp @@ -24,7 +24,7 @@ static bool _assembleatex(duint addr, const char* instruction, char* error, bool static bool _sectionfromaddr(duint addr, char* section) { - HMODULE hMod = (HMODULE)modbasefromaddr(addr); + HMODULE hMod = (HMODULE)ModBaseFromAddr(addr); if(!hMod) return false; wchar_t curModPath[MAX_PATH] = L""; @@ -169,10 +169,10 @@ void dbgfunctionsinit() { _dbgfunctions.AssembleAtEx = _assembleatex; _dbgfunctions.SectionFromAddr = _sectionfromaddr; - _dbgfunctions.ModNameFromAddr = modnamefromaddr; - _dbgfunctions.ModBaseFromAddr = modbasefromaddr; - _dbgfunctions.ModBaseFromName = modbasefromname; - _dbgfunctions.ModSizeFromAddr = modsizefromaddr; + _dbgfunctions.ModNameFromAddr = ModNameFromAddr; + _dbgfunctions.ModBaseFromAddr = ModBaseFromAddr; + _dbgfunctions.ModBaseFromName = ModBaseFromName; + _dbgfunctions.ModSizeFromAddr = ModSizeFromAddr; _dbgfunctions.Assemble = assemble; _dbgfunctions.PatchGet = _patchget; _dbgfunctions.PatchInRange = _patchinrange; @@ -181,8 +181,8 @@ void dbgfunctionsinit() _dbgfunctions.PatchEnum = (PATCHENUM)patchenum; _dbgfunctions.PatchRestore = _patchrestore; _dbgfunctions.PatchFile = (PATCHFILE)patchfile; - _dbgfunctions.ModPathFromAddr = modpathfromaddr; - _dbgfunctions.ModPathFromName = modpathfromname; + _dbgfunctions.ModPathFromAddr = ModPathFromAddr; + _dbgfunctions.ModPathFromName = ModPathFromName; _dbgfunctions.DisasmFast = disasmfast; _dbgfunctions.MemUpdateMap = _memupdatemap; _dbgfunctions.GetCallStack = _getcallstack; diff --git a/x64_dbg_dbg/_exports.cpp b/x64_dbg_dbg/_exports.cpp index 8c7c46ac..a166eb95 100644 --- a/x64_dbg_dbg/_exports.cpp +++ b/x64_dbg_dbg/_exports.cpp @@ -69,6 +69,7 @@ extern "C" DLL_EXPORT bool _dbg_isdebugging() { if(IsFileBeingDebugged()) return true; + return false; } @@ -93,7 +94,7 @@ extern "C" DLL_EXPORT bool _dbg_addrinfoget(duint addr, SEGMENTREG segment, ADDR bool retval = false; if(addrinfo->flags & flagmodule) //get module { - if(modnamefromaddr(addr, addrinfo->module, false)) //get module name + if(ModNameFromAddr(addr, addrinfo->module, false)) //get module name retval = true; } if(addrinfo->flags & flaglabel) @@ -701,7 +702,7 @@ extern "C" DLL_EXPORT uint _dbg_sendmessage(DBGMSG type, void* param1, void* par case DBG_MODBASE_FROM_NAME: { - return modbasefromname((const char*)param1); + return ModBaseFromName((const char*)param1); } break; diff --git a/x64_dbg_dbg/addrinfo.cpp b/x64_dbg_dbg/addrinfo.cpp index b49f2d87..07f4b451 100644 --- a/x64_dbg_dbg/addrinfo.cpp +++ b/x64_dbg_dbg/addrinfo.cpp @@ -123,7 +123,7 @@ bool apienumexports(uint base, EXPORTENUMCALLBACK cbEnum) if(!export_dir.NumberOfFunctions or !NumberOfNames) //no named exports return false; char modname[MAX_MODULE_SIZE] = ""; - modnamefromaddr(base, modname, true); + ModNameFromAddr(base, modname, true); uint original_name_va = export_dir.Name + base; char original_name[deflen] = ""; memset(original_name, 0, sizeof(original_name)); diff --git a/x64_dbg_dbg/bookmark.cpp b/x64_dbg_dbg/bookmark.cpp index 39785f54..fae3011a 100644 --- a/x64_dbg_dbg/bookmark.cpp +++ b/x64_dbg_dbg/bookmark.cpp @@ -19,14 +19,14 @@ bool bookmarkset(uint addr, bool manual) return false; BOOKMARKSINFO bookmark; - modnamefromaddr(addr, bookmark.mod, true); - bookmark.addr = addr - modbasefromaddr(addr); + ModNameFromAddr(addr, bookmark.mod, true); + bookmark.addr = addr - ModBaseFromAddr(addr); bookmark.manual = manual; // Exclusive lock to insert new data EXCLUSIVE_ACQUIRE(LockBookmarks); - if(!bookmarks.insert(std::make_pair(modhashfromva(addr), bookmark)).second) + if(!bookmarks.insert(std::make_pair(ModHashFromAddr(addr), bookmark)).second) return bookmarkdel(addr); return true; @@ -35,7 +35,7 @@ bool bookmarkset(uint addr, bool manual) bool bookmarkget(uint addr) { SHARED_ACQUIRE(LockBookmarks); - return (bookmarks.count(modhashfromva(addr)) > 0); + return (bookmarks.count(ModHashFromAddr(addr)) > 0); } bool bookmarkdel(uint addr) @@ -45,7 +45,7 @@ bool bookmarkdel(uint addr) return false; EXCLUSIVE_ACQUIRE(LockBookmarks); - return (bookmarks.erase(modhashfromva(addr)) > 0); + return (bookmarks.erase(ModHashFromAddr(addr)) > 0); } void bookmarkdelrange(uint start, uint end) @@ -63,9 +63,9 @@ void bookmarkdelrange(uint start, uint end) else { // Make sure 'start' and 'end' reference the same module - uint modbase = modbasefromaddr(start); + uint modbase = ModBaseFromAddr(start); - if(modbase != modbasefromaddr(end)) + if(modbase != ModBaseFromAddr(end)) return; start -= modbase; @@ -144,7 +144,7 @@ void bookmarkcacheload(JSON root) bookmarkInfo.addr = (uint)json_hex_value(json_object_get(value, "address")); bookmarkInfo.manual = Manual; - const uint key = modhashfromname(bookmarkInfo.mod) + bookmarkInfo.addr; + const uint key = ModHashFromName(bookmarkInfo.mod) + bookmarkInfo.addr; bookmarks.insert(std::make_pair(key, bookmarkInfo)); } }; @@ -183,7 +183,7 @@ bool bookmarkenum(BOOKMARKSINFO* bookmarklist, size_t* cbsize) for(auto itr = bookmarks.begin(); itr != bookmarks.end(); itr++, bookmarklist++) { *bookmarklist = itr->second; - bookmarklist->addr += modbasefromname(bookmarklist->mod); + bookmarklist->addr += ModBaseFromName(bookmarklist->mod); } return true; diff --git a/x64_dbg_dbg/breakpoint.cpp b/x64_dbg_dbg/breakpoint.cpp index f673d0f1..bc9bcd56 100644 --- a/x64_dbg_dbg/breakpoint.cpp +++ b/x64_dbg_dbg/breakpoint.cpp @@ -21,7 +21,7 @@ int bpgetlist(std::vector* list) for(BreakpointsInfo::iterator i = breakpoints.begin(); i != breakpoints.end(); ++i) { curBp = i->second; - curBp.addr += modbasefromname(curBp.mod); + curBp.addr += ModBaseFromName(curBp.mod); curBp.active = memisvalidreadptr(fdProcessInfo->hProcess, curBp.addr); count++; if(list) @@ -35,8 +35,8 @@ bool bpnew(uint addr, bool enabled, bool singleshoot, short oldbytes, BP_TYPE ty if(!DbgIsDebugging() or !memisvalidreadptr(fdProcessInfo->hProcess, addr) or bpget(addr, type, name, 0)) return false; BREAKPOINT bp; - modnamefromaddr(addr, bp.mod, true); - uint modbase = modbasefromaddr(addr); + ModNameFromAddr(addr, bp.mod, true); + uint modbase = ModBaseFromAddr(addr); bp.active = true; bp.addr = addr - modbase; bp.enabled = enabled; @@ -49,7 +49,7 @@ bool bpnew(uint addr, bool enabled, bool singleshoot, short oldbytes, BP_TYPE ty bp.titantype = titantype; bp.type = type; CriticalSectionLocker locker(LockBreakpoints); - breakpoints.insert(std::make_pair(BreakpointKey(type, modhashfromva(addr)), bp)); + breakpoints.insert(std::make_pair(BreakpointKey(type, ModHashFromAddr(addr)), bp)); return true; } @@ -61,13 +61,13 @@ bool bpget(uint addr, BP_TYPE type, const char* name, BREAKPOINT* bp) CriticalSectionLocker locker(LockBreakpoints); if(!name) { - BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, modhashfromva(addr))); + BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, ModHashFromAddr(addr))); if(found == breakpoints.end()) //not found return false; if(!bp) return true; curBp = found->second; - curBp.addr += modbasefromaddr(addr); + curBp.addr += ModBaseFromAddr(addr); curBp.active = memisvalidreadptr(fdProcessInfo->hProcess, curBp.addr); *bp = curBp; return true; @@ -81,7 +81,7 @@ bool bpget(uint addr, BP_TYPE type, const char* name, BREAKPOINT* bp) { if(bp) { - curBp.addr += modbasefromname(curBp.mod); + curBp.addr += ModBaseFromName(curBp.mod); curBp.active = memisvalidreadptr(fdProcessInfo->hProcess, curBp.addr); *bp = curBp; } @@ -97,7 +97,7 @@ bool bpdel(uint addr, BP_TYPE type) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockBreakpoints); - return (breakpoints.erase(BreakpointKey(type, modhashfromva(addr))) > 0); + return (breakpoints.erase(BreakpointKey(type, ModHashFromAddr(addr))) > 0); } bool bpenable(uint addr, BP_TYPE type, bool enable) @@ -105,7 +105,7 @@ bool bpenable(uint addr, BP_TYPE type, bool enable) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockBreakpoints); - BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, modhashfromva(addr))); + BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, ModHashFromAddr(addr))); if(found == breakpoints.end()) //not found return false; breakpoints[found->first].enabled = enable; @@ -117,7 +117,7 @@ bool bpsetname(uint addr, BP_TYPE type, const char* name) if(!DbgIsDebugging() or !name or !*name) return false; CriticalSectionLocker locker(LockBreakpoints); - BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, modhashfromva(addr))); + BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, ModHashFromAddr(addr))); if(found == breakpoints.end()) //not found return false; strcpy_s(breakpoints[found->first].name, name); @@ -129,7 +129,7 @@ bool bpsettitantype(uint addr, BP_TYPE type, int titantype) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockBreakpoints); - BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, modhashfromva(addr))); + BreakpointsInfo::iterator found = breakpoints.find(BreakpointKey(type, ModHashFromAddr(addr))); if(found == breakpoints.end()) //not found return false; breakpoints[found->first].titantype = titantype; @@ -149,7 +149,7 @@ bool bpenumall(BPENUMCALLBACK cbEnum, const char* module) BreakpointsInfo::iterator j = i; ++i; curBp = j->second; - curBp.addr += modbasefromname(curBp.mod); //RVA to VA + curBp.addr += ModBaseFromName(curBp.mod); //RVA to VA curBp.active = memisvalidreadptr(fdProcessInfo->hProcess, curBp.addr); //TODO: wtf am I doing? if(module and * module) { @@ -263,7 +263,7 @@ void bpcacheload(JSON root) const char* mod = json_string_value(json_object_get(value, "module")); if(mod && *mod && strlen(mod) < MAX_MODULE_SIZE) strcpy_s(curBreakpoint.mod, mod); - const uint key = modhashfromname(curBreakpoint.mod) + curBreakpoint.addr; + const uint key = ModHashFromName(curBreakpoint.mod) + curBreakpoint.addr; breakpoints.insert(std::make_pair(BreakpointKey(curBreakpoint.type, key), curBreakpoint)); } } diff --git a/x64_dbg_dbg/comment.cpp b/x64_dbg_dbg/comment.cpp index ab9c1db8..a42b5940 100644 --- a/x64_dbg_dbg/comment.cpp +++ b/x64_dbg_dbg/comment.cpp @@ -20,9 +20,9 @@ bool commentset(uint addr, const char* text, bool manual) COMMENTSINFO comment; comment.manual = manual; strcpy_s(comment.text, text); - modnamefromaddr(addr, comment.mod, true); - comment.addr = addr - modbasefromaddr(addr); - const uint key = modhashfromva(addr); + ModNameFromAddr(addr, comment.mod, true); + comment.addr = addr - ModBaseFromAddr(addr); + const uint key = ModHashFromAddr(addr); CriticalSectionLocker locker(LockComments); if(!comments.insert(std::make_pair(key, comment)).second) //key already present comments[key] = comment; @@ -34,7 +34,7 @@ bool commentget(uint addr, char* text) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockComments); - const CommentsInfo::iterator found = comments.find(modhashfromva(addr)); + const CommentsInfo::iterator found = comments.find(ModHashFromAddr(addr)); if(found == comments.end()) //not found return false; strcpy_s(text, MAX_COMMENT_SIZE, found->second.text); @@ -46,7 +46,7 @@ bool commentdel(uint addr) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockComments); - return (comments.erase(modhashfromva(addr)) == 1); + return (comments.erase(ModHashFromAddr(addr)) == 1); } void commentdelrange(uint start, uint end) @@ -54,8 +54,8 @@ void commentdelrange(uint start, uint end) if(!DbgIsDebugging()) return; bool bDelAll = (start == 0 && end == ~0); //0x00000000-0xFFFFFFFF - uint modbase = modbasefromaddr(start); - if(modbase != modbasefromaddr(end)) + uint modbase = ModBaseFromAddr(start); + if(modbase != ModBaseFromAddr(end)) return; start -= modbase; end -= modbase; @@ -124,7 +124,7 @@ void commentcacheload(JSON root) strcpy_s(curComment.text, text); else continue; //skip - const uint key = modhashfromname(curComment.mod) + curComment.addr; + const uint key = ModHashFromName(curComment.mod) + curComment.addr; comments.insert(std::make_pair(key, curComment)); } } @@ -148,7 +148,7 @@ void commentcacheload(JSON root) strcpy_s(curComment.text, text); else continue; //skip - const uint key = modhashfromname(curComment.mod) + curComment.addr; + const uint key = ModHashFromName(curComment.mod) + curComment.addr; comments.insert(std::make_pair(key, curComment)); } } @@ -170,7 +170,7 @@ bool commentenum(COMMENTSINFO* commentlist, size_t* cbsize) for(CommentsInfo::iterator i = comments.begin(); i != comments.end(); ++i, j++) { commentlist[j] = i->second; - commentlist[j].addr += modbasefromname(commentlist[j].mod); + commentlist[j].addr += ModBaseFromName(commentlist[j].mod); } return true; } diff --git a/x64_dbg_dbg/debugger.cpp b/x64_dbg_dbg/debugger.cpp index 4c744b4d..d2e59f15 100644 --- a/x64_dbg_dbg/debugger.cpp +++ b/x64_dbg_dbg/debugger.cpp @@ -198,7 +198,7 @@ void DebugUpdateGui(uint disasm_addr, bool stack) } char modname[MAX_MODULE_SIZE] = ""; char modtext[MAX_MODULE_SIZE * 2] = ""; - if(!modnamefromaddr(disasm_addr, modname, true)) + if(!ModNameFromAddr(disasm_addr, modname, true)) *modname = 0; else sprintf(modtext, "Module: %s - ", modname); @@ -637,7 +637,7 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo) dbggetprivateusage(fdProcessInfo->hProcess, true); memupdatemap(fdProcessInfo->hProcess); //update memory map char modname[256] = ""; - if(modnamefromaddr((uint)base, modname, true)) + if(ModNameFromAddr((uint)base, modname, true)) bpenumall(cbSetModuleBreakpoints, modname); GuiUpdateBreakpointsView(); if(!bFileIsDll and !bIsAttached) //Set entry breakpoint @@ -688,7 +688,7 @@ static void cbCreateProcess(CREATE_PROCESS_DEBUG_INFO* CreateProcessInfo) threadInfo.hThread = CreateProcessInfo->hThread; threadInfo.lpStartAddress = CreateProcessInfo->lpStartAddress; threadInfo.lpThreadLocalBase = CreateProcessInfo->lpThreadLocalBase; - threadcreate(&threadInfo); + ThreadCreate(&threadInfo); } static void cbExitProcess(EXIT_PROCESS_DEBUG_INFO* ExitProcess) @@ -702,7 +702,7 @@ static void cbExitProcess(EXIT_PROCESS_DEBUG_INFO* ExitProcess) static void cbCreateThread(CREATE_THREAD_DEBUG_INFO* CreateThread) { - threadcreate(CreateThread); //update thread list + ThreadCreate(CreateThread); //update thread list DWORD dwThreadId = ((DEBUG_EVENT*)GetDebugData())->dwThreadId; hActiveThread = ThreadGetHandle(dwThreadId); @@ -745,7 +745,7 @@ static void cbExitThread(EXIT_THREAD_DEBUG_INFO* ExitThread) callbackInfo.ExitThread = ExitThread; callbackInfo.dwThreadId = dwThreadId; plugincbcall(CB_EXITTHREAD, &callbackInfo); - threadexit(dwThreadId); + ThreadExit(dwThreadId); dprintf("Thread %X exit\n", dwThreadId); if(settingboolget("Events", "ThreadEnd")) @@ -817,7 +817,7 @@ static void cbLoadDll(LOAD_DLL_DEBUG_INFO* LoadDll) dbggetprivateusage(fdProcessInfo->hProcess, true); memupdatemap(fdProcessInfo->hProcess); //update memory map char modname[256] = ""; - if(modnamefromaddr((uint)base, modname, true)) + if(ModNameFromAddr((uint)base, modname, true)) bpenumall(cbSetModuleBreakpoints, modname); GuiUpdateBreakpointsView(); bool bAlreadySetEntry = false; @@ -907,7 +907,7 @@ static void cbUnloadDll(UNLOAD_DLL_DEBUG_INFO* UnloadDll) void* base = UnloadDll->lpBaseOfDll; char modname[256] = "???"; - if(modnamefromaddr((uint)base, modname, true)) + if(ModNameFromAddr((uint)base, modname, true)) bpenumall(cbRemoveModuleBreakpoints, modname); GuiUpdateBreakpointsView(); SymUnloadModule64(fdProcessInfo->hProcess, (DWORD64)base); @@ -928,7 +928,7 @@ static void cbUnloadDll(UNLOAD_DLL_DEBUG_INFO* UnloadDll) wait(WAITID_RUN); } - modunload((uint)base); + ModUnload((uint)base); } static void cbOutputDebugString(OUTPUT_DEBUG_STRING_INFO* DebugString) @@ -1151,8 +1151,8 @@ DWORD WINAPI threadDebugLoop(void* lpParameter) RemoveAllBreakPoints(UE_OPTION_REMOVEALL); //remove all breakpoints //cleanup dbclose(); - modclear(); - threadclear(); + ModClear(); + ThreadClear(); GuiSetDebugState(stopped); dputs("debugging stopped!"); varset("$hp", (uint)0, true); @@ -1367,8 +1367,8 @@ DWORD WINAPI threadAttachLoop(void* lpParameter) RemoveAllBreakPoints(UE_OPTION_REMOVEALL); //remove all breakpoints //cleanup dbclose(); - modclear(); - threadclear(); + ModClear(); + ThreadClear(); GuiSetDebugState(stopped); dputs("debugging stopped!"); varset("$hp", (uint)0, true); diff --git a/x64_dbg_dbg/debugger_commands.cpp b/x64_dbg_dbg/debugger_commands.cpp index ed586f34..890df723 100644 --- a/x64_dbg_dbg/debugger_commands.cpp +++ b/x64_dbg_dbg/debugger_commands.cpp @@ -1119,18 +1119,16 @@ CMDRESULT cbDebugKillthread(int argc, char* argv[]) CMDRESULT cbDebugSuspendAllThreads(int argc, char* argv[]) { - int threadCount = ThreadGetCount(); - int suspendedCount = ThreadSuspendAll(); - dprintf("%d/%d thread(s) suspended\n", suspendedCount, threadCount); + dprintf("%d/%d thread(s) suspended\n", ThreadSuspendAll(), ThreadGetCount()); + GuiUpdateAllViews(); return STATUS_CONTINUE; } CMDRESULT cbDebugResumeAllThreads(int argc, char* argv[]) { - int threadCount = ThreadGetCount(); - int resumeCount = ThreadResumeAll(); - dprintf("%d/%d thread(s) resumed\n", resumeCount, threadCount); + dprintf("%d/%d thread(s) resumed\n", ThreadResumeAll(), ThreadGetCount()); + GuiUpdateAllViews(); return STATUS_CONTINUE; } @@ -1390,7 +1388,7 @@ CMDRESULT cbDebugDownloadSymbol(int argc, char* argv[]) return STATUS_CONTINUE; } //get some module information - uint modbase = modbasefromname(argv[1]); + uint modbase = ModBaseFromName(argv[1]); if(!modbase) { dprintf("Invalid module \"%s\"!\n", argv[1]); diff --git a/x64_dbg_dbg/function.cpp b/x64_dbg_dbg/function.cpp index cc5406d6..e24c01e5 100644 --- a/x64_dbg_dbg/function.cpp +++ b/x64_dbg_dbg/function.cpp @@ -12,18 +12,18 @@ bool functionadd(uint start, uint end, bool manual) { if(!DbgIsDebugging() or end < start or !memisvalidreadptr(fdProcessInfo->hProcess, start)) return false; - const uint modbase = modbasefromaddr(start); - if(modbase != modbasefromaddr(end)) //the function boundaries are not in the same module + const uint modbase = ModBaseFromAddr(start); + if(modbase != ModBaseFromAddr(end)) //the function boundaries are not in the same module return false; if(functionoverlaps(start, end)) return false; FUNCTIONSINFO function; - modnamefromaddr(start, function.mod, true); + ModNameFromAddr(start, function.mod, true); function.start = start - modbase; function.end = end - modbase; function.manual = manual; CriticalSectionLocker locker(LockFunctions); - functions.insert(std::make_pair(ModuleRange(modhashfromva(modbase), Range(function.start, function.end)), function)); + functions.insert(std::make_pair(ModuleRange(ModHashFromAddr(modbase), Range(function.start, function.end)), function)); return true; } @@ -31,9 +31,9 @@ bool functionget(uint addr, uint* start, uint* end) { if(!DbgIsDebugging()) return false; - uint modbase = modbasefromaddr(addr); + uint modbase = ModBaseFromAddr(addr); CriticalSectionLocker locker(LockFunctions); - const FunctionsInfo::iterator found = functions.find(ModuleRange(modhashfromva(modbase), Range(addr - modbase, addr - modbase))); + const FunctionsInfo::iterator found = functions.find(ModuleRange(ModHashFromAddr(modbase), Range(addr - modbase, addr - modbase))); if(found == functions.end()) //not found return false; if(start) @@ -47,18 +47,18 @@ bool functionoverlaps(uint start, uint end) { if(!DbgIsDebugging() or end < start) return false; - const uint modbase = modbasefromaddr(start); + const uint modbase = ModBaseFromAddr(start); CriticalSectionLocker locker(LockFunctions); - return (functions.count(ModuleRange(modhashfromva(modbase), Range(start - modbase, end - modbase))) > 0); + return (functions.count(ModuleRange(ModHashFromAddr(modbase), Range(start - modbase, end - modbase))) > 0); } bool functiondel(uint addr) { if(!DbgIsDebugging()) return false; - const uint modbase = modbasefromaddr(addr); + const uint modbase = ModBaseFromAddr(addr); CriticalSectionLocker locker(LockFunctions); - return (functions.erase(ModuleRange(modhashfromva(modbase), Range(addr - modbase, addr - modbase))) > 0); + return (functions.erase(ModuleRange(ModHashFromAddr(modbase), Range(addr - modbase, addr - modbase))) > 0); } void functiondelrange(uint start, uint end) @@ -66,8 +66,8 @@ void functiondelrange(uint start, uint end) if(!DbgIsDebugging()) return; bool bDelAll = (start == 0 && end == ~0); //0x00000000-0xFFFFFFFF - uint modbase = modbasefromaddr(start); - if(modbase != modbasefromaddr(end)) + uint modbase = ModBaseFromAddr(start); + if(modbase != ModBaseFromAddr(end)) return; start -= modbase; end -= modbase; @@ -134,8 +134,8 @@ void functioncacheload(JSON root) if(curFunction.end < curFunction.start) continue; //invalid function curFunction.manual = true; - const uint key = modhashfromname(curFunction.mod); - functions.insert(std::make_pair(ModuleRange(modhashfromname(curFunction.mod), Range(curFunction.start, curFunction.end)), curFunction)); + const uint key = ModHashFromName(curFunction.mod); + functions.insert(std::make_pair(ModuleRange(ModHashFromName(curFunction.mod), Range(curFunction.start, curFunction.end)), curFunction)); } } JSON jsonautofunctions = json_object_get(root, "autofunctions"); @@ -156,8 +156,8 @@ void functioncacheload(JSON root) if(curFunction.end < curFunction.start) continue; //invalid function curFunction.manual = true; - const uint key = modhashfromname(curFunction.mod); - functions.insert(std::make_pair(ModuleRange(modhashfromname(curFunction.mod), Range(curFunction.start, curFunction.end)), curFunction)); + const uint key = ModHashFromName(curFunction.mod); + functions.insert(std::make_pair(ModuleRange(ModHashFromName(curFunction.mod), Range(curFunction.start, curFunction.end)), curFunction)); } } } @@ -178,7 +178,7 @@ bool functionenum(FUNCTIONSINFO* functionlist, size_t* cbsize) for(FunctionsInfo::iterator i = functions.begin(); i != functions.end(); ++i, j++) { functionlist[j] = i->second; - uint modbase = modbasefromname(functionlist[j].mod); + uint modbase = ModBaseFromName(functionlist[j].mod); functionlist[j].start += modbase; functionlist[j].end += modbase; } diff --git a/x64_dbg_dbg/label.cpp b/x64_dbg_dbg/label.cpp index 9421ef7f..7f6ad248 100644 --- a/x64_dbg_dbg/label.cpp +++ b/x64_dbg_dbg/label.cpp @@ -20,11 +20,11 @@ bool labelset(uint addr, const char* text, bool manual) LABELSINFO label; label.manual = manual; strcpy_s(label.text, text); - modnamefromaddr(addr, label.mod, true); - label.addr = addr - modbasefromaddr(addr); - uint key = modhashfromva(addr); + ModNameFromAddr(addr, label.mod, true); + label.addr = addr - ModBaseFromAddr(addr); + uint key = ModHashFromAddr(addr); CriticalSectionLocker locker(LockLabels); - if(!labels.insert(std::make_pair(modhashfromva(key), label)).second) //already present + if(!labels.insert(std::make_pair(ModHashFromAddr(key), label)).second) //already present labels[key] = label; return true; } @@ -39,7 +39,7 @@ bool labelfromstring(const char* text, uint* addr) if(!strcmp(i->second.text, text)) { if(addr) - *addr = i->second.addr + modbasefromname(i->second.mod); + *addr = i->second.addr + ModBaseFromName(i->second.mod); return true; } } @@ -51,7 +51,7 @@ bool labelget(uint addr, char* text) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockLabels); - const LabelsInfo::iterator found = labels.find(modhashfromva(addr)); + const LabelsInfo::iterator found = labels.find(ModHashFromAddr(addr)); if(found == labels.end()) //not found return false; if(text) @@ -64,7 +64,7 @@ bool labeldel(uint addr) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockLabels); - return (labels.erase(modhashfromva(addr)) > 0); + return (labels.erase(ModHashFromAddr(addr)) > 0); } void labeldelrange(uint start, uint end) @@ -72,8 +72,8 @@ void labeldelrange(uint start, uint end) if(!DbgIsDebugging()) return; bool bDelAll = (start == 0 && end == ~0); //0x00000000-0xFFFFFFFF - uint modbase = modbasefromaddr(start); - if(modbase != modbasefromaddr(end)) + uint modbase = ModBaseFromAddr(start); + if(modbase != ModBaseFromAddr(end)) return; start -= modbase; end -= modbase; @@ -146,7 +146,7 @@ void labelcacheload(JSON root) for(int i = 0; i < len; i++) if(curLabel.text[i] == '&') curLabel.text[i] = ' '; - const uint key = modhashfromname(curLabel.mod) + curLabel.addr; + const uint key = ModHashFromName(curLabel.mod) + curLabel.addr; labels.insert(std::make_pair(key, curLabel)); } } @@ -170,7 +170,7 @@ void labelcacheload(JSON root) strcpy_s(curLabel.text, text); else continue; //skip - const uint key = modhashfromname(curLabel.mod) + curLabel.addr; + const uint key = ModHashFromName(curLabel.mod) + curLabel.addr; labels.insert(std::make_pair(key, curLabel)); } } @@ -192,7 +192,7 @@ bool labelenum(LABELSINFO* labellist, size_t* cbsize) for(LabelsInfo::iterator i = labels.begin(); i != labels.end(); ++i, j++) { labellist[j] = i->second; - labellist[j].addr += modbasefromname(labellist[j].mod); + labellist[j].addr += ModBaseFromName(labellist[j].mod); } return true; } diff --git a/x64_dbg_dbg/loop.cpp b/x64_dbg_dbg/loop.cpp index a390513d..584c3403 100644 --- a/x64_dbg_dbg/loop.cpp +++ b/x64_dbg_dbg/loop.cpp @@ -12,14 +12,14 @@ bool loopadd(uint start, uint end, bool manual) { if(!DbgIsDebugging() or end < start or !memisvalidreadptr(fdProcessInfo->hProcess, start)) return false; - const uint modbase = modbasefromaddr(start); - if(modbase != modbasefromaddr(end)) //the function boundaries are not in the same mem page + const uint modbase = ModBaseFromAddr(start); + if(modbase != ModBaseFromAddr(end)) //the function boundaries are not in the same mem page return false; int finaldepth; if(loopoverlaps(0, start, end, &finaldepth)) //loop cannot overlap another loop return false; LOOPSINFO loop; - modnamefromaddr(start, loop.mod, true); + ModNameFromAddr(start, loop.mod, true); loop.start = start - modbase; loop.end = end - modbase; loop.depth = finaldepth; @@ -29,7 +29,7 @@ bool loopadd(uint start, uint end, bool manual) loop.parent = 0; loop.manual = manual; CriticalSectionLocker locker(LockLoops); - loops.insert(std::make_pair(DepthModuleRange(finaldepth, ModuleRange(modhashfromva(modbase), Range(loop.start, loop.end))), loop)); + loops.insert(std::make_pair(DepthModuleRange(finaldepth, ModuleRange(ModHashFromAddr(modbase), Range(loop.start, loop.end))), loop)); return true; } @@ -38,9 +38,9 @@ bool loopget(int depth, uint addr, uint* start, uint* end) { if(!DbgIsDebugging()) return false; - const uint modbase = modbasefromaddr(addr); + const uint modbase = ModBaseFromAddr(addr); CriticalSectionLocker locker(LockLoops); - LoopsInfo::iterator found = loops.find(DepthModuleRange(depth, ModuleRange(modhashfromva(modbase), Range(addr - modbase, addr - modbase)))); + LoopsInfo::iterator found = loops.find(DepthModuleRange(depth, ModuleRange(ModHashFromAddr(modbase), Range(addr - modbase, addr - modbase)))); if(found == loops.end()) //not found return false; if(start) @@ -56,10 +56,10 @@ bool loopoverlaps(int depth, uint start, uint end, int* finaldepth) if(!DbgIsDebugging()) return false; - const uint modbase = modbasefromaddr(start); + const uint modbase = ModBaseFromAddr(start); uint curStart = start - modbase; uint curEnd = end - modbase; - const uint key = modhashfromva(modbase); + const uint key = ModHashFromAddr(modbase); CriticalSectionLocker locker(LockLoops); @@ -145,7 +145,7 @@ void loopcacheload(JSON root) if(curLoop.end < curLoop.start) continue; //invalid loop curLoop.manual = true; - loops.insert(std::make_pair(DepthModuleRange(curLoop.depth, ModuleRange(modhashfromname(curLoop.mod), Range(curLoop.start, curLoop.end))), curLoop)); + loops.insert(std::make_pair(DepthModuleRange(curLoop.depth, ModuleRange(ModHashFromName(curLoop.mod), Range(curLoop.start, curLoop.end))), curLoop)); } } JSON jsonautoloops = json_object_get(root, "autoloops"); @@ -168,36 +168,46 @@ void loopcacheload(JSON root) if(curLoop.end < curLoop.start) continue; //invalid loop curLoop.manual = false; - loops.insert(std::make_pair(DepthModuleRange(curLoop.depth, ModuleRange(modhashfromname(curLoop.mod), Range(curLoop.start, curLoop.end))), curLoop)); + loops.insert(std::make_pair(DepthModuleRange(curLoop.depth, ModuleRange(ModHashFromName(curLoop.mod), Range(curLoop.start, curLoop.end))), curLoop)); } } } bool loopenum(LOOPSINFO* looplist, size_t* cbsize) { - if(!DbgIsDebugging()) - return false; + // If looplist or size is not requested, fail if(!looplist && !cbsize) return false; - CriticalSectionLocker locker(LockLoops); - if(!looplist && cbsize) + + SHARED_ACQUIRE(LockLoops); + + // See if the caller requested an output size + if(cbsize) { *cbsize = loops.size() * sizeof(LOOPSINFO); - return true; + + if(!looplist) + return true; } - int j = 0; - for(LoopsInfo::iterator i = loops.begin(); i != loops.end(); ++i, j++) + + for(auto itr = loops.begin(); itr != loops.end(); itr++) { - looplist[j] = i->second; - uint modbase = modbasefromname(looplist[j].mod); - looplist[j].start += modbase; - looplist[j].end += modbase; + *looplist = itr->second; + + // Adjust the offset to a real virtual address + uint modbase = ModBaseFromName(looplist->mod); + looplist->start += modbase; + looplist->end += modbase; + + looplist++; } + return true; } void loopclear() { - CriticalSectionLocker locker(LockLoops); - LoopsInfo().swap(loops); + EXCLUSIVE_ACQUIRE(LockLoops); + + loops.clear(); } \ No newline at end of file diff --git a/x64_dbg_dbg/memory.cpp b/x64_dbg_dbg/memory.cpp index 03074db3..5bdde6c7 100644 --- a/x64_dbg_dbg/memory.cpp +++ b/x64_dbg_dbg/memory.cpp @@ -27,7 +27,7 @@ void memupdatemap(HANDLE hProcess) curAllocationBase = (uint)mbi.AllocationBase; MEMPAGE curPage; *curPage.info = 0; - modnamefromaddr(MyAddress, curPage.info, true); + ModNameFromAddr(MyAddress, curPage.info, true); memcpy(&curPage.mbi, &mbi, sizeof(mbi)); pageVector.push_back(curPage); } @@ -50,11 +50,11 @@ void memupdatemap(HANDLE hProcess) if(!pageVector.at(i).info[0] || (scmp(curMod, pageVector.at(i).info) && !bListAllPages)) //there is a module continue; //skip non-modules strcpy(curMod, pageVector.at(i).info); - uint base = modbasefromname(pageVector.at(i).info); + uint base = ModBaseFromName(pageVector.at(i).info); if(!base) continue; std::vector sections; - if(!modsectionsfromaddr(base, §ions)) + if(!ModSectionsFromAddr(base, §ions)) continue; int SectionNumber = (int)sections.size(); if(!SectionNumber) //no sections = skip diff --git a/x64_dbg_dbg/module.cpp b/x64_dbg_dbg/module.cpp index 91e82c0f..71adf766 100644 --- a/x64_dbg_dbg/module.cpp +++ b/x64_dbg_dbg/module.cpp @@ -6,155 +6,186 @@ static ModulesInfo modinfo; -///module functions -bool modload(uint base, uint size, const char* fullpath) +bool ModLoad(uint base, uint size, const char* fullpath) { + // + // Handle a new module being loaded + // + // TODO: Do loaded modules always require a path? if(!base or !size or !fullpath) return false; - char name[deflen] = ""; - int len = (int)strlen(fullpath); - while(fullpath[len] != '\\' and len) - len--; - if(len) - len++; - strcpy_s(name, fullpath + len); - _strlwr(name); - len = (int)strlen(name); - name[MAX_MODULE_SIZE - 1] = 0; //ignore later characters - while(name[len] != '.' and len) - len--; MODINFO info; - memset(&info, 0, sizeof(MODINFO)); - info.sections.clear(); - info.hash = modhashfromname(name); - if(len) + + // + // Break the module path into a directory and file name + // + char dir[deflen]; + char* file; + + GetFullPathNameA(fullpath, deflen, dir, &file); + + // Make everything lowercase + _strlwr(dir); + + // Copy the extension into the module struct { - strcpy_s(info.extension, name + len); - name[len] = 0; //remove extension + char* extensionPos = strrchr(file, '.'); + + if(extensionPos) + { + extensionPos[0] = '\0'; + strcpy_s(info.extension, extensionPos + 1); + } } + + // Copy the name to the module struct + strcpy_s(info.name, file); + + // + // Module base address/size/hash index + // + info.hash = ModHashFromName(info.name); info.base = base; info.size = size; - strcpy_s(info.name, name); - //process module sections - HANDLE FileHandle; - DWORD LoadedSize; - HANDLE FileMap; - ULONG_PTR FileMapVA; + // + // Process module sections + // + info.sections.clear(); + WString wszFullPath = StringUtils::Utf8ToUtf16(fullpath); - if(StaticFileLoadW(wszFullPath.c_str(), UE_ACCESS_READ, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA)) + if(StaticFileLoadW(wszFullPath.c_str(), UE_ACCESS_READ, false, &info.Handle, &info.FileMapSize, &info.MapHandle, &info.FileMapVA)) { - info.entry = GetPE32DataFromMappedFile(FileMapVA, 0, UE_OEP) + info.base; //get entry point - int SectionCount = (int)GetPE32DataFromMappedFile(FileMapVA, 0, UE_SECTIONNUMBER); - if(SectionCount > 0) + // Get the entry point + info.entry = GetPE32DataFromMappedFile(info.FileMapVA, 0, UE_OEP) + info.base; + + int sectionCount = (int)GetPE32DataFromMappedFile(info.FileMapVA, 0, UE_SECTIONNUMBER); + for(int i = 0; i < sectionCount; i++) { - for(int i = 0; i < SectionCount; i++) - { - MODSECTIONINFO curSection; - curSection.addr = GetPE32DataFromMappedFile(FileMapVA, i, UE_SECTIONVIRTUALOFFSET) + base; - curSection.size = GetPE32DataFromMappedFile(FileMapVA, i, UE_SECTIONVIRTUALSIZE); - const char* SectionName = (const char*)GetPE32DataFromMappedFile(FileMapVA, i, UE_SECTIONNAME); - //escape section name when needed - int len = (int)strlen(SectionName); - int escape_count = 0; - for(int k = 0; k < len; k++) - if(SectionName[k] == '\\' or SectionName[k] == '\"' or !isprint(SectionName[k])) - escape_count++; - strcpy_s(curSection.name, StringUtils::Escape(SectionName).c_str()); - info.sections.push_back(curSection); - } + MODSECTIONINFO curSection; + + curSection.addr = GetPE32DataFromMappedFile(info.FileMapVA, i, UE_SECTIONVIRTUALOFFSET) + info.base; + curSection.size = GetPE32DataFromMappedFile(info.FileMapVA, i, UE_SECTIONVIRTUALSIZE); + const char* sectionName = (const char*)GetPE32DataFromMappedFile(info.FileMapVA, i, UE_SECTIONNAME); + + // Escape section name when needed + strcpy_s(curSection.name, StringUtils::Escape(sectionName).c_str()); + + // Add entry to the vector + info.sections.push_back(curSection); } - StaticFileUnloadW(wszFullPath.c_str(), false, FileHandle, LoadedSize, FileMap, FileMapVA); } - //add module to list - CriticalSectionLocker locker(LockModules); + // Add module to list + EXCLUSIVE_ACQUIRE(LockModules); modinfo.insert(std::make_pair(Range(base, base + size - 1), info)); + EXCLUSIVE_RELEASE(LockModules); + symupdatemodulelist(); return true; } -bool modunload(uint base) +bool ModUnload(uint base) { EXCLUSIVE_ACQUIRE(LockModules); - const ModulesInfo::iterator found = modinfo.find(Range(base, base)); - if(found == modinfo.end()) //not found + // Find the iterator index + const auto found = modinfo.find(Range(base, base)); + + if(found == modinfo.end()) return false; + // Remove it from the list modinfo.erase(found); + + // Unload everything from TitanEngine + StaticFileUnloadW(nullptr, false, found->second.Handle, found->second.FileMapSize, found->second.MapHandle, found->second.FileMapVA); + + // Update symbols symupdatemodulelist(); return true; } -void modclear() +void ModClear() { - EXCLUSIVE_ACQUIRE(LockModules); - // Remove all modules in the list + EXCLUSIVE_ACQUIRE(LockModules); modinfo.clear(); + EXCLUSIVE_RELEASE(LockModules); // Tell the symbol updater symupdatemodulelist(); } -bool modnamefromaddr(uint addr, char* modname, bool extension) +MODINFO* ModInfoFromAddr(uint addr) +{ + // + // NOTE: THIS DOES _NOT_ USE LOCKS + // + auto found = modinfo.find(Range(addr, addr)); + + // Was the module found with this address? + if(found == modinfo.end()) + return nullptr; + + return &found->second; +} + +bool ModNameFromAddr(uint addr, char* modname, bool extension) { if(!modname) return false; SHARED_ACQUIRE(LockModules); - // Was the module found with this address? - auto found = modinfo.find(Range(addr, addr)); + // Get a pointer to module information + auto module = ModInfoFromAddr(addr); - if(found == modinfo.end()) + if(!module) return false; // Zero buffer first memset(modname, 0, MAX_MODULE_SIZE); // Append the module path/name - strcat_s(modname, MAX_MODULE_SIZE, found->second.name); + strcat_s(modname, MAX_MODULE_SIZE, module->name); // Append the extension if(extension) - strcat_s(modname, MAX_MODULE_SIZE, found->second.extension); + strcat_s(modname, MAX_MODULE_SIZE, module->extension); return true; } -uint modbasefromaddr(uint addr) +uint ModBaseFromAddr(uint addr) { SHARED_ACQUIRE(LockModules); - // Was the module found with this address? - auto found = modinfo.find(Range(addr, addr)); + auto module = ModInfoFromAddr(addr); - if(found == modinfo.end()) + if(!module) return 0; - return found->second.base; + return module->base; } -uint modhashfromva(uint va) +uint ModHashFromAddr(uint addr) { // // Returns a unique hash from a virtual address // SHARED_ACQUIRE(LockModules); - // Was the module found with this address? - auto found = modinfo.find(Range(va, va)); + auto module = ModInfoFromAddr(addr); - if(found == modinfo.end()) - return va; + if(!module) + return addr; - return found->second.hash + (va - found->second.base); + return module->hash + (addr - module->base); } -uint modhashfromname(const char* mod) +uint ModHashFromName(const char* mod) { // // return MODINFO.hash (based on the name) @@ -165,7 +196,7 @@ uint modhashfromname(const char* mod) return murmurhash(mod, (int)strlen(mod)); } -uint modbasefromname(const char* modname) +uint ModBaseFromName(const char* modname) { if(!modname || strlen(modname) >= MAX_MODULE_SIZE) return 0; @@ -177,74 +208,64 @@ uint modbasefromname(const char* modname) char curmodname[MAX_MODULE_SIZE]; sprintf(curmodname, "%s%s", itr->second.name, itr->second.extension); - // Test with extension - if(!_stricmp(curmodname, modname)) - return itr->second.base; - - // Test without extension - if(!_stricmp(itr->second.name, modname)) + // Test with and without extension + if(!_stricmp(curmodname, modname) || !_stricmp(itr->second.name, modname)) return itr->second.base; } return 0; } -uint modsizefromaddr(uint addr) +uint ModSizeFromAddr(uint addr) { SHARED_ACQUIRE(LockModules); - // Was the module found with this address? - auto found = modinfo.find(Range(addr, addr)); + auto module = ModInfoFromAddr(addr); - if(found == modinfo.end()) + if(!module) return 0; - return found->second.size; + return module->size; } -bool modsectionsfromaddr(uint addr, std::vector* sections) +bool ModSectionsFromAddr(uint addr, std::vector* sections) { SHARED_ACQUIRE(LockModules); - // Was the module found with this address? - auto found = modinfo.find(Range(addr, addr)); + auto module = ModInfoFromAddr(addr); - if(found == modinfo.end()) + if(!module) return false; // Copy vector <-> vector - *sections = found->second.sections; + *sections = module->sections; return true; } -uint modentryfromaddr(uint addr) +uint ModEntryFromAddr(uint addr) { SHARED_ACQUIRE(LockModules); - // Was the module found with this address? - auto found = modinfo.find(Range(addr, addr)); + auto module = ModInfoFromAddr(addr); - if(found == modinfo.end()) //not found + if(!module) return 0; - return found->second.entry; + return module->entry; } -int modpathfromaddr(duint addr, char* path, int size) +int ModPathFromAddr(duint addr, char* path, int size) { - Memory wszModPath(size * sizeof(wchar_t), "modpathfromaddr:wszModPath"); + auto module = ModInfoFromAddr(addr); - if(!GetModuleFileNameExW(fdProcessInfo->hProcess, (HMODULE)modbasefromaddr(addr), wszModPath, size)) - { - *path = '\0'; + if(!module) return 0; - } - strcpy_s(path, size, StringUtils::Utf16ToUtf8(wszModPath()).c_str()); + strcpy_s(path, size, module->path); return (int)strlen(path); } -int modpathfromname(const char* modname, char* path, int size) +int ModPathFromName(const char* modname, char* path, int size) { - return modpathfromaddr(modbasefromname(modname), path, size); + return ModPathFromAddr(ModBaseFromName(modname), path, size); } \ No newline at end of file diff --git a/x64_dbg_dbg/module.h b/x64_dbg_dbg/module.h index 95edba18..c59cb97c 100644 --- a/x64_dbg_dbg/module.h +++ b/x64_dbg_dbg/module.h @@ -1,40 +1,47 @@ -#ifndef _MODULE_H -#define _MODULE_H +#pragma once #include "_global.h" #include "addrinfo.h" struct MODSECTIONINFO { - uint addr; //va - uint size; //virtual size - char name[50]; + uint addr; // Virtual address + uint size; // Virtual size + char name[50]; // Escaped section name }; struct MODINFO { - uint base; //module base - uint size; //module size - uint hash; //full module name hash - uint entry; //entry point - char name[MAX_MODULE_SIZE]; //module name (without extension) - char extension[MAX_MODULE_SIZE]; //file extension + uint base; // Module base + uint size; // Module size + uint hash; // Full module name hash + uint entry; // Entry point + + char name[MAX_MODULE_SIZE]; // Module name (without extension) + char extension[MAX_MODULE_SIZE]; // File extension + char path[MAX_PATH]; // File path (in UTF8) + + HANDLE Handle; // Handle to the file opened by TitanEngine + HANDLE MapHandle; // Handle to the memory map + ULONG_PTR FileMapVA; // File map virtual address (Debugger local) + DWORD FileMapSize; // File map virtual size + std::vector sections; }; + typedef std::map ModulesInfo; -bool modload(uint base, uint size, const char* fullpath); -bool modunload(uint base); -void modclear(); -bool modnamefromaddr(uint addr, char* modname, bool extension); -uint modbasefromaddr(uint addr); -uint modhashfromva(uint va); -uint modhashfromname(const char* mod); -uint modbasefromname(const char* modname); -uint modsizefromaddr(uint addr); -bool modsectionsfromaddr(uint addr, std::vector* sections); -uint modentryfromaddr(uint addr); -int modpathfromaddr(duint addr, char* path, int size); -int modpathfromname(const char* modname, char* path, int size); - -#endif //_MODULE_H \ No newline at end of file +bool ModLoad(uint base, uint size, const char* fullpath); +bool ModUnload(uint base); +void ModClear(); +MODINFO* ModInfoFromAddr(uint addr); +bool ModNameFromAddr(uint addr, char* modname, bool extension); +uint ModBaseFromAddr(uint addr); +uint ModHashFromAddr(uint addr); +uint ModHashFromName(const char* mod); +uint ModBaseFromName(const char* modname); +uint ModSizeFromAddr(uint addr); +bool ModSectionsFromAddr(uint addr, std::vector* sections); +uint ModEntryFromAddr(uint addr); +int ModPathFromAddr(duint addr, char* path, int size); +int ModPathFromName(const char* modname, char* path, int size); \ No newline at end of file diff --git a/x64_dbg_dbg/patches.cpp b/x64_dbg_dbg/patches.cpp index c567aec7..2b9f6ef5 100644 --- a/x64_dbg_dbg/patches.cpp +++ b/x64_dbg_dbg/patches.cpp @@ -15,11 +15,11 @@ bool patchset(uint addr, unsigned char oldbyte, unsigned char newbyte) if(oldbyte == newbyte) return true; //no need to make a patch for a byte that is equal to itself PATCHINFO newPatch; - newPatch.addr = addr - modbasefromaddr(addr); - modnamefromaddr(addr, newPatch.mod, true); + newPatch.addr = addr - ModBaseFromAddr(addr); + ModNameFromAddr(addr, newPatch.mod, true); newPatch.oldbyte = oldbyte; newPatch.newbyte = newbyte; - uint key = modhashfromva(addr); + uint key = ModHashFromAddr(addr); CriticalSectionLocker locker(LockPatches); PatchesInfo::iterator found = patches.find(key); if(found != patches.end()) //we found a patch on the specified address @@ -45,13 +45,13 @@ bool patchget(uint addr, PATCHINFO* patch) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockPatches); - PatchesInfo::iterator found = patches.find(modhashfromva(addr)); + PatchesInfo::iterator found = patches.find(ModHashFromAddr(addr)); if(found == patches.end()) //not found return false; if(patch) { *patch = found->second; - patch->addr += modbasefromaddr(addr); + patch->addr += ModBaseFromAddr(addr); return true; } return (found->second.oldbyte != found->second.newbyte); @@ -62,11 +62,11 @@ bool patchdel(uint addr, bool restore) if(!DbgIsDebugging()) return false; CriticalSectionLocker locker(LockPatches); - PatchesInfo::iterator found = patches.find(modhashfromva(addr)); + PatchesInfo::iterator found = patches.find(ModHashFromAddr(addr)); if(found == patches.end()) //not found return false; if(restore) - memwrite(fdProcessInfo->hProcess, (void*)(found->second.addr + modbasefromaddr(addr)), &found->second.oldbyte, sizeof(char), 0); + memwrite(fdProcessInfo->hProcess, (void*)(found->second.addr + ModBaseFromAddr(addr)), &found->second.oldbyte, sizeof(char), 0); patches.erase(found); return true; } @@ -76,8 +76,8 @@ void patchdelrange(uint start, uint end, bool restore) if(!DbgIsDebugging()) return; bool bDelAll = (start == 0 && end == ~0); //0x00000000-0xFFFFFFFF - uint modbase = modbasefromaddr(start); - if(modbase != modbasefromaddr(end)) + uint modbase = ModBaseFromAddr(start); + if(modbase != ModBaseFromAddr(end)) return; start -= modbase; end -= modbase; @@ -130,7 +130,7 @@ bool patchenum(PATCHINFO* patcheslist, size_t* cbsize) for(PatchesInfo::iterator i = patches.begin(); i != patches.end(); ++i, j++) { patcheslist[j] = i->second; - uint modbase = modbasefromname(patcheslist[j].mod); + uint modbase = ModBaseFromName(patcheslist[j].mod); patcheslist[j].addr += modbase; } return true; @@ -154,7 +154,7 @@ int patchfile(const PATCHINFO* patchlist, int count, const char* szFileName, cha sprintf(error, "not all patches are in module %s", modname); return -1; } - uint modbase = modbasefromname(modname); + uint modbase = ModBaseFromName(modname); if(!modbase) //module not loaded { if(error) diff --git a/x64_dbg_dbg/reference.cpp b/x64_dbg_dbg/reference.cpp index 0f7e3ccd..03254508 100644 --- a/x64_dbg_dbg/reference.cpp +++ b/x64_dbg_dbg/reference.cpp @@ -53,7 +53,7 @@ int reffind(uint addr, uint size, CBREF cbRef, void* userinfo, bool silent, cons refinfo.userinfo = userinfo; char fullName[deflen] = ""; char modname[MAX_MODULE_SIZE] = ""; - if(modnamefromaddr(start_addr, modname, true)) + if(ModNameFromAddr(start_addr, modname, true)) sprintf_s(fullName, "%s (%s)", name, modname); else sprintf_s(fullName, "%s (%p)", name, start_addr); diff --git a/x64_dbg_dbg/stackinfo.cpp b/x64_dbg_dbg/stackinfo.cpp index 89ebb514..9b92c83f 100644 --- a/x64_dbg_dbg/stackinfo.cpp +++ b/x64_dbg_dbg/stackinfo.cpp @@ -44,7 +44,7 @@ bool stackcommentget(uint addr, STACK_COMMENT* comment) if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); char module[MAX_MODULE_SIZE] = ""; - modnamefromaddr(data, module, false); + ModNameFromAddr(data, module, false); char returnToAddr[MAX_COMMENT_SIZE] = ""; if(*module) sprintf(returnToAddr, "%s.", module); @@ -60,7 +60,7 @@ bool stackcommentget(uint addr, STACK_COMMENT* comment) if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); *module = 0; - modnamefromaddr(data, module, false); + ModNameFromAddr(data, module, false); char returnFromAddr[MAX_COMMENT_SIZE] = ""; if(*module) sprintf(returnFromAddr, "%s.", module); @@ -94,7 +94,7 @@ bool stackcommentget(uint addr, STACK_COMMENT* comment) if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); char module[MAX_MODULE_SIZE] = ""; - modnamefromaddr(data, module, false); + ModNameFromAddr(data, module, false); char addrInfo[MAX_COMMENT_SIZE] = ""; if(*module) //module { @@ -153,7 +153,7 @@ void stackgetcallstack(uint csp, CALLSTACK* callstack) if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); char module[MAX_MODULE_SIZE] = ""; - modnamefromaddr(data, module, false); + ModNameFromAddr(data, module, false); char returnToAddr[MAX_COMMENT_SIZE] = ""; if(*module) sprintf(returnToAddr, "%s.", module); @@ -176,7 +176,7 @@ void stackgetcallstack(uint csp, CALLSTACK* callstack) if(_dbg_addrinfoget(data, SEG_DEFAULT, &addrinfo)) strcpy_s(label, addrinfo.label); *module = 0; - modnamefromaddr(data, module, false); + ModNameFromAddr(data, module, false); char returnFromAddr[MAX_COMMENT_SIZE] = ""; if(*module) sprintf(returnFromAddr, "%s.", module); diff --git a/x64_dbg_dbg/symbolinfo.cpp b/x64_dbg_dbg/symbolinfo.cpp index 37162335..07cb2e40 100644 --- a/x64_dbg_dbg/symbolinfo.cpp +++ b/x64_dbg_dbg/symbolinfo.cpp @@ -59,7 +59,7 @@ static BOOL CALLBACK EnumModules(LPCTSTR ModuleName, ULONG BaseOfDll, PVOID User SYMBOLMODULEINFO curModule; memset(&curModule, 0, sizeof(SYMBOLMODULEINFO)); curModule.base = BaseOfDll; - modnamefromaddr(BaseOfDll, curModule.name, true); + ModNameFromAddr(BaseOfDll, curModule.name, true); ((std::vector*)UserContext)->push_back(curModule); return TRUE; } @@ -166,7 +166,7 @@ const char* symgetsymbolicname(uint addr) if(retval) { char modname[MAX_MODULE_SIZE] = ""; - if(modnamefromaddr(addr, modname, false)) + if(ModNameFromAddr(addr, modname, false)) sprintf(symbolicname, "%s.%s", modname, label); else sprintf(symbolicname, "<%s>", label); diff --git a/x64_dbg_dbg/thread.cpp b/x64_dbg_dbg/thread.cpp index 149b3b04..4070c1cf 100644 --- a/x64_dbg_dbg/thread.cpp +++ b/x64_dbg_dbg/thread.cpp @@ -5,34 +5,32 @@ #include "threading.h" static std::vector threadList; -static int threadNum; -void threadcreate(CREATE_THREAD_DEBUG_INFO* CreateThread) +void ThreadCreate(CREATE_THREAD_DEBUG_INFO* CreateThread) { THREADINFO curInfo; memset(&curInfo, 0, sizeof(THREADINFO)); - curInfo.ThreadNumber = threadNum; + curInfo.ThreadNumber = ThreadGetCount(); curInfo.Handle = CreateThread->hThread; curInfo.ThreadId = ((DEBUG_EVENT*)GetDebugData())->dwThreadId; curInfo.ThreadStartAddress = (uint)CreateThread->lpStartAddress; curInfo.ThreadLocalBase = (uint)CreateThread->lpThreadLocalBase; // The first thread (#0) is always the main program thread - if(threadNum <= 0) + if(curInfo.ThreadNumber <= 0) strcpy_s(curInfo.threadName, "Main Thread"); // Modify global thread list EXCLUSIVE_ACQUIRE(LockThreads); threadList.push_back(curInfo); - threadNum++; EXCLUSIVE_RELEASE(); // Notify GUI GuiUpdateThreadView(); } -void threadexit(DWORD dwThreadId) +void ThreadExit(DWORD dwThreadId) { EXCLUSIVE_ACQUIRE(LockThreads); @@ -49,10 +47,8 @@ void threadexit(DWORD dwThreadId) GuiUpdateThreadView(); } -void threadclear() +void ThreadClear() { - threadNum = 0; - // Clear the current array of threads EXCLUSIVE_ACQUIRE(LockThreads); threadList.clear(); @@ -62,56 +58,9 @@ void threadclear() GuiUpdateThreadView(); } -bool ThreadGetTeb(uint TEBAddress, TEB* Teb) +int ThreadGetCount() { - // - // TODO: Keep a cached copy inside the vector - // - memset(Teb, 0, sizeof(TEB)); - - return memread(fdProcessInfo->hProcess, (void*)TEBAddress, Teb, sizeof(TEB), nullptr); -} - -int ThreadGetSuspendCount(HANDLE Thread) -{ - // - // Suspend a thread in order to get the previous suspension count - // WARNING: This function is very bad (threads should not be randomly interrupted) - // - int suspendCount = (int)SuspendThread(Thread); - - if(suspendCount == -1) - return 0; - - // Resume the thread's normal execution - ResumeThread(Thread); - - return suspendCount; -} - -THREADPRIORITY ThreadGetPriority(HANDLE Thread) -{ - return (THREADPRIORITY)GetThreadPriority(Thread); -} - -THREADWAITREASON ThreadGetWaitReason(HANDLE Thread) -{ - UNREFERENCED_PARAMETER(Thread); - - //TODO: Implement this - return _Executive; -} - -DWORD ThreadGetLastError(uint tebAddress) -{ - TEB teb; - if(!ThreadGetTeb(tebAddress, &teb)) - { - // TODO: Assert (Why would the TEB fail?) - return 0; - } - - return teb.LastErrorValue; + return (int)threadList.size(); } void ThreadGetList(THREADLIST* list) @@ -162,19 +111,72 @@ bool ThreadIsValid(DWORD dwThreadId) return false; } +bool ThreadGetTeb(uint TEBAddress, TEB* Teb) +{ + // + // TODO: Keep a cached copy inside the vector + // + memset(Teb, 0, sizeof(TEB)); + + return memread(fdProcessInfo->hProcess, (void*)TEBAddress, Teb, sizeof(TEB), nullptr); +} + +int ThreadGetSuspendCount(HANDLE Thread) +{ + // + // Suspend a thread in order to get the previous suspension count + // WARNING: This function is very bad (threads should not be randomly interrupted) + // + int suspendCount = (int)SuspendThread(Thread); + + if(suspendCount == -1) + return 0; + + // Resume the thread's normal execution + ResumeThread(Thread); + + return suspendCount; +} + +THREADPRIORITY ThreadGetPriority(HANDLE Thread) +{ + return (THREADPRIORITY)GetThreadPriority(Thread); +} + +THREADWAITREASON ThreadGetWaitReason(HANDLE Thread) +{ + UNREFERENCED_PARAMETER(Thread); + + // TODO: Implement this + return _Executive; +} + +DWORD ThreadGetLastError(uint tebAddress) +{ + TEB teb; + if(!ThreadGetTeb(tebAddress, &teb)) + { + // TODO: Assert (Why would the TEB fail?) + return 0; + } + + return teb.LastErrorValue; +} + bool ThreadSetName(DWORD dwThreadId, const char* name) { EXCLUSIVE_ACQUIRE(LockThreads); - // This modifies a variable (name), so an exclusive lock is required + // Modifies a variable (name), so an exclusive lock is required for(auto itr = threadList.begin(); itr != threadList.end(); itr++) { if(itr->ThreadId == dwThreadId) { - if(name) - strcpy_s(itr->threadName, name); - else - itr->threadName[0] = '\0'; + if(!name) + name = ""; + + strcpy_s(itr->threadName, name); + return true; } } @@ -208,20 +210,11 @@ DWORD ThreadGetId(HANDLE hThread) } // Wasn't found, check with Windows - // This also returns 0 on error - /* - REQUIRES VISTA+ + DWORD id = GetThreadId(hThread); - return GetThreadId(hThread); - */ - - // TODO: Same problem with threadgethandle() - return 0; -} - -int ThreadGetCount() -{ - return (int)threadList.size(); + // Returns 0 on error; + // TODO: Same problem with ThreadGetHandle() + return id; } int ThreadSuspendAll() @@ -246,7 +239,7 @@ int ThreadResumeAll() // // ResumeThread does not modify any internal variables // - EXCLUSIVE_ACQUIRE(LockThreads); + SHARED_ACQUIRE(LockThreads); int count = 0; for(auto itr = threadList.begin(); itr != threadList.end(); itr++) diff --git a/x64_dbg_dbg/thread.h b/x64_dbg_dbg/thread.h index 22276dc3..5eb62bd2 100644 --- a/x64_dbg_dbg/thread.h +++ b/x64_dbg_dbg/thread.h @@ -1,20 +1,22 @@ -#ifndef _THREAD_H -#define _THREAD_H +#pragma once #include "_global.h" #include "debugger.h" -//functions -void threadcreate(CREATE_THREAD_DEBUG_INFO* CreateThread); -void threadexit(DWORD dwThreadId); -void threadclear(); +void ThreadCreate(CREATE_THREAD_DEBUG_INFO* CreateThread); +void ThreadExit(DWORD dwThreadId); +void ThreadClear(); +int ThreadGetCount(); void ThreadGetList(THREADLIST* list); bool ThreadIsValid(DWORD dwThreadId); bool ThreadSetName(DWORD dwTHreadId, const char* name); +bool ThreadGetTeb(uint TEBAddress, TEB* Teb); +int ThreadGetSuspendCount(HANDLE Thread); +THREADPRIORITY ThreadGetPriority(HANDLE Thread); +THREADWAITREASON ThreadGetWaitReason(HANDLE Thread); +DWORD ThreadGetLastError(uint tebAddress); +bool ThreadSetName(DWORD dwThreadId, const char* name); HANDLE ThreadGetHandle(DWORD dwThreadId); DWORD ThreadGetId(HANDLE hThread); -int ThreadGetCount(); int ThreadSuspendAll(); -int ThreadResumeAll(); - -#endif //_THREAD_H +int ThreadResumeAll(); \ No newline at end of file diff --git a/x64_dbg_dbg/value.cpp b/x64_dbg_dbg/value.cpp index 055a8e41..000038bd 100644 --- a/x64_dbg_dbg/value.cpp +++ b/x64_dbg_dbg/value.cpp @@ -1189,7 +1189,7 @@ bool valapifromstring(const char* name, uint* value, int* value_size, bool print SELECTIONDATA seldata; memset(&seldata, 0, sizeof(seldata)); GuiSelectionGet(GUI_DISASSEMBLY, &seldata); - if(!modnamefromaddr(seldata.start, modname, true)) + if(!ModNameFromAddr(seldata.start, modname, true)) return false; } else @@ -1200,7 +1200,7 @@ bool valapifromstring(const char* name, uint* value, int* value_size, bool print apiname++; if(!strlen(apiname)) return false; - uint modbase = modbasefromname(modname); + uint modbase = ModBaseFromName(modname); wchar_t szModName[MAX_PATH] = L""; if(!GetModuleFileNameExW(fdProcessInfo->hProcess, (HMODULE)modbase, szModName, MAX_PATH)) { @@ -2094,7 +2094,7 @@ bool valtostring(const char* string, uint* value, bool silent) uint valfileoffsettova(const char* modname, uint offset) { char modpath[MAX_PATH] = ""; - if(modpathfromname(modname, modpath, MAX_PATH)) + if(ModPathFromName(modname, modpath, MAX_PATH)) { HANDLE FileHandle; DWORD LoadedSize; @@ -2106,7 +2106,7 @@ uint valfileoffsettova(const char* modname, uint offset) FileMapVA + (ULONG_PTR)offset, //Offset inside FileMapVA false); //Return without ImageBase StaticFileUnloadW(StringUtils::Utf8ToUtf16(modpath).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA); - return offset < LoadedSize ? (duint)rva + modbasefromname(modname) : 0; + return offset < LoadedSize ? (duint)rva + ModBaseFromName(modname) : 0; } } return 0; @@ -2115,7 +2115,7 @@ uint valfileoffsettova(const char* modname, uint offset) uint valvatofileoffset(uint va) { char modpath[MAX_PATH] = ""; - if(modpathfromaddr(va, modpath, MAX_PATH)) + if(ModPathFromAddr(va, modpath, MAX_PATH)) { HANDLE FileHandle; DWORD LoadedSize; @@ -2123,7 +2123,7 @@ uint valvatofileoffset(uint va) ULONG_PTR FileMapVA; if(StaticFileLoadW(StringUtils::Utf8ToUtf16(modpath).c_str(), UE_ACCESS_READ, false, &FileHandle, &LoadedSize, &FileMap, &FileMapVA)) { - ULONGLONG offset = ConvertVAtoFileOffsetEx(FileMapVA, LoadedSize, 0, va - modbasefromaddr(va), true, false); + ULONGLONG offset = ConvertVAtoFileOffsetEx(FileMapVA, LoadedSize, 0, va - ModBaseFromAddr(va), true, false); StaticFileUnloadW(StringUtils::Utf8ToUtf16(modpath).c_str(), true, FileHandle, LoadedSize, FileMap, FileMapVA); return (duint)offset; }