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