Skip to content

Commit 3d9533d

Browse files
authored
Merge pull request #69570 from al45tair/eng/PR-117470489
[Linux] Disable fatalError() backtraces when the runtime backtracer is active.
2 parents 9f33d3e + 003b455 commit 3d9533d

File tree

5 files changed

+17
-3
lines changed

5 files changed

+17
-3
lines changed

include/swift/Runtime/Backtrace.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ struct BacktraceSettings {
135135

136136
SWIFT_RUNTIME_STDLIB_INTERNAL BacktraceSettings _swift_backtraceSettings;
137137

138+
inline bool _swift_backtrace_isEnabled() {
139+
return _swift_backtraceSettings.enabled == OnOffTty::On;
140+
}
141+
138142
SWIFT_RUNTIME_STDLIB_SPI
139143
bool _swift_backtrace_isThunkFunction(const char *mangledName);
140144

stdlib/public/runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY
151151
${swift_runtime_threading_sources}
152152
C_COMPILE_FLAGS
153153
${swift_runtime_library_compile_flags}
154+
${swift_enable_backtracing}
154155
LINK_FLAGS ${swift_runtime_linker_flags}
155156
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
156157
INSTALL_IN_COMPONENT never_install)

stdlib/public/runtime/Errors.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
#include "ImageInspection.h"
3636
#include "swift/Demangling/Demangle.h"
37+
#include "swift/Runtime/Backtrace.h"
3738
#include "swift/Runtime/Debug.h"
3839
#include "swift/Runtime/Portability.h"
3940
#include "swift/Runtime/Win32.h"
@@ -353,7 +354,13 @@ void swift::swift_reportError(uint32_t flags,
353354
const char *message) {
354355
#if defined(__APPLE__) && NDEBUG
355356
flags &= ~FatalErrorFlags::ReportBacktrace;
357+
#elif SWIFT_ENABLE_BACKTRACING
358+
// Disable fatalError backtraces if the backtracer is enabled
359+
if (runtime::backtrace::_swift_backtrace_isEnabled()) {
360+
flags &= ~FatalErrorFlags::ReportBacktrace;
361+
}
356362
#endif
363+
357364
reportNow(flags, message);
358365
reportOnCrash(flags, message);
359366
}
@@ -388,9 +395,9 @@ swift::warningv(uint32_t flags, const char *format, va_list args)
388395
#pragma GCC diagnostic ignored "-Wuninitialized"
389396
swift_vasprintf(&log, format, args);
390397
#pragma GCC diagnostic pop
391-
398+
392399
reportNow(flags, log);
393-
400+
394401
free(log);
395402
}
396403

test/Backtracing/FatalError.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ struct FatalError {
3737
}
3838
}
3939

40+
// CHECK-NOT: Current stack trace:
41+
4042
// CHECK: *** Program crashed: {{Illegal instruction|System trap}} at 0x{{[0-9a-f]+}} ***
4143

4244
// CHECK: Thread 0 {{(".*" )?}}crashed:

test/Runtime/backtrace.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift %s -o %t/a.out
3-
// RUN: %{python} %S/../Inputs/not.py "%target-run %t/a.out" 2>&1 | %{python} %utils/backtrace-check
3+
// RUN: env SWIFT_BACKTRACE=enable=no %{python} %S/../Inputs/not.py "%target-run %t/a.out" 2>&1 | %{python} %utils/backtrace-check
44

55
// NOTE: not.py is used above instead of "not --crash" because %target-run
66
// doesn't pass through the crash, and `not` may not be available when running

0 commit comments

Comments
 (0)