Skip to content

Commit 3322551

Browse files
committed
[Backtracing] Add an option to output to stderr.
In CI, it would be better if the backtraces went to stderr rather than stdout, so provide an option for that. rdar://107192120
1 parent 7abb1e5 commit 3322551

File tree

7 files changed

+230
-104
lines changed

7 files changed

+230
-104
lines changed

docs/Backtracing.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ follows:
9696
| | | only has effect on platforms that have a symbol |
9797
| | | cache that can be controlled by the runtime. |
9898
+-----------------+---------+--------------------------------------------------+
99+
| output-to | stdout | Set to ``stderr`` to send the backtrace to the |
100+
| | | standard error instead of standard output. This |
101+
| | | may be useful in some CI systems. |
102+
+-----------------+---------+--------------------------------------------------+
99103
| swift-backtrace | | If specified, gives the full path to the |
100104
| | | swift-backtrace binary to use for crashes. |
101105
| | | Otherwise, Swift will locate the binary relative |

include/swift/Runtime/Backtrace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,11 @@ enum class SanitizePaths {
9797
On = 1
9898
};
9999

100+
enum class OutputTo {
101+
Stdout = 0,
102+
Stderr = 2,
103+
};
104+
100105
struct BacktraceSettings {
101106
UnwindAlgorithm algorithm;
102107
OnOffTty enabled;
@@ -112,6 +117,7 @@ struct BacktraceSettings {
112117
SanitizePaths sanitize;
113118
Preset preset;
114119
bool cache;
120+
OutputTo outputTo;
115121
const char *swiftBacktracePath;
116122
};
117123

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

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class Target {
146146
if let parentTask = Self.getParentTask() {
147147
task = parentTask
148148
} else {
149-
print("swift-backtrace: couldn't fetch parent task")
149+
print("swift-backtrace: couldn't fetch parent task", to: &standardError)
150150
exit(1)
151151
}
152152

@@ -158,7 +158,7 @@ class Target {
158158
do {
159159
crashInfo = try reader.fetch(from: crashInfoAddr, as: CrashInfo.self)
160160
} catch {
161-
print("swift-backtrace: unable to fetch crash info.")
161+
print("swift-backtrace: unable to fetch crash info.", to: &standardError)
162162
exit(1)
163163
}
164164

@@ -168,7 +168,7 @@ class Target {
168168

169169
guard let mctx: MContext = try? reader.fetch(from: crashInfo.mctx,
170170
as: MContext.self) else {
171-
print("swift-backtrace: unable to fetch mcontext.")
171+
print("swift-backtrace: unable to fetch mcontext.", to: &standardError)
172172
exit(1)
173173
}
174174

@@ -188,12 +188,13 @@ class Target {
188188
&threadCount)
189189

190190
if kr != KERN_SUCCESS {
191-
print("swift-backtrace: failed to enumerate threads - \(kr)")
191+
print("swift-backtrace: failed to enumerate threads - \(kr)",
192+
to: &standardError)
192193
exit(1)
193194
}
194195

195196
guard let ports = threadPorts else {
196-
print("swift-backtrace: thread array is nil")
197+
print("swift-backtrace: thread array is nil", to: &standardError)
197198
exit(1)
198199
}
199200

@@ -202,12 +203,13 @@ class Target {
202203
var kr = mach_thread_info(ports[Int(ndx)], THREAD_IDENTIFIER_INFO,
203204
&threadIdInfo)
204205
if kr != KERN_SUCCESS {
205-
print("swift-backtrace: unable to get thread info for thread \(ndx) - \(kr)")
206+
print("swift-backtrace: unable to get thread info for thread \(ndx) - \(kr)",
207+
to: &standardError)
206208
exit(1)
207209
}
208210

209211
guard let info = threadIdInfo else {
210-
print("swift-backtrace: thread info is nil")
212+
print("swift-backtrace: thread info is nil", to: &standardError)
211213
exit(1)
212214
}
213215

@@ -221,7 +223,8 @@ class Target {
221223
if kr == KERN_SUCCESS {
222224
threadName = extInfo.pth_swiftName
223225
} else {
224-
print("unable to fetch ext info \(kr)")
226+
print("swift-backtrace: unable to fetch ext info \(kr)",
227+
to: &standardError)
225228
threadName = ""
226229
}
227230

@@ -243,14 +246,16 @@ class Target {
243246
using: reader,
244247
limit: limit,
245248
top: top) else {
246-
print("unable to capture backtrace from context for thread \(ndx)")
249+
print("swift-backtrace: unable to capture backtrace from context for thread \(ndx)",
250+
to: &standardError)
247251
exit(1)
248252
}
249253

250254
guard let symbolicated = backtrace.symbolicated(with: images,
251255
sharedCacheInfo: sharedCacheInfo,
252256
useSymbolCache: cache) else {
253-
print("unable to symbolicate backtrace from context for thread \(ndx)")
257+
print("unable to symbolicate backtrace from context for thread \(ndx)",
258+
to: &standardError)
254259
exit(1)
255260
}
256261

@@ -273,14 +278,16 @@ class Target {
273278
using: reader,
274279
limit: limit,
275280
top: top) else {
276-
print("unable to capture backtrace from context for thread \(ndx)")
281+
print("swift-backtrace: unable to capture backtrace from context for thread \(ndx)",
282+
to: &standardError)
277283
continue
278284
}
279285

280286
guard let symbolicated = backtrace.symbolicated(with: images,
281287
sharedCacheInfo: sharedCacheInfo,
282288
useSymbolCache: cache) else {
283-
print("unable to symbolicate backtrace from context for thread \(ndx)")
289+
print("swift-backtrace: unable to symbolicate backtrace from context for thread \(ndx)",
290+
to: &standardError)
284291
continue
285292
}
286293

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,19 @@ internal func spawn(_ path: String, args: [String]) throws {
139139
}
140140
}
141141

142+
struct CFileStream: TextOutputStream {
143+
var fp: UnsafeMutablePointer<FILE>
144+
145+
public func write(_ string: String) {
146+
fputs(string, fp)
147+
}
148+
149+
public func flush() {
150+
fflush(fp)
151+
}
152+
}
153+
154+
var standardOutput = CFileStream(fp: stdout)
155+
var standardError = CFileStream(fp: stderr)
156+
142157
#endif // os(macOS)

0 commit comments

Comments
 (0)