-
Notifications
You must be signed in to change notification settings - Fork 595
/
Copy pathgdbserver.ts
243 lines (220 loc) · 8.58 KB
/
gdbserver.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
namespace pxt {
function hex2(n: number) {
return ("0" + n.toString(16)).slice(-2)
}
function hex2str(h: string) {
return U.uint8ArrayToString(U.fromHex(h))
}
function str2hex(h: string) {
return U.toHex(U.stringToUint8Array(h))
}
export class GDBServer {
private q = new U.PromiseQueue();
private dataBuf = "";
private numSent = 0;
private pktSize = 400;
trace = false
bmpMode = true
targetInfo = ""
private onResponse: (s: string) => void;
private onEvent = (s: string) => { }
constructor(public io: pxt.TCPIO) {
this.io.onData = b => this.onData(b)
}
private onData(buf: Uint8Array) {
this.dataBuf += U.uint8ArrayToString(buf)
while (this.dataBuf.length > 0) {
let ch = this.dataBuf[0]
if (ch == '+')
this.dataBuf = this.dataBuf.slice(1)
else if (ch == '$' || ch == '%') {
let resp = this.decodeResp(this.dataBuf.slice(1))
if (resp != null) {
if (ch == '$') {
this.io.sendPacketAsync(this.buildCmd("+"));
if (this.onResponse)
this.onResponse(resp)
else {
// ignore unexpected responses right after connection
// they are likely left-over from a previous session
if (this.numSent > 0)
this.io.error("unexpected response: " + resp)
}
} else {
this.onEvent(resp)
}
} else {
break
}
} else {
this.io.error("invalid character: " + ch)
}
}
}
private buildCmd(cmd: string) {
if (cmd == "+")
return U.stringToUint8Array(cmd)
let r = ""
for (let i = 0; i < cmd.length; ++i) {
let ch = cmd.charAt(i)
if (ch == '}' || ch == '#' || ch == '$') {
r += '}'
r += String.fromCharCode(ch.charCodeAt(0) ^ 0x20)
} else {
r += ch
}
}
let ch = 0
cmd = r
for (let i = 0; i < cmd.length; ++i) {
ch = (ch + cmd.charCodeAt(i)) & 0xff
}
r = "$" + cmd + "#" + hex2(ch)
return U.stringToUint8Array(r)
}
private decodeResp(resp: string) {
let r = ""
for (let i = 0; i < resp.length; ++i) {
let ch = resp[i]
if (ch == '}') {
++i
r += String.fromCharCode(resp.charCodeAt(i) ^ 0x20)
} else if (ch == '*') {
++i
let rep = resp.charCodeAt(i) - 29
let ch = r.charAt(r.length - 1)
while (rep-- > 0)
r += ch
} else if (ch == '#') {
let checksum = resp.slice(i + 1, i + 3)
if (checksum.length == 2) {
// TODO validate checksum?
this.dataBuf = resp.slice(i + 3)
return r
} else {
// incomplete
return null
}
} else {
r += ch
}
}
return null
}
sendCmdOKAsync(cmd: string) {
return this.sendCmdAsync(cmd, r => r == "OK")
}
error(msg: string) {
this.io.error(msg)
this.io.disconnectAsync()
}
sendCmdAsync(cmd: string, respTest?: (resp: string) => boolean) {
this.numSent++
const cmd2 = this.buildCmd(cmd)
return this.q.enqueue("one", () =>
respTest === null ? this.io.sendPacketAsync(cmd2).then(() => null as string) :
new Promise<string>((resolve) => {
this.onResponse = v => {
this.onResponse = null
if (this.trace)
pxt.log(`GDB: '${cmd}' -> '${v}'`)
if (respTest !== undefined && !respTest(v))
this.error(`Invalid GDB command response: '${cmd}' -> '${v}'`)
resolve(v)
}
this.io.sendPacketAsync(cmd2);
}))
}
sendRCmdAsync(cmd: string) {
return this.sendMCmdAsync("qRcmd," + str2hex(cmd))
}
sendMCmdAsync(cmd: string) {
this.numSent++
const cmd2 = this.buildCmd(cmd)
let r = ""
return this.q.enqueue("one", () =>
new Promise<string>((resolve) => {
this.onResponse = v => {
if (v != "OK" && v[0] == "O")
r += hex2str(v.slice(1))
else {
if (v != "OK")
r += " - " + v
this.onResponse = null
if (this.trace)
pxt.log(`Final GDB: '${cmd}' -> '${r}'`)
resolve(r)
}
}
this.io.sendPacketAsync(cmd2);
}))
}
write32Async(addr: number, data: number): Promise<void> {
let b = new Uint8Array(4)
HF2.write32(b, 0, data)
return this.writeMemAsync(addr, b)
}
writeMemAsync(addr: number, data: Uint8Array): Promise<void> {
const maxBytes = this.pktSize / 2 - 10
U.assert(data.length < maxBytes)
return this.sendCmdOKAsync("M" + addr.toString(16) + "," +
data.length.toString(16) + ":" + U.toHex(data))
.then(r => {
pxt.log(r)
})
}
readMemAsync(addr: number, bytes: number): Promise<Uint8Array> {
const maxBytes = this.pktSize / 2 - 6
if (bytes > maxBytes) {
const result = new Uint8Array(bytes)
const loop = (ptr: number): Promise<Uint8Array> => {
const len = Math.min(bytes - ptr, maxBytes)
if (len == 0)
return Promise.resolve(result)
return this.readMemAsync(addr + ptr, len)
.then(part => {
U.memcpy(result, ptr, part)
return loop(ptr + len)
})
}
return loop(0)
}
return this.sendCmdAsync("m" + addr.toString(16) + "," + bytes.toString(16))
.then(res => U.fromHex(res))
}
private initBMPAsync() {
return Promise.resolve()
.then(() => this.sendRCmdAsync("swdp_scan"))
.then(r => {
this.targetInfo = r
return this.sendCmdAsync("vAttach;1", r => r[0] == "T")
})
.then(() => { })
}
initAsync() {
return U.delay(1000)
.then(() => this.sendCmdAsync("!")) // extended mode
.then(() => this.sendCmdAsync("qSupported"))
.then(res => {
let features: any = {}
res = ";" + res + ";"
res = res
.replace(/;([^;]+)[=:]([^:;]+)/g, (f, k, v) => {
features[k] = v
return ";"
})
this.pktSize = parseInt(features["PacketSize"]) || 1024
pxt.log("GDB-server caps: " + JSON.stringify(features)
+ " " + res.replace(/;+/g, ";"))
if (this.bmpMode)
return this.initBMPAsync()
else {
// continue
return this.sendCmdAsync("c")
.then(() => { })
}
// return this.sendCmdAsync("?") // reason for stop
})
}
}
}