Skip to content

Commit 8bf538c

Browse files
authored
Merge pull request #11947 from compnerd/windows-demangling
runtime: add undecoration support for Windows
2 parents 241b4f9 + fb68207 commit 8bf538c

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

stdlib/public/runtime/Errors.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
2121
#endif
2222

23+
#if defined(_WIN32)
24+
#include <mutex>
25+
#endif
26+
2327
#include <stdio.h>
2428
#include <stdlib.h>
2529
#include <string.h>
@@ -30,13 +34,17 @@
3034
#include <unistd.h>
3135
#endif
3236
#include <stdarg.h>
37+
3338
#include "ImageInspection.h"
3439
#include "swift/Runtime/Debug.h"
3540
#include "swift/Runtime/Mutex.h"
3641
#include "swift/Demangling/Demangle.h"
3742
#include "swift/Basic/LLVM.h"
3843
#include "llvm/ADT/StringRef.h"
39-
#if !defined(_MSC_VER)
44+
45+
#if defined(_MSC_VER)
46+
#include <DbgHelp.h>
47+
#else
4048
#include <cxxabi.h>
4149
#endif
4250

@@ -78,17 +86,39 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName, SymbolInfo syminfo,
7886
// demangle with swift. We are taking advantage of __cxa_demangle actually
7987
// providing failure status instead of just returning the original string like
8088
// swift demangle.
89+
#if defined(_WIN32)
90+
DWORD dwFlags = UNDNAME_COMPLETE;
91+
#if !defined(_WIN64)
92+
dwFlags |= UNDNAME_32_BIT_DECODE;
93+
#endif
94+
static std::mutex mutex;
95+
96+
char szUndName[1024];
97+
DWORD dwResult;
98+
99+
{
100+
std::lock_guard<std::mutex> lock(m);
101+
dwResult = UnDecorateSymbolName(syminfo.symbolName, szUndName,
102+
sizeof(szUndName), dwFlags);
103+
}
104+
105+
if (dwResult == TRUE) {
106+
symbolName += szUndName;
107+
return true;
108+
}
109+
#else
81110
int status;
82111
char *demangled = abi::__cxa_demangle(syminfo.symbolName, 0, 0, &status);
83112
if (status == 0) {
84-
assert(demangled != nullptr && "If __cxa_demangle succeeds, demangled "
85-
"should never be nullptr");
113+
assert(demangled != nullptr &&
114+
"If __cxa_demangle succeeds, demangled should never be nullptr");
86115
symbolName += demangled;
87116
free(demangled);
88117
return true;
89118
}
90-
assert(demangled == nullptr && "If __cxa_demangle fails, demangled should "
91-
"be a nullptr");
119+
assert(demangled == nullptr &&
120+
"If __cxa_demangle fails, demangled should be a nullptr");
121+
#endif
92122

93123
// Otherwise, try to demangle with swift. If swift fails to demangle, it will
94124
// just pass through the original output.

0 commit comments

Comments
 (0)