Skip to content

Commit 861fd2f

Browse files
authored
Merge pull request #66406 from al45tair/eng/PR-110371557
[Backtracing] Use stderr by default unless interactive.
2 parents 274a43b + 646c221 commit 861fd2f

File tree

10 files changed

+40
-17
lines changed

10 files changed

+40
-17
lines changed

include/swift/Runtime/Backtrace.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ enum class SanitizePaths {
109109
};
110110

111111
enum class OutputTo {
112+
Auto = -1,
112113
Stdout = 0,
113114
Stderr = 2,
114115
};

stdlib/public/runtime/Backtrace.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ SWIFT_RUNTIME_STDLIB_INTERNAL BacktraceSettings _swift_backtraceSettings = {
123123
true,
124124

125125
// outputTo,
126-
OutputTo::Stdout,
126+
OutputTo::Auto,
127127

128128
// swiftBacktracePath
129129
NULL,
@@ -322,6 +322,13 @@ BacktraceInitializer::BacktraceInitializer() {
322322
_swift_backtraceSettings.preset = Preset::Full;
323323
}
324324

325+
if (_swift_backtraceSettings.outputTo == OutputTo::Auto) {
326+
if (_swift_backtraceSettings.interactive == OnOffTty::On)
327+
_swift_backtraceSettings.outputTo = OutputTo::Stdout;
328+
else
329+
_swift_backtraceSettings.outputTo = OutputTo::Stderr;
330+
}
331+
325332
#if !defined(SWIFT_RUNTIME_FIXED_BACKTRACER_PATH)
326333
if (_swift_backtraceSettings.enabled == OnOffTty::On
327334
&& !_swift_backtraceSettings.swiftBacktracePath) {
@@ -648,7 +655,9 @@ _swift_processBacktracingSetting(llvm::StringRef key,
648655
} else if (key.equals_insensitive("cache")) {
649656
_swift_backtraceSettings.cache = parseBoolean(value);
650657
} else if (key.equals_insensitive("output-to")) {
651-
if (value.equals_insensitive("stdout"))
658+
if (value.equals_insensitive("auto"))
659+
_swift_backtraceSettings.outputTo = OutputTo::Auto;
660+
else if (value.equals_insensitive("stdout"))
652661
_swift_backtraceSettings.outputTo = OutputTo::Stdout;
653662
else if (value.equals_insensitive("stderr"))
654663
_swift_backtraceSettings.outputTo = OutputTo::Stderr;

stdlib/public/runtime/CrashHandlerLinux.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ const char *backtracer_argv[] = {
617617
"preset", // 26
618618
"--cache", // 27
619619
"true", // 28
620+
"--output-to", // 29
621+
"stdout", // 30
620622
NULL
621623
};
622624

@@ -769,6 +771,16 @@ run_backtracer(int memserver_fd)
769771
break;
770772
}
771773

774+
switch (_swift_backtraceSettings.outputTo) {
775+
case OutputTo::Stdout:
776+
backtracer_argv[30] = "stdout";
777+
break;
778+
case OutputTo::Auto: // Shouldn't happen, but if it does pick stderr
779+
case OutputTo::Stderr:
780+
backtracer_argv[30] = "stderr";
781+
break;
782+
}
783+
772784
backtracer_argv[28] = trueOrFalse(_swift_backtraceSettings.cache);
773785

774786
format_unsigned(_swift_backtraceSettings.timeout, timeout_buf);

stdlib/public/runtime/CrashHandlerMacOS.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,7 @@ run_backtracer()
428428
case OutputTo::Stdout:
429429
backtracer_argv[30] = "stdout";
430430
break;
431+
case OutputTo::Auto: // Shouldn't happen, but if it does pick stderr
431432
case OutputTo::Stderr:
432433
backtracer_argv[30] = "stderr";
433434
break;

test/Backtracing/Crash.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
// RUN: %target-codesign %t/CrashNoDebug
88
// RUN: %target-codesign %t/CrashOpt
99
// RUN: %target-codesign %t/CrashOptNoDebug
10-
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/Crash || true) | %FileCheck %s
11-
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/Crash || true) | %FileCheck %s --check-prefix FRIENDLY
12-
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashNoDebug || true) | %FileCheck %s --check-prefix NODEBUG
13-
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashOpt || true) | %FileCheck %s --check-prefix OPTIMIZED
14-
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashOptNoDebug || true) | %FileCheck %s --check-prefix OPTNODEBUG
10+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/Crash 2>&1 || true) | %FileCheck %s
11+
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/Crash 2>&1 || true) | %FileCheck %s --check-prefix FRIENDLY
12+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashNoDebug 2>&1 || true) | %FileCheck %s --check-prefix NODEBUG
13+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashOpt 2>&1 || true) | %FileCheck %s --check-prefix OPTIMIZED
14+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashOptNoDebug 2>&1 || true) | %FileCheck %s --check-prefix OPTNODEBUG
1515

