Skip to content

Commit 8a29657

Browse files
committed
[Runtime] [Darwin] Add code to trigger the external backtracer.
When a Swift program crashes, we should catch the crash and execute the external backtracer. rdar://105391747
1 parent 1c86433 commit 8a29657

File tree

6 files changed

+1355
-0
lines changed

6 files changed

+1355
-0
lines changed

include/swift/Runtime/Backtrace.h

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
//===--- Backtrace.cpp - Swift crash catching and backtracing support ---- ===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Definitions relating to backtracing.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
#ifndef SWIFT_RUNTIME_BACKTRACE_H
18+
#define SWIFT_RUNTIME_BACKTRACE_H
19+
20+
#include "swift/Runtime/Config.h"
21+
22+
#include "swift/shims/Visibility.h"
23+
#include "swift/shims/_SwiftBacktracing.h"
24+
25+
#include <inttypes.h>
26+
27+
#ifdef __cplusplus
28+
namespace swift {
29+
namespace runtime {
30+
namespace backtrace {
31+
#endif
32+
33+
#ifdef _WIN32
34+
typedef wchar_t ArgChar;
35+
typedef DWORD ErrorCode;
36+
#else
37+
typedef char ArgChar;
38+
typedef int ErrorCode;
39+
#endif
40+
41+
SWIFT_RUNTIME_STDLIB_INTERNAL ErrorCode _swift_installCrashHandler();
42+
43+
SWIFT_RUNTIME_STDLIB_INTERNAL bool _swift_spawnBacktracer(const ArgChar * const *argv);
44+
45+
enum class UnwindAlgorithm {
46+
Auto = 0,
47+
Fast = 1,
48+
Precise = 2
49+
};
50+
51+
enum class OnOffTty {
52+
Off = 0,
53+
On = 1,
54+
TTY = 2
55+
};
56+
57+
enum class Preset {
58+
Auto = -1,
59+
Friendly = 0,
60+
Medium = 1,
61+
Full = 2
62+
};
63+
64+
enum class ThreadsToShow {
65+
Preset = -1,
66+
All = 0,
67+
Crashed = 1
68+
};
69+
70+
enum class RegistersToShow {
71+
Preset = -1,
72+
None = 0,
73+
All = 1,
74+
Crashed = 2
75+
};
76+
77+
enum class ImagesToShow {
78+
Preset = -1,
79+
None = 0,
80+
All = 1,
81+
Mentioned = 2
82+
};
83+
84+
enum class SanitizePaths {
85+
Preset = -1,
86+
Off = 0,
87+
On = 1
88+
};
89+
90+
struct BacktraceSettings {
91+
UnwindAlgorithm algorithm;
92+
OnOffTty enabled;
93+
bool demangle;
94+
OnOffTty interactive;
95+
OnOffTty color;
96+
unsigned timeout;
97+
ThreadsToShow threads;
98+
RegistersToShow registers;
99+
ImagesToShow images;
100+
unsigned limit;
101+
unsigned top;
102+
SanitizePaths sanitize;
103+
Preset preset;
104+
const char *swiftBacktracePath;
105+
};
106+
107+
SWIFT_RUNTIME_STDLIB_INTERNAL BacktraceSettings _swift_backtraceSettings;
108+
109+
SWIFT_RUNTIME_STDLIB_SPI SWIFT_CC(swift) bool _swift_isThunkFunction(const char *mangledName);
110+
111+
#ifdef __cplusplus
112+
} // namespace backtrace
113+
} // namespace runtime
114+
} // namespace swift
115+
#endif
116+
117+
#endif // SWIFT_RUNTIME_BACKTRACE_H

include/swift/Runtime/Config.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,34 @@ swift_auth_code(T value, unsigned extra) {
481481
#endif
482482
}
483483

484+
/// Does this platform support backtrace-on-crash?
485+
#ifdef __APPLE__
486+
# include <TargetConditionals.h>
487+
# if TARGET_OS_OSX
488+
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 1
489+
# define SWIFT_BACKTRACE_SECTION "__DATA,swift5_backtrace"
490+
# else
491+
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 0
492+
# endif
493+
#elif defined(_WIN32)
494+
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 0
495+
# define SWIFT_BACKTRACE_SECTION ".sw5bckt"
496+
#elif defined(__linux__)
497+
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 0
498+
# define SWIFT_BACKTRACE_SECTION "swift5_backtrace"
499+
#else
500+
# define SWIFT_BACKTRACE_ON_CRASH_SUPPORTED 0
501+
#endif
502+
503+
/// What is the system page size?
504+
#if defined(__APPLE__) && defined(__arm64__)
505+
// Apple Silicon systems use a 16KB page size
506+
#define SWIFT_PAGE_SIZE 16384
507+
#else
508+
// Everything else uses 4KB pages
509+
#define SWIFT_PAGE_SIZE 4096
510+
#endif
511+
484512
#endif
485513

486514
#endif // SWIFT_RUNTIME_CONFIG_H

0 commit comments

Comments
 (0)