blob: aed5106272d1bb4dcaa13ad6eab37e1f58d7d3a8 [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// C Includes
14// C++ Includes
15#include <map>
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +000016#include <mutex>
Pavel Labath6e4f19d2015-07-29 12:33:31 +000017#include <set>
18
Eugene Zelenkoedb35d92015-10-24 01:08:35 +000019// Other libraries and framework includes
20// Project includes
21#include "GDBRemoteCommunicationServerCommon.h"
22#include "lldb/Host/Socket.h"
23
Tamas Berghammerdb264a62015-03-31 09:52:22 +000024namespace lldb_private {
25namespace process_gdb_remote {
26
Kate Stoneb9c1b512016-09-06 20:57:50 +000027class GDBRemoteCommunicationServerPlatform
28 : public GDBRemoteCommunicationServerCommon {
Tamas Berghammere13c2732015-02-11 10:29:30 +000029public:
Kate Stoneb9c1b512016-09-06 20:57:50 +000030 typedef std::map<uint16_t, lldb::pid_t> PortMap;
Tamas Berghammere13c2732015-02-11 10:29:30 +000031
Kate Stoneb9c1b512016-09-06 20:57:50 +000032 GDBRemoteCommunicationServerPlatform(
33 const Socket::SocketProtocol socket_protocol, const char *socket_scheme);
Tamas Berghammere13c2732015-02-11 10:29:30 +000034
Kate Stoneb9c1b512016-09-06 20:57:50 +000035 ~GDBRemoteCommunicationServerPlatform() override;
Tamas Berghammere13c2732015-02-11 10:29:30 +000036
Zachary Turner97206d52017-05-12 04:51:55 +000037 Status LaunchProcess() override;
Tamas Berghammere13c2732015-02-11 10:29:30 +000038
Kate Stoneb9c1b512016-09-06 20:57:50 +000039 // Set both ports to zero to let the platform automatically bind to
40 // a port chosen by the OS.
41 void SetPortMap(PortMap &&port_map);
Tamas Berghammere13c2732015-02-11 10:29:30 +000042
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 //----------------------------------------------------------------------
44 // 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 UINT16_MAX
48 //
49 // If we aren't using a port map, return 0 to indicate we should bind to
50 // port 0 and then figure out which port we used.
51 //----------------------------------------------------------------------
52 uint16_t GetNextAvailablePort();
Tamas Berghammere13c2732015-02-11 10:29:30 +000053
Kate Stoneb9c1b512016-09-06 20:57:50 +000054 bool AssociatePortWithProcess(uint16_t port, lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000055
Kate Stoneb9c1b512016-09-06 20:57:50 +000056 bool FreePort(uint16_t port);
Tamas Berghammere13c2732015-02-11 10:29:30 +000057
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 bool FreePortForProcess(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +000059
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 void SetPortOffset(uint16_t port_offset);
Tamas Berghammere13c2732015-02-11 10:29:30 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 void SetInferiorArguments(const lldb_private::Args &args);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000063
Zachary Turner97206d52017-05-12 04:51:55 +000064 Status LaunchGDBServer(const lldb_private::Args &args, std::string hostname,
65 lldb::pid_t &pid, uint16_t &port,
66 std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000067
Kate Stoneb9c1b512016-09-06 20:57:50 +000068 void SetPendingGdbServer(lldb::pid_t pid, uint16_t port,
69 const std::string &socket_name);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000070
Tamas Berghammere13c2732015-02-11 10:29:30 +000071protected:
Kate Stoneb9c1b512016-09-06 20:57:50 +000072 const Socket::SocketProtocol m_socket_protocol;
73 const std::string m_socket_scheme;
74 std::recursive_mutex m_spawned_pids_mutex;
75 std::set<lldb::pid_t> m_spawned_pids;
Tamas Berghammere13c2732015-02-11 10:29:30 +000076
Kate Stoneb9c1b512016-09-06 20:57:50 +000077 PortMap m_port_map;
78 uint16_t m_port_offset;
79 struct {
80 lldb::pid_t pid;
81 uint16_t port;
82 std::string socket_name;
83 } m_pending_gdb_server;
Tamas Berghammere13c2732015-02-11 10:29:30 +000084
Kate Stoneb9c1b512016-09-06 20:57:50 +000085 PacketResult Handle_qLaunchGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000086
Kate Stoneb9c1b512016-09-06 20:57:50 +000087 PacketResult Handle_qQueryGDBServer(StringExtractorGDBRemote &packet);
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000088
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 PacketResult Handle_qKillSpawnedProcess(StringExtractorGDBRemote &packet);
Pavel Labath6e4f19d2015-07-29 12:33:31 +000090
Kate Stoneb9c1b512016-09-06 20:57:50 +000091 PacketResult Handle_qProcessInfo(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000092
Kate Stoneb9c1b512016-09-06 20:57:50 +000093 PacketResult Handle_qGetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000094
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 PacketResult Handle_QSetWorkingDir(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000096
Kate Stoneb9c1b512016-09-06 20:57:50 +000097 PacketResult Handle_qC(StringExtractorGDBRemote &packet);
Tamas Berghammere13c2732015-02-11 10:29:30 +000098
Kate Stoneb9c1b512016-09-06 20:57:50 +000099 PacketResult Handle_jSignalsInfo(StringExtractorGDBRemote &packet);
Chaoren Lin98d0a4b2015-07-14 01:09:28 +0000100
Tamas Berghammere13c2732015-02-11 10:29:30 +0000101private:
Kate Stoneb9c1b512016-09-06 20:57:50 +0000102 bool KillSpawnedProcess(lldb::pid_t pid);
Pavel Labath6e4f19d2015-07-29 12:33:31 +0000103
Kate Stoneb9c1b512016-09-06 20:57:50 +0000104 bool DebugserverProcessReaped(lldb::pid_t pid);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 static const FileSpec &GetDomainSocketDir();
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000107
Kate Stoneb9c1b512016-09-06 20:57:50 +0000108 static FileSpec GetDomainSocketPath(const char *prefix);
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000109
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110 DISALLOW_COPY_AND_ASSIGN(GDBRemoteCommunicationServerPlatform);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000111};
112
Tamas Berghammerdb264a62015-03-31 09:52:22 +0000113} // namespace process_gdb_remote
114} // namespace lldb_private
115
Eugene Zelenkoedb35d92015-10-24 01:08:35 +0000116#endif // liblldb_GDBRemoteCommunicationServerPlatform_h_