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 
89     PortMap m_port_map;
90     uint16_t m_port_offset;
91     struct { lldb::pid_t pid; uint16_t port; std::string socket_name; } m_pending_gdb_server;
92 
93     PacketResult
94     Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet);
95 
96     PacketResult
97     Handle_qQueryGDBServer (StringExtractorGDBRemote &packet);
98 
99     PacketResult
100     Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet);
101 
102     PacketResult
103     Handle_qProcessInfo (StringExtractorGDBRemote &packet);
104 
105     PacketResult
106     Handle_qGetWorkingDir (StringExtractorGDBRemote &packet);
107 
108     PacketResult
109     Handle_QSetWorkingDir (StringExtractorGDBRemote &packet);
110 
111     PacketResult
112     Handle_qC (StringExtractorGDBRemote &packet);
113 
114     PacketResult
115     Handle_jSignalsInfo(StringExtractorGDBRemote &packet);
116 
117 private:
118     bool
119     KillSpawnedProcess (lldb::pid_t pid);
120 
121     bool
122     DebugserverProcessReaped (lldb::pid_t pid);
123 
124     static const FileSpec&
125     GetDomainSocketDir();
126 
127     static FileSpec
128     GetDomainSocketPath(const char* prefix);
129 
130     DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationServerPlatform);
131 };
132 
133 } // namespace process_gdb_remote
134 } // namespace lldb_private
135 
136 #endif // liblldb_GDBRemoteCommunicationServerPlatform_h_
137