Path: blob/main/contrib/llvm-project/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h
39644 views
//===-- GDBRemoteCommunicationServerPlatform.h ------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERPLATFORM_H9#define LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERPLATFORM_H1011#include <map>12#include <mutex>13#include <optional>14#include <set>1516#include "GDBRemoteCommunicationServerCommon.h"17#include "lldb/Host/Socket.h"1819#include "llvm/Support/Error.h"2021namespace lldb_private {22namespace process_gdb_remote {2324class GDBRemoteCommunicationServerPlatform25: public GDBRemoteCommunicationServerCommon {26public:27class PortMap {28public:29// This class is used to restrict the range of ports that30// platform created debugserver/gdbserver processes will31// communicate on.3233// Construct an empty map, where empty means any port is allowed.34PortMap() = default;3536// Make a port map with a range of free ports37// from min_port to max_port-1.38PortMap(uint16_t min_port, uint16_t max_port);3940// Add a port to the map. If it is already in the map do not modify41// its mapping. (used ports remain used, new ports start as free)42void AllowPort(uint16_t port);4344// If we are using a port map where we can only use certain ports,45// get the next available port.46//47// If we are using a port map and we are out of ports, return an error.48//49// If we aren't using a port map, return 0 to indicate we should bind to50// port 0 and then figure out which port we used.51llvm::Expected<uint16_t> GetNextAvailablePort();5253// Tie a port to a process ID. Returns false if the port is not in the port54// map. If the port is already in use it will be moved to the given pid.55// FIXME: This is and GetNextAvailablePort make create a race condition if56// the portmap is shared between processes.57bool AssociatePortWithProcess(uint16_t port, lldb::pid_t pid);5859// Free the given port. Returns false if the port is not in the map.60bool FreePort(uint16_t port);6162// Free the port associated with the given pid. Returns false if there is63// no port associated with the pid.64bool FreePortForProcess(lldb::pid_t pid);6566// Returns true if there are no ports in the map, regardless of the state67// of those ports. Meaning a map with 1 used port is not empty.68bool empty() const;6970private:71std::map<uint16_t, lldb::pid_t> m_port_map;72};7374GDBRemoteCommunicationServerPlatform(75const Socket::SocketProtocol socket_protocol, const char *socket_scheme);7677~GDBRemoteCommunicationServerPlatform() override;7879Status LaunchProcess() override;8081// Set both ports to zero to let the platform automatically bind to82// a port chosen by the OS.83void SetPortMap(PortMap &&port_map);8485void SetPortOffset(uint16_t port_offset);8687void SetInferiorArguments(const lldb_private::Args &args);8889// Set port if you want to use a specific port number.90// Otherwise port will be set to the port that was chosen for you.91Status LaunchGDBServer(const lldb_private::Args &args, std::string hostname,92lldb::pid_t &pid, std::optional<uint16_t> &port,93std::string &socket_name);9495void SetPendingGdbServer(lldb::pid_t pid, uint16_t port,96const std::string &socket_name);9798protected:99const Socket::SocketProtocol m_socket_protocol;100const std::string m_socket_scheme;101std::recursive_mutex m_spawned_pids_mutex;102std::set<lldb::pid_t> m_spawned_pids;103104PortMap m_port_map;105uint16_t m_port_offset;106struct {107lldb::pid_t pid;108uint16_t port;109std::string socket_name;110} m_pending_gdb_server;111112PacketResult Handle_qLaunchGDBServer(StringExtractorGDBRemote &packet);113114PacketResult Handle_qQueryGDBServer(StringExtractorGDBRemote &packet);115116PacketResult Handle_qKillSpawnedProcess(StringExtractorGDBRemote &packet);117118PacketResult Handle_qPathComplete(StringExtractorGDBRemote &packet);119120PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);121122PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);123124PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);125126PacketResult Handle_qC(StringExtractorGDBRemote &packet);127128PacketResult Handle_jSignalsInfo(StringExtractorGDBRemote &packet);129130private:131bool KillSpawnedProcess(lldb::pid_t pid);132133void DebugserverProcessReaped(lldb::pid_t pid);134135static const FileSpec &GetDomainSocketDir();136137static FileSpec GetDomainSocketPath(const char *prefix);138139GDBRemoteCommunicationServerPlatform(140const GDBRemoteCommunicationServerPlatform &) = delete;141const GDBRemoteCommunicationServerPlatform &142operator=(const GDBRemoteCommunicationServerPlatform &) = delete;143};144145} // namespace process_gdb_remote146} // namespace lldb_private147148#endif // LLDB_SOURCE_PLUGINS_PROCESS_GDB_REMOTE_GDBREMOTECOMMUNICATIONSERVERPLATFORM_H149150151