Files
VirtualBuddy/VirtualWormhole/Source/Services/Debugging/DebugVirtioSocket.swift
Guilherme Rambo baa001af93 Refactor Xcode debugging support (#822)
* Refactor Xcode debugging support into independent Swift packages

* Fix remote process name in Xcode debug navigator, display progress for shared cache symbol extraction, bring app to the foreground when a run requires extracting symbols
2026-11-24 10:08:45 -03:00

34 lines
1.4 KiB
Swift

import Darwin
import Foundation
import MacDebugProtocol
extension DebugSocket {
static func guestListener() throws -> (DebugSocket, UInt32) {
let stream = try DebugSocket(owning: socket(AF_VSOCK, SOCK_STREAM, 0))
var address = sockaddr_vm()
address.svm_family = sa_family_t(AF_VSOCK)
address.svm_len = UInt8(MemoryLayout<sockaddr_vm>.size)
address.svm_port = UInt32(VMADDR_PORT_ANY)
address.svm_cid = UInt32(VMADDR_CID_ANY)
var size = socklen_t(address.svm_len)
let ok = withUnsafeMutablePointer(to: &address) { ptr in
ptr.withMemoryRebound(to: sockaddr.self, capacity: 1) {
bind(stream.descriptor, $0, size) == 0 && listen(stream.descriptor, 1) == 0 && getsockname(stream.descriptor, $0, &size) == 0
}
}
guard ok else { throw POSIXError(.init(rawValue: errno) ?? .EIO) }
return (stream, address.svm_port)
}
func acceptDebuggerFromHost() async throws -> DebugSocket {
try await accept(timeout: 90_000) { _, address in
let isHost = withUnsafePointer(to: address) { pointer in
pointer.withMemoryRebound(to: sockaddr_vm.self, capacity: 1) {
$0.pointee.svm_family == AF_VSOCK && $0.pointee.svm_cid == VMADDR_CID_HOST
}
}
guard isHost else { throw POSIXError(.EACCES) }
}
}
}