StringExtractorGDBRemote.cpp
17.4 KB
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//===-- StringExtractorGDBRemote.cpp --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/StringExtractorGDBRemote.h"
#include <ctype.h>
#include <string.h>
StringExtractorGDBRemote::ResponseType
StringExtractorGDBRemote::GetResponseType() const {
if (m_packet.empty())
return eUnsupported;
switch (m_packet[0]) {
case 'E':
if (isxdigit(m_packet[1]) && isxdigit(m_packet[2])) {
if (m_packet.size() == 3)
return eError;
llvm::StringRef packet_ref(m_packet);
if (packet_ref[3] == ';') {
auto err_string = packet_ref.substr(4);
for (auto e : err_string)
if (!isxdigit(e))
return eResponse;
return eError;
}
}
break;
case 'O':
if (m_packet.size() == 2 && m_packet[1] == 'K')
return eOK;
break;
case '+':
if (m_packet.size() == 1)
return eAck;
break;
case '-':
if (m_packet.size() == 1)
return eNack;
break;
}
return eResponse;
}
StringExtractorGDBRemote::ServerPacketType
StringExtractorGDBRemote::GetServerPacketType() const {
#define PACKET_MATCHES(s) \
((packet_size == (sizeof(s) - 1)) && (strcmp((packet_cstr), (s)) == 0))
#define PACKET_STARTS_WITH(s) \
((packet_size >= (sizeof(s) - 1)) && \
::strncmp(packet_cstr, s, (sizeof(s) - 1)) == 0)
// Empty is not a supported packet...
if (m_packet.empty())
return eServerPacketType_invalid;
const size_t packet_size = m_packet.size();
const char *packet_cstr = m_packet.c_str();
switch (m_packet[0]) {
case '%':
return eServerPacketType_notify;
case '\x03':
if (packet_size == 1)
return eServerPacketType_interrupt;
break;
case '-':
if (packet_size == 1)
return eServerPacketType_nack;
break;
case '+':
if (packet_size == 1)
return eServerPacketType_ack;
break;
case 'A':
return eServerPacketType_A;
case 'Q':
switch (packet_cstr[1]) {
case 'E':
if (PACKET_STARTS_WITH("QEnvironment:"))
return eServerPacketType_QEnvironment;
if (PACKET_STARTS_WITH("QEnvironmentHexEncoded:"))
return eServerPacketType_QEnvironmentHexEncoded;
if (PACKET_STARTS_WITH("QEnableErrorStrings"))
return eServerPacketType_QEnableErrorStrings;
break;
case 'P':
if (PACKET_STARTS_WITH("QPassSignals:"))
return eServerPacketType_QPassSignals;
break;
case 'S':
if (PACKET_MATCHES("QStartNoAckMode"))
return eServerPacketType_QStartNoAckMode;
if (PACKET_STARTS_WITH("QSaveRegisterState"))
return eServerPacketType_QSaveRegisterState;
if (PACKET_STARTS_WITH("QSetDisableASLR:"))
return eServerPacketType_QSetDisableASLR;
if (PACKET_STARTS_WITH("QSetDetachOnError:"))
return eServerPacketType_QSetDetachOnError;
if (PACKET_STARTS_WITH("QSetSTDIN:"))
return eServerPacketType_QSetSTDIN;
if (PACKET_STARTS_WITH("QSetSTDOUT:"))
return eServerPacketType_QSetSTDOUT;
if (PACKET_STARTS_WITH("QSetSTDERR:"))
return eServerPacketType_QSetSTDERR;
if (PACKET_STARTS_WITH("QSetWorkingDir:"))
return eServerPacketType_QSetWorkingDir;
if (PACKET_STARTS_WITH("QSetLogging:"))
return eServerPacketType_QSetLogging;
if (PACKET_STARTS_WITH("QSetMaxPacketSize:"))
return eServerPacketType_QSetMaxPacketSize;
if (PACKET_STARTS_WITH("QSetMaxPayloadSize:"))
return eServerPacketType_QSetMaxPayloadSize;
if (PACKET_STARTS_WITH("QSetEnableAsyncProfiling;"))
return eServerPacketType_QSetEnableAsyncProfiling;
if (PACKET_STARTS_WITH("QSyncThreadState:"))
return eServerPacketType_QSyncThreadState;
break;
case 'L':
if (PACKET_STARTS_WITH("QLaunchArch:"))
return eServerPacketType_QLaunchArch;
if (PACKET_MATCHES("QListThreadsInStopReply"))
return eServerPacketType_QListThreadsInStopReply;
break;
case 'R':
if (PACKET_STARTS_WITH("QRestoreRegisterState:"))
return eServerPacketType_QRestoreRegisterState;
break;
case 'T':
if (PACKET_MATCHES("QThreadSuffixSupported"))
return eServerPacketType_QThreadSuffixSupported;
break;
}
break;
case 'q':
switch (packet_cstr[1]) {
case 's':
if (PACKET_MATCHES("qsProcessInfo"))
return eServerPacketType_qsProcessInfo;
if (PACKET_MATCHES("qsThreadInfo"))
return eServerPacketType_qsThreadInfo;
break;
case 'f':
if (PACKET_STARTS_WITH("qfProcessInfo"))
return eServerPacketType_qfProcessInfo;
if (PACKET_STARTS_WITH("qfThreadInfo"))
return eServerPacketType_qfThreadInfo;
break;
case 'C':
if (packet_size == 2)
return eServerPacketType_qC;
break;
case 'E':
if (PACKET_STARTS_WITH("qEcho:"))
return eServerPacketType_qEcho;
break;
case 'F':
if (PACKET_STARTS_WITH("qFileLoadAddress:"))
return eServerPacketType_qFileLoadAddress;
break;
case 'G':
if (PACKET_STARTS_WITH("qGroupName:"))
return eServerPacketType_qGroupName;
if (PACKET_MATCHES("qGetWorkingDir"))
return eServerPacketType_qGetWorkingDir;
if (PACKET_MATCHES("qGetPid"))
return eServerPacketType_qGetPid;
if (PACKET_STARTS_WITH("qGetProfileData;"))
return eServerPacketType_qGetProfileData;
if (PACKET_MATCHES("qGDBServerVersion"))
return eServerPacketType_qGDBServerVersion;
break;
case 'H':
if (PACKET_MATCHES("qHostInfo"))
return eServerPacketType_qHostInfo;
break;
case 'K':
if (PACKET_STARTS_WITH("qKillSpawnedProcess"))
return eServerPacketType_qKillSpawnedProcess;
break;
case 'L':
if (PACKET_STARTS_WITH("qLaunchGDBServer"))
return eServerPacketType_qLaunchGDBServer;
if (PACKET_MATCHES("qLaunchSuccess"))
return eServerPacketType_qLaunchSuccess;
break;
case 'M':
if (PACKET_STARTS_WITH("qMemoryRegionInfo:"))
return eServerPacketType_qMemoryRegionInfo;
if (PACKET_MATCHES("qMemoryRegionInfo"))
return eServerPacketType_qMemoryRegionInfoSupported;
if (PACKET_STARTS_WITH("qModuleInfo:"))
return eServerPacketType_qModuleInfo;
break;
case 'P':
if (PACKET_STARTS_WITH("qProcessInfoPID:"))
return eServerPacketType_qProcessInfoPID;
if (PACKET_STARTS_WITH("qPlatform_shell:"))
return eServerPacketType_qPlatform_shell;
if (PACKET_STARTS_WITH("qPlatform_mkdir:"))
return eServerPacketType_qPlatform_mkdir;
if (PACKET_STARTS_WITH("qPlatform_chmod:"))
return eServerPacketType_qPlatform_chmod;
if (PACKET_MATCHES("qProcessInfo"))
return eServerPacketType_qProcessInfo;
if (PACKET_STARTS_WITH("qPathComplete:"))
return eServerPacketType_qPathComplete;
break;
case 'Q':
if (PACKET_MATCHES("qQueryGDBServer"))
return eServerPacketType_qQueryGDBServer;
break;
case 'R':
if (PACKET_STARTS_WITH("qRcmd,"))
return eServerPacketType_qRcmd;
if (PACKET_STARTS_WITH("qRegisterInfo"))
return eServerPacketType_qRegisterInfo;
break;
case 'S':
if (PACKET_STARTS_WITH("qSpeedTest:"))
return eServerPacketType_qSpeedTest;
if (PACKET_MATCHES("qShlibInfoAddr"))
return eServerPacketType_qShlibInfoAddr;
if (PACKET_MATCHES("qStepPacketSupported"))
return eServerPacketType_qStepPacketSupported;
if (PACKET_STARTS_WITH("qSupported"))
return eServerPacketType_qSupported;
if (PACKET_MATCHES("qSyncThreadStateSupported"))
return eServerPacketType_qSyncThreadStateSupported;
break;
case 'T':
if (PACKET_STARTS_WITH("qThreadExtraInfo,"))
return eServerPacketType_qThreadExtraInfo;
if (PACKET_STARTS_WITH("qThreadStopInfo"))
return eServerPacketType_qThreadStopInfo;
break;
case 'U':
if (PACKET_STARTS_WITH("qUserName:"))
return eServerPacketType_qUserName;
break;
case 'V':
if (PACKET_MATCHES("qVAttachOrWaitSupported"))
return eServerPacketType_qVAttachOrWaitSupported;
break;
case 'W':
if (PACKET_STARTS_WITH("qWatchpointSupportInfo:"))
return eServerPacketType_qWatchpointSupportInfo;
if (PACKET_MATCHES("qWatchpointSupportInfo"))
return eServerPacketType_qWatchpointSupportInfoSupported;
break;
case 'X':
if (PACKET_STARTS_WITH("qXfer:"))
return eServerPacketType_qXfer;
break;
}
break;
case 'j':
if (PACKET_STARTS_WITH("jModulesInfo:"))
return eServerPacketType_jModulesInfo;
if (PACKET_MATCHES("jSignalsInfo"))
return eServerPacketType_jSignalsInfo;
if (PACKET_MATCHES("jThreadsInfo"))
return eServerPacketType_jThreadsInfo;
if (PACKET_STARTS_WITH("jTraceBufferRead:"))
return eServerPacketType_jTraceBufferRead;
if (PACKET_STARTS_WITH("jTraceConfigRead:"))
return eServerPacketType_jTraceConfigRead;
if (PACKET_STARTS_WITH("jTraceMetaRead:"))
return eServerPacketType_jTraceMetaRead;
if (PACKET_STARTS_WITH("jTraceStart:"))
return eServerPacketType_jTraceStart;
if (PACKET_STARTS_WITH("jTraceStop:"))
return eServerPacketType_jTraceStop;
break;
case 'v':
if (PACKET_STARTS_WITH("vFile:")) {
if (PACKET_STARTS_WITH("vFile:open:"))
return eServerPacketType_vFile_open;
else if (PACKET_STARTS_WITH("vFile:close:"))
return eServerPacketType_vFile_close;
else if (PACKET_STARTS_WITH("vFile:pread"))
return eServerPacketType_vFile_pread;
else if (PACKET_STARTS_WITH("vFile:pwrite"))
return eServerPacketType_vFile_pwrite;
else if (PACKET_STARTS_WITH("vFile:size"))
return eServerPacketType_vFile_size;
else if (PACKET_STARTS_WITH("vFile:exists"))
return eServerPacketType_vFile_exists;
else if (PACKET_STARTS_WITH("vFile:stat"))
return eServerPacketType_vFile_stat;
else if (PACKET_STARTS_WITH("vFile:mode"))
return eServerPacketType_vFile_mode;
else if (PACKET_STARTS_WITH("vFile:MD5"))
return eServerPacketType_vFile_md5;
else if (PACKET_STARTS_WITH("vFile:symlink"))
return eServerPacketType_vFile_symlink;
else if (PACKET_STARTS_WITH("vFile:unlink"))
return eServerPacketType_vFile_unlink;
} else {
if (PACKET_STARTS_WITH("vAttach;"))
return eServerPacketType_vAttach;
if (PACKET_STARTS_WITH("vAttachWait;"))
return eServerPacketType_vAttachWait;
if (PACKET_STARTS_WITH("vAttachOrWait;"))
return eServerPacketType_vAttachOrWait;
if (PACKET_STARTS_WITH("vAttachName;"))
return eServerPacketType_vAttachName;
if (PACKET_STARTS_WITH("vCont;"))
return eServerPacketType_vCont;
if (PACKET_MATCHES("vCont?"))
return eServerPacketType_vCont_actions;
}
break;
case '_':
switch (packet_cstr[1]) {
case 'M':
return eServerPacketType__M;
case 'm':
return eServerPacketType__m;
}
break;
case '?':
if (packet_size == 1)
return eServerPacketType_stop_reason;
break;
case 'c':
return eServerPacketType_c;
case 'C':
return eServerPacketType_C;
case 'D':
if (packet_size == 1)
return eServerPacketType_D;
break;
case 'g':
return eServerPacketType_g;
case 'G':
return eServerPacketType_G;
case 'H':
return eServerPacketType_H;
case 'I':
return eServerPacketType_I;
case 'k':
if (packet_size == 1)
return eServerPacketType_k;
break;
case 'm':
return eServerPacketType_m;
case 'M':
return eServerPacketType_M;
case 'p':
return eServerPacketType_p;
case 'P':
return eServerPacketType_P;
case 's':
if (packet_size == 1)
return eServerPacketType_s;
break;
case 'S':
return eServerPacketType_S;
case 'x':
return eServerPacketType_x;
case 'X':
return eServerPacketType_X;
case 'T':
return eServerPacketType_T;
case 'z':
if (packet_cstr[1] >= '0' && packet_cstr[1] <= '4')
return eServerPacketType_z;
break;
case 'Z':
if (packet_cstr[1] >= '0' && packet_cstr[1] <= '4')
return eServerPacketType_Z;
break;
}
return eServerPacketType_unimplemented;
}
bool StringExtractorGDBRemote::IsOKResponse() const {
return GetResponseType() == eOK;
}
bool StringExtractorGDBRemote::IsUnsupportedResponse() const {
return GetResponseType() == eUnsupported;
}
bool StringExtractorGDBRemote::IsNormalResponse() const {
return GetResponseType() == eResponse;
}
bool StringExtractorGDBRemote::IsErrorResponse() const {
return GetResponseType() == eError && isxdigit(m_packet[1]) &&
isxdigit(m_packet[2]);
}
uint8_t StringExtractorGDBRemote::GetError() {
if (GetResponseType() == eError) {
SetFilePos(1);
return GetHexU8(255);
}
return 0;
}
lldb_private::Status StringExtractorGDBRemote::GetStatus() {
lldb_private::Status error;
if (GetResponseType() == eError) {
SetFilePos(1);
uint8_t errc = GetHexU8(255);
error.SetError(errc, lldb::eErrorTypeGeneric);
error.SetErrorStringWithFormat("Error %u", errc);
std::string error_messg;
if (GetChar() == ';') {
GetHexByteString(error_messg);
error.SetErrorString(error_messg);
}
}
return error;
}
size_t StringExtractorGDBRemote::GetEscapedBinaryData(std::string &str) {
// Just get the data bytes in the string as
// GDBRemoteCommunication::CheckForPacket() already removes any 0x7d escaped
// characters. If any 0x7d characters are left in the packet, then they are
// supposed to be there...
str.clear();
const size_t bytes_left = GetBytesLeft();
if (bytes_left > 0) {
str.assign(m_packet, m_index, bytes_left);
m_index += bytes_left;
}
return str.size();
}
static bool
OKErrorNotSupportedResponseValidator(void *,
const StringExtractorGDBRemote &response) {
switch (response.GetResponseType()) {
case StringExtractorGDBRemote::eOK:
case StringExtractorGDBRemote::eError:
case StringExtractorGDBRemote::eUnsupported:
return true;
case StringExtractorGDBRemote::eAck:
case StringExtractorGDBRemote::eNack:
case StringExtractorGDBRemote::eResponse:
break;
}
return false;
}
static bool JSONResponseValidator(void *,
const StringExtractorGDBRemote &response) {
switch (response.GetResponseType()) {
case StringExtractorGDBRemote::eUnsupported:
case StringExtractorGDBRemote::eError:
return true; // Accept unsupported or EXX as valid responses
case StringExtractorGDBRemote::eOK:
case StringExtractorGDBRemote::eAck:
case StringExtractorGDBRemote::eNack:
break;
case StringExtractorGDBRemote::eResponse:
// JSON that is returned in from JSON query packets is currently always
// either a dictionary which starts with a '{', or an array which starts
// with a '['. This is a quick validator to just make sure the response
// could be valid JSON without having to validate all of the
// JSON content.
switch (response.GetStringRef()[0]) {
case '{':
return true;
case '[':
return true;
default:
break;
}
break;
}
return false;
}
static bool
ASCIIHexBytesResponseValidator(void *,
const StringExtractorGDBRemote &response) {
switch (response.GetResponseType()) {
case StringExtractorGDBRemote::eUnsupported:
case StringExtractorGDBRemote::eError:
return true; // Accept unsupported or EXX as valid responses
case StringExtractorGDBRemote::eOK:
case StringExtractorGDBRemote::eAck:
case StringExtractorGDBRemote::eNack:
break;
case StringExtractorGDBRemote::eResponse: {
uint32_t valid_count = 0;
for (const char ch : response.GetStringRef()) {
if (!isxdigit(ch)) {
return false;
}
if (++valid_count >= 16)
break; // Don't validate all the characters in case the packet is very
// large
}
return true;
} break;
}
return false;
}
void StringExtractorGDBRemote::CopyResponseValidator(
const StringExtractorGDBRemote &rhs) {
m_validator = rhs.m_validator;
m_validator_baton = rhs.m_validator_baton;
}
void StringExtractorGDBRemote::SetResponseValidator(
ResponseValidatorCallback callback, void *baton) {
m_validator = callback;
m_validator_baton = baton;
}
void StringExtractorGDBRemote::SetResponseValidatorToOKErrorNotSupported() {
m_validator = OKErrorNotSupportedResponseValidator;
m_validator_baton = nullptr;
}
void StringExtractorGDBRemote::SetResponseValidatorToASCIIHexBytes() {
m_validator = ASCIIHexBytesResponseValidator;
m_validator_baton = nullptr;
}
void StringExtractorGDBRemote::SetResponseValidatorToJSON() {
m_validator = JSONResponseValidator;
m_validator_baton = nullptr;
}
bool StringExtractorGDBRemote::ValidateResponse() const {
// If we have a validator callback, try to validate the callback
if (m_validator)
return m_validator(m_validator_baton, *this);
else
return true; // No validator, so response is valid
}