1 //===-- GDBRemoteCommunicationServerPlatform.h ------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #ifndef liblldb_GDBRemoteCommunicationServerPlatform_h_
11 #define liblldb_GDBRemoteCommunicationServerPlatform_h_
12 
13 // C Includes
14 // C++ Includes
15 #include <map>
16 #include <mutex>
17 #include <set>
18 
19 // Other libraries and framework includes
20 // Project includes
21 #include "GDBRemoteCommunicationServerCommon.h"
22 #include "lldb/Host/Socket.h"
23 
24 namespace lldb_private {
25 namespace process_gdb_remote {
26 
27 class GDBRemoteCommunicationServerPlatform
28     : public GDBRemoteCommunicationServerCommon {
29 public:
30   typedef std::map<uint16_t, lldb::pid_t> PortMap;
31 
32   GDBRemoteCommunicationServerPlatform(
33       const Socket::SocketProtocol socket_protocol, const char *socket_scheme);
34 
35   ~GDBRemoteCommunicationServerPlatform() override;
36 
37   Error LaunchProcess() override;
38 
39   // Set both ports to zero to let the platform automatically bind to
40   // a port chosen by the OS.
41   void SetPortMap(PortMap &&port_map);
42 
43   //----------------------------------------------------------------------
44   // If we are using a port map where we can only use certain ports,
45   // get the next available port.
46   //
47   // If we are using a port map and we are out of ports, return UINT16_MAX
48   //
49   // If we aren't using a port map, return 0 to indicate we should bind to
50   // port 0 and then figure out which port we used.
51   //----------------------------------------------------------------------
52   uint16_t GetNextAvailablePort();
53 
54   bool AssociatePortWithProcess(uint16_t port, lldb::pid_t pid);
55 
56   bool FreePort(uint16_t port);
57 
58   bool FreePortForProcess(lldb::pid_t pid);
59 
60   void SetPortOffset(uint16_t port_offset);
61 
62   void SetInferiorArguments(const lldb_private::Args &args);
63 
64   Error LaunchGDBServer(const lldb_private::Args &args, std::string hostname,
65                         lldb::pid_t &pid, uint16_t &port,
66                         std::string &socket_name);
67 
68   void SetPendingGdbServer(lldb::pid_t pid, uint16_t port,
69                            const std::string &socket_name);
70 
71 protected:
72   const Socket::SocketProtocol m_socket_protocol;
73   const std::string m_socket_scheme;
74   std::recursive_mutex m_spawned_pids_mutex;
75   std::set<lldb::pid_t> m_spawned_pids;
76 
77   PortMap m_port_map;
78   uint16_t m_port_offset;
79   struct {
80     lldb::pid_t pid;
81     uint16_t port;
82     std::string socket_name;
83   } m_pending_gdb_server;
84 
85   PacketResult Handle_qLaunchGDBServer(StringExtractorGDBRemote &packet);
86 
87   PacketResult Handle_qQueryGDBServer(StringExtractorGDBRemote &packet);
88 
89   PacketResult Handle_qKillSpawnedProcess(StringExtractorGDBRemote &packet);
90 
91   PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
92 
93   PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
94 
95   PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
96 
97   PacketResult Handle_qC(StringExtractorGDBRemote &packet);
98 
99   PacketResult Handle_jSignalsInfo(StringExtractorGDBRemote &packet);
100 
101 private:
102   bool KillSpawnedProcess(lldb::pid_t pid);
103 
104   bool DebugserverProcessReaped(lldb::pid_t pid);
105 
106   static const FileSpec &GetDomainSocketDir();
107 
108   static FileSpec GetDomainSocketPath(const char *prefix);
109 
110   DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerPlatform);
111 };
112 
113 } // namespace process_gdb_remote
114 } // namespace lldb_private
115 
116 #endif // liblldb_GDBRemoteCommunicationServerPlatform_h_
117