Skip to content

Commit ae91e24

Browse files
committed
Add DebuggerTests test suite
1 parent a25399d commit ae91e24

File tree

3 files changed

+46
-7
lines changed

3 files changed

+46
-7
lines changed

Sources/WasmKit/Execution/Debugger.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#if WasmDebuggingSupport
22

33
package struct Debugger: ~Copyable {
4-
enum Error: Swift.Error {
4+
package enum Error: Swift.Error {
55
case entrypointFunctionNotFound
66
case noInstructionMappingAvailable(Int)
77
}
@@ -59,8 +59,9 @@
5959
}
6060
}
6161

62-
package mutating func enableBreakpoint(address: Int) throws {
62+
package mutating func enableBreakpoint(address: Int) throws(Error) {
6363
print("attempt to toggle a breakpoint at \(address)")
64+
print("available mapping: \(self.instance.handle.wasmToIseqMapping)")
6465

6566
guard self.breakpoints[address] == nil else {
6667
print("breakpoint at \(address) already enabled")
@@ -74,7 +75,7 @@
7475
iseq.pointee = Instruction.breakpoint.headSlot(threadingModel: self.threadingModel)
7576
}
7677

77-
package mutating func disableBreakpoint(address: Int) throws {
78+
package mutating func disableBreakpoint(address: Int) throws(Error) {
7879
print("attempt to toggle a breakpoint at \(address)")
7980

8081
guard let oldCodeSlot = self.breakpoints[address] else {
@@ -85,11 +86,12 @@
8586
guard let iseq = self.instance.handle.wasmToIseqMapping[address] else {
8687
throw Error.noInstructionMappingAvailable(address)
8788
}
88-
iseq.pointee = oldCodeSlot
8989
self.breakpoints[address] = nil
90+
iseq.pointee = oldCodeSlot
9091
}
9192

92-
mutating func enableBreakpoint(functionIndex: FunctionIndex, offset: Int) {
93+
package func run() throws {
94+
try self.entrypointFunction()
9395
}
9496

9597
/// Array of addresses in the Wasm binary of executed instructions on the call stack.

Sources/WasmKit/Execution/Errors.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ struct Backtrace: CustomStringConvertible, Sendable {
1616

1717
/// Textual description of the backtrace.
1818
var description: String {
19-
print("backtrace contains \(symbols.count) symbols")
20-
return symbols.enumerated().map { (index, symbol) in
19+
symbols.enumerated().map { (index, symbol) in
2120
let name = symbol.name ?? "unknown"
2221
return " \(index): (\(symbol.address)) \(name)"
2322
}.joined(separator: "\n")
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#if WasmDebuggingSupport
2+
3+
import Testing
4+
import WAT
5+
@testable import WasmKit
6+
7+
private let trivialModuleWAT = """
8+
(module
9+
(func (export "_start") (result i32) (local $x i32)
10+
(i32.const 42)
11+
(i32.const 0)
12+
(i32.eqz)
13+
(drop)
14+
(local.set $x)
15+
(local.get $x)
16+
)
17+
)
18+
"""
19+
20+
@Suite
21+
struct DebuggerTests {
22+
@Test
23+
func stopAtEntrypoint() throws {
24+
let store = Store(engine: Engine())
25+
let bytes = try wat2wasm(trivialModuleWAT)
26+
print(bytes.count)
27+
let module = try parseWasm(bytes: bytes)
28+
var debugger = try Debugger(module: module, store: store, imports: [:])
29+
30+
try debugger.stopAtEntrypoint()
31+
32+
#expect(throws: Execution.Breakpoint.self) {
33+
try debugger.run()
34+
}
35+
}
36+
}
37+
38+
#endif

0 commit comments

Comments
 (0)