1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#elseif canImport(Musl)
import Musl
#elseif canImport(CRT)
import CRT
#endif
@_spi(Contexts) import Runtime
@_spi(Registers) import Runtime
@_spi(Formatting) import Runtime
@_spi(Internal) import Runtime
@_spi(MemoryReaders) import Runtime
extension SwiftBacktrace {
static func outputJSONCrashLog() {
guard let target = target else {
print("swift-backtrace: unable to get target",
to: &standardError)
return
}
let crashingThread = target.threads[target.crashingThreadNdx]
let description: String
if case let .symbolicated(symbolicated) = crashingThread.backtrace,
let failure = symbolicated.swiftRuntimeFailure {
description = failure
} else {
description = target.signalDescription
}
let architecture: String
switch crashingThread.backtrace {
case let .raw(backtrace):
architecture = backtrace.architecture
case let .symbolicated(backtrace):
architecture = backtrace.architecture
}
write("""
{ \
"timestamp": "\(formatISO8601(now))", \
"kind": "crashReport", \
"description": "\(escapeJSON(description))", \
"faultAddress": "\(hex(target.faultAddress))", \
"platform": "\(escapeJSON(target.images.platform))", \
"architecture": "\(escapeJSON(architecture))"
""")
var mentionedImages = Set<Int>()
var capturedBytes: [UInt64:Array<UInt8>] = [:]
func outputJSONRegister<T: FixedWidthInteger>(
name: String, value: T, first: Bool = false
) {
if !first {
write(", ")
}
write("\"\(name)\": \"\(hex(value))\"")
if let bytes = try? target.reader.fetch(
from: RemoteMemoryReader.Address(value),
count: 16,
as: UInt8.self) {
capturedBytes[UInt64(truncatingIfNeeded: value)] = bytes
}
}
func outputJSONRegister<C: Context>(
name: String, context: C, register: C.Register, first: Bool = false
) {
let value = context.getRegister(register)!
outputJSONRegister(name: name, value: value, first: first)
}
func outputJSONGPRs<C: Context, Rs: Sequence>(_ context: C, range: Rs)
where Rs.Element == C.Register
{
var first = true
for reg in range {
outputJSONRegister(name: "\(reg)", context: context, register: reg,
first: first)
if first {
first = false
}
}
}
func outputJSONRegisterDump(_ context: X86_64Context) {
outputJSONGPRs(context, range: .rax ... .r15)
outputJSONRegister(name: "rip", value: context.programCounter)
outputJSONRegister(name: "rflags", context: context, register: .rflags)
outputJSONRegister(name: "cs", context: context, register: .cs)
outputJSONRegister(name: "fs", context: context, register: .fs)
outputJSONRegister(name: "gs", context: context, register: .gs)
}
func outputJSONRegisterDump(_ context: I386Context) {
outputJSONGPRs(context, range: .eax ... .edi)
outputJSONRegister(name: "eip", value: context.programCounter)
outputJSONRegister(name: "eflags", context: context, register: .eflags)
outputJSONRegister(name: "es", context: context, register: .es)
outputJSONRegister(name: "cs", context: context, register: .cs)
outputJSONRegister(name: "ss", context: context, register: .ss)
outputJSONRegister(name: "ds", context: context, register: .ds)
outputJSONRegister(name: "fs", context: context, register: .fs)
outputJSONRegister(name: "gs", context: context, register: .gs)
}
func outputJSONRegisterDump(_ context: ARM64Context) {
outputJSONGPRs(context, range: .x0 ..< .x29)
outputJSONRegister(name: "fp", context: context, register: .x29)
outputJSONRegister(name: "lr", context: context, register: .x30)
outputJSONRegister(name: "sp", context: context, register: .sp)
outputJSONRegister(name: "pc", context: context, register: .pc)
}
func outputJSONRegisterDump(_ context: ARMContext) {
outputJSONGPRs(context, range: .r0 ... .r10)
outputJSONRegister(name: "fp", context: context, register: .r11)
outputJSONRegister(name: "ip", context: context, register: .r12)
outputJSONRegister(name: "sp", context: context, register: .r13)
outputJSONRegister(name: "lr", context: context, register: .r14)
outputJSONRegister(name: "pc", context: context, register: .r15)
}
func outputJSONThread(ndx: Int, thread: TargetThread) {
write("{ ")
if !thread.name.isEmpty {
write("\"name\": \"\(escapeJSON(thread.name))\", ")
}
if thread.id == target.crashingThread {
write(#""crashed": true, "#)
}
if args.registers! == .all || thread.id == target.crashingThread {
if let context = thread.context {
write(#""registers": {"#)
outputJSONRegisterDump(context)
write(" }, ")
}
}
write(#""frames": ["#)
var first = true
switch thread.backtrace {
case let .raw(backtrace):
for frame in backtrace.frames {
if first {
first = false
} else {
write(",")
}
write(" { \(frame.jsonBody) }")
}
case let .symbolicated(backtrace):
for frame in backtrace.frames {
if first {
first = false
} else {
write(",")
}
write(" { ")
write(frame.captured.jsonBody)
if frame.inlined {
write(#", "inlined": true"#)
}
if frame.isSwiftRuntimeFailure {
write(#", "runtimeFailure": true"#)
}
if frame.isSwiftThunk {
write(#", "thunk": true"#)
}
if frame.isSystem {
write(#", "system": true"#)
}
if let symbol = frame.symbol {
write("""
, "symbol": "\(escapeJSON(symbol.rawName))"\
, "offset": \(symbol.offset)
""")
if args.demangle {
let formattedOffset: String
if symbol.offset > 0 {
formattedOffset = " + \(symbol.offset)"
} else if symbol.offset < 0 {
formattedOffset = " - \(symbol.offset)"
} else {
formattedOffset = ""
}
write("""
, "description": \"\(escapeJSON(symbol.name))\(formattedOffset)\"
""")
}
if symbol.imageIndex >= 0 {
write(", \"image\": \"\(symbol.imageName)\"")
}
if var sourceLocation = symbol.sourceLocation {
if args.sanitize ?? false {
sourceLocation.path = sanitizePath(sourceLocation.path)
}
write(#", "sourceLocation": { "#)
write("""
"file": "\(escapeJSON(sourceLocation.path))", \
"line": \(sourceLocation.line), \
"column": \(sourceLocation.column)
""")
write(" }")
}
}
write(" }")
}
}
write(" ] ")
write("}")
if args.showImages! == .mentioned {
switch thread.backtrace {
case let .raw(backtrace):
for frame in backtrace.frames {
let address = frame.adjustedProgramCounter
if let imageNdx = target.images.firstIndex(
where: { address >= $0.baseAddress
&& address < $0.endOfText }
) {
mentionedImages.insert(imageNdx)
}
}
case let .symbolicated(backtrace):
for frame in backtrace.frames {
if let symbol = frame.symbol, symbol.imageIndex >= 0 {
mentionedImages.insert(symbol.imageIndex)
}
}
}
}
}
write(#", "threads": [ "#)
if args.threads! {
var first = true
for (ndx, thread) in target.threads.enumerated() {
if first {
first = false
} else {
write(", ")
}
outputJSONThread(ndx: ndx, thread: thread)
}
} else {
outputJSONThread(ndx: target.crashingThreadNdx,
thread: crashingThread)
}
write(" ]")
if !args.threads! && target.threads.count > 1 {
write(", \"omittedThreads\": \(target.threads.count - 1)")
}
if !capturedBytes.isEmpty && !(args.sanitize ?? false) {
write(#", "capturedMemory": {"#)
var first = true
for (address, bytes) in capturedBytes {
let formattedBytes = bytes
.map{ hex($0, withPrefix: false) }
.joined(separator: "")
if first {
first = false
} else {
write(",")
}
write(" \"\(hex(address))\": \"\(formattedBytes)\"")
}
write(" }")
}
func outputJSONImage(_ image: Backtrace.Image, first: Bool) {
if !first {
write(", ")
}
write("{ ")
if let name = image.name {
write("\"name\": \"\(escapeJSON(name))\", ")
}
if let bytes = image.uniqueID {
let buildID = hex(bytes)
write("\"buildId\": \"\(buildID)\", ")
}
if var path = image.path {
if args.sanitize ?? false {
path = sanitizePath(path)
}
write("\"path\": \"\(path)\", ")
}
write("""
"baseAddress": "\(image.baseAddress)", \
"endOfText": "\(image.endOfText)"
""")
write(" }")
}
switch args.showImages! {
case .none:
break
case .mentioned:
let images = mentionedImages.sorted().map{ target.images[$0] }
let omitted = target.images.count - images.count
write(", \"omittedImages\": \(omitted), \"images\": [ ")
var first = true
for image in images {
outputJSONImage(image, first: first)
if first {
first = false
}
}
write(" ] ")
case .all:
write(#", "images": [ "#)
var first = true
for image in target.images {
outputJSONImage(image, first: first)
if first {
first = false
}
}
write(" ] ")
}
let secs = Double(backtraceDuration.tv_sec)
+ 1.0e-9 * Double(backtraceDuration.tv_nsec)
write(", \"backtraceTime\": \(secs) ")
writeln("}")
}
}
|