1616
// UNSUPPORTED: use_os_stdlib
1717
// UNSUPPORTED: back_deployment_runtime

test/Backtracing/CrashAsync.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// Demangling is disabled for now because older macOS can't demangle async
66
// function names. We test demangling elsewhere, so this is no big deal.
77

8-
// RUN: (env SWIFT_BACKTRACE=enable=yes,demangle=no,cache=no %target-run %t/CrashAsync || true) | %FileCheck %s
9-
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,demangle=no,cache=no %target-run %t/CrashAsync || true) | %FileCheck %s --check-prefix FRIENDLY
8+
// RUN: (env SWIFT_BACKTRACE=enable=yes,demangle=no,cache=no %target-run %t/CrashAsync 2>&1 || true) | %FileCheck %s
9+
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,demangle=no,cache=no %target-run %t/CrashAsync 2>&1 || true) | %FileCheck %s --check-prefix FRIENDLY
1010

1111
// UNSUPPORTED: use_os_stdlib
1212
// UNSUPPORTED: back_deployment_runtime

test/Backtracing/CrashWithThunk.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift %s -parse-as-library -Onone -g -o %t/CrashWithThunk
33
// RUN: %target-codesign %t/CrashWithThunk
4-
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashWithThunk || true) | %FileCheck %s
5-
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/CrashWithThunk || true) | %FileCheck %s --check-prefix FRIENDLY
4+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/CrashWithThunk 2>&1 || true) | %FileCheck %s
5+
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/CrashWithThunk 2>&1 || true) | %FileCheck %s --check-prefix FRIENDLY
66

77
// UNSUPPORTED: use_os_stdlib
88
// UNSUPPORTED: back_deployment_runtime

test/Backtracing/Overflow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift %s -parse-as-library -Onone -g -o %t/Overflow
33
// RUN: %target-codesign %t/Overflow
4-
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/Overflow || true) | %FileCheck %s
5-
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/Overflow || true) | %FileCheck %s --check-prefix FRIENDLY
4+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/Overflow 2>&1 || true) | %FileCheck %s
5+
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/Overflow 2>&1 || true) | %FileCheck %s --check-prefix FRIENDLY
66

77
// UNSUPPORTED: use_os_stdlib
88
// UNSUPPORTED: back_deployment_runtime

test/Backtracing/StackOverflow.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// RUN: %empty-directory(%t)
22
// RUN: %target-build-swift %s -parse-as-library -Onone -g -o %t/StackOverflow
33
// RUN: %target-codesign %t/StackOverflow
4-
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/StackOverflow || true) | %FileCheck %s
5-
// RUN: (env SWIFT_BACKTRACE=limit=17,top=5,enable=yes,cache=no %target-run %t/StackOverflow || true) | %FileCheck %s --check-prefix LIMITED
6-
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/StackOverflow || true) | %FileCheck %s --check-prefix FRIENDLY
4+
// RUN: (env SWIFT_BACKTRACE=enable=yes,cache=no %target-run %t/StackOverflow 2>&1|| true) | %FileCheck %s
5+
// RUN: (env SWIFT_BACKTRACE=limit=17,top=5,enable=yes,cache=no %target-run %t/StackOverflow 2>&1 || true) | %FileCheck %s --check-prefix LIMITED
6+
// RUN: (env SWIFT_BACKTRACE=preset=friendly,enable=yes,cache=no %target-run %t/StackOverflow 2>&1 || true) | %FileCheck %s --check-prefix FRIENDLY
77

88
// UNSUPPORTED: use_os_stdlib
99
// UNSUPPORTED: back_deployment_runtime

test/lit.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ if backtracing is not None:
494494

495495
backtrace_on_crash = lit_config.params.get('backtrace_on_crash', None)
496496
if backtrace_on_crash is not None:
497-
config.environment['SWIFT_BACKTRACE'] = 'enable=on,output-to=stderr'
497+
config.environment['SWIFT_BACKTRACE'] = 'enable=on'
498498

499499
config.available_features.add('lld_lto')
500500

0 commit comments

Comments
 (0)