1 //===-- StringExtractorGDBRemote.cpp --------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/StringExtractorGDBRemote.h"
15 constexpr lldb::pid_t
StringExtractorGDBRemote::AllProcesses
;
16 constexpr lldb::tid_t
StringExtractorGDBRemote::AllThreads
;
18 StringExtractorGDBRemote::ResponseType
19 StringExtractorGDBRemote::GetResponseType() const {
23 switch (m_packet
[0]) {
25 if (isxdigit(m_packet
[1]) && isxdigit(m_packet
[2])) {
26 if (m_packet
.size() == 3)
28 llvm::StringRef
packet_ref(m_packet
);
29 if (packet_ref
[3] == ';') {
30 auto err_string
= packet_ref
.substr(4);
31 for (auto e
: err_string
)
40 if (m_packet
.size() == 2 && m_packet
[1] == 'K')
45 if (m_packet
.size() == 1)
50 if (m_packet
.size() == 1)
57 StringExtractorGDBRemote::ServerPacketType
58 StringExtractorGDBRemote::GetServerPacketType() const {
59 #define PACKET_MATCHES(s) \
60 ((packet_size == (sizeof(s) - 1)) && (strcmp((packet_cstr), (s)) == 0))
61 #define PACKET_STARTS_WITH(s) \
62 ((packet_size >= (sizeof(s) - 1)) && \
63 ::strncmp(packet_cstr, s, (sizeof(s) - 1)) == 0)
65 // Empty is not a supported packet...
67 return eServerPacketType_invalid
;
69 const size_t packet_size
= m_packet
.size();
70 const char *packet_cstr
= m_packet
.c_str();
71 switch (m_packet
[0]) {
74 return eServerPacketType_notify
;
78 return eServerPacketType_interrupt
;
83 return eServerPacketType_nack
;
88 return eServerPacketType_ack
;
92 return eServerPacketType_A
;
96 switch (packet_cstr
[1]) {
98 if (PACKET_STARTS_WITH("QEnvironment:"))
99 return eServerPacketType_QEnvironment
;
100 if (PACKET_STARTS_WITH("QEnvironmentHexEncoded:"))
101 return eServerPacketType_QEnvironmentHexEncoded
;
102 if (PACKET_STARTS_WITH("QEnableErrorStrings"))
103 return eServerPacketType_QEnableErrorStrings
;
107 if (PACKET_STARTS_WITH("QPassSignals:"))
108 return eServerPacketType_QPassSignals
;
112 if (PACKET_MATCHES("QStartNoAckMode"))
113 return eServerPacketType_QStartNoAckMode
;
114 if (PACKET_STARTS_WITH("QSaveRegisterState"))
115 return eServerPacketType_QSaveRegisterState
;
116 if (PACKET_STARTS_WITH("QSetDisableASLR:"))
117 return eServerPacketType_QSetDisableASLR
;
118 if (PACKET_STARTS_WITH("QSetDetachOnError:"))
119 return eServerPacketType_QSetDetachOnError
;
120 if (PACKET_STARTS_WITH("QSetSTDIN:"))
121 return eServerPacketType_QSetSTDIN
;
122 if (PACKET_STARTS_WITH("QSetSTDOUT:"))
123 return eServerPacketType_QSetSTDOUT
;
124 if (PACKET_STARTS_WITH("QSetSTDERR:"))
125 return eServerPacketType_QSetSTDERR
;
126 if (PACKET_STARTS_WITH("QSetWorkingDir:"))
127 return eServerPacketType_QSetWorkingDir
;
128 if (PACKET_STARTS_WITH("QSetLogging:"))
129 return eServerPacketType_QSetLogging
;
130 if (PACKET_STARTS_WITH("QSetIgnoredExceptions"))
131 return eServerPacketType_QSetIgnoredExceptions
;
132 if (PACKET_STARTS_WITH("QSetMaxPacketSize:"))
133 return eServerPacketType_QSetMaxPacketSize
;
134 if (PACKET_STARTS_WITH("QSetMaxPayloadSize:"))
135 return eServerPacketType_QSetMaxPayloadSize
;
136 if (PACKET_STARTS_WITH("QSetEnableAsyncProfiling;"))
137 return eServerPacketType_QSetEnableAsyncProfiling
;
138 if (PACKET_STARTS_WITH("QSyncThreadState:"))
139 return eServerPacketType_QSyncThreadState
;
143 if (PACKET_STARTS_WITH("QLaunchArch:"))
144 return eServerPacketType_QLaunchArch
;
145 if (PACKET_MATCHES("QListThreadsInStopReply"))
146 return eServerPacketType_QListThreadsInStopReply
;
150 if (PACKET_STARTS_WITH("QMemTags"))
151 return eServerPacketType_QMemTags
;
155 if (PACKET_STARTS_WITH("QNonStop:"))
156 return eServerPacketType_QNonStop
;
160 if (PACKET_STARTS_WITH("QRestoreRegisterState:"))
161 return eServerPacketType_QRestoreRegisterState
;
165 if (PACKET_MATCHES("QThreadSuffixSupported"))
166 return eServerPacketType_QThreadSuffixSupported
;
172 switch (packet_cstr
[1]) {
174 if (PACKET_MATCHES("qsProcessInfo"))
175 return eServerPacketType_qsProcessInfo
;
176 if (PACKET_MATCHES("qsThreadInfo"))
177 return eServerPacketType_qsThreadInfo
;
181 if (PACKET_STARTS_WITH("qfProcessInfo"))
182 return eServerPacketType_qfProcessInfo
;
183 if (PACKET_STARTS_WITH("qfThreadInfo"))
184 return eServerPacketType_qfThreadInfo
;
188 if (packet_size
== 2)
189 return eServerPacketType_qC
;
193 if (PACKET_STARTS_WITH("qEcho:"))
194 return eServerPacketType_qEcho
;
198 if (PACKET_STARTS_WITH("qFileLoadAddress:"))
199 return eServerPacketType_qFileLoadAddress
;
203 if (PACKET_STARTS_WITH("qGroupName:"))
204 return eServerPacketType_qGroupName
;
205 if (PACKET_MATCHES("qGetWorkingDir"))
206 return eServerPacketType_qGetWorkingDir
;
207 if (PACKET_MATCHES("qGetPid"))
208 return eServerPacketType_qGetPid
;
209 if (PACKET_STARTS_WITH("qGetProfileData;"))
210 return eServerPacketType_qGetProfileData
;
211 if (PACKET_MATCHES("qGDBServerVersion"))
212 return eServerPacketType_qGDBServerVersion
;
216 if (PACKET_MATCHES("qHostInfo"))
217 return eServerPacketType_qHostInfo
;
221 if (PACKET_STARTS_WITH("qKillSpawnedProcess"))
222 return eServerPacketType_qKillSpawnedProcess
;
226 if (PACKET_STARTS_WITH("qLaunchGDBServer"))
227 return eServerPacketType_qLaunchGDBServer
;
228 if (PACKET_MATCHES("qLaunchSuccess"))
229 return eServerPacketType_qLaunchSuccess
;
233 if (PACKET_STARTS_WITH("qMemoryRegionInfo:"))
234 return eServerPacketType_qMemoryRegionInfo
;
235 if (PACKET_MATCHES("qMemoryRegionInfo"))
236 return eServerPacketType_qMemoryRegionInfoSupported
;
237 if (PACKET_STARTS_WITH("qModuleInfo:"))
238 return eServerPacketType_qModuleInfo
;
239 if (PACKET_STARTS_WITH("qMemTags:"))
240 return eServerPacketType_qMemTags
;
244 if (PACKET_STARTS_WITH("qProcessInfoPID:"))
245 return eServerPacketType_qProcessInfoPID
;
246 if (PACKET_STARTS_WITH("qPlatform_shell:"))
247 return eServerPacketType_qPlatform_shell
;
248 if (PACKET_STARTS_WITH("qPlatform_mkdir:"))
249 return eServerPacketType_qPlatform_mkdir
;
250 if (PACKET_STARTS_WITH("qPlatform_chmod:"))
251 return eServerPacketType_qPlatform_chmod
;
252 if (PACKET_MATCHES("qProcessInfo"))
253 return eServerPacketType_qProcessInfo
;
254 if (PACKET_STARTS_WITH("qPathComplete:"))
255 return eServerPacketType_qPathComplete
;
259 if (PACKET_MATCHES("qQueryGDBServer"))
260 return eServerPacketType_qQueryGDBServer
;
264 if (PACKET_STARTS_WITH("qRcmd,"))
265 return eServerPacketType_qRcmd
;
266 if (PACKET_STARTS_WITH("qRegisterInfo"))
267 return eServerPacketType_qRegisterInfo
;
271 if (PACKET_STARTS_WITH("qSaveCore"))
272 return eServerPacketType_qLLDBSaveCore
;
273 if (PACKET_STARTS_WITH("qSpeedTest:"))
274 return eServerPacketType_qSpeedTest
;
275 if (PACKET_MATCHES("qShlibInfoAddr"))
276 return eServerPacketType_qShlibInfoAddr
;
277 if (PACKET_MATCHES("qStepPacketSupported"))
278 return eServerPacketType_qStepPacketSupported
;
279 if (PACKET_STARTS_WITH("qSupported"))
280 return eServerPacketType_qSupported
;
281 if (PACKET_MATCHES("qSyncThreadStateSupported"))
282 return eServerPacketType_qSyncThreadStateSupported
;
286 if (PACKET_STARTS_WITH("qThreadExtraInfo,"))
287 return eServerPacketType_qThreadExtraInfo
;
288 if (PACKET_STARTS_WITH("qThreadStopInfo"))
289 return eServerPacketType_qThreadStopInfo
;
293 if (PACKET_STARTS_WITH("qUserName:"))
294 return eServerPacketType_qUserName
;
298 if (PACKET_MATCHES("qVAttachOrWaitSupported"))
299 return eServerPacketType_qVAttachOrWaitSupported
;
303 if (PACKET_STARTS_WITH("qWatchpointSupportInfo:"))
304 return eServerPacketType_qWatchpointSupportInfo
;
305 if (PACKET_MATCHES("qWatchpointSupportInfo"))
306 return eServerPacketType_qWatchpointSupportInfoSupported
;
310 if (PACKET_STARTS_WITH("qXfer:"))
311 return eServerPacketType_qXfer
;
317 if (PACKET_STARTS_WITH("jModulesInfo:"))
318 return eServerPacketType_jModulesInfo
;
319 if (PACKET_MATCHES("jSignalsInfo"))
320 return eServerPacketType_jSignalsInfo
;
321 if (PACKET_MATCHES("jThreadsInfo"))
322 return eServerPacketType_jThreadsInfo
;
324 if (PACKET_MATCHES("jLLDBTraceSupported"))
325 return eServerPacketType_jLLDBTraceSupported
;
326 if (PACKET_STARTS_WITH("jLLDBTraceStop:"))
327 return eServerPacketType_jLLDBTraceStop
;
328 if (PACKET_STARTS_WITH("jLLDBTraceStart:"))
329 return eServerPacketType_jLLDBTraceStart
;
330 if (PACKET_STARTS_WITH("jLLDBTraceGetState:"))
331 return eServerPacketType_jLLDBTraceGetState
;
332 if (PACKET_STARTS_WITH("jLLDBTraceGetBinaryData:"))
333 return eServerPacketType_jLLDBTraceGetBinaryData
;
337 if (PACKET_STARTS_WITH("vFile:")) {
338 if (PACKET_STARTS_WITH("vFile:open:"))
339 return eServerPacketType_vFile_open
;
340 else if (PACKET_STARTS_WITH("vFile:close:"))
341 return eServerPacketType_vFile_close
;
342 else if (PACKET_STARTS_WITH("vFile:pread"))
343 return eServerPacketType_vFile_pread
;
344 else if (PACKET_STARTS_WITH("vFile:pwrite"))
345 return eServerPacketType_vFile_pwrite
;
346 else if (PACKET_STARTS_WITH("vFile:size"))
347 return eServerPacketType_vFile_size
;
348 else if (PACKET_STARTS_WITH("vFile:exists"))
349 return eServerPacketType_vFile_exists
;
350 else if (PACKET_STARTS_WITH("vFile:fstat"))
351 return eServerPacketType_vFile_fstat
;
352 else if (PACKET_STARTS_WITH("vFile:stat"))
353 return eServerPacketType_vFile_stat
;
354 else if (PACKET_STARTS_WITH("vFile:mode"))
355 return eServerPacketType_vFile_mode
;
356 else if (PACKET_STARTS_WITH("vFile:MD5"))
357 return eServerPacketType_vFile_md5
;
358 else if (PACKET_STARTS_WITH("vFile:symlink"))
359 return eServerPacketType_vFile_symlink
;
360 else if (PACKET_STARTS_WITH("vFile:unlink"))
361 return eServerPacketType_vFile_unlink
;
364 if (PACKET_STARTS_WITH("vAttach;"))
365 return eServerPacketType_vAttach
;
366 if (PACKET_STARTS_WITH("vAttachWait;"))
367 return eServerPacketType_vAttachWait
;
368 if (PACKET_STARTS_WITH("vAttachOrWait;"))
369 return eServerPacketType_vAttachOrWait
;
370 if (PACKET_STARTS_WITH("vAttachName;"))
371 return eServerPacketType_vAttachName
;
372 if (PACKET_STARTS_WITH("vCont;"))
373 return eServerPacketType_vCont
;
374 if (PACKET_MATCHES("vCont?"))
375 return eServerPacketType_vCont_actions
;
376 if (PACKET_STARTS_WITH("vKill;"))
377 return eServerPacketType_vKill
;
378 if (PACKET_STARTS_WITH("vRun;"))
379 return eServerPacketType_vRun
;
380 if (PACKET_MATCHES("vStopped"))
381 return eServerPacketType_vStopped
;
382 if (PACKET_MATCHES("vCtrlC"))
383 return eServerPacketType_vCtrlC
;
384 if (PACKET_MATCHES("vStdio"))
385 return eServerPacketType_vStdio
;
391 switch (packet_cstr
[1]) {
393 return eServerPacketType__M
;
396 return eServerPacketType__m
;
401 if (packet_size
== 1)
402 return eServerPacketType_stop_reason
;
406 return eServerPacketType_c
;
409 return eServerPacketType_C
;
412 return eServerPacketType_D
;
415 return eServerPacketType_g
;
418 return eServerPacketType_G
;
421 return eServerPacketType_H
;
424 return eServerPacketType_I
;
427 if (packet_size
== 1)
428 return eServerPacketType_k
;
432 return eServerPacketType_m
;
435 return eServerPacketType_M
;
438 return eServerPacketType_p
;
441 return eServerPacketType_P
;
444 if (packet_size
== 1)
445 return eServerPacketType_s
;
449 return eServerPacketType_S
;
452 return eServerPacketType_x
;
455 return eServerPacketType_X
;
458 return eServerPacketType_T
;
461 if (packet_cstr
[1] >= '0' && packet_cstr
[1] <= '4')
462 return eServerPacketType_z
;
466 if (packet_cstr
[1] >= '0' && packet_cstr
[1] <= '4')
467 return eServerPacketType_Z
;
470 return eServerPacketType_unimplemented
;
473 bool StringExtractorGDBRemote::IsOKResponse() const {
474 return GetResponseType() == eOK
;
477 bool StringExtractorGDBRemote::IsUnsupportedResponse() const {
478 return GetResponseType() == eUnsupported
;
481 bool StringExtractorGDBRemote::IsNormalResponse() const {
482 return GetResponseType() == eResponse
;
485 bool StringExtractorGDBRemote::IsErrorResponse() const {
486 return GetResponseType() == eError
&& isxdigit(m_packet
[1]) &&
487 isxdigit(m_packet
[2]);
490 uint8_t StringExtractorGDBRemote::GetError() {
491 if (GetResponseType() == eError
) {
493 return GetHexU8(255);
498 lldb_private::Status
StringExtractorGDBRemote::GetStatus() {
499 lldb_private::Status error
;
500 if (GetResponseType() == eError
) {
502 uint8_t errc
= GetHexU8(255);
503 error
= lldb_private::Status::FromErrorStringWithFormat("Error %u", errc
);
504 std::string error_messg
;
505 if (GetChar() == ';') {
506 GetHexByteString(error_messg
);
507 error
= lldb_private::Status(error_messg
);
513 size_t StringExtractorGDBRemote::GetEscapedBinaryData(std::string
&str
) {
514 // Just get the data bytes in the string as
515 // GDBRemoteCommunication::CheckForPacket() already removes any 0x7d escaped
516 // characters. If any 0x7d characters are left in the packet, then they are
517 // supposed to be there...
519 const size_t bytes_left
= GetBytesLeft();
520 if (bytes_left
> 0) {
521 str
.assign(m_packet
, m_index
, bytes_left
);
522 m_index
+= bytes_left
;
528 OKErrorNotSupportedResponseValidator(void *,
529 const StringExtractorGDBRemote
&response
) {
530 switch (response
.GetResponseType()) {
531 case StringExtractorGDBRemote::eOK
:
532 case StringExtractorGDBRemote::eError
:
533 case StringExtractorGDBRemote::eUnsupported
:
536 case StringExtractorGDBRemote::eAck
:
537 case StringExtractorGDBRemote::eNack
:
538 case StringExtractorGDBRemote::eResponse
:
544 static bool JSONResponseValidator(void *,
545 const StringExtractorGDBRemote
&response
) {
546 switch (response
.GetResponseType()) {
547 case StringExtractorGDBRemote::eUnsupported
:
548 case StringExtractorGDBRemote::eError
:
549 return true; // Accept unsupported or EXX as valid responses
551 case StringExtractorGDBRemote::eOK
:
552 case StringExtractorGDBRemote::eAck
:
553 case StringExtractorGDBRemote::eNack
:
556 case StringExtractorGDBRemote::eResponse
:
557 // JSON that is returned in from JSON query packets is currently always
558 // either a dictionary which starts with a '{', or an array which starts
559 // with a '['. This is a quick validator to just make sure the response
560 // could be valid JSON without having to validate all of the
562 switch (response
.GetStringRef()[0]) {
576 ASCIIHexBytesResponseValidator(void *,
577 const StringExtractorGDBRemote
&response
) {
578 switch (response
.GetResponseType()) {
579 case StringExtractorGDBRemote::eUnsupported
:
580 case StringExtractorGDBRemote::eError
:
581 return true; // Accept unsupported or EXX as valid responses
583 case StringExtractorGDBRemote::eOK
:
584 case StringExtractorGDBRemote::eAck
:
585 case StringExtractorGDBRemote::eNack
:
588 case StringExtractorGDBRemote::eResponse
: {
589 uint32_t valid_count
= 0;
590 for (const char ch
: response
.GetStringRef()) {
594 if (++valid_count
>= 16)
595 break; // Don't validate all the characters in case the packet is very
604 void StringExtractorGDBRemote::CopyResponseValidator(
605 const StringExtractorGDBRemote
&rhs
) {
606 m_validator
= rhs
.m_validator
;
607 m_validator_baton
= rhs
.m_validator_baton
;
610 void StringExtractorGDBRemote::SetResponseValidator(
611 ResponseValidatorCallback callback
, void *baton
) {
612 m_validator
= callback
;
613 m_validator_baton
= baton
;
616 void StringExtractorGDBRemote::SetResponseValidatorToOKErrorNotSupported() {
617 m_validator
= OKErrorNotSupportedResponseValidator
;
618 m_validator_baton
= nullptr;
621 void StringExtractorGDBRemote::SetResponseValidatorToASCIIHexBytes() {
622 m_validator
= ASCIIHexBytesResponseValidator
;
623 m_validator_baton
= nullptr;
626 void StringExtractorGDBRemote::SetResponseValidatorToJSON() {
627 m_validator
= JSONResponseValidator
;
628 m_validator_baton
= nullptr;
631 bool StringExtractorGDBRemote::ValidateResponse() const {
632 // If we have a validator callback, try to validate the callback
634 return m_validator(m_validator_baton
, *this);
636 return true; // No validator, so response is valid
639 std::optional
<std::pair
<lldb::pid_t
, lldb::tid_t
>>
640 StringExtractorGDBRemote::GetPidTid(lldb::pid_t default_pid
) {
641 llvm::StringRef view
= llvm::StringRef(m_packet
).substr(m_index
);
642 size_t initial_length
= view
.size();
643 lldb::pid_t pid
= LLDB_INVALID_PROCESS_ID
;
646 if (view
.consume_front("p")) {
647 // process identifier
648 if (view
.consume_front("-1")) {
649 // -1 is a special case
651 } else if (view
.consumeInteger(16, pid
) || pid
== 0) {
652 // not a valid hex integer OR unsupported pid 0
653 m_index
= UINT64_MAX
;
657 // "." must follow if we expect TID too; otherwise, we assume -1
658 if (!view
.consume_front(".")) {
660 m_index
+= initial_length
- view
.size();
662 return {{pid
, AllThreads
}};
667 if (view
.consume_front("-1")) {
668 // -1 is a special case
670 } else if (view
.consumeInteger(16, tid
) || tid
== 0 || pid
== AllProcesses
) {
671 // not a valid hex integer OR tid 0 OR pid -1 + a specific tid
672 m_index
= UINT64_MAX
;
677 m_index
+= initial_length
- view
.size();
679 return {{pid
!= LLDB_INVALID_PROCESS_ID
? pid
: default_pid
, tid
}};