Skip to content

Commit ea04cc3

Browse files
committed
Rewind Pc back by 1 word after catching Breakpoint
1 parent 8bf6849 commit ea04cc3

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

Sources/WasmKit/Execution/Debugger.swift

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@
2424
}
2525

2626
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]
3537
else {
3638
throw Debugger.Error.noInstructionMappingAvailable(address)
3739
}
3840

39-
return iseq
41+
return (iseq, nextAddress)
4042
}
4143
}
4244

@@ -112,49 +114,60 @@
112114
return
113115
}
114116

115-
let iseq = try self.instance.findIseq(forWasmAddress: address)
117+
let (iseq, wasm) = try self.instance.findIseq(forWasmAddress: address)
116118

117-
self.breakpoints[address] = iseq.pointee
119+
self.breakpoints[wasm] = iseq.pointee
118120
iseq.pointee = Instruction.breakpoint.headSlot(threadingModel: self.threadingModel)
121+
122+
print("breakpoint enabled at \(iseq)")
119123
}
120124

121125
package mutating func disableBreakpoint(address: Int) throws(Error) {
122126
guard let oldCodeSlot = self.breakpoints[address] else {
123127
return
124128
}
125129

126-
let iseq = try self.instance.findIseq(forWasmAddress: address)
130+
let (iseq, wasm) = try self.instance.findIseq(forWasmAddress: address)
127131

128-
self.breakpoints[address] = nil
132+
self.breakpoints[wasm] = nil
129133
iseq.pointee = oldCodeSlot
130134
}
131135

132136
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+
}
141150
}
142151

143152
/// Array of addresses in the Wasm binary of executed instructions on the call stack.
144153
package var currentCallStack: [Int] {
145154
let isDebuggable = self.instance.handle.isDebuggable
146155

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 {
151157
return self.instance.handle.iseqToWasmMapping[$0.address]
152158
}
159+
if let wasmPc = self.instance.handle.iseqToWasmMapping[self.pc] {
160+
result.append(wasmPc)
161+
}
162+
163+
print(result)
164+
return result
153165
}
154166

155167
deinit {
156-
self.valueStack.deallocate()
157-
self.pc.deallocate()
168+
print("Debugger.deinit")
169+
// self.valueStack.deallocate()
170+
// self.pc.deallocate()
158171
}
159172
}
160173

0 commit comments

Comments
 (0)