From ae4a32ef56a9b1cb44052b170f49a60c8eb6a279 Mon Sep 17 00:00:00 2001 From: xiren7 Date: Fri, 13 Jan 2017 22:38:14 +0800 Subject: [PATCH 1/2] Fix no stack trace on windows --- src/ldc/eh/msvc.d | 40 +++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/src/ldc/eh/msvc.d b/src/ldc/eh/msvc.d index 933ef1004d..451a6cc0c8 100644 --- a/src/ldc/eh/msvc.d +++ b/src/ldc/eh/msvc.d @@ -94,6 +94,8 @@ struct CxxExceptionInfo version(Win64) void* ImgBase; } +extern(C) Throwable.TraceInfo _d_traceContext(void* ptr = null); + extern(C) void _d_throw_exception(Object e) { if (e is null) @@ -109,12 +111,16 @@ extern(C) void _d_throw_exception(Object e) if (!old_terminate_handler) old_terminate_handler = set_terminate(&msvc_eh_terminate); } - auto t = cast(Throwable) e; - exceptionStack.push(t); + + auto throwable = cast(Throwable) e; + exceptionStack.push(throwable); + + if (throwable.info is null && cast(byte*)throwable !is typeid(throwable).init.ptr) + throwable.info = _d_traceContext(); CxxExceptionInfo info; info.Magic = EH_MAGIC_NUMBER1; - info.pThrowable = &t; + info.pThrowable = &throwable; info.ThrowInfo = getThrowInfo(ti).toPointer; version(Win64) info.ImgBase = ehHeap.base; @@ -377,14 +383,14 @@ void msvc_eh_terminate() nothrow mov RBX, 0xFFFFFF; and RDX, RBX; cmp RDX, 0xC48348; // add ESP,nn (debug UCRT libs) - je L_addESP_found; + je L_addESP_found; cmp DL, 0x90; // nop; (release libs) - jne L_term; - - L_release_ucrt: - mov RDX,[RSP+8]; - cmp word ptr[RDX-2], 0xD3FF; // call ebx? - sete BL; // if not, it's UCRT 10.0.14393.0 + jne L_term; + + L_release_ucrt: + mov RDX,[RSP+8]; + cmp word ptr[RDX-2], 0xD3FF; // call ebx? + sete BL; // if not, it's UCRT 10.0.14393.0 movzx RBX,BL; mov RDX, 0x28; // release build of vcruntimelib jmp L_retTerminate; @@ -394,13 +400,13 @@ void msvc_eh_terminate() nothrow cmp byte ptr [RAX+4], 0xC3; // ret? jne L_term; - + L_retTerminate: lea RDX,[RSP+RDX+0x10]; // RSP before returning from terminate() mov RAX,[RDX]; // return address inside __FrameUnwindHandler - or RDX,RBX; // RDX aligned, save RBX == 0 for UCRT 10.0.14393.0, 1 otherwise + or RDX,RBX; // RDX aligned, save RBX == 0 for UCRT 10.0.14393.0, 1 otherwise cmp byte ptr [RAX-19], 0xEB; // skip back to default jump inside "switch" (libvcruntimed.lib) je L_switchFound; @@ -424,7 +430,7 @@ void msvc_eh_terminate() nothrow L_switchFound2: dec RAX; - L_switchFound: + L_switchFound: movsx RBX, byte ptr [RAX-18]; // follow jump lea RAX, [RAX+RBX-17]; @@ -433,12 +439,12 @@ void msvc_eh_terminate() nothrow add RAX,2; L_xorSkipped: - mov RBX, RDX; // extract UCRT marker from EDX - and RDX, ~1; - and RBX, 1; + mov RBX, RDX; // extract UCRT marker from EDX + and RDX, ~1; + and RBX, 1; cmovnz RBX,[RDX-8]; // restore RBX (pushed inside terminate()) - cmovz RBX,[RSP]; // RBX not changed in terminate inside UCRT 10.0.14393.0 + cmovz RBX,[RSP]; // RBX not changed in terminate inside UCRT 10.0.14393.0 lea RSP,[RDX+8]; push RAX; // new return after setting return value in __frameUnwindHandler From fb275b35cd7461fe1ae634f75abab4a892227e68 Mon Sep 17 00:00:00 2001 From: xiren7 Date: Sat, 14 Jan 2017 00:30:18 +0800 Subject: [PATCH 2/2] Update runtime.d --- src/core/runtime.d | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/core/runtime.d b/src/core/runtime.d index e7c50bb513..8efb86b30c 100644 --- a/src/core/runtime.d +++ b/src/core/runtime.d @@ -760,11 +760,17 @@ Throwable.TraceInfo defaultTraceHandler( void* ptr = null ) { version (Win64) { - static enum FIRSTFRAME = 4; + version (LDC) + static enum FIRSTFRAME = 1; + else + static enum FIRSTFRAME = 4; } else version (Win32) { - static enum FIRSTFRAME = 0; + version (LDC) + static enum FIRSTFRAME = 1; + else + static enum FIRSTFRAME = 0; } import core.sys.windows.windows : CONTEXT; auto s = new StackTrace(FIRSTFRAME, cast(CONTEXT*)ptr);