blob: df51e0367d1d2729f06511a038843ba507dc09a3 [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerPlatform.h ------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_GDBRemoteCommunicationServerPlatform_h_
11#define liblldb_GDBRemoteCommunicationServerPlatform_h_
12
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000013#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000014#include <mutex>
Pavel Labath6e4f19d2015-07-29 12:33:31 +000015#include <set>
16
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000017#include "GDBRemoteCommunicationServerCommon.h"
18#include "lldb/Host/Socket.h"
19
Tamas Berghammerdb264a62015-03-31 09:52:22 +000020namespace lldb_private {
21namespace process_gdb_remote {
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023class GDBRemoteCommunicationServerPlatform
24 : public GDBRemoteCommunicationServerCommon {
Tamas Berghammere13c2732015-02-11 10:29:30 +000025public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000026 typedef std::map<uint16_t, lldb::pid_t> PortMap;
Tamas Berghammere13c2732015-02-11 10:29:30 +000027
Kate Stoneb9c1b512016-09-06 20:57:50 +000028 GDBRemoteCommunicationServerPlatform(
29 const Socket::SocketProtocol socket_protocol, const char *socket_scheme);
Tamas Berghammere13c2732015-02-11 10:29:30 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 ~GDBRemoteCommunicationServerPlatform() override;
Tamas Berghammere13c2732015-02-11 10:29:30 +000032
Zachary Turner97206d52017-05-12 04:51:55 +000033 Status LaunchProcess() override;
Tamas Berghammere13c2732015-02-11 10:29:30 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 // Set both ports to zero to let the platform automatically bind to
36 // a port chosen by the OS.
37 void SetPortMap(PortMap &&port_map);
Tamas Berghammere13c2732015-02-11 10:29:30 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 //----------------------------------------------------------------------
40 // If we are using a port map where we can only use certain ports,
41 // get the next available port.
42 //
43 // If we are using a port map and we are out of ports, return UINT16_MAX
44 //
45 // If we aren't using a port map, return 0 to indicate we should bind to
46 // port 0 and then figure out which port we used.
47 //----------------------------------------------------------------------
48 uint16_t GetNextAvailablePort();
Tamas Berghammere13c2732015-02-11 10:29:30 +000049
Kate Stoneb9c1b512016-09-06 20:57:50 +000050 bool AssociatePortWithProcess(uint16_t port, lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000051
Kate Stoneb9c1b512016-09-06 20:57:50 +000052 bool FreePort(uint16_t port);
Tamas Berghammere13c2732015-02-11 10:29:30 +000053
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 bool FreePortForProcess(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 void SetPortOffset(uint16_t port_offset);
Tamas Berghammere13c2732015-02-11 10:29:30 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 void SetInferiorArguments(const lldb_private::Args &args);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000059
Zachary Turner97206d52017-05-12 04:51:55 +000060 Status LaunchGDBServer(const lldb_private::Args &args, std::string hostname,
61 lldb::pid_t &pid, uint16_t &port,
62 std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 void SetPendingGdbServer(lldb::pid_t pid, uint16_t port,
65 const std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000066
Tamas Berghammere13c2732015-02-11 10:29:30 +000067protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 const Socket::SocketProtocol m_socket_protocol;
69 const std::string m_socket_scheme;
70 std::recursive_mutex m_spawned_pids_mutex;
71 std::set<lldb::pid_t> m_spawned_pids;
Tamas Berghammere13c2732015-02-11 10:29:30 +000072
Kate Stoneb9c1b512016-09-06 20:57:50 +000073 PortMap m_port_map;
74 uint16_t m_port_offset;
75 struct {
76 lldb::pid_t pid;
77 uint16_t port;
78 std::string socket_name;
79 } m_pending_gdb_server;
Tamas Berghammere13c2732015-02-11 10:29:30 +000080
Kate Stoneb9c1b512016-09-06 20:57:50 +000081 PacketResult Handle_qLaunchGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000082
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 PacketResult Handle_qQueryGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 PacketResult Handle_qKillSpawnedProcess(StringExtractorGDBRemote &packet);
Pavel Labath6e4f19d2015-07-29 12:33:31 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 PacketResult Handle_qC(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 PacketResult Handle_jSignalsInfo(StringExtractorGDBRemote &packet);
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000096
Tamas Berghammere13c2732015-02-11 10:29:30 +000097private:
Kate Stoneb9c1b512016-09-06 20:57:50 +000098 bool KillSpawnedProcess(lldb::pid_t pid);
Pavel Labath6e4f19d2015-07-29 12:33:31 +000099
Kate Stoneb9c1b512016-09-06 20:57:50 +0000100 bool DebugserverProcessReaped(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000101
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 static const FileSpec &GetDomainSocketDir();
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 static FileSpec GetDomainSocketPath(const char *prefix);
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerPlatform);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000107};
108
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000109} // namespace process_gdb_remote
110} // namespace lldb_private
111
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000112#endif // liblldb_GDBRemoteCommunicationServerPlatform_h_