Skip to content

Commit 6dc7c32

Browse files
committed
address some code review comments
1 parent 4906ed9 commit 6dc7c32

File tree

4 files changed

+32
-53
lines changed

4 files changed

+32
-53
lines changed

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

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,60 +14,36 @@ import Foundation
1414
import LinuxSystemHeaders
1515

1616
internal class AuxVec {
17-
enum Error: Swift.Error { case FileReadFailure(_ filePath: String) }
18-
19-
// enum values must match the constants defined in usr/include/linux/auxv.h
20-
enum Tag: UInt64 {
21-
case AT_NULL = 0
22-
case AT_IGNORE = 1
23-
case AT_EXECFD = 2
24-
case AT_PHDR = 3
25-
case AT_PHENT = 4
26-
case AT_PHNUM = 5
27-
case AT_PAGESZ = 6
28-
case AT_BASE = 7
29-
case AT_FLAGS = 8
30-
case AT_ENTRY = 9
31-
case AT_NOTELF = 10
32-
case AT_UID = 11
33-
case AT_EUID = 12
34-
case AT_GID = 13
35-
case AT_EGID = 14
36-
case AT_PLATFORM = 15
37-
case AT_HWCAP = 16
38-
case AT_CLKTCK = 17
39-
case AT_SECURE = 23
40-
case AT_BASE_PLATFORM = 24
41-
case AT_RANDOM = 25
42-
case AT_HWCAP2 = 26
43-
case AT_RSEQ_FEATURE_SIZE = 27
44-
case AT_RSEQ_ALIGN = 28
45-
case AT_EXECFN = 31
46-
case AT_SYSINFO_EHDR = 33
47-
case AT_MINSIGSTKSZ = 51
48-
}
49-
50-
static func load(for process: Process) throws -> [Tag: UInt64] {
17+
// loads the auxiliary vector for a process
18+
public static func load(for process: Process) -> [Int32 : UInt64]? {
5119
let filePath = "/proc/\(process.pid)/auxv"
52-
53-
let fileHandle = try FileHandle(forReadingFrom: URL(fileURLWithPath: filePath))
20+
guard let fileHandle = FileHandle(forReadingAtPath: filePath) else { return nil }
5421
defer { fileHandle.closeFile() }
5522

56-
guard let data = try fileHandle.readToEnd() else { throw Error.FileReadFailure(filePath) }
23+
guard let data = try? fileHandle.readToEnd(), data.count > 0 else { return nil }
5724

58-
// aux vector is an array of 8-byte pairs in a 64-bit process
59-
assert(process.elfFile.isElf64, "only 64-bit processes are supported")
60-
let auxVec: [(UInt64, UInt64)] = data.withUnsafeBytes {
61-
let count = $0.count / MemoryLayout<(UInt64, UInt64)>.stride
62-
return Array($0.bindMemory(to: (UInt64, UInt64).self)[..<count])
25+
func fromData<T: UnsignedInteger>(_ data: Data) -> [(T, T)] {
26+
return data.withUnsafeBytes {
27+
let count = $0.count / MemoryLayout<(T, T)>.stride
28+
return Array($0.bindMemory(to: (T, T).self)[..<count])
29+
}
6330
}
6431

65-
var entries: [Tag: UInt64] = [:]
66-
for (rawTag, value) in auxVec {
67-
guard let tag = Tag(rawValue: rawTag) else { continue }
68-
entries[tag] = value
32+
func fromArray<T: UnsignedInteger>(_ array: [(T, T)]) -> [Int32: UInt64] {
33+
var entries: [Int32: UInt64] = [:]
34+
for (rawTag, value) in array {
35+
// the AT_ constants defined in linux/auxv.h are imported as Int32
36+
guard let tag = Int32(exactly: rawTag) else { continue }
37+
entries[tag] = UInt64(value)
38+
}
39+
return entries
6940
}
7041

71-
return entries
42+
// in a 32-bit process, aux vector is an array of 4-byte pairs
43+
// in a 64-bit process, aux vector is an array of 8-byte pairs
44+
return process.elfFile.isElf64
45+
? fromArray(fromData(data) as [(UInt64, UInt64)])
46+
: fromArray(fromData(data) as [(UInt32, UInt32)])
7247
}
48+
7349
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ElfFile {
7777
public func loadSymbols(baseAddress: UInt64 = 0) throws -> SymbolMap {
7878
guard let sectionCount = UInt(exactly: self.ehdr.shnum) else {
7979
throw Error.MalformedElfFile(
80-
self.filePath, description: "invalid ehdr.shnum: \(self.ehdr.shnum)")
80+
self.filePath, description: "invalid ElfEhdr.e_shnum: \(self.ehdr.shnum)")
8181
}
8282

8383
var symbols: SymbolMap = [:]

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@ class LinkMap {
2828
public let entries: [Entry]
2929

3030
public init(for process: Process) throws {
31-
let auxVec = try AuxVec.load(for: process)
32-
guard let phdrAddr = auxVec[.AT_PHDR] else { throw Error.MissingAuxVecEntry("missing AT_PHDR") }
31+
guard let auxVec = AuxVec.load(for: process) else {
32+
throw Error.MissingAuxVecEntry("failed reading auxvec for \(process)")
33+
}
34+
35+
guard let phdrAddr = auxVec[AT_PHDR] else { throw Error.MissingAuxVecEntry("missing AT_PHDR") }
3336

34-
guard let phdrSize = auxVec[.AT_PHENT] else {
37+
guard let phdrSize = auxVec[AT_PHENT] else {
3538
throw Error.MissingAuxVecEntry("missing AT_PHENT")
3639
}
3740

38-
guard let phdrCount = auxVec[.AT_PHNUM] else {
41+
guard let phdrCount = auxVec[AT_PHNUM] else {
3942
throw Error.MissingAuxVecEntry("missing AT_PHNUM")
4043
}
4144

tools/swift-inspect/Sources/swift-inspect/Operations/DumpArray.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if os(iOS) || os(macOS) || os(tvOS) || os(watchOS) || os(Windows)
13+
#if !os(Linux)
1414

1515
import ArgumentParser
1616
import SwiftRemoteMirror

0 commit comments

Comments
 (0)