11#include "lldb/Host/Config.h"
44#include "llvm/Support/JSON.h"
45#include "llvm/Support/ScopedPrinter.h"
46#include "llvm/TargetParser/Triple.h"
60enum GDBRemoteServerError {
63 eErrorNoProcess = eErrorFirst,
73 m_process_manager(process_manager), m_current_process(nullptr),
74 m_continue_process(nullptr), m_stdio_communication() {
229 bool &interrupt,
bool &quit) {
265 "%s: no process command line specified to launch", __FUNCTION__);
267 const bool should_forward_stdio =
274 if (should_forward_stdio) {
287 "process but one already exists");
297 SetEnabledExtensions(*m_current_process);
304 if (should_forward_stdio) {
309 "pid = {0}: setting up stdout/stderr redirection via $O "
310 "gdb-remote commands",
311 m_current_process->GetID());
314 auto terminal_fd = m_current_process->GetTerminalFileDescriptor();
315 if (terminal_fd >= 0) {
317 "ProcessGDBRemoteCommunicationServerLLGS::%s setting "
318 "inferior STDIO fd to %d",
319 __FUNCTION__, terminal_fd);
320 Status status = SetSTDIOFileDescriptor(terminal_fd);
325 "ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
326 "inferior STDIO since terminal fd reported as %d",
327 __FUNCTION__, terminal_fd);
331 "pid = {0} skipping stdout/stderr redirection via $O: inferior "
332 "will communicate over client-provided file descriptors",
333 m_current_process->GetID());
336 printf(
"Launched '%s' as process %" PRIu64
"...\n",
337 m_process_launch_info.GetArguments().GetArgumentAtIndex(0),
338 m_current_process->GetID());
345 LLDB_LOGF(log,
"GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64,
352 "cannot attach to process %" PRIu64
353 " when another process with pid %" PRIu64
" is being debugged.",
360 llvm::errs() << llvm::formatv(
"failed to attach to process {0}: {1}\n", pid,
368 SetEnabledExtensions(*m_current_process);
371 auto terminal_fd = m_current_process->GetTerminalFileDescriptor();
372 if (terminal_fd >= 0) {
374 "ProcessGDBRemoteCommunicationServerLLGS::%s setting "
375 "inferior STDIO fd to %d",
376 __FUNCTION__, terminal_fd);
377 Status status = SetSTDIOFileDescriptor(terminal_fd);
382 "ProcessGDBRemoteCommunicationServerLLGS::%s ignoring "
383 "inferior STDIO since terminal fd reported as %d",
384 __FUNCTION__, terminal_fd);
387 printf(
"Attached to process %" PRIu64
"...\n", pid);
392 llvm::StringRef process_name,
bool include_existing) {
395 std::chrono::milliseconds polling_interval = std::chrono::milliseconds(1);
401 process_name, llvm::sys::path::Style::native);
404 if (include_existing) {
405 LLDB_LOG(log,
"including existing processes in search");
409 LLDB_LOG(log,
"placed '{0}' processes in the exclusion list.",
410 exclusion_list.size());
413 LLDB_LOG(log,
"waiting for '{0}' to appear", process_name);
415 auto is_in_exclusion_list =
417 for (
auto &excluded : exclusion_list) {
418 if (excluded.GetProcessID() == info.GetProcessID())
426 loop_process_list.clear();
429 llvm::erase_if(loop_process_list, is_in_exclusion_list);
432 if (loop_process_list.size() == 1) {
433 auto matching_process_pid = loop_process_list[0].GetProcessID();
434 LLDB_LOG(log,
"found pid {0}", matching_process_pid);
439 if (loop_process_list.size() > 1) {
442 "Multiple executables with name: '{0}' found. Pids: ",
444 for (
size_t i = 0; i < loop_process_list.size() - 1; ++i) {
445 error_stream.
Format(
"{0}, ", loop_process_list[i].GetProcessID());
447 error_stream.
Format(
"{0}.", loop_process_list.back().GetProcessID());
455 LLDB_LOG(log,
"sleep {0} seconds", polling_interval);
456 std::this_thread::sleep_for(polling_interval);
462 assert(process &&
"process cannot be NULL");
466 "GDBRemoteCommunicationServerLLGS::%s called with "
467 "NativeProcessProtocol pid %" PRIu64
", current state: %s",
468 __FUNCTION__, process->
GetID(),
476 assert(process &&
"process cannot be NULL");
482 LLDB_LOG(log,
"pid = {0}, failed to retrieve process exit status",
487 response.
PutHex8(GDBRemoteServerError::eErrorExitStatus);
491 LLDB_LOG(log,
"pid = {0}, returning exit type {1}", process->
GetID(),
500 response.
Format(
"{0:g}", *wait_status);
503 response.
Format(
";process:{0:x-}", process->
GetID());
511 uint32_t buf_size,
bool swap) {
514 for (i = buf_size - 1; i >= 0; i--)
517 for (i = 0; i < buf_size; i++)
538 switch (reg_info.
format) {
548 return "vector-sint8";
550 return "vector-uint8";
552 return "vector-sint16";
554 return "vector-uint16";
556 return "vector-sint32";
558 return "vector-uint32";
560 return "vector-float32";
562 return "vector-uint64";
564 return "vector-uint128";
571 switch (reg_info.
kinds[RegisterKind::eRegisterKindGeneric]) {
611 response.
Printf(
"%" PRIx32, *reg_num);
613 response.
Printf(
"%" PRIu32, *reg_num);
625 reg_value_p = ®_value;
636 std::vector<uint8_t> zeros(reg_info.
byte_size,
'\0');
642static std::optional<json::Object>
648 json::Object register_object;
650#ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET
651 const auto expedited_regs =
654 const auto expedited_regs =
657 if (expedited_regs.empty())
660 for (
auto ®_num : expedited_regs) {
663 if (reg_info_p ==
nullptr) {
665 "%s failed to get register info for register index %" PRIu32,
666 __FUNCTION__, reg_num);
677 LLDB_LOGF(log,
"%s failed to read register '%s' index %" PRIu32
": %s",
679 reg_info_p->
name ? reg_info_p->
name :
"<unnamed-register>",
680 reg_num,
error.AsCString());
688 register_object.try_emplace(llvm::to_string(reg_num),
692 return register_object;
696 switch (stop_reason) {
710 return "processor trace";
718 return "async interrupt";
729static llvm::Expected<json::Array>
733 json::Array threads_array;
740 std::string description;
741 if (!thread.GetStopReason(tid_stop_info, description))
742 return llvm::make_error<llvm::StringError>(
743 "failed to get stop reason", llvm::inconvertibleErrorCode());
745 const int signum = tid_stop_info.
signo;
748 "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64
750 " got signal signo = %d, reason = %d, exc_type = %" PRIu64,
751 __FUNCTION__, process.
GetID(), tid, signum,
755 json::Object thread_obj;
759 thread_obj.try_emplace(
"registers", std::move(*registers));
762 thread_obj.try_emplace(
"tid",
static_cast<int64_t
>(tid));
765 thread_obj.try_emplace(
"signal", signum);
767 const std::string thread_name = thread.GetName();
768 if (!thread_name.empty())
769 thread_obj.try_emplace(
"name", thread_name);
773 thread_obj.try_emplace(
"reason", stop_reason);
775 if (!description.empty())
776 thread_obj.try_emplace(
"description", description);
780 thread_obj.try_emplace(
783 json::Array medata_array;
786 medata_array.push_back(
789 thread_obj.try_emplace(
"medata", std::move(medata_array));
791 threads_array.push_back(std::move(thread_obj));
793 return threads_array;
803 LLDB_LOG(log,
"preparing packet for pid {0} tid {1}", process.
GetID(),
809 std::string description;
821 int signum = tid_stop_info.
signo;
824 "pid {0}, tid {1}, got signal signo = {2}, reason = {3}, exc_type = {4}",
829 response.
PutHex8(signum & 0xff);
837 const std::string thread_name = thread.
GetName();
838 if (!thread_name.empty()) {
839 size_t thread_name_len = thread_name.length();
841 if (::strcspn(thread_name.c_str(),
"$#+-;:") == thread_name_len) {
863 uint32_t thread_num = 0;
867 response.
Printf(
"%" PRIx64, listed_thread.GetID());
877 if (thread_num > 1) {
878 const bool threads_with_valid_stop_info_only =
true;
884 unescaped_response.
AsRawOstream() << std::move(*threads_info);
889 "failed to prepare a jstopinfo field for pid {1}: {0}",
895 char delimiter =
':';
907 LLDB_LOGF(log,
"%s failed to read register '%s' index %" PRIu32
": %s",
909 reg_info_p->
name ? reg_info_p->
name :
"<unnamed-register>",
910 reg_to_read,
error.AsCString());
929 const auto expedited_regs =
932 for (
auto ®_num : expedited_regs) {
936 if (reg_info_p !=
nullptr && reg_info_p->
value_regs ==
nullptr) {
939 if (
error.Success()) {
940 response.
Printf(
"%.02x:", reg_num);
946 "GDBRemoteCommunicationServerLLGS::%s failed to read "
947 "register '%s' index %" PRIu32
": %s",
949 reg_info_p->
name ? reg_info_p->
name :
"<unnamed-register>",
950 reg_num,
error.AsCString());
956 if (reason_str !=
nullptr) {
957 response.
Printf(
"reason:%s;", reason_str);
960 if (!description.empty()) {
991 response.
Printf(
"%s:p%" PRIx64
".%" PRIx64
";", reason_str,
1008 if (response.
Empty())
1028 if (listed_thread.GetID() != thread_to_skip) {
1030 if (!stop_reply.
Empty())
1038 assert(process &&
"process cannot be NULL");
1041 LLDB_LOGF(log,
"GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1044 *process, StateType::eStateExited,
false);
1047 "GDBRemoteCommunicationServerLLGS::%s failed to send stop "
1048 "notification for PID %" PRIu64
", state: eStateExited",
1049 __FUNCTION__, process->
GetID());
1080 assert(process &&
"process cannot be NULL");
1083 LLDB_LOGF(log,
"GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1086 *process, StateType::eStateStopped,
false);
1089 "GDBRemoteCommunicationServerLLGS::%s failed to send stop "
1090 "notification for PID %" PRIu64
", state: eStateExited",
1091 __FUNCTION__, process->
GetID());
1097 assert(process &&
"process cannot be NULL");
1101 "GDBRemoteCommunicationServerLLGS::%s called with "
1102 "NativeProcessProtocol pid %" PRIu64
", state: %s",
1107 case StateType::eStateRunning:
1110 case StateType::eStateStopped:
1121 case StateType::eStateExited:
1132 "GDBRemoteCommunicationServerLLGS::%s didn't handle state "
1133 "change for pid %" PRIu64
", new state: %s",
1146 std::unique_ptr<NativeProcessProtocol> child_process) {
1158 bool interrupt =
false;
1163 std::chrono::microseconds(0),
error, interrupt, done);
1169 "GDBRemoteCommunicationServerLLGS::%s processing a packet "
1171 __FUNCTION__,
error.AsCString());
1179 std::unique_ptr<Connection> connection) {
1180 IOObjectSP read_object_sp = connection->GetReadObject();
1193 if ((buffer ==
nullptr) || (len == 0)) {
1212 std::unique_ptr<ConnectionFileDescriptor> conn_up(
1224 "failed to set connection for inferior I/O communication");
1246 LLDB_LOG(log,
"Failed to set up stdio forwarding: {0}",
error);
1260 buffer,
sizeof buffer, std::chrono::microseconds(0), status, &
error);
1271 "GDBRemoteCommunicationServerLLGS::%s Stopping stdio "
1272 "forwarding as communication returned status %d (error: "
1274 __FUNCTION__, status,
error.AsCString());
1306 Expected<TraceStopRequest> stop_request =
1307 json::parse<TraceStopRequest>(packet.
Peek(),
"TraceStopRequest");
1327 Expected<TraceStartRequest> request =
1328 json::parse<TraceStartRequest>(packet.
Peek(),
"TraceStartRequest");
1348 Expected<TraceGetStateRequest> request =
1349 json::parse<TraceGetStateRequest>(packet.
Peek(),
"TraceGetStateRequest");
1366 llvm::Expected<TraceGetBinaryDataRequest> request =
1367 llvm::json::parse<TraceGetBinaryDataRequest>(packet.
Peek(),
1368 "TraceGetBinaryDataRequest");
1372 if (Expected<std::vector<uint8_t>> bytes =
1435 LLDB_LOG(log,
"No debugged process found.");
1441 LLDB_LOG(log,
"Killing process {0}", it->first);
1444 LLDB_LOG(log,
"Failed to kill debugged process {0}: {1}", it->first,
1465 "vKill failed to parse the process id");
1483 packet.
SetFilePos(::strlen(
"QSetDisableASLR:"));
1494 packet.
SetFilePos(::strlen(
"QSetWorkingDir:"));
1536 LLDB_LOG(log,
"process {0} cannot be resumed (state={1})", process.
GetID(),
1555 LLDB_LOGF(log,
"GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1560 "GDBRemoteCommunicationServerLLGS::%s no debugged process "
1572 const uint32_t signo =
1573 packet.
GetHexMaxU32(
false, std::numeric_limits<uint32_t>::max());
1574 if (signo == std::numeric_limits<uint32_t>::max())
1580 if (*packet.
Peek() ==
';')
1584 packet,
"unexpected content after $C{signal-number}");
1590 LLDB_LOG(log,
"process cannot be resumed (state={0})",
1611 static_cast<int>(signo)};
1615 resume_actions.
Append(action);
1621 LLDB_LOG(log,
"failed to send signal for process {0}: {1}",
1642 LLDB_LOGF(log,
"GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__);
1647 const bool has_continue_address = (packet.
GetBytesLeft() > 0);
1648 if (has_continue_address) {
1649 LLDB_LOG(log,
"not implemented for c[address] variant [{0} remains]",
1657 "GDBRemoteCommunicationServerLLGS::%s no debugged process "
1678 response.
Printf(
"vCont;c;C;s;S;t");
1699 LLDB_LOGF(log,
"GDBRemoteCommunicationServerLLGS::%s handling vCont packet",
1706 "GDBRemoteCommunicationServerLLGS::%s missing action from "
1712 if (::strcmp(packet.
Peek(),
";s") == 0) {
1718 std::unordered_map<lldb::pid_t, ResumeActionList> thread_actions;
1730 const char action = packet.
GetChar();
1734 if (thread_action.
signal == 0)
1736 packet,
"Could not parse signal in vCont packet C action");
1746 if (thread_action.
signal == 0)
1748 packet,
"Could not parse signal in vCont packet S action");
1779 pid = pid_tid->first;
1780 tid = pid_tid->second;
1786 packet,
"'t' action not supported for individual threads");
1792 LLDB_LOG(log,
"no process selected via Hc");
1801 thread_action.
tid = tid;
1806 packet,
"vCont: p-1 is not valid with a specific tid");
1808 thread_actions[process_it.first].Append(thread_action);
1810 thread_actions[pid].Append(thread_action);
1813 assert(thread_actions.size() >= 1);
1814 if (thread_actions.size() > 1 && !
m_non_stop)
1817 "Resuming multiple processes is supported in non-stop mode only");
1819 for (std::pair<lldb::pid_t, ResumeActionList> x : thread_actions) {
1822 LLDB_LOG(log,
"vCont failed for process {0}: process not debugged",
1839 assert(process_it->second.process_up);
1841 if (process_it->second.process_up->IsRunning()) {
1844 Status error = process_it->second.process_up->Interrupt();
1846 LLDB_LOG(log,
"vCont failed to halt process {0}: {1}", x.first,
1851 LLDB_LOG(log,
"halted process {0}", x.first);
1855 assert(thread_actions.size() == 1);
1871 LLDB_LOG(log,
"setting current thread id to {0}", tid);
1880 LLDB_LOG(log,
"setting continue thread id to {0}", tid);
1894 return x.front() !=
'W' && x.front() !=
'X';
1904 if (!stop_reply.
Empty())
1931 bool force_synchronous) {
1938 if (it.second.process_up->IsRunning())
1948 switch (process_state) {
1974 LLDB_LOG(log,
"pid {0}, current state reporting not handled: {1}",
1975 process.
GetID(), process_state);
2000 const uint32_t reg_index =
2001 packet.
GetHexMaxU32(
false, std::numeric_limits<uint32_t>::max());
2002 if (reg_index == std::numeric_limits<uint32_t>::max())
2033 if (!encoding.empty())
2034 response <<
"encoding:" << encoding <<
';';
2037 if (!format.empty())
2038 response <<
"format:" << format <<
';';
2040 const char *
const register_set_name =
2042 if (register_set_name)
2043 response <<
"set:" << register_set_name <<
';';
2045 if (reg_info->
kinds[RegisterKind::eRegisterKindEHFrame] !=
2047 response.
Printf(
"ehframe:%" PRIu32
";",
2048 reg_info->
kinds[RegisterKind::eRegisterKindEHFrame]);
2051 response.
Printf(
"dwarf:%" PRIu32
";",
2052 reg_info->
kinds[RegisterKind::eRegisterKindDWARF]);
2055 if (!kind_generic.empty())
2056 response <<
"generic:" << kind_generic <<
';';
2081 LLDB_LOG(log,
"iterating over threads of process {0}", process.
GetID());
2083 LLDB_LOG(log,
"iterated thread tid={0}", thread.GetID());
2084 response.
PutChar(had_any ?
',' :
'm');
2097 bool had_any =
false;
2126 LLDB_LOG(log,
"failed, no thread available");
2133 std::vector<uint8_t> regs_buffer;
2138 if (reg_info ==
nullptr) {
2139 LLDB_LOG(log,
"failed to get register info for register index {0}",
2150 LLDB_LOG(log,
"failed to read register at index {0}", reg_num);
2177 const uint32_t reg_index =
2178 packet.
GetHexMaxU32(
false, std::numeric_limits<uint32_t>::max());
2179 if (reg_index == std::numeric_limits<uint32_t>::max()) {
2181 "GDBRemoteCommunicationServerLLGS::%s failed, could not "
2182 "parse register number from request \"%s\"",
2190 LLDB_LOG(log,
"failed, no thread available");
2201 "GDBRemoteCommunicationServerLLGS::%s failed, requested "
2202 "register %" PRIu32
" beyond register count %" PRIu32,
2210 "GDBRemoteCommunicationServerLLGS::%s failed, requested "
2211 "register %" PRIu32
" returned NULL",
2212 __FUNCTION__, reg_index);
2224 "GDBRemoteCommunicationServerLLGS::%s failed, read of "
2225 "requested register %" PRIu32
" (%s) failed: %s",
2226 __FUNCTION__, reg_index, reg_info->
name,
error.AsCString());
2230 const uint8_t *
const data =
2231 static_cast<const uint8_t *
>(reg_value.
GetBytes());
2234 "GDBRemoteCommunicationServerLLGS::%s failed to get data "
2235 "bytes from requested register %" PRIu32,
2236 __FUNCTION__, reg_index);
2241 for (uint32_t i = 0; i < reg_value.
GetByteSize(); ++i)
2257 const uint32_t reg_index =
2258 packet.
GetHexMaxU32(
false, std::numeric_limits<uint32_t>::max());
2259 if (reg_index == std::numeric_limits<uint32_t>::max()) {
2261 "GDBRemoteCommunicationServerLLGS::%s failed, could not "
2262 "parse register number from request \"%s\"",
2270 packet,
"P packet missing '=' char after register number");
2279 "GDBRemoteCommunicationServerLLGS::%s failed, no thread "
2280 "available (thread index 0)",
2290 "GDBRemoteCommunicationServerLLGS::%s failed, requested "
2291 "register %" PRIu32
" returned NULL",
2292 __FUNCTION__, reg_index);
2300 "GDBRemoteCommunicationServerLLGS::%s failed, requested "
2301 "register %" PRIu32
" beyond register count %" PRIu32,
2317 "GDBRemoteCommunicationServerLLGS::%s failed, write of "
2318 "requested register %" PRIu32
" (%s) failed: %s",
2319 __FUNCTION__, reg_index, reg_info->
name,
error.AsCString());
2334 "GDBRemoteCommunicationServerLLGS::%s failed, H command "
2335 "missing {g,c} variant",
2340 const char h_variant = packet.
GetChar();
2342 switch (h_variant) {
2354 "GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c",
2355 __FUNCTION__, h_variant);
2357 "H variant unsupported, should be c or g");
2361 auto pid_tid = packet.
GetPidTid(default_process ? default_process->
GetID()
2365 inconvertibleErrorCode(),
"Malformed thread-id"));
2374 inconvertibleErrorCode(),
"No current process and no PID provided"));
2380 inconvertibleErrorCode(),
2381 llvm::formatv(
"No process with PID {0} debugged", pid)));
2387 new_process_it->second.process_up->GetThreadByID(tid);
2390 "GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64
2398 switch (h_variant) {
2410 assert(
false &&
"unsupported $H variant - shouldn't get here");
2412 "H variant unsupported, should be c or g");
2427 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2461 LLDB_LOG(log,
"failed, no process available");
2488 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2511 const uint64_t byte_count = packet.
GetHexMaxU64(
false, 0);
2512 if (byte_count == 0) {
2514 "GDBRemoteCommunicationServerLLGS::%s nothing to read: "
2515 "zero-length packet",
2521 std::string buf(byte_count,
'\0');
2526 size_t bytes_read = 0;
2528 read_addr, &buf[0], byte_count, bytes_read);
2531 "ReadMemoryWithoutTrap({0}) read {1} of {2} requested bytes (error: {3})",
2532 read_addr, byte_count, bytes_read,
error);
2533 if (bytes_read == 0)
2538 char kind = packet.
GetChar(
'?');
2542 assert(kind ==
'm');
2543 for (
size_t i = 0; i < bytes_read; ++i)
2558 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2573 Permissions perms = {};
2577 perms |= ePermissionsReadable;
2580 perms |= ePermissionsWritable;
2583 perms |= ePermissionsExecutable;
2607 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2635 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2658 const uint64_t byte_count = packet.
GetHexMaxU64(
false, 0);
2659 if (byte_count == 0) {
2660 LLDB_LOG(log,
"nothing to write: zero-length packet");
2667 packet,
"Comma sep missing in M packet after byte length");
2670 std::vector<uint8_t> buf(byte_count, 0);
2676 const uint64_t convert_count = packet.
GetHexBytes(buf, 0);
2677 if (convert_count != byte_count) {
2679 "pid {0} mem {1:x}: asked to write {2} bytes, but only found {3} "
2683 "not match hex-encoded content "
2688 size_t bytes_written = 0;
2692 LLDB_LOG(log,
"pid {0} mem {1:x}: failed to write. Error: {2}",
2697 if (bytes_written == 0) {
2698 LLDB_LOG(log,
"pid {0} mem {1:x}: wrote 0 of {2} requested bytes",
2722 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2749 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
2755 packet.
SetFilePos(strlen(
"qMemoryRegionInfo:"));
2776 response.
Printf(
"start:%" PRIx64
";size:%" PRIx64
";",
2825 LLDB_LOG(log,
"failed, no process available");
2833 packet,
"Too short Z packet, missing software/hardware specifier");
2835 bool want_breakpoint =
true;
2836 bool want_hardware =
false;
2837 uint32_t watch_flags = 0;
2841 switch (stoppoint_type) {
2843 want_hardware =
false;
2844 want_breakpoint =
true;
2847 want_hardware =
true;
2848 want_breakpoint =
true;
2852 want_hardware =
true;
2853 want_breakpoint =
false;
2857 want_hardware =
true;
2858 want_breakpoint =
false;
2862 want_hardware =
true;
2863 want_breakpoint =
false;
2867 packet,
"Z packet had invalid software/hardware specifier");
2872 packet,
"Malformed Z packet, expecting comma after stoppoint type");
2881 packet,
"Malformed Z packet, expecting comma after address");
2884 const uint32_t size =
2885 packet.
GetHexMaxU32(
false, std::numeric_limits<uint32_t>::max());
2886 if (size == std::numeric_limits<uint32_t>::max())
2888 packet,
"Malformed Z packet, failed to parse size argument");
2890 if (want_breakpoint) {
2894 if (
error.Success())
2897 LLDB_LOG(log,
"pid {0} failed to set breakpoint: {1}",
2903 addr, size, watch_flags, want_hardware);
2904 if (
error.Success())
2907 LLDB_LOG(log,
"pid {0} failed to set watchpoint: {1}",
2919 LLDB_LOG(log,
"failed, no process available");
2927 packet,
"Too short z packet, missing software/hardware specifier");
2929 bool want_breakpoint =
true;
2930 bool want_hardware =
false;
2934 switch (stoppoint_type) {
2936 want_breakpoint =
true;
2937 want_hardware =
true;
2940 want_breakpoint =
true;
2943 want_breakpoint =
false;
2946 want_breakpoint =
false;
2949 want_breakpoint =
false;
2953 packet,
"z packet had invalid software/hardware specifier");
2958 packet,
"Malformed z packet, expecting comma after stoppoint type");
2967 packet,
"Malformed z packet, expecting comma after address");
2978 if (want_breakpoint) {
2982 if (
error.Success())
2985 LLDB_LOG(log,
"pid {0} failed to remove breakpoint: {1}",
2991 if (
error.Success())
2994 LLDB_LOG(log,
"pid {0} failed to remove watchpoint: {1}",
3009 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
3047llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
3052 return llvm::createStringError(llvm::inconvertibleErrorCode(),
3053 "No thread available");
3061 response.
Printf(
"<?xml version=\"1.0\"?>\n");
3062 response.
Printf(
"<target version=\"1.0\">\n");
3066 response.
Printf(
"<architecture>%s</architecture>\n",
3073 response.
Indent(
"<feature>\n");
3076 if (registers_count)
3079 llvm::StringSet<> field_enums_seen;
3080 for (
int reg_index = 0; reg_index < registers_count; reg_index++) {
3086 "%s failed to get register info for register index %" PRIu32,
3087 "target.xml", reg_index);
3099 response.
Printf(
"<reg name=\"%s\" bitsize=\"%" PRIu32
3100 "\" regnum=\"%d\" ",
3110 if (!encoding.empty())
3111 response <<
"encoding=\"" << encoding <<
"\" ";
3114 if (!format.empty())
3115 response <<
"format=\"" << format <<
"\" ";
3120 const char *
const register_set_name =
3122 if (register_set_name)
3123 response <<
"group=\"" << register_set_name <<
"\" ";
3125 if (reg_info->
kinds[RegisterKind::eRegisterKindEHFrame] !=
3127 response.
Printf(
"ehframe_regnum=\"%" PRIu32
"\" ",
3128 reg_info->
kinds[RegisterKind::eRegisterKindEHFrame]);
3130 if (reg_info->
kinds[RegisterKind::eRegisterKindDWARF] !=
3132 response.
Printf(
"dwarf_regnum=\"%" PRIu32
"\" ",
3133 reg_info->
kinds[RegisterKind::eRegisterKindDWARF]);
3136 if (!kind_generic.empty())
3137 response <<
"generic=\"" << kind_generic <<
"\" ";
3147 response.
PutCString(
"invalidate_regnums=\"");
3155 if (registers_count)
3158 response.
Indent(
"</feature>\n");
3160 response.
Indent(
"</target>\n");
3161 return MemoryBuffer::getMemBufferCopy(response.
GetString(),
"target.xml");
3164llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>>
3166 llvm::StringRef annex) {
3170 return llvm::createStringError(llvm::inconvertibleErrorCode(),
3171 "No process available");
3174 if (
object ==
"auxv") {
3177 if (!buffer_or_error)
3178 return llvm::errorCodeToError(buffer_or_error.getError());
3179 return std::move(*buffer_or_error);
3182 if (
object ==
"siginfo") {
3185 return llvm::createStringError(llvm::inconvertibleErrorCode(),
3186 "no current thread");
3189 if (!buffer_or_error)
3190 return buffer_or_error.takeError();
3191 return std::move(*buffer_or_error);
3194 if (
object ==
"libraries-svr4") {
3197 return library_list.takeError();
3200 response.
Printf(
"<library-list-svr4 version=\"1.0\">");
3201 for (
auto const &library : *library_list) {
3202 response.
Printf(
"<library name=\"%s\" ",
3204 response.
Printf(
"lm=\"0x%" PRIx64
"\" ", library.link_map);
3205 response.
Printf(
"l_addr=\"0x%" PRIx64
"\" ", library.base_addr);
3206 response.
Printf(
"l_ld=\"0x%" PRIx64
"\" />", library.ld_addr);
3208 response.
Printf(
"</library-list-svr4>");
3209 return MemoryBuffer::getMemBufferCopy(response.
GetString(), __FUNCTION__);
3212 if (
object ==
"features" && annex ==
"target.xml")
3215 return llvm::make_error<UnimplementedError>();
3221 SmallVector<StringRef, 5> fields;
3223 StringRef(packet.
GetStringRef()).split(fields,
':', 4);
3224 if (fields.size() != 5)
3226 StringRef &xfer_object = fields[1];
3227 StringRef &xfer_action = fields[2];
3228 StringRef &xfer_annex = fields[3];
3230 if (xfer_action !=
"read")
3233 const uint64_t xfer_offset =
3234 offset_data.
GetHexMaxU64(
false, std::numeric_limits<uint64_t>::max());
3235 if (xfer_offset == std::numeric_limits<uint64_t>::max())
3238 if (offset_data.
GetChar() !=
',')
3240 "qXfer packet missing comma after offset");
3242 const uint64_t xfer_length =
3243 offset_data.
GetHexMaxU64(
false, std::numeric_limits<uint64_t>::max());
3244 if (xfer_length == std::numeric_limits<uint64_t>::max())
3248 std::string buffer_key = (xfer_object + xfer_action + xfer_annex).str();
3255 .insert(std::make_pair(buffer_key, std::move(*buffer_up)))
3261 bool done_with_buffer =
false;
3262 llvm::StringRef buffer = buffer_it->second->getBuffer();
3263 if (xfer_offset >= buffer.size()) {
3266 done_with_buffer =
true;
3269 buffer = buffer.drop_front(xfer_offset);
3272 if (xfer_length >= buffer.size()) {
3275 done_with_buffer =
true;
3279 buffer = buffer.take_front(xfer_length);
3285 if (done_with_buffer)
3297 packet.
SetFilePos(strlen(
"QSaveRegisterState"));
3304 packet,
"No thread specified in QSaveRegisterState packet");
3307 "No thread was is set with the Hg packet");
3317 LLDB_LOG(log,
"pid {0} failed to save all register values: {1}",
3325 "GetNextRegisterSaveID() returned an existing register save id");
3335 response.
Printf(
"%" PRIu32, save_id);
3345 packet.
SetFilePos(strlen(
"QRestoreRegisterState:"));
3348 packet,
"QRestoreRegisterState packet missing register save id");
3350 const uint32_t save_id = packet.
GetU32(0);
3352 LLDB_LOG(log,
"QRestoreRegisterState packet has malformed save id, "
3353 "expecting decimal uint32_t");
3362 packet,
"No thread specified in QRestoreRegisterState packet");
3365 "No thread was is set with the Hg packet");
3380 "pid {0} does not have a register set save buffer for id {1}",
3384 register_data_sp = it->second;
3392 LLDB_LOG(log,
"pid {0} failed to restore all register values: {1}",
3414 "vAttach failed to parse the process id");
3418 "GDBRemoteCommunicationServerLLGS::%s attempting to attach to "
3426 "GDBRemoteCommunicationServerLLGS::%s failed to attach to "
3427 "pid %" PRIu64
": %s\n",
3428 __FUNCTION__, pid,
error.AsCString());
3451 std::string process_name;
3454 "vAttachWait failed to parse process name");
3456 LLDB_LOG(log,
"attempting to attach to process named '{0}'", process_name);
3460 LLDB_LOG(log,
"failed to attach to process named '{0}': {1}", process_name,
3490 std::string process_name;
3493 "vAttachOrWait failed to parse process name");
3495 LLDB_LOG(log,
"attempting to attach to process named '{0}'", process_name);
3499 LLDB_LOG(log,
"failed to attach to process named '{0}': {1}", process_name,
3517 if (!s.consume_front(
"vRun;"))
3520 llvm::SmallVector<llvm::StringRef, 16> argv;
3523 for (llvm::StringRef hex_arg : argv) {
3528 LLDB_LOGF(log,
"LLGSPacketHandler::%s added arg: \"%s\"", __FUNCTION__,
3567 llvm::Error detach_error = llvm::Error::success();
3568 bool detached =
false;
3573 "GDBRemoteCommunicationServerLLGS::%s detaching %" PRId64,
3574 __FUNCTION__, it->first);
3575 if (llvm::Error e = it->second.process_up->Detach().ToError())
3576 detach_error = llvm::joinErrors(std::move(detach_error), std::move(e));
3607 packet.
SetFilePos(strlen(
"qThreadStopInfo"));
3611 "GDBRemoteCommunicationServerLLGS::%s failed, could not "
3612 "parse thread id from request \"%s\"",
3632 const bool threads_with_valid_stop_info_only =
false;
3633 llvm::Expected<json::Value> threads_info =
3635 if (!threads_info) {
3637 "failed to prepare a packet for pid {1}: {0}",
3656 packet.
SetFilePos(strlen(
"qWatchpointSupportInfo"));
3665 if (hw_debug_cap == std::nullopt)
3666 response.
Printf(
"num:0;");
3668 response.
Printf(
"num:%d;", hw_debug_cap->second);
3681 packet.
SetFilePos(strlen(
"qFileLoadAddress:"));
3685 std::string file_name;
3698 response.
PutHex64(file_load_address);
3705 std::vector<int> signals;
3711 int signal = packet.
GetS32(-1, 16);
3714 signals.push_back(signal);
3717 char separator = packet.
GetChar();
3718 if (separator ==
'\0')
3720 if (separator !=
';')
3722 " expected semicolon.");
3746 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
3756 const char *current_char = packet.
Peek();
3757 if (!current_char || *current_char ==
',')
3762 char previous_char = packet.
GetChar();
3763 current_char = packet.
Peek();
3765 if (previous_char !=
',' || (current_char && *current_char ==
':'))
3767 "Invalid addr,length pair in qMemTags packet");
3771 packet,
"Too short qMemtags: packet (looking for length)");
3775 const char *invalid_type_err =
"Invalid type field in qMemTags: packet";
3781 const char *first_type_char = packet.
Peek();
3782 if (first_type_char && (*first_type_char ==
'+' || *first_type_char ==
'-'))
3789 packet.
GetU64(std::numeric_limits<uint64_t>::max(), 16);
3792 raw_type > std::numeric_limits<uint32_t>::max() ||
3801 uint32_t raw_type_32 = raw_type;
3802 int32_t type =
reinterpret_cast<int32_t &
>(raw_type_32);
3805 std::vector<uint8_t> tags;
3827 "GDBRemoteCommunicationServerLLGS::%s failed, no process available",
3837 const char *current_char = packet.
Peek();
3838 if (!current_char || *current_char ==
',')
3843 char previous_char = packet.
GetChar();
3844 current_char = packet.
Peek();
3846 if (previous_char !=
',' || (current_char && *current_char ==
':'))
3848 "Invalid addr,length pair in QMemTags packet");
3852 packet,
"Too short QMemtags: packet (looking for length)");
3856 const char *invalid_type_err =
"Invalid type field in QMemTags: packet";
3861 const char *first_type_char = packet.
Peek();
3862 if (first_type_char && (*first_type_char ==
'+' || *first_type_char ==
'-'))
3870 packet.
GetU64(std::numeric_limits<uint64_t>::max(), 16);
3871 if (raw_type > std::numeric_limits<uint32_t>::max())
3876 uint32_t raw_type_32 = raw_type;
3877 int32_t type =
reinterpret_cast<int32_t &
>(raw_type_32);
3882 "Missing tag data in QMemTags: packet");
3885 const char *invalid_data_err =
"Invalid tag data in QMemTags: packet";
3892 std::vector<uint8_t> tag_data;
3897 tag_data.resize(byte_count);
3898 size_t converted_bytes = packet.
GetHexBytes(tag_data, 0);
3899 if (converted_bytes != byte_count) {
3917 std::string path_hint;
3920 assert(packet_str.starts_with(
"qSaveCore"));
3921 if (packet_str.consume_front(
"qSaveCore;")) {
3922 for (
auto x : llvm::split(packet_str,
';')) {
3923 if (x.consume_front(
"path-hint:"))
3947 assert(packet_str.starts_with(
"QNonStop:"));
3948 packet_str.consume_front(
"QNonStop:");
3949 if (packet_str ==
"0") {
3953 if (process_it.second.process_up->IsRunning()) {
3955 Status error = process_it.second.process_up->Interrupt();
3958 "while disabling nonstop, failed to halt process {0}: {1}",
3959 process_it.first,
error);
3973 }
else if (packet_str ==
"1") {
3985 std::deque<std::string> &queue) {
4029 return interrupt_res;
4041 inconvertibleErrorCode(),
"Malformed thread-id"));
4050 inconvertibleErrorCode(),
"No current process and no PID provided"));
4058 if (!new_process_it->second.process_up->GetThreadByID(tid))
4072 connection->Disconnect(&
error);
4074 if (
error.Success()) {
4076 "GDBRemoteCommunicationServerLLGS::%s disconnect process "
4077 "terminal stdio - SUCCESS",
4081 "GDBRemoteCommunicationServerLLGS::%s disconnect process "
4082 "terminal stdio - FAIL: %s",
4083 __FUNCTION__,
error.AsCString());
4102 else if (current_tid == 0) {
4114 "GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
4115 "error: expected ';' prior to start of thread suffix: packet "
4125 if (strncmp(packet.
Peek(),
"thread:", strlen(
"thread:")) != 0) {
4127 "GDBRemoteCommunicationServerLLGS::%s gdb-remote parse "
4128 "error: expected 'thread:' but not found, packet contents = "
4172 ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec)
4183 llvm::StringRef value) {
4185 for (
const char &c : value) {
4208 const llvm::ArrayRef<llvm::StringRef> client_features) {
4209 std::vector<std::string> ret =
4211 ret.insert(ret.end(), {
4212 "QThreadSuffixSupported+",
4213 "QListThreadsInStopReply+",
4214 "qXfer:features:read+",
4221 if (
bool(plugin_features & Extension::pass_signals))
4222 ret.push_back(
"QPassSignals+");
4223 if (
bool(plugin_features & Extension::auxv))
4224 ret.push_back(
"qXfer:auxv:read+");
4225 if (
bool(plugin_features & Extension::libraries_svr4))
4226 ret.push_back(
"qXfer:libraries-svr4:read+");
4227 if (
bool(plugin_features & Extension::siginfo_read))
4228 ret.push_back(
"qXfer:siginfo:read+");
4229 if (
bool(plugin_features & Extension::memory_tagging))
4230 ret.push_back(
"memory-tagging+");
4231 if (
bool(plugin_features & Extension::savecore))
4232 ret.push_back(
"qSaveCore+");
4236 for (llvm::StringRef x : client_features)
4238 llvm::StringSwitch<Extension>(x)
4239 .Case(
"multiprocess+", Extension::multiprocess)
4240 .Case(
"fork-events+", Extension::fork)
4241 .Case(
"vfork-events+", Extension::vfork)
4256 ret.push_back(
"multiprocess+");
4258 ret.push_back(
"fork-events+");
4260 ret.push_back(
"vfork-events+");
4286 response.
Format(
"p{0:x-}.", pid);
4287 response.
Format(
"{0:x-}", tid);
4292 bool reverse_connect) {
4294 if (std::optional<URI> url =
URI::Parse(url_arg)) {
4295 if (reverse_connect)
4296 return url_arg.str();
4301 std::string new_url = llvm::StringSwitch<std::string>(url->scheme)
4302 .Case(
"tcp",
"listen")
4303 .Case(
"unix",
"unix-accept")
4304 .Case(
"unix-abstract",
"unix-abstract-accept")
4305 .Default(url->scheme.str());
4306 llvm::append_range(new_url, url_arg.substr(url->scheme.size()));
4310 std::string host_port = url_arg.str();
4313 if (url_arg.starts_with(
":"))
4314 host_port.insert(0,
"localhost");
4318 return (reverse_connect ?
"connect://" :
"listen://") + host_port;
4321 return (reverse_connect ?
"unix-connect://" :
"unix-accept://") +
static const size_t reg_size
static llvm::raw_ostream & error(Stream &strm)
static llvm::StringRef GetEncodingNameOrEmpty(const RegisterInfo ®_info)
static llvm::StringRef GetFormatNameOrEmpty(const RegisterInfo ®_info)
static void WriteRegisterValueInHexFixedWidth(StreamString &response, NativeRegisterContext ®_ctx, const RegisterInfo ®_info, const RegisterValue *reg_value_p, lldb::ByteOrder byte_order)
static void AppendHexValue(StreamString &response, const uint8_t *buf, uint32_t buf_size, bool swap)
static std::optional< json::Object > GetRegistersAsJSON(NativeThreadProtocol &thread)
static const char * GetStopReasonString(StopReason stop_reason)
static void CollectRegNums(const uint32_t *reg_num, StreamString &response, bool usehex)
static bool ResumeActionListStopsAllThreads(ResumeActionList &actions)
static llvm::StringRef GetKindGenericOrEmpty(const RegisterInfo ®_info)
static llvm::Expected< json::Array > GetJSONThreadsInfo(NativeProcessProtocol &process, bool abridged)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOGF(log,...)
#define LLDB_LOG_ERROR(log, error,...)