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 {
30 public:
31     typedef std::map<uint16_t, lldb::pid_t> PortMap;
32 
33     GDBRemoteCommunicationServerPlatform(const Socket::SocketProtocol socket_protocol,
34                                          const char* socket_scheme);
35 
36     ~GDBRemoteCommunicationServerPlatform() override;
37 
38     Error
39     LaunchProcess () override;
40 
41     // Set both ports to zero to let the platform automatically bind to
42     // a port chosen by the OS.
43     void
44     SetPortMap (PortMap &&port_map);
45 
46     //----------------------------------------------------------------------
47     // If we are using a port map where we can only use certain ports,
48     // get the next available port.
49     //
50     // If we are using a port map and we are out of ports, return UINT16_MAX
51     //
52     // If we aren't using a port map, return 0 to indicate we should bind to
53     // port 0 and then figure out which port we used.
54     //----------------------------------------------------------------------
55     uint16_t
56     GetNextAvailablePort ();
57 
58     bool
59     AssociatePortWithProcess (uint16_t port, lldb::pid_t pid);
60 
61     bool
62     FreePort (uint16_t port);
63 
64     bool
65     FreePortForProcess (lldb::pid_t pid);
66 
67     void
68     SetPortOffset (uint16_t port_offset);
69 
70     void
71     SetInferiorArguments (const lldb_private::Args& args);
72 
73     Error
74     LaunchGDBServer(const lldb_private::Args& args,
75                     std::string hostname,
76                     lldb::pid_t& pid,
77                     uint16_t& port,
78                     std::string& socket_name);
79 
80     void
81     SetPendingGdbServer(lldb::pid_t pid, uint16_t port, const std::string& socket_name);
82 
83 protected:
84     const Socket::SocketProtocol m_socket_protocol;
85     const std::string m_socket_scheme;
86     std::recursive_mutex m_spawned_pids_mutex;
87     std::set<lldb::pid_t> m_spawned_pids;
88     lldb::PlatformSP m_platform_sp;
89 
90     PortMap m_port_map;
91     uint16_t m_port_offset;
92     struct { lldb::pid_t pid; uint16_t port; std::string socket_name; } m_pending_gdb_server;
93 
94     PacketResult
95     Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet);
96 
97     PacketResult
98     Handle_qQueryGDBServer (StringExtractorGDBRemote &packet);
99 
100     PacketResult
101     Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet);
102 
103     PacketResult
104     Handle_qProcessInfo (StringExtractorGDBRemote &packet);
105 
106     PacketResult
107     Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
108 
109     PacketResult
110     Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
111 
112     PacketResult
113     Handle_qC (StringExtractorGDBRemote &packet);
114 
115     PacketResult
116     Handle_jSignalsInfo(StringExtractorGDBRemote &packet);
117 
118 private:
119     bool
120     KillSpawnedProcess (lldb::pid_t pid);
121 
122     bool
123     DebugserverProcessReaped (lldb::pid_t pid);
124 
125     static const FileSpec&
126     GetDomainSocketDir();
127 
128     static FileSpec
129     GetDomainSocketPath(const char* prefix);
130 
131     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServerPlatform);
132 };
133 
134 } // namespace process_gdb_remote
135 } // namespace lldb_private
136 
137 #endif // liblldb_GDBRemoteCommunicationServerPlatform_h_
138