blob: eacc99a012db58d917e695c67d3310d62c054f96 [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerPlatform.h ------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Tamas Berghammere13c2732015-02-11 10:29:30 +00006//
7//===----------------------------------------------------------------------===//
8
9#ifndef liblldb_GDBRemoteCommunicationServerPlatform_h_
10#define liblldb_GDBRemoteCommunicationServerPlatform_h_
11
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000012#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000013#include <mutex>
Pavel Labath6e4f19d2015-07-29 12:33:31 +000014#include <set>
15
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000016#include "GDBRemoteCommunicationServerCommon.h"
17#include "lldb/Host/Socket.h"
18
Tamas Berghammerdb264a62015-03-31 09:52:22 +000019namespace lldb_private {
20namespace process_gdb_remote {
21
Kate Stoneb9c1b512016-09-06 20:57:50 +000022class GDBRemoteCommunicationServerPlatform
23 : public GDBRemoteCommunicationServerCommon {
Tamas Berghammere13c2732015-02-11 10:29:30 +000024public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000025 typedef std::map<uint16_t, lldb::pid_t> PortMap;
Tamas Berghammere13c2732015-02-11 10:29:30 +000026
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 GDBRemoteCommunicationServerPlatform(
28 const Socket::SocketProtocol socket_protocol, const char *socket_scheme);
Tamas Berghammere13c2732015-02-11 10:29:30 +000029
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 ~GDBRemoteCommunicationServerPlatform() override;
Tamas Berghammere13c2732015-02-11 10:29:30 +000031
Zachary Turner97206d52017-05-12 04:51:55 +000032 Status LaunchProcess() override;
Tamas Berghammere13c2732015-02-11 10:29:30 +000033
Kate Stoneb9c1b512016-09-06 20:57:50 +000034 // 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);
Tamas Berghammere13c2732015-02-11 10:29:30 +000037
Kate Stoneb9c1b512016-09-06 20:57:50 +000038 // 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.
Kate Stoneb9c1b512016-09-06 20:57:50 +000045 uint16_t GetNextAvailablePort();
Tamas Berghammere13c2732015-02-11 10:29:30 +000046
Kate Stoneb9c1b512016-09-06 20:57:50 +000047 bool AssociatePortWithProcess(uint16_t port, lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 bool FreePort(uint16_t port);
Tamas Berghammere13c2732015-02-11 10:29:30 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 bool FreePortForProcess(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 void SetPortOffset(uint16_t port_offset);
Tamas Berghammere13c2732015-02-11 10:29:30 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 void SetInferiorArguments(const lldb_private::Args &args);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000056
Zachary Turner97206d52017-05-12 04:51:55 +000057 Status LaunchGDBServer(const lldb_private::Args &args, std::string hostname,
58 lldb::pid_t &pid, uint16_t &port,
59 std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000060
Kate Stoneb9c1b512016-09-06 20:57:50 +000061 void SetPendingGdbServer(lldb::pid_t pid, uint16_t port,
62 const std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000063
Tamas Berghammere13c2732015-02-11 10:29:30 +000064protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000065 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;
Tamas Berghammere13c2732015-02-11 10:29:30 +000069
Kate Stoneb9c1b512016-09-06 20:57:50 +000070 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;
Tamas Berghammere13c2732015-02-11 10:29:30 +000077
Kate Stoneb9c1b512016-09-06 20:57:50 +000078 PacketResult Handle_qLaunchGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 PacketResult Handle_qQueryGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 PacketResult Handle_qKillSpawnedProcess(StringExtractorGDBRemote &packet);
Pavel Labath6e4f19d2015-07-29 12:33:31 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 PacketResult Handle_qC(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 PacketResult Handle_jSignalsInfo(StringExtractorGDBRemote &packet);
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000093
Tamas Berghammere13c2732015-02-11 10:29:30 +000094private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 bool KillSpawnedProcess(lldb::pid_t pid);
Pavel Labath6e4f19d2015-07-29 12:33:31 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 bool DebugserverProcessReaped(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 static const FileSpec &GetDomainSocketDir();
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 static FileSpec GetDomainSocketPath(const char *prefix);
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerPlatform);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000104};
105
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000106} // namespace process_gdb_remote
107} // namespace lldb_private
108
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000109#endif // liblldb_GDBRemoteCommunicationServerPlatform_h_