10#include "lldb/Host/Config.h"
32#include "llvm/Support/FormatAdapters.h"
79 return "A platform that uses the GDB remote protocol as the communication "
100 const auto module_path = module_file_spec.
GetPath(
false);
103 !
m_gdb_client_up->GetModuleInfo(module_file_spec, arch, module_spec)) {
106 "PlatformRemoteGDBServer::%s - failed to get module info for %s:%s",
107 __FUNCTION__, module_path.c_str(),
114 module_spec.
Dump(stream);
116 "PlatformRemoteGDBServer::%s - got module info for (%s:%s) : %s",
117 __FUNCTION__, module_path.c_str(),
125 const UUID *uuid_ptr,
128 local_file = platform_file;
161std::optional<std::string>
181 "PlatformRemoteGDBServer::GetRemoteWorkingDirectory() -> '%s'",
182 working_dir.
GetPath().c_str());
195 LLDB_LOGF(log,
"PlatformRemoteGDBServer::SetRemoteWorkingDirectory('%s')",
196 working_dir.
GetPath().c_str());
213 error.SetErrorStringWithFormat(
"the platform is already connected to '%s', "
214 "execute 'platform disconnect' to close the "
215 "current connection",
221 error.SetErrorString(
222 "\"platform connect\" takes a single argument: <connect-url>");
228 return Status(
"URL is null.");
230 std::optional<URI> parsed_url =
URI::Parse(url);
232 return Status(
"Invalid URL: %s", url);
239 std::make_unique<process_gdb_remote::GDBRemoteCommunicationClient>();
240 client_up->SetPacketTimeout(
242 client_up->SetConnection(std::make_unique<ConnectionFileDescriptor>());
243 client_up->Connect(url, &
error);
248 if (client_up->HandshakeWithServer(&
error)) {
260 if (remote_arch.
GetTriple().isArch64Bit())
265 client_up->Disconnect();
267 error.SetErrorString(
"handshake failed");
287std::optional<std::string>
291 return std::move(name);
295std::optional<std::string>
299 return std::move(name);
322 LLDB_LOGF(log,
"PlatformRemoteGDBServer::%s() called", __FUNCTION__);
325 return Status(
"Not connected.");
327 for (
decltype(num_file_actions) i = 0; i < num_file_actions; ++i) {
331 switch (file_action->GetFD()) {
358 const char *arch_triple = arch_spec.
GetTriple().str().c_str();
363 "PlatformRemoteGDBServer::%s() set launch architecture triple to '%s'",
364 __FUNCTION__, arch_triple ? arch_triple :
"<NULL>");
376 error.SetErrorStringWithFormatv(
"Cannot launch '{0}': {1}",
378 llvm::fmt_consume(std::move(err)));
387 "PlatformRemoteGDBServer::%s() pid %" PRIu64
388 " launched successfully",
392 "PlatformRemoteGDBServer::%s() launch succeeded but we "
393 "didn't get a valid process id back!",
395 error.SetErrorString(
"failed to get PID");
402 return Status(
"failed to kill remote spawned process");
410 lldb::ProcessSP process_sp;
414 std::string connect_url;
416 error.SetErrorStringWithFormat(
"unable to launch a GDB server on '%s'",
422 "gdb-remote",
nullptr,
true);
428 error = process_sp->ConnectRemote(connect_url.c_str());
431 error = process_sp->ConnectRemote(connect_url.c_str());
433 error = process_sp->Launch(launch_info);
435 printf(
"error: connect remote failed (%s)\n",
error.AsCString());
441 error.SetErrorString(
"not connected to remote gdb server");
448 std::string &connect_url) {
452 llvm::Triple &remote_triple = remote_arch.
GetTriple();
455 std::string socket_name;
456 bool launch_result =
false;
457 if (remote_triple.getVendor() == llvm::Triple::Apple &&
458 remote_triple.getOS() == llvm::Triple::IOS) {
475 (socket_name.empty()) ?
nullptr : socket_name.c_str());
489 lldb::ProcessSP process_sp;
493 std::string connect_url;
495 error.SetErrorStringWithFormat(
"unable to launch a GDB server on '%s'",
498 if (target ==
nullptr) {
499 TargetSP new_target_sp;
503 target = new_target_sp.get();
507 if (target &&
error.Success()) {
512 "gdb-remote",
nullptr,
true);
514 error = process_sp->ConnectRemote(connect_url.c_str());
515 if (
error.Success()) {
518 process_sp->HijackProcessEvents(listener_sp);
520 error = process_sp->Attach(attach_info);
530 error.SetErrorString(
"not connected to remote gdb server");
539 return Status(
"Not connected.");
543 "PlatformRemoteGDBServer::MakeDirectory(path='%s', mode=%o) "
553 return Status(
"Not connected.");
558 "PlatformRemoteGDBServer::GetFilePermissions(path='%s', "
559 "file_permissions=%o) error = %u (%s)",
560 file_spec.
GetPath().c_str(), file_permissions,
error.GetError(),
568 return Status(
"Not connected.");
573 "PlatformRemoteGDBServer::SetFilePermissions(path='%s', "
574 "file_permissions=%o) error = %u (%s)",
575 file_spec.
GetPath().c_str(), file_permissions,
error.GetError(),
610 void *dst, uint64_t dst_len,
619 const void *src, uint64_t src_len,
638 return Status(
"Not connected.");
642 "PlatformRemoteGDBServer::CreateSymlink(src='%s', dst='%s') "
651 return Status(
"Not connected.");
654 LLDB_LOGF(log,
"PlatformRemoteGDBServer::Unlink(path='%s') error = %u (%s)",
666 llvm::StringRef shell, llvm::StringRef command,
676 return Status(
"Not connected.");
677 return m_gdb_client_up->RunShellCommand(command, working_dir, status_ptr,
678 signo_ptr, command_output, timeout);
698 m_gdb_client_up->SendPacketAndWaitForResponse(
"jSignalsInfo", response);
700 if (result !=
decltype(result)
::Success ||
705 if (!object_sp || !object_sp->IsValid())
708 auto array_sp = object_sp->GetAsArray();
709 if (!array_sp || !array_sp->IsValid())
712 auto remote_signals_sp = std::make_shared<lldb_private::GDBRemoteSignals>();
714 bool done = array_sp->ForEach(
716 if (!
object || !object->
IsValid())
720 if (!dict || !dict->IsValid())
725 if (!dict->GetValueForKeyAsInteger(
"signo", signo))
728 llvm::StringRef name;
729 if (!dict->GetValueForKeyAsString(
"name", name))
733 bool suppress{false};
735 if (object_sp && object_sp->IsValid())
736 suppress = object_sp->GetBooleanValue();
739 object_sp = dict->GetValueForKey(
"stop");
740 if (object_sp && object_sp->IsValid())
741 stop = object_sp->GetBooleanValue();
744 object_sp = dict->GetValueForKey(
"notify");
745 if (object_sp && object_sp->IsValid())
746 notify = object_sp->GetBooleanValue();
748 std::string description;
749 object_sp = dict->GetValueForKey(
"description");
750 if (object_sp && object_sp->IsValid())
751 description = std::string(object_sp->GetStringValue());
753 remote_signals_sp->AddSignal(signo, name.str().c_str(), suppress, stop,
754 notify, description.c_str());
765 const std::string &platform_scheme,
const std::string &platform_hostname,
766 uint16_t port,
const char *socket_name) {
767 const char *override_scheme =
768 getenv(
"LLDB_PLATFORM_REMOTE_GDB_SERVER_SCHEME");
769 const char *override_hostname =
770 getenv(
"LLDB_PLATFORM_REMOTE_GDB_SERVER_HOSTNAME");
771 const char *port_offset_c_str =
772 getenv(
"LLDB_PLATFORM_REMOTE_GDB_SERVER_PORT_OFFSET");
773 int port_offset = port_offset_c_str ? ::atoi(port_offset_c_str) : 0;
775 return MakeUrl(override_scheme ? override_scheme : platform_scheme.c_str(),
776 override_hostname ? override_hostname
777 : platform_hostname.c_str(),
778 port + port_offset, socket_name);
782 const char *hostname,
785 result.
Printf(
"%s://[%s]", scheme, hostname);
787 result.
Printf(
":%u", port);
789 result.
Write(path, strlen(path));
795 std::vector<std::string> connection_urls;
798 for (
size_t i = 0; i < connection_urls.size(); ++i) {
803 return connection_urls.size();
807 std::vector<std::string> &connection_urls) {
808 std::vector<std::pair<uint16_t, std::string>> remote_servers;
812 for (
const auto &gdbserver : remote_servers) {
813 const char *socket_name_cstr =
814 gdbserver.second.empty() ? nullptr : gdbserver.second.c_str();
815 connection_urls.emplace_back(
817 gdbserver.first, socket_name_cstr));
819 return connection_urls.size();
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE_ADV(ClassName, PluginName)
An architecture specification class.
llvm::Triple & GetTriple()
Architecture triple accessor.
bool TripleVendorWasSpecified() const
bool TripleOSWasSpecified() const
A command line argument class.
size_t GetArgumentCount() const
Gets the number of arguments left in this command object.
void ReplaceArgumentAtIndex(size_t idx, llvm::StringRef arg_str, char quote_char='\0')
Replaces the argument value at index idx to arg_str if idx is a valid argument index.
const char * GetArgumentAtIndex(size_t idx) const
Gets the NULL terminated C string argument pointer for the argument at index idx.
Class that manages the actual breakpoint that will be inserted into the running program.
"lldb/Utility/ArgCompletionRequest.h"
A uniqued constant string class.
A class to manage flag bits.
TargetList & GetTargetList()
Get accessor for the target list.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
bool Test(ValueType bit) const
Test a single flag bit.
void Dump(Stream &strm) const
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
lldb::ListenerSP GetListenerForProcess(Debugger &debugger)
lldb::ListenerSP GetHijackListener() const
void SetProcessID(lldb::pid_t pid)
FileSpec & GetExecutableFile()
lldb::ListenerSP GetListener() const
lldb::ListenerSP GetShadowListener() const
Environment & GetEnvironment()
ArchSpec & GetArchitecture()
const FileAction * GetFileActionAtIndex(size_t idx) const
const FileSpec & GetWorkingDirectory() const
size_t GetNumFileActions() const
const char * GetData() const
llvm::StringRef GetString() const
size_t Write(const void *src, size_t src_len)
Output character bytes to the stream.
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
ObjectSP GetValueForKey(llvm::StringRef key) const
Dictionary * GetAsDictionary()
virtual bool IsValid() const
static ObjectSP ParseJSON(llvm::StringRef json_text)
Status CreateTarget(Debugger &debugger, llvm::StringRef user_exe_path, llvm::StringRef triple_str, LoadDependentFiles get_dependent_modules, const OptionGroupPlatform *platform_options, lldb::TargetSP &target_sp)
Create a new Target.
const lldb::ProcessSP & CreateProcess(lldb::ListenerSP listener_sp, llvm::StringRef plugin_name, const FileSpec *crash_file, bool can_connect)
static lldb::UnixSignalsSP Create(const ArchSpec &arch)
static std::chrono::seconds GetPacketTimeout()
#define LLDB_INVALID_PROCESS_ID
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::vector< ProcessInstanceInfo > ProcessInstanceInfoList
static std::optional< URI > Parse(llvm::StringRef uri)