1 //===-- GDBRemoteCommunicationServerCommon.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_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERCOMMON_H
10 #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERCOMMON_H
11 
12 #include <string>
13 
14 #include "lldb/Host/ProcessLaunchInfo.h"
15 #include "lldb/lldb-private-forward.h"
16 
17 #include "GDBRemoteCommunicationServer.h"
18 
19 class StringExtractorGDBRemote;
20 
21 namespace lldb_private {
22 namespace process_gdb_remote {
23 
24 class ProcessGDBRemote;
25 
26 class GDBRemoteCommunicationServerCommon : public GDBRemoteCommunicationServer {
27 public:
28   GDBRemoteCommunicationServerCommon(const char *comm_name,
29                                      const char *listener_name);
30 
31   ~GDBRemoteCommunicationServerCommon() override;
32 
33 protected:
34   ProcessLaunchInfo m_process_launch_info;
35   Status m_process_launch_error;
36   ProcessInstanceInfoList m_proc_infos;
37   uint32_t m_proc_infos_index;
38 
39   PacketResult Handle_A(StringExtractorGDBRemote &packet);
40 
41   PacketResult Handle_qHostInfo(StringExtractorGDBRemote &packet);
42 
43   PacketResult Handle_qProcessInfoPID(StringExtractorGDBRemote &packet);
44 
45   PacketResult Handle_qfProcessInfo(StringExtractorGDBRemote &packet);
46 
47   PacketResult Handle_qsProcessInfo(StringExtractorGDBRemote &packet);
48 
49   PacketResult Handle_qUserName(StringExtractorGDBRemote &packet);
50 
51   PacketResult Handle_qGroupName(StringExtractorGDBRemote &packet);
52 
53   PacketResult Handle_qSpeedTest(StringExtractorGDBRemote &packet);
54 
55   PacketResult Handle_vFile_Open(StringExtractorGDBRemote &packet);
56 
57   PacketResult Handle_vFile_Close(StringExtractorGDBRemote &packet);
58 
59   PacketResult Handle_vFile_pRead(StringExtractorGDBRemote &packet);
60 
61   PacketResult Handle_vFile_pWrite(StringExtractorGDBRemote &packet);
62 
63   PacketResult Handle_vFile_Size(StringExtractorGDBRemote &packet);
64 
65   PacketResult Handle_vFile_Mode(StringExtractorGDBRemote &packet);
66 
67   PacketResult Handle_vFile_Exists(StringExtractorGDBRemote &packet);
68 
69   PacketResult Handle_vFile_symlink(StringExtractorGDBRemote &packet);
70 
71   PacketResult Handle_vFile_unlink(StringExtractorGDBRemote &packet);
72 
73   PacketResult Handle_vFile_FStat(StringExtractorGDBRemote &packet);
74 
75   PacketResult Handle_vFile_Stat(StringExtractorGDBRemote &packet);
76 
77   PacketResult Handle_vFile_MD5(StringExtractorGDBRemote &packet);
78 
79   PacketResult Handle_qEcho(StringExtractorGDBRemote &packet);
80 
81   PacketResult Handle_qModuleInfo(StringExtractorGDBRemote &packet);
82 
83   PacketResult Handle_jModulesInfo(StringExtractorGDBRemote &packet);
84 
85   PacketResult Handle_qPlatform_shell(StringExtractorGDBRemote &packet);
86 
87   PacketResult Handle_qPlatform_mkdir(StringExtractorGDBRemote &packet);
88 
89   PacketResult Handle_qPlatform_chmod(StringExtractorGDBRemote &packet);
90 
91   PacketResult Handle_qSupported(StringExtractorGDBRemote &packet);
92 
93   PacketResult Handle_QSetDetachOnError(StringExtractorGDBRemote &packet);
94 
95   PacketResult Handle_QStartNoAckMode(StringExtractorGDBRemote &packet);
96 
97   PacketResult Handle_QSetSTDIN(StringExtractorGDBRemote &packet);
98 
99   PacketResult Handle_QSetSTDOUT(StringExtractorGDBRemote &packet);
100 
101   PacketResult Handle_QSetSTDERR(StringExtractorGDBRemote &packet);
102 
103   PacketResult Handle_qLaunchSuccess(StringExtractorGDBRemote &packet);
104 
105   PacketResult Handle_QEnvironment(StringExtractorGDBRemote &packet);
106 
107   PacketResult Handle_QEnvironmentHexEncoded(StringExtractorGDBRemote &packet);
108 
109   PacketResult Handle_QLaunchArch(StringExtractorGDBRemote &packet);
110 
111   static void CreateProcessInfoResponse(const ProcessInstanceInfo &proc_info,
112                                         StreamString &response);
113 
114   static void CreateProcessInfoResponse_DebugServerStyle(
115       const ProcessInstanceInfo &proc_info, StreamString &response);
116 
117   template <typename T>
118   void RegisterMemberFunctionHandler(
119       StringExtractorGDBRemote::ServerPacketType packet_type,
120       PacketResult (T::*handler)(StringExtractorGDBRemote &packet)) {
121     RegisterPacketHandler(packet_type,
122                           [this, handler](StringExtractorGDBRemote packet,
123                                           Status &error, bool &interrupt,
124                                           bool &quit) {
125                             return (static_cast<T *>(this)->*handler)(packet);
126                           });
127   }
128 
129   /// Launch a process with the current launch settings.
130   ///
131   /// This method supports running an lldb-gdbserver or similar
132   /// server in a situation where the startup code has been provided
133   /// with all the information for a child process to be launched.
134   ///
135   /// \return
136   ///     An Status object indicating the success or failure of the
137   ///     launch.
138   virtual Status LaunchProcess() = 0;
139 
140   virtual FileSpec FindModuleFile(const std::string &module_path,
141                                   const ArchSpec &arch);
142 
143   // Process client_features (qSupported) and return an array of server features
144   // to be returned in response.
145   virtual std::vector<std::string>
146   HandleFeatures(llvm::ArrayRef<llvm::StringRef> client_features);
147 
148 private:
149   ModuleSpec GetModuleInfo(llvm::StringRef module_path, llvm::StringRef triple);
150 };
151 
152 } // namespace process_gdb_remote
153 } // namespace lldb_private
154 
155 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERCOMMON_H
156