]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / source / Plugins / Process / gdb-remote / GDBRemoteCommunicationServerPlatform.cpp
1 //===-- GDBRemoteCommunicationServerPlatform.cpp ----------------*- 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 #include "GDBRemoteCommunicationServerPlatform.h"
11
12 #include <errno.h>
13
14 // C Includes
15 // C++ Includes
16 #include <cstring>
17 #include <chrono>
18 #include <mutex>
19 #include <sstream>
20
21 // Other libraries and framework includes
22 #include "llvm/Support/FileSystem.h"
23
24 #include "lldb/Core/Log.h"
25 #include "lldb/Core/StreamGDBRemote.h"
26 #include "lldb/Core/StreamString.h"
27 #include "lldb/Core/StructuredData.h"
28 #include "lldb/Host/Config.h"
29 #include "lldb/Host/ConnectionFileDescriptor.h"
30 #include "lldb/Host/Host.h"
31 #include "lldb/Host/HostInfo.h"
32 #include "lldb/Host/StringConvert.h"
33 #include "lldb/Target/FileAction.h"
34 #include "lldb/Target/Platform.h"
35 #include "lldb/Target/Process.h"
36 #include "lldb/Target/UnixSignals.h"
37 #include "lldb/Utility/JSON.h"
38
39 // Project includes
40 #include "Utility/StringExtractorGDBRemote.h"
41 #include "Utility/UriParser.h"
42
43 using namespace lldb;
44 using namespace lldb_private;
45 using namespace lldb_private::process_gdb_remote;
46
47 //----------------------------------------------------------------------
48 // GDBRemoteCommunicationServerPlatform constructor
49 //----------------------------------------------------------------------
50 GDBRemoteCommunicationServerPlatform::GDBRemoteCommunicationServerPlatform(const Socket::SocketProtocol socket_protocol,
51                                                                            const char *socket_scheme)
52     : GDBRemoteCommunicationServerCommon("gdb-remote.server", "gdb-remote.server.rx_packet"),
53       m_socket_protocol(socket_protocol),
54       m_socket_scheme(socket_scheme),
55       m_spawned_pids_mutex(),
56       m_port_map(),
57       m_port_offset(0)
58 {
59     m_pending_gdb_server.pid = LLDB_INVALID_PROCESS_ID;
60     m_pending_gdb_server.port = 0;
61
62     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qC,
63                                   &GDBRemoteCommunicationServerPlatform::Handle_qC);
64     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir,
65                                   &GDBRemoteCommunicationServerPlatform::Handle_qGetWorkingDir);
66     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qLaunchGDBServer,
67                                   &GDBRemoteCommunicationServerPlatform::Handle_qLaunchGDBServer);
68     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qQueryGDBServer,
69                                   &GDBRemoteCommunicationServerPlatform::Handle_qQueryGDBServer);
70     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qKillSpawnedProcess,
71                                   &GDBRemoteCommunicationServerPlatform::Handle_qKillSpawnedProcess);
72     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qProcessInfo,
73                                   &GDBRemoteCommunicationServerPlatform::Handle_qProcessInfo);
74     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir,
75                                   &GDBRemoteCommunicationServerPlatform::Handle_QSetWorkingDir);
76     RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_jSignalsInfo,
77                                   &GDBRemoteCommunicationServerPlatform::Handle_jSignalsInfo);
78
79     RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_interrupt,
80                           [this](StringExtractorGDBRemote packet, Error &error, bool &interrupt, bool &quit) {
81                               error.SetErrorString("interrupt received");
82                               interrupt = true;
83                               return PacketResult::Success;
84                           });
85 }
86
87 //----------------------------------------------------------------------
88 // Destructor
89 //----------------------------------------------------------------------
90 GDBRemoteCommunicationServerPlatform::~GDBRemoteCommunicationServerPlatform()
91 {
92 }
93
94 Error
95 GDBRemoteCommunicationServerPlatform::LaunchGDBServer(const lldb_private::Args& args,
96                                                       std::string hostname,
97                                                       lldb::pid_t& pid,
98                                                       uint16_t& port,
99                                                       std::string& socket_name)
100 {
101     if (port == UINT16_MAX)
102         port = GetNextAvailablePort();
103     
104     // Spawn a new thread to accept the port that gets bound after
105     // binding to port 0 (zero).
106
107     // ignore the hostname send from the remote end, just use the ip address
108     // that we're currently communicating with as the hostname
109
110     // Spawn a debugserver and try to get the port it listens to.
111     ProcessLaunchInfo debugserver_launch_info;
112     if (hostname.empty())
113         hostname = "127.0.0.1";
114
115     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
116     if (log)
117         log->Printf("Launching debugserver with: %s:%u...", hostname.c_str(), port);
118
119     // Do not run in a new session so that it can not linger after the
120     // platform closes.
121     debugserver_launch_info.SetLaunchInSeparateProcessGroup(false);
122     debugserver_launch_info.SetMonitorProcessCallback(
123         std::bind(&GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped, this, std::placeholders::_1), false);
124
125     std::string platform_scheme;
126     std::string platform_ip;
127     int platform_port;
128     std::string platform_path;
129     bool ok = UriParser::Parse(GetConnection()->GetURI().c_str(), platform_scheme, platform_ip, platform_port, platform_path);
130     UNUSED_IF_ASSERT_DISABLED(ok);
131     assert(ok);
132
133     std::ostringstream url;
134     // debugserver does not accept the URL scheme prefix.
135 #if !defined(__APPLE__)
136     url << m_socket_scheme << "://";
137 #endif
138     uint16_t* port_ptr = &port;
139     if (m_socket_protocol == Socket::ProtocolTcp)
140         url << platform_ip << ":" << port;
141     else
142     {
143         socket_name = GetDomainSocketPath("gdbserver").GetPath();
144         url << socket_name;
145         port_ptr = nullptr;
146     }
147
148     Error error = StartDebugserverProcess (url.str().c_str(),
149                                            nullptr,
150                                            debugserver_launch_info,
151                                            port_ptr,
152                                            args);
153
154     pid = debugserver_launch_info.GetProcessID();
155     if (pid != LLDB_INVALID_PROCESS_ID)
156     {
157         std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
158         m_spawned_pids.insert(pid);
159         if (port > 0)
160             AssociatePortWithProcess(port, pid);
161     }
162     else
163     {
164         if (port > 0)
165             FreePort(port);
166     }
167     return error;
168 }
169
170 GDBRemoteCommunication::PacketResult
171 GDBRemoteCommunicationServerPlatform::Handle_qLaunchGDBServer (StringExtractorGDBRemote &packet)
172 {
173 #ifdef _WIN32
174     return SendErrorResponse(9);
175 #else
176     // Spawn a local debugserver as a platform so we can then attach or launch
177     // a process...
178
179     Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM));
180     if (log)
181         log->Printf ("GDBRemoteCommunicationServerPlatform::%s() called", __FUNCTION__);
182
183     ConnectionFileDescriptor file_conn;
184     std::string hostname;
185     packet.SetFilePos(::strlen ("qLaunchGDBServer;"));
186     std::string name;
187     std::string value;
188     uint16_t port = UINT16_MAX;
189     while (packet.GetNameColonValue(name, value))
190     {
191         if (name.compare ("host") == 0)
192             hostname.swap(value);
193         else if (name.compare ("port") == 0)
194             port = StringConvert::ToUInt32(value.c_str(), 0, 0);
195     }
196
197     lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
198     std::string socket_name;
199     Error error = LaunchGDBServer(Args(), hostname, debugserver_pid, port, socket_name);
200     if (error.Fail())
201     {
202         if (log)
203             log->Printf("GDBRemoteCommunicationServerPlatform::%s() debugserver launch failed: %s", __FUNCTION__, error.AsCString ());
204         return SendErrorResponse(9);
205     }
206
207     if (log)
208         log->Printf ("GDBRemoteCommunicationServerPlatform::%s() debugserver launched successfully as pid %" PRIu64, __FUNCTION__, debugserver_pid);
209
210     StreamGDBRemote response;
211     response.Printf("pid:%" PRIu64 ";port:%u;", debugserver_pid, port + m_port_offset);
212     if (!socket_name.empty())
213     {
214         response.PutCString("socket_name:");
215         response.PutCStringAsRawHex8(socket_name.c_str());
216         response.PutChar(';');
217     }
218
219     PacketResult packet_result = SendPacketNoLock(response.GetData(), response.GetSize());
220     if (packet_result != PacketResult::Success)
221     {
222         if (debugserver_pid != LLDB_INVALID_PROCESS_ID)
223             ::kill (debugserver_pid, SIGINT);
224     }
225     return packet_result;
226 #endif
227 }
228
229 GDBRemoteCommunication::PacketResult
230 GDBRemoteCommunicationServerPlatform::Handle_qQueryGDBServer (StringExtractorGDBRemote &packet)
231 {
232     if (m_pending_gdb_server.pid == LLDB_INVALID_PROCESS_ID)
233         return SendErrorResponse(4);
234
235     JSONObject::SP server_sp = std::make_shared<JSONObject>();
236     server_sp->SetObject("port", std::make_shared<JSONNumber>(m_pending_gdb_server.port));
237     if (!m_pending_gdb_server.socket_name.empty())
238         server_sp->SetObject("socket_name",
239                              std::make_shared<JSONString>(m_pending_gdb_server.socket_name.c_str()));
240
241     JSONArray server_list;
242     server_list.AppendObject(server_sp);
243
244     StreamGDBRemote response;
245     server_list.Write(response);
246
247     StreamGDBRemote escaped_response;
248     escaped_response.PutEscapedBytes(response.GetData(), response.GetSize());
249     return SendPacketNoLock(escaped_response.GetData(), escaped_response.GetSize());
250 }
251
252 GDBRemoteCommunication::PacketResult
253 GDBRemoteCommunicationServerPlatform::Handle_qKillSpawnedProcess (StringExtractorGDBRemote &packet)
254 {
255     packet.SetFilePos(::strlen ("qKillSpawnedProcess:"));
256
257     lldb::pid_t pid = packet.GetU64(LLDB_INVALID_PROCESS_ID);
258
259     // verify that we know anything about this pid.
260     // Scope for locker
261     {
262         std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
263         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
264         {
265             // not a pid we know about
266             return SendErrorResponse (10);
267         }
268     }
269
270     // go ahead and attempt to kill the spawned process
271     if (KillSpawnedProcess (pid))
272         return SendOKResponse ();
273     else
274         return SendErrorResponse (11);
275 }
276
277 bool
278 GDBRemoteCommunicationServerPlatform::KillSpawnedProcess (lldb::pid_t pid)
279 {
280     // make sure we know about this process
281     {
282         std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
283         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
284             return false;
285     }
286
287     // first try a SIGTERM (standard kill)
288     Host::Kill (pid, SIGTERM);
289
290     // check if that worked
291     for (size_t i=0; i<10; ++i)
292     {
293         {
294             std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
295             if (m_spawned_pids.find(pid) == m_spawned_pids.end())
296             {
297                 // it is now killed
298                 return true;
299             }
300         }
301         usleep (10000);
302     }
303
304     // check one more time after the final usleep
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             return true;
309     }
310
311     // the launched process still lives.  Now try killing it again,
312     // this time with an unblockable signal.
313     Host::Kill (pid, SIGKILL);
314
315     for (size_t i=0; i<10; ++i)
316     {
317         {
318             std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
319             if (m_spawned_pids.find(pid) == m_spawned_pids.end())
320             {
321                 // it is now killed
322                 return true;
323             }
324         }
325         usleep (10000);
326     }
327
328     // check one more time after the final usleep
329     // Scope for locker
330     {
331         std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
332         if (m_spawned_pids.find(pid) == m_spawned_pids.end())
333             return true;
334     }
335
336     // no luck - the process still lives
337     return false;
338 }
339
340 GDBRemoteCommunication::PacketResult
341 GDBRemoteCommunicationServerPlatform::Handle_qProcessInfo (StringExtractorGDBRemote &packet)
342 {
343     lldb::pid_t pid = m_process_launch_info.GetProcessID ();
344     m_process_launch_info.Clear ();
345
346     if (pid == LLDB_INVALID_PROCESS_ID)
347         return SendErrorResponse (1);
348
349     ProcessInstanceInfo proc_info;
350     if (!Host::GetProcessInfo (pid, proc_info))
351         return SendErrorResponse (1);
352
353     StreamString response;
354     CreateProcessInfoResponse_DebugServerStyle(proc_info, response);
355     return SendPacketNoLock (response.GetData (), response.GetSize ());
356 }
357
358 GDBRemoteCommunication::PacketResult
359 GDBRemoteCommunicationServerPlatform::Handle_qGetWorkingDir (StringExtractorGDBRemote &packet)
360 {
361     // If this packet is sent to a platform, then change the current working directory
362
363     char cwd[PATH_MAX];
364     if (getcwd(cwd, sizeof(cwd)) == NULL)
365         return SendErrorResponse(errno);
366
367     StreamString response;
368     response.PutBytesAsRawHex8(cwd, strlen(cwd));
369     return SendPacketNoLock(response.GetData(), response.GetSize());
370 }
371
372 GDBRemoteCommunication::PacketResult
373 GDBRemoteCommunicationServerPlatform::Handle_QSetWorkingDir (StringExtractorGDBRemote &packet)
374 {
375     packet.SetFilePos (::strlen ("QSetWorkingDir:"));
376     std::string path;
377     packet.GetHexByteString (path);
378
379     // If this packet is sent to a platform, then change the current working directory
380     if (::chdir(path.c_str()) != 0)
381         return SendErrorResponse (errno);
382     return SendOKResponse ();
383 }
384
385 GDBRemoteCommunication::PacketResult
386 GDBRemoteCommunicationServerPlatform::Handle_qC (StringExtractorGDBRemote &packet)
387 {
388     // NOTE: lldb should now be using qProcessInfo for process IDs.  This path here
389     // should not be used.  It is reporting process id instead of thread id.  The
390     // correct answer doesn't seem to make much sense for lldb-platform.
391     // CONSIDER: flip to "unsupported".
392     lldb::pid_t pid = m_process_launch_info.GetProcessID();
393
394     StreamString response;
395     response.Printf("QC%" PRIx64, pid);
396
397     // If we launch a process and this GDB server is acting as a platform,
398     // then we need to clear the process launch state so we can start
399     // launching another process. In order to launch a process a bunch or
400     // packets need to be sent: environment packets, working directory,
401     // disable ASLR, and many more settings. When we launch a process we
402     // then need to know when to clear this information. Currently we are
403     // selecting the 'qC' packet as that packet which seems to make the most
404     // sense.
405     if (pid != LLDB_INVALID_PROCESS_ID)
406     {
407         m_process_launch_info.Clear();
408     }
409
410     return SendPacketNoLock (response.GetData(), response.GetSize());
411 }
412
413 GDBRemoteCommunication::PacketResult
414 GDBRemoteCommunicationServerPlatform::Handle_jSignalsInfo(StringExtractorGDBRemote &packet)
415 {
416     StructuredData::Array signal_array;
417
418     const auto &signals = Host::GetUnixSignals();
419     for (auto signo = signals->GetFirstSignalNumber();
420          signo != LLDB_INVALID_SIGNAL_NUMBER;
421          signo = signals->GetNextSignalNumber(signo))
422     {
423         auto dictionary = std::make_shared<StructuredData::Dictionary>();
424
425         dictionary->AddIntegerItem("signo", signo);
426         dictionary->AddStringItem("name", signals->GetSignalAsCString(signo));
427
428         bool suppress, stop, notify;
429         signals->GetSignalInfo(signo, suppress, stop, notify);
430         dictionary->AddBooleanItem("suppress", suppress);
431         dictionary->AddBooleanItem("stop", stop);
432         dictionary->AddBooleanItem("notify", notify);
433
434         signal_array.Push(dictionary);
435     }
436
437     StreamString response;
438     signal_array.Dump(response);
439     return SendPacketNoLock(response.GetData(), response.GetSize());
440 }
441
442 bool
443 GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped (lldb::pid_t pid)
444 {
445     std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
446     FreePortForProcess(pid);
447     m_spawned_pids.erase(pid);
448     return true;
449 }
450
451 Error
452 GDBRemoteCommunicationServerPlatform::LaunchProcess ()
453 {
454     if (!m_process_launch_info.GetArguments ().GetArgumentCount ())
455         return Error ("%s: no process command line specified to launch", __FUNCTION__);
456
457     // specify the process monitor if not already set.  This should
458     // generally be what happens since we need to reap started
459     // processes.
460     if (!m_process_launch_info.GetMonitorProcessCallback ())
461         m_process_launch_info.SetMonitorProcessCallback(
462             std::bind(&GDBRemoteCommunicationServerPlatform::DebugserverProcessReaped, this, std::placeholders::_1),
463             false);
464
465     Error error = Host::LaunchProcess(m_process_launch_info);
466     if (!error.Success ())
467     {
468         fprintf (stderr, "%s: failed to launch executable %s", __FUNCTION__, m_process_launch_info.GetArguments ().GetArgumentAtIndex (0));
469         return error;
470     }
471
472     printf ("Launched '%s' as process %" PRIu64 "...\n", m_process_launch_info.GetArguments ().GetArgumentAtIndex (0), m_process_launch_info.GetProcessID());
473
474     // add to list of spawned processes.  On an lldb-gdbserver, we
475     // would expect there to be only one.
476     const auto pid = m_process_launch_info.GetProcessID();
477     if (pid != LLDB_INVALID_PROCESS_ID)
478     {
479         // add to spawned pids
480         std::lock_guard<std::recursive_mutex> guard(m_spawned_pids_mutex);
481         m_spawned_pids.insert(pid);
482     }
483
484     return error;
485 }
486
487 void
488 GDBRemoteCommunicationServerPlatform::SetPortMap (PortMap &&port_map)
489 {
490     m_port_map = port_map;
491 }
492
493 uint16_t
494 GDBRemoteCommunicationServerPlatform::GetNextAvailablePort ()
495 {
496     if (m_port_map.empty())
497         return 0; // Bind to port zero and get a port, we didn't have any limitations
498
499     for (auto &pair : m_port_map)
500     {
501         if (pair.second == LLDB_INVALID_PROCESS_ID)
502         {
503             pair.second = ~(lldb::pid_t)LLDB_INVALID_PROCESS_ID;
504             return pair.first;
505         }
506     }
507     return UINT16_MAX;
508 }
509
510 bool
511 GDBRemoteCommunicationServerPlatform::AssociatePortWithProcess (uint16_t port, lldb::pid_t pid)
512 {
513     PortMap::iterator pos = m_port_map.find(port);
514     if (pos != m_port_map.end())
515     {
516         pos->second = pid;
517         return true;
518     }
519     return false;
520 }
521
522 bool
523 GDBRemoteCommunicationServerPlatform::FreePort (uint16_t port)
524 {
525     PortMap::iterator pos = m_port_map.find(port);
526     if (pos != m_port_map.end())
527     {
528         pos->second = LLDB_INVALID_PROCESS_ID;
529         return true;
530     }
531     return false;
532 }
533
534 bool
535 GDBRemoteCommunicationServerPlatform::FreePortForProcess (lldb::pid_t pid)
536 {
537     if (!m_port_map.empty())
538     {
539         for (auto &pair : m_port_map)
540         {
541             if (pair.second == pid)
542             {
543                 pair.second = LLDB_INVALID_PROCESS_ID;
544                 return true;
545             }
546         }
547     }
548     return false;
549 }
550
551 const FileSpec&
552 GDBRemoteCommunicationServerPlatform::GetDomainSocketDir()
553 {
554     static FileSpec g_domainsocket_dir;
555     static std::once_flag g_once_flag;
556
557     std::call_once(g_once_flag, []() {
558         const char* domainsocket_dir_env = ::getenv("LLDB_DEBUGSERVER_DOMAINSOCKET_DIR");
559         if (domainsocket_dir_env != nullptr)
560             g_domainsocket_dir = FileSpec(domainsocket_dir_env, false);
561         else
562             HostInfo::GetLLDBPath(ePathTypeLLDBTempSystemDir, g_domainsocket_dir);
563     });
564
565     return g_domainsocket_dir;
566 }
567
568 FileSpec
569 GDBRemoteCommunicationServerPlatform::GetDomainSocketPath(const char* prefix)
570 {
571     llvm::SmallString<PATH_MAX> socket_path;
572     llvm::SmallString<PATH_MAX> socket_name((llvm::StringRef(prefix) + ".%%%%%%").str());
573
574     FileSpec socket_path_spec(GetDomainSocketDir());
575     socket_path_spec.AppendPathComponent(socket_name.c_str());
576
577     llvm::sys::fs::createUniqueFile(socket_path_spec.GetCString(), socket_path);
578     return FileSpec(socket_path.c_str(), false);
579 }
580
581 void
582 GDBRemoteCommunicationServerPlatform::SetPortOffset(uint16_t port_offset)
583 {
584     m_port_offset = port_offset;
585 }
586
587 void
588 GDBRemoteCommunicationServerPlatform::SetPendingGdbServer(lldb::pid_t pid,
589                                                           uint16_t port,
590                                                           const std::string& socket_name)
591 {
592     m_pending_gdb_server.pid = pid;
593     m_pending_gdb_server.port = port;
594     m_pending_gdb_server.socket_name = socket_name;
595 }