1 //===-- PlatformRemoteGDBServer.h ----------------------------------------*- C++
2 //-*-===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef LLDB_SOURCE_PLUGINS_PLATFORM_GDB_SERVER_PLATFORMREMOTEGDBSERVER_H
11 #define LLDB_SOURCE_PLUGINS_PLATFORM_GDB_SERVER_PLATFORMREMOTEGDBSERVER_H
12 
13 #include <string>
14 
15 #include "Plugins/Process/Utility/GDBRemoteSignals.h"
16 #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h"
17 #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationReplayServer.h"
18 #include "lldb/Target/Platform.h"
19 
20 namespace lldb_private {
21 namespace platform_gdb_server {
22 
23 class PlatformRemoteGDBServer : public Platform, private UserIDResolver {
24 public:
25   static void Initialize();
26 
27   static void Terminate();
28 
29   static lldb::PlatformSP CreateInstance(bool force, const ArchSpec *arch);
30 
31   static ConstString GetPluginNameStatic();
32 
33   static const char *GetDescriptionStatic();
34 
35   PlatformRemoteGDBServer();
36 
37   ~PlatformRemoteGDBServer() override;
38 
39   // lldb_private::PluginInterface functions
40   llvm::StringRef GetPluginName() override {
41     return GetPluginNameStatic().GetStringRef();
42   }
43 
44   // lldb_private::Platform functions
45   Status
46   ResolveExecutable(const ModuleSpec &module_spec, lldb::ModuleSP &module_sp,
47                     const FileSpecList *module_search_paths_ptr) override;
48 
49   bool GetModuleSpec(const FileSpec &module_file_spec, const ArchSpec &arch,
50                      ModuleSpec &module_spec) override;
51 
52   const char *GetDescription() override;
53 
54   Status GetFileWithUUID(const FileSpec &platform_file, const UUID *uuid_ptr,
55                          FileSpec &local_file) override;
56 
57   bool GetProcessInfo(lldb::pid_t pid, ProcessInstanceInfo &proc_info) override;
58 
59   uint32_t FindProcesses(const ProcessInstanceInfoMatch &match_info,
60                          ProcessInstanceInfoList &process_infos) override;
61 
62   Status LaunchProcess(ProcessLaunchInfo &launch_info) override;
63 
64   Status KillProcess(const lldb::pid_t pid) override;
65 
66   lldb::ProcessSP DebugProcess(ProcessLaunchInfo &launch_info,
67                                Debugger &debugger, Target &target,
68                                Status &error) override;
69 
70   lldb::ProcessSP Attach(ProcessAttachInfo &attach_info, Debugger &debugger,
71                          Target *target, // Can be NULL, if NULL create a new
72                                          // target, else use existing one
73                          Status &error) override;
74 
75   bool GetSupportedArchitectureAtIndex(uint32_t idx, ArchSpec &arch) override;
76 
77   size_t GetSoftwareBreakpointTrapOpcode(Target &target,
78                                          BreakpointSite *bp_site) override;
79 
80   bool GetRemoteOSVersion() override;
81 
82   llvm::Optional<std::string> GetRemoteOSBuildString() override;
83 
84   bool GetRemoteOSKernelDescription(std::string &s) override;
85 
86   // Remote Platform subclasses need to override this function
87   ArchSpec GetRemoteSystemArchitecture() override;
88 
89   FileSpec GetRemoteWorkingDirectory() override;
90 
91   bool SetRemoteWorkingDirectory(const FileSpec &working_dir) override;
92 
93   // Remote subclasses should override this and return a valid instance
94   // name if connected.
95   const char *GetHostname() override;
96 
97   UserIDResolver &GetUserIDResolver() override { return *this; }
98 
99   bool IsConnected() const override;
100 
101   Status ConnectRemote(Args &args) override;
102 
103   Status DisconnectRemote() override;
104 
105   Status MakeDirectory(const FileSpec &file_spec,
106                        uint32_t file_permissions) override;
107 
108   Status GetFilePermissions(const FileSpec &file_spec,
109                             uint32_t &file_permissions) override;
110 
111   Status SetFilePermissions(const FileSpec &file_spec,
112                             uint32_t file_permissions) override;
113 
114   lldb::user_id_t OpenFile(const FileSpec &file_spec, File::OpenOptions flags,
115                            uint32_t mode, Status &error) override;
116 
117   bool CloseFile(lldb::user_id_t fd, Status &error) override;
118 
119   uint64_t ReadFile(lldb::user_id_t fd, uint64_t offset, void *data_ptr,
120                     uint64_t len, Status &error) override;
121 
122   uint64_t WriteFile(lldb::user_id_t fd, uint64_t offset, const void *data,
123                      uint64_t len, Status &error) override;
124 
125   lldb::user_id_t GetFileSize(const FileSpec &file_spec) override;
126 
127   void AutoCompleteDiskFileOrDirectory(CompletionRequest &request,
128                                        bool only_dir) override;
129 
130   Status PutFile(const FileSpec &source, const FileSpec &destination,
131                  uint32_t uid = UINT32_MAX, uint32_t gid = UINT32_MAX) override;
132 
133   Status CreateSymlink(const FileSpec &src, const FileSpec &dst) override;
134 
135   bool GetFileExists(const FileSpec &file_spec) override;
136 
137   Status Unlink(const FileSpec &path) override;
138 
139   Status RunShellCommand(
140       llvm::StringRef shell, llvm::StringRef command,
141       const FileSpec &working_dir, // Pass empty FileSpec to use the current
142                                    // working directory
143       int *status_ptr, // Pass NULL if you don't want the process exit status
144       int *signo_ptr,  // Pass NULL if you don't want the signal that caused the
145                        // process to exit
146       std::string
147           *command_output, // Pass NULL if you don't want the command output
148       const lldb_private::Timeout<std::micro> &timeout) override;
149 
150   void CalculateTrapHandlerSymbolNames() override;
151 
152   const lldb::UnixSignalsSP &GetRemoteUnixSignals() override;
153 
154   size_t ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
155                                    lldb_private::Status &error) override;
156 
157   virtual size_t
158   GetPendingGdbServerList(std::vector<std::string> &connection_urls);
159 
160 protected:
161   process_gdb_remote::GDBRemoteCommunicationClient m_gdb_client;
162   process_gdb_remote::GDBRemoteCommunicationReplayServer m_gdb_replay_server;
163   std::string m_platform_description; // After we connect we can get a more
164                                       // complete description of what we are
165                                       // connected to
166   std::string m_platform_scheme;
167   std::string m_platform_hostname;
168 
169   lldb::UnixSignalsSP m_remote_signals_sp;
170 
171   // Launch the debug server on the remote host - caller connects to launched
172   // debug server using connect_url.
173   // Subclasses should override this method if they want to do extra actions
174   // before or
175   // after launching the debug server.
176   virtual bool LaunchGDBServer(lldb::pid_t &pid, std::string &connect_url);
177 
178   virtual bool KillSpawnedProcess(lldb::pid_t pid);
179 
180   virtual std::string MakeUrl(const char *scheme, const char *hostname,
181                               uint16_t port, const char *path);
182 
183 private:
184   std::string MakeGdbServerUrl(const std::string &platform_scheme,
185                                const std::string &platform_hostname,
186                                uint16_t port, const char *socket_name);
187 
188   llvm::Optional<std::string> DoGetUserName(UserIDResolver::id_t uid) override;
189   llvm::Optional<std::string> DoGetGroupName(UserIDResolver::id_t uid) override;
190 
191   PlatformRemoteGDBServer(const PlatformRemoteGDBServer &) = delete;
192   const PlatformRemoteGDBServer &
193   operator=(const PlatformRemoteGDBServer &) = delete;
194 };
195 
196 } // namespace platform_gdb_server
197 } // namespace lldb_private
198 
199 #endif // LLDB_SOURCE_PLUGINS_PLATFORM_GDB_SERVER_PLATFORMREMOTEGDBSERVER_H
200