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