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/Host/PosixApi.h"
27 #include "lldb/Target/Process.h"
28 #include "lldb/Target/Target.h"
29 #include "lldb/Utility/FileSpec.h"
30 #include "lldb/Utility/Log.h"
31 #include "lldb/Utility/Status.h"
32 #include "lldb/Utility/StreamString.h"
33 #include "lldb/Utility/UriParser.h"
35 #include "Plugins/Process/Utility/GDBRemoteSignals.h"
38 using namespace lldb_private;
39 using namespace lldb_private::platform_gdb_server;
41 static bool g_initialized = false;
43 void PlatformRemoteGDBServer::Initialize() {
44 Platform::Initialize();
46 if (g_initialized == false) {
48 PluginManager::RegisterPlugin(
49 PlatformRemoteGDBServer::GetPluginNameStatic(),
50 PlatformRemoteGDBServer::GetDescriptionStatic(),
51 PlatformRemoteGDBServer::CreateInstance);
55 void PlatformRemoteGDBServer::Terminate() {
57 g_initialized = false;
58 PluginManager::UnregisterPlugin(PlatformRemoteGDBServer::CreateInstance);
61 Platform::Terminate();
64 PlatformSP PlatformRemoteGDBServer::CreateInstance(bool force,
65 const ArchSpec *arch) {
68 create = !arch->TripleVendorWasSpecified() && !arch->TripleOSWasSpecified();
71 return PlatformSP(new PlatformRemoteGDBServer());
75 ConstString PlatformRemoteGDBServer::GetPluginNameStatic() {
76 static ConstString g_name("remote-gdb-server");
80 const char *PlatformRemoteGDBServer::GetDescriptionStatic() {
81 return "A platform that uses the GDB remote protocol as the communication "
85 const char *PlatformRemoteGDBServer::GetDescription() {
86 if (m_platform_description.empty()) {
88 // Send the get description packet
92 if (!m_platform_description.empty())
93 return m_platform_description.c_str();
94 return GetDescriptionStatic();
97 Status PlatformRemoteGDBServer::ResolveExecutable(
98 const ModuleSpec &module_spec, lldb::ModuleSP &exe_module_sp,
99 const FileSpecList *module_search_paths_ptr) {
100 // copied from PlatformRemoteiOS
103 // Nothing special to do here, just use the actual file and architecture
105 ModuleSpec resolved_module_spec(module_spec);
107 // Resolve any executable within an apk on Android?
108 // Host::ResolveExecutableInBundle (resolved_module_spec.GetFileSpec());
110 if (resolved_module_spec.GetFileSpec().Exists() ||
111 module_spec.GetUUID().IsValid()) {
112 if (resolved_module_spec.GetArchitecture().IsValid() ||
113 resolved_module_spec.GetUUID().IsValid()) {
114 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
115 module_search_paths_ptr, NULL, NULL);
117 if (exe_module_sp && exe_module_sp->GetObjectFile())
119 exe_module_sp.reset();
121 // No valid architecture was specified or the exact arch wasn't found so
122 // ask the platform for the architectures that we should be using (in the
123 // correct order) and see if we can find a match that way
124 StreamString arch_names;
125 for (uint32_t idx = 0; GetSupportedArchitectureAtIndex(
126 idx, resolved_module_spec.GetArchitecture());
128 error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
129 module_search_paths_ptr, NULL, NULL);
130 // Did we find an executable using one of the
131 if (error.Success()) {
132 if (exe_module_sp && exe_module_sp->GetObjectFile())
135 error.SetErrorToGenericError();
139 arch_names.PutCString(", ");
140 arch_names.PutCString(
141 resolved_module_spec.GetArchitecture().GetArchitectureName());
144 if (error.Fail() || !exe_module_sp) {
145 if (resolved_module_spec.GetFileSpec().Readable()) {
146 error.SetErrorStringWithFormat(
147 "'%s' doesn't contain any '%s' platform architectures: %s",
148 resolved_module_spec.GetFileSpec().GetPath().c_str(),
149 GetPluginName().GetCString(), arch_names.GetData());
151 error.SetErrorStringWithFormat(
152 "'%s' is not readable",
153 resolved_module_spec.GetFileSpec().GetPath().c_str());
157 error.SetErrorStringWithFormat(
158 "'%s' does not exist",
159 resolved_module_spec.GetFileSpec().GetPath().c_str());
165 bool PlatformRemoteGDBServer::GetModuleSpec(const FileSpec &module_file_spec,
166 const ArchSpec &arch,
167 ModuleSpec &module_spec) {
168 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
170 const auto module_path = module_file_spec.GetPath(false);
172 if (!m_gdb_client.GetModuleInfo(module_file_spec, arch, module_spec)) {
175 "PlatformRemoteGDBServer::%s - failed to get module info for %s:%s",
176 __FUNCTION__, module_path.c_str(),
177 arch.GetTriple().getTriple().c_str());
183 module_spec.Dump(stream);
185 "PlatformRemoteGDBServer::%s - got module info for (%s:%s) : %s",
186 __FUNCTION__, module_path.c_str(), arch.GetTriple().getTriple().c_str(),
193 Status PlatformRemoteGDBServer::GetFileWithUUID(const FileSpec &platform_file,
194 const UUID *uuid_ptr,
195 FileSpec &local_file) {
196 // Default to the local case
197 local_file = platform_file;
201 //------------------------------------------------------------------
202 /// Default Constructor
203 //------------------------------------------------------------------
204 PlatformRemoteGDBServer::PlatformRemoteGDBServer()
205 : Platform(false), // This is a remote platform
208 //------------------------------------------------------------------
211 /// The destructor is virtual since this class is designed to be
212 /// inherited from by the plug-in instance.
213 //------------------------------------------------------------------
214 PlatformRemoteGDBServer::~PlatformRemoteGDBServer() {}
216 bool PlatformRemoteGDBServer::GetSupportedArchitectureAtIndex(uint32_t idx,
218 ArchSpec remote_arch = m_gdb_client.GetSystemArchitecture();
222 return arch.IsValid();
223 } else if (idx == 1 && remote_arch.IsValid() &&
224 remote_arch.GetTriple().isArch64Bit()) {
225 arch.SetTriple(remote_arch.GetTriple().get32BitArchVariant());
226 return arch.IsValid();
231 size_t PlatformRemoteGDBServer::GetSoftwareBreakpointTrapOpcode(
232 Target &target, BreakpointSite *bp_site) {
233 // This isn't needed if the z/Z packets are supported in the GDB remote
234 // server. But we might need a packet to detect this.
238 bool PlatformRemoteGDBServer::GetRemoteOSVersion() {
239 m_os_version = m_gdb_client.GetOSVersion();
240 return !m_os_version.empty();
243 bool PlatformRemoteGDBServer::GetRemoteOSBuildString(std::string &s) {
244 return m_gdb_client.GetOSBuildString(s);
247 bool PlatformRemoteGDBServer::GetRemoteOSKernelDescription(std::string &s) {
248 return m_gdb_client.GetOSKernelDescription(s);
251 // Remote Platform subclasses need to override this function
252 ArchSpec PlatformRemoteGDBServer::GetRemoteSystemArchitecture() {
253 return m_gdb_client.GetSystemArchitecture();
256 FileSpec PlatformRemoteGDBServer::GetRemoteWorkingDirectory() {
258 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
259 FileSpec working_dir;
260 if (m_gdb_client.GetWorkingDir(working_dir) && log)
262 "PlatformRemoteGDBServer::GetRemoteWorkingDirectory() -> '%s'",
263 working_dir.GetCString());
266 return Platform::GetRemoteWorkingDirectory();
270 bool PlatformRemoteGDBServer::SetRemoteWorkingDirectory(
271 const FileSpec &working_dir) {
273 // Clear the working directory it case it doesn't get set correctly. This
274 // will for use to re-read it
275 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
277 log->Printf("PlatformRemoteGDBServer::SetRemoteWorkingDirectory('%s')",
278 working_dir.GetCString());
279 return m_gdb_client.SetWorkingDir(working_dir) == 0;
281 return Platform::SetRemoteWorkingDirectory(working_dir);
284 bool PlatformRemoteGDBServer::IsConnected() const {
285 return m_gdb_client.IsConnected();
288 Status PlatformRemoteGDBServer::ConnectRemote(Args &args) {
291 error.SetErrorStringWithFormat("the platform is already connected to '%s', "
292 "execute 'platform disconnect' to close the "
293 "current connection",
296 if (args.GetArgumentCount() == 1) {
297 m_gdb_client.SetConnection(new ConnectionFileDescriptor());
298 // we're going to reuse the hostname when we connect to the debugserver
301 const char *url = args.GetArgumentAtIndex(0);
303 return Status("URL is null.");
304 llvm::StringRef scheme, hostname, pathname;
305 if (!UriParser::Parse(url, scheme, hostname, port, pathname))
306 return Status("Invalid URL: %s", url);
307 m_platform_scheme = scheme;
308 m_platform_hostname = hostname;
311 const ConnectionStatus status = m_gdb_client.Connect(url, &error);
312 if (status == eConnectionStatusSuccess) {
313 if (m_gdb_client.HandshakeWithServer(&error)) {
314 m_gdb_client.GetHostInfo();
315 // If a working directory was set prior to connecting, send it down
318 m_gdb_client.SetWorkingDir(m_working_dir);
320 m_gdb_client.Disconnect();
322 error.SetErrorString("handshake failed");
326 error.SetErrorString(
327 "\"platform connect\" takes a single argument: <connect-url>");
333 Status PlatformRemoteGDBServer::DisconnectRemote() {
335 m_gdb_client.Disconnect(&error);
336 m_remote_signals_sp.reset();
340 const char *PlatformRemoteGDBServer::GetHostname() {
341 m_gdb_client.GetHostname(m_name);
344 return m_name.c_str();
347 const char *PlatformRemoteGDBServer::GetUserName(uint32_t uid) {
348 // Try and get a cache user name first
349 const char *cached_user_name = Platform::GetUserName(uid);
350 if (cached_user_name)
351 return cached_user_name;
353 if (m_gdb_client.GetUserName(uid, name))
354 return SetCachedUserName(uid, name.c_str(), name.size());
356 SetUserNameNotFound(uid); // Negative cache so we don't keep sending packets
360 const char *PlatformRemoteGDBServer::GetGroupName(uint32_t gid) {
361 const char *cached_group_name = Platform::GetGroupName(gid);
362 if (cached_group_name)
363 return cached_group_name;
365 if (m_gdb_client.GetGroupName(gid, name))
366 return SetCachedGroupName(gid, name.c_str(), name.size());
368 SetGroupNameNotFound(gid); // Negative cache so we don't keep sending packets
372 uint32_t PlatformRemoteGDBServer::FindProcesses(
373 const ProcessInstanceInfoMatch &match_info,
374 ProcessInstanceInfoList &process_infos) {
375 return m_gdb_client.FindProcesses(match_info, process_infos);
378 bool PlatformRemoteGDBServer::GetProcessInfo(
379 lldb::pid_t pid, ProcessInstanceInfo &process_info) {
380 return m_gdb_client.GetProcessInfo(pid, process_info);
383 Status PlatformRemoteGDBServer::LaunchProcess(ProcessLaunchInfo &launch_info) {
384 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_PLATFORM));
388 log->Printf("PlatformRemoteGDBServer::%s() called", __FUNCTION__);
390 auto num_file_actions = launch_info.GetNumFileActions();
391 for (decltype(num_file_actions) i = 0; i < num_file_actions; ++i) {
392 const auto file_action = launch_info.GetFileActionAtIndex(i);
393 if (file_action->GetAction() != FileAction::eFileActionOpen)
395 switch (file_action->GetFD()) {
397 m_gdb_client.SetSTDIN(file_action->GetFileSpec());
400 m_gdb_client.SetSTDOUT(file_action->GetFileSpec());
403 m_gdb_client.SetSTDERR(file_action->GetFileSpec());
408 m_gdb_client.SetDisableASLR(
409 launch_info.GetFlags().Test(eLaunchFlagDisableASLR));
410 m_gdb_client.SetDetachOnError(
411 launch_info.GetFlags().Test(eLaunchFlagDetachOnError));
413 FileSpec working_dir = launch_info.GetWorkingDirectory();
415 m_gdb_client.SetWorkingDir(working_dir);
418 // Send the environment and the program + arguments after we connect
419 m_gdb_client.SendEnvironment(launch_info.GetEnvironment());
421 ArchSpec arch_spec = launch_info.GetArchitecture();
422 const char *arch_triple = arch_spec.GetTriple().str().c_str();
424 m_gdb_client.SendLaunchArchPacket(arch_triple);
427 "PlatformRemoteGDBServer::%s() set launch architecture triple to '%s'",
428 __FUNCTION__, arch_triple ? arch_triple : "<NULL>");
432 // Scope for the scoped timeout object
433 process_gdb_remote::GDBRemoteCommunication::ScopedTimeout timeout(
434 m_gdb_client, std::chrono::seconds(5));
435 arg_packet_err = m_gdb_client.SendArgumentsPacket(launch_info);
438 if (arg_packet_err == 0) {
439 std::string error_str;
440 if (m_gdb_client.GetLaunchSuccess(error_str)) {
441 const auto pid = m_gdb_client.GetCurrentProcessID(false);
442 if (pid != LLDB_INVALID_PROCESS_ID) {
443 launch_info.SetProcessID(pid);
445 log->Printf("PlatformRemoteGDBServer::%s() pid %" PRIu64
446 " launched successfully",
450 log->Printf("PlatformRemoteGDBServer::%s() launch succeeded but we "
451 "didn't get a valid process id back!",
453 error.SetErrorString("failed to get PID");
456 error.SetErrorString(error_str.c_str());
458 log->Printf("PlatformRemoteGDBServer::%s() launch failed: %s",
459 __FUNCTION__, error.AsCString());
462 error.SetErrorStringWithFormat("'A' packet returned an error: %i",
468 Status PlatformRemoteGDBServer::KillProcess(const lldb::pid_t pid) {
469 if (!KillSpawnedProcess(pid))
470 return Status("failed to kill remote spawned process");
474 lldb::ProcessSP PlatformRemoteGDBServer::DebugProcess(
475 ProcessLaunchInfo &launch_info, Debugger &debugger,
476 Target *target, // Can be NULL, if NULL create a new target, else use
479 lldb::ProcessSP process_sp;
482 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
483 std::string connect_url;
484 if (!LaunchGDBServer(debugserver_pid, connect_url)) {
485 error.SetErrorStringWithFormat("unable to launch a GDB server on '%s'",
488 if (target == NULL) {
489 TargetSP new_target_sp;
491 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
492 NULL, new_target_sp);
493 target = new_target_sp.get();
497 if (target && error.Success()) {
498 debugger.GetTargetList().SetSelectedTarget(target);
500 // The darwin always currently uses the GDB remote debugger plug-in
501 // so even when debugging locally we are debugging remotely!
502 process_sp = target->CreateProcess(
503 launch_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
506 error = process_sp->ConnectRemote(nullptr, connect_url.c_str());
507 // Retry the connect remote one time...
509 error = process_sp->ConnectRemote(nullptr, connect_url.c_str());
511 error = process_sp->Launch(launch_info);
512 else if (debugserver_pid != LLDB_INVALID_PROCESS_ID) {
513 printf("error: connect remote failed (%s)\n", error.AsCString());
514 KillSpawnedProcess(debugserver_pid);
520 error.SetErrorString("not connected to remote gdb server");
526 bool PlatformRemoteGDBServer::LaunchGDBServer(lldb::pid_t &pid,
527 std::string &connect_url) {
528 ArchSpec remote_arch = GetRemoteSystemArchitecture();
529 llvm::Triple &remote_triple = remote_arch.GetTriple();
532 std::string socket_name;
533 bool launch_result = false;
534 if (remote_triple.getVendor() == llvm::Triple::Apple &&
535 remote_triple.getOS() == llvm::Triple::IOS) {
536 // When remote debugging to iOS, we use a USB mux that always talks to
537 // localhost, so we will need the remote debugserver to accept connections
538 // only from localhost, no matter what our current hostname is
540 m_gdb_client.LaunchGDBServer("127.0.0.1", pid, port, socket_name);
542 // All other hosts should use their actual hostname
544 m_gdb_client.LaunchGDBServer(nullptr, pid, port, socket_name);
551 MakeGdbServerUrl(m_platform_scheme, m_platform_hostname, port,
552 (socket_name.empty()) ? nullptr : socket_name.c_str());
556 bool PlatformRemoteGDBServer::KillSpawnedProcess(lldb::pid_t pid) {
557 return m_gdb_client.KillSpawnedProcess(pid);
560 lldb::ProcessSP PlatformRemoteGDBServer::Attach(
561 ProcessAttachInfo &attach_info, Debugger &debugger,
562 Target *target, // Can be NULL, if NULL create a new target, else use
565 lldb::ProcessSP process_sp;
568 lldb::pid_t debugserver_pid = LLDB_INVALID_PROCESS_ID;
569 std::string connect_url;
570 if (!LaunchGDBServer(debugserver_pid, connect_url)) {
571 error.SetErrorStringWithFormat("unable to launch a GDB server on '%s'",
574 if (target == NULL) {
575 TargetSP new_target_sp;
577 error = debugger.GetTargetList().CreateTarget(debugger, "", "", false,
578 NULL, new_target_sp);
579 target = new_target_sp.get();
583 if (target && error.Success()) {
584 debugger.GetTargetList().SetSelectedTarget(target);
586 // The darwin always currently uses the GDB remote debugger plug-in
587 // so even when debugging locally we are debugging remotely!
588 process_sp = target->CreateProcess(
589 attach_info.GetListenerForProcess(debugger), "gdb-remote", NULL);
591 error = process_sp->ConnectRemote(nullptr, connect_url.c_str());
592 if (error.Success()) {
593 ListenerSP listener_sp = attach_info.GetHijackListener();
595 process_sp->HijackProcessEvents(listener_sp);
596 error = process_sp->Attach(attach_info);
599 if (error.Fail() && debugserver_pid != LLDB_INVALID_PROCESS_ID) {
600 KillSpawnedProcess(debugserver_pid);
606 error.SetErrorString("not connected to remote gdb server");
612 Status PlatformRemoteGDBServer::MakeDirectory(const FileSpec &file_spec,
614 Status error = m_gdb_client.MakeDirectory(file_spec, mode);
615 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
617 log->Printf("PlatformRemoteGDBServer::MakeDirectory(path='%s', mode=%o) "
619 file_spec.GetCString(), mode, error.GetError(),
624 Status PlatformRemoteGDBServer::GetFilePermissions(const FileSpec &file_spec,
625 uint32_t &file_permissions) {
626 Status error = m_gdb_client.GetFilePermissions(file_spec, file_permissions);
627 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
629 log->Printf("PlatformRemoteGDBServer::GetFilePermissions(path='%s', "
630 "file_permissions=%o) error = %u (%s)",
631 file_spec.GetCString(), file_permissions, error.GetError(),
636 Status PlatformRemoteGDBServer::SetFilePermissions(const FileSpec &file_spec,
637 uint32_t file_permissions) {
638 Status error = m_gdb_client.SetFilePermissions(file_spec, file_permissions);
639 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
641 log->Printf("PlatformRemoteGDBServer::SetFilePermissions(path='%s', "
642 "file_permissions=%o) error = %u (%s)",
643 file_spec.GetCString(), file_permissions, error.GetError(),
648 lldb::user_id_t PlatformRemoteGDBServer::OpenFile(const FileSpec &file_spec,
649 uint32_t flags, uint32_t mode,
651 return m_gdb_client.OpenFile(file_spec, flags, mode, error);
654 bool PlatformRemoteGDBServer::CloseFile(lldb::user_id_t fd, Status &error) {
655 return m_gdb_client.CloseFile(fd, error);
659 PlatformRemoteGDBServer::GetFileSize(const FileSpec &file_spec) {
660 return m_gdb_client.GetFileSize(file_spec);
663 uint64_t PlatformRemoteGDBServer::ReadFile(lldb::user_id_t fd, uint64_t offset,
664 void *dst, uint64_t dst_len,
666 return m_gdb_client.ReadFile(fd, offset, dst, dst_len, error);
669 uint64_t PlatformRemoteGDBServer::WriteFile(lldb::user_id_t fd, uint64_t offset,
670 const void *src, uint64_t src_len,
672 return m_gdb_client.WriteFile(fd, offset, src, src_len, error);
675 Status PlatformRemoteGDBServer::PutFile(const FileSpec &source,
676 const FileSpec &destination,
677 uint32_t uid, uint32_t gid) {
678 return Platform::PutFile(source, destination, uid, gid);
681 Status PlatformRemoteGDBServer::CreateSymlink(
682 const FileSpec &src, // The name of the link is in src
683 const FileSpec &dst) // The symlink points to dst
685 Status error = m_gdb_client.CreateSymlink(src, dst);
686 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
688 log->Printf("PlatformRemoteGDBServer::CreateSymlink(src='%s', dst='%s') "
690 src.GetCString(), dst.GetCString(), error.GetError(),
695 Status PlatformRemoteGDBServer::Unlink(const FileSpec &file_spec) {
696 Status error = m_gdb_client.Unlink(file_spec);
697 Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PLATFORM);
699 log->Printf("PlatformRemoteGDBServer::Unlink(path='%s') error = %u (%s)",
700 file_spec.GetCString(), error.GetError(), error.AsCString());
704 bool PlatformRemoteGDBServer::GetFileExists(const FileSpec &file_spec) {
705 return m_gdb_client.GetFileExists(file_spec);
708 Status PlatformRemoteGDBServer::RunShellCommand(
709 const char *command, // Shouldn't be NULL
711 working_dir, // Pass empty FileSpec to use the current working directory
712 int *status_ptr, // Pass NULL if you don't want the process exit status
713 int *signo_ptr, // Pass NULL if you don't want the signal that caused the
716 *command_output, // Pass NULL if you don't want the command output
717 const Timeout<std::micro> &timeout) {
718 return m_gdb_client.RunShellCommand(command, working_dir, status_ptr,
719 signo_ptr, command_output, timeout);
722 void PlatformRemoteGDBServer::CalculateTrapHandlerSymbolNames() {
723 m_trap_handlers.push_back(ConstString("_sigtramp"));
726 const UnixSignalsSP &PlatformRemoteGDBServer::GetRemoteUnixSignals() {
728 return Platform::GetRemoteUnixSignals();
730 if (m_remote_signals_sp)
731 return m_remote_signals_sp;
733 // If packet not implemented or JSON failed to parse, we'll guess the signal
734 // set based on the remote architecture.
735 m_remote_signals_sp = UnixSignals::Create(GetRemoteSystemArchitecture());
737 StringExtractorGDBRemote response;
738 auto result = m_gdb_client.SendPacketAndWaitForResponse("jSignalsInfo",
741 if (result != decltype(result)::Success ||
742 response.GetResponseType() != response.eResponse)
743 return m_remote_signals_sp;
745 auto object_sp = StructuredData::ParseJSON(response.GetStringRef());
746 if (!object_sp || !object_sp->IsValid())
747 return m_remote_signals_sp;
749 auto array_sp = object_sp->GetAsArray();
750 if (!array_sp || !array_sp->IsValid())
751 return m_remote_signals_sp;
753 auto remote_signals_sp = std::make_shared<lldb_private::GDBRemoteSignals>();
755 bool done = array_sp->ForEach(
756 [&remote_signals_sp](StructuredData::Object *object) -> bool {
757 if (!object || !object->IsValid())
760 auto dict = object->GetAsDictionary();
761 if (!dict || !dict->IsValid())
764 // Signal number and signal name are required.
766 if (!dict->GetValueForKeyAsInteger("signo", signo))
769 llvm::StringRef name;
770 if (!dict->GetValueForKeyAsString("name", name))
773 // We can live without short_name, description, etc.
774 bool suppress{false};
775 auto object_sp = dict->GetValueForKey("suppress");
776 if (object_sp && object_sp->IsValid())
777 suppress = object_sp->GetBooleanValue();
780 object_sp = dict->GetValueForKey("stop");
781 if (object_sp && object_sp->IsValid())
782 stop = object_sp->GetBooleanValue();
785 object_sp = dict->GetValueForKey("notify");
786 if (object_sp && object_sp->IsValid())
787 notify = object_sp->GetBooleanValue();
789 std::string description{""};
790 object_sp = dict->GetValueForKey("description");
791 if (object_sp && object_sp->IsValid())
792 description = object_sp->GetStringValue();
794 remote_signals_sp->AddSignal(signo, name.str().c_str(), suppress, stop,
795 notify, description.c_str());
800 m_remote_signals_sp = std::move(remote_signals_sp);
802 return m_remote_signals_sp;
805 std::string PlatformRemoteGDBServer::MakeGdbServerUrl(
806 const std::string &platform_scheme, const std::string &platform_hostname,
807 uint16_t port, const char *socket_name) {
808 const char *override_scheme =
809 getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_SCHEME");
810 const char *override_hostname =
811 getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
812 const char *port_offset_c_str =
813 getenv("LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
814 int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
816 return MakeUrl(override_scheme ? override_scheme : platform_scheme.c_str(),
817 override_hostname ? override_hostname
818 : platform_hostname.c_str(),
819 port + port_offset, socket_name);
822 std::string PlatformRemoteGDBServer::MakeUrl(const char *scheme,
823 const char *hostname,
824 uint16_t port, const char *path) {
826 result.Printf("%s://%s", scheme, hostname);
828 result.Printf(":%u", port);
830 result.Write(path, strlen(path));
831 return result.GetString();
834 lldb::ProcessSP PlatformRemoteGDBServer::ConnectProcess(
835 llvm::StringRef connect_url, llvm::StringRef plugin_name,
836 lldb_private::Debugger &debugger, lldb_private::Target *target,
837 lldb_private::Status &error) {
838 if (!IsRemote() || !IsConnected()) {
839 error.SetErrorString("Not connected to remote gdb server");
842 return Platform::ConnectProcess(connect_url, plugin_name, debugger, target,
846 size_t PlatformRemoteGDBServer::ConnectToWaitingProcesses(Debugger &debugger,
848 std::vector<std::string> connection_urls;
849 GetPendingGdbServerList(connection_urls);
851 for (size_t i = 0; i < connection_urls.size(); ++i) {
852 ConnectProcess(connection_urls[i].c_str(), "", debugger, nullptr, error);
854 return i; // We already connected to i process succsessfully
856 return connection_urls.size();
859 size_t PlatformRemoteGDBServer::GetPendingGdbServerList(
860 std::vector<std::string> &connection_urls) {
861 std::vector<std::pair<uint16_t, std::string>> remote_servers;
862 m_gdb_client.QueryGDBServer(remote_servers);
863 for (const auto &gdbserver : remote_servers) {
864 const char *socket_name_cstr =
865 gdbserver.second.empty() ? nullptr : gdbserver.second.c_str();
866 connection_urls.emplace_back(
867 MakeGdbServerUrl(m_platform_scheme, m_platform_hostname,
868 gdbserver.first, socket_name_cstr));
870 return connection_urls.size();