|
| 1 | +#if WasmDebuggingSupport |
| 2 | + |
| 3 | + package struct Debugger: ~Copyable { |
| 4 | + enum Error: Swift.Error { |
| 5 | + case entrypointFunctionNotFound |
| 6 | + case noInstructionMappingAvailable(Int) |
| 7 | + } |
| 8 | + |
| 9 | + private let valueStack: Sp |
| 10 | + private let execution: Execution |
| 11 | + private let store: Store |
| 12 | + |
| 13 | + /// Parsed in-memory representation of a Wasm module instantiated for debugging. |
| 14 | + private let module: Module |
| 15 | + |
| 16 | + /// Instance of parsed Wasm ``module``. |
| 17 | + private let instance: Instance |
| 18 | + |
| 19 | + /// Reference to the entrypoint function of the currently debugged module, for use in ``stopAtEntrypoint``. |
| 20 | + private let entrypointFunction: Function |
| 21 | + |
| 22 | + /// Threading model of the Wasm engine configuration cached for a potentially hot path. |
| 23 | + private let threadingModel: EngineConfiguration.ThreadingModel |
| 24 | + |
| 25 | + private var breakpoints = [Int: CodeSlot]() |
| 26 | + |
| 27 | + package init(module: Module, store: Store, imports: Imports) throws { |
| 28 | + let limit = store.engine.configuration.stackSize / MemoryLayout<StackSlot>.stride |
| 29 | + let instance = try module.instantiate(store: store, imports: imports, isDebuggable: true) |
| 30 | + |
| 31 | + guard case .function(let entrypointFunction) = instance.exports["_start"] else { |
| 32 | + throw Error.entrypointFunctionNotFound |
| 33 | + } |
| 34 | + |
| 35 | + self.instance = instance |
| 36 | + self.module = module |
| 37 | + self.entrypointFunction = entrypointFunction |
| 38 | + self.valueStack = UnsafeMutablePointer<StackSlot>.allocate(capacity: limit) |
| 39 | + self.store = store |
| 40 | + self.execution = Execution(store: StoreRef(store), stackEnd: valueStack.advanced(by: limit)) |
| 41 | + self.threadingModel = store.engine.configuration.threadingModel |
| 42 | + } |
| 43 | + |
| 44 | + package mutating func stopAtEntrypoint() throws { |
| 45 | + try self.enableBreakpoint(address: self.originalAddress(function: entrypointFunction)) |
| 46 | + } |
| 47 | + |
| 48 | + package func originalAddress(function: Function) throws -> Int { |
| 49 | + precondition(function.handle.isWasm) |
| 50 | + |
| 51 | + switch function.handle.wasm.code { |
| 52 | + case .debuggable(let wasm, _): |
| 53 | + return wasm.originalAddress |
| 54 | + case .uncompiled: |
| 55 | + try function.handle.wasm.ensureCompiled(store: StoreRef(self.store)) |
| 56 | + return try self.originalAddress(function: function) |
| 57 | + case .compiled: |
| 58 | + fatalError() |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + package mutating func enableBreakpoint(address: Int) throws { |
| 63 | + print("attempt to toggle a breakpoint at \(address)") |
| 64 | + |
| 65 | + guard self.breakpoints[address] == nil else { |
| 66 | + print("breakpoint at \(address) already enabled") |
| 67 | + return |
| 68 | + } |
| 69 | + |
| 70 | + guard let iseq = self.instance.handle.wasmToIseqMapping[address] else { |
| 71 | + throw Error.noInstructionMappingAvailable(address) |
| 72 | + } |
| 73 | + self.breakpoints[address] = iseq.pointee |
| 74 | + iseq.pointee = Instruction.breakpoint.headSlot(threadingModel: self.threadingModel) |
| 75 | + } |
| 76 | + |
| 77 | + package mutating func disableBreakpoint(address: Int) throws { |
| 78 | + print("attempt to toggle a breakpoint at \(address)") |
| 79 | + |
| 80 | + guard let oldCodeSlot = self.breakpoints[address] else { |
| 81 | + print("breakpoint at \(address) already disabled") |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + guard let iseq = self.instance.handle.wasmToIseqMapping[address] else { |
| 86 | + throw Error.noInstructionMappingAvailable(address) |
| 87 | + } |
| 88 | + iseq.pointee = oldCodeSlot |
| 89 | + self.breakpoints[address] = nil |
| 90 | + } |
| 91 | + |
| 92 | + mutating func enableBreakpoint(functionIndex: FunctionIndex, offset: Int) { |
| 93 | + } |
| 94 | + |
| 95 | + /// Array of addresses in the Wasm binary of executed instructions on the call stack. |
| 96 | + package var currentCallStack: [Int] { |
| 97 | + let isDebuggable = self.instance.handle.isDebuggable |
| 98 | + print("isDebuggable is \(isDebuggable)") |
| 99 | + |
| 100 | + return Execution.captureBacktrace(sp: self.valueStack, store: self.store).symbols.map { |
| 101 | + self.instance.handle.iseqToWasmMapping[$0.address]! |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + deinit { |
| 106 | + valueStack.deallocate() |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | +#endif |
0 commit comments