// SPDX-License-Identifier: GPL-3.0-or-later import Foundation /// One immutable observation of an explicitly shared program. Only the permitted /// ranges are retained. Memory requests never perform another guest bus read. struct MCPDebuggerInspection { let runID: String let id = UUID().uuidString let machine: VCCMachineSnapshot private let memory: [(range: Range, bytes: Data)] let fields: [String: Any] init(runID: String, capture: VCCDebuggerSnapshot, scope: [String: Any], sourceStop: [String: Any]?, watches: [[String: String]], watchStatus: String, ranges: [Range]) throws { let state = capture.machine guard state.state == .paused, state.assistantDeviceBlockers.isEmpty else { throw ProfileFailure.message("pause_required") } self.runID = runID machine = state let data = capture.logicalMemory, available = capture.logicalMemoryAvailable var permitted = IndexSet() for range in ranges where range.lowerBound >= 0 && range.upperBound <= min(0xff00, data.count, available.count) { permitted.insert(integersIn: range) } for address in Array(permitted) where available[address] == 0 { permitted.remove(address) } memory = permitted.rangeView.map { ($0, data.subdata(in: $0)) } var fields = scope fields["run_id"] = runID fields["snapshot_id"] = id fields["status"] = "paused" fields["processor"] = state.use6309 ? "HD6309" : "MC6809" fields["registers"] = capture.registers.reduce(into: [String: Int]()) { $0[$1.key.lowercased()] = $1.value.intValue } fields["source_stop"] = sourceStop as Any? ?? NSNull() fields["watches"] = watches fields["watch_status"] = watchStatus fields["residency_fingerprint"] = String(state.debuggerResidencyFingerprint) fields["memory_ranges"] = memory.map { ["address": $0.range.lowerBound, "count": $0.range.count] } self.fields = fields } static func sameProgram(_ state: VCCMachineSnapshot, _ expected: VCCMachineSnapshot) -> Bool { state.assistantDeviceBlockers.isEmpty && state.generation == expected.generation && state.debuggerProgramRevision == expected.debuggerProgramRevision && state.debuggerResidencyFingerprint == expected.debuggerResidencyFingerprint && state.debuggerMappingFingerprint == expected.debuggerMappingFingerprint && state.use6309 == expected.use6309 } func reply(tool: String, arguments: [String: Any]) throws -> [String: Any] { guard arguments["run_id"] as? String == runID else { throw ProfileFailure.message("stale_run") } if tool == "get_debug_snapshot", arguments.count == 1 { return fields } guard tool == "read_debug_memory", arguments.count == 4, let requested = arguments["snapshot_id"] as? String, let address = Self.integer(arguments["address"]), let count = Self.integer(arguments["count"]), (0...65535).contains(address), (1...256).contains(count), address + count <= 65536 else { throw ProfileFailure.message("invalid_arguments") } guard requested == id else { throw ProfileFailure.message("stale_snapshot") } guard let block = memory.first(where: { $0.range.contains(address) && address + count <= $0.range.upperBound }) else { throw ProfileFailure.message("memory_unavailable") } let offset = address - block.range.lowerBound return ["run_id": runID, "snapshot_id": id, "address": address, "count": count, "bytes": block.bytes[offset..<(offset + count)].map(Int.init)] } private static func integer(_ value: Any?) -> Int? { guard let number = value as? NSNumber, CFGetTypeID(number) != CFBooleanGetTypeID(), !["f", "d"].contains(String(cString: number.objCType)) else { return nil } return Int(exactly: number) } @MainActor static func capture(_ machine: MachineModel) async throws -> VCCDebuggerSnapshot { let snapshot: VCCDebuggerSnapshot? = await withCheckedContinuation { continuation in machine.readDebugger(address: 0, followPC: true, memoryAddress: 0) { continuation.resume(returning: $0) } } guard let snapshot else { throw ProfileFailure.message("debugger_busy") } return snapshot } }