-
Notifications
You must be signed in to change notification settings - Fork 19
Basic GDB remote protocol scaffolding #204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MaxDesiatov
wants to merge
16
commits into
main
Choose a base branch
from
maxd/lldb-remote-protocol
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
c5a5b35
Basic LLDB remote protocol scaffolding
MaxDesiatov f89a842
Implement more packet handling, update naming
MaxDesiatov 3601dae
Provide original license notice in `wasmkit-gdb-tool/Entrypoint.swift`
MaxDesiatov 7439bb7
Apply formatter
MaxDesiatov a94257c
Make naming of types more specific to avoid future collisions
MaxDesiatov c652f64
More cleanups for type naming
MaxDesiatov 6395039
Remove unused `import struct Foundation.Date`
MaxDesiatov c6ea692
Remove unused `else` clause from command decoder
MaxDesiatov 2b74e4a
Add `swift-nio` in `SWIFTCI_USE_LOCAL_DEPS` clause
MaxDesiatov 0b8c293
Add FIXME note for `.supportedFeatures` response
MaxDesiatov 6cdf164
Use `Logger`, `NIOAsyncChannel`
MaxDesiatov 345ddb3
Add required dependencies to `[email protected]`
MaxDesiatov 1309bae
Add `iSeqToWasmMapping` to `InstanceEntity`
MaxDesiatov 4f17bf5
Fix formatting and iOS compatibility
MaxDesiatov b1f54cd
Update [email protected]
MaxDesiatov 838381d
Update doc comment of `func instantiate` in `Module.swift`
MaxDesiatov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
// swift-tools-version:6.1 | ||
|
||
import PackageDescription | ||
|
||
import class Foundation.ProcessInfo | ||
|
||
let DarwinPlatforms: [Platform] = [.macOS, .iOS, .watchOS, .tvOS, .visionOS] | ||
|
||
let package = Package( | ||
name: "WasmKit", | ||
platforms: [.macOS(.v10_13), .iOS(.v12)], | ||
products: [ | ||
.executable(name: "wasmkit-cli", targets: ["CLI"]), | ||
.library(name: "WasmKit", targets: ["WasmKit"]), | ||
.library(name: "WasmKitWASI", targets: ["WasmKitWASI"]), | ||
.library(name: "WASI", targets: ["WASI"]), | ||
.library(name: "WasmParser", targets: ["WasmParser"]), | ||
.library(name: "WAT", targets: ["WAT"]), | ||
.library(name: "WIT", targets: ["WIT"]), | ||
.library(name: "_CabiShims", targets: ["_CabiShims"]), | ||
], | ||
traits: [ | ||
.default(enabledTraits: []), | ||
"WasmDebuggingSupport" | ||
], | ||
targets: [ | ||
.executableTarget( | ||
name: "CLI", | ||
dependencies: [ | ||
"WAT", | ||
"WasmKit", | ||
"WasmKitWASI", | ||
.product(name: "ArgumentParser", package: "swift-argument-parser"), | ||
.product(name: "SystemPackage", package: "swift-system"), | ||
], | ||
exclude: ["CMakeLists.txt"] | ||
), | ||
|
||
.target( | ||
name: "WasmKit", | ||
dependencies: [ | ||
"_CWasmKit", | ||
"WasmParser", | ||
"WasmTypes", | ||
"SystemExtras", | ||
.product(name: "SystemPackage", package: "swift-system"), | ||
], | ||
exclude: ["CMakeLists.txt"] | ||
), | ||
.target(name: "_CWasmKit"), | ||
.target( | ||
name: "WasmKitFuzzing", | ||
dependencies: ["WasmKit"], | ||
path: "FuzzTesting/Sources/WasmKitFuzzing" | ||
), | ||
.testTarget( | ||
name: "WasmKitTests", | ||
dependencies: ["WasmKit", "WAT", "WasmKitFuzzing"], | ||
exclude: ["ExtraSuite"] | ||
), | ||
|
||
.target( | ||
name: "WAT", | ||
dependencies: ["WasmParser"], | ||
exclude: ["CMakeLists.txt"] | ||
), | ||
.testTarget(name: "WATTests", dependencies: ["WAT"]), | ||
|
||
.target( | ||
name: "WasmParser", | ||
dependencies: [ | ||
"WasmTypes", | ||
.product(name: "SystemPackage", package: "swift-system"), | ||
], | ||
exclude: ["CMakeLists.txt"] | ||
), | ||
.testTarget(name: "WasmParserTests", dependencies: ["WasmParser"]), | ||
|
||
.target(name: "WasmTypes", exclude: ["CMakeLists.txt"]), | ||
|
||
.target( | ||
name: "WasmKitWASI", | ||
dependencies: ["WasmKit", "WASI"], | ||
exclude: ["CMakeLists.txt"] | ||
), | ||
.target( | ||
name: "WASI", | ||
dependencies: ["WasmTypes", "SystemExtras"], | ||
exclude: ["CMakeLists.txt"] | ||
), | ||
.testTarget(name: "WASITests", dependencies: ["WASI", "WasmKitWASI"]), | ||
|
||
.target( | ||
name: "SystemExtras", | ||
dependencies: [ | ||
.product(name: "SystemPackage", package: "swift-system") | ||
], | ||
exclude: ["CMakeLists.txt"], | ||
swiftSettings: [ | ||
.define("SYSTEM_PACKAGE_DARWIN", .when(platforms: DarwinPlatforms)) | ||
] | ||
), | ||
|
||
.executableTarget( | ||
name: "WITTool", | ||
dependencies: [ | ||
"WIT", | ||
"WITOverlayGenerator", | ||
"WITExtractor", | ||
.product(name: "ArgumentParser", package: "swift-argument-parser"), | ||
] | ||
), | ||
|
||
.target(name: "WIT"), | ||
.testTarget(name: "WITTests", dependencies: ["WIT"]), | ||
|
||
.target(name: "WITOverlayGenerator", dependencies: ["WIT"]), | ||
.target(name: "_CabiShims"), | ||
|
||
.target(name: "WITExtractor"), | ||
.testTarget(name: "WITExtractorTests", dependencies: ["WITExtractor", "WIT"]), | ||
|
||
.target(name: "GDBRemoteProtocol", | ||
dependencies: [ | ||
.product(name: "NIOCore", package: "swift-nio"), | ||
] | ||
), | ||
.testTarget(name: "GDBRemoteProtocolTests", dependencies: ["GDBRemoteProtocol"]), | ||
Comment on lines
123
to
129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These two modules are meant to be completely independent of WasmKit and in the future could live in a separate package to be adopted in any suitable environment that needs debugging facilities, not just Wasm. |
||
|
||
.target( | ||
name: "WasmKitGDBHandler", | ||
dependencies: [ | ||
.product(name: "NIOCore", package: "swift-nio"), | ||
"WasmKit", | ||
"GDBRemoteProtocol", | ||
], | ||
), | ||
|
||
.executableTarget( | ||
name: "wasmkit-gdb-tool", | ||
dependencies: [ | ||
.product(name: "ArgumentParser", package: "swift-argument-parser"), | ||
.product(name: "NIOCore", package: "swift-nio"), | ||
.product(name: "NIOPosix", package: "swift-nio"), | ||
"GDBRemoteProtocol", | ||
"WasmKitGDBHandler", | ||
] | ||
), | ||
], | ||
) | ||
|
||
if ProcessInfo.processInfo.environment["SWIFTCI_USE_LOCAL_DEPS"] == nil { | ||
package.dependencies += [ | ||
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.1"), | ||
.package(url: "https://github.com/apple/swift-system", from: "1.5.0"), | ||
.package(url: "https://github.com/apple/swift-nio", from: "2.86.2"), | ||
] | ||
} else { | ||
package.dependencies += [ | ||
.package(path: "../swift-argument-parser"), | ||
.package(path: "../swift-system"), | ||
MaxDesiatov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
} | ||
|
||
#if !os(Windows) | ||
// Add build tool plugins only for non-Windows platforms | ||
package.products.append(contentsOf: [ | ||
.plugin(name: "WITOverlayPlugin", targets: ["WITOverlayPlugin"]), | ||
.plugin(name: "WITExtractorPlugin", targets: ["WITExtractorPlugin"]), | ||
]) | ||
|
||
package.targets.append(contentsOf: [ | ||
.plugin(name: "WITOverlayPlugin", capability: .buildTool(), dependencies: ["WITTool"]), | ||
.plugin(name: "GenerateOverlayForTesting", capability: .buildTool(), dependencies: ["WITTool"]), | ||
.testTarget( | ||
name: "WITOverlayGeneratorTests", | ||
dependencies: ["WITOverlayGenerator", "WasmKit", "WasmKitWASI"], | ||
exclude: ["Fixtures", "Compiled", "Generated", "EmbeddedSupport"], | ||
plugins: [.plugin(name: "GenerateOverlayForTesting")] | ||
), | ||
.plugin( | ||
name: "WITExtractorPlugin", | ||
capability: .command( | ||
intent: .custom(verb: "extract-wit", description: "Extract WIT definition from Swift module"), | ||
permissions: [] | ||
), | ||
dependencies: ["WITTool"] | ||
), | ||
.testTarget( | ||
name: "WITExtractorPluginTests", | ||
exclude: ["Fixtures"] | ||
), | ||
]) | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/// See GDB and LLDB remote protocol documentation for more details: | ||
/// * https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Packets.html | ||
/// * https://lldb.llvm.org/resources/lldbgdbremote.html | ||
package struct GDBHostCommand: Equatable { | ||
package enum Kind: Equatable { | ||
// Currently listed in the order that LLDB sends them in. | ||
case generalRegisters | ||
case startNoAckMode | ||
case firstThreadInfo | ||
case supportedFeatures | ||
case isThreadSuffixSupported | ||
case listThreadsInStopReply | ||
case hostInfo | ||
case vContSupportedActions | ||
case isVAttachOrWaitSupported | ||
case enableErrorStrings | ||
case processInfo | ||
case currentThreadID | ||
|
||
package init?(rawValue: String) { | ||
switch rawValue { | ||
case "g": | ||
self = .generalRegisters | ||
case "QStartNoAckMode": | ||
self = .startNoAckMode | ||
case "qSupported": | ||
self = .supportedFeatures | ||
case "QThreadSuffixSupported": | ||
self = .isThreadSuffixSupported | ||
case "QListThreadsInStopReply": | ||
self = .listThreadsInStopReply | ||
case "qHostInfo": | ||
self = .hostInfo | ||
case "vCont?": | ||
self = .vContSupportedActions | ||
case "qVAttachOrWaitSupported": | ||
self = .isVAttachOrWaitSupported | ||
case "QEnableErrorStrings": | ||
self = .enableErrorStrings | ||
case "qProcessInfo": | ||
self = .processInfo | ||
case "qC": | ||
self = .currentThreadID | ||
default: | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
package let kind: Kind | ||
|
||
package let arguments: String | ||
|
||
package init(kind: Kind, arguments: String) { | ||
self.kind = kind | ||
self.arguments = arguments | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import NIOCore | ||
|
||
extension ByteBuffer { | ||
var isChecksumDelimiterAtReader: Bool { | ||
self.peekInteger(as: UInt8.self) == UInt8(ascii: "#") | ||
} | ||
|
||
var isArgumentsDelimiterAtReader: Bool { | ||
self.peekInteger(as: UInt8.self) == UInt8(ascii: ":") | ||
} | ||
} | ||
|
||
package struct GDBHostCommandDecoder: ByteToMessageDecoder { | ||
enum Error: Swift.Error { | ||
case expectedCommandStart | ||
case unknownCommandKind(String) | ||
case expectedChecksum | ||
case checksumIncorrect | ||
} | ||
|
||
package typealias InboundOut = GDBPacket<GDBHostCommand> | ||
|
||
private var accummulatedKind = [UInt8]() | ||
private var accummulatedArguments = [UInt8]() | ||
|
||
package init() {} | ||
|
||
private var accummulatedSum = 0 | ||
package var accummulatedChecksum: UInt8 { | ||
UInt8(self.accummulatedSum % 256) | ||
} | ||
|
||
mutating package func decode(buffer: inout ByteBuffer) throws -> GDBPacket<GDBHostCommand>? { | ||
// Command start delimiters. | ||
let firstStartDelimiter = buffer.readInteger(as: UInt8.self) | ||
let secondStartDelimiter = buffer.readInteger(as: UInt8.self) | ||
guard | ||
firstStartDelimiter == UInt8(ascii: "+") | ||
&& secondStartDelimiter == UInt8(ascii: "$") | ||
else { | ||
if let firstStartDelimiter, let secondStartDelimiter { | ||
print("unexpected delimiter: \(Character(UnicodeScalar(firstStartDelimiter)))\(Character(UnicodeScalar(secondStartDelimiter)))") | ||
} | ||
throw Error.expectedCommandStart | ||
} | ||
|
||
// Byte offset for command start. | ||
while !buffer.isChecksumDelimiterAtReader && !buffer.isArgumentsDelimiterAtReader, | ||
let char = buffer.readInteger(as: UInt8.self) | ||
{ | ||
self.accummulatedSum += Int(char) | ||
self.accummulatedKind.append(char) | ||
} | ||
|
||
if buffer.isArgumentsDelimiterAtReader, | ||
let argumentsDelimiter = buffer.readInteger(as: UInt8.self) | ||
{ | ||
self.accummulatedSum += Int(argumentsDelimiter) | ||
|
||
while !buffer.isChecksumDelimiterAtReader, let char = buffer.readInteger(as: UInt8.self) { | ||
self.accummulatedSum += Int(char) | ||
self.accummulatedArguments.append(char) | ||
} | ||
} | ||
|
||
// Command checksum delimiter. | ||
if !buffer.isChecksumDelimiterAtReader { | ||
// If delimiter not available yet, return `nil` to indicate that the caller needs to top up the buffer. | ||
return nil | ||
} | ||
|
||
defer { | ||
self.accummulatedKind = [] | ||
self.accummulatedArguments = [] | ||
self.accummulatedSum = 0 | ||
} | ||
|
||
let kindString = String(decoding: self.accummulatedKind, as: UTF8.self) | ||
|
||
if let commandKind = GDBHostCommand.Kind(rawValue: kindString) { | ||
buffer.moveReaderIndex(forwardBy: 1) | ||
|
||
guard let checksumString = buffer.readString(length: 2), | ||
let first = checksumString.first?.hexDigitValue, | ||
let last = checksumString.last?.hexDigitValue | ||
else { | ||
throw Error.expectedChecksum | ||
} | ||
|
||
guard (first * 16) + last == self.accummulatedChecksum else { | ||
// FIXME: better diagnostics | ||
throw Error.checksumIncorrect | ||
} | ||
|
||
return .init( | ||
payload: .init( | ||
kind: commandKind, | ||
arguments: String(decoding: self.accummulatedArguments, as: UTF8.self) | ||
), | ||
checksum: accummulatedChecksum, | ||
) | ||
} else { | ||
throw Error.unknownCommandKind(kindString) | ||
} | ||
} | ||
|
||
mutating package func decode( | ||
context: ChannelHandlerContext, | ||
buffer: inout ByteBuffer | ||
) throws -> DecodingState { | ||
print(buffer.peekString(length: buffer.readableBytes)!) | ||
|
||
guard let command = try self.decode(buffer: &buffer) else { | ||
return .needMoreData | ||
} | ||
|
||
// Shift by checksum bytes | ||
context.fireChannelRead(wrapInboundOut(command)) | ||
return .continue | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package struct GDBPacket<Payload> { | ||
package let payload: Payload | ||
|
||
package let checksum: UInt8 | ||
|
||
package init(payload: Payload, checksum: UInt8) { | ||
self.payload = payload | ||
self.checksum = checksum | ||
} | ||
} | ||
|
||
extension GDBPacket: Equatable where Payload: Equatable {} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added separate
Package.swift
manifest to hide debugger support behind a trait for now, and traits aren't supported in 6.0.