Skip to content

Commit e702be9

Browse files
committed
PR feedback: rework error definitions
1 parent 6e466c4 commit e702be9

File tree

3 files changed

+27
-25
lines changed

3 files changed

+27
-25
lines changed

tools/swift-inspect/Sources/SwiftInspectLinux/LinkMap.swift

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ import Foundation
1414
import LinuxSystemHeaders
1515

1616
class LinkMap {
17-
public enum Error: Swift.Error {
18-
case MalformedElf(_ description: String)
19-
case MissingAuxVecEntry(_ description: String)
20-
case ProcessReadMemoryFailure(address: UInt, _ description: String = "")
17+
public enum LinkMapError: Error {
18+
case failedLoadingAuxVec(for: pid_t)
19+
case missingAuxVecEntry(for: pid_t, _ tag: Int32)
20+
case malformedELF(for: pid_t, _ description: String)
2121
}
2222

2323
public struct Entry {
@@ -28,22 +28,21 @@ class LinkMap {
2828
public let entries: [Entry]
2929

3030
public init(for process: Process) throws {
31-
guard let auxVec = Self.loadAuxVec(for: process) else {
32-
throw Error.MissingAuxVecEntry("failed reading auxvec for \(process)")
31+
let auxVec = try Self.loadAuxVec(for: process.pid)
32+
guard let phdrAddr = auxVec[AT_PHDR] else {
33+
throw LinkMapError.missingAuxVecEntry(for: process.pid, AT_PHDR)
3334
}
3435

35-
guard let phdrAddr = auxVec[AT_PHDR] else { throw Error.MissingAuxVecEntry("missing AT_PHDR") }
36-
3736
guard let phdrSize = auxVec[AT_PHENT] else {
38-
throw Error.MissingAuxVecEntry("missing AT_PHENT")
37+
throw LinkMapError.missingAuxVecEntry(for: process.pid, AT_PHENT)
3938
}
4039

4140
guard let phdrCount = auxVec[AT_PHNUM] else {
42-
throw Error.MissingAuxVecEntry("missing AT_PHNUM")
41+
throw LinkMapError.missingAuxVecEntry(for: process.pid, AT_PHNUM)
4342
}
4443

4544
guard phdrSize == MemoryLayout<Elf64_Phdr>.size else {
46-
throw Error.MalformedElf("AT_PHENT invalid size: \(phdrSize)")
45+
throw LinkMapError.malformedELF(for: process.pid, "AT_PHENT invalid size: \(phdrSize)")
4746
}
4847

4948
// determine the base load address for the executable file and locate the
@@ -66,7 +65,7 @@ class LinkMap {
6665

6766
case UInt32(PT_DYNAMIC):
6867
guard dynamicSegment == nil else {
69-
throw Error.MalformedElf("multiple PT_DYNAMIC segments found")
68+
throw LinkMapError.malformedELF(for: process.pid, "multiple PT_DYNAMIC segments found")
7069
}
7170
dynamicSegment = phdr
7271

@@ -75,11 +74,11 @@ class LinkMap {
7574
}
7675

7776
guard let dynamicSegment = dynamicSegment else {
78-
throw Error.MalformedElf("PT_DYNAMIC segment not found")
77+
throw LinkMapError.malformedELF(for: process.pid, "PT_DYNAMIC segment not found")
7978
}
8079

8180
guard let baseLoadSegment = baseLoadSegment else {
82-
throw Error.MalformedElf("PT_LOAD segment not found")
81+
throw LinkMapError.malformedELF(for: process.pid, "PT_LOAD segment not found")
8382
}
8483

8584
let ehdrSize = MemoryLayout<Elf64_Ehdr>.size
@@ -101,7 +100,7 @@ class LinkMap {
101100
}
102101

103102
guard let rDebugEntry = rDebugEntry else {
104-
throw Error.MalformedElf("DT_DEBUG not found in dynamic segment")
103+
throw LinkMapError.malformedELF(for: process.pid, "DT_DEBUG not found in dynamic segment")
105104
}
106105

107106
let rDebugAddr: UInt64 = rDebugEntry.d_un.d_val
@@ -122,8 +121,11 @@ class LinkMap {
122121
}
123122

124123
// loads the auxiliary vector for a 64-bit process
125-
static func loadAuxVec(for process: Process) -> [Int32 : UInt64]? {
126-
guard let data = ProcFS.loadFile(for: process.pid, "auxv") else { return nil }
124+
static func loadAuxVec(for pid: pid_t) throws -> [Int32 : UInt64] {
125+
guard let data = ProcFS.loadFile(for: pid, "auxv") else {
126+
throw LinkMapError.failedLoadingAuxVec(for: pid)
127+
}
128+
127129
return data.withUnsafeBytes {
128130
// in a 64-bit process, aux vector is an array of 8-byte pairs
129131
let count = $0.count / MemoryLayout<(UInt64, UInt64)>.stride

tools/swift-inspect/Sources/SwiftInspectLinux/MemoryMap.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
import Foundation
1414

1515
public class MemoryMap {
16-
public enum Error: Swift.Error {
17-
case InvalidProcMapsFile
16+
public enum MemoryMapError: Error {
17+
case failedLoadingMapsFile(for: pid_t)
1818
}
1919

2020
public struct Entry {
@@ -31,7 +31,7 @@ public class MemoryMap {
3131

3232
public init(for pid: pid_t) throws {
3333
guard let content = ProcFS.loadFileAsString(for: pid, "maps") else {
34-
throw Error.InvalidProcMapsFile
34+
throw MemoryMapError.failedLoadingMapsFile(for: pid)
3535
}
3636

3737
var entries: [Entry] = []

tools/swift-inspect/Sources/SwiftInspectLinux/Process.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import Foundation
1414
import LinuxSystemHeaders
1515

1616
public class Process {
17-
public enum Error: Swift.Error {
18-
case ProcessVmReadFailure(pid: pid_t, address: UInt64, size: UInt64)
19-
case InvalidString(address: UInt64)
17+
public enum ProcessError: Error {
18+
case processVmReadFailure(pid: pid_t, address: UInt64, size: UInt64)
19+
case malformedString(address: UInt64)
2020
}
2121

2222
let pid: pid_t
@@ -38,7 +38,7 @@ public class Process {
3838
public func readString(address: UInt64, encoding: String.Encoding = .utf8) throws -> String {
3939
let rawBytes = try readRawString(address: address)
4040
guard let result = String(bytes: rawBytes, encoding: encoding) else {
41-
throw Error.InvalidString(address: address)
41+
throw ProcessError.malformedString(address: address)
4242
}
4343

4444
return result
@@ -79,7 +79,7 @@ public class Process {
7979
}
8080

8181
guard array.count > 0 else {
82-
throw Error.ProcessVmReadFailure(pid: self.pid, address: address, size: UInt64(maxSize))
82+
throw ProcessError.processVmReadFailure(pid: self.pid, address: address, size: UInt64(maxSize))
8383
}
8484

8585
return array

0 commit comments

Comments
 (0)