Fix detaching

This commit is contained in:
Duncan Ogilvie 2020-10-10 17:36:40 +02:00
parent 35fdd5684e
commit 885e290cc4
2 changed files with 17 additions and 32 deletions

View File

@ -12,8 +12,6 @@
#define UE_MODULEx86 0x2000; #define UE_MODULEx86 0x2000;
#define UE_MODULEx64 0x2000; #define UE_MODULEx64 0x2000;
static DWORD engineWaitForDebugEventTimeOut = INFINITE;
__declspec(dllexport) void TITCALL DebugLoop() __declspec(dllexport) void TITCALL DebugLoop()
{ {
bool FirstBPX = true; bool FirstBPX = true;
@ -78,8 +76,14 @@ __declspec(dllexport) void TITCALL DebugLoop()
while(!BreakDBG) //actual debug loop while(!BreakDBG) //actual debug loop
{ {
// Fix based on work by https://github.com/number201724 // Fix based on work by https://github.com/number201724
if(!WaitForDebugEvent(&DBGEvent, engineWaitForDebugEventTimeOut)) if(!WaitForDebugEvent(&DBGEvent, 100))
{ {
if (engineProcessIsNowDetached)
{
DebugActiveProcessStop(dbgProcessInformation.dwProcessId);
DebugAttachedToProcess = false;
break;
}
if(WaitForSingleObject(dbgProcessInformation.hProcess, 0) == WAIT_OBJECT_0) if(WaitForSingleObject(dbgProcessInformation.hProcess, 0) == WAIT_OBJECT_0)
{ {
DBGEvent.dwDebugEventCode = EXIT_PROCESS_DEBUG_EVENT; DBGEvent.dwDebugEventCode = EXIT_PROCESS_DEBUG_EVENT;
@ -90,6 +94,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
} }
else else
{ {
// Regular timeout, wait again
continue; continue;
} }
} }
@ -1169,6 +1174,12 @@ __declspec(dllexport) void TITCALL DebugLoop()
{ {
break; break;
} }
if (engineProcessIsNowDetached)
{
DebugActiveProcessStop(dbgProcessInformation.dwProcessId);
DebugAttachedToProcess = false;
break;
}
if(!ThreaderGetThreadInfo(0, DBGEvent.dwThreadId)) //switch thread if(!ThreaderGetThreadInfo(0, DBGEvent.dwThreadId)) //switch thread
DBGEvent.dwThreadId = dbgProcessInformation.dwThreadId; DBGEvent.dwThreadId = dbgProcessInformation.dwThreadId;
} }
@ -1195,5 +1206,5 @@ __declspec(dllexport) void TITCALL DebugLoopEx(DWORD TimeOut)
__declspec(dllexport) void TITCALL SetDebugLoopTimeOut(DWORD TimeOut) __declspec(dllexport) void TITCALL SetDebugLoopTimeOut(DWORD TimeOut)
{ {
engineWaitForDebugEventTimeOut = TimeOut; __debugbreak();
} }

View File

@ -575,34 +575,9 @@ __declspec(dllexport) bool TITCALL AttachDebugger(DWORD ProcessId, bool KillOnEx
__declspec(dllexport) bool TITCALL DetachDebugger(DWORD ProcessId) __declspec(dllexport) bool TITCALL DetachDebugger(DWORD ProcessId)
{ {
typedef bool(WINAPI * fDebugActiveProcessStop)(DWORD dwProcessId);
fDebugActiveProcessStop myDebugActiveProcessStop;
LPVOID funcDebugActiveProcessStop = NULL;
bool FuncReturn = false;
RemoveAllBreakPoints(UE_OPTION_REMOVEALL); RemoveAllBreakPoints(UE_OPTION_REMOVEALL);
engineProcessIsNowDetached = true; // Request detach
if(ProcessId != NULL)
{
funcDebugActiveProcessStop = GetProcAddress(GetModuleHandleA("kernel32.dll"), "DebugActiveProcessStop");
if(funcDebugActiveProcessStop != NULL)
{
myDebugActiveProcessStop = (fDebugActiveProcessStop)(funcDebugActiveProcessStop);
FuncReturn = myDebugActiveProcessStop(ProcessId);
engineProcessIsNowDetached = true;
Sleep(250);
}
DebugAttachedToProcess = false;
if(FuncReturn)
{
return true; return true;
}
else
{
return false;
}
}
return false;
} }
__declspec(dllexport) bool TITCALL DetachDebuggerEx(DWORD ProcessId) __declspec(dllexport) bool TITCALL DetachDebuggerEx(DWORD ProcessId)
@ -620,7 +595,6 @@ __declspec(dllexport) bool TITCALL DetachDebuggerEx(DWORD ProcessId)
SetThreadContext(hActiveThread, &myDBGContext); SetThreadContext(hActiveThread, &myDBGContext);
EngineCloseHandle(hActiveThread); EngineCloseHandle(hActiveThread);
} }
ContinueDebugEvent(DBGEvent.dwProcessId, DBGEvent.dwThreadId, DBGCode);
ThreaderResumeProcess(); ThreaderResumeProcess();
return DetachDebugger(ProcessId); return DetachDebugger(ProcessId);
} }