DBG: allow loading different debug engines

This commit is contained in:
Duncan Ogilvie 2020-09-08 22:09:36 +02:00
parent 54051bdcd1
commit d24c278d2b
4 changed files with 90 additions and 12 deletions

2
deps

@ -1 +1 @@
Subproject commit 0dd89c2c71eab89bdb44d11d14facf1c21dbbf92
Subproject commit 791d3bcce5d384b508a1d4e2d64088272a50dd5b

View File

@ -4,11 +4,30 @@
@brief Implements the main class.
*/
#include "_global.h"
#include "debugger.h"
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
if(fdwReason == DLL_PROCESS_ATTACH)
{
hInst = hinstDLL;
// Get program directory
{
wchar_t wszDir[deflen] = L"";
if(GetModuleFileNameW(hInst, wszDir, deflen))
{
strcpy_s(szProgramDir, StringUtils::Utf16ToUtf8(wszDir).c_str());
int len = (int)strlen(szProgramDir);
while(szProgramDir[len] != '\\')
len--;
szProgramDir[len] = 0;
}
}
// https://docs.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-disablethreadlibrarycalls
DisableThreadLibraryCalls(hinstDLL);
}
return TRUE;
}

View File

@ -617,22 +617,77 @@ static WString escape(WString cmdline)
return cmdline;
}
#include <delayimp.h>
// https://devblogs.microsoft.com/oldnewthing/20170126-00/?p=95265
static FARPROC WINAPI delayHook(unsigned dliNotify, PDelayLoadInfo pdli)
{
if(dliNotify == dliNotePreLoadLibrary && _stricmp(pdli->szDll, "TitanEngine.dll") == 0)
{
enum DebugEngine : duint
{
TitanEngine,
GleeBug,
StaticEngine,
};
static DebugEngine debugEngine = []
{
duint setting = TitanEngine;
if(!BridgeSettingGetUint("Engine", "DebugEngine", &setting))
{
auto msg = String(GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "GleeBug is now available for beta testing, would you like to enable it? Some bugs can be expected, but generally things are looking stable!\n\nYou can change this setting in the Settings dialog.")));
auto title = String(GuiTranslateText(QT_TRANSLATE_NOOP("DBG", "New debug engine available!")));
if(MessageBoxW(GuiGetWindowHandle(), StringUtils::Utf8ToUtf16(msg).c_str(), StringUtils::Utf8ToUtf16(title).c_str(), MB_YESNO | MB_ICONQUESTION | MB_DEFBUTTON2) == IDYES)
setting = GleeBug;
BridgeSettingSetUint("Engine", "DebugEngine", setting);
}
return (DebugEngine)setting;
}();
String fullPath = szProgramDir;
fullPath += '\\';
switch(debugEngine)
{
case GleeBug:
fullPath += "GleeBug\\TitanEngine.dll";
break;
case StaticEngine:
fullPath += "StaticEngine\\TitanEngine.dll";
break;
case TitanEngine:
default:
return 0;
}
auto hModule = LoadLibraryW(StringUtils::Utf8ToUtf16(fullPath).c_str());
if(hModule)
{
dprintf(QT_TRANSLATE_NOOP("DBG", "Successfully loaded %s!\n"), fullPath.c_str());
}
else
{
dprintf(QT_TRANSLATE_NOOP("DBG", "Failed to load %s, falling back to regular TitanEngine.dll"), fullPath.c_str());
}
return (FARPROC)hModule;
}
return 0;
}
PfnDliHook __pfnDliNotifyHook2 = delayHook;
extern "C" DLL_EXPORT const char* _dbg_dbginit()
{
if(!*szProgramDir)
return "GetModuleFileNameW failed!";
if(!EngineCheckStructAlignment(UE_STRUCT_TITAN_ENGINE_CONTEXT, sizeof(TITAN_ENGINE_CONTEXT_t)))
return "Invalid TITAN_ENGINE_CONTEXT_t alignment!";
static_assert(sizeof(TITAN_ENGINE_CONTEXT_t) == sizeof(REGISTERCONTEXT), "Invalid REGISTERCONTEXT alignment!");
wchar_t wszDir[deflen] = L"";
if(!GetModuleFileNameW(hInst, wszDir, deflen))
return "GetModuleFileNameW failed!";
strcpy_s(szProgramDir, StringUtils::Utf16ToUtf8(wszDir).c_str());
int len = (int)strlen(szProgramDir);
while(szProgramDir[len] != '\\')
len--;
szProgramDir[len] = 0;
strcpy_s(szDllLoaderPath, szProgramDir);
strcat_s(szDllLoaderPath, "\\loaddll.exe");
@ -815,4 +870,4 @@ extern "C" DLL_EXPORT bool _dbg_dbgcmddirectexec(const char* cmd)
bool dbgisstopped()
{
return bIsStopped;
}
}

View File

@ -386,6 +386,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>ntdll\ntdll_x86.lib;lz4\lz4_x86.lib;jansson\jansson_x86.lib;DeviceNameResolver\DeviceNameResolver_x86.lib;XEDParse\XEDParse_x86.lib;dbghelp\dbghelp_x86.lib;TitanEngine\TitanEngine_x86.lib;ws2_32.lib;psapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>TitanEngine.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
@ -407,6 +408,7 @@
<EnableCOMDATFolding>false</EnableCOMDATFolding>
<OptimizeReferences>false</OptimizeReferences>
<AdditionalDependencies>ntdll\ntdll_x86.lib;lz4\lz4_x86.lib;jansson\jansson_x86.lib;DeviceNameResolver\DeviceNameResolver_x86.lib;XEDParse\XEDParse_x86.lib;dbghelp\dbghelp_x86.lib;TitanEngine\TitanEngine_x86.lib;ws2_32.lib;psapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>TitanEngine.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -434,6 +436,7 @@
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>ntdll\ntdll_x64.lib;lz4\lz4_x64.lib;jansson\jansson_x64.lib;DeviceNameResolver\DeviceNameResolver_x64.lib;XEDParse\XEDParse_x64.lib;dbghelp\dbghelp_x64.lib;TitanEngine\TitanEngine_x64.lib;ws2_32.lib;psapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>TitanEngine.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -458,6 +461,7 @@
<EnableCOMDATFolding>false</EnableCOMDATFolding>
<OptimizeReferences>false</OptimizeReferences>
<AdditionalDependencies>ntdll\ntdll_x64.lib;lz4\lz4_x64.lib;jansson\jansson_x64.lib;DeviceNameResolver\DeviceNameResolver_x64.lib;XEDParse\XEDParse_x64.lib;dbghelp\dbghelp_x64.lib;TitanEngine\TitanEngine_x64.lib;ws2_32.lib;psapi.lib;kernel32.lib;user32.lib;gdi32.lib;winspool.lib;comdlg32.lib;advapi32.lib;shell32.lib;ole32.lib;oleaut32.lib;uuid.lib;odbc32.lib;odbccp32.lib;shlwapi.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>TitanEngine.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />