Skip to content

Commit 1abe971

Browse files
author
Jason Mittertreiner
committed
Fix StackTraces on Windows
1 parent 0cf79bb commit 1abe971

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

stdlib/public/runtime/Errors.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
//
1515
//===----------------------------------------------------------------------===//
1616

17-
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(_WIN32) || defined(__HAIKU__)
18-
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0
17+
#if defined(__CYGWIN__) || defined(__ANDROID__) || defined(__HAIKU__)
18+
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 0
1919
#else
20-
# define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
20+
#define SWIFT_SUPPORTS_BACKTRACE_REPORTING 1
2121
#endif
2222

2323
#if defined(_WIN32)
2424
#include <mutex>
2525
#endif
2626

27+
#include <stdint.h>
2728
#include <stdio.h>
2829
#include <stdlib.h>
2930
#include <string.h>
30-
#include <stdint.h>
3131
#if defined(_WIN32)
3232
#include <io.h>
3333
#else
@@ -48,9 +48,7 @@
4848
#include <cxxabi.h>
4949
#endif
5050

51-
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
52-
// execinfo.h is not available on Android. Checks in this file ensure that
53-
// fatalError behaves as expected, but without stack traces.
51+
#if __has_include(<execinfo.h>)
5452
#include <execinfo.h>
5553
#endif
5654

@@ -97,7 +95,7 @@ static bool getSymbolNameAddr(llvm::StringRef libraryName, SymbolInfo syminfo,
9795
DWORD dwResult;
9896

9997
{
100-
std::lock_guard<std::mutex> lock(m);
98+
std::lock_guard<std::mutex> lock(mutex);
10199
dwResult = UnDecorateSymbolName(syminfo.symbolName, szUndName,
102100
sizeof(szUndName), dwFlags);
103101
}
@@ -193,8 +191,11 @@ void swift::printCurrentBacktrace(unsigned framesToSkip) {
193191
#if SWIFT_SUPPORTS_BACKTRACE_REPORTING
194192
constexpr unsigned maxSupportedStackDepth = 128;
195193
void *addrs[maxSupportedStackDepth];
196-
194+
#if defined(_WIN32)
195+
int symbolCount = CaptureStackBackTrace(0, maxSupportedStackDepth, addrs, NULL);
196+
#else
197197
int symbolCount = backtrace(addrs, maxSupportedStackDepth);
198+
#endif
198199
for (int i = framesToSkip; i < symbolCount; ++i) {
199200
dumpStackTraceEntry(i - framesToSkip, addrs[i]);
200201
}

0 commit comments

Comments
 (0)