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