From e0dfd69a584784f86248ce9fcde98ccf8d8c88cf Mon Sep 17 00:00:00 2001 From: ryuukk <44361234+ryuukk@users.noreply.github.com> Date: Wed, 10 Mar 2021 15:00:32 +0100 Subject: [PATCH 1/4] Avoid GC allocation when building the search path --- src/core/sys/windows/stacktrace.d | 35 ++++++++++++++++++------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/core/sys/windows/stacktrace.d b/src/core/sys/windows/stacktrace.d index 2922e54eba..24a615dc94 100644 --- a/src/core/sys/windows/stacktrace.d +++ b/src/core/sys/windows/stacktrace.d @@ -345,26 +345,28 @@ extern(Windows) BOOL FixupDebugHeader(HANDLE hProcess, ULONG ActionCode, return FALSE; } -private string generateSearchPath() +private size_t generateSearchPath(char[] path) { - __gshared string[3] defaultPathList = ["_NT_SYMBOL_PATH", - "_NT_ALTERNATE_SYMBOL_PATH", - "SYSTEMROOT"]; + __gshared string[3] defaultPathList = [ + "_NT_SYMBOL_PATH", "_NT_ALTERNATE_SYMBOL_PATH", "SYSTEMROOT" + ]; - string path; - char[2048] temp = void; + char[MAX_PATH] temp = void; DWORD len; - - foreach ( e; defaultPathList ) + size_t index = 0; + foreach (e; defaultPathList) { - if ( (len = GetEnvironmentVariableA( e.ptr, temp.ptr, temp.length )) > 0 ) + if ((len = GetEnvironmentVariableA(e.ptr, temp.ptr, temp.length)) > 0) { - path ~= temp[0 .. len]; - path ~= ";"; + path[index .. index + len] = temp[0 .. len]; + index += len; + path[index] = ';'; + index += 1; } } - path ~= "\0"; - return path; + path[index] = '\0'; + index += 1; + return index; } @@ -400,9 +402,12 @@ shared static this() symOptions |= SYMOPT_DEFERRED_LOAD; symOptions = dbghelp.SymSetOptions( symOptions ); - debug(PRINTF) printf("Search paths: %s\n", generateSearchPath().ptr); + char[MAX_PATH] buffer; + generateSearchPath(buffer); + + debug(PRINTF) printf("Search paths: %s\n", buffer.ptr); - if (!dbghelp.SymInitialize(hProcess, generateSearchPath().ptr, TRUE)) + if (!dbghelp.SymInitialize(hProcess, buffer.ptr, TRUE)) return; dbghelp.SymRegisterCallback64(hProcess, &FixupDebugHeader, 0); From 73eefd6d80313c4f307840d217777ba846cb70be Mon Sep 17 00:00:00 2001 From: ryuukk <44361234+ryuukk@users.noreply.github.com> Date: Wed, 10 Mar 2021 15:39:07 +0100 Subject: [PATCH 2/4] Update src/core/sys/windows/stacktrace.d Co-authored-by: Mathias LANG --- src/core/sys/windows/stacktrace.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/sys/windows/stacktrace.d b/src/core/sys/windows/stacktrace.d index 24a615dc94..5030ae27f0 100644 --- a/src/core/sys/windows/stacktrace.d +++ b/src/core/sys/windows/stacktrace.d @@ -345,7 +345,7 @@ extern(Windows) BOOL FixupDebugHeader(HANDLE hProcess, ULONG ActionCode, return FALSE; } -private size_t generateSearchPath(char[] path) +private size_t generateSearchPath(ref char[MAX_PATH] path) { __gshared string[3] defaultPathList = [ "_NT_SYMBOL_PATH", "_NT_ALTERNATE_SYMBOL_PATH", "SYSTEMROOT" From 2b53cbffb2a50991b1d5d0e2f9740cb929961b1d Mon Sep 17 00:00:00 2001 From: ryuukk <44361234+ryuukk@users.noreply.github.com> Date: Wed, 10 Mar 2021 19:15:04 +0100 Subject: [PATCH 3/4] Use heap allocated array instead of a stack one to retrieve paths --- src/core/sys/windows/stacktrace.d | 36 ++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/src/core/sys/windows/stacktrace.d b/src/core/sys/windows/stacktrace.d index 5030ae27f0..4e26ca4233 100644 --- a/src/core/sys/windows/stacktrace.d +++ b/src/core/sys/windows/stacktrace.d @@ -345,7 +345,7 @@ extern(Windows) BOOL FixupDebugHeader(HANDLE hProcess, ULONG ActionCode, return FALSE; } -private size_t generateSearchPath(ref char[MAX_PATH] path) +private char[] generateSearchPathAlloc() { __gshared string[3] defaultPathList = [ "_NT_SYMBOL_PATH", "_NT_ALTERNATE_SYMBOL_PATH", "SYSTEMROOT" @@ -353,20 +353,30 @@ private size_t generateSearchPath(ref char[MAX_PATH] path) char[MAX_PATH] temp = void; DWORD len; - size_t index = 0; + DWORD total; foreach (e; defaultPathList) { if ((len = GetEnvironmentVariableA(e.ptr, temp.ptr, temp.length)) > 0) { - path[index .. index + len] = temp[0 .. len]; - index += len; - path[index] = ';'; - index += 1; + total += len + 1; } } - path[index] = '\0'; - index += 1; - return index; + + if(total > 0) + { + auto buffer = cast(char[]) malloc(total + 1)[0 .. total + 1]; + foreach (e; defaultPathList) + { + if ((len = GetEnvironmentVariableA(e.ptr, &buffer[len], buffer.length - len)) > 0) + { + buffer[len] = ';'; + len += 1; + } + } + buffer[$-1] = '\0'; + return buffer; + } + return []; } @@ -402,12 +412,12 @@ shared static this() symOptions |= SYMOPT_DEFERRED_LOAD; symOptions = dbghelp.SymSetOptions( symOptions ); - char[MAX_PATH] buffer; - generateSearchPath(buffer); + auto paths = generateSearchPathAlloc(buffer); + scope(exit) if(paths.length > 0) free(paths); - debug(PRINTF) printf("Search paths: %s\n", buffer.ptr); + debug(PRINTF) printf("Search paths: %s\n", paths.ptr); - if (!dbghelp.SymInitialize(hProcess, buffer.ptr, TRUE)) + if (!dbghelp.SymInitialize(hProcess, paths.ptr, TRUE)) return; dbghelp.SymRegisterCallback64(hProcess, &FixupDebugHeader, 0); From 745f975b27c4d26920028fd0a48a8ce9caa4ad3a Mon Sep 17 00:00:00 2001 From: ryuukk <44361234+ryuukk@users.noreply.github.com> Date: Wed, 10 Mar 2021 19:19:21 +0100 Subject: [PATCH 4/4] Fix build --- src/core/sys/windows/stacktrace.d | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/sys/windows/stacktrace.d b/src/core/sys/windows/stacktrace.d index 4e26ca4233..77a7fcea0a 100644 --- a/src/core/sys/windows/stacktrace.d +++ b/src/core/sys/windows/stacktrace.d @@ -413,7 +413,7 @@ shared static this() symOptions = dbghelp.SymSetOptions( symOptions ); auto paths = generateSearchPathAlloc(buffer); - scope(exit) if(paths.length > 0) free(paths); + scope(exit) if(paths.length > 0) free(paths.ptr); debug(PRINTF) printf("Search paths: %s\n", paths.ptr);