1 //===-- PlatformRemoteGDBServer.cpp -----------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "PlatformRemoteGDBServer.h"
11 #include "lldb/Host/Config.h"
14 // Other libraries and framework includes
16 #include "lldb/Breakpoint/BreakpointLocation.h"
17 #include "lldb/Core/Debugger.h"
18 #include "lldb/Core/Module.h"
19 #include "lldb/Core/ModuleList.h"
20 #include "lldb/Core/ModuleSpec.h"
21 #include "lldb/Core/PluginManager.h"
22 #include "lldb/Core/StreamFile.h"
23 #include "lldb/Host/ConnectionFileDescriptor.h"
24 #include "lldb/Host/Host.h"
25 #include "lldb/Host/HostInfo.h"
26 #include "lldb/Target/Process.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Utility/FileSpec.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/Status.h"
31 #include "lldb/Utility/StreamString.h"
32 #include "lldb/Utility/UriParser.h"
34 #include "Plugins/Process/Utility/GDBRemoteSignals.h"
37 using namespace lldb_private;
38 using namespace lldb_private::platform_gdb_server;
40 static bool g_initialized = false;
42 void PlatformRemoteGDBServer::Initialize() {
43 Platform::Initialize();
45 if (g_initialized == false) {
47 PluginManager::RegisterPlugin(
48 PlatformRemoteGDBServer::GetPluginNameStatic(),
49 PlatformRemoteGDBServer::GetDescriptionStatic(),
50 PlatformRemoteGDBServer::CreateInstance);
54 void PlatformRemoteGDBServer::Terminate() {
56 g_initialized = false;
57 PluginManager::UnregisterPlugin(PlatformRemoteGDBServer::CreateInstance);
60 Platform::Terminate();
63 PlatformSP PlatformRemoteGDBServer::CreateInstance(bool force,
64 const ArchSpec *arch) {
67 create = !arch->TripleVendorWasSpecified() && !arch->TripleOSWasSpecified();
70 return PlatformSP(new PlatformRemoteGDBServer());
74 ConstString PlatformRemoteGDBServer::GetPluginNameStatic() {
75 static ConstString g_name("remote-gdb-server");
79 const char *PlatformRemoteGDBServer::GetDescriptionStatic() {
80 return "A platform that uses the GDB remote protocol as the communication "
84 const char *PlatformRemoteGDBServer::GetDescription() {
85 if (m_platform_description.empty()) {
87 // Send the get description packet
91 if (!m_platform_description.empty())
92 return m_platform_description.c_str();
93 return GetDescriptionStatic();
96 Status PlatformRemoteGDBServer::ResolveExecutable(
97 const ModuleSpec &module_spec, lldb::ModuleSP &exe_module_sp,
98 const FileSpecList *module_search_paths_ptr) {
99 // copied from PlatformRemoteiOS
102 // Nothing special to do here, just use the actual file and architecture
104 ModuleSpec resolved_module_spec(module_spec);
106 // Resolve any executable within an apk on Android?
107 // Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
109 if (resolved_module_spec.GetFileSpec().Exists() ||
110 module_spec.GetUUID().IsValid()) {
111 if (resolved_module_spec.GetArchitecture().IsValid() ||
112 resolved_module_spec.GetUUID().IsValid()) {
113 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
114 module_search_paths_ptr, NULL, NULL);
116 if (exe_module_sp && exe_module_sp->GetObjectFile())
118 exe_module_sp.reset();
120 // No valid architecture was specified or the exact arch wasn't
121 // found so ask the platform for the architectures that we should be
122 // using (in the correct order) and see if we can find a match that way
123 StreamString arch_names;
124 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
125 idx, resolved_module_spec.GetArchitecture());
127 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
128 module_search_paths_ptr, NULL, NULL);
129 // Did we find an executable using one of the
130 if (error.Success()) {
131 if (exe_module_sp && exe_module_sp->GetObjectFile())
134 error.SetErrorToGenericError();
138 arch_names.PutCString(", ");
139 arch_names.PutCString(
140 resolved_module_spec.GetArchitecture().GetArchitectureName());
143 if (error.Fail() || !exe_module_sp) {
144 if (resolved_module_spec.GetFileSpec().Readable()) {
145 error.SetErrorStringWithFormat(
146 "'%s' doesn't contain any '%s' platform architectures: %s",
147 resolved_module_spec.GetFileSpec().GetPath().c_str(),
148 GetPluginName().GetCString(), arch_names.GetData());
150 error.SetErrorStringWithFormat(
151 "'%s' is not readable",
152 resolved_module_spec.GetFileSpec().GetPath().c_str());
156 error.SetErrorStringWithFormat(
157 "'%s' does not exist",
158 resolved_module_spec.GetFileSpec().GetPath().c_str());
164 bool PlatformRemoteGDBServer::GetModuleSpec(const FileSpec &module_file_spec,
165 const ArchSpec &arch,
166 ModuleSpec &module_spec) {
167 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
169 const auto module_path = module_file_spec.GetPath(false);
171 if (!m_gdb_client.GetModuleInfo(module_file_spec, arch, module_spec)) {
174 "PlatformRemoteGDBServer::%s - failed to get module info for %s:%s",
175 __FUNCTION__, module_path.c_str(),
176 arch.GetTriple().getTriple().c_str());
182 module_spec.Dump(stream);
184 "PlatformRemoteGDBServer::%s - got module info for (%s:%s) : %s",
185 __FUNCTION__, module_path.c_str(), arch.GetTriple().getTriple().c_str(),
192 Status PlatformRemoteGDBServer::GetFileWithUUID(const FileSpec &platform_file,
193 const UUID *uuid_ptr,
194 FileSpec &local_file) {
195 // Default to the local case
196 local_file = platform_file;
200 //------------------------------------------------------------------
201 /// Default Constructor
202 //------------------------------------------------------------------
203 PlatformRemoteGDBServer::PlatformRemoteGDBServer()
204 : Platform(false), // This is a remote platform
207 //------------------------------------------------------------------
210 /// The destructor is virtual since this class is designed to be
211 /// inherited from by the plug-in instance.
212 //------------------------------------------------------------------
213 PlatformRemoteGDBServer::~PlatformRemoteGDBServer() {}
215 bool PlatformRemoteGDBServer::GetSupportedArchitectureAtIndex(uint32_t idx,
217 ArchSpec remote_arch = m_gdb_client.GetSystemArchitecture();
221 return arch.IsValid();
222 } else if (idx == 1 && remote_arch.IsValid() &&
223 remote_arch.GetTriple().isArch64Bit()) {
224 arch.SetTriple(remote_arch.GetTriple().get32BitArchVariant());
225 return arch.IsValid();
230 size_t PlatformRemoteGDBServer::GetSoftwareBreakpointTrapOpcode(
231 Target &target, BreakpointSite *bp_site) {
232 // This isn't needed if the z/Z packets are supported in the GDB remote
233 // server. But we might need a packet to detect this.
237 bool PlatformRemoteGDBServer::GetRemoteOSVersion() {
238 uint32_t major, minor, update;
239 if (m_gdb_client.GetOSVersion(major, minor, update)) {
240 m_major_os_version = major;
241 m_minor_os_version = minor;
242 m_update_os_version = update;
248 bool PlatformRemoteGDBServer::GetRemoteOSBuildString(std::string &s) {
249 return m_gdb_client.GetOSBuildString(s);
252 bool PlatformRemoteGDBServer::GetRemoteOSKernelDescription(std::string &s) {
253 return m_gdb_client.GetOSKernelDescription(s);
256 // Remote Platform subclasses need to override this function
257 ArchSpec PlatformRemoteGDBServer::GetRemoteSystemArchitecture() {
258 return m_gdb_client.GetSystemArchitecture();
261 FileSpec PlatformRemoteGDBServer::GetRemoteWorkingDirectory() {
263 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
264 FileSpec working_dir;
265 if (m_gdb_client.GetWorkingDir(working_dir) && log)
267 "PlatformRemoteGDBServer::GetRemoteWorkingDirectory() -> '%s'",
268 working_dir.GetCString());
271 return Platform::GetRemoteWorkingDirectory();
275 bool PlatformRemoteGDBServer::SetRemoteWorkingDirectory(
276 const FileSpec &working_dir) {
278 // Clear the working directory it case it doesn't get set correctly. This
280 // for use to re-read it
281 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
283 log->Printf("PlatformRemoteGDBServer::SetRemoteWorkingDirectory('%s')",
284 working_dir.GetCString());
285 return m_gdb_client.SetWorkingDir(working_dir) == 0;
287 return Platform::SetRemoteWorkingDirectory(working_dir);
290 bool PlatformRemoteGDBServer::IsConnected() const {
291 return m_gdb_client.IsConnected();
294 Status PlatformRemoteGDBServer::ConnectRemote(Args &args) {
297 error.SetErrorStringWithFormat("the platform is already connected to '%s', "
298 "execute 'platform disconnect' to close the "
299 "current connection",
302 if (args.GetArgumentCount() == 1) {
303 m_gdb_client.SetConnection(new ConnectionFileDescriptor());
304 // we're going to reuse the hostname when we connect to the debugserver
307 const char *url = args.GetArgumentAtIndex(0);
309 return Status("URL is null.");
310 llvm::StringRef scheme, hostname, pathname;
311 if (!UriParser::Parse(url, scheme, hostname, port, pathname))
312 return Status("Invalid URL: %s", url);
313 m_platform_scheme = scheme;
314 m_platform_hostname = hostname;
317 const ConnectionStatus status = m_gdb_client.Connect(url, &error);
318 if (status == eConnectionStatusSuccess) {
319 if (m_gdb_client.HandshakeWithServer(&error)) {
320 m_gdb_client.GetHostInfo();
321 // If a working directory was set prior to connecting, send it down
324 m_gdb_client.SetWorkingDir(m_working_dir);
326 m_gdb_client.Disconnect();
328 error.SetErrorString("handshake failed");
332 error.SetErrorString(
333 "\"platform connect\" takes a single argument: <connect-url>");
339 Status PlatformRemoteGDBServer::DisconnectRemote() {
341 m_gdb_client.Disconnect(&error);
342 m_remote_signals_sp.reset();
346 const char *PlatformRemoteGDBServer::GetHostname() {
347 m_gdb_client.GetHostname(m_name);
350 return m_name.c_str();
353 const char *PlatformRemoteGDBServer::GetUserName(uint32_t uid) {
354 // Try and get a cache user name first
355 const char *cached_user_name = Platform::GetUserName(uid);
356 if (cached_user_name)
357 return cached_user_name;
359 if (m_gdb_client.GetUserName(uid, name))
360 return SetCachedUserName(uid, name.c_str(), name.size());
362 SetUserNameNotFound(uid); // Negative cache so we don't keep sending packets
366 const char *PlatformRemoteGDBServer::GetGroupName(uint32_t gid) {
367 const char *cached_group_name = Platform::GetGroupName(gid);
368 if (cached_group_name)
369 return cached_group_name;
371 if (m_gdb_client.GetGroupName(gid, name))
372 return SetCachedGroupName(gid, name.c_str(), name.size());
374 SetGroupNameNotFound(gid); // Negative cache so we don't keep sending packets
378 uint32_t PlatformRemoteGDBServer::FindProcesses(
379 const ProcessInstanceInfoMatch &match_info,
380 ProcessInstanceInfoList &process_infos) {
381 return m_gdb_client.FindProcesses(match_info, process_infos);
384 bool PlatformRemoteGDBServer::GetProcessInfo(
385 lldb::pid_t pid, ProcessInstanceInfo &process_info) {
386 return m_gdb_client.GetProcessInfo(pid, process_info);
389 Status PlatformRemoteGDBServer::LaunchProcess(ProcessLaunchInfo &launch_info) {
390 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
394 log->Printf("PlatformRemoteGDBServer::%s() called", __FUNCTION__);
396 auto num_file_actions = launch_info.GetNumFileActions();
397 for (decltype(num_file_actions) i = 0; i < num_file_actions; ++i) {
398 const auto file_action = launch_info.GetFileActionAtIndex(i);
399 if (file_action->GetAction() != FileAction::eFileActionOpen)
401 switch (file_action->GetFD()) {
403 m_gdb_client.SetSTDIN(file_action->GetFileSpec());
406 m_gdb_client.SetSTDOUT(file_action->GetFileSpec());
409 m_gdb_client.SetSTDERR(file_action->GetFileSpec());
414 m_gdb_client.SetDisableASLR(
415 launch_info.GetFlags().Test(eLaunchFlagDisableASLR));
416 m_gdb_client.SetDetachOnError(
417 launch_info.GetFlags().Test(eLaunchFlagDetachOnError));
419 FileSpec working_dir = launch_info.GetWorkingDirectory();
421 m_gdb_client.SetWorkingDir(working_dir);
424 // Send the environment and the program + arguments after we connect
426 launch_info.GetEnvironmentEntries().GetConstArgumentVector();
429 const char *env_entry;
430 for (int i = 0; (env_entry = envp[i]); ++i) {
431 if (m_gdb_client.SendEnvironmentPacket(env_entry) != 0)
436 ArchSpec arch_spec = launch_info.GetArchitecture();
437 const char *arch_triple = arch_spec.GetTriple().str().c_str();
439 m_gdb_client.SendLaunchArchPacket(arch_triple);
442 "PlatformRemoteGDBServer::%s() set launch architecture triple to '%s'",
443 __FUNCTION__, arch_triple ? arch_triple : "<NULL>");
447 // Scope for the scoped timeout object
448 process_gdb_remote::GDBRemoteCommunication::ScopedTimeout timeout(
449 m_gdb_client, std::chrono::seconds(5));
450 arg_packet_err = m_gdb_client.SendArgumentsPacket(launch_info);
453 if (arg_packet_err == 0) {
454 std::string error_str;
455 if (m_gdb_client.GetLaunchSuccess(error_str)) {
456 const auto pid = m_gdb_client.GetCurrentProcessID(false);
457 if (pid != LLDB_INVALID_PROCESS_ID) {
458 launch_info.SetProcessID(pid);
460 log->Printf("PlatformRemoteGDBServer::%s() pid %" PRIu64
461 " launched successfully",
465 log->Printf("PlatformRemoteGDBServer::%s() launch succeeded but we "
466 "didn't get a valid process id back!",
468 error.SetErrorString("failed to get PID");
471 error.SetErrorString(error_str.c_str());
473 log->Printf("PlatformRemoteGDBServer::%s() launch failed: %s",
474 __FUNCTION__, error.AsCString());
477 error.SetErrorStringWithFormat("'A' packet returned an error: %i",
483 Status PlatformRemoteGDBServer::KillProcess(const lldb::pid_t pid) {
484 if (!KillSpawnedProcess(pid))
485 return Status("failed to kill remote spawned process");
489 lldb::ProcessSP PlatformRemoteGDBServer::DebugProcess(
490 ProcessLaunchInfo &launch_info, Debugger &debugger,
491 Target *target, // Can be NULL, if NULL create a new target, else use
494 lldb::ProcessSP process_sp;
497 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
498 std::string connect_url;
499 if (!LaunchGDBServer(debugserver_pid, connect_url)) {
500 error.SetErrorStringWithFormat("unable to launch a GDB server on '%s'",
503 if (target == NULL) {
504 TargetSP new_target_sp;
506 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
507 NULL, new_target_sp);
508 target = new_target_sp.get();
512 if (target && error.Success()) {
513 debugger.GetTargetList().SetSelectedTarget(target);
515 // The darwin always currently uses the GDB remote debugger plug-in
516 // so even when debugging locally we are debugging remotely!
517 process_sp = target->CreateProcess(
518 launch_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
521 error = process_sp->ConnectRemote(nullptr, connect_url.c_str());
522 // Retry the connect remote one time...
524 error = process_sp->ConnectRemote(nullptr, connect_url.c_str());
526 error = process_sp->Launch(launch_info);
527 else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) {
528 printf("error: connect remote failed (%s)\n", error.AsCString());
529 KillSpawnedProcess(debugserver_pid);
535 error.SetErrorString("not connected to remote gdb server");
541 bool PlatformRemoteGDBServer::LaunchGDBServer(lldb::pid_t &pid,
542 std::string &connect_url) {
543 ArchSpec remote_arch = GetRemoteSystemArchitecture();
544 llvm::Triple &remote_triple = remote_arch.GetTriple();
547 std::string socket_name;
548 bool launch_result = false;
549 if (remote_triple.getVendor() == llvm::Triple::Apple &&
550 remote_triple.getOS() == llvm::Triple::IOS) {
551 // When remote debugging to iOS, we use a USB mux that always talks
552 // to localhost, so we will need the remote debugserver to accept
554 // only from localhost, no matter what our current hostname is
556 m_gdb_client.LaunchGDBServer("127.0.0.1", pid, port, socket_name);
558 // All other hosts should use their actual hostname
560 m_gdb_client.LaunchGDBServer(nullptr, pid, port, socket_name);
567 MakeGdbServerUrl(m_platform_scheme, m_platform_hostname, port,
568 (socket_name.empty()) ? nullptr : socket_name.c_str());
572 bool PlatformRemoteGDBServer::KillSpawnedProcess(lldb::pid_t pid) {
573 return m_gdb_client.KillSpawnedProcess(pid);
576 lldb::ProcessSP PlatformRemoteGDBServer::Attach(
577 ProcessAttachInfo &attach_info, Debugger &debugger,
578 Target *target, // Can be NULL, if NULL create a new target, else use
581 lldb::ProcessSP process_sp;
584 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
585 std::string connect_url;
586 if (!LaunchGDBServer(debugserver_pid, connect_url)) {
587 error.SetErrorStringWithFormat("unable to launch a GDB server on '%s'",
590 if (target == NULL) {
591 TargetSP new_target_sp;
593 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
594 NULL, new_target_sp);
595 target = new_target_sp.get();
599 if (target && error.Success()) {
600 debugger.GetTargetList().SetSelectedTarget(target);
602 // The darwin always currently uses the GDB remote debugger plug-in
603 // so even when debugging locally we are debugging remotely!
604 process_sp = target->CreateProcess(
605 attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
607 error = process_sp->ConnectRemote(nullptr, connect_url.c_str());
608 if (error.Success()) {
609 ListenerSP listener_sp = attach_info.GetHijackListener();
611 process_sp->HijackProcessEvents(listener_sp);
612 error = process_sp->Attach(attach_info);
615 if (error.Fail() && debugserver_pid != LLDB_INVALID_PROCESS_ID) {
616 KillSpawnedProcess(debugserver_pid);
622 error.SetErrorString("not connected to remote gdb server");
628 Status PlatformRemoteGDBServer::MakeDirectory(const FileSpec &file_spec,
630 Status error = m_gdb_client.MakeDirectory(file_spec, mode);
631 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
633 log->Printf("PlatformRemoteGDBServer::MakeDirectory(path='%s', mode=%o) "
635 file_spec.GetCString(), mode, error.GetError(),
640 Status PlatformRemoteGDBServer::GetFilePermissions(const FileSpec &file_spec,
641 uint32_t &file_permissions) {
642 Status error = m_gdb_client.GetFilePermissions(file_spec, file_permissions);
643 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
645 log->Printf("PlatformRemoteGDBServer::GetFilePermissions(path='%s', "
646 "file_permissions=%o) error = %u (%s)",
647 file_spec.GetCString(), file_permissions, error.GetError(),
652 Status PlatformRemoteGDBServer::SetFilePermissions(const FileSpec &file_spec,
653 uint32_t file_permissions) {
654 Status error = m_gdb_client.SetFilePermissions(file_spec, file_permissions);
655 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
657 log->Printf("PlatformRemoteGDBServer::SetFilePermissions(path='%s', "
658 "file_permissions=%o) error = %u (%s)",
659 file_spec.GetCString(), file_permissions, error.GetError(),
664 lldb::user_id_t PlatformRemoteGDBServer::OpenFile(const FileSpec &file_spec,
665 uint32_t flags, uint32_t mode,
667 return m_gdb_client.OpenFile(file_spec, flags, mode, error);
670 bool PlatformRemoteGDBServer::CloseFile(lldb::user_id_t fd, Status &error) {
671 return m_gdb_client.CloseFile(fd, error);
675 PlatformRemoteGDBServer::GetFileSize(const FileSpec &file_spec) {
676 return m_gdb_client.GetFileSize(file_spec);
679 uint64_t PlatformRemoteGDBServer::ReadFile(lldb::user_id_t fd, uint64_t offset,
680 void *dst, uint64_t dst_len,
682 return m_gdb_client.ReadFile(fd, offset, dst, dst_len, error);
685 uint64_t PlatformRemoteGDBServer::WriteFile(lldb::user_id_t fd, uint64_t offset,
686 const void *src, uint64_t src_len,
688 return m_gdb_client.WriteFile(fd, offset, src, src_len, error);
691 Status PlatformRemoteGDBServer::PutFile(const FileSpec &source,
692 const FileSpec &destination,
693 uint32_t uid, uint32_t gid) {
694 return Platform::PutFile(source, destination, uid, gid);
697 Status PlatformRemoteGDBServer::CreateSymlink(
698 const FileSpec &src, // The name of the link is in src
699 const FileSpec &dst) // The symlink points to dst
701 Status error = m_gdb_client.CreateSymlink(src, dst);
702 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
704 log->Printf("PlatformRemoteGDBServer::CreateSymlink(src='%s', dst='%s') "
706 src.GetCString(), dst.GetCString(), error.GetError(),
711 Status PlatformRemoteGDBServer::Unlink(const FileSpec &file_spec) {
712 Status error = m_gdb_client.Unlink(file_spec);
713 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
715 log->Printf("PlatformRemoteGDBServer::Unlink(path='%s') error = %u (%s)",
716 file_spec.GetCString(), error.GetError(), error.AsCString());
720 bool PlatformRemoteGDBServer::GetFileExists(const FileSpec &file_spec) {
721 return m_gdb_client.GetFileExists(file_spec);
724 Status PlatformRemoteGDBServer::RunShellCommand(
725 const char *command, // Shouldn't be NULL
727 working_dir, // Pass empty FileSpec to use the current working directory
728 int *status_ptr, // Pass NULL if you don't want the process exit status
729 int *signo_ptr, // Pass NULL if you don't want the signal that caused the
732 *command_output, // Pass NULL if you don't want the command output
734 timeout_sec) // Timeout in seconds to wait for shell program to finish
736 return m_gdb_client.RunShellCommand(command, working_dir, status_ptr,
737 signo_ptr, command_output, timeout_sec);
740 void PlatformRemoteGDBServer::CalculateTrapHandlerSymbolNames() {
741 m_trap_handlers.push_back(ConstString("_sigtramp"));
744 const UnixSignalsSP &PlatformRemoteGDBServer::GetRemoteUnixSignals() {
746 return Platform::GetRemoteUnixSignals();
748 if (m_remote_signals_sp)
749 return m_remote_signals_sp;
751 // If packet not implemented or JSON failed to parse,
752 // we'll guess the signal set based on the remote architecture.
753 m_remote_signals_sp = UnixSignals::Create(GetRemoteSystemArchitecture());
755 StringExtractorGDBRemote response;
756 auto result = m_gdb_client.SendPacketAndWaitForResponse("jSignalsInfo",
759 if (result != decltype(result)::Success ||
760 response.GetResponseType() != response.eResponse)
761 return m_remote_signals_sp;
763 auto object_sp = StructuredData::ParseJSON(response.GetStringRef());
764 if (!object_sp || !object_sp->IsValid())
765 return m_remote_signals_sp;
767 auto array_sp = object_sp->GetAsArray();
768 if (!array_sp || !array_sp->IsValid())
769 return m_remote_signals_sp;
771 auto remote_signals_sp = std::make_shared<lldb_private::GDBRemoteSignals>();
773 bool done = array_sp->ForEach(
774 [&remote_signals_sp](StructuredData::Object *object) -> bool {
775 if (!object || !object->IsValid())
778 auto dict = object->GetAsDictionary();
779 if (!dict || !dict->IsValid())
782 // Signal number and signal name are required.
784 if (!dict->GetValueForKeyAsInteger("signo", signo))
787 llvm::StringRef name;
788 if (!dict->GetValueForKeyAsString("name", name))
791 // We can live without short_name, description, etc.
792 bool suppress{false};
793 auto object_sp = dict->GetValueForKey("suppress");
794 if (object_sp && object_sp->IsValid())
795 suppress = object_sp->GetBooleanValue();
798 object_sp = dict->GetValueForKey("stop");
799 if (object_sp && object_sp->IsValid())
800 stop = object_sp->GetBooleanValue();
803 object_sp = dict->GetValueForKey("notify");
804 if (object_sp && object_sp->IsValid())
805 notify = object_sp->GetBooleanValue();
807 std::string description{""};
808 object_sp = dict->GetValueForKey("description");
809 if (object_sp && object_sp->IsValid())
810 description = object_sp->GetStringValue();
812 remote_signals_sp->AddSignal(signo, name.str().c_str(), suppress, stop,
813 notify, description.c_str());
818 m_remote_signals_sp = std::move(remote_signals_sp);
820 return m_remote_signals_sp;
823 std::string PlatformRemoteGDBServer::MakeGdbServerUrl(
824 const std::string &platform_scheme, const std::string &platform_hostname,
825 uint16_t port, const char *socket_name) {
826 const char *override_scheme =
827 getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_SCHEME");
828 const char *override_hostname =
829 getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
830 const char *port_offset_c_str =
831 getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
832 int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
834 return MakeUrl(override_scheme ? override_scheme : platform_scheme.c_str(),
835 override_hostname ? override_hostname
836 : platform_hostname.c_str(),
837 port + port_offset, socket_name);
840 std::string PlatformRemoteGDBServer::MakeUrl(const char *scheme,
841 const char *hostname,
842 uint16_t port, const char *path) {
844 result.Printf("%s://%s", scheme, hostname);
846 result.Printf(":%u", port);
848 result.Write(path, strlen(path));
849 return result.GetString();
852 lldb::ProcessSP PlatformRemoteGDBServer::ConnectProcess(
853 llvm::StringRef connect_url, llvm::StringRef plugin_name,
854 lldb_private::Debugger &debugger, lldb_private::Target *target,
855 lldb_private::Status &error) {
856 if (!IsRemote() || !IsConnected()) {
857 error.SetErrorString("Not connected to remote gdb server");
860 return Platform::ConnectProcess(connect_url, plugin_name, debugger, target,
864 size_t PlatformRemoteGDBServer::ConnectToWaitingProcesses(Debugger &debugger,
866 std::vector<std::string> connection_urls;
867 GetPendingGdbServerList(connection_urls);
869 for (size_t i = 0; i < connection_urls.size(); ++i) {
870 ConnectProcess(connection_urls[i].c_str(), "", debugger, nullptr, error);
872 return i; // We already connected to i process succsessfully
874 return connection_urls.size();
877 size_t PlatformRemoteGDBServer::GetPendingGdbServerList(
878 std::vector<std::string> &connection_urls) {
879 std::vector<std::pair<uint16_t, std::string>> remote_servers;
880 m_gdb_client.QueryGDBServer(remote_servers);
881 for (const auto &gdbserver : remote_servers) {
882 const char *socket_name_cstr =
883 gdbserver.second.empty() ? nullptr : gdbserver.second.c_str();
884 connection_urls.emplace_back(
885 MakeGdbServerUrl(m_platform_scheme, m_platform_hostname,
886 gdbserver.first, socket_name_cstr));
888 return connection_urls.size();