|
24 | 24 | } |
25 | 25 |
|
26 | 26 | extension Instance { |
27 | | - func findIseq(forWasmAddress address: Int) throws(Debugger.Error) -> Pc { |
28 | | - // Look in the main mapping first |
29 | | - guard |
30 | | - let iseq = handle.wasmToIseqMapping[address] |
31 | | - // If nothing found, find the closest Wasm address using binary search |
32 | | - ?? handle.wasmMappings.binarySearch(nextClosestTo: address) |
33 | | - // Look in the main mapping again with the next closest address if binary search produced anything |
34 | | - .flatMap({ handle.wasmToIseqMapping[$0] }) |
| 27 | + func findIseq(forWasmAddress address: Int) throws(Debugger.Error) -> (iseq: Pc, wasm: Int) { |
| 28 | + // Look in the main mapping |
| 29 | + if let iseq = handle.wasmToIseqMapping[address] { |
| 30 | + return (iseq, address) |
| 31 | + } |
| 32 | + |
| 33 | + // If nothing found, find the closest Wasm address using binary search |
| 34 | + guard let nextAddress = handle.wasmMappings.binarySearch(nextClosestTo: address), |
| 35 | + // Look in the main mapping again with the next closest address if binary search produced anything |
| 36 | + let iseq = handle.wasmToIseqMapping[nextAddress] |
35 | 37 | else { |
36 | 38 | throw Debugger.Error.noInstructionMappingAvailable(address) |
37 | 39 | } |
38 | 40 |
|
39 | | - return iseq |
| 41 | + return (iseq, nextAddress) |
40 | 42 | } |
41 | 43 | } |
42 | 44 |
|
|
112 | 114 | return |
113 | 115 | } |
114 | 116 |
|
115 | | - let iseq = try self.instance.findIseq(forWasmAddress: address) |
| 117 | + let (iseq, wasm) = try self.instance.findIseq(forWasmAddress: address) |
116 | 118 |
|
117 | | - self.breakpoints[address] = iseq.pointee |
| 119 | + self.breakpoints[wasm] = iseq.pointee |
118 | 120 | iseq.pointee = Instruction.breakpoint.headSlot(threadingModel: self.threadingModel) |
| 121 | + |
| 122 | + print("breakpoint enabled at \(iseq)") |
119 | 123 | } |
120 | 124 |
|
121 | 125 | package mutating func disableBreakpoint(address: Int) throws(Error) { |
122 | 126 | guard let oldCodeSlot = self.breakpoints[address] else { |
123 | 127 | return |
124 | 128 | } |
125 | 129 |
|
126 | | - let iseq = try self.instance.findIseq(forWasmAddress: address) |
| 130 | + let (iseq, wasm) = try self.instance.findIseq(forWasmAddress: address) |
127 | 131 |
|
128 | | - self.breakpoints[address] = nil |
| 132 | + self.breakpoints[wasm] = nil |
129 | 133 | iseq.pointee = oldCodeSlot |
130 | 134 | } |
131 | 135 |
|
132 | 136 | package mutating func run() throws { |
133 | | - try self.execution.executeWasm( |
134 | | - threadingModel: self.threadingModel, |
135 | | - function: self.entrypointFunction.handle, |
136 | | - type: self.entrypointFunction.type, |
137 | | - arguments: [], |
138 | | - sp: &self.valueStack, |
139 | | - pc: &self.pc |
140 | | - ) |
| 137 | + do { |
| 138 | + try self.execution.executeWasm( |
| 139 | + threadingModel: self.threadingModel, |
| 140 | + function: self.entrypointFunction.handle, |
| 141 | + type: self.entrypointFunction.type, |
| 142 | + arguments: [], |
| 143 | + sp: &self.valueStack, |
| 144 | + pc: &self.pc |
| 145 | + ) |
| 146 | + } catch _ as Execution.Breakpoint { |
| 147 | + pc -= 1 |
| 148 | + throw Execution.Breakpoint() |
| 149 | + } |
141 | 150 | } |
142 | 151 |
|
143 | 152 | /// Array of addresses in the Wasm binary of executed instructions on the call stack. |
144 | 153 | package var currentCallStack: [Int] { |
145 | 154 | let isDebuggable = self.instance.handle.isDebuggable |
146 | 155 |
|
147 | | - print(self.pc) |
148 | | - return Execution.captureBacktrace(sp: self.valueStack, store: self.store).symbols.compactMap { |
149 | | - print(self.instance.handle.iseqToWasmMapping) |
150 | | - print(self.instance.handle.wasmToIseqMapping) |
| 156 | + var result = Execution.captureBacktrace(sp: self.valueStack, store: self.store).symbols.compactMap { |
151 | 157 | return self.instance.handle.iseqToWasmMapping[$0.address] |
152 | 158 | } |
| 159 | + if let wasmPc = self.instance.handle.iseqToWasmMapping[self.pc] { |
| 160 | + result.append(wasmPc) |
| 161 | + } |
| 162 | + |
| 163 | + print(result) |
| 164 | + return result |
153 | 165 | } |
154 | 166 |
|
155 | 167 | deinit { |
156 | | - self.valueStack.deallocate() |
157 | | - self.pc.deallocate() |
| 168 | + print("Debugger.deinit") |
| 169 | + // self.valueStack.deallocate() |
| 170 | + // self.pc.deallocate() |
158 | 171 | } |
159 | 172 | } |
160 | 173 |
|
|
0 commit comments