blob: f0ee77dc1d66c2fe78482089ed33f587d330451b [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 //----------------------------------------------------------------------
39 // If we are using a port map where we can only use certain ports,
40 // get the next available port.
41 //
42 // If we are using a port map and we are out of ports, return UINT16_MAX
43 //
44 // If we aren't using a port map, return 0 to indicate we should bind to
45 // port 0 and then figure out which port we used.
46 //----------------------------------------------------------------------
47 uint16_t GetNextAvailablePort();
Tamas Berghammere13c2732015-02-11 10:29:30 +000048
Kate Stoneb9c1b512016-09-06 20:57:50 +000049 bool AssociatePortWithProcess(uint16_t port, lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000050
Kate Stoneb9c1b512016-09-06 20:57:50 +000051 bool FreePort(uint16_t port);
Tamas Berghammere13c2732015-02-11 10:29:30 +000052
Kate Stoneb9c1b512016-09-06 20:57:50 +000053 bool FreePortForProcess(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 void SetPortOffset(uint16_t port_offset);
Tamas Berghammere13c2732015-02-11 10:29:30 +000056
Kate Stoneb9c1b512016-09-06 20:57:50 +000057 void SetInferiorArguments(const lldb_private::Args &args);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000058
Zachary Turner97206d52017-05-12 04:51:55 +000059 Status LaunchGDBServer(const lldb_private::Args &args, std::string hostname,
60 lldb::pid_t &pid, uint16_t &port,
61 std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000062
Kate Stoneb9c1b512016-09-06 20:57:50 +000063 void SetPendingGdbServer(lldb::pid_t pid, uint16_t port,
64 const std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000065
Tamas Berghammere13c2732015-02-11 10:29:30 +000066protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000067 const Socket::SocketProtocol m_socket_protocol;
68 const std::string m_socket_scheme;
69 std::recursive_mutex m_spawned_pids_mutex;
70 std::set<lldb::pid_t> m_spawned_pids;
Tamas Berghammere13c2732015-02-11 10:29:30 +000071
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 PortMap m_port_map;
73 uint16_t m_port_offset;
74 struct {
75 lldb::pid_t pid;
76 uint16_t port;
77 std::string socket_name;
78 } m_pending_gdb_server;
Tamas Berghammere13c2732015-02-11 10:29:30 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 PacketResult Handle_qLaunchGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000081
Kate Stoneb9c1b512016-09-06 20:57:50 +000082 PacketResult Handle_qQueryGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000083
Kate Stoneb9c1b512016-09-06 20:57:50 +000084 PacketResult Handle_qKillSpawnedProcess(StringExtractorGDBRemote &packet);
Pavel Labath6e4f19d2015-07-29 12:33:31 +000085
Kate Stoneb9c1b512016-09-06 20:57:50 +000086 PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000087
Kate Stoneb9c1b512016-09-06 20:57:50 +000088 PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000089
Kate Stoneb9c1b512016-09-06 20:57:50 +000090 PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000091
Kate Stoneb9c1b512016-09-06 20:57:50 +000092 PacketResult Handle_qC(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000093
Kate Stoneb9c1b512016-09-06 20:57:50 +000094 PacketResult Handle_jSignalsInfo(StringExtractorGDBRemote &packet);
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000095
Tamas Berghammere13c2732015-02-11 10:29:30 +000096private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 bool KillSpawnedProcess(lldb::pid_t pid);
Pavel Labath6e4f19d2015-07-29 12:33:31 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 bool DebugserverProcessReaped(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000100
Kate Stoneb9c1b512016-09-06 20:57:50 +0000101 static const FileSpec &GetDomainSocketDir();
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000102
Kate Stoneb9c1b512016-09-06 20:57:50 +0000103 static FileSpec GetDomainSocketPath(const char *prefix);
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000104
Kate Stoneb9c1b512016-09-06 20:57:50 +0000105 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerPlatform);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000106};
107
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000108} // namespace process_gdb_remote
109} // namespace lldb_private
110
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000111#endif // liblldb_GDBRemoteCommunicationServerPlatform_h_