xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/Platform/gdb-server/PlatformRemoteGDBServer.h (revision c9ccf3a32da427475985b85d7df023ccfb138c27)
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> GetSupportedArchitectures() override {
69     return m_supported_architectures;
70   }
71 
72   size_t GetSoftwareBreakpointTrapOpcode(Target &target,
73                                          BreakpointSite *bp_site) override;
74 
75   bool GetRemoteOSVersion() override;
76 
77   llvm::Optional<std::string> GetRemoteOSBuildString() override;
78 
79   llvm::Optional<std::string> GetRemoteOSKernelDescription() override;
80 
81   // Remote Platform subclasses need to override this function
82   ArchSpec GetRemoteSystemArchitecture() override;
83 
84   FileSpec GetRemoteWorkingDirectory() override;
85 
86   bool SetRemoteWorkingDirectory(const FileSpec &working_dir) override;
87 
88   // Remote subclasses should override this and return a valid instance
89   // name if connected.
90   const char *GetHostname() override;
91 
92   UserIDResolver &GetUserIDResolver() override { return *this; }
93 
94   bool IsConnected() const override;
95 
96   Status ConnectRemote(Args &args) override;
97 
98   Status DisconnectRemote() override;
99 
100   Status MakeDirectory(const FileSpec &file_spec,
101                        uint32_t file_permissions) override;
102 
103   Status GetFilePermissions(const FileSpec &file_spec,
104                             uint32_t &file_permissions) override;
105 
106   Status SetFilePermissions(const FileSpec &file_spec,
107                             uint32_t file_permissions) override;
108 
109   lldb::user_id_t OpenFile(const FileSpec &file_spec, File::OpenOptions flags,
110                            uint32_t mode, Status &error) override;
111 
112   bool CloseFile(lldb::user_id_t fd, Status &error) override;
113 
114   uint64_t ReadFile(lldb::user_id_t fd, uint64_t offset, void *data_ptr,
115                     uint64_t len, Status &error) override;
116 
117   uint64_t WriteFile(lldb::user_id_t fd, uint64_t offset, const void *data,
118                      uint64_t len, Status &error) override;
119 
120   lldb::user_id_t GetFileSize(const FileSpec &file_spec) override;
121 
122   void AutoCompleteDiskFileOrDirectory(CompletionRequest &request,
123                                        bool only_dir) override;
124 
125   Status PutFile(const FileSpec &source, const FileSpec &destination,
126                  uint32_t uid = UINT32_MAX, uint32_t gid = UINT32_MAX) override;
127 
128   Status CreateSymlink(const FileSpec &src, const FileSpec &dst) override;
129 
130   bool GetFileExists(const FileSpec &file_spec) override;
131 
132   Status Unlink(const FileSpec &path) override;
133 
134   Status RunShellCommand(
135       llvm::StringRef shell, llvm::StringRef command,
136       const FileSpec &working_dir, // Pass empty FileSpec to use the current
137                                    // working directory
138       int *status_ptr, // Pass NULL if you don't want the process exit status
139       int *signo_ptr,  // Pass NULL if you don't want the signal that caused the
140                        // process to exit
141       std::string
142           *command_output, // Pass NULL if you don't want the command output
143       const lldb_private::Timeout<std::micro> &timeout) override;
144 
145   void CalculateTrapHandlerSymbolNames() override;
146 
147   const lldb::UnixSignalsSP &GetRemoteUnixSignals() override;
148 
149   size_t ConnectToWaitingProcesses(lldb_private::Debugger &debugger,
150                                    lldb_private::Status &error) override;
151 
152   virtual size_t
153   GetPendingGdbServerList(std::vector<std::string> &connection_urls);
154 
155 protected:
156   std::unique_ptr<process_gdb_remote::GDBRemoteCommunicationClient>
157       m_gdb_client_up;
158   std::string m_platform_description; // After we connect we can get a more
159                                       // complete description of what we are
160                                       // connected to
161   std::string m_platform_scheme;
162   std::string m_platform_hostname;
163 
164   lldb::UnixSignalsSP m_remote_signals_sp;
165 
166   // Launch the debug server on the remote host - caller connects to launched
167   // debug server using connect_url.
168   // Subclasses should override this method if they want to do extra actions
169   // before or
170   // after launching the debug server.
171   virtual bool LaunchGDBServer(lldb::pid_t &pid, std::string &connect_url);
172 
173   virtual bool KillSpawnedProcess(lldb::pid_t pid);
174 
175   virtual std::string MakeUrl(const char *scheme, const char *hostname,
176                               uint16_t port, const char *path);
177 
178 private:
179   std::string MakeGdbServerUrl(const std::string &platform_scheme,
180                                const std::string &platform_hostname,
181                                uint16_t port, const char *socket_name);
182 
183   llvm::Optional<std::string> DoGetUserName(UserIDResolver::id_t uid) override;
184   llvm::Optional<std::string> DoGetGroupName(UserIDResolver::id_t uid) override;
185 
186   std::vector<ArchSpec> m_supported_architectures;
187 
188   PlatformRemoteGDBServer(const PlatformRemoteGDBServer &) = delete;
189   const PlatformRemoteGDBServer &
190   operator=(const PlatformRemoteGDBServer &) = delete;
191 };
192 
193 } // namespace platform_gdb_server
194 } // namespace lldb_private
195 
196 #endif // LLDB_SOURCE_PLUGINS_PLATFORM_GDB_SERVER_PLATFORMREMOTEGDBSERVER_H
197