Skip to content

Commit 3b0e5bb

Browse files
committed
consistent use of /proc
1 parent 9e5f0ca commit 3b0e5bb

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ import LinuxSystemHeaders
1616
internal class AuxVec {
1717
// loads the auxiliary vector for a process
1818
public static func load(for process: Process) -> [Int32 : UInt64]? {
19-
let filePath = "/proc/\(process.pid)/auxv"
20-
guard let fileHandle = FileHandle(forReadingAtPath: filePath) else { return nil }
21-
defer { fileHandle.closeFile() }
22-
23-
guard let data = try? fileHandle.readToEnd(), data.count > 0 else { return nil }
19+
guard let data = ProcFS.loadFile(for: process.pid, "auxv") else { return nil }
2420

2521
func fromData<T: UnsignedInteger>(_ data: Data) -> [(T, T)] {
2622
return data.withUnsafeBytes {

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Foundation
1414

1515
public class MemoryMap {
1616
public enum Error: Swift.Error {
17-
case InvalidProcMapsFile(_ filePath: String)
17+
case InvalidProcMapsFile
1818
}
1919

2020
public struct Entry {
@@ -30,12 +30,8 @@ public class MemoryMap {
3030
public let entries: [Entry]
3131

3232
public init(for pid: pid_t) throws {
33-
let filePath = "/proc/\(pid)/maps"
34-
let file = try FileHandle(forReadingFrom: URL(fileURLWithPath: filePath))
35-
defer { file.closeFile() }
36-
37-
guard let content = String(data: file.readDataToEndOfFile(), encoding: .ascii) else {
38-
throw Error.InvalidProcMapsFile(filePath)
33+
guard let content = ProcFS.loadFileAsString(for: pid, "maps") else {
34+
throw Error.InvalidProcMapsFile
3935
}
4036

4137
var entries: [Entry] = []
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
import LinuxSystemHeaders
15+
16+
public class ProcFS {
17+
public static func loadFile(for pid: pid_t, _ fileName: String) -> Data? {
18+
let filePath = "/proc/\(pid)/\(fileName)"
19+
// Loading contents of files under /proc may not work correctly using
20+
// Data(contentsOf:) or String(contentsOfFile:) because the files may
21+
// appear empty from stat(2) and may not be seekable. FileHandle.readToEnd
22+
// handles these cases.
23+
guard let fileHandle = FileHandle(forReadingAtPath: filePath) else { return nil }
24+
defer { fileHandle.closeFile() }
25+
guard let data = try? fileHandle.readToEnd() else { return nil }
26+
return data
27+
}
28+
29+
public static func loadFileAsString(
30+
for pid: pid_t, _ fileName: String, encoding: String.Encoding = .utf8
31+
) -> String? {
32+
guard let data = Self.loadFile(for: pid, fileName) else { return nil }
33+
return String(data: data, encoding: encoding)
34+
}
35+
}

tools/swift-inspect/Sources/swift-inspect/LinuxRemoteProcess.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,12 @@
102102

103103
init?(processId: ProcessIdentifier) {
104104
self.processIdentifier = processId
105+
106+
if let processName = SwiftInspectLinux.ProcFS.loadFileAsString(for: processId, "cmdline") {
107+
self.processName = processName
108+
}
109+
105110
do {
106-
let path = "/proc/\(processId)/cmdline"
107-
self.processName = try String(contentsOfFile: path, encoding: .ascii)
108111
self.process = try SwiftInspectLinux.Process(processId)
109112
self.symbolCache = try SwiftInspectLinux.SymbolCache(for: process)
110113
self.memoryMap = try SwiftInspectLinux.MemoryMap(for: processId)

0 commit comments

Comments
 (0)