File tree Expand file tree Collapse file tree 4 files changed +44
-14
lines changed
tools/swift-inspect/Sources Expand file tree Collapse file tree 4 files changed +44
-14
lines changed Original file line number Diff line number Diff line change @@ -16,11 +16,7 @@ import LinuxSystemHeaders
16
16
internal class AuxVec {
17
17
// loads the auxiliary vector for a process
18
18
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 }
24
20
25
21
func fromData< T: UnsignedInteger > ( _ data: Data ) -> [ ( T , T ) ] {
26
22
return data. withUnsafeBytes {
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import Foundation
14
14
15
15
public class MemoryMap {
16
16
public enum Error : Swift . Error {
17
- case InvalidProcMapsFile( _ filePath : String )
17
+ case InvalidProcMapsFile
18
18
}
19
19
20
20
public struct Entry {
@@ -30,12 +30,8 @@ public class MemoryMap {
30
30
public let entries : [ Entry ]
31
31
32
32
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
39
35
}
40
36
41
37
var entries : [ Entry ] = [ ]
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 102
102
103
103
init ? ( processId: ProcessIdentifier ) {
104
104
self . processIdentifier = processId
105
+
106
+ if let processName = SwiftInspectLinux . ProcFS. loadFileAsString ( for: processId, " cmdline " ) {
107
+ self . processName = processName
108
+ }
109
+
105
110
do {
106
- let path = " /proc/ \( processId) /cmdline "
107
- self . processName = try String ( contentsOfFile: path, encoding: . ascii)
108
111
self . process = try SwiftInspectLinux . Process ( processId)
109
112
self . symbolCache = try SwiftInspectLinux . SymbolCache ( for: process)
110
113
self . memoryMap = try SwiftInspectLinux . MemoryMap ( for: processId)
You can’t perform that action at this time.
0 commit comments