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