|
| 1 | +#if WasmDebuggingSupport |
| 2 | + /// User-facing debugger state driven by a debugger host. This implementation has no knowledge of the exact |
| 3 | + /// debugger protocol, which allows any protocol implementation or direct API users to be layered on top if needed. |
| 4 | + package struct Debugger: ~Copyable { |
| 5 | + package enum Error: Swift.Error, @unchecked Sendable { |
| 6 | + case entrypointFunctionNotFound |
| 7 | + case unknownCurrentFunctionForResumedBreakpoint(UnsafeMutablePointer<UInt64>) |
| 8 | + case noInstructionMappingAvailable(Int) |
| 9 | + case noReverseInstructionMappingAvailable(UnsafeMutablePointer<UInt64>) |
| 10 | + } |
| 11 | + |
| 12 | + private let valueStack: Sp |
| 13 | + private var execution: Execution |
| 14 | + private let store: Store |
| 15 | + |
| 16 | + /// Parsed in-memory representation of a Wasm module instantiated for debugging. |
| 17 | + private let module: Module |
| 18 | + |
| 19 | + /// Instance of parsed Wasm ``module``. |
| 20 | + private let instance: Instance |
| 21 | + |
| 22 | + /// Reference to the entrypoint function of the currently debugged module, for use in ``stopAtEntrypoint``. |
| 23 | + /// Currently assumed to be the WASI command `_start` entrypoint. |
| 24 | + private let entrypointFunction: Function |
| 25 | + |
| 26 | + /// Threading model of the Wasm engine configuration, cached for a potentially hot path. |
| 27 | + private let threadingModel: EngineConfiguration.ThreadingModel |
| 28 | + |
| 29 | + private(set) var breakpoints = [Int: CodeSlot]() |
| 30 | + |
| 31 | + private var currentBreakpoint: (iseq: Execution.Breakpoint, wasmPc: Int)? |
| 32 | + |
| 33 | + private var pc = Pc.allocate(capacity: 1) |
| 34 | + |
| 35 | + /// Initializes a new debugger state instance. |
| 36 | + /// - Parameters: |
| 37 | + /// - module: Wasm module to instantiate. |
| 38 | + /// - store: Store that instantiates the module. |
| 39 | + /// - imports: Imports required by `module` for instantiation. |
| 40 | + package init(module: Module, store: Store, imports: Imports) throws { |
| 41 | + let limit = store.engine.configuration.stackSize / MemoryLayout<StackSlot>.stride |
| 42 | + let instance = try module.instantiate(store: store, imports: imports, isDebuggable: true) |
| 43 | + |
| 44 | + guard case .function(let entrypointFunction) = instance.exports["_start"] else { |
| 45 | + throw Error.entrypointFunctionNotFound |
| 46 | + } |
| 47 | + |
| 48 | + self.instance = instance |
| 49 | + self.module = module |
| 50 | + self.entrypointFunction = entrypointFunction |
| 51 | + self.valueStack = UnsafeMutablePointer<StackSlot>.allocate(capacity: limit) |
| 52 | + self.store = store |
| 53 | + self.execution = Execution(store: StoreRef(store), stackEnd: valueStack.advanced(by: limit)) |
| 54 | + self.threadingModel = store.engine.configuration.threadingModel |
| 55 | + self.pc.pointee = Instruction.endOfExecution.headSlot(threadingModel: threadingModel) |
| 56 | + } |
| 57 | + |
| 58 | + /// Sets a breakpoint at the first instruction in the entrypoint function of the module instantiated by |
| 59 | + /// this debugger. |
| 60 | + package mutating func stopAtEntrypoint() throws { |
| 61 | + try self.enableBreakpoint(address: self.originalAddress(function: entrypointFunction)) |
| 62 | + } |
| 63 | + |
| 64 | + /// Finds a Wasm address for the first instruction in a given function. |
| 65 | + /// - Parameter function: the Wasm function to find the first Wasm instruction address for. |
| 66 | + /// - Returns: byte offset of the first Wasm instruction of given function in the module it was parsed from. |
| 67 | + private func originalAddress(function: Function) throws -> Int { |
| 68 | + precondition(function.handle.isWasm) |
| 69 | + |
| 70 | + switch function.handle.wasm.code { |
| 71 | + case .debuggable(let wasm, _): |
| 72 | + return wasm.originalAddress |
| 73 | + case .uncompiled: |
| 74 | + try function.handle.wasm.ensureCompiled(store: StoreRef(self.store)) |
| 75 | + return try self.originalAddress(function: function) |
| 76 | + case .compiled: |
| 77 | + fatalError() |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + /// Enables a breakpoint at a given Wasm address. |
| 82 | + /// - Parameter address: byte offset of the Wasm instruction that will be replaced with a breakpoint. If no |
| 83 | + /// direct internal bytecode matching instruction is found, the next closest internal bytecode instruction |
| 84 | + /// is replaced with a breakpoint. The original instruction to be restored is preserved in debugger state |
| 85 | + /// represented by `self`. |
| 86 | + /// See also ``Debugger/disableBreakpoint(address:)``. |
| 87 | + package mutating func enableBreakpoint(address: Int) throws(Error) { |
| 88 | + guard self.breakpoints[address] == nil else { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + guard let (iseq, wasm) = try self.instance.handle.instructionMapping.findIseq(forWasmAddress: address) else { |
| 93 | + throw Error.noInstructionMappingAvailable(address) |
| 94 | + } |
| 95 | + |
| 96 | + self.breakpoints[wasm] = iseq.pointee |
| 97 | + iseq.pointee = Instruction.breakpoint.headSlot(threadingModel: self.threadingModel) |
| 98 | + } |
| 99 | + |
| 100 | + /// Disables a breakpoint at a given Wasm address. If no breakpoint at a given address was previously set with |
| 101 | + /// `self.enableBreakpoint(address:), this function immediately returns. |
| 102 | + /// - Parameter address: byte offset of the Wasm instruction that was replaced with a breakpoint. The original |
| 103 | + /// instruction is restored from debugger state and replaces the breakpoint instruction. |
| 104 | + /// See also ``Debugger/enableBreakpoint(address:)``. |
| 105 | + package mutating func disableBreakpoint(address: Int) throws(Error) { |
| 106 | + guard let oldCodeSlot = self.breakpoints[address] else { |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + guard let (iseq, wasm) = try self.instance.handle.instructionMapping.findIseq(forWasmAddress: address) else { |
| 111 | + throw Error.noInstructionMappingAvailable(address) |
| 112 | + } |
| 113 | + |
| 114 | + self.breakpoints[wasm] = nil |
| 115 | + iseq.pointee = oldCodeSlot |
| 116 | + } |
| 117 | + |
| 118 | + /// Resumes the module instantiated by the debugger stopped at a breakpoint. The breakpoint is disabled |
| 119 | + /// and execution is resumed until the next breakpoint is triggered or all remaining instructions are |
| 120 | + /// executed. If the module is not stopped at a breakpoint, this function returns immediately. |
| 121 | + /// - Returns: `[Value]` result of `entrypointFunction` if current instance ran to completion, |
| 122 | + /// `nil` if it stopped at a breakpoint. |
| 123 | + package mutating func run() throws -> [Value]? { |
| 124 | + do { |
| 125 | + if let currentBreakpoint { |
| 126 | + // Remove the breakpoint before resuming |
| 127 | + try self.disableBreakpoint(address: currentBreakpoint.wasmPc) |
| 128 | + self.execution.resetError() |
| 129 | + |
| 130 | + var sp = currentBreakpoint.iseq.sp |
| 131 | + var pc = currentBreakpoint.iseq.pc |
| 132 | + var md: Md = nil |
| 133 | + var ms: Ms = 0 |
| 134 | + |
| 135 | + guard let currentFunction = sp.currentFunction else { |
| 136 | + throw Error.unknownCurrentFunctionForResumedBreakpoint(sp) |
| 137 | + } |
| 138 | + |
| 139 | + Execution.CurrentMemory.mayUpdateCurrentInstance( |
| 140 | + instance: currentFunction.instance, |
| 141 | + from: self.instance.handle, |
| 142 | + md: &md, |
| 143 | + ms: &ms |
| 144 | + ) |
| 145 | + |
| 146 | + do { |
| 147 | + switch self.threadingModel { |
| 148 | + case .direct: |
| 149 | + try self.execution.runDirectThreaded(sp: sp, pc: pc, md: md, ms: ms) |
| 150 | + case .token: |
| 151 | + try self.execution.runTokenThreaded(sp: &sp, pc: &pc, md: &md, ms: &ms) |
| 152 | + } |
| 153 | + } catch is Execution.EndOfExecution { |
| 154 | + } |
| 155 | + |
| 156 | + let type = self.store.engine.funcTypeInterner.resolve(currentFunction.type) |
| 157 | + return type.results.enumerated().map { (i, type) in |
| 158 | + sp[VReg(i)].cast(to: type) |
| 159 | + } |
| 160 | + } else { |
| 161 | + return try self.execution.executeWasm( |
| 162 | + threadingModel: self.threadingModel, |
| 163 | + function: self.entrypointFunction.handle, |
| 164 | + type: self.entrypointFunction.type, |
| 165 | + arguments: [], |
| 166 | + sp: self.valueStack, |
| 167 | + pc: self.pc |
| 168 | + ) |
| 169 | + } |
| 170 | + } catch let breakpoint as Execution.Breakpoint { |
| 171 | + let pc = breakpoint.pc |
| 172 | + guard let wasmPc = self.instance.handle.instructionMapping.findWasm(forIseqAddress: pc) else { |
| 173 | + throw Error.noReverseInstructionMappingAvailable(pc) |
| 174 | + } |
| 175 | + |
| 176 | + self.currentBreakpoint = (breakpoint, wasmPc) |
| 177 | + return nil |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + /// Array of addresses in the Wasm binary of executed instructions on the call stack. |
| 182 | + package var currentCallStack: [Int] { |
| 183 | + guard let currentBreakpoint else { |
| 184 | + return [] |
| 185 | + } |
| 186 | + |
| 187 | + var result = Execution.captureBacktrace(sp: currentBreakpoint.iseq.sp, store: self.store).symbols.compactMap { |
| 188 | + return self.instance.handle.instructionMapping.findWasm(forIseqAddress: $0.address) |
| 189 | + } |
| 190 | + result.append(currentBreakpoint.wasmPc) |
| 191 | + |
| 192 | + return result |
| 193 | + } |
| 194 | + |
| 195 | + deinit { |
| 196 | + self.valueStack.deallocate() |
| 197 | + self.pc.deallocate() |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | +#endif |
0 commit comments