Skip to content

Commit b5461ae

Browse files
committed
[Backtracing] Added JSON crash log option.
Also made it so the `sanitize` option causes the crash logs to not include memory dumps. Fixes #71057 rdar://121430255
1 parent 45277d2 commit b5461ae

File tree

9 files changed

+656
-43
lines changed

9 files changed

+656
-43
lines changed

stdlib/public/RuntimeModule/Backtrace.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,30 @@ public struct Backtrace: CustomStringConvertible, Sendable {
194194
return "..."
195195
}
196196
}
197+
198+
/// A JSON description of this frame, less the surrounding braces.
199+
/// This is useful if you want to add extra information.
200+
@_spi(Internal)
201+
public var jsonBody: String {
202+
switch self {
203+
case let .programCounter(addr):
204+
return "\"kind\": \"programCounter\", \"address\": \"\(addr)\""
205+
case let .returnAddress(addr):
206+
return "\"kind\": \"returnAddress\", \"address\": \"\(addr)\""
207+
case let .asyncResumePoint(addr):
208+
return "\"kind\": \"asyncResumePoint\", \"address\": \"\(addr)\""
209+
case let .omittedFrames(count):
210+
return "\"kind\": \"omittedFrames\", \"count\": \(count)"
211+
case .truncated:
212+
return "\"kind\": \"truncated\""
213+
}
214+
}
215+
216+
/// A JSON description of this frame.
217+
@_spi(Internal)
218+
public var jsonDescription: String {
219+
return "{ \(jsonBody) }"
220+
}
197221
}
198222

199223
/// Represents an image loaded in the process's address space

stdlib/public/RuntimeModule/BacktraceFormatter.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,8 @@ private func untabify(_ s: String, tabWidth: Int = 8) -> String {
416416
/// @param path The path to sanitize.
417417
///
418418
/// @returns A string containing the sanitized path.
419-
private func sanitizePath(_ path: String) -> String {
419+
@_spi(Formatting)
420+
public func sanitizePath(_ path: String) -> String {
420421
#if os(macOS)
421422
return CRCopySanitizedPath(path,
422423
kCRSanitizePathGlobAllTypes

stdlib/public/libexec/swift-backtrace/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(BACKTRACING_COMPILE_FLAGS
3333
set(BACKTRACING_SOURCES
3434
main.swift
3535
AnsiColor.swift
36+
JSON.swift
3637
TargetMacOS.swift
3738
TargetLinux.swift
3839
Themes.swift

0 commit comments

Comments
 (0)