Skip to content

Commit

Permalink
[Backtracing][WIP] Add some debug to track down problem in Linux CI.
Browse files Browse the repository at this point in the history
  • Loading branch information
al45tair committed Jun 4, 2023
1 parent 7fcd48a commit 4ad370a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
9 changes: 9 additions & 0 deletions stdlib/public/Backtracing/Backtrace.swift
Expand Up @@ -472,7 +472,10 @@ public struct Backtrace: CustomStringConvertible, Sendable {
path = "/proc/self/maps"
}

print("Capturing images by reading \(path)")

guard let procMaps = readString(from: path) else {
print("Failed to read \(path)")
return []
}

Expand All @@ -486,6 +489,9 @@ public struct Backtrace: CustomStringConvertible, Sendable {
/#
let lines = procMaps.split(separator: "\n")

print("Read \(lines.count) from \(path):")
print(procMaps)

// Find all the mapped files and get high/low ranges
var mappedFiles: [Substring:AddressRange] = [:]
for line in lines {
Expand Down Expand Up @@ -547,6 +553,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
var endOfText: Address = range.low

if let image = try? Elf32Image(source: subSource) {
print("\(path) is an ELF32 image")
theUUID = image.uuid

for hdr in image.programHeaders {
Expand All @@ -556,6 +563,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
}
}
} else if let image = try? Elf64Image(source: subSource) {
print("\(path) is an ELF64 image")
theUUID = image.uuid

for hdr in image.programHeaders {
Expand All @@ -566,6 +574,7 @@ public struct Backtrace: CustomStringConvertible, Sendable {
}
} else {
// Not a valid ELF image
print("\(path) is not an ELF image")
continue
}

Expand Down
15 changes: 15 additions & 0 deletions stdlib/public/Backtracing/SymbolicatedBacktrace.swift
Expand Up @@ -473,12 +473,17 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
// number programs once per frame, whereas we could just run them once
// for all the addresses we're interested in.

print("Images: \(theImages)")

for frame in backtrace.frames {
let address = FileImageSource.Address(frame.adjustedProgramCounter)
if let imageNdx = theImages.firstIndex(
where: { address >= $0.baseAddress
&& address < $0.endOfText }
) {
print("\(hex(address)): checking image \(imageNdx) \(theImages[imageNdx].name)")
print("\(hex(address)): path is \(theImages[imageNdx].path)")

let relativeAddress = address - FileImageSource.Address(theImages[imageNdx].baseAddress)
var symbol: Symbol = Symbol(imageIndex: imageNdx,
imageName: theImages[imageNdx].name,
Expand All @@ -491,16 +496,20 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
if elf32Image == nil && elf64Image == nil {
if let source = try? FileImageSource(path: theImages[imageNdx].path) {
if let elfImage = try? Elf32Image(source: source) {
print("\(hex(address)): found an ELF32 image")
elf32Image = elfImage
elf32Cache[imageNdx] = elfImage
} else if let elfImage = try? Elf64Image(source: source) {
print("\(hex(address)): found an ELF64 image")
elf64Image = elfImage
elf64Cache[imageNdx] = elfImage
}
}
}

if let theSymbol = elf32Image?.lookupSymbol(address: relativeAddress) {
print("\(hex(address)): found \(theSymbol.name) in ELF32")

var location = try? elf32Image!.sourceLocation(for: relativeAddress)

for inline in elf32Image!.inlineCallSites(at: relativeAddress) {
Expand All @@ -524,6 +533,8 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
offset: theSymbol.offset,
sourceLocation: location)
} else if let theSymbol = elf64Image?.lookupSymbol(address: relativeAddress) {
print("\(hex(address)): found \(theSymbol.name) in ELF64")

var location = try? elf64Image!.sourceLocation(for: relativeAddress)

for inline in elf64Image!.inlineCallSites(at: relativeAddress) {
Expand All @@ -547,6 +558,8 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
offset: theSymbol.offset,
sourceLocation: location)
} else {
print("\(hex(address)): symbol lookup failed")

symbol = Symbol(imageIndex: imageNdx,
imageName: theImages[imageNdx].name,
rawName: "<unknown>",
Expand All @@ -558,6 +571,8 @@ public struct SymbolicatedBacktrace: CustomStringConvertible {
continue
}

print("\(hex(address)): address not found in image")

frames.append(Frame(captured: frame, symbol: nil))
}
#else
Expand Down

0 comments on commit 4ad370a

Please sign in to comment.