forked from eclipse-cdt-cloud/cdt-gdb-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMIParser.ts
380 lines (350 loc) · 11.3 KB
/
MIParser.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*********************************************************************
* Copyright (c) 2018 QNX Software Systems and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*********************************************************************/
import { Readable } from 'stream';
import { logger } from '@vscode/debugadapter/lib/logger';
import { IGDBBackend } from './types/gdb';
import * as utf8 from 'utf8';
type CommandQueue = {
[key: string]: (resultClass: string, resultData: any) => void;
};
export class MIParser {
protected line = '';
protected pos = 0;
protected commandQueue: CommandQueue = {};
protected waitReady?: (value?: void | PromiseLike<void>) => void;
constructor(protected gdb: IGDBBackend) {}
public parse(stream: Readable): Promise<void> {
return new Promise((resolve) => {
this.waitReady = resolve;
const lineBreakRegex = /\r?\n/;
let buff = '';
stream.on('data', (chunk) => {
const newChunk = chunk.toString();
let regexArray = lineBreakRegex.exec(newChunk);
if (regexArray) {
regexArray.index += buff.length;
}
buff += newChunk;
while (regexArray) {
const line = buff.slice(0, regexArray.index);
this.parseLine(line);
buff = buff.slice(regexArray.index + regexArray[0].length);
regexArray = lineBreakRegex.exec(buff);
}
});
});
}
public parseLine(line: string) {
this.line = line;
this.pos = 0;
this.handleLine();
}
public queueCommand(
token: number,
command: (resultClass: string, resultData: any) => void
) {
this.commandQueue[token] = command;
}
protected peek() {
if (this.pos < this.line.length) {
return this.line[this.pos];
} else {
return null;
}
}
protected next() {
if (this.pos < this.line.length) {
return this.line[this.pos++];
} else {
return null;
}
}
protected back() {
this.pos--;
}
protected restOfLine() {
return this.line.substr(this.pos);
}
protected handleToken(firstChar: string) {
let token = firstChar;
let c = this.next();
while (c && c >= '0' && c <= '9') {
token += c;
c = this.next();
}
this.back();
return token;
}
protected handleCString() {
let c = this.next();
if (!c || c !== '"') {
return null;
}
let cstring = '';
let octal = '';
mainloop: for (c = this.next(); c; c = this.next()) {
if (octal) {
octal += c;
if (octal.length == 3) {
cstring += String.fromCodePoint(parseInt(octal, 8));
octal = '';
}
continue;
}
switch (c) {
case '"':
break mainloop;
case '\\':
c = this.next();
if (c) {
switch (c) {
case 'n':
cstring += '\n';
break;
case 't':
cstring += '\t';
break;
case 'r':
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
octal = c;
break;
default:
cstring += c;
}
} else {
this.back();
}
break;
default:
cstring += c;
}
}
try {
return utf8.decode(cstring);
} catch (err) {
logger.error(
`Failed to decode cstring '${cstring}'. ${JSON.stringify(err)}`
);
return cstring;
}
}
protected handleString() {
let str = '';
for (let c = this.next(); c; c = this.next()) {
if (c === '=' || c === ',') {
this.back();
return str;
} else {
str += c;
}
}
return str;
}
protected handleObject() {
let c = this.next();
const result: any = {};
if (c === '{') {
c = this.next();
if (c !== '"') {
// oject contains name-value pairs
while (c !== '}') {
if (c !== ',') {
this.back();
}
const name = this.handleString();
if (this.next() === '=') {
result[name] = this.handleValue();
}
c = this.next();
}
} else {
// "object" contains just values
this.back();
let key = 0;
while (c !== '}') {
let value = this.handleCString();
if (value) result[key++] = value;
c = this.next();
}
}
}
if (c === '}') {
return result;
} else {
return null;
}
}
protected handleArray() {
let c = this.next();
const result: any[] = [];
if (c === '[') {
c = this.next();
while (c !== ']') {
if (c !== ',') {
this.back();
}
result.push(this.handleValue());
c = this.next();
}
}
if (c === ']') {
return result;
} else {
return null;
}
}
protected handleValue(): any {
const c = this.next();
this.back();
switch (c) {
case '"':
return this.handleCString();
case '{':
return this.handleObject();
case '[':
return this.handleArray();
default:
// A weird array element with a name, ignore the name and return the value
this.handleString();
if (this.next() === '=') {
return this.handleValue();
}
}
return null;
}
protected handleAsyncData() {
const result: any = {};
let c = this.next();
let name = 'missing';
while (c === ',') {
if (this.peek() !== '{') {
name = this.handleString();
if (this.next() === '=') {
result[name] = this.handleValue();
}
} else {
// In some cases, such as -break-insert with multiple results
// GDB does not return an array, so we have to identify that
// case and convert result to an array
// An example is (many fields removed to make example readable):
// 3-break-insert --function staticfunc1
// 3^done,bkpt={number="1",addr="<MULTIPLE>"},{number="1.1",func="staticfunc1",file="functions.c"},{number="1.2",func="staticfunc1",file="functions_other.c"}
if (!Array.isArray(result[name])) {
result[name] = [result[name]];
}
result[name].push(this.handleValue());
}
c = this.next();
}
return result;
}
protected handleConsoleStream() {
const msg = this.handleCString();
if (msg) {
this.gdb.emit('consoleStreamOutput', msg, 'stdout');
}
}
protected handleLogStream() {
const msg = this.handleCString();
if (msg) {
this.gdb.emit('consoleStreamOutput', msg, 'log');
}
}
protected handleLine() {
let c = this.next();
if (!c) {
return;
}
let token = '';
if (c >= '0' && c <= '9') {
token = this.handleToken(c);
c = this.next();
}
switch (c) {
case '^': {
const rest = this.restOfLine();
for (let i = 0; i < rest.length; i += 1000) {
const msg = i === 0 ? 'result' : '-cont-';
logger.verbose(
`GDB ${msg}: ${token} ${rest.substr(i, 1000)}`
);
}
const command = this.commandQueue[token];
if (command) {
const resultClass = this.handleString();
const resultData = this.handleAsyncData();
command(resultClass, resultData);
delete this.commandQueue[token];
} else {
logger.error('GDB response with no command: ' + token);
}
break;
}
case '~':
case '@':
this.handleConsoleStream();
break;
case '&':
this.handleLogStream();
break;
case '=': {
logger.verbose('GDB notify async: ' + this.restOfLine());
const notifyClass = this.handleString();
this.gdb.emit(
'notifyAsync',
notifyClass,
this.handleAsyncData()
);
break;
}
case '*': {
logger.verbose('GDB exec async: ' + this.restOfLine());
const execClass = this.handleString();
this.gdb.emit('execAsync', execClass, this.handleAsyncData());
break;
}
case '+': {
logger.verbose('GDB status async: ' + this.restOfLine());
const statusClass = this.handleString();
this.gdb.emit(
'statusAsync',
statusClass,
this.handleAsyncData()
);
break;
}
case '(':
// this is the (gdb) prompt and used
// to know that GDB has started and is ready
// for commands
if (this.waitReady) {
this.waitReady();
this.waitReady = undefined;
}
break;
default:
// treat as console output. happens on Windows.
this.back();
this.gdb.emit(
'consoleStreamOutput',
this.restOfLine() + '\n',
'stdout'
);
}
}
}