blob: 808299120c378c4cc3a5d58e4637b212816628f8 [file] [log] [blame]
Tamas Berghammere13c2732015-02-11 10:29:30 +00001//===-- GDBRemoteCommunicationServerPlatform.cpp ----------------*- 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#include "GDBRemoteCommunicationServerPlatform.h"
10
11#include <errno.h>
12
Tamas Berghammere13c2732015-02-11 10:29:30 +000013#include <chrono>
Pavel Labath6b3c8bb2018-04-05 16:23:54 +000014#include <csignal>
Kate Stoneb9c1b512016-09-06 20:57:50 +000015#include <cstring>
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +000016#include <mutex>
17#include <sstream>
Tamas Berghammere13c2732015-02-11 10:29:30 +000018
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +000019#include "llvm/Support/FileSystem.h"
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +000020#include "llvm/Support/Threading.h"
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +000021
Tamas Berghammere13c2732015-02-11 10:29:30 +000022#include "lldb/Host/Config.h"
23#include "lldb/Host/ConnectionFileDescriptor.h"
Pavel Labatheef758e2019-02-04 14:28:08 +000024#include "lldb/Host/FileAction.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000025#include "lldb/Host/Host.h"
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +000026#include "lldb/Host/HostInfo.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000027#include "lldb/Target/Platform.h"
28#include "lldb/Target/Process.h"
Chaoren Lin98d0a4b2015-07-14 01:09:28 +000029#include "lldb/Target/UnixSignals.h"
Tamas Berghammerccd6cff2015-12-08 14:08:19 +000030#include "lldb/Utility/JSON.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000031#include "lldb/Utility/Log.h"
Zachary Turnerfb1a0a02017-03-06 18:34:25 +000032#include "lldb/Utility/StreamGDBRemote.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000033#include "lldb/Utility/StreamString.h"
Pavel Labathf2a8bcc2017-06-27 10:45:31 +000034#include "lldb/Utility/StructuredData.h"
Pavel Labath5f7e5832017-02-10 12:21:22 +000035#include "lldb/Utility/UriParser.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000036
Pavel Labath9af71b32018-03-20 16:14:00 +000037#include "lldb/Utility/StringExtractorGDBRemote.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000038
39using namespace lldb;
40using namespace lldb_private;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000041using namespace lldb_private::process_gdb_remote;
Tamas Berghammere13c2732015-02-11 10:29:30 +000042
43//----------------------------------------------------------------------
44// GDBRemoteCommunicationServerPlatform constructor
45//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000046GDBRemoteCommunicationServerPlatform::GDBRemoteCommunicationServerPlatform(
47 const Socket::SocketProtocol socket_protocol, const char *socket_scheme)
48 : GDBRemoteCommunicationServerCommon("gdb-remote.server",
49 "gdb-remote.server.rx_packet"),
50 m_socket_protocol(socket_protocol), m_socket_scheme(socket_scheme),
51 m_spawned_pids_mutex(), m_port_map(), m_port_offset(0) {
52 m_pending_gdb_server.pid = LLDB_INVALID_PROCESS_ID;
53 m_pending_gdb_server.port = 0;
Tamas Berghammer372810f2015-12-08 14:27:40 +000054
Kate Stoneb9c1b512016-09-06 20:57:50 +000055 RegisterMemberFunctionHandler(
56 StringExtractorGDBRemote::eServerPacketType_qC,
57 &GDBRemoteCommunicationServerPlatform::Handle_qC);
58 RegisterMemberFunctionHandler(
59 StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir,
60 &GDBRemoteCommunicationServerPlatform::Handle_qGetWorkingDir);
61 RegisterMemberFunctionHandler(
62 StringExtractorGDBRemote::eServerPacketType_qLaunchGDBServer,
63 &GDBRemoteCommunicationServerPlatform::Handle_qLaunchGDBServer);
64 RegisterMemberFunctionHandler(
65 StringExtractorGDBRemote::eServerPacketType_qQueryGDBServer,
66 &GDBRemoteCommunicationServerPlatform::Handle_qQueryGDBServer);
67 RegisterMemberFunctionHandler(
68 StringExtractorGDBRemote::eServerPacketType_qKillSpawnedProcess,
69 &GDBRemoteCommunicationServerPlatform::Handle_qKillSpawnedProcess);
70 RegisterMemberFunctionHandler(
71 StringExtractorGDBRemote::eServerPacketType_qProcessInfo,
72 &GDBRemoteCommunicationServerPlatform::Handle_qProcessInfo);
73 RegisterMemberFunctionHandler(
74 StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir,
75 &GDBRemoteCommunicationServerPlatform::Handle_QSetWorkingDir);
76 RegisterMemberFunctionHandler(
77 StringExtractorGDBRemote::eServerPacketType_jSignalsInfo,
78 &GDBRemoteCommunicationServerPlatform::Handle_jSignalsInfo);
Tamas Berghammere13c2732015-02-11 10:29:30 +000079
Kate Stoneb9c1b512016-09-06 20:57:50 +000080 RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_interrupt,
Zachary Turner97206d52017-05-12 04:51:55 +000081 [](StringExtractorGDBRemote packet, Status &error,
Zachary Turner3bc714b2017-03-02 00:05:25 +000082 bool &interrupt, bool &quit) {
Kate Stoneb9c1b512016-09-06 20:57:50 +000083 error.SetErrorString("interrupt received");
84 interrupt = true;
85 return PacketResult::Success;
86 });
Tamas Berghammere13c2732015-02-11 10:29:30 +000087}
88
89//----------------------------------------------------------------------
90// Destructor
91//----------------------------------------------------------------------
Kate Stoneb9c1b512016-09-06 20:57:50 +000092GDBRemoteCommunicationServerPlatform::~GDBRemoteCommunicationServerPlatform() {}
Tamas Berghammere13c2732015-02-11 10:29:30 +000093
Zachary Turner97206d52017-05-12 04:51:55 +000094Status GDBRemoteCommunicationServerPlatform::LaunchGDBServer(
Kate Stoneb9c1b512016-09-06 20:57:50 +000095 const lldb_private::Args &args, std::string hostname, lldb::pid_t &pid,
96 uint16_t &port, std::string &socket_name) {
97 if (port == UINT16_MAX)
98 port = GetNextAvailablePort();
Tamas Berghammere13c2732015-02-11 10:29:30 +000099
Adrian Prantl05097242018-04-30 16:49:04 +0000100 // Spawn a new thread to accept the port that gets bound after binding to
101 // port 0 (zero).
Tamas Berghammere13c2732015-02-11 10:29:30 +0000102
Adrian Prantl05097242018-04-30 16:49:04 +0000103 // ignore the hostname send from the remote end, just use the ip address that
104 // we're currently communicating with as the hostname
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000105
Kate Stoneb9c1b512016-09-06 20:57:50 +0000106 // Spawn a debugserver and try to get the port it listens to.
107 ProcessLaunchInfo debugserver_launch_info;
108 if (hostname.empty())
109 hostname = "127.0.0.1";
Tamas Berghammere13c2732015-02-11 10:29:30 +0000110
Kate Stoneb9c1b512016-09-06 20:57:50 +0000111 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
112 if (log)
113 log->Printf("Launching debugserver with: %s:%u...", hostname.c_str(), port);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000114
Adrian Prantl05097242018-04-30 16:49:04 +0000115 // Do not run in a new session so that it can not linger after the platform
116 // closes.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000117 debugserver_launch_info.SetLaunchInSeparateProcessGroup(false);
118 debugserver_launch_info.SetMonitorProcessCallback(
119 std::bind(&GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped,
120 this, std::placeholders::_1),
121 false);
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000122
Zachary Turner245f7fd2016-11-17 01:38:02 +0000123 llvm::StringRef platform_scheme;
124 llvm::StringRef platform_ip;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000125 int platform_port;
Zachary Turner245f7fd2016-11-17 01:38:02 +0000126 llvm::StringRef platform_path;
Pavel Labathae7dd122017-10-27 04:53:24 +0000127 std::string platform_uri = GetConnection()->GetURI();
128 bool ok = UriParser::Parse(platform_uri, platform_scheme, platform_ip,
129 platform_port, platform_path);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 UNUSED_IF_ASSERT_DISABLED(ok);
131 assert(ok);
132
133 std::ostringstream url;
134// debugserver does not accept the URL scheme prefix.
Todd Fiala873a2ab2016-05-27 04:04:52 +0000135#if !defined(__APPLE__)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000136 url << m_socket_scheme << "://";
Todd Fiala873a2ab2016-05-27 04:04:52 +0000137#endif
Kate Stoneb9c1b512016-09-06 20:57:50 +0000138 uint16_t *port_ptr = &port;
139 if (m_socket_protocol == Socket::ProtocolTcp)
Zachary Turner245f7fd2016-11-17 01:38:02 +0000140 url << platform_ip.str() << ":" << port;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 else {
142 socket_name = GetDomainSocketPath("gdbserver").GetPath();
143 url << socket_name;
144 port_ptr = nullptr;
145 }
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000146
Zachary Turner97206d52017-05-12 04:51:55 +0000147 Status error = StartDebugserverProcess(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148 url.str().c_str(), nullptr, debugserver_launch_info, port_ptr, &args, -1);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000149
Kate Stoneb9c1b512016-09-06 20:57:50 +0000150 pid = debugserver_launch_info.GetProcessID();
151 if (pid != LLDB_INVALID_PROCESS_ID) {
Saleem Abdulrasool16ff8602016-05-18 01:59:10 +0000152 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 m_spawned_pids.insert(pid);
154 if (port > 0)
155 AssociatePortWithProcess(port, pid);
156 } else {
157 if (port > 0)
158 FreePort(port);
159 }
160 return error;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000161}
162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163GDBRemoteCommunication::PacketResult
164GDBRemoteCommunicationServerPlatform::Handle_qLaunchGDBServer(
165 StringExtractorGDBRemote &packet) {
Adrian Prantl05097242018-04-30 16:49:04 +0000166 // Spawn a local debugserver as a platform so we can then attach or launch a
167 // process...
Tamas Berghammere13c2732015-02-11 10:29:30 +0000168
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
170 if (log)
171 log->Printf("GDBRemoteCommunicationServerPlatform::%s() called",
172 __FUNCTION__);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000173
Kate Stoneb9c1b512016-09-06 20:57:50 +0000174 ConnectionFileDescriptor file_conn;
175 std::string hostname;
176 packet.SetFilePos(::strlen("qLaunchGDBServer;"));
177 llvm::StringRef name;
178 llvm::StringRef value;
179 uint16_t port = UINT16_MAX;
180 while (packet.GetNameColonValue(name, value)) {
181 if (name.equals("host"))
182 hostname = value;
183 else if (name.equals("port"))
184 value.getAsInteger(0, port);
185 }
186
187 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
188 std::string socket_name;
Zachary Turner97206d52017-05-12 04:51:55 +0000189 Status error =
Kate Stoneb9c1b512016-09-06 20:57:50 +0000190 LaunchGDBServer(Args(), hostname, debugserver_pid, port, socket_name);
191 if (error.Fail()) {
192 if (log)
193 log->Printf("GDBRemoteCommunicationServerPlatform::%s() debugserver "
194 "launch failed: %s",
195 __FUNCTION__, error.AsCString());
196 return SendErrorResponse(9);
197 }
198
199 if (log)
200 log->Printf("GDBRemoteCommunicationServerPlatform::%s() debugserver "
201 "launched successfully as pid %" PRIu64,
202 __FUNCTION__, debugserver_pid);
203
204 StreamGDBRemote response;
205 response.Printf("pid:%" PRIu64 ";port:%u;", debugserver_pid,
206 port + m_port_offset);
207 if (!socket_name.empty()) {
208 response.PutCString("socket_name:");
Pavel Labath7f815a92019-02-12 14:28:55 +0000209 response.PutStringAsRawHex8(socket_name);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 response.PutChar(';');
211 }
212
213 PacketResult packet_result = SendPacketNoLock(response.GetString());
214 if (packet_result != PacketResult::Success) {
215 if (debugserver_pid != LLDB_INVALID_PROCESS_ID)
Aaron Smithe55850b2019-01-10 00:46:09 +0000216 Host::Kill(debugserver_pid, SIGINT);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217 }
218 return packet_result;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219}
220
221GDBRemoteCommunication::PacketResult
222GDBRemoteCommunicationServerPlatform::Handle_qQueryGDBServer(
223 StringExtractorGDBRemote &packet) {
224 if (m_pending_gdb_server.pid == LLDB_INVALID_PROCESS_ID)
225 return SendErrorResponse(4);
226
227 JSONObject::SP server_sp = std::make_shared<JSONObject>();
228 server_sp->SetObject("port",
229 std::make_shared<JSONNumber>(m_pending_gdb_server.port));
230 if (!m_pending_gdb_server.socket_name.empty())
231 server_sp->SetObject(
232 "socket_name",
233 std::make_shared<JSONString>(m_pending_gdb_server.socket_name.c_str()));
234
235 JSONArray server_list;
236 server_list.AppendObject(server_sp);
237
238 StreamGDBRemote response;
239 server_list.Write(response);
240
241 StreamGDBRemote escaped_response;
Zachary Turnerc1564272016-11-16 21:15:24 +0000242 escaped_response.PutEscapedBytes(response.GetString().data(),
243 response.GetSize());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 return SendPacketNoLock(escaped_response.GetString());
245}
246
247GDBRemoteCommunication::PacketResult
248GDBRemoteCommunicationServerPlatform::Handle_qKillSpawnedProcess(
249 StringExtractorGDBRemote &packet) {
250 packet.SetFilePos(::strlen("qKillSpawnedProcess:"));
251
252 lldb::pid_t pid = packet.GetU64(LLDB_INVALID_PROCESS_ID);
253
Adrian Prantl05097242018-04-30 16:49:04 +0000254 // verify that we know anything about this pid. Scope for locker
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 {
256 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
257 if (m_spawned_pids.find(pid) == m_spawned_pids.end()) {
258 // not a pid we know about
259 return SendErrorResponse(10);
Tamas Berghammere13c2732015-02-11 10:29:30 +0000260 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000262
Kate Stoneb9c1b512016-09-06 20:57:50 +0000263 // go ahead and attempt to kill the spawned process
264 if (KillSpawnedProcess(pid))
265 return SendOKResponse();
266 else
267 return SendErrorResponse(11);
268}
Tamas Berghammere13c2732015-02-11 10:29:30 +0000269
Kate Stoneb9c1b512016-09-06 20:57:50 +0000270bool GDBRemoteCommunicationServerPlatform::KillSpawnedProcess(lldb::pid_t pid) {
271 // make sure we know about this process
272 {
273 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
274 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
275 return false;
276 }
277
278 // first try a SIGTERM (standard kill)
279 Host::Kill(pid, SIGTERM);
280
281 // check if that worked
282 for (size_t i = 0; i < 10; ++i) {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000283 {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000284 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
285 if (m_spawned_pids.find(pid) == m_spawned_pids.end()) {
286 // it is now killed
287 return true;
288 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000289 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000290 usleep(10000);
291 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000292
Kate Stoneb9c1b512016-09-06 20:57:50 +0000293 // check one more time after the final usleep
294 {
295 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
296 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
297 return true;
298 }
299
Adrian Prantl05097242018-04-30 16:49:04 +0000300 // the launched process still lives. Now try killing it again, this time
301 // with an unblockable signal.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000302 Host::Kill(pid, SIGKILL);
303
304 for (size_t i = 0; i < 10; ++i) {
305 {
306 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
307 if (m_spawned_pids.find(pid) == m_spawned_pids.end()) {
308 // it is now killed
309 return true;
310 }
311 }
312 usleep(10000);
313 }
314
Adrian Prantl05097242018-04-30 16:49:04 +0000315 // check one more time after the final usleep Scope for locker
Kate Stoneb9c1b512016-09-06 20:57:50 +0000316 {
317 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
318 if (m_spawned_pids.find(pid) == m_spawned_pids.end())
319 return true;
320 }
321
322 // no luck - the process still lives
323 return false;
324}
325
326GDBRemoteCommunication::PacketResult
327GDBRemoteCommunicationServerPlatform::Handle_qProcessInfo(
328 StringExtractorGDBRemote &packet) {
329 lldb::pid_t pid = m_process_launch_info.GetProcessID();
330 m_process_launch_info.Clear();
331
332 if (pid == LLDB_INVALID_PROCESS_ID)
333 return SendErrorResponse(1);
334
335 ProcessInstanceInfo proc_info;
336 if (!Host::GetProcessInfo(pid, proc_info))
337 return SendErrorResponse(1);
338
339 StreamString response;
340 CreateProcessInfoResponse_DebugServerStyle(proc_info, response);
341 return SendPacketNoLock(response.GetString());
342}
343
344GDBRemoteCommunication::PacketResult
345GDBRemoteCommunicationServerPlatform::Handle_qGetWorkingDir(
346 StringExtractorGDBRemote &packet) {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000347
Pavel Labath1d5855b2017-01-23 15:56:45 +0000348 llvm::SmallString<64> cwd;
349 if (std::error_code ec = llvm::sys::fs::current_path(cwd))
350 return SendErrorResponse(ec.value());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000351
352 StreamString response;
Pavel Labath1d5855b2017-01-23 15:56:45 +0000353 response.PutBytesAsRawHex8(cwd.data(), cwd.size());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000354 return SendPacketNoLock(response.GetString());
355}
356
357GDBRemoteCommunication::PacketResult
358GDBRemoteCommunicationServerPlatform::Handle_QSetWorkingDir(
359 StringExtractorGDBRemote &packet) {
360 packet.SetFilePos(::strlen("QSetWorkingDir:"));
361 std::string path;
362 packet.GetHexByteString(path);
363
Pavel Labath2d0c5b02017-01-25 11:10:52 +0000364 if (std::error_code ec = llvm::sys::fs::set_current_path(path))
365 return SendErrorResponse(ec.value());
Kate Stoneb9c1b512016-09-06 20:57:50 +0000366 return SendOKResponse();
367}
368
369GDBRemoteCommunication::PacketResult
370GDBRemoteCommunicationServerPlatform::Handle_qC(
371 StringExtractorGDBRemote &packet) {
372 // NOTE: lldb should now be using qProcessInfo for process IDs. This path
373 // here
374 // should not be used. It is reporting process id instead of thread id. The
375 // correct answer doesn't seem to make much sense for lldb-platform.
376 // CONSIDER: flip to "unsupported".
377 lldb::pid_t pid = m_process_launch_info.GetProcessID();
378
379 StreamString response;
380 response.Printf("QC%" PRIx64, pid);
381
Adrian Prantl05097242018-04-30 16:49:04 +0000382 // If we launch a process and this GDB server is acting as a platform, then
383 // we need to clear the process launch state so we can start launching
384 // another process. In order to launch a process a bunch or packets need to
385 // be sent: environment packets, working directory, disable ASLR, and many
386 // more settings. When we launch a process we then need to know when to clear
387 // this information. Currently we are selecting the 'qC' packet as that
388 // packet which seems to make the most sense.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 if (pid != LLDB_INVALID_PROCESS_ID) {
390 m_process_launch_info.Clear();
391 }
392
393 return SendPacketNoLock(response.GetString());
394}
395
396GDBRemoteCommunication::PacketResult
397GDBRemoteCommunicationServerPlatform::Handle_jSignalsInfo(
398 StringExtractorGDBRemote &packet) {
399 StructuredData::Array signal_array;
400
401 const auto &signals = Host::GetUnixSignals();
402 for (auto signo = signals->GetFirstSignalNumber();
403 signo != LLDB_INVALID_SIGNAL_NUMBER;
404 signo = signals->GetNextSignalNumber(signo)) {
405 auto dictionary = std::make_shared<StructuredData::Dictionary>();
406
407 dictionary->AddIntegerItem("signo", signo);
408 dictionary->AddStringItem("name", signals->GetSignalAsCString(signo));
409
410 bool suppress, stop, notify;
411 signals->GetSignalInfo(signo, suppress, stop, notify);
412 dictionary->AddBooleanItem("suppress", suppress);
413 dictionary->AddBooleanItem("stop", stop);
414 dictionary->AddBooleanItem("notify", notify);
415
416 signal_array.Push(dictionary);
417 }
418
419 StreamString response;
420 signal_array.Dump(response);
421 return SendPacketNoLock(response.GetString());
422}
423
424bool GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped(
425 lldb::pid_t pid) {
426 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
427 FreePortForProcess(pid);
428 m_spawned_pids.erase(pid);
429 return true;
430}
431
Zachary Turner97206d52017-05-12 04:51:55 +0000432Status GDBRemoteCommunicationServerPlatform::LaunchProcess() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000433 if (!m_process_launch_info.GetArguments().GetArgumentCount())
Zachary Turner97206d52017-05-12 04:51:55 +0000434 return Status("%s: no process command line specified to launch",
435 __FUNCTION__);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000436
Adrian Prantl05097242018-04-30 16:49:04 +0000437 // specify the process monitor if not already set. This should generally be
438 // what happens since we need to reap started processes.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000439 if (!m_process_launch_info.GetMonitorProcessCallback())
440 m_process_launch_info.SetMonitorProcessCallback(
441 std::bind(
442 &GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped,
443 this, std::placeholders::_1),
444 false);
445
Zachary Turner97206d52017-05-12 04:51:55 +0000446 Status error = Host::LaunchProcess(m_process_launch_info);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000447 if (!error.Success()) {
448 fprintf(stderr, "%s: failed to launch executable %s", __FUNCTION__,
449 m_process_launch_info.GetArguments().GetArgumentAtIndex(0));
Tamas Berghammere13c2732015-02-11 10:29:30 +0000450 return error;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000451 }
452
453 printf("Launched '%s' as process %" PRIu64 "...\n",
454 m_process_launch_info.GetArguments().GetArgumentAtIndex(0),
455 m_process_launch_info.GetProcessID());
456
Adrian Prantl05097242018-04-30 16:49:04 +0000457 // add to list of spawned processes. On an lldb-gdbserver, we would expect
458 // there to be only one.
Kate Stoneb9c1b512016-09-06 20:57:50 +0000459 const auto pid = m_process_launch_info.GetProcessID();
460 if (pid != LLDB_INVALID_PROCESS_ID) {
461 // add to spawned pids
462 std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
463 m_spawned_pids.insert(pid);
464 }
465
466 return error;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000467}
468
Kate Stoneb9c1b512016-09-06 20:57:50 +0000469void GDBRemoteCommunicationServerPlatform::SetPortMap(PortMap &&port_map) {
470 m_port_map = port_map;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000471}
472
Kate Stoneb9c1b512016-09-06 20:57:50 +0000473uint16_t GDBRemoteCommunicationServerPlatform::GetNextAvailablePort() {
474 if (m_port_map.empty())
475 return 0; // Bind to port zero and get a port, we didn't have any
476 // limitations
Tamas Berghammere13c2732015-02-11 10:29:30 +0000477
Kate Stoneb9c1b512016-09-06 20:57:50 +0000478 for (auto &pair : m_port_map) {
479 if (pair.second == LLDB_INVALID_PROCESS_ID) {
480 pair.second = ~(lldb::pid_t)LLDB_INVALID_PROCESS_ID;
481 return pair.first;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000482 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000483 }
484 return UINT16_MAX;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000485}
486
Kate Stoneb9c1b512016-09-06 20:57:50 +0000487bool GDBRemoteCommunicationServerPlatform::AssociatePortWithProcess(
488 uint16_t port, lldb::pid_t pid) {
489 PortMap::iterator pos = m_port_map.find(port);
490 if (pos != m_port_map.end()) {
491 pos->second = pid;
492 return true;
493 }
494 return false;
495}
496
497bool GDBRemoteCommunicationServerPlatform::FreePort(uint16_t port) {
498 PortMap::iterator pos = m_port_map.find(port);
499 if (pos != m_port_map.end()) {
500 pos->second = LLDB_INVALID_PROCESS_ID;
501 return true;
502 }
503 return false;
504}
505
506bool GDBRemoteCommunicationServerPlatform::FreePortForProcess(lldb::pid_t pid) {
507 if (!m_port_map.empty()) {
508 for (auto &pair : m_port_map) {
509 if (pair.second == pid) {
510 pair.second = LLDB_INVALID_PROCESS_ID;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000511 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000512 }
Tamas Berghammere13c2732015-02-11 10:29:30 +0000513 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000514 }
515 return false;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000516}
517
Kate Stoneb9c1b512016-09-06 20:57:50 +0000518const FileSpec &GDBRemoteCommunicationServerPlatform::GetDomainSocketDir() {
519 static FileSpec g_domainsocket_dir;
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000520 static llvm::once_flag g_once_flag;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000521
Kamil Rytarowskic5f28e22017-02-06 17:55:02 +0000522 llvm::call_once(g_once_flag, []() {
Kate Stoneb9c1b512016-09-06 20:57:50 +0000523 const char *domainsocket_dir_env =
524 ::getenv("LLDB_DEBUGSERVER_DOMAINSOCKET_DIR");
525 if (domainsocket_dir_env != nullptr)
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000526 g_domainsocket_dir = FileSpec(domainsocket_dir_env);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000527 else
Pavel Labath60f028f2018-06-19 15:09:07 +0000528 g_domainsocket_dir = HostInfo::GetProcessTempDir();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000529 });
Tamas Berghammere13c2732015-02-11 10:29:30 +0000530
Kate Stoneb9c1b512016-09-06 20:57:50 +0000531 return g_domainsocket_dir;
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000532}
533
534FileSpec
Kate Stoneb9c1b512016-09-06 20:57:50 +0000535GDBRemoteCommunicationServerPlatform::GetDomainSocketPath(const char *prefix) {
Stella Stamenovab3f44ad2018-12-10 17:23:28 +0000536 llvm::SmallString<128> socket_path;
537 llvm::SmallString<128> socket_name(
Kate Stoneb9c1b512016-09-06 20:57:50 +0000538 (llvm::StringRef(prefix) + ".%%%%%%").str());
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000539
Kate Stoneb9c1b512016-09-06 20:57:50 +0000540 FileSpec socket_path_spec(GetDomainSocketDir());
541 socket_path_spec.AppendPathComponent(socket_name.c_str());
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000542
Kate Stoneb9c1b512016-09-06 20:57:50 +0000543 llvm::sys::fs::createUniqueFile(socket_path_spec.GetCString(), socket_path);
Jonas Devlieghere8f3be7a2018-11-01 21:05:36 +0000544 return FileSpec(socket_path.c_str());
Oleksiy Vyalov9fe526c2015-10-21 19:34:26 +0000545}
546
Kate Stoneb9c1b512016-09-06 20:57:50 +0000547void GDBRemoteCommunicationServerPlatform::SetPortOffset(uint16_t port_offset) {
548 m_port_offset = port_offset;
Tamas Berghammere13c2732015-02-11 10:29:30 +0000549}
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000550
Kate Stoneb9c1b512016-09-06 20:57:50 +0000551void GDBRemoteCommunicationServerPlatform::SetPendingGdbServer(
552 lldb::pid_t pid, uint16_t port, const std::string &socket_name) {
553 m_pending_gdb_server.pid = pid;
554 m_pending_gdb_server.port = port;
555 m_pending_gdb_server.socket_name = socket_name;
Tamas Berghammerccd6cff2015-12-08 14:08:19 +0000556}