Skip to content

Commit 65a6aae

Browse files
committed
[Backtracing] Add swift-backtrace to the build.
This is Swift's external backtracer. It's written in Swift, and it's responsible for generating nice backtraces when Swift programs crash. It makes use of the new `_Backtracing` library, which is also (mostly, aside from some assembly language) implemented in Swift. rdar://103442000
1 parent 44783e7 commit 65a6aae

15 files changed

+2611
-0
lines changed

stdlib/public/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,5 @@ if(SWIFT_BUILD_SDK_OVERLAY)
179179
add_subdirectory(Windows)
180180
endif()
181181
endif()
182+
183+
add_subdirectory(libexec)

stdlib/public/libexec/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(swift-backtrace)

stdlib/public/libexec/README.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
What is this?
2+
=============
3+
4+
The libexec directory contains the source code for auxiliary executables that
5+
ship with the Swift runtime, as opposed to being part of the toolchain.
6+
7+
On UNIX-like platforms, if the runtime is installed in /usr/lib/swift (for
8+
instance), these are things that you would expect to find in /usr/libexec/swift.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
//===--- AnsiColor.swift - ANSI formatting control codes ------------------===//
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+
// Provides ANSI support via Swift string interpolation.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
17+
enum AnsiColor {
18+
case normal
19+
case black
20+
case red
21+
case green
22+
case yellow
23+
case blue
24+
case magenta
25+
case cyan
26+
case white
27+
case gray
28+
case brightRed
29+
case brightGreen
30+
case brightYellow
31+
case brightBlue
32+
case brightMagenta
33+
case brightCyan
34+
case brightWhite
35+
case rgb(r: Int, g: Int, b: Int)
36+
case grayscale(Int)
37+
38+
var foregroundCode: String {
39+
switch self {
40+
case .normal: return "39"
41+
case .black: return "30"
42+
case .red: return "31"
43+
case .green: return "32"
44+
case .yellow: return "33"
45+
case .blue: return "34"
46+
case .cyan: return "35"
47+
case .magenta: return "36"
48+
case .white: return "37"
49+
case .gray: return "90"
50+
case .brightRed: return "91"
51+
case .brightGreen: return "92"
52+
case .brightYellow: return "93"
53+
case .brightBlue: return "94"
54+
case .brightCyan: return "95"
55+
case .brightMagenta: return "96"
56+
case .brightWhite: return "97"
57+
case let .rgb(r, g, b):
58+
let ndx = 16 + 36 * r + 6 * g + b
59+
return "38;5;\(ndx)"
60+
case let .grayscale(g):
61+
let ndx = 232 + g
62+
return "38;5;\(ndx)"
63+
}
64+
}
65+
66+
var backgroundCode: String {
67+
switch self {
68+
case .normal: return "49"
69+
case .black: return "40"
70+
case .red: return "41"
71+
case .green: return "42"
72+
case .yellow: return "43"
73+
case .blue: return "44"
74+
case .cyan: return "45"
75+
case .magenta: return "46"
76+
case .white: return "47"
77+
case .gray: return "100"
78+
case .brightRed: return "101"
79+
case .brightGreen: return "102"
80+
case .brightYellow: return "103"
81+
case .brightBlue: return "104"
82+
case .brightCyan: return "105"
83+
case .brightMagenta: return "106"
84+
case .brightWhite: return "107"
85+
case let .rgb(r, g, b):
86+
let ndx = 16 + 36 * r + 6 * g + b
87+
return "48;5;\(ndx)"
88+
case let .grayscale(g):
89+
let ndx = 232 + g
90+
return "48;5;\(ndx)"
91+
}
92+
}
93+
}
94+
95+
enum AnsiWeight {
96+
case normal
97+
case bold
98+
case faint
99+
100+
var code: String {
101+
switch self {
102+
case .normal: return "22"
103+
case .bold: return "1"
104+
case .faint: return "2"
105+
}
106+
}
107+
}
108+
109+
enum AnsiAttribute {
110+
case fg(AnsiColor)
111+
case bg(AnsiColor)
112+
case weight(AnsiWeight)
113+
case inverse(Bool)
114+
}
115+
116+
extension DefaultStringInterpolation {
117+
mutating func appendInterpolation(ansi attrs: AnsiAttribute...) {
118+
var code = "\u{1b}["
119+
var first = true
120+
for attr in attrs {
121+
if first {
122+
first = false
123+
} else {
124+
code += ";"
125+
}
126+
127+
switch attr {
128+
case let .fg(color):
129+
code += color.foregroundCode
130+
case let .bg(color):
131+
code += color.backgroundCode
132+
case let .weight(weight):
133+
code += weight.code
134+
case let .inverse(enabled):
135+
if enabled {
136+
code += "7"
137+
} else {
138+
code += "27"
139+
}
140+
}
141+
}
142+
code += "m"
143+
144+
appendInterpolation(code)
145+
}
146+
147+
mutating func appendInterpolation(fg: AnsiColor) {
148+
return appendInterpolation(ansi: .fg(fg))
149+
}
150+
mutating func appendInterpolation(bg: AnsiColor) {
151+
return appendInterpolation(ansi: .bg(bg))
152+
}
153+
mutating func appendInterpolation(weight: AnsiWeight) {
154+
return appendInterpolation(ansi: .weight(weight))
155+
}
156+
mutating func appendInterpolation(inverse: Bool) {
157+
return appendInterpolation(ansi: .inverse(inverse))
158+
}
159+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
add_swift_target_executable(swift-backtrace BUILD_WITH_STDLIB
2+
main.swift
3+
AnsiColor.swift
4+
Target.swift
5+
Themes.swift
6+
Utils.swift
7+
8+
SWIFT_MODULE_DEPENDS
9+
_Backtracing
10+
11+
INSTALL_IN_COMPONENT stdlib
12+
COMPILE_FLAGS -parse-as-library)

0 commit comments

Comments
 (0)