LLDB  mainline
GDBRemoteCommunicationServerPlatform.h
Go to the documentation of this file.
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 LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERPLATFORM_H
10 #define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERPLATFORM_H
11 
12 #include <map>
13 #include <mutex>
14 #include <set>
15 
17 #include "lldb/Host/Socket.h"
18 
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/Support/Error.h"
21 
22 namespace lldb_private {
23 namespace process_gdb_remote {
24 
27 public:
28  class PortMap {
29  public:
30  // This class is used to restrict the range of ports that
31  // platform created debugserver/gdbserver processes will
32  // communicate on.
33 
34  // Construct an empty map, where empty means any port is allowed.
35  PortMap() = default;
36 
37  // Make a port map with a range of free ports
38  // from min_port to max_port-1.
39  PortMap(uint16_t min_port, uint16_t max_port);
40 
41  // Add a port to the map. If it is already in the map do not modify
42  // its mapping. (used ports remain used, new ports start as free)
43  void AllowPort(uint16_t port);
44 
45  // If we are using a port map where we can only use certain ports,
46  // get the next available port.
47  //
48  // If we are using a port map and we are out of ports, return an error.
49  //
50  // If we aren't using a port map, return 0 to indicate we should bind to
51  // port 0 and then figure out which port we used.
52  llvm::Expected<uint16_t> GetNextAvailablePort();
53 
54  // Tie a port to a process ID. Returns false if the port is not in the port
55  // map. If the port is already in use it will be moved to the given pid.
56  // FIXME: This is and GetNextAvailablePort make create a race condition if
57  // the portmap is shared between processes.
59 
60  // Free the given port. Returns false if the port is not in the map.
61  bool FreePort(uint16_t port);
62 
63  // Free the port associated with the given pid. Returns false if there is
64  // no port associated with the pid.
66 
67  // Returns true if there are no ports in the map, regardless of the state
68  // of those ports. Meaning a map with 1 used port is not empty.
69  bool empty() const;
70 
71  private:
72  std::map<uint16_t, lldb::pid_t> m_port_map;
73  };
74 
76  const Socket::SocketProtocol socket_protocol, const char *socket_scheme);
77 
79 
80  Status LaunchProcess() override;
81 
82  // Set both ports to zero to let the platform automatically bind to
83  // a port chosen by the OS.
84  void SetPortMap(PortMap &&port_map);
85 
86  void SetPortOffset(uint16_t port_offset);
87 
88  void SetInferiorArguments(const lldb_private::Args &args);
89 
90  // Set port if you want to use a specific port number.
91  // Otherwise port will be set to the port that was chosen for you.
93  lldb::pid_t &pid, llvm::Optional<uint16_t> &port,
95 
97  const std::string &socket_name);
98 
99 protected:
102  std::recursive_mutex m_spawned_pids_mutex;
103  std::set<lldb::pid_t> m_spawned_pids;
104 
107  struct {
112 
114 
116 
118 
120 
122 
124 
126 
128 
130 
131 private:
133 
135 
136  static const FileSpec &GetDomainSocketDir();
137 
138  static FileSpec GetDomainSocketPath(const char *prefix);
139 
141  const GDBRemoteCommunicationServerPlatform &) = delete;
144 };
145 
146 } // namespace process_gdb_remote
147 } // namespace lldb_private
148 
149 #endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERPLATFORM_H
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_qProcessInfo
PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:390
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_qGetWorkingDir
PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:440
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_jSignalsInfo
PacketResult Handle_jSignalsInfo(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:492
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::m_socket_protocol
const Socket::SocketProtocol m_socket_protocol
Definition: GDBRemoteCommunicationServerPlatform.h:100
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::LaunchProcess
Status LaunchProcess() override
Launch a process with the current launch settings.
Definition: GDBRemoteCommunicationServerPlatform.cpp:527
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_qQueryGDBServer
PacketResult Handle_qQueryGDBServer(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:287
lldb_private::Socket::SocketProtocol
SocketProtocol
Definition: Socket.h:43
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::AssociatePortWithProcess
bool AssociatePortWithProcess(uint16_t port, lldb::pid_t pid)
Definition: GDBRemoteCommunicationServerPlatform.cpp:72
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::GetDomainSocketPath
static FileSpec GetDomainSocketPath(const char *prefix)
Definition: GDBRemoteCommunicationServerPlatform.cpp:585
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap
Definition: GDBRemoteCommunicationServerPlatform.h:28
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::~GDBRemoteCommunicationServerPlatform
~GDBRemoteCommunicationServerPlatform() override
GDBRemoteCommunicationServerCommon.h
lldb_private::process_gdb_remote::GDBRemoteCommunication::PacketResult
PacketResult
Definition: GDBRemoteCommunication.h:91
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::SetPortMap
void SetPortMap(PortMap &&port_map)
Definition: GDBRemoteCommunicationServerPlatform.cpp:564
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::m_port_map
PortMap m_port_map
Definition: GDBRemoteCommunicationServerPlatform.h:105
lldb_private::Args
Definition: Args.h:33
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_qLaunchGDBServer
PacketResult Handle_qLaunchGDBServer(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:226
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::PortMap
PortMap()=default
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerCommon
Definition: GDBRemoteCommunicationServerCommon.h:27
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::LaunchGDBServer
Status LaunchGDBServer(const lldb_private::Args &args, std::string hostname, lldb::pid_t &pid, llvm::Optional< uint16_t > &port, std::string &socket_name)
Definition: GDBRemoteCommunicationServerPlatform.cpp:159
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::SetPortOffset
void SetPortOffset(uint16_t port_offset)
Definition: GDBRemoteCommunicationServerPlatform.cpp:597
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::m_spawned_pids
std::set< lldb::pid_t > m_spawned_pids
Definition: GDBRemoteCommunicationServerPlatform.h:103
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::operator=
const GDBRemoteCommunicationServerPlatform & operator=(const GDBRemoteCommunicationServerPlatform &)=delete
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::GetNextAvailablePort
llvm::Expected< uint16_t > GetNextAvailablePort()
Definition: GDBRemoteCommunicationServerPlatform.cpp:57
lldb_private::FileSpec
Definition: FileSpec.h:56
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::FreePortForProcess
bool FreePortForProcess(lldb::pid_t pid)
Definition: GDBRemoteCommunicationServerPlatform.cpp:91
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::m_port_map
std::map< uint16_t, lldb::pid_t > m_port_map
Definition: GDBRemoteCommunicationServerPlatform.h:72
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::FreePort
bool FreePort(uint16_t port)
Definition: GDBRemoteCommunicationServerPlatform.cpp:82
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_qC
PacketResult Handle_qC(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:465
Socket.h
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_qPathComplete
PacketResult Handle_qPathComplete(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:408
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_QSetWorkingDir
PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:453
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::m_spawned_pids_mutex
std::recursive_mutex m_spawned_pids_mutex
Definition: GDBRemoteCommunicationServerPlatform.h:102
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::m_socket_scheme
const std::string m_socket_scheme
Definition: GDBRemoteCommunicationServerPlatform.h:101
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::pid
lldb::pid_t pid
Definition: GDBRemoteCommunicationServerPlatform.h:108
string
string(SUBSTRING ${p} 10 -1 pStripped) if($
Definition: Plugins/CMakeLists.txt:40
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::AllowPort
void AllowPort(uint16_t port)
Definition: GDBRemoteCommunicationServerPlatform.cpp:51
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::socket_name
std::string socket_name
Definition: GDBRemoteCommunicationServerPlatform.h:110
lldb_private::Status
Definition: Status.h:44
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::Handle_qKillSpawnedProcess
PacketResult Handle_qKillSpawnedProcess(StringExtractorGDBRemote &packet)
Definition: GDBRemoteCommunicationServerPlatform.cpp:312
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped
bool DebugserverProcessReaped(lldb::pid_t pid)
Definition: GDBRemoteCommunicationServerPlatform.cpp:519
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform
Definition: GDBRemoteCommunicationServerPlatform.h:25
lldb::pid_t
uint64_t pid_t
Definition: lldb-types.h:85
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::GetDomainSocketDir
static const FileSpec & GetDomainSocketDir()
Definition: GDBRemoteCommunicationServerPlatform.cpp:568
uint16_t
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::SetPendingGdbServer
void SetPendingGdbServer(lldb::pid_t pid, uint16_t port, const std::string &socket_name)
Definition: GDBRemoteCommunicationServerPlatform.cpp:601
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::port
uint16_t port
Definition: GDBRemoteCommunicationServerPlatform.h:109
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::m_port_offset
uint16_t m_port_offset
Definition: GDBRemoteCommunicationServerPlatform.h:106
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::PortMap::empty
bool empty() const
Definition: GDBRemoteCommunicationServerPlatform.cpp:104
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::m_pending_gdb_server
struct lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::@76 m_pending_gdb_server
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
StringExtractorGDBRemote
Definition: StringExtractorGDBRemote.h:21
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::SetInferiorArguments
void SetInferiorArguments(const lldb_private::Args &args)
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::KillSpawnedProcess
bool KillSpawnedProcess(lldb::pid_t pid)
Definition: GDBRemoteCommunicationServerPlatform.cpp:334
lldb_private::process_gdb_remote::GDBRemoteCommunicationServerPlatform::GDBRemoteCommunicationServerPlatform
GDBRemoteCommunicationServerPlatform(const Socket::SocketProtocol socket_protocol, const char *socket_scheme)
Definition: GDBRemoteCommunicationServerPlatform.cpp:109