Skip to content

Commit 160d669

Browse files
committed
Workaround for a bug in the kernel with x64 emulation on ARM
1 parent fb1babc commit 160d669

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

TitanEngine/Global.Debugger.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ bool DebugStepFinal = false;
4242
LPVOID StepOutCallBack = NULL;
4343
CRITICAL_SECTION engineStepActiveCr;
4444

45+
// Workaround for a bug in the kernel with x64 emulation on ARM
46+
DWORD ContextControlFlags = []
47+
{
48+
DWORD flags = CONTEXT_CONTROL;
49+
typedef BOOL(WINAPI *type_IsWow64Process2)(HANDLE, USHORT*, USHORT*);
50+
auto p_IsWow64Process2 = (type_IsWow64Process2)GetProcAddress(GetModuleHandleW(L"kernel32.dll"), "IsWow64Process2");
51+
if (p_IsWow64Process2)
52+
{
53+
USHORT processMachine = 0;
54+
USHORT nativeMachine = 0;
55+
if (p_IsWow64Process2(GetCurrentProcess(), &processMachine, &nativeMachine))
56+
{
57+
if (nativeMachine == IMAGE_FILE_MACHINE_ARM || nativeMachine == IMAGE_FILE_MACHINE_ARM64)
58+
{
59+
flags = CONTEXT_ALL;
60+
}
61+
}
62+
}
63+
return flags;
64+
}();
65+
4566
// Global.Debugger.functions:
4667
long DebugLoopInSecondThread(LPVOID InputParameter)
4768
{

TitanEngine/Global.Debugger.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ extern wchar_t szDebuggerName[512];
4141
extern bool DebugStepFinal;
4242
extern LPVOID StepOutCallBack;
4343
extern CRITICAL_SECTION engineStepActiveCr;
44+
extern DWORD ContextControlFlags;
4445

4546
long DebugLoopInSecondThread(LPVOID InputParameter);
4647
void DebuggerReset();

TitanEngine/TitanEngine.Debugger.Control.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ __declspec(dllexport) void TITCALL StepInto(LPVOID StepCallBack)
5555
{
5656
CONTEXT myDBGContext;
5757
HANDLE hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
58-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
58+
myDBGContext.ContextFlags = ContextControlFlags;
5959
GetThreadContext(hActiveThread, &myDBGContext);
6060
myDBGContext.EFlags |= UE_TRAP_FLAG;
6161
SetThreadContext(hActiveThread, &myDBGContext);

TitanEngine/TitanEngine.Debugger.DebugLoop.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
552552
FlushInstructionCache(dbgProcessInformation.hProcess, NULL, 0);
553553
DBGCode = DBG_CONTINUE;
554554
hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
555-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
555+
myDBGContext.ContextFlags = ContextControlFlags;
556556
GetThreadContext(hActiveThread, &myDBGContext);
557557
if(FoundBreakPoint.BreakPointType != UE_SINGLESHOOT)
558558
myDBGContext.EFlags |= UE_TRAP_FLAG;
@@ -674,7 +674,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
674674
else
675675
{
676676
hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
677-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
677+
myDBGContext.ContextFlags = ContextControlFlags;
678678
GetThreadContext(hActiveThread, &myDBGContext);
679679
myDBGContext.EFlags |= UE_TRAP_FLAG;
680680
SetThreadContext(hActiveThread, &myDBGContext);
@@ -727,7 +727,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
727727
{
728728
//handle hardware breakpoints
729729
hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
730-
myDBGContext.ContextFlags = CONTEXT_DEBUG_REGISTERS | CONTEXT_CONTROL;
730+
myDBGContext.ContextFlags = CONTEXT_DEBUG_REGISTERS | ContextControlFlags;
731731
GetThreadContext(hActiveThread, &myDBGContext);
732732
if((ULONG_PTR)DBGEvent.u.Exception.ExceptionRecord.ExceptionAddress == myDBGContext.Dr0 || (myDBGContext.Dr6 & 0x1))
733733
{
@@ -893,7 +893,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
893893
if(bFoundBreakPoint) //found memory breakpoint
894894
{
895895
hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
896-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
896+
myDBGContext.ContextFlags = ContextControlFlags;
897897
GetThreadContext(hActiveThread, &myDBGContext);
898898
DBGCode = DBG_CONTINUE; //debugger handled the exception
899899
MemoryBpxCallBack = FoundBreakPoint.ExecuteCallBack;
@@ -1062,7 +1062,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
10621062
if(bFoundBreakPoint && engineMembpAlt) //found memory breakpoint
10631063
{
10641064
hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
1065-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
1065+
myDBGContext.ContextFlags = ContextControlFlags;
10661066
GetThreadContext(hActiveThread, &myDBGContext);
10671067
DBGCode = DBG_CONTINUE; //debugger handled the exception
10681068
MemoryBpxCallBack = FoundBreakPoint.ExecuteCallBack;
@@ -1239,7 +1239,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
12391239
FlushInstructionCache(dbgProcessInformation.hProcess, NULL, 0);
12401240
DBGCode = DBG_CONTINUE;
12411241
hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
1242-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
1242+
myDBGContext.ContextFlags = ContextControlFlags;
12431243
GetThreadContext(hActiveThread, &myDBGContext);
12441244
if(FoundBreakPoint.BreakPointType != UE_SINGLESHOOT)
12451245
myDBGContext.EFlags |= UE_TRAP_FLAG;
@@ -1400,7 +1400,7 @@ __declspec(dllexport) void TITCALL DebugLoop()
14001400
{
14011401
CONTEXT DbgCtx;
14021402

1403-
DbgCtx.ContextFlags = CONTEXT_CONTROL;
1403+
DbgCtx.ContextFlags = ContextControlFlags;
14041404

14051405
hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, DBGEvent.dwThreadId);
14061406

TitanEngine/TitanEngine.Debugger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ __declspec(dllexport) bool TITCALL DetachDebuggerEx(DWORD ProcessId)
620620
{
621621
HANDLE hActiveThread = EngineOpenThread(THREAD_GETSETSUSPEND, false, hListThread.at(i).dwThreadId);
622622
CONTEXT myDBGContext;
623-
myDBGContext.ContextFlags = CONTEXT_CONTROL;
623+
myDBGContext.ContextFlags = ContextControlFlags;
624624
GetThreadContext(hActiveThread, &myDBGContext);
625625
myDBGContext.EFlags &= ~UE_TRAP_FLAG;
626626
myDBGContext.EFlags &= ~UE_RESUME_FLAG;

0 commit comments

Comments
 (0)