bool PatchEnum(PATCHINFO* List, size_t* Size) { ASSERT_DEBUGGING("Export call"); ASSERT_FALSE(!List && !Size); SHARED_ACQUIRE(LockPatches); // Did the user request the size? if(Size) { *Size = patches.size() * sizeof(PATCHINFO); if(!List) return true; } // Copy each vector entry to a C-style array for(auto & itr : patches) { *List = itr.second; List->addr += ModBaseFromName(itr.second.mod);; List++; } return true; }
bool LabelFromString(const char* Text, uint* Address) { // CHECK: Future? (Not used) if(!DbgIsDebugging()) return false; SHARED_ACQUIRE(LockLabels); for(auto & itr : labels) { // Check if the actual label name matches if(strcmp(itr.second.text, Text)) continue; if(Address) *Address = itr.second.addr + ModBaseFromName(itr.second.mod); // Set status to indicate if label was ever found return true; } return false; }
void AdjustValue(ARGUMENTSINFO & value) const override { auto base = ModBaseFromName(value.mod); value.start += base; value.end += base; }
SCRIPT_EXPORT duint Script::Module::BaseFromName(const char* name) { return ModBaseFromName(name); }
int PatchFile(const PATCHINFO* List, int Count, const char* FileName, char* Error) { // // This function returns an int based on the number // of patches applied. -1 indicates a failure. // if(Count <= 0) { // Notify the user of the error if(Error) strcpy_s(Error, MAX_ERROR_SIZE, "No patches to apply"); return -1; } // Get a copy of the first module name in the array char moduleName[MAX_MODULE_SIZE]; strcpy_s(moduleName, List[0].mod); // Check if all patches are in the same module for(int i = 0; i < Count; i++) { if(_stricmp(List[i].mod, moduleName)) { if(Error) sprintf_s(Error, MAX_ERROR_SIZE, "Not all patches are in module %s", moduleName); return -1; } } // See if the module was loaded duint moduleBase = ModBaseFromName(moduleName); if(!moduleBase) { if(Error) sprintf_s(Error, MAX_ERROR_SIZE, "Failed to get base of module %s", moduleName); return -1; } // Get the unicode version of the module's path wchar_t originalName[MAX_PATH]; if(!GetModuleFileNameExW(fdProcessInfo->hProcess, (HMODULE)moduleBase, originalName, ARRAYSIZE(originalName))) { if(Error) sprintf_s(Error, MAX_ERROR_SIZE, "Failed to get module path of module %s", moduleName); return -1; } // Create a temporary backup file if(!CopyFileW(originalName, StringUtils::Utf8ToUtf16(FileName).c_str(), false)) { if(Error) strcpy_s(Error, MAX_ERROR_SIZE, "Failed to make a copy of the original file (patch target is in use?)"); return -1; } HANDLE fileHandle; DWORD loadedSize; HANDLE fileMap; ULONG_PTR fileMapVa; if(!StaticFileLoadW(StringUtils::Utf8ToUtf16(FileName).c_str(), UE_ACCESS_ALL, false, &fileHandle, &loadedSize, &fileMap, &fileMapVa)) { strcpy_s(Error, MAX_ERROR_SIZE, "StaticFileLoad failed"); return -1; } // Begin iterating all patches, applying them to a file int patchCount = 0; for(int i = 0; i < Count; i++) { // Convert the virtual address to an offset within disk file data unsigned char* ptr = (unsigned char*)ConvertVAtoFileOffsetEx(fileMapVa, loadedSize, moduleBase, List[i].addr, false, true); // Skip patches that do not have a raw address if(!ptr) continue; *ptr = List[i].newbyte; patchCount++; } // Unload the file from memory and commit changes to disk if(!StaticFileUnloadW(StringUtils::Utf8ToUtf16(FileName).c_str(), true, fileHandle, loadedSize, fileMap, fileMapVa)) { if(Error) strcpy_s(Error, MAX_ERROR_SIZE, "StaticFileUnload failed"); return -1; } // Zero the error message and return count if(Error) memset(Error, 0, MAX_ERROR_SIZE * sizeof(char)); return patchCount; }
int ModPathFromName(const char* Module, char* Path, int Size) { return ModPathFromAddr(ModBaseFromName(Module), Path, Size); }
extern "C" DLL_EXPORT uint _dbg_sendmessage(DBGMSG type, void* param1, void* param2) { if(dbgisstopped()) { switch(type) //ignore win events { //these functions are safe to call when we did not initialize yet case DBG_DEINITIALIZE_LOCKS: case DBG_INITIALIZE_LOCKS: case DBG_GET_FUNCTIONS: case DBG_SETTINGS_UPDATED: case DBG_GET_THREAD_LIST: case DBG_WIN_EVENT: case DBG_WIN_EVENT_GLOBAL: break; //the rest is unsafe -> throw an exception when people try to call them default: __debugbreak(); //we cannot process messages when the debugger is stopped, this must be a bug } } switch(type) { case DBG_SCRIPT_LOAD: { scriptload((const char*)param1); } break; case DBG_SCRIPT_UNLOAD: { scriptunload(); } break; case DBG_SCRIPT_RUN: { scriptrun((int)(duint)param1); } break; case DBG_SCRIPT_STEP: { scriptstep(); } break; case DBG_SCRIPT_BPTOGGLE: { return scriptbptoggle((int)(duint)param1); } break; case DBG_SCRIPT_BPGET: { return scriptbpget((int)(duint)param1); } break; case DBG_SCRIPT_CMDEXEC: { return scriptcmdexec((const char*)param1); } break; case DBG_SCRIPT_ABORT: { scriptabort(); } break; case DBG_SCRIPT_GETLINETYPE: { return (duint)scriptgetlinetype((int)(duint)param1); } break; case DBG_SCRIPT_SETIP: { scriptsetip((int)(duint)param1); } break; case DBG_SCRIPT_GETBRANCHINFO: { return (duint)scriptgetbranchinfo((int)(duint)param1, (SCRIPTBRANCH*)param2); } break; case DBG_SYMBOL_ENUM: { SYMBOLCBINFO* cbInfo = (SYMBOLCBINFO*)param1; SymEnum(cbInfo->base, cbInfo->cbSymbolEnum, cbInfo->user); } break; case DBG_ASSEMBLE_AT: { return assembleat((duint)param1, (const char*)param2, 0, 0, false); } break; case DBG_MODBASE_FROM_NAME: { return ModBaseFromName((const char*)param1); } break; case DBG_DISASM_AT: { disasmget((uint)param1, (DISASM_INSTR*)param2); } break; case DBG_STACK_COMMENT_GET: { return stackcommentget((uint)param1, (STACK_COMMENT*)param2); } break; case DBG_GET_THREAD_LIST: { ThreadGetList((THREADLIST*)param1); } break; case DBG_SETTINGS_UPDATED: { valuesetsignedcalc(!settingboolget("Engine", "CalculationType")); //0:signed, 1:unsigned SetEngineVariable(UE_ENGINE_SET_DEBUG_PRIVILEGE, settingboolget("Engine", "EnableDebugPrivilege")); bOnlyCipAutoComments = settingboolget("Disassembler", "OnlyCipAutoComments"); bListAllPages = settingboolget("Engine", "ListAllPages"); bUndecorateSymbolNames = settingboolget("Engine", "UndecorateSymbolNames"); bEnableSourceDebugging = settingboolget("Engine", "EnableSourceDebugging"); uint setting; if(BridgeSettingGetUint("Engine", "BreakpointType", &setting)) { switch(setting) { case 0: //break_int3short SetBPXOptions(UE_BREAKPOINT_INT3); break; case 1: //break_int3long SetBPXOptions(UE_BREAKPOINT_LONG_INT3); break; case 2: //break_ud2 SetBPXOptions(UE_BREAKPOINT_UD2); break; } } char exceptionRange[MAX_SETTING_SIZE] = ""; dbgclearignoredexceptions(); if(BridgeSettingGet("Exceptions", "IgnoreRange", exceptionRange)) { char* entry = strtok(exceptionRange, ","); while(entry) { unsigned long start; unsigned long end; if(sscanf(entry, "%08X-%08X", &start, &end) == 2 && start <= end) { ExceptionRange range; range.start = start; range.end = end; dbgaddignoredexception(range); } entry = strtok(0, ","); } } char cachePath[MAX_SETTING_SIZE]; if(BridgeSettingGet("Symbols", "CachePath", cachePath)) { // Trim the buffer to fit inside MAX_PATH strncpy_s(szSymbolCachePath, cachePath, _TRUNCATE); } } break; case DBG_DISASM_FAST_AT: { if(!param1 || !param2) return 0; BASIC_INSTRUCTION_INFO* basicinfo = (BASIC_INSTRUCTION_INFO*)param2; if(!disasmfast((uint)param1, basicinfo)) basicinfo->size = 1; return 0; } break; case DBG_MENU_ENTRY_CLICKED: { int hEntry = (int)(uint)param1; pluginmenucall(hEntry); } break; case DBG_FUNCTION_GET: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)FunctionGet(info->addr, &info->start, &info->end); } break; case DBG_FUNCTION_OVERLAPS: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)FunctionOverlaps(info->start, info->end); } break; case DBG_FUNCTION_ADD: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)FunctionAdd(info->start, info->end, info->manual); } break; case DBG_FUNCTION_DEL: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)FunctionDelete(info->addr); } break; case DBG_LOOP_GET: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)LoopGet(info->depth, info->addr, &info->start, &info->end); } break; case DBG_LOOP_OVERLAPS: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)LoopOverlaps(info->depth, info->start, info->end, 0); } break; case DBG_LOOP_ADD: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)LoopAdd(info->start, info->end, info->manual); } break; case DBG_LOOP_DEL: { FUNCTION_LOOP_INFO* info = (FUNCTION_LOOP_INFO*)param1; return (uint)LoopDelete(info->depth, info->addr); } break; case DBG_IS_RUN_LOCKED: { return (uint)waitislocked(WAITID_RUN); } break; case DBG_IS_BP_DISABLED: { BREAKPOINT bp; if(BpGet((uint)param1, BPNORMAL, 0, &bp)) return !(uint)bp.enabled; return (uint)false; } break; case DBG_SET_AUTO_COMMENT_AT: { return (uint)CommentSet((uint)param1, (const char*)param2, false); } break; case DBG_DELETE_AUTO_COMMENT_RANGE: { CommentDelRange((uint)param1, (uint)param2); } break; case DBG_SET_AUTO_LABEL_AT: { return (uint)LabelSet((uint)param1, (const char*)param2, false); } break; case DBG_DELETE_AUTO_LABEL_RANGE: { LabelDelRange((uint)param1, (uint)param2); } break; case DBG_SET_AUTO_BOOKMARK_AT: { return (uint)BookmarkSet((uint)param1, false); } break; case DBG_DELETE_AUTO_BOOKMARK_RANGE: { BookmarkDelRange((uint)param1, (uint)param2); } break; case DBG_SET_AUTO_FUNCTION_AT: { return (uint)FunctionAdd((uint)param1, (uint)param2, false); } break; case DBG_DELETE_AUTO_FUNCTION_RANGE: { FunctionDelRange((uint)param1, (uint)param2); } break; case DBG_GET_STRING_AT: { STRING_TYPE strtype; char string[MAX_STRING_SIZE]; if(disasmgetstringat((uint)param1, &strtype, string, string, MAX_STRING_SIZE-3)) { if(strtype == str_ascii) sprintf((char*)param2, "\"%s\"", string); else //unicode sprintf((char*)param2, "L\"%s\"", string); return true; } return false; } break; case DBG_GET_FUNCTIONS: { return (uint)dbgfunctionsget(); } break; case DBG_WIN_EVENT: { return (uint)pluginwinevent((MSG*)param1, (long*)param2); } break; case DBG_WIN_EVENT_GLOBAL: { return (uint)pluginwineventglobal((MSG*)param1); } break; case DBG_INITIALIZE_LOCKS: { SectionLockerGlobal::Initialize(); } break; case DBG_DEINITIALIZE_LOCKS: { SectionLockerGlobal::Deinitialize(); } break; case DBG_GET_TIME_WASTED_COUNTER: return dbggettimewastedcounter(); } return 0; }