Files
swift-mirror/stdlib/public/Backtracing/FileImageSource.swift
Alastair Houghton 1fc2ca35e8 [Backtracing] Add a caching wrapper for MemoryReader.
Currently we read many small chunks from the process we're backtracing.
If it so happens that it's the local process, that isn't really a big
problem, but if it's a remote process, especially on Linux where we have
to use the memory server, it's probably a little slow.

Fix by adding a caching layer.

rdar://117681625
2023-11-03 14:36:19 +00:00

75 lines
2.3 KiB
Swift

//===--- FileImageSource.swift - An image source that reads from a file ---===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 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
//
//===----------------------------------------------------------------------===//
//
// Defines FileImageSource, an image source that reads data from a file.
//
//===----------------------------------------------------------------------===//
import Swift
@_implementationOnly import OS.Libc
enum FileImageSourceError: Error {
case posixError(Int32)
case outOfRangeRead
}
class FileImageSource: ImageSource {
private var _mapping: UnsafeRawBufferPointer
public var isMappedImage: Bool { return false }
private var _path: String
public var path: String? { return _path }
public var bounds: Bounds? {
return Bounds(base: 0, size: Size(_mapping.count))
}
public init(path: String) throws {
_path = path
let fd = _swift_open(path, O_RDONLY, 0)
if fd < 0 {
throw FileImageSourceError.posixError(_swift_get_errno())
}
defer { close(fd) }
let size = lseek(fd, 0, SEEK_END)
if size < 0 {
throw FileImageSourceError.posixError(_swift_get_errno())
}
let base = mmap(nil, Int(size), PROT_READ, MAP_FILE|MAP_PRIVATE, fd, 0)
if base == nil || base! == UnsafeRawPointer(bitPattern: -1)! {
throw FileImageSourceError.posixError(_swift_get_errno())
}
_mapping = UnsafeRawBufferPointer(start: base, count: Int(size))
}
deinit {
munmap(UnsafeMutableRawPointer(mutating: _mapping.baseAddress),
_mapping.count)
}
public func fetch(from addr: Address,
into buffer: UnsafeMutableRawBufferPointer) throws {
let start = Int(addr)
_ = try buffer.withMemoryRebound(to: UInt8.self) { byteBuf in
guard _mapping.indices.contains(start) else {
throw FileImageSourceError.outOfRangeRead
}
let slice = _mapping[start...]
guard slice.count >= byteBuf.count else {
throw FileImageSourceError.outOfRangeRead
}
}
}
}