1 //===-- StringExtractorGDBRemote.h ------------------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLDB_UTILITY_STRINGEXTRACTORGDBREMOTE_H
10 #define LLDB_UTILITY_STRINGEXTRACTORGDBREMOTE_H
11 
12 #include "lldb/Utility/Status.h"
13 #include "lldb/Utility/StringExtractor.h"
14 #include "llvm/ADT/StringRef.h"
15 
16 #include <optional>
17 #include <string>
18 
19 #include <cstddef>
20 #include <cstdint>
21 
22 class StringExtractorGDBRemote : public StringExtractor {
23 public:
24   typedef bool (*ResponseValidatorCallback)(
25       void *baton, const StringExtractorGDBRemote &response);
26 
27   StringExtractorGDBRemote() = default;
28 
StringExtractorGDBRemote(llvm::StringRef str)29   StringExtractorGDBRemote(llvm::StringRef str)
30       : StringExtractor(str), m_validator(nullptr) {}
31 
StringExtractorGDBRemote(const char * cstr)32   StringExtractorGDBRemote(const char *cstr)
33       : StringExtractor(cstr), m_validator(nullptr) {}
34 
35   bool ValidateResponse() const;
36 
37   void CopyResponseValidator(const StringExtractorGDBRemote &rhs);
38 
39   void SetResponseValidator(ResponseValidatorCallback callback, void *baton);
40 
41   void SetResponseValidatorToOKErrorNotSupported();
42 
43   void SetResponseValidatorToASCIIHexBytes();
44 
45   void SetResponseValidatorToJSON();
46 
47   enum ServerPacketType {
48     eServerPacketType_nack = 0,
49     eServerPacketType_ack,
50     eServerPacketType_invalid,
51     eServerPacketType_unimplemented,
52     eServerPacketType_interrupt, // CTRL+c packet or "\x03"
53     eServerPacketType_A,         // Program arguments packet
54     eServerPacketType_qfProcessInfo,
55     eServerPacketType_qsProcessInfo,
56     eServerPacketType_qC,
57     eServerPacketType_qEcho,
58     eServerPacketType_qGroupName,
59     eServerPacketType_qHostInfo,
60     eServerPacketType_qLaunchGDBServer,
61     eServerPacketType_qQueryGDBServer,
62     eServerPacketType_qKillSpawnedProcess,
63     eServerPacketType_qLaunchSuccess,
64     eServerPacketType_qModuleInfo,
65     eServerPacketType_qProcessInfoPID,
66     eServerPacketType_qSpeedTest,
67     eServerPacketType_qUserName,
68     eServerPacketType_qGetWorkingDir,
69     eServerPacketType_qFileLoadAddress,
70     eServerPacketType_QEnvironment,
71     eServerPacketType_QEnableErrorStrings,
72     eServerPacketType_QLaunchArch,
73     eServerPacketType_QSetDisableASLR,
74     eServerPacketType_QSetDetachOnError,
75     eServerPacketType_QSetSTDIN,
76     eServerPacketType_QSetSTDOUT,
77     eServerPacketType_QSetSTDERR,
78     eServerPacketType_QSetWorkingDir,
79     eServerPacketType_QStartNoAckMode,
80     eServerPacketType_qPathComplete,
81     eServerPacketType_qPlatform_shell,
82     eServerPacketType_qPlatform_mkdir,
83     eServerPacketType_qPlatform_chmod,
84     eServerPacketType_vFile_open,
85     eServerPacketType_vFile_close,
86     eServerPacketType_vFile_pread,
87     eServerPacketType_vFile_pwrite,
88     eServerPacketType_vFile_size,
89     eServerPacketType_vFile_mode,
90     eServerPacketType_vFile_exists,
91     eServerPacketType_vFile_md5,
92     eServerPacketType_vFile_fstat,
93     eServerPacketType_vFile_stat,
94     eServerPacketType_vFile_symlink,
95     eServerPacketType_vFile_unlink,
96     // debug server packages
97     eServerPacketType_QEnvironmentHexEncoded,
98     eServerPacketType_QListThreadsInStopReply,
99     eServerPacketType_QPassSignals,
100     eServerPacketType_QRestoreRegisterState,
101     eServerPacketType_QSaveRegisterState,
102     eServerPacketType_QSetLogging,
103     eServerPacketType_QSetMaxPacketSize,
104     eServerPacketType_QSetMaxPayloadSize,
105     eServerPacketType_QSetEnableAsyncProfiling,
106     eServerPacketType_QSyncThreadState,
107     eServerPacketType_QThreadSuffixSupported,
108 
109     eServerPacketType_jThreadsInfo,
110     eServerPacketType_qsThreadInfo,
111     eServerPacketType_qfThreadInfo,
112     eServerPacketType_qGetPid,
113     eServerPacketType_qGetProfileData,
114     eServerPacketType_qGDBServerVersion,
115     eServerPacketType_qMemoryRegionInfo,
116     eServerPacketType_qMemoryRegionInfoSupported,
117     eServerPacketType_qProcessInfo,
118     eServerPacketType_qRcmd,
119     eServerPacketType_qRegisterInfo,
120     eServerPacketType_qShlibInfoAddr,
121     eServerPacketType_qStepPacketSupported,
122     eServerPacketType_qSupported,
123     eServerPacketType_qSyncThreadStateSupported,
124     eServerPacketType_qThreadExtraInfo,
125     eServerPacketType_qThreadStopInfo,
126     eServerPacketType_qVAttachOrWaitSupported,
127     eServerPacketType_qWatchpointSupportInfo,
128     eServerPacketType_qWatchpointSupportInfoSupported,
129     eServerPacketType_qXfer,
130 
131     eServerPacketType_jSignalsInfo,
132     eServerPacketType_jModulesInfo,
133 
134     eServerPacketType_vAttach,
135     eServerPacketType_vAttachWait,
136     eServerPacketType_vAttachOrWait,
137     eServerPacketType_vAttachName,
138     eServerPacketType_vCont,
139     eServerPacketType_vCont_actions, // vCont?
140     eServerPacketType_vKill,
141     eServerPacketType_vRun,
142 
143     eServerPacketType_stop_reason, // '?'
144 
145     eServerPacketType_c,
146     eServerPacketType_C,
147     eServerPacketType_D,
148     eServerPacketType_g,
149     eServerPacketType_G,
150     eServerPacketType_H,
151     eServerPacketType_I, // stdin notification
152     eServerPacketType_k,
153     eServerPacketType_m,
154     eServerPacketType_M,
155     eServerPacketType_p,
156     eServerPacketType_P,
157     eServerPacketType_s,
158     eServerPacketType_S,
159     eServerPacketType_T,
160     eServerPacketType_x,
161     eServerPacketType_X,
162     eServerPacketType_Z,
163     eServerPacketType_z,
164 
165     eServerPacketType__M,
166     eServerPacketType__m,
167     eServerPacketType_notify, // '%' notification
168 
169     eServerPacketType_jLLDBTraceSupported,
170     eServerPacketType_jLLDBTraceStart,
171     eServerPacketType_jLLDBTraceStop,
172     eServerPacketType_jLLDBTraceGetState,
173     eServerPacketType_jLLDBTraceGetBinaryData,
174 
175     eServerPacketType_qMemTags, // read memory tags
176     eServerPacketType_QMemTags, // write memory tags
177 
178     eServerPacketType_qLLDBSaveCore,
179     eServerPacketType_QSetIgnoredExceptions,
180     eServerPacketType_QNonStop,
181     eServerPacketType_vStopped,
182     eServerPacketType_vCtrlC,
183     eServerPacketType_vStdio,
184   };
185 
186   ServerPacketType GetServerPacketType() const;
187 
188   enum ResponseType { eUnsupported = 0, eAck, eNack, eError, eOK, eResponse };
189 
190   ResponseType GetResponseType() const;
191 
192   bool IsOKResponse() const;
193 
194   bool IsUnsupportedResponse() const;
195 
196   bool IsNormalResponse() const;
197 
198   bool IsErrorResponse() const;
199 
200   // Returns zero if the packet isn't a EXX packet where XX are two hex digits.
201   // Otherwise the error encoded in XX is returned.
202   uint8_t GetError();
203 
204   lldb_private::Status GetStatus();
205 
206   size_t GetEscapedBinaryData(std::string &str);
207 
208   static constexpr lldb::pid_t AllProcesses = UINT64_MAX;
209   static constexpr lldb::tid_t AllThreads = UINT64_MAX;
210 
211   // Read thread-id from the packet.  If the packet is valid, returns
212   // the pair (PID, TID), otherwise returns std::nullopt.  If the packet
213   // does not list a PID, default_pid is used.
214   std::optional<std::pair<lldb::pid_t, lldb::tid_t>>
215   GetPidTid(lldb::pid_t default_pid);
216 
217 protected:
218   ResponseValidatorCallback m_validator = nullptr;
219   void *m_validator_baton = nullptr;
220 };
221 
222 #endif // LLDB_UTILITY_STRINGEXTRACTORGDBREMOTE_H
223