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