Skip to content

Commit 2ac8460

Browse files
committed
[Backtracing] Massage the output a little in various cases.
We shouldn't try to erase the message when in non-color mode; mostly that's intended for redirected output scenarios anyway, and in that case we don't really want garbage in the output. Also, improve the failure messages when the backtracer itself goes wrong or can't be executed. Finally, change the behaviour slightly so that *if* we're explicitly enabled, *and* the backtracer path wasn't explicitly specified, *and* we can't find the backtracer, we print a warning on start-up. We don't do that in any other case because we don't want spurious warnings everywhere. rdar://118055527
1 parent 72f8aae commit 2ac8460

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

stdlib/public/libexec/swift-backtrace/main.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,11 +652,11 @@ Generate a backtrace for the parent process.
652652
description = "Program crashed: \(target.signalDescription) at \(hex(target.faultAddress))"
653653
}
654654

655-
// Clear the message written by the crash handler
655+
// Clear (or complete) the message written by the crash handler
656656
if args.color {
657657
write("\r\u{1b}[0K")
658658
} else {
659-
write("\r" + String(repeating:" ", count: 80) + "\r")
659+
write(" done ***\n\n")
660660
}
661661

662662
writeln(theme.crashReason(description))

stdlib/public/runtime/Backtrace.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,7 @@ SWIFT_RUNTIME_STDLIB_INTERNAL BacktraceSettings _swift_backtraceSettings = {
7676
UnwindAlgorithm::Auto,
7777

7878
// enabled
79-
#if TARGET_OS_OSX
80-
OnOffTty::TTY,
81-
#elif defined(__linux__) // || defined(_WIN32)
82-
OnOffTty::On,
83-
#else
84-
OnOffTty::Off,
85-
#endif
79+
OnOffTty::Default,
8680

8781
// demangle
8882
true,
@@ -232,6 +226,7 @@ const char *algorithmToString(UnwindAlgorithm algorithm) {
232226

233227
const char *onOffTtyToString(OnOffTty oot) {
234228
switch (oot) {
229+
case OnOffTty::Default: return "Default";
235230
case OnOffTty::On: return "On";
236231
case OnOffTty::Off: return "Off";
237232
case OnOffTty::TTY: return "TTY";
@@ -279,6 +274,32 @@ BacktraceInitializer::BacktraceInitializer() {
279274
if (backtracing)
280275
_swift_parseBacktracingSettings(backtracing);
281276

277+
#if !defined(SWIFT_RUNTIME_FIXED_BACKTRACER_PATH)
278+
if (!_swift_backtraceSettings.swiftBacktracePath) {
279+
_swift_backtraceSettings.swiftBacktracePath
280+
= swift_copyAuxiliaryExecutablePath("swift-backtrace");
281+
282+
if (!_swift_backtraceSettings.swiftBacktracePath) {
283+
if (_swift_backtraceSettings.enabled == OnOffTty::On) {
284+
swift::warning(0,
285+
"swift runtime: unable to locate swift-backtrace; "
286+
"disabling backtracing.\n");
287+
}
288+
_swift_backtraceSettings.enabled = OnOffTty::Off;
289+
}
290+
}
291+
#endif
292+
293+
if (_swift_backtraceSettings.enabled == OnOffTty::Default) {
294+
#if TARGET_OS_OSX
295+
_swift_backtraceSettings.enabled = OnOffTty::TTY;
296+
#elif defined(__linux__) // || defined(_WIN32)
297+
_swift_backtraceSettings.enabled = OnOffTty::On;
298+
#else
299+
_swift_backtraceSettings.enabled = OnOffTty::Off;
300+
#endif
301+
}
302+
282303
#if !SWIFT_BACKTRACE_ON_CRASH_SUPPORTED
283304
if (_swift_backtraceSettings.enabled != OnOffTty::Off) {
284305
swift::warning(0,
@@ -331,22 +352,6 @@ BacktraceInitializer::BacktraceInitializer() {
331352
_swift_backtraceSettings.outputTo = OutputTo::Stderr;
332353
}
333354

334-
#if !defined(SWIFT_RUNTIME_FIXED_BACKTRACER_PATH)
335-
if (_swift_backtraceSettings.enabled == OnOffTty::On
336-
&& !_swift_backtraceSettings.swiftBacktracePath) {
337-
_swift_backtraceSettings.swiftBacktracePath
338-
= swift_copyAuxiliaryExecutablePath("swift-backtrace");
339-
340-
if (!_swift_backtraceSettings.swiftBacktracePath) {
341-
// Disabled warning for now - rdar://106813646
342-
/* swift::warning(0,
343-
"swift runtime: unable to locate swift-backtrace; "
344-
"disabling backtracing.\n"); */
345-
_swift_backtraceSettings.enabled = OnOffTty::Off;
346-
}
347-
}
348-
#endif
349-
350355
if (_swift_backtraceSettings.enabled == OnOffTty::On) {
351356
// Copy the path to swift-backtrace into swiftBacktracePath, then write
352357
// protect it so that it can't be overwritten easily at runtime. We do
@@ -1007,7 +1012,7 @@ _swift_displayCrashMessage(int signum, const void *pc)
10071012
if (_swift_backtraceSettings.color == OnOffTty::On) {
10081013
intro = "\n💣 \033[91mProgram crashed: ";
10091014
} else {
1010-
intro = "\n*** Program crashed: ";
1015+
intro = "\n*** ";
10111016
}
10121017
write(fd, intro, strlen(intro));
10131018

stdlib/public/runtime/BacktracePrivate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ enum class UnwindAlgorithm {
5151
};
5252

5353
enum class OnOffTty {
54+
Default = -1,
5455
Off = 0,
5556
On = 1,
5657
TTY = 2

stdlib/public/runtime/CrashHandlerLinux.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,8 @@ handle_fatal_signal(int signum,
250250

251251
// Actually start the backtracer
252252
if (!run_backtracer(fd)) {
253-
const char *message = "\n\nBacktracing failed\n";
253+
const char *message = _swift_backtraceSettings.color == OnOffTty::On
254+
? " failed\n\n" : " failed ***\n\n";
254255
if (_swift_backtraceSettings.outputTo == OutputTo::Stderr)
255256
write(STDERR_FILENO, message, strlen(message));
256257
else

stdlib/public/runtime/CrashHandlerMacOS.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,8 @@ handle_fatal_signal(int signum,
248248
/* Start the backtracer; this will suspend the process, so there's no need
249249
to try to suspend other threads from here. */
250250
if (!run_backtracer()) {
251-
const char *message = "\n\nBacktracing failed\n";
251+
const char *message = _swift_backtraceSettings.color == OnOffTty::On
252+
? " failed\n\n" : " failed ***\n\n";
252253
if (_swift_backtraceSettings.outputTo == OutputTo::Stderr)
253254
write(STDERR_FILENO, message, strlen(message));
254255
else

0 commit comments

Comments
 (0)