mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[linux] swift-inspect support for Linux
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// This source file is part of the Swift.org open source project
|
||||
//
|
||||
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
|
||||
// Licensed under Apache License v2.0 with Runtime Library Exception
|
||||
//
|
||||
// See https://swift.org/LICENSE.txt for license information
|
||||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
import Foundation
|
||||
|
||||
public class MemoryMap {
|
||||
public enum Error: Swift.Error {
|
||||
case InvalidProcMapsFile(_ filePath: String)
|
||||
}
|
||||
|
||||
public struct Entry {
|
||||
public let startAddr: UInt64
|
||||
public let endAddr: UInt64
|
||||
public let permissions: String
|
||||
public let offset: UInt64
|
||||
public let device: String
|
||||
public let inode: UInt64
|
||||
public let pathname: String?
|
||||
}
|
||||
|
||||
public let entries: [Entry]
|
||||
|
||||
public init(for pid: pid_t) throws {
|
||||
let filePath = "/proc/\(pid)/maps"
|
||||
let file = try FileHandle(forReadingFrom: URL(fileURLWithPath: filePath))
|
||||
defer { file.closeFile() }
|
||||
|
||||
guard let content = String(data: file.readDataToEndOfFile(), encoding: .ascii) else {
|
||||
throw Error.InvalidProcMapsFile(filePath)
|
||||
}
|
||||
|
||||
var entries: [Entry] = []
|
||||
|
||||
content.enumerateLines { (line, _) in
|
||||
let components = line.split(separator: " ", omittingEmptySubsequences: true)
|
||||
guard components.count >= 5 else { return }
|
||||
let addrParts = components[0].split(separator: "-")
|
||||
let entry = Entry(
|
||||
startAddr: UInt64(addrParts[0], radix: 16) ?? 0,
|
||||
endAddr: UInt64(addrParts[1], radix: 16) ?? 0, permissions: String(components[1]),
|
||||
offset: UInt64(components[2], radix: 16) ?? 0, device: String(components[3]),
|
||||
inode: UInt64(components[4]) ?? 0,
|
||||
pathname: components.count == 6 ? String(components[5]) : nil)
|
||||
entries.append(entry)
|
||||
}
|
||||
|
||||
self.entries = entries
|
||||
}
|
||||
|
||||
public func findEntry(containing addr: UInt64) -> Entry? {
|
||||
// The map array returned by loadMaps will be ordered the same as the
|
||||
// contents of /proc/<pid>/maps, which is in ascending address order.
|
||||
// Binary search it to find the entry containing the target address.
|
||||
var lowerBound = 0
|
||||
var upperBound = self.entries.count
|
||||
while lowerBound < upperBound {
|
||||
let currentIndex = (lowerBound + upperBound) / 2
|
||||
let entry = self.entries[currentIndex]
|
||||
if entry.startAddr > addr {
|
||||
upperBound = currentIndex
|
||||
} else if entry.endAddr <= addr {
|
||||
lowerBound = currentIndex + 1
|
||||
} else {
|
||||
assert(addr >= entry.startAddr)
|
||||
assert(addr < entry.endAddr)
|
||||
return entry
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user