Skip to content

[Linux] Disable fatalError() backtraces when the runtime backtracer is active. #69570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/Runtime/Backtrace.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ struct BacktraceSettings {

SWIFT_RUNTIME_STDLIB_INTERNAL BacktraceSettings _swift_backtraceSettings;

inline bool _swift_backtrace_isEnabled() {
return _swift_backtraceSettings.enabled == OnOffTty::On;
}

SWIFT_RUNTIME_STDLIB_SPI
bool _swift_backtrace_isThunkFunction(const char *mangledName);

Expand Down
1 change: 1 addition & 0 deletions stdlib/public/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ add_swift_target_library(swiftRuntime OBJECT_LIBRARY
${swift_runtime_threading_sources}
C_COMPILE_FLAGS
${swift_runtime_library_compile_flags}
${swift_enable_backtracing}
LINK_FLAGS ${swift_runtime_linker_flags}
SWIFT_COMPILE_FLAGS ${SWIFT_STANDARD_LIBRARY_SWIFT_FLAGS}
INSTALL_IN_COMPONENT never_install)
Expand Down
11 changes: 9 additions & 2 deletions stdlib/public/runtime/Errors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "ImageInspection.h"
#include "swift/Demangling/Demangle.h"
#include "swift/Runtime/Backtrace.h"
#include "swift/Runtime/Debug.h"
#include "swift/Runtime/Portability.h"
#include "swift/Runtime/Win32.h"
Expand Down Expand Up @@ -353,7 +354,13 @@ void swift::swift_reportError(uint32_t flags,
const char *message) {
#if defined(__APPLE__) && NDEBUG
flags &= ~FatalErrorFlags::ReportBacktrace;
#elif SWIFT_ENABLE_BACKTRACING
// Disable fatalError backtraces if the backtracer is enabled
if (runtime::backtrace::_swift_backtrace_isEnabled()) {
flags &= ~FatalErrorFlags::ReportBacktrace;
}
#endif

reportNow(flags, message);
reportOnCrash(flags, message);
}
Expand Down Expand Up @@ -388,9 +395,9 @@ swift::warningv(uint32_t flags, const char *format, va_list args)
#pragma GCC diagnostic ignored "-Wuninitialized"
swift_vasprintf(&log, format, args);
#pragma GCC diagnostic pop

reportNow(flags, log);

free(log);
}

Expand Down
2 changes: 2 additions & 0 deletions test/Backtracing/FatalError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct FatalError {
}
}

// CHECK-NOT: Current stack trace:

// CHECK: *** Program crashed: {{Illegal instruction|System trap}} at 0x{{[0-9a-f]+}} ***

// CHECK: Thread 0 {{(".*" )?}}crashed:
Expand Down
2 changes: 1 addition & 1 deletion test/Runtime/backtrace.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -o %t/a.out
// RUN: %{python} %S/../Inputs/not.py "%target-run %t/a.out" 2>&1 | %{python} %utils/backtrace-check
// RUN: env SWIFT_BACKTRACE=enable=no %{python} %S/../Inputs/not.py "%target-run %t/a.out" 2>&1 | %{python} %utils/backtrace-check

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