Skip to content

Commit b1e69ac

Browse files
committed
Add qRegisterInfo parsing to GDBHostCommand.init
1 parent ad68248 commit b1e69ac

File tree

3 files changed

+58
-33
lines changed

3 files changed

+58
-33
lines changed

Sources/GDBRemoteProtocol/GDBHostCommand.swift

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/// See GDB and LLDB remote protocol documentation for more details:
22
/// * https://sourceware.org/gdb/current/onlinedocs/gdb.html/General-Query-Packets.html
33
/// * https://lldb.llvm.org/resources/lldbgdbremote.html
4-
package struct GDBHostCommand: Equatable {
4+
package struct GDBHostCommand {
5+
enum Error: Swift.Error {
6+
case unexpectedArgumentsValue
7+
case unknownCommand(kind: String, arguments: String)
8+
}
9+
510
package enum Kind: String, Equatable {
611
// Currently listed in the order that LLDB sends them in.
712
case startNoAckMode
@@ -17,6 +22,7 @@ package struct GDBHostCommand: Equatable {
1722
case firstThreadInfo
1823
case subsequentThreadInfo
1924
case targetStatus
25+
case registerInfo
2026

2127
case generalRegisters
2228

@@ -61,8 +67,22 @@ package struct GDBHostCommand: Equatable {
6167

6268
package let arguments: String
6369

64-
package init(kind: Kind, arguments: String) {
65-
self.kind = kind
66-
self.arguments = arguments
70+
package init(kind: String, arguments: String) throws {
71+
let registerInfoPrefix = "qRegisterInfo"
72+
if kind.starts(with: registerInfoPrefix) {
73+
self.kind = .registerInfo
74+
75+
guard arguments.isEmpty else {
76+
throw Error.unexpectedArgumentsValue
77+
}
78+
self.arguments = String(kind.dropFirst(registerInfoPrefix.count))
79+
return
80+
} else if let kind = Kind(rawValue: kind) {
81+
self.kind = kind
82+
} else {
83+
throw Error.unknownCommand(kind: kind, arguments: arguments)
84+
}
85+
86+
self.arguments = arguments
6787
}
6888
}

Sources/GDBRemoteProtocol/GDBHostCommandDecoder.swift

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ package struct GDBHostCommandDecoder: ByteToMessageDecoder {
1717
case expectedCommandStart
1818
case unknownCommandKind(String)
1919
case expectedChecksum
20-
case checksumIncorrect
20+
case checksumIncorrect(expectedChecksum: Int, receivedChecksum: UInt8)
2121
}
2222

2323
package typealias InboundOut = GDBPacket<GDBHostCommand>
@@ -106,37 +106,34 @@ package struct GDBHostCommandDecoder: ByteToMessageDecoder {
106106
self.accummulatedSum = 0
107107
}
108108

109-
let kindString = String(decoding: self.accummulatedKind, as: UTF8.self)
109+
buffer.moveReaderIndex(forwardBy: 1)
110110

111-
if let commandKind = GDBHostCommand.Kind(rawValue: kindString) {
112-
buffer.moveReaderIndex(forwardBy: 1)
111+
guard let checksumString = buffer.readString(length: 2),
112+
let first = checksumString.first?.hexDigitValue,
113+
let last = checksumString.last?.hexDigitValue
114+
else {
115+
throw Error.expectedChecksum
116+
}
113117

114-
guard let checksumString = buffer.readString(length: 2),
115-
let first = checksumString.first?.hexDigitValue,
116-
let last = checksumString.last?.hexDigitValue
117-
else {
118-
throw Error.expectedChecksum
119-
}
118+
let expectedChecksum = (first * 16) + last
120119

121-
guard (first * 16) + last == self.accummulatedChecksum else {
122-
// FIXME: better diagnostics
123-
throw Error.checksumIncorrect
124-
}
120+
guard expectedChecksum == self.accummulatedChecksum else {
121+
throw Error.checksumIncorrect(
122+
expectedChecksum: expectedChecksum,
123+
receivedChecksum: self.accummulatedChecksum
124+
)
125+
}
125126

126-
if commandKind == .startNoAckMode {
127-
self.isNoAckModeRequested = true
128-
}
127+
let payload = try GDBHostCommand(
128+
kind: String(decoding: self.accummulatedKind, as: UTF8.self),
129+
arguments: String(decoding: self.accummulatedArguments, as: UTF8.self)
130+
)
129131

130-
return .init(
131-
payload: .init(
132-
kind: commandKind,
133-
arguments: String(decoding: self.accummulatedArguments, as: UTF8.self)
134-
),
135-
checksum: accummulatedChecksum,
136-
)
137-
} else {
138-
throw Error.unknownCommandKind(kindString)
132+
if payload.kind == .startNoAckMode {
133+
self.isNoAckModeRequested = true
139134
}
135+
136+
return .init(payload: payload, checksum: accummulatedChecksum)
140137
}
141138

142139
mutating package func decode(

Sources/WasmKitGDBHandler/WasmKitDebugger.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ package actor WasmKitDebugger {
3737
])
3838

3939
case .supportedFeatures:
40-
// FIXME: should return a different set of supported features instead of echoing.
41-
.raw(command.arguments)
40+
.raw("qXfer:libraries:read+;PacketSize=1000;")
4241

4342
case .vContSupportedActions:
4443
.vContSupportedActions([.continue, .step])
4544

4645
case .isVAttachOrWaitSupported, .enableErrorStrings:
4746
.empty
4847
case .processInfo:
49-
.raw("pid:1;parent-pid:1;arch:wasm32;endian:little;ptrsize:4;")
48+
.keyValuePairs([
49+
"pid": "1",
50+
"parent-pid": "1",
51+
"arch": "wasm32",
52+
"endian": "little",
53+
"ptrsize": "4",
54+
])
5055

5156
case .currentThreadID:
5257
.raw("QC1")
@@ -63,6 +68,9 @@ package actor WasmKitDebugger {
6368
"reason": "trace",
6469
])
6570

71+
case .registerInfo:
72+
.raw("name:pc;alt-name:pc;bitsize:64;offset:0;encoding:uint;format:hex;set:General Purpose Registers;gcc:16;dwarf:16;generic:pc;")
73+
6674
case .generalRegisters:
6775
fatalError()
6876
}

0 commit comments

Comments
 (0)