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