Skip to content

Commit c3b9ebd

Browse files
committed
Add DebuggerExecution wrapper type
1 parent 5ae3831 commit c3b9ebd

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package struct DebuggerExecution: ~Copyable {
2+
let valueStack: Sp
3+
let execution: Execution
4+
let store: Store
5+
6+
package init(store: Store) {
7+
let limit = store.engine.configuration.stackSize / MemoryLayout<StackSlot>.stride
8+
self.valueStack = UnsafeMutablePointer<StackSlot>.allocate(capacity: limit)
9+
self.store = store
10+
self.execution = Execution(store: StoreRef(store), stackEnd: valueStack.advanced(by: limit))
11+
}
12+
13+
package func captureBacktrace() -> Backtrace {
14+
return Execution.captureBacktrace(sp: self.valueStack, store: self.store)
15+
}
16+
17+
deinit {
18+
valueStack.deallocate()
19+
}
20+
}
21+

Sources/WasmKit/Execution/Errors.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import WasmTypes
33
import struct WasmParser.Import
44

55
/// The backtrace of the trap.
6-
struct Backtrace: CustomStringConvertible, Sendable {
6+
package struct Backtrace: CustomStringConvertible, Sendable {
77
/// A symbol in the backtrace.
8-
struct Symbol {
8+
package struct Symbol {
99
/// The name of the symbol.
1010
let name: String?
1111
let debuggingAddress: DebuggingAddress
@@ -31,8 +31,9 @@ struct Backtrace: CustomStringConvertible, Sendable {
3131
let symbols: [Symbol]
3232

3333
/// Textual description of the backtrace.
34-
var description: String {
35-
symbols.enumerated().map { (index, symbol) in
34+
package var description: String {
35+
print("backtrace contains \(symbols.count) symbols")
36+
return symbols.enumerated().map { (index, symbol) in
3637
let name = symbol.name ?? "unknown"
3738
return " \(index): (\(symbol.debuggingAddress)) \(name)"
3839
}.joined(separator: "\n")

Sources/WasmKit/Execution/Execution.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _CWasmKit
44
///
55
/// Each new invocation through exported function has a separate ``Execution``
66
/// even though the invocation happens during another invocation.
7-
struct Execution {
7+
struct Execution: ~Copyable {
88
/// The reference to the ``Store`` associated with the execution.
99
let store: StoreRef
1010
/// The end of the VM stack space.
@@ -14,6 +14,11 @@ struct Execution {
1414
/// - Note: If the trap is set, it must be released manually.
1515
private var trap: (error: UnsafeRawPointer, sp: Sp)? = nil
1616

17+
package init(store: StoreRef, stackEnd: UnsafeMutablePointer<StackSlot>) {
18+
self.store = store
19+
self.stackEnd = stackEnd
20+
}
21+
1722
/// Executes the given closure with a new execution state associated with
1823
/// the given ``Store`` instance.
1924
static func with<T>(

Sources/WasmKitGDBHandler/WasmKitDebugger.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,24 @@ import Synchronization
44
import SystemPackage
55
import WasmKit
66

7-
package actor WasmKitDebugger {
7+
package actor WasmKitGDBHandler {
88
enum Error: Swift.Error {
99
case unknownTransferArguments
1010
}
1111

1212
private let module: Module
1313
private let moduleFilePath: FilePath
1414
private let logger: Logger
15+
private let debuggerExecution: DebuggerExecution
16+
private let instance: Instance
1517

1618
package init(logger: Logger, moduleFilePath: FilePath) throws {
1719
self.logger = logger
1820
self.module = try parseWasm(filePath: moduleFilePath)
1921
self.moduleFilePath = moduleFilePath
22+
let store = Store(engine: Engine())
23+
self.debuggerExecution = DebuggerExecution(store: store)
24+
self.instance = try module.instantiate(store: store)
2025
}
2126

2227
package func handle(command: GDBHostCommand) throws -> GDBTargetResponse {
@@ -106,7 +111,11 @@ package actor WasmKitDebugger {
106111
case .readMemory:
107112
responseKind = .empty
108113

109-
case .wasmCallStack, .generalRegisters:
114+
case .wasmCallStack:
115+
print(self.debuggerExecution.captureBacktrace())
116+
responseKind = .empty
117+
118+
case .generalRegisters:
110119
fatalError()
111120
}
112121

Sources/wasmkit-gdb-tool/Entrypoint.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct Entrypoint: AsyncParsableCommand {
8181
/* the server will now be accepting connections */
8282
logger.info("listening on port \(port)")
8383

84-
let debugger = try WasmKitDebugger(logger: logger, moduleFilePath: self.wasmModulePath)
84+
let debugger = try WasmKitGDBHandler(logger: logger, moduleFilePath: self.wasmModulePath)
8585

8686
try await withThrowingDiscardingTaskGroup { group in
8787
try await serverChannel.executeThenClose { serverChannelInbound in

0 commit comments

Comments
 (0)