1*dda28197Spatrick //===-- GDBRemoteCommunicationServerLLGS.cpp ------------------------------===// 2061da546Spatrick // 3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information. 5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6061da546Spatrick // 7061da546Spatrick //===----------------------------------------------------------------------===// 8061da546Spatrick 9061da546Spatrick #include <errno.h> 10061da546Spatrick 11061da546Spatrick #include "lldb/Host/Config.h" 12061da546Spatrick 13061da546Spatrick #include "GDBRemoteCommunicationServerLLGS.h" 14061da546Spatrick #include "lldb/Utility/GDBRemote.h" 15061da546Spatrick 16061da546Spatrick #include <chrono> 17061da546Spatrick #include <cstring> 18061da546Spatrick #include <thread> 19061da546Spatrick 20061da546Spatrick #include "lldb/Host/ConnectionFileDescriptor.h" 21061da546Spatrick #include "lldb/Host/Debug.h" 22061da546Spatrick #include "lldb/Host/File.h" 23061da546Spatrick #include "lldb/Host/FileAction.h" 24061da546Spatrick #include "lldb/Host/FileSystem.h" 25061da546Spatrick #include "lldb/Host/Host.h" 26061da546Spatrick #include "lldb/Host/HostInfo.h" 27061da546Spatrick #include "lldb/Host/PosixApi.h" 28061da546Spatrick #include "lldb/Host/common/NativeProcessProtocol.h" 29061da546Spatrick #include "lldb/Host/common/NativeRegisterContext.h" 30061da546Spatrick #include "lldb/Host/common/NativeThreadProtocol.h" 31061da546Spatrick #include "lldb/Target/MemoryRegionInfo.h" 32061da546Spatrick #include "lldb/Utility/Args.h" 33061da546Spatrick #include "lldb/Utility/DataBuffer.h" 34061da546Spatrick #include "lldb/Utility/Endian.h" 35061da546Spatrick #include "lldb/Utility/LLDBAssert.h" 36061da546Spatrick #include "lldb/Utility/Log.h" 37061da546Spatrick #include "lldb/Utility/RegisterValue.h" 38061da546Spatrick #include "lldb/Utility/State.h" 39061da546Spatrick #include "lldb/Utility/StreamString.h" 40061da546Spatrick #include "lldb/Utility/UriParser.h" 41061da546Spatrick #include "llvm/ADT/Triple.h" 42061da546Spatrick #include "llvm/Support/JSON.h" 43061da546Spatrick #include "llvm/Support/ScopedPrinter.h" 44061da546Spatrick 45061da546Spatrick #include "ProcessGDBRemote.h" 46061da546Spatrick #include "ProcessGDBRemoteLog.h" 47061da546Spatrick #include "lldb/Utility/StringExtractorGDBRemote.h" 48061da546Spatrick 49061da546Spatrick using namespace lldb; 50061da546Spatrick using namespace lldb_private; 51061da546Spatrick using namespace lldb_private::process_gdb_remote; 52061da546Spatrick using namespace llvm; 53061da546Spatrick 54061da546Spatrick // GDBRemote Errors 55061da546Spatrick 56061da546Spatrick namespace { 57061da546Spatrick enum GDBRemoteServerError { 58061da546Spatrick // Set to the first unused error number in literal form below 59061da546Spatrick eErrorFirst = 29, 60061da546Spatrick eErrorNoProcess = eErrorFirst, 61061da546Spatrick eErrorResume, 62061da546Spatrick eErrorExitStatus 63061da546Spatrick }; 64061da546Spatrick } 65061da546Spatrick 66061da546Spatrick // GDBRemoteCommunicationServerLLGS constructor 67061da546Spatrick GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS( 68061da546Spatrick MainLoop &mainloop, const NativeProcessProtocol::Factory &process_factory) 69061da546Spatrick : GDBRemoteCommunicationServerCommon("gdb-remote.server", 70061da546Spatrick "gdb-remote.server.rx_packet"), 71061da546Spatrick m_mainloop(mainloop), m_process_factory(process_factory), 72061da546Spatrick m_stdio_communication("process.stdio") { 73061da546Spatrick RegisterPacketHandlers(); 74061da546Spatrick } 75061da546Spatrick 76061da546Spatrick void GDBRemoteCommunicationServerLLGS::RegisterPacketHandlers() { 77061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_C, 78061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_C); 79061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_c, 80061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_c); 81061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_D, 82061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_D); 83061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_H, 84061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_H); 85061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_I, 86061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_I); 87061da546Spatrick RegisterMemberFunctionHandler( 88061da546Spatrick StringExtractorGDBRemote::eServerPacketType_interrupt, 89061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_interrupt); 90061da546Spatrick RegisterMemberFunctionHandler( 91061da546Spatrick StringExtractorGDBRemote::eServerPacketType_m, 92061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_memory_read); 93061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_M, 94061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_M); 95061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_p, 96061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_p); 97061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_P, 98061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_P); 99061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qC, 100061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qC); 101061da546Spatrick RegisterMemberFunctionHandler( 102061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qfThreadInfo, 103061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo); 104061da546Spatrick RegisterMemberFunctionHandler( 105061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qFileLoadAddress, 106061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress); 107061da546Spatrick RegisterMemberFunctionHandler( 108061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir, 109061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir); 110061da546Spatrick RegisterMemberFunctionHandler( 111061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfo, 112061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo); 113061da546Spatrick RegisterMemberFunctionHandler( 114061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfoSupported, 115061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported); 116061da546Spatrick RegisterMemberFunctionHandler( 117061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qProcessInfo, 118061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo); 119061da546Spatrick RegisterMemberFunctionHandler( 120061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qRegisterInfo, 121061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo); 122061da546Spatrick RegisterMemberFunctionHandler( 123061da546Spatrick StringExtractorGDBRemote::eServerPacketType_QRestoreRegisterState, 124061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState); 125061da546Spatrick RegisterMemberFunctionHandler( 126061da546Spatrick StringExtractorGDBRemote::eServerPacketType_QSaveRegisterState, 127061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState); 128061da546Spatrick RegisterMemberFunctionHandler( 129061da546Spatrick StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR, 130061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR); 131061da546Spatrick RegisterMemberFunctionHandler( 132061da546Spatrick StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir, 133061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir); 134061da546Spatrick RegisterMemberFunctionHandler( 135061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qsThreadInfo, 136061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo); 137061da546Spatrick RegisterMemberFunctionHandler( 138061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qThreadStopInfo, 139061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo); 140061da546Spatrick RegisterMemberFunctionHandler( 141061da546Spatrick StringExtractorGDBRemote::eServerPacketType_jThreadsInfo, 142061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo); 143061da546Spatrick RegisterMemberFunctionHandler( 144061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qWatchpointSupportInfo, 145061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo); 146061da546Spatrick RegisterMemberFunctionHandler( 147061da546Spatrick StringExtractorGDBRemote::eServerPacketType_qXfer, 148061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_qXfer); 149061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_s, 150061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_s); 151061da546Spatrick RegisterMemberFunctionHandler( 152061da546Spatrick StringExtractorGDBRemote::eServerPacketType_stop_reason, 153061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_stop_reason); // ? 154061da546Spatrick RegisterMemberFunctionHandler( 155061da546Spatrick StringExtractorGDBRemote::eServerPacketType_vAttach, 156061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_vAttach); 157061da546Spatrick RegisterMemberFunctionHandler( 158061da546Spatrick StringExtractorGDBRemote::eServerPacketType_vCont, 159061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_vCont); 160061da546Spatrick RegisterMemberFunctionHandler( 161061da546Spatrick StringExtractorGDBRemote::eServerPacketType_vCont_actions, 162061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_vCont_actions); 163061da546Spatrick RegisterMemberFunctionHandler( 164061da546Spatrick StringExtractorGDBRemote::eServerPacketType_x, 165061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_memory_read); 166061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_Z, 167061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_Z); 168061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_z, 169061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_z); 170061da546Spatrick RegisterMemberFunctionHandler( 171061da546Spatrick StringExtractorGDBRemote::eServerPacketType_QPassSignals, 172061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_QPassSignals); 173061da546Spatrick 174061da546Spatrick RegisterMemberFunctionHandler( 175061da546Spatrick StringExtractorGDBRemote::eServerPacketType_jTraceStart, 176061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_jTraceStart); 177061da546Spatrick RegisterMemberFunctionHandler( 178061da546Spatrick StringExtractorGDBRemote::eServerPacketType_jTraceBufferRead, 179061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead); 180061da546Spatrick RegisterMemberFunctionHandler( 181061da546Spatrick StringExtractorGDBRemote::eServerPacketType_jTraceMetaRead, 182061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_jTraceRead); 183061da546Spatrick RegisterMemberFunctionHandler( 184061da546Spatrick StringExtractorGDBRemote::eServerPacketType_jTraceStop, 185061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_jTraceStop); 186061da546Spatrick RegisterMemberFunctionHandler( 187061da546Spatrick StringExtractorGDBRemote::eServerPacketType_jTraceConfigRead, 188061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead); 189061da546Spatrick 190061da546Spatrick RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_g, 191061da546Spatrick &GDBRemoteCommunicationServerLLGS::Handle_g); 192061da546Spatrick 193061da546Spatrick RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_k, 194061da546Spatrick [this](StringExtractorGDBRemote packet, Status &error, 195061da546Spatrick bool &interrupt, bool &quit) { 196061da546Spatrick quit = true; 197061da546Spatrick return this->Handle_k(packet); 198061da546Spatrick }); 199061da546Spatrick } 200061da546Spatrick 201061da546Spatrick void GDBRemoteCommunicationServerLLGS::SetLaunchInfo(const ProcessLaunchInfo &info) { 202061da546Spatrick m_process_launch_info = info; 203061da546Spatrick } 204061da546Spatrick 205061da546Spatrick Status GDBRemoteCommunicationServerLLGS::LaunchProcess() { 206061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 207061da546Spatrick 208061da546Spatrick if (!m_process_launch_info.GetArguments().GetArgumentCount()) 209061da546Spatrick return Status("%s: no process command line specified to launch", 210061da546Spatrick __FUNCTION__); 211061da546Spatrick 212061da546Spatrick const bool should_forward_stdio = 213061da546Spatrick m_process_launch_info.GetFileActionForFD(STDIN_FILENO) == nullptr || 214061da546Spatrick m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr || 215061da546Spatrick m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr; 216061da546Spatrick m_process_launch_info.SetLaunchInSeparateProcessGroup(true); 217061da546Spatrick m_process_launch_info.GetFlags().Set(eLaunchFlagDebug); 218061da546Spatrick 219061da546Spatrick if (should_forward_stdio) { 220061da546Spatrick // Temporarily relax the following for Windows until we can take advantage 221061da546Spatrick // of the recently added pty support. This doesn't really affect the use of 222061da546Spatrick // lldb-server on Windows. 223061da546Spatrick #if !defined(_WIN32) 224061da546Spatrick if (llvm::Error Err = m_process_launch_info.SetUpPtyRedirection()) 225061da546Spatrick return Status(std::move(Err)); 226061da546Spatrick #endif 227061da546Spatrick } 228061da546Spatrick 229061da546Spatrick { 230061da546Spatrick std::lock_guard<std::recursive_mutex> guard(m_debugged_process_mutex); 231061da546Spatrick assert(!m_debugged_process_up && "lldb-server creating debugged " 232061da546Spatrick "process but one already exists"); 233061da546Spatrick auto process_or = 234061da546Spatrick m_process_factory.Launch(m_process_launch_info, *this, m_mainloop); 235061da546Spatrick if (!process_or) 236061da546Spatrick return Status(process_or.takeError()); 237061da546Spatrick m_debugged_process_up = std::move(*process_or); 238061da546Spatrick } 239061da546Spatrick 240061da546Spatrick // Handle mirroring of inferior stdout/stderr over the gdb-remote protocol as 241061da546Spatrick // needed. llgs local-process debugging may specify PTY paths, which will 242061da546Spatrick // make these file actions non-null process launch -i/e/o will also make 243061da546Spatrick // these file actions non-null nullptr means that the traffic is expected to 244061da546Spatrick // flow over gdb-remote protocol 245061da546Spatrick if (should_forward_stdio) { 246061da546Spatrick // nullptr means it's not redirected to file or pty (in case of LLGS local) 247061da546Spatrick // at least one of stdio will be transferred pty<->gdb-remote we need to 248061da546Spatrick // give the pty master handle to this object to read and/or write 249061da546Spatrick LLDB_LOG(log, 250061da546Spatrick "pid = {0}: setting up stdout/stderr redirection via $O " 251061da546Spatrick "gdb-remote commands", 252061da546Spatrick m_debugged_process_up->GetID()); 253061da546Spatrick 254061da546Spatrick // Setup stdout/stderr mapping from inferior to $O 255061da546Spatrick auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor(); 256061da546Spatrick if (terminal_fd >= 0) { 257061da546Spatrick LLDB_LOGF(log, 258061da546Spatrick "ProcessGDBRemoteCommunicationServerLLGS::%s setting " 259061da546Spatrick "inferior STDIO fd to %d", 260061da546Spatrick __FUNCTION__, terminal_fd); 261061da546Spatrick Status status = SetSTDIOFileDescriptor(terminal_fd); 262061da546Spatrick if (status.Fail()) 263061da546Spatrick return status; 264061da546Spatrick } else { 265061da546Spatrick LLDB_LOGF(log, 266061da546Spatrick "ProcessGDBRemoteCommunicationServerLLGS::%s ignoring " 267061da546Spatrick "inferior STDIO since terminal fd reported as %d", 268061da546Spatrick __FUNCTION__, terminal_fd); 269061da546Spatrick } 270061da546Spatrick } else { 271061da546Spatrick LLDB_LOG(log, 272061da546Spatrick "pid = {0} skipping stdout/stderr redirection via $O: inferior " 273061da546Spatrick "will communicate over client-provided file descriptors", 274061da546Spatrick m_debugged_process_up->GetID()); 275061da546Spatrick } 276061da546Spatrick 277061da546Spatrick printf("Launched '%s' as process %" PRIu64 "...\n", 278061da546Spatrick m_process_launch_info.GetArguments().GetArgumentAtIndex(0), 279061da546Spatrick m_debugged_process_up->GetID()); 280061da546Spatrick 281061da546Spatrick return Status(); 282061da546Spatrick } 283061da546Spatrick 284061da546Spatrick Status GDBRemoteCommunicationServerLLGS::AttachToProcess(lldb::pid_t pid) { 285061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 286061da546Spatrick LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64, 287061da546Spatrick __FUNCTION__, pid); 288061da546Spatrick 289061da546Spatrick // Before we try to attach, make sure we aren't already monitoring something 290061da546Spatrick // else. 291061da546Spatrick if (m_debugged_process_up && 292061da546Spatrick m_debugged_process_up->GetID() != LLDB_INVALID_PROCESS_ID) 293061da546Spatrick return Status("cannot attach to process %" PRIu64 294061da546Spatrick " when another process with pid %" PRIu64 295061da546Spatrick " is being debugged.", 296061da546Spatrick pid, m_debugged_process_up->GetID()); 297061da546Spatrick 298061da546Spatrick // Try to attach. 299061da546Spatrick auto process_or = m_process_factory.Attach(pid, *this, m_mainloop); 300061da546Spatrick if (!process_or) { 301061da546Spatrick Status status(process_or.takeError()); 302061da546Spatrick llvm::errs() << llvm::formatv("failed to attach to process {0}: {1}", pid, 303061da546Spatrick status); 304061da546Spatrick return status; 305061da546Spatrick } 306061da546Spatrick m_debugged_process_up = std::move(*process_or); 307061da546Spatrick 308061da546Spatrick // Setup stdout/stderr mapping from inferior. 309061da546Spatrick auto terminal_fd = m_debugged_process_up->GetTerminalFileDescriptor(); 310061da546Spatrick if (terminal_fd >= 0) { 311061da546Spatrick LLDB_LOGF(log, 312061da546Spatrick "ProcessGDBRemoteCommunicationServerLLGS::%s setting " 313061da546Spatrick "inferior STDIO fd to %d", 314061da546Spatrick __FUNCTION__, terminal_fd); 315061da546Spatrick Status status = SetSTDIOFileDescriptor(terminal_fd); 316061da546Spatrick if (status.Fail()) 317061da546Spatrick return status; 318061da546Spatrick } else { 319061da546Spatrick LLDB_LOGF(log, 320061da546Spatrick "ProcessGDBRemoteCommunicationServerLLGS::%s ignoring " 321061da546Spatrick "inferior STDIO since terminal fd reported as %d", 322061da546Spatrick __FUNCTION__, terminal_fd); 323061da546Spatrick } 324061da546Spatrick 325061da546Spatrick printf("Attached to process %" PRIu64 "...\n", pid); 326061da546Spatrick return Status(); 327061da546Spatrick } 328061da546Spatrick 329061da546Spatrick void GDBRemoteCommunicationServerLLGS::InitializeDelegate( 330061da546Spatrick NativeProcessProtocol *process) { 331061da546Spatrick assert(process && "process cannot be NULL"); 332061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 333061da546Spatrick if (log) { 334061da546Spatrick LLDB_LOGF(log, 335061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s called with " 336061da546Spatrick "NativeProcessProtocol pid %" PRIu64 ", current state: %s", 337061da546Spatrick __FUNCTION__, process->GetID(), 338061da546Spatrick StateAsCString(process->GetState())); 339061da546Spatrick } 340061da546Spatrick } 341061da546Spatrick 342061da546Spatrick GDBRemoteCommunication::PacketResult 343061da546Spatrick GDBRemoteCommunicationServerLLGS::SendWResponse( 344061da546Spatrick NativeProcessProtocol *process) { 345061da546Spatrick assert(process && "process cannot be NULL"); 346061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 347061da546Spatrick 348061da546Spatrick // send W notification 349061da546Spatrick auto wait_status = process->GetExitStatus(); 350061da546Spatrick if (!wait_status) { 351061da546Spatrick LLDB_LOG(log, "pid = {0}, failed to retrieve process exit status", 352061da546Spatrick process->GetID()); 353061da546Spatrick 354061da546Spatrick StreamGDBRemote response; 355061da546Spatrick response.PutChar('E'); 356061da546Spatrick response.PutHex8(GDBRemoteServerError::eErrorExitStatus); 357061da546Spatrick return SendPacketNoLock(response.GetString()); 358061da546Spatrick } 359061da546Spatrick 360061da546Spatrick LLDB_LOG(log, "pid = {0}, returning exit type {1}", process->GetID(), 361061da546Spatrick *wait_status); 362061da546Spatrick 363061da546Spatrick StreamGDBRemote response; 364061da546Spatrick response.Format("{0:g}", *wait_status); 365061da546Spatrick return SendPacketNoLock(response.GetString()); 366061da546Spatrick } 367061da546Spatrick 368061da546Spatrick static void AppendHexValue(StreamString &response, const uint8_t *buf, 369061da546Spatrick uint32_t buf_size, bool swap) { 370061da546Spatrick int64_t i; 371061da546Spatrick if (swap) { 372061da546Spatrick for (i = buf_size - 1; i >= 0; i--) 373061da546Spatrick response.PutHex8(buf[i]); 374061da546Spatrick } else { 375061da546Spatrick for (i = 0; i < buf_size; i++) 376061da546Spatrick response.PutHex8(buf[i]); 377061da546Spatrick } 378061da546Spatrick } 379061da546Spatrick 380*dda28197Spatrick static llvm::StringRef GetEncodingNameOrEmpty(const RegisterInfo ®_info) { 381*dda28197Spatrick switch (reg_info.encoding) { 382*dda28197Spatrick case eEncodingUint: 383*dda28197Spatrick return "uint"; 384*dda28197Spatrick case eEncodingSint: 385*dda28197Spatrick return "sint"; 386*dda28197Spatrick case eEncodingIEEE754: 387*dda28197Spatrick return "ieee754"; 388*dda28197Spatrick case eEncodingVector: 389*dda28197Spatrick return "vector"; 390*dda28197Spatrick default: 391*dda28197Spatrick return ""; 392*dda28197Spatrick } 393*dda28197Spatrick } 394*dda28197Spatrick 395*dda28197Spatrick static llvm::StringRef GetFormatNameOrEmpty(const RegisterInfo ®_info) { 396*dda28197Spatrick switch (reg_info.format) { 397*dda28197Spatrick case eFormatBinary: 398*dda28197Spatrick return "binary"; 399*dda28197Spatrick case eFormatDecimal: 400*dda28197Spatrick return "decimal"; 401*dda28197Spatrick case eFormatHex: 402*dda28197Spatrick return "hex"; 403*dda28197Spatrick case eFormatFloat: 404*dda28197Spatrick return "float"; 405*dda28197Spatrick case eFormatVectorOfSInt8: 406*dda28197Spatrick return "vector-sint8"; 407*dda28197Spatrick case eFormatVectorOfUInt8: 408*dda28197Spatrick return "vector-uint8"; 409*dda28197Spatrick case eFormatVectorOfSInt16: 410*dda28197Spatrick return "vector-sint16"; 411*dda28197Spatrick case eFormatVectorOfUInt16: 412*dda28197Spatrick return "vector-uint16"; 413*dda28197Spatrick case eFormatVectorOfSInt32: 414*dda28197Spatrick return "vector-sint32"; 415*dda28197Spatrick case eFormatVectorOfUInt32: 416*dda28197Spatrick return "vector-uint32"; 417*dda28197Spatrick case eFormatVectorOfFloat32: 418*dda28197Spatrick return "vector-float32"; 419*dda28197Spatrick case eFormatVectorOfUInt64: 420*dda28197Spatrick return "vector-uint64"; 421*dda28197Spatrick case eFormatVectorOfUInt128: 422*dda28197Spatrick return "vector-uint128"; 423*dda28197Spatrick default: 424*dda28197Spatrick return ""; 425*dda28197Spatrick }; 426*dda28197Spatrick } 427*dda28197Spatrick 428*dda28197Spatrick static llvm::StringRef GetKindGenericOrEmpty(const RegisterInfo ®_info) { 429*dda28197Spatrick switch (reg_info.kinds[RegisterKind::eRegisterKindGeneric]) { 430*dda28197Spatrick case LLDB_REGNUM_GENERIC_PC: 431*dda28197Spatrick return "pc"; 432*dda28197Spatrick case LLDB_REGNUM_GENERIC_SP: 433*dda28197Spatrick return "sp"; 434*dda28197Spatrick case LLDB_REGNUM_GENERIC_FP: 435*dda28197Spatrick return "fp"; 436*dda28197Spatrick case LLDB_REGNUM_GENERIC_RA: 437*dda28197Spatrick return "ra"; 438*dda28197Spatrick case LLDB_REGNUM_GENERIC_FLAGS: 439*dda28197Spatrick return "flags"; 440*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG1: 441*dda28197Spatrick return "arg1"; 442*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG2: 443*dda28197Spatrick return "arg2"; 444*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG3: 445*dda28197Spatrick return "arg3"; 446*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG4: 447*dda28197Spatrick return "arg4"; 448*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG5: 449*dda28197Spatrick return "arg5"; 450*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG6: 451*dda28197Spatrick return "arg6"; 452*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG7: 453*dda28197Spatrick return "arg7"; 454*dda28197Spatrick case LLDB_REGNUM_GENERIC_ARG8: 455*dda28197Spatrick return "arg8"; 456*dda28197Spatrick default: 457*dda28197Spatrick return ""; 458*dda28197Spatrick } 459*dda28197Spatrick } 460*dda28197Spatrick 461*dda28197Spatrick static void CollectRegNums(const uint32_t *reg_num, StreamString &response, 462*dda28197Spatrick bool usehex) { 463*dda28197Spatrick for (int i = 0; *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) { 464*dda28197Spatrick if (i > 0) 465*dda28197Spatrick response.PutChar(','); 466*dda28197Spatrick if (usehex) 467*dda28197Spatrick response.Printf("%" PRIx32, *reg_num); 468*dda28197Spatrick else 469*dda28197Spatrick response.Printf("%" PRIu32, *reg_num); 470*dda28197Spatrick } 471*dda28197Spatrick } 472*dda28197Spatrick 473061da546Spatrick static void WriteRegisterValueInHexFixedWidth( 474061da546Spatrick StreamString &response, NativeRegisterContext ®_ctx, 475061da546Spatrick const RegisterInfo ®_info, const RegisterValue *reg_value_p, 476061da546Spatrick lldb::ByteOrder byte_order) { 477061da546Spatrick RegisterValue reg_value; 478061da546Spatrick if (!reg_value_p) { 479061da546Spatrick Status error = reg_ctx.ReadRegister(®_info, reg_value); 480061da546Spatrick if (error.Success()) 481061da546Spatrick reg_value_p = ®_value; 482061da546Spatrick // else log. 483061da546Spatrick } 484061da546Spatrick 485061da546Spatrick if (reg_value_p) { 486061da546Spatrick AppendHexValue(response, (const uint8_t *)reg_value_p->GetBytes(), 487061da546Spatrick reg_value_p->GetByteSize(), 488061da546Spatrick byte_order == lldb::eByteOrderLittle); 489061da546Spatrick } else { 490061da546Spatrick // Zero-out any unreadable values. 491061da546Spatrick if (reg_info.byte_size > 0) { 492061da546Spatrick std::basic_string<uint8_t> zeros(reg_info.byte_size, '\0'); 493061da546Spatrick AppendHexValue(response, zeros.data(), zeros.size(), false); 494061da546Spatrick } 495061da546Spatrick } 496061da546Spatrick } 497061da546Spatrick 498061da546Spatrick static llvm::Expected<json::Object> 499061da546Spatrick GetRegistersAsJSON(NativeThreadProtocol &thread) { 500061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 501061da546Spatrick 502061da546Spatrick NativeRegisterContext& reg_ctx = thread.GetRegisterContext(); 503061da546Spatrick 504061da546Spatrick json::Object register_object; 505061da546Spatrick 506061da546Spatrick #ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET 507061da546Spatrick // Expedite all registers in the first register set (i.e. should be GPRs) 508061da546Spatrick // that are not contained in other registers. 509061da546Spatrick const RegisterSet *reg_set_p = reg_ctx_sp->GetRegisterSet(0); 510061da546Spatrick if (!reg_set_p) 511061da546Spatrick return llvm::make_error<llvm::StringError>("failed to get registers", 512061da546Spatrick llvm::inconvertibleErrorCode()); 513061da546Spatrick for (const uint32_t *reg_num_p = reg_set_p->registers; 514061da546Spatrick *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) { 515061da546Spatrick uint32_t reg_num = *reg_num_p; 516061da546Spatrick #else 517061da546Spatrick // Expedite only a couple of registers until we figure out why sending 518061da546Spatrick // registers is expensive. 519061da546Spatrick static const uint32_t k_expedited_registers[] = { 520061da546Spatrick LLDB_REGNUM_GENERIC_PC, LLDB_REGNUM_GENERIC_SP, LLDB_REGNUM_GENERIC_FP, 521061da546Spatrick LLDB_REGNUM_GENERIC_RA, LLDB_INVALID_REGNUM}; 522061da546Spatrick 523061da546Spatrick for (const uint32_t *generic_reg_p = k_expedited_registers; 524061da546Spatrick *generic_reg_p != LLDB_INVALID_REGNUM; ++generic_reg_p) { 525061da546Spatrick uint32_t reg_num = reg_ctx.ConvertRegisterKindToRegisterNumber( 526061da546Spatrick eRegisterKindGeneric, *generic_reg_p); 527061da546Spatrick if (reg_num == LLDB_INVALID_REGNUM) 528061da546Spatrick continue; // Target does not support the given register. 529061da546Spatrick #endif 530061da546Spatrick 531061da546Spatrick const RegisterInfo *const reg_info_p = 532061da546Spatrick reg_ctx.GetRegisterInfoAtIndex(reg_num); 533061da546Spatrick if (reg_info_p == nullptr) { 534061da546Spatrick LLDB_LOGF(log, 535061da546Spatrick "%s failed to get register info for register index %" PRIu32, 536061da546Spatrick __FUNCTION__, reg_num); 537061da546Spatrick continue; 538061da546Spatrick } 539061da546Spatrick 540061da546Spatrick if (reg_info_p->value_regs != nullptr) 541061da546Spatrick continue; // Only expedite registers that are not contained in other 542061da546Spatrick // registers. 543061da546Spatrick 544061da546Spatrick RegisterValue reg_value; 545061da546Spatrick Status error = reg_ctx.ReadRegister(reg_info_p, reg_value); 546061da546Spatrick if (error.Fail()) { 547061da546Spatrick LLDB_LOGF(log, "%s failed to read register '%s' index %" PRIu32 ": %s", 548061da546Spatrick __FUNCTION__, 549061da546Spatrick reg_info_p->name ? reg_info_p->name : "<unnamed-register>", 550061da546Spatrick reg_num, error.AsCString()); 551061da546Spatrick continue; 552061da546Spatrick } 553061da546Spatrick 554061da546Spatrick StreamString stream; 555061da546Spatrick WriteRegisterValueInHexFixedWidth(stream, reg_ctx, *reg_info_p, 556061da546Spatrick ®_value, lldb::eByteOrderBig); 557061da546Spatrick 558061da546Spatrick register_object.try_emplace(llvm::to_string(reg_num), 559061da546Spatrick stream.GetString().str()); 560061da546Spatrick } 561061da546Spatrick 562061da546Spatrick return register_object; 563061da546Spatrick } 564061da546Spatrick 565061da546Spatrick static const char *GetStopReasonString(StopReason stop_reason) { 566061da546Spatrick switch (stop_reason) { 567061da546Spatrick case eStopReasonTrace: 568061da546Spatrick return "trace"; 569061da546Spatrick case eStopReasonBreakpoint: 570061da546Spatrick return "breakpoint"; 571061da546Spatrick case eStopReasonWatchpoint: 572061da546Spatrick return "watchpoint"; 573061da546Spatrick case eStopReasonSignal: 574061da546Spatrick return "signal"; 575061da546Spatrick case eStopReasonException: 576061da546Spatrick return "exception"; 577061da546Spatrick case eStopReasonExec: 578061da546Spatrick return "exec"; 579061da546Spatrick case eStopReasonInstrumentation: 580061da546Spatrick case eStopReasonInvalid: 581061da546Spatrick case eStopReasonPlanComplete: 582061da546Spatrick case eStopReasonThreadExiting: 583061da546Spatrick case eStopReasonNone: 584061da546Spatrick break; // ignored 585061da546Spatrick } 586061da546Spatrick return nullptr; 587061da546Spatrick } 588061da546Spatrick 589061da546Spatrick static llvm::Expected<json::Array> 590061da546Spatrick GetJSONThreadsInfo(NativeProcessProtocol &process, bool abridged) { 591061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 592061da546Spatrick 593061da546Spatrick json::Array threads_array; 594061da546Spatrick 595061da546Spatrick // Ensure we can get info on the given thread. 596061da546Spatrick uint32_t thread_idx = 0; 597061da546Spatrick for (NativeThreadProtocol *thread; 598061da546Spatrick (thread = process.GetThreadAtIndex(thread_idx)) != nullptr; 599061da546Spatrick ++thread_idx) { 600061da546Spatrick 601061da546Spatrick lldb::tid_t tid = thread->GetID(); 602061da546Spatrick 603061da546Spatrick // Grab the reason this thread stopped. 604061da546Spatrick struct ThreadStopInfo tid_stop_info; 605061da546Spatrick std::string description; 606061da546Spatrick if (!thread->GetStopReason(tid_stop_info, description)) 607061da546Spatrick return llvm::make_error<llvm::StringError>( 608061da546Spatrick "failed to get stop reason", llvm::inconvertibleErrorCode()); 609061da546Spatrick 610061da546Spatrick const int signum = tid_stop_info.details.signal.signo; 611061da546Spatrick if (log) { 612061da546Spatrick LLDB_LOGF(log, 613061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 614061da546Spatrick " tid %" PRIu64 615061da546Spatrick " got signal signo = %d, reason = %d, exc_type = %" PRIu64, 616061da546Spatrick __FUNCTION__, process.GetID(), tid, signum, 617061da546Spatrick tid_stop_info.reason, tid_stop_info.details.exception.type); 618061da546Spatrick } 619061da546Spatrick 620061da546Spatrick json::Object thread_obj; 621061da546Spatrick 622061da546Spatrick if (!abridged) { 623061da546Spatrick if (llvm::Expected<json::Object> registers = 624061da546Spatrick GetRegistersAsJSON(*thread)) { 625061da546Spatrick thread_obj.try_emplace("registers", std::move(*registers)); 626061da546Spatrick } else { 627061da546Spatrick return registers.takeError(); 628061da546Spatrick } 629061da546Spatrick } 630061da546Spatrick 631061da546Spatrick thread_obj.try_emplace("tid", static_cast<int64_t>(tid)); 632061da546Spatrick 633061da546Spatrick if (signum != 0) 634061da546Spatrick thread_obj.try_emplace("signal", signum); 635061da546Spatrick 636061da546Spatrick const std::string thread_name = thread->GetName(); 637061da546Spatrick if (!thread_name.empty()) 638061da546Spatrick thread_obj.try_emplace("name", thread_name); 639061da546Spatrick 640061da546Spatrick const char *stop_reason = GetStopReasonString(tid_stop_info.reason); 641061da546Spatrick if (stop_reason) 642061da546Spatrick thread_obj.try_emplace("reason", stop_reason); 643061da546Spatrick 644061da546Spatrick if (!description.empty()) 645061da546Spatrick thread_obj.try_emplace("description", description); 646061da546Spatrick 647061da546Spatrick if ((tid_stop_info.reason == eStopReasonException) && 648061da546Spatrick tid_stop_info.details.exception.type) { 649061da546Spatrick thread_obj.try_emplace( 650061da546Spatrick "metype", static_cast<int64_t>(tid_stop_info.details.exception.type)); 651061da546Spatrick 652061da546Spatrick json::Array medata_array; 653061da546Spatrick for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; 654061da546Spatrick ++i) { 655061da546Spatrick medata_array.push_back( 656061da546Spatrick static_cast<int64_t>(tid_stop_info.details.exception.data[i])); 657061da546Spatrick } 658061da546Spatrick thread_obj.try_emplace("medata", std::move(medata_array)); 659061da546Spatrick } 660061da546Spatrick threads_array.push_back(std::move(thread_obj)); 661061da546Spatrick } 662061da546Spatrick return threads_array; 663061da546Spatrick } 664061da546Spatrick 665061da546Spatrick GDBRemoteCommunication::PacketResult 666061da546Spatrick GDBRemoteCommunicationServerLLGS::SendStopReplyPacketForThread( 667061da546Spatrick lldb::tid_t tid) { 668061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 669061da546Spatrick 670061da546Spatrick // Ensure we have a debugged process. 671061da546Spatrick if (!m_debugged_process_up || 672061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 673061da546Spatrick return SendErrorResponse(50); 674061da546Spatrick 675061da546Spatrick LLDB_LOG(log, "preparing packet for pid {0} tid {1}", 676061da546Spatrick m_debugged_process_up->GetID(), tid); 677061da546Spatrick 678061da546Spatrick // Ensure we can get info on the given thread. 679061da546Spatrick NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid); 680061da546Spatrick if (!thread) 681061da546Spatrick return SendErrorResponse(51); 682061da546Spatrick 683061da546Spatrick // Grab the reason this thread stopped. 684061da546Spatrick struct ThreadStopInfo tid_stop_info; 685061da546Spatrick std::string description; 686061da546Spatrick if (!thread->GetStopReason(tid_stop_info, description)) 687061da546Spatrick return SendErrorResponse(52); 688061da546Spatrick 689061da546Spatrick // FIXME implement register handling for exec'd inferiors. 690061da546Spatrick // if (tid_stop_info.reason == eStopReasonExec) { 691061da546Spatrick // const bool force = true; 692061da546Spatrick // InitializeRegisters(force); 693061da546Spatrick // } 694061da546Spatrick 695061da546Spatrick StreamString response; 696061da546Spatrick // Output the T packet with the thread 697061da546Spatrick response.PutChar('T'); 698061da546Spatrick int signum = tid_stop_info.details.signal.signo; 699061da546Spatrick LLDB_LOG( 700061da546Spatrick log, 701061da546Spatrick "pid {0}, tid {1}, got signal signo = {2}, reason = {3}, exc_type = {4}", 702061da546Spatrick m_debugged_process_up->GetID(), tid, signum, int(tid_stop_info.reason), 703061da546Spatrick tid_stop_info.details.exception.type); 704061da546Spatrick 705061da546Spatrick // Print the signal number. 706061da546Spatrick response.PutHex8(signum & 0xff); 707061da546Spatrick 708061da546Spatrick // Include the tid. 709061da546Spatrick response.Printf("thread:%" PRIx64 ";", tid); 710061da546Spatrick 711061da546Spatrick // Include the thread name if there is one. 712061da546Spatrick const std::string thread_name = thread->GetName(); 713061da546Spatrick if (!thread_name.empty()) { 714061da546Spatrick size_t thread_name_len = thread_name.length(); 715061da546Spatrick 716061da546Spatrick if (::strcspn(thread_name.c_str(), "$#+-;:") == thread_name_len) { 717061da546Spatrick response.PutCString("name:"); 718061da546Spatrick response.PutCString(thread_name); 719061da546Spatrick } else { 720061da546Spatrick // The thread name contains special chars, send as hex bytes. 721061da546Spatrick response.PutCString("hexname:"); 722061da546Spatrick response.PutStringAsRawHex8(thread_name); 723061da546Spatrick } 724061da546Spatrick response.PutChar(';'); 725061da546Spatrick } 726061da546Spatrick 727061da546Spatrick // If a 'QListThreadsInStopReply' was sent to enable this feature, we will 728061da546Spatrick // send all thread IDs back in the "threads" key whose value is a list of hex 729061da546Spatrick // thread IDs separated by commas: 730061da546Spatrick // "threads:10a,10b,10c;" 731061da546Spatrick // This will save the debugger from having to send a pair of qfThreadInfo and 732061da546Spatrick // qsThreadInfo packets, but it also might take a lot of room in the stop 733061da546Spatrick // reply packet, so it must be enabled only on systems where there are no 734061da546Spatrick // limits on packet lengths. 735061da546Spatrick if (m_list_threads_in_stop_reply) { 736061da546Spatrick response.PutCString("threads:"); 737061da546Spatrick 738061da546Spatrick uint32_t thread_index = 0; 739061da546Spatrick NativeThreadProtocol *listed_thread; 740061da546Spatrick for (listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index); 741061da546Spatrick listed_thread; ++thread_index, 742061da546Spatrick listed_thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) { 743061da546Spatrick if (thread_index > 0) 744061da546Spatrick response.PutChar(','); 745061da546Spatrick response.Printf("%" PRIx64, listed_thread->GetID()); 746061da546Spatrick } 747061da546Spatrick response.PutChar(';'); 748061da546Spatrick 749061da546Spatrick // Include JSON info that describes the stop reason for any threads that 750061da546Spatrick // actually have stop reasons. We use the new "jstopinfo" key whose values 751061da546Spatrick // is hex ascii JSON that contains the thread IDs thread stop info only for 752061da546Spatrick // threads that have stop reasons. Only send this if we have more than one 753061da546Spatrick // thread otherwise this packet has all the info it needs. 754061da546Spatrick if (thread_index > 1) { 755061da546Spatrick const bool threads_with_valid_stop_info_only = true; 756061da546Spatrick llvm::Expected<json::Array> threads_info = GetJSONThreadsInfo( 757061da546Spatrick *m_debugged_process_up, threads_with_valid_stop_info_only); 758061da546Spatrick if (threads_info) { 759061da546Spatrick response.PutCString("jstopinfo:"); 760061da546Spatrick StreamString unescaped_response; 761061da546Spatrick unescaped_response.AsRawOstream() << std::move(*threads_info); 762061da546Spatrick response.PutStringAsRawHex8(unescaped_response.GetData()); 763061da546Spatrick response.PutChar(';'); 764061da546Spatrick } else { 765061da546Spatrick LLDB_LOG_ERROR(log, threads_info.takeError(), 766061da546Spatrick "failed to prepare a jstopinfo field for pid {1}: {0}", 767061da546Spatrick m_debugged_process_up->GetID()); 768061da546Spatrick } 769061da546Spatrick } 770061da546Spatrick 771061da546Spatrick uint32_t i = 0; 772061da546Spatrick response.PutCString("thread-pcs"); 773061da546Spatrick char delimiter = ':'; 774061da546Spatrick for (NativeThreadProtocol *thread; 775061da546Spatrick (thread = m_debugged_process_up->GetThreadAtIndex(i)) != nullptr; 776061da546Spatrick ++i) { 777061da546Spatrick NativeRegisterContext& reg_ctx = thread->GetRegisterContext(); 778061da546Spatrick 779061da546Spatrick uint32_t reg_to_read = reg_ctx.ConvertRegisterKindToRegisterNumber( 780061da546Spatrick eRegisterKindGeneric, LLDB_REGNUM_GENERIC_PC); 781061da546Spatrick const RegisterInfo *const reg_info_p = 782061da546Spatrick reg_ctx.GetRegisterInfoAtIndex(reg_to_read); 783061da546Spatrick 784061da546Spatrick RegisterValue reg_value; 785061da546Spatrick Status error = reg_ctx.ReadRegister(reg_info_p, reg_value); 786061da546Spatrick if (error.Fail()) { 787061da546Spatrick LLDB_LOGF(log, "%s failed to read register '%s' index %" PRIu32 ": %s", 788061da546Spatrick __FUNCTION__, 789061da546Spatrick reg_info_p->name ? reg_info_p->name : "<unnamed-register>", 790061da546Spatrick reg_to_read, error.AsCString()); 791061da546Spatrick continue; 792061da546Spatrick } 793061da546Spatrick 794061da546Spatrick response.PutChar(delimiter); 795061da546Spatrick delimiter = ','; 796061da546Spatrick WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p, 797061da546Spatrick ®_value, endian::InlHostByteOrder()); 798061da546Spatrick } 799061da546Spatrick 800061da546Spatrick response.PutChar(';'); 801061da546Spatrick } 802061da546Spatrick 803061da546Spatrick // 804061da546Spatrick // Expedite registers. 805061da546Spatrick // 806061da546Spatrick 807061da546Spatrick // Grab the register context. 808061da546Spatrick NativeRegisterContext& reg_ctx = thread->GetRegisterContext(); 809061da546Spatrick // Expedite all registers in the first register set (i.e. should be GPRs) 810061da546Spatrick // that are not contained in other registers. 811061da546Spatrick const RegisterSet *reg_set_p; 812061da546Spatrick if (reg_ctx.GetRegisterSetCount() > 0 && 813061da546Spatrick ((reg_set_p = reg_ctx.GetRegisterSet(0)) != nullptr)) { 814061da546Spatrick LLDB_LOGF(log, 815061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s expediting registers " 816061da546Spatrick "from set '%s' (registers set count: %zu)", 817061da546Spatrick __FUNCTION__, reg_set_p->name ? reg_set_p->name : "<unnamed-set>", 818061da546Spatrick reg_set_p->num_registers); 819061da546Spatrick 820061da546Spatrick for (const uint32_t *reg_num_p = reg_set_p->registers; 821061da546Spatrick *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) { 822061da546Spatrick const RegisterInfo *const reg_info_p = 823061da546Spatrick reg_ctx.GetRegisterInfoAtIndex(*reg_num_p); 824061da546Spatrick if (reg_info_p == nullptr) { 825061da546Spatrick LLDB_LOGF(log, 826061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed to get " 827061da546Spatrick "register info for register set '%s', register index " 828061da546Spatrick "%" PRIu32, 829061da546Spatrick __FUNCTION__, 830061da546Spatrick reg_set_p->name ? reg_set_p->name : "<unnamed-set>", 831061da546Spatrick *reg_num_p); 832061da546Spatrick } else if (reg_info_p->value_regs == nullptr) { 833061da546Spatrick // Only expediate registers that are not contained in other registers. 834061da546Spatrick RegisterValue reg_value; 835061da546Spatrick Status error = reg_ctx.ReadRegister(reg_info_p, reg_value); 836061da546Spatrick if (error.Success()) { 837061da546Spatrick response.Printf("%.02x:", *reg_num_p); 838061da546Spatrick WriteRegisterValueInHexFixedWidth(response, reg_ctx, *reg_info_p, 839061da546Spatrick ®_value, lldb::eByteOrderBig); 840061da546Spatrick response.PutChar(';'); 841061da546Spatrick } else { 842061da546Spatrick LLDB_LOGF(log, 843061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed to read " 844061da546Spatrick "register '%s' index %" PRIu32 ": %s", 845061da546Spatrick __FUNCTION__, 846061da546Spatrick reg_info_p->name ? reg_info_p->name : "<unnamed-register>", 847061da546Spatrick *reg_num_p, error.AsCString()); 848061da546Spatrick } 849061da546Spatrick } 850061da546Spatrick } 851061da546Spatrick } 852061da546Spatrick 853061da546Spatrick const char *reason_str = GetStopReasonString(tid_stop_info.reason); 854061da546Spatrick if (reason_str != nullptr) { 855061da546Spatrick response.Printf("reason:%s;", reason_str); 856061da546Spatrick } 857061da546Spatrick 858061da546Spatrick if (!description.empty()) { 859061da546Spatrick // Description may contains special chars, send as hex bytes. 860061da546Spatrick response.PutCString("description:"); 861061da546Spatrick response.PutStringAsRawHex8(description); 862061da546Spatrick response.PutChar(';'); 863061da546Spatrick } else if ((tid_stop_info.reason == eStopReasonException) && 864061da546Spatrick tid_stop_info.details.exception.type) { 865061da546Spatrick response.PutCString("metype:"); 866061da546Spatrick response.PutHex64(tid_stop_info.details.exception.type); 867061da546Spatrick response.PutCString(";mecount:"); 868061da546Spatrick response.PutHex32(tid_stop_info.details.exception.data_count); 869061da546Spatrick response.PutChar(';'); 870061da546Spatrick 871061da546Spatrick for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; ++i) { 872061da546Spatrick response.PutCString("medata:"); 873061da546Spatrick response.PutHex64(tid_stop_info.details.exception.data[i]); 874061da546Spatrick response.PutChar(';'); 875061da546Spatrick } 876061da546Spatrick } 877061da546Spatrick 878061da546Spatrick return SendPacketNoLock(response.GetString()); 879061da546Spatrick } 880061da546Spatrick 881061da546Spatrick void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Exited( 882061da546Spatrick NativeProcessProtocol *process) { 883061da546Spatrick assert(process && "process cannot be NULL"); 884061da546Spatrick 885061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 886061da546Spatrick LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 887061da546Spatrick 888061da546Spatrick PacketResult result = SendStopReasonForState(StateType::eStateExited); 889061da546Spatrick if (result != PacketResult::Success) { 890061da546Spatrick LLDB_LOGF(log, 891061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed to send stop " 892061da546Spatrick "notification for PID %" PRIu64 ", state: eStateExited", 893061da546Spatrick __FUNCTION__, process->GetID()); 894061da546Spatrick } 895061da546Spatrick 896061da546Spatrick // Close the pipe to the inferior terminal i/o if we launched it and set one 897061da546Spatrick // up. 898061da546Spatrick MaybeCloseInferiorTerminalConnection(); 899061da546Spatrick 900061da546Spatrick // We are ready to exit the debug monitor. 901061da546Spatrick m_exit_now = true; 902061da546Spatrick m_mainloop.RequestTermination(); 903061da546Spatrick } 904061da546Spatrick 905061da546Spatrick void GDBRemoteCommunicationServerLLGS::HandleInferiorState_Stopped( 906061da546Spatrick NativeProcessProtocol *process) { 907061da546Spatrick assert(process && "process cannot be NULL"); 908061da546Spatrick 909061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 910061da546Spatrick LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 911061da546Spatrick 912061da546Spatrick // Send the stop reason unless this is the stop after the launch or attach. 913061da546Spatrick switch (m_inferior_prev_state) { 914061da546Spatrick case eStateLaunching: 915061da546Spatrick case eStateAttaching: 916061da546Spatrick // Don't send anything per debugserver behavior. 917061da546Spatrick break; 918061da546Spatrick default: 919061da546Spatrick // In all other cases, send the stop reason. 920061da546Spatrick PacketResult result = SendStopReasonForState(StateType::eStateStopped); 921061da546Spatrick if (result != PacketResult::Success) { 922061da546Spatrick LLDB_LOGF(log, 923061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed to send stop " 924061da546Spatrick "notification for PID %" PRIu64 ", state: eStateExited", 925061da546Spatrick __FUNCTION__, process->GetID()); 926061da546Spatrick } 927061da546Spatrick break; 928061da546Spatrick } 929061da546Spatrick } 930061da546Spatrick 931061da546Spatrick void GDBRemoteCommunicationServerLLGS::ProcessStateChanged( 932061da546Spatrick NativeProcessProtocol *process, lldb::StateType state) { 933061da546Spatrick assert(process && "process cannot be NULL"); 934061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 935061da546Spatrick if (log) { 936061da546Spatrick LLDB_LOGF(log, 937061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s called with " 938061da546Spatrick "NativeProcessProtocol pid %" PRIu64 ", state: %s", 939061da546Spatrick __FUNCTION__, process->GetID(), StateAsCString(state)); 940061da546Spatrick } 941061da546Spatrick 942061da546Spatrick switch (state) { 943061da546Spatrick case StateType::eStateRunning: 944061da546Spatrick StartSTDIOForwarding(); 945061da546Spatrick break; 946061da546Spatrick 947061da546Spatrick case StateType::eStateStopped: 948061da546Spatrick // Make sure we get all of the pending stdout/stderr from the inferior and 949061da546Spatrick // send it to the lldb host before we send the state change notification 950061da546Spatrick SendProcessOutput(); 951061da546Spatrick // Then stop the forwarding, so that any late output (see llvm.org/pr25652) 952061da546Spatrick // does not interfere with our protocol. 953061da546Spatrick StopSTDIOForwarding(); 954061da546Spatrick HandleInferiorState_Stopped(process); 955061da546Spatrick break; 956061da546Spatrick 957061da546Spatrick case StateType::eStateExited: 958061da546Spatrick // Same as above 959061da546Spatrick SendProcessOutput(); 960061da546Spatrick StopSTDIOForwarding(); 961061da546Spatrick HandleInferiorState_Exited(process); 962061da546Spatrick break; 963061da546Spatrick 964061da546Spatrick default: 965061da546Spatrick if (log) { 966061da546Spatrick LLDB_LOGF(log, 967061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s didn't handle state " 968061da546Spatrick "change for pid %" PRIu64 ", new state: %s", 969061da546Spatrick __FUNCTION__, process->GetID(), StateAsCString(state)); 970061da546Spatrick } 971061da546Spatrick break; 972061da546Spatrick } 973061da546Spatrick 974061da546Spatrick // Remember the previous state reported to us. 975061da546Spatrick m_inferior_prev_state = state; 976061da546Spatrick } 977061da546Spatrick 978061da546Spatrick void GDBRemoteCommunicationServerLLGS::DidExec(NativeProcessProtocol *process) { 979061da546Spatrick ClearProcessSpecificData(); 980061da546Spatrick } 981061da546Spatrick 982061da546Spatrick void GDBRemoteCommunicationServerLLGS::DataAvailableCallback() { 983061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(GDBR_LOG_COMM)); 984061da546Spatrick 985061da546Spatrick if (!m_handshake_completed) { 986061da546Spatrick if (!HandshakeWithClient()) { 987061da546Spatrick LLDB_LOGF(log, 988061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s handshake with " 989061da546Spatrick "client failed, exiting", 990061da546Spatrick __FUNCTION__); 991061da546Spatrick m_mainloop.RequestTermination(); 992061da546Spatrick return; 993061da546Spatrick } 994061da546Spatrick m_handshake_completed = true; 995061da546Spatrick } 996061da546Spatrick 997061da546Spatrick bool interrupt = false; 998061da546Spatrick bool done = false; 999061da546Spatrick Status error; 1000061da546Spatrick while (true) { 1001061da546Spatrick const PacketResult result = GetPacketAndSendResponse( 1002061da546Spatrick std::chrono::microseconds(0), error, interrupt, done); 1003061da546Spatrick if (result == PacketResult::ErrorReplyTimeout) 1004061da546Spatrick break; // No more packets in the queue 1005061da546Spatrick 1006061da546Spatrick if ((result != PacketResult::Success)) { 1007061da546Spatrick LLDB_LOGF(log, 1008061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s processing a packet " 1009061da546Spatrick "failed: %s", 1010061da546Spatrick __FUNCTION__, error.AsCString()); 1011061da546Spatrick m_mainloop.RequestTermination(); 1012061da546Spatrick break; 1013061da546Spatrick } 1014061da546Spatrick } 1015061da546Spatrick } 1016061da546Spatrick 1017061da546Spatrick Status GDBRemoteCommunicationServerLLGS::InitializeConnection( 1018*dda28197Spatrick std::unique_ptr<Connection> connection) { 1019061da546Spatrick IOObjectSP read_object_sp = connection->GetReadObject(); 1020*dda28197Spatrick GDBRemoteCommunicationServer::SetConnection(std::move(connection)); 1021061da546Spatrick 1022061da546Spatrick Status error; 1023061da546Spatrick m_network_handle_up = m_mainloop.RegisterReadObject( 1024061da546Spatrick read_object_sp, [this](MainLoopBase &) { DataAvailableCallback(); }, 1025061da546Spatrick error); 1026061da546Spatrick return error; 1027061da546Spatrick } 1028061da546Spatrick 1029061da546Spatrick GDBRemoteCommunication::PacketResult 1030061da546Spatrick GDBRemoteCommunicationServerLLGS::SendONotification(const char *buffer, 1031061da546Spatrick uint32_t len) { 1032061da546Spatrick if ((buffer == nullptr) || (len == 0)) { 1033061da546Spatrick // Nothing to send. 1034061da546Spatrick return PacketResult::Success; 1035061da546Spatrick } 1036061da546Spatrick 1037061da546Spatrick StreamString response; 1038061da546Spatrick response.PutChar('O'); 1039061da546Spatrick response.PutBytesAsRawHex8(buffer, len); 1040061da546Spatrick 1041061da546Spatrick return SendPacketNoLock(response.GetString()); 1042061da546Spatrick } 1043061da546Spatrick 1044061da546Spatrick Status GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor(int fd) { 1045061da546Spatrick Status error; 1046061da546Spatrick 1047061da546Spatrick // Set up the reading/handling of process I/O 1048061da546Spatrick std::unique_ptr<ConnectionFileDescriptor> conn_up( 1049061da546Spatrick new ConnectionFileDescriptor(fd, true)); 1050061da546Spatrick if (!conn_up) { 1051061da546Spatrick error.SetErrorString("failed to create ConnectionFileDescriptor"); 1052061da546Spatrick return error; 1053061da546Spatrick } 1054061da546Spatrick 1055061da546Spatrick m_stdio_communication.SetCloseOnEOF(false); 1056*dda28197Spatrick m_stdio_communication.SetConnection(std::move(conn_up)); 1057061da546Spatrick if (!m_stdio_communication.IsConnected()) { 1058061da546Spatrick error.SetErrorString( 1059061da546Spatrick "failed to set connection for inferior I/O communication"); 1060061da546Spatrick return error; 1061061da546Spatrick } 1062061da546Spatrick 1063061da546Spatrick return Status(); 1064061da546Spatrick } 1065061da546Spatrick 1066061da546Spatrick void GDBRemoteCommunicationServerLLGS::StartSTDIOForwarding() { 1067061da546Spatrick // Don't forward if not connected (e.g. when attaching). 1068061da546Spatrick if (!m_stdio_communication.IsConnected()) 1069061da546Spatrick return; 1070061da546Spatrick 1071061da546Spatrick Status error; 1072061da546Spatrick lldbassert(!m_stdio_handle_up); 1073061da546Spatrick m_stdio_handle_up = m_mainloop.RegisterReadObject( 1074061da546Spatrick m_stdio_communication.GetConnection()->GetReadObject(), 1075061da546Spatrick [this](MainLoopBase &) { SendProcessOutput(); }, error); 1076061da546Spatrick 1077061da546Spatrick if (!m_stdio_handle_up) { 1078061da546Spatrick // Not much we can do about the failure. Log it and continue without 1079061da546Spatrick // forwarding. 1080061da546Spatrick if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)) 1081061da546Spatrick LLDB_LOGF(log, 1082061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s Failed to set up stdio " 1083061da546Spatrick "forwarding: %s", 1084061da546Spatrick __FUNCTION__, error.AsCString()); 1085061da546Spatrick } 1086061da546Spatrick } 1087061da546Spatrick 1088061da546Spatrick void GDBRemoteCommunicationServerLLGS::StopSTDIOForwarding() { 1089061da546Spatrick m_stdio_handle_up.reset(); 1090061da546Spatrick } 1091061da546Spatrick 1092061da546Spatrick void GDBRemoteCommunicationServerLLGS::SendProcessOutput() { 1093061da546Spatrick char buffer[1024]; 1094061da546Spatrick ConnectionStatus status; 1095061da546Spatrick Status error; 1096061da546Spatrick while (true) { 1097061da546Spatrick size_t bytes_read = m_stdio_communication.Read( 1098061da546Spatrick buffer, sizeof buffer, std::chrono::microseconds(0), status, &error); 1099061da546Spatrick switch (status) { 1100061da546Spatrick case eConnectionStatusSuccess: 1101061da546Spatrick SendONotification(buffer, bytes_read); 1102061da546Spatrick break; 1103061da546Spatrick case eConnectionStatusLostConnection: 1104061da546Spatrick case eConnectionStatusEndOfFile: 1105061da546Spatrick case eConnectionStatusError: 1106061da546Spatrick case eConnectionStatusNoConnection: 1107061da546Spatrick if (Log *log = GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)) 1108061da546Spatrick LLDB_LOGF(log, 1109061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s Stopping stdio " 1110061da546Spatrick "forwarding as communication returned status %d (error: " 1111061da546Spatrick "%s)", 1112061da546Spatrick __FUNCTION__, status, error.AsCString()); 1113061da546Spatrick m_stdio_handle_up.reset(); 1114061da546Spatrick return; 1115061da546Spatrick 1116061da546Spatrick case eConnectionStatusInterrupted: 1117061da546Spatrick case eConnectionStatusTimedOut: 1118061da546Spatrick return; 1119061da546Spatrick } 1120061da546Spatrick } 1121061da546Spatrick } 1122061da546Spatrick 1123061da546Spatrick GDBRemoteCommunication::PacketResult 1124061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_jTraceStart( 1125061da546Spatrick StringExtractorGDBRemote &packet) { 1126061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1127061da546Spatrick // Fail if we don't have a current process. 1128061da546Spatrick if (!m_debugged_process_up || 1129061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 1130061da546Spatrick return SendErrorResponse(68); 1131061da546Spatrick 1132061da546Spatrick if (!packet.ConsumeFront("jTraceStart:")) 1133061da546Spatrick return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet "); 1134061da546Spatrick 1135061da546Spatrick TraceOptions options; 1136061da546Spatrick uint64_t type = std::numeric_limits<uint64_t>::max(); 1137061da546Spatrick uint64_t buffersize = std::numeric_limits<uint64_t>::max(); 1138061da546Spatrick lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 1139061da546Spatrick uint64_t metabuffersize = std::numeric_limits<uint64_t>::max(); 1140061da546Spatrick 1141061da546Spatrick auto json_object = StructuredData::ParseJSON(packet.Peek()); 1142061da546Spatrick 1143061da546Spatrick if (!json_object || 1144061da546Spatrick json_object->GetType() != lldb::eStructuredDataTypeDictionary) 1145061da546Spatrick return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet "); 1146061da546Spatrick 1147061da546Spatrick auto json_dict = json_object->GetAsDictionary(); 1148061da546Spatrick 1149061da546Spatrick json_dict->GetValueForKeyAsInteger("metabuffersize", metabuffersize); 1150061da546Spatrick options.setMetaDataBufferSize(metabuffersize); 1151061da546Spatrick 1152061da546Spatrick json_dict->GetValueForKeyAsInteger("buffersize", buffersize); 1153061da546Spatrick options.setTraceBufferSize(buffersize); 1154061da546Spatrick 1155061da546Spatrick json_dict->GetValueForKeyAsInteger("type", type); 1156061da546Spatrick options.setType(static_cast<lldb::TraceType>(type)); 1157061da546Spatrick 1158061da546Spatrick json_dict->GetValueForKeyAsInteger("threadid", tid); 1159061da546Spatrick options.setThreadID(tid); 1160061da546Spatrick 1161061da546Spatrick StructuredData::ObjectSP custom_params_sp = 1162061da546Spatrick json_dict->GetValueForKey("params"); 1163061da546Spatrick if (custom_params_sp && 1164061da546Spatrick custom_params_sp->GetType() != lldb::eStructuredDataTypeDictionary) 1165061da546Spatrick return SendIllFormedResponse(packet, "jTraceStart: Ill formed packet "); 1166061da546Spatrick 1167061da546Spatrick options.setTraceParams( 1168*dda28197Spatrick std::static_pointer_cast<StructuredData::Dictionary>(custom_params_sp)); 1169061da546Spatrick 1170061da546Spatrick if (buffersize == std::numeric_limits<uint64_t>::max() || 1171061da546Spatrick type != lldb::TraceType::eTraceTypeProcessorTrace) { 1172061da546Spatrick LLDB_LOG(log, "Ill formed packet buffersize = {0} type = {1}", buffersize, 1173061da546Spatrick type); 1174061da546Spatrick return SendIllFormedResponse(packet, "JTrace:start: Ill formed packet "); 1175061da546Spatrick } 1176061da546Spatrick 1177061da546Spatrick Status error; 1178061da546Spatrick lldb::user_id_t uid = LLDB_INVALID_UID; 1179061da546Spatrick uid = m_debugged_process_up->StartTrace(options, error); 1180061da546Spatrick LLDB_LOG(log, "uid is {0} , error is {1}", uid, error.GetError()); 1181061da546Spatrick if (error.Fail()) 1182061da546Spatrick return SendErrorResponse(error); 1183061da546Spatrick 1184061da546Spatrick StreamGDBRemote response; 1185061da546Spatrick response.Printf("%" PRIx64, uid); 1186061da546Spatrick return SendPacketNoLock(response.GetString()); 1187061da546Spatrick } 1188061da546Spatrick 1189061da546Spatrick GDBRemoteCommunication::PacketResult 1190061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_jTraceStop( 1191061da546Spatrick StringExtractorGDBRemote &packet) { 1192061da546Spatrick // Fail if we don't have a current process. 1193061da546Spatrick if (!m_debugged_process_up || 1194061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 1195061da546Spatrick return SendErrorResponse(68); 1196061da546Spatrick 1197061da546Spatrick if (!packet.ConsumeFront("jTraceStop:")) 1198061da546Spatrick return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet "); 1199061da546Spatrick 1200061da546Spatrick lldb::user_id_t uid = LLDB_INVALID_UID; 1201061da546Spatrick lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 1202061da546Spatrick 1203061da546Spatrick auto json_object = StructuredData::ParseJSON(packet.Peek()); 1204061da546Spatrick 1205061da546Spatrick if (!json_object || 1206061da546Spatrick json_object->GetType() != lldb::eStructuredDataTypeDictionary) 1207061da546Spatrick return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet "); 1208061da546Spatrick 1209061da546Spatrick auto json_dict = json_object->GetAsDictionary(); 1210061da546Spatrick 1211061da546Spatrick if (!json_dict->GetValueForKeyAsInteger("traceid", uid)) 1212061da546Spatrick return SendIllFormedResponse(packet, "jTraceStop: Ill formed packet "); 1213061da546Spatrick 1214061da546Spatrick json_dict->GetValueForKeyAsInteger("threadid", tid); 1215061da546Spatrick 1216061da546Spatrick Status error = m_debugged_process_up->StopTrace(uid, tid); 1217061da546Spatrick 1218061da546Spatrick if (error.Fail()) 1219061da546Spatrick return SendErrorResponse(error); 1220061da546Spatrick 1221061da546Spatrick return SendOKResponse(); 1222061da546Spatrick } 1223061da546Spatrick 1224061da546Spatrick GDBRemoteCommunication::PacketResult 1225061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_jTraceConfigRead( 1226061da546Spatrick StringExtractorGDBRemote &packet) { 1227061da546Spatrick 1228061da546Spatrick // Fail if we don't have a current process. 1229061da546Spatrick if (!m_debugged_process_up || 1230061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 1231061da546Spatrick return SendErrorResponse(68); 1232061da546Spatrick 1233061da546Spatrick if (!packet.ConsumeFront("jTraceConfigRead:")) 1234061da546Spatrick return SendIllFormedResponse(packet, 1235061da546Spatrick "jTraceConfigRead: Ill formed packet "); 1236061da546Spatrick 1237061da546Spatrick lldb::user_id_t uid = LLDB_INVALID_UID; 1238061da546Spatrick lldb::tid_t threadid = LLDB_INVALID_THREAD_ID; 1239061da546Spatrick 1240061da546Spatrick auto json_object = StructuredData::ParseJSON(packet.Peek()); 1241061da546Spatrick 1242061da546Spatrick if (!json_object || 1243061da546Spatrick json_object->GetType() != lldb::eStructuredDataTypeDictionary) 1244061da546Spatrick return SendIllFormedResponse(packet, 1245061da546Spatrick "jTraceConfigRead: Ill formed packet "); 1246061da546Spatrick 1247061da546Spatrick auto json_dict = json_object->GetAsDictionary(); 1248061da546Spatrick 1249061da546Spatrick if (!json_dict->GetValueForKeyAsInteger("traceid", uid)) 1250061da546Spatrick return SendIllFormedResponse(packet, 1251061da546Spatrick "jTraceConfigRead: Ill formed packet "); 1252061da546Spatrick 1253061da546Spatrick json_dict->GetValueForKeyAsInteger("threadid", threadid); 1254061da546Spatrick 1255061da546Spatrick TraceOptions options; 1256061da546Spatrick StreamGDBRemote response; 1257061da546Spatrick 1258061da546Spatrick options.setThreadID(threadid); 1259061da546Spatrick Status error = m_debugged_process_up->GetTraceConfig(uid, options); 1260061da546Spatrick 1261061da546Spatrick if (error.Fail()) 1262061da546Spatrick return SendErrorResponse(error); 1263061da546Spatrick 1264061da546Spatrick StreamGDBRemote escaped_response; 1265061da546Spatrick StructuredData::Dictionary json_packet; 1266061da546Spatrick 1267061da546Spatrick json_packet.AddIntegerItem("type", options.getType()); 1268061da546Spatrick json_packet.AddIntegerItem("buffersize", options.getTraceBufferSize()); 1269061da546Spatrick json_packet.AddIntegerItem("metabuffersize", options.getMetaDataBufferSize()); 1270061da546Spatrick 1271061da546Spatrick StructuredData::DictionarySP custom_params = options.getTraceParams(); 1272061da546Spatrick if (custom_params) 1273061da546Spatrick json_packet.AddItem("params", custom_params); 1274061da546Spatrick 1275061da546Spatrick StreamString json_string; 1276061da546Spatrick json_packet.Dump(json_string, false); 1277061da546Spatrick escaped_response.PutEscapedBytes(json_string.GetData(), 1278061da546Spatrick json_string.GetSize()); 1279061da546Spatrick return SendPacketNoLock(escaped_response.GetString()); 1280061da546Spatrick } 1281061da546Spatrick 1282061da546Spatrick GDBRemoteCommunication::PacketResult 1283061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_jTraceRead( 1284061da546Spatrick StringExtractorGDBRemote &packet) { 1285061da546Spatrick 1286061da546Spatrick // Fail if we don't have a current process. 1287061da546Spatrick if (!m_debugged_process_up || 1288061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 1289061da546Spatrick return SendErrorResponse(68); 1290061da546Spatrick 1291061da546Spatrick enum PacketType { MetaData, BufferData }; 1292061da546Spatrick PacketType tracetype = MetaData; 1293061da546Spatrick 1294061da546Spatrick if (packet.ConsumeFront("jTraceBufferRead:")) 1295061da546Spatrick tracetype = BufferData; 1296061da546Spatrick else if (packet.ConsumeFront("jTraceMetaRead:")) 1297061da546Spatrick tracetype = MetaData; 1298061da546Spatrick else { 1299061da546Spatrick return SendIllFormedResponse(packet, "jTrace: Ill formed packet "); 1300061da546Spatrick } 1301061da546Spatrick 1302061da546Spatrick lldb::user_id_t uid = LLDB_INVALID_UID; 1303061da546Spatrick 1304061da546Spatrick uint64_t byte_count = std::numeric_limits<uint64_t>::max(); 1305061da546Spatrick lldb::tid_t tid = LLDB_INVALID_THREAD_ID; 1306061da546Spatrick uint64_t offset = std::numeric_limits<uint64_t>::max(); 1307061da546Spatrick 1308061da546Spatrick auto json_object = StructuredData::ParseJSON(packet.Peek()); 1309061da546Spatrick 1310061da546Spatrick if (!json_object || 1311061da546Spatrick json_object->GetType() != lldb::eStructuredDataTypeDictionary) 1312061da546Spatrick return SendIllFormedResponse(packet, "jTrace: Ill formed packet "); 1313061da546Spatrick 1314061da546Spatrick auto json_dict = json_object->GetAsDictionary(); 1315061da546Spatrick 1316061da546Spatrick if (!json_dict->GetValueForKeyAsInteger("traceid", uid) || 1317061da546Spatrick !json_dict->GetValueForKeyAsInteger("offset", offset) || 1318061da546Spatrick !json_dict->GetValueForKeyAsInteger("buffersize", byte_count)) 1319061da546Spatrick return SendIllFormedResponse(packet, "jTrace: Ill formed packet "); 1320061da546Spatrick 1321061da546Spatrick json_dict->GetValueForKeyAsInteger("threadid", tid); 1322061da546Spatrick 1323061da546Spatrick // Allocate the response buffer. 1324061da546Spatrick std::unique_ptr<uint8_t[]> buffer (new (std::nothrow) uint8_t[byte_count]); 1325061da546Spatrick if (!buffer) 1326061da546Spatrick return SendErrorResponse(0x78); 1327061da546Spatrick 1328061da546Spatrick StreamGDBRemote response; 1329061da546Spatrick Status error; 1330061da546Spatrick llvm::MutableArrayRef<uint8_t> buf(buffer.get(), byte_count); 1331061da546Spatrick 1332061da546Spatrick if (tracetype == BufferData) 1333061da546Spatrick error = m_debugged_process_up->GetData(uid, tid, buf, offset); 1334061da546Spatrick else if (tracetype == MetaData) 1335061da546Spatrick error = m_debugged_process_up->GetMetaData(uid, tid, buf, offset); 1336061da546Spatrick 1337061da546Spatrick if (error.Fail()) 1338061da546Spatrick return SendErrorResponse(error); 1339061da546Spatrick 1340061da546Spatrick for (auto i : buf) 1341061da546Spatrick response.PutHex8(i); 1342061da546Spatrick 1343061da546Spatrick StreamGDBRemote escaped_response; 1344061da546Spatrick escaped_response.PutEscapedBytes(response.GetData(), response.GetSize()); 1345061da546Spatrick return SendPacketNoLock(escaped_response.GetString()); 1346061da546Spatrick } 1347061da546Spatrick 1348061da546Spatrick GDBRemoteCommunication::PacketResult 1349061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo( 1350061da546Spatrick StringExtractorGDBRemote &packet) { 1351061da546Spatrick // Fail if we don't have a current process. 1352061da546Spatrick if (!m_debugged_process_up || 1353061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 1354061da546Spatrick return SendErrorResponse(68); 1355061da546Spatrick 1356061da546Spatrick lldb::pid_t pid = m_debugged_process_up->GetID(); 1357061da546Spatrick 1358061da546Spatrick if (pid == LLDB_INVALID_PROCESS_ID) 1359061da546Spatrick return SendErrorResponse(1); 1360061da546Spatrick 1361061da546Spatrick ProcessInstanceInfo proc_info; 1362061da546Spatrick if (!Host::GetProcessInfo(pid, proc_info)) 1363061da546Spatrick return SendErrorResponse(1); 1364061da546Spatrick 1365061da546Spatrick StreamString response; 1366061da546Spatrick CreateProcessInfoResponse_DebugServerStyle(proc_info, response); 1367061da546Spatrick return SendPacketNoLock(response.GetString()); 1368061da546Spatrick } 1369061da546Spatrick 1370061da546Spatrick GDBRemoteCommunication::PacketResult 1371061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qC(StringExtractorGDBRemote &packet) { 1372061da546Spatrick // Fail if we don't have a current process. 1373061da546Spatrick if (!m_debugged_process_up || 1374061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 1375061da546Spatrick return SendErrorResponse(68); 1376061da546Spatrick 1377061da546Spatrick // Make sure we set the current thread so g and p packets return the data the 1378061da546Spatrick // gdb will expect. 1379061da546Spatrick lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID(); 1380061da546Spatrick SetCurrentThreadID(tid); 1381061da546Spatrick 1382061da546Spatrick NativeThreadProtocol *thread = m_debugged_process_up->GetCurrentThread(); 1383061da546Spatrick if (!thread) 1384061da546Spatrick return SendErrorResponse(69); 1385061da546Spatrick 1386061da546Spatrick StreamString response; 1387061da546Spatrick response.Printf("QC%" PRIx64, thread->GetID()); 1388061da546Spatrick 1389061da546Spatrick return SendPacketNoLock(response.GetString()); 1390061da546Spatrick } 1391061da546Spatrick 1392061da546Spatrick GDBRemoteCommunication::PacketResult 1393061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_k(StringExtractorGDBRemote &packet) { 1394061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1395061da546Spatrick 1396061da546Spatrick StopSTDIOForwarding(); 1397061da546Spatrick 1398061da546Spatrick if (!m_debugged_process_up) { 1399061da546Spatrick LLDB_LOG(log, "No debugged process found."); 1400061da546Spatrick return PacketResult::Success; 1401061da546Spatrick } 1402061da546Spatrick 1403061da546Spatrick Status error = m_debugged_process_up->Kill(); 1404061da546Spatrick if (error.Fail()) 1405061da546Spatrick LLDB_LOG(log, "Failed to kill debugged process {0}: {1}", 1406061da546Spatrick m_debugged_process_up->GetID(), error); 1407061da546Spatrick 1408061da546Spatrick // No OK response for kill packet. 1409061da546Spatrick // return SendOKResponse (); 1410061da546Spatrick return PacketResult::Success; 1411061da546Spatrick } 1412061da546Spatrick 1413061da546Spatrick GDBRemoteCommunication::PacketResult 1414061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR( 1415061da546Spatrick StringExtractorGDBRemote &packet) { 1416061da546Spatrick packet.SetFilePos(::strlen("QSetDisableASLR:")); 1417061da546Spatrick if (packet.GetU32(0)) 1418061da546Spatrick m_process_launch_info.GetFlags().Set(eLaunchFlagDisableASLR); 1419061da546Spatrick else 1420061da546Spatrick m_process_launch_info.GetFlags().Clear(eLaunchFlagDisableASLR); 1421061da546Spatrick return SendOKResponse(); 1422061da546Spatrick } 1423061da546Spatrick 1424061da546Spatrick GDBRemoteCommunication::PacketResult 1425061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir( 1426061da546Spatrick StringExtractorGDBRemote &packet) { 1427061da546Spatrick packet.SetFilePos(::strlen("QSetWorkingDir:")); 1428061da546Spatrick std::string path; 1429061da546Spatrick packet.GetHexByteString(path); 1430061da546Spatrick m_process_launch_info.SetWorkingDirectory(FileSpec(path)); 1431061da546Spatrick return SendOKResponse(); 1432061da546Spatrick } 1433061da546Spatrick 1434061da546Spatrick GDBRemoteCommunication::PacketResult 1435061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir( 1436061da546Spatrick StringExtractorGDBRemote &packet) { 1437061da546Spatrick FileSpec working_dir{m_process_launch_info.GetWorkingDirectory()}; 1438061da546Spatrick if (working_dir) { 1439061da546Spatrick StreamString response; 1440061da546Spatrick response.PutStringAsRawHex8(working_dir.GetCString()); 1441061da546Spatrick return SendPacketNoLock(response.GetString()); 1442061da546Spatrick } 1443061da546Spatrick 1444061da546Spatrick return SendErrorResponse(14); 1445061da546Spatrick } 1446061da546Spatrick 1447061da546Spatrick GDBRemoteCommunication::PacketResult 1448061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_C(StringExtractorGDBRemote &packet) { 1449061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 1450061da546Spatrick LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 1451061da546Spatrick 1452061da546Spatrick // Ensure we have a native process. 1453061da546Spatrick if (!m_debugged_process_up) { 1454061da546Spatrick LLDB_LOGF(log, 1455061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s no debugged process " 1456061da546Spatrick "shared pointer", 1457061da546Spatrick __FUNCTION__); 1458061da546Spatrick return SendErrorResponse(0x36); 1459061da546Spatrick } 1460061da546Spatrick 1461061da546Spatrick // Pull out the signal number. 1462061da546Spatrick packet.SetFilePos(::strlen("C")); 1463061da546Spatrick if (packet.GetBytesLeft() < 1) { 1464061da546Spatrick // Shouldn't be using a C without a signal. 1465061da546Spatrick return SendIllFormedResponse(packet, "C packet specified without signal."); 1466061da546Spatrick } 1467061da546Spatrick const uint32_t signo = 1468061da546Spatrick packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); 1469061da546Spatrick if (signo == std::numeric_limits<uint32_t>::max()) 1470061da546Spatrick return SendIllFormedResponse(packet, "failed to parse signal number"); 1471061da546Spatrick 1472061da546Spatrick // Handle optional continue address. 1473061da546Spatrick if (packet.GetBytesLeft() > 0) { 1474061da546Spatrick // FIXME add continue at address support for $C{signo}[;{continue-address}]. 1475061da546Spatrick if (*packet.Peek() == ';') 1476061da546Spatrick return SendUnimplementedResponse(packet.GetStringRef().data()); 1477061da546Spatrick else 1478061da546Spatrick return SendIllFormedResponse( 1479061da546Spatrick packet, "unexpected content after $C{signal-number}"); 1480061da546Spatrick } 1481061da546Spatrick 1482061da546Spatrick ResumeActionList resume_actions(StateType::eStateRunning, 1483061da546Spatrick LLDB_INVALID_SIGNAL_NUMBER); 1484061da546Spatrick Status error; 1485061da546Spatrick 1486061da546Spatrick // We have two branches: what to do if a continue thread is specified (in 1487061da546Spatrick // which case we target sending the signal to that thread), or when we don't 1488061da546Spatrick // have a continue thread set (in which case we send a signal to the 1489061da546Spatrick // process). 1490061da546Spatrick 1491061da546Spatrick // TODO discuss with Greg Clayton, make sure this makes sense. 1492061da546Spatrick 1493061da546Spatrick lldb::tid_t signal_tid = GetContinueThreadID(); 1494061da546Spatrick if (signal_tid != LLDB_INVALID_THREAD_ID) { 1495061da546Spatrick // The resume action for the continue thread (or all threads if a continue 1496061da546Spatrick // thread is not set). 1497061da546Spatrick ResumeAction action = {GetContinueThreadID(), StateType::eStateRunning, 1498061da546Spatrick static_cast<int>(signo)}; 1499061da546Spatrick 1500061da546Spatrick // Add the action for the continue thread (or all threads when the continue 1501061da546Spatrick // thread isn't present). 1502061da546Spatrick resume_actions.Append(action); 1503061da546Spatrick } else { 1504061da546Spatrick // Send the signal to the process since we weren't targeting a specific 1505061da546Spatrick // continue thread with the signal. 1506061da546Spatrick error = m_debugged_process_up->Signal(signo); 1507061da546Spatrick if (error.Fail()) { 1508061da546Spatrick LLDB_LOG(log, "failed to send signal for process {0}: {1}", 1509061da546Spatrick m_debugged_process_up->GetID(), error); 1510061da546Spatrick 1511061da546Spatrick return SendErrorResponse(0x52); 1512061da546Spatrick } 1513061da546Spatrick } 1514061da546Spatrick 1515061da546Spatrick // Resume the threads. 1516061da546Spatrick error = m_debugged_process_up->Resume(resume_actions); 1517061da546Spatrick if (error.Fail()) { 1518061da546Spatrick LLDB_LOG(log, "failed to resume threads for process {0}: {1}", 1519061da546Spatrick m_debugged_process_up->GetID(), error); 1520061da546Spatrick 1521061da546Spatrick return SendErrorResponse(0x38); 1522061da546Spatrick } 1523061da546Spatrick 1524061da546Spatrick // Don't send an "OK" packet; response is the stopped/exited message. 1525061da546Spatrick return PacketResult::Success; 1526061da546Spatrick } 1527061da546Spatrick 1528061da546Spatrick GDBRemoteCommunication::PacketResult 1529061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_c(StringExtractorGDBRemote &packet) { 1530061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 1531061da546Spatrick LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 1532061da546Spatrick 1533061da546Spatrick packet.SetFilePos(packet.GetFilePos() + ::strlen("c")); 1534061da546Spatrick 1535061da546Spatrick // For now just support all continue. 1536061da546Spatrick const bool has_continue_address = (packet.GetBytesLeft() > 0); 1537061da546Spatrick if (has_continue_address) { 1538061da546Spatrick LLDB_LOG(log, "not implemented for c[address] variant [{0} remains]", 1539061da546Spatrick packet.Peek()); 1540061da546Spatrick return SendUnimplementedResponse(packet.GetStringRef().data()); 1541061da546Spatrick } 1542061da546Spatrick 1543061da546Spatrick // Ensure we have a native process. 1544061da546Spatrick if (!m_debugged_process_up) { 1545061da546Spatrick LLDB_LOGF(log, 1546061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s no debugged process " 1547061da546Spatrick "shared pointer", 1548061da546Spatrick __FUNCTION__); 1549061da546Spatrick return SendErrorResponse(0x36); 1550061da546Spatrick } 1551061da546Spatrick 1552061da546Spatrick // Build the ResumeActionList 1553061da546Spatrick ResumeActionList actions(StateType::eStateRunning, 1554061da546Spatrick LLDB_INVALID_SIGNAL_NUMBER); 1555061da546Spatrick 1556061da546Spatrick Status error = m_debugged_process_up->Resume(actions); 1557061da546Spatrick if (error.Fail()) { 1558061da546Spatrick LLDB_LOG(log, "c failed for process {0}: {1}", 1559061da546Spatrick m_debugged_process_up->GetID(), error); 1560061da546Spatrick return SendErrorResponse(GDBRemoteServerError::eErrorResume); 1561061da546Spatrick } 1562061da546Spatrick 1563061da546Spatrick LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID()); 1564061da546Spatrick // No response required from continue. 1565061da546Spatrick return PacketResult::Success; 1566061da546Spatrick } 1567061da546Spatrick 1568061da546Spatrick GDBRemoteCommunication::PacketResult 1569061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_vCont_actions( 1570061da546Spatrick StringExtractorGDBRemote &packet) { 1571061da546Spatrick StreamString response; 1572061da546Spatrick response.Printf("vCont;c;C;s;S"); 1573061da546Spatrick 1574061da546Spatrick return SendPacketNoLock(response.GetString()); 1575061da546Spatrick } 1576061da546Spatrick 1577061da546Spatrick GDBRemoteCommunication::PacketResult 1578061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_vCont( 1579061da546Spatrick StringExtractorGDBRemote &packet) { 1580061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1581061da546Spatrick LLDB_LOGF(log, "GDBRemoteCommunicationServerLLGS::%s handling vCont packet", 1582061da546Spatrick __FUNCTION__); 1583061da546Spatrick 1584061da546Spatrick packet.SetFilePos(::strlen("vCont")); 1585061da546Spatrick 1586061da546Spatrick if (packet.GetBytesLeft() == 0) { 1587061da546Spatrick LLDB_LOGF(log, 1588061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s missing action from " 1589061da546Spatrick "vCont package", 1590061da546Spatrick __FUNCTION__); 1591061da546Spatrick return SendIllFormedResponse(packet, "Missing action from vCont package"); 1592061da546Spatrick } 1593061da546Spatrick 1594061da546Spatrick // Check if this is all continue (no options or ";c"). 1595061da546Spatrick if (::strcmp(packet.Peek(), ";c") == 0) { 1596061da546Spatrick // Move past the ';', then do a simple 'c'. 1597061da546Spatrick packet.SetFilePos(packet.GetFilePos() + 1); 1598061da546Spatrick return Handle_c(packet); 1599061da546Spatrick } else if (::strcmp(packet.Peek(), ";s") == 0) { 1600061da546Spatrick // Move past the ';', then do a simple 's'. 1601061da546Spatrick packet.SetFilePos(packet.GetFilePos() + 1); 1602061da546Spatrick return Handle_s(packet); 1603061da546Spatrick } 1604061da546Spatrick 1605061da546Spatrick // Ensure we have a native process. 1606061da546Spatrick if (!m_debugged_process_up) { 1607061da546Spatrick LLDB_LOG(log, "no debugged process"); 1608061da546Spatrick return SendErrorResponse(0x36); 1609061da546Spatrick } 1610061da546Spatrick 1611061da546Spatrick ResumeActionList thread_actions; 1612061da546Spatrick 1613061da546Spatrick while (packet.GetBytesLeft() && *packet.Peek() == ';') { 1614061da546Spatrick // Skip the semi-colon. 1615061da546Spatrick packet.GetChar(); 1616061da546Spatrick 1617061da546Spatrick // Build up the thread action. 1618061da546Spatrick ResumeAction thread_action; 1619061da546Spatrick thread_action.tid = LLDB_INVALID_THREAD_ID; 1620061da546Spatrick thread_action.state = eStateInvalid; 1621061da546Spatrick thread_action.signal = LLDB_INVALID_SIGNAL_NUMBER; 1622061da546Spatrick 1623061da546Spatrick const char action = packet.GetChar(); 1624061da546Spatrick switch (action) { 1625061da546Spatrick case 'C': 1626061da546Spatrick thread_action.signal = packet.GetHexMaxU32(false, 0); 1627061da546Spatrick if (thread_action.signal == 0) 1628061da546Spatrick return SendIllFormedResponse( 1629061da546Spatrick packet, "Could not parse signal in vCont packet C action"); 1630061da546Spatrick LLVM_FALLTHROUGH; 1631061da546Spatrick 1632061da546Spatrick case 'c': 1633061da546Spatrick // Continue 1634061da546Spatrick thread_action.state = eStateRunning; 1635061da546Spatrick break; 1636061da546Spatrick 1637061da546Spatrick case 'S': 1638061da546Spatrick thread_action.signal = packet.GetHexMaxU32(false, 0); 1639061da546Spatrick if (thread_action.signal == 0) 1640061da546Spatrick return SendIllFormedResponse( 1641061da546Spatrick packet, "Could not parse signal in vCont packet S action"); 1642061da546Spatrick LLVM_FALLTHROUGH; 1643061da546Spatrick 1644061da546Spatrick case 's': 1645061da546Spatrick // Step 1646061da546Spatrick thread_action.state = eStateStepping; 1647061da546Spatrick break; 1648061da546Spatrick 1649061da546Spatrick default: 1650061da546Spatrick return SendIllFormedResponse(packet, "Unsupported vCont action"); 1651061da546Spatrick break; 1652061da546Spatrick } 1653061da546Spatrick 1654061da546Spatrick // Parse out optional :{thread-id} value. 1655061da546Spatrick if (packet.GetBytesLeft() && (*packet.Peek() == ':')) { 1656061da546Spatrick // Consume the separator. 1657061da546Spatrick packet.GetChar(); 1658061da546Spatrick 1659061da546Spatrick thread_action.tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID); 1660061da546Spatrick if (thread_action.tid == LLDB_INVALID_THREAD_ID) 1661061da546Spatrick return SendIllFormedResponse( 1662061da546Spatrick packet, "Could not parse thread number in vCont packet"); 1663061da546Spatrick } 1664061da546Spatrick 1665061da546Spatrick thread_actions.Append(thread_action); 1666061da546Spatrick } 1667061da546Spatrick 1668061da546Spatrick Status error = m_debugged_process_up->Resume(thread_actions); 1669061da546Spatrick if (error.Fail()) { 1670061da546Spatrick LLDB_LOG(log, "vCont failed for process {0}: {1}", 1671061da546Spatrick m_debugged_process_up->GetID(), error); 1672061da546Spatrick return SendErrorResponse(GDBRemoteServerError::eErrorResume); 1673061da546Spatrick } 1674061da546Spatrick 1675061da546Spatrick LLDB_LOG(log, "continued process {0}", m_debugged_process_up->GetID()); 1676061da546Spatrick // No response required from vCont. 1677061da546Spatrick return PacketResult::Success; 1678061da546Spatrick } 1679061da546Spatrick 1680061da546Spatrick void GDBRemoteCommunicationServerLLGS::SetCurrentThreadID(lldb::tid_t tid) { 1681061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1682061da546Spatrick LLDB_LOG(log, "setting current thread id to {0}", tid); 1683061da546Spatrick 1684061da546Spatrick m_current_tid = tid; 1685061da546Spatrick if (m_debugged_process_up) 1686061da546Spatrick m_debugged_process_up->SetCurrentThreadID(m_current_tid); 1687061da546Spatrick } 1688061da546Spatrick 1689061da546Spatrick void GDBRemoteCommunicationServerLLGS::SetContinueThreadID(lldb::tid_t tid) { 1690061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1691061da546Spatrick LLDB_LOG(log, "setting continue thread id to {0}", tid); 1692061da546Spatrick 1693061da546Spatrick m_continue_tid = tid; 1694061da546Spatrick } 1695061da546Spatrick 1696061da546Spatrick GDBRemoteCommunication::PacketResult 1697061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_stop_reason( 1698061da546Spatrick StringExtractorGDBRemote &packet) { 1699061da546Spatrick // Handle the $? gdbremote command. 1700061da546Spatrick 1701061da546Spatrick // If no process, indicate error 1702061da546Spatrick if (!m_debugged_process_up) 1703061da546Spatrick return SendErrorResponse(02); 1704061da546Spatrick 1705061da546Spatrick return SendStopReasonForState(m_debugged_process_up->GetState()); 1706061da546Spatrick } 1707061da546Spatrick 1708061da546Spatrick GDBRemoteCommunication::PacketResult 1709061da546Spatrick GDBRemoteCommunicationServerLLGS::SendStopReasonForState( 1710061da546Spatrick lldb::StateType process_state) { 1711061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1712061da546Spatrick 1713061da546Spatrick switch (process_state) { 1714061da546Spatrick case eStateAttaching: 1715061da546Spatrick case eStateLaunching: 1716061da546Spatrick case eStateRunning: 1717061da546Spatrick case eStateStepping: 1718061da546Spatrick case eStateDetached: 1719061da546Spatrick // NOTE: gdb protocol doc looks like it should return $OK 1720061da546Spatrick // when everything is running (i.e. no stopped result). 1721061da546Spatrick return PacketResult::Success; // Ignore 1722061da546Spatrick 1723061da546Spatrick case eStateSuspended: 1724061da546Spatrick case eStateStopped: 1725061da546Spatrick case eStateCrashed: { 1726061da546Spatrick lldb::tid_t tid = m_debugged_process_up->GetCurrentThreadID(); 1727061da546Spatrick // Make sure we set the current thread so g and p packets return the data 1728061da546Spatrick // the gdb will expect. 1729061da546Spatrick SetCurrentThreadID(tid); 1730061da546Spatrick return SendStopReplyPacketForThread(tid); 1731061da546Spatrick } 1732061da546Spatrick 1733061da546Spatrick case eStateInvalid: 1734061da546Spatrick case eStateUnloaded: 1735061da546Spatrick case eStateExited: 1736061da546Spatrick return SendWResponse(m_debugged_process_up.get()); 1737061da546Spatrick 1738061da546Spatrick default: 1739061da546Spatrick LLDB_LOG(log, "pid {0}, current state reporting not handled: {1}", 1740061da546Spatrick m_debugged_process_up->GetID(), process_state); 1741061da546Spatrick break; 1742061da546Spatrick } 1743061da546Spatrick 1744061da546Spatrick return SendErrorResponse(0); 1745061da546Spatrick } 1746061da546Spatrick 1747061da546Spatrick GDBRemoteCommunication::PacketResult 1748061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo( 1749061da546Spatrick StringExtractorGDBRemote &packet) { 1750061da546Spatrick // Fail if we don't have a current process. 1751061da546Spatrick if (!m_debugged_process_up || 1752061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 1753061da546Spatrick return SendErrorResponse(68); 1754061da546Spatrick 1755061da546Spatrick // Ensure we have a thread. 1756061da546Spatrick NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0); 1757061da546Spatrick if (!thread) 1758061da546Spatrick return SendErrorResponse(69); 1759061da546Spatrick 1760061da546Spatrick // Get the register context for the first thread. 1761061da546Spatrick NativeRegisterContext ®_context = thread->GetRegisterContext(); 1762061da546Spatrick 1763061da546Spatrick // Parse out the register number from the request. 1764061da546Spatrick packet.SetFilePos(strlen("qRegisterInfo")); 1765061da546Spatrick const uint32_t reg_index = 1766061da546Spatrick packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); 1767061da546Spatrick if (reg_index == std::numeric_limits<uint32_t>::max()) 1768061da546Spatrick return SendErrorResponse(69); 1769061da546Spatrick 1770061da546Spatrick // Return the end of registers response if we've iterated one past the end of 1771061da546Spatrick // the register set. 1772061da546Spatrick if (reg_index >= reg_context.GetUserRegisterCount()) 1773061da546Spatrick return SendErrorResponse(69); 1774061da546Spatrick 1775061da546Spatrick const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index); 1776061da546Spatrick if (!reg_info) 1777061da546Spatrick return SendErrorResponse(69); 1778061da546Spatrick 1779061da546Spatrick // Build the reginfos response. 1780061da546Spatrick StreamGDBRemote response; 1781061da546Spatrick 1782061da546Spatrick response.PutCString("name:"); 1783061da546Spatrick response.PutCString(reg_info->name); 1784061da546Spatrick response.PutChar(';'); 1785061da546Spatrick 1786061da546Spatrick if (reg_info->alt_name && reg_info->alt_name[0]) { 1787061da546Spatrick response.PutCString("alt-name:"); 1788061da546Spatrick response.PutCString(reg_info->alt_name); 1789061da546Spatrick response.PutChar(';'); 1790061da546Spatrick } 1791061da546Spatrick 1792061da546Spatrick response.Printf("bitsize:%" PRIu32 ";offset:%" PRIu32 ";", 1793061da546Spatrick reg_info->byte_size * 8, reg_info->byte_offset); 1794061da546Spatrick 1795*dda28197Spatrick llvm::StringRef encoding = GetEncodingNameOrEmpty(*reg_info); 1796*dda28197Spatrick if (!encoding.empty()) 1797*dda28197Spatrick response << "encoding:" << encoding << ';'; 1798061da546Spatrick 1799*dda28197Spatrick llvm::StringRef format = GetFormatNameOrEmpty(*reg_info); 1800*dda28197Spatrick if (!format.empty()) 1801*dda28197Spatrick response << "format:" << format << ';'; 1802061da546Spatrick 1803061da546Spatrick const char *const register_set_name = 1804061da546Spatrick reg_context.GetRegisterSetNameForRegisterAtIndex(reg_index); 1805*dda28197Spatrick if (register_set_name) 1806*dda28197Spatrick response << "set:" << register_set_name << ';'; 1807061da546Spatrick 1808061da546Spatrick if (reg_info->kinds[RegisterKind::eRegisterKindEHFrame] != 1809061da546Spatrick LLDB_INVALID_REGNUM) 1810061da546Spatrick response.Printf("ehframe:%" PRIu32 ";", 1811061da546Spatrick reg_info->kinds[RegisterKind::eRegisterKindEHFrame]); 1812061da546Spatrick 1813061da546Spatrick if (reg_info->kinds[RegisterKind::eRegisterKindDWARF] != LLDB_INVALID_REGNUM) 1814061da546Spatrick response.Printf("dwarf:%" PRIu32 ";", 1815061da546Spatrick reg_info->kinds[RegisterKind::eRegisterKindDWARF]); 1816061da546Spatrick 1817*dda28197Spatrick llvm::StringRef kind_generic = GetKindGenericOrEmpty(*reg_info); 1818*dda28197Spatrick if (!kind_generic.empty()) 1819*dda28197Spatrick response << "generic:" << kind_generic << ';'; 1820061da546Spatrick 1821061da546Spatrick if (reg_info->value_regs && reg_info->value_regs[0] != LLDB_INVALID_REGNUM) { 1822061da546Spatrick response.PutCString("container-regs:"); 1823*dda28197Spatrick CollectRegNums(reg_info->value_regs, response, true); 1824061da546Spatrick response.PutChar(';'); 1825061da546Spatrick } 1826061da546Spatrick 1827061da546Spatrick if (reg_info->invalidate_regs && reg_info->invalidate_regs[0]) { 1828061da546Spatrick response.PutCString("invalidate-regs:"); 1829*dda28197Spatrick CollectRegNums(reg_info->invalidate_regs, response, true); 1830061da546Spatrick response.PutChar(';'); 1831061da546Spatrick } 1832061da546Spatrick 1833061da546Spatrick if (reg_info->dynamic_size_dwarf_expr_bytes) { 1834061da546Spatrick const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len; 1835061da546Spatrick response.PutCString("dynamic_size_dwarf_expr_bytes:"); 1836061da546Spatrick for (uint32_t i = 0; i < dwarf_opcode_len; ++i) 1837061da546Spatrick response.PutHex8(reg_info->dynamic_size_dwarf_expr_bytes[i]); 1838061da546Spatrick response.PutChar(';'); 1839061da546Spatrick } 1840061da546Spatrick return SendPacketNoLock(response.GetString()); 1841061da546Spatrick } 1842061da546Spatrick 1843061da546Spatrick GDBRemoteCommunication::PacketResult 1844061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo( 1845061da546Spatrick StringExtractorGDBRemote &packet) { 1846061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1847061da546Spatrick 1848061da546Spatrick // Fail if we don't have a current process. 1849061da546Spatrick if (!m_debugged_process_up || 1850061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 1851061da546Spatrick LLDB_LOG(log, "no process ({0}), returning OK", 1852061da546Spatrick m_debugged_process_up ? "invalid process id" 1853061da546Spatrick : "null m_debugged_process_up"); 1854061da546Spatrick return SendOKResponse(); 1855061da546Spatrick } 1856061da546Spatrick 1857061da546Spatrick StreamGDBRemote response; 1858061da546Spatrick response.PutChar('m'); 1859061da546Spatrick 1860061da546Spatrick LLDB_LOG(log, "starting thread iteration"); 1861061da546Spatrick NativeThreadProtocol *thread; 1862061da546Spatrick uint32_t thread_index; 1863061da546Spatrick for (thread_index = 0, 1864061da546Spatrick thread = m_debugged_process_up->GetThreadAtIndex(thread_index); 1865061da546Spatrick thread; ++thread_index, 1866061da546Spatrick thread = m_debugged_process_up->GetThreadAtIndex(thread_index)) { 1867061da546Spatrick LLDB_LOG(log, "iterated thread {0}(tid={2})", thread_index, 1868061da546Spatrick thread->GetID()); 1869061da546Spatrick if (thread_index > 0) 1870061da546Spatrick response.PutChar(','); 1871061da546Spatrick response.Printf("%" PRIx64, thread->GetID()); 1872061da546Spatrick } 1873061da546Spatrick 1874061da546Spatrick LLDB_LOG(log, "finished thread iteration"); 1875061da546Spatrick return SendPacketNoLock(response.GetString()); 1876061da546Spatrick } 1877061da546Spatrick 1878061da546Spatrick GDBRemoteCommunication::PacketResult 1879061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo( 1880061da546Spatrick StringExtractorGDBRemote &packet) { 1881061da546Spatrick // FIXME for now we return the full thread list in the initial packet and 1882061da546Spatrick // always do nothing here. 1883061da546Spatrick return SendPacketNoLock("l"); 1884061da546Spatrick } 1885061da546Spatrick 1886061da546Spatrick GDBRemoteCommunication::PacketResult 1887061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_g(StringExtractorGDBRemote &packet) { 1888061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1889061da546Spatrick 1890061da546Spatrick // Move past packet name. 1891061da546Spatrick packet.SetFilePos(strlen("g")); 1892061da546Spatrick 1893061da546Spatrick // Get the thread to use. 1894061da546Spatrick NativeThreadProtocol *thread = GetThreadFromSuffix(packet); 1895061da546Spatrick if (!thread) { 1896061da546Spatrick LLDB_LOG(log, "failed, no thread available"); 1897061da546Spatrick return SendErrorResponse(0x15); 1898061da546Spatrick } 1899061da546Spatrick 1900061da546Spatrick // Get the thread's register context. 1901061da546Spatrick NativeRegisterContext ®_ctx = thread->GetRegisterContext(); 1902061da546Spatrick 1903061da546Spatrick std::vector<uint8_t> regs_buffer; 1904061da546Spatrick for (uint32_t reg_num = 0; reg_num < reg_ctx.GetUserRegisterCount(); 1905061da546Spatrick ++reg_num) { 1906061da546Spatrick const RegisterInfo *reg_info = reg_ctx.GetRegisterInfoAtIndex(reg_num); 1907061da546Spatrick 1908061da546Spatrick if (reg_info == nullptr) { 1909061da546Spatrick LLDB_LOG(log, "failed to get register info for register index {0}", 1910061da546Spatrick reg_num); 1911061da546Spatrick return SendErrorResponse(0x15); 1912061da546Spatrick } 1913061da546Spatrick 1914061da546Spatrick if (reg_info->value_regs != nullptr) 1915061da546Spatrick continue; // skip registers that are contained in other registers 1916061da546Spatrick 1917061da546Spatrick RegisterValue reg_value; 1918061da546Spatrick Status error = reg_ctx.ReadRegister(reg_info, reg_value); 1919061da546Spatrick if (error.Fail()) { 1920061da546Spatrick LLDB_LOG(log, "failed to read register at index {0}", reg_num); 1921061da546Spatrick return SendErrorResponse(0x15); 1922061da546Spatrick } 1923061da546Spatrick 1924061da546Spatrick if (reg_info->byte_offset + reg_info->byte_size >= regs_buffer.size()) 1925061da546Spatrick // Resize the buffer to guarantee it can store the register offsetted 1926061da546Spatrick // data. 1927061da546Spatrick regs_buffer.resize(reg_info->byte_offset + reg_info->byte_size); 1928061da546Spatrick 1929061da546Spatrick // Copy the register offsetted data to the buffer. 1930061da546Spatrick memcpy(regs_buffer.data() + reg_info->byte_offset, reg_value.GetBytes(), 1931061da546Spatrick reg_info->byte_size); 1932061da546Spatrick } 1933061da546Spatrick 1934061da546Spatrick // Write the response. 1935061da546Spatrick StreamGDBRemote response; 1936061da546Spatrick response.PutBytesAsRawHex8(regs_buffer.data(), regs_buffer.size()); 1937061da546Spatrick 1938061da546Spatrick return SendPacketNoLock(response.GetString()); 1939061da546Spatrick } 1940061da546Spatrick 1941061da546Spatrick GDBRemoteCommunication::PacketResult 1942061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_p(StringExtractorGDBRemote &packet) { 1943061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1944061da546Spatrick 1945061da546Spatrick // Parse out the register number from the request. 1946061da546Spatrick packet.SetFilePos(strlen("p")); 1947061da546Spatrick const uint32_t reg_index = 1948061da546Spatrick packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); 1949061da546Spatrick if (reg_index == std::numeric_limits<uint32_t>::max()) { 1950061da546Spatrick LLDB_LOGF(log, 1951061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, could not " 1952061da546Spatrick "parse register number from request \"%s\"", 1953061da546Spatrick __FUNCTION__, packet.GetStringRef().data()); 1954061da546Spatrick return SendErrorResponse(0x15); 1955061da546Spatrick } 1956061da546Spatrick 1957061da546Spatrick // Get the thread to use. 1958061da546Spatrick NativeThreadProtocol *thread = GetThreadFromSuffix(packet); 1959061da546Spatrick if (!thread) { 1960061da546Spatrick LLDB_LOG(log, "failed, no thread available"); 1961061da546Spatrick return SendErrorResponse(0x15); 1962061da546Spatrick } 1963061da546Spatrick 1964061da546Spatrick // Get the thread's register context. 1965061da546Spatrick NativeRegisterContext ®_context = thread->GetRegisterContext(); 1966061da546Spatrick 1967061da546Spatrick // Return the end of registers response if we've iterated one past the end of 1968061da546Spatrick // the register set. 1969061da546Spatrick if (reg_index >= reg_context.GetUserRegisterCount()) { 1970061da546Spatrick LLDB_LOGF(log, 1971061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, requested " 1972061da546Spatrick "register %" PRIu32 " beyond register count %" PRIu32, 1973061da546Spatrick __FUNCTION__, reg_index, reg_context.GetUserRegisterCount()); 1974061da546Spatrick return SendErrorResponse(0x15); 1975061da546Spatrick } 1976061da546Spatrick 1977061da546Spatrick const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index); 1978061da546Spatrick if (!reg_info) { 1979061da546Spatrick LLDB_LOGF(log, 1980061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, requested " 1981061da546Spatrick "register %" PRIu32 " returned NULL", 1982061da546Spatrick __FUNCTION__, reg_index); 1983061da546Spatrick return SendErrorResponse(0x15); 1984061da546Spatrick } 1985061da546Spatrick 1986061da546Spatrick // Build the reginfos response. 1987061da546Spatrick StreamGDBRemote response; 1988061da546Spatrick 1989061da546Spatrick // Retrieve the value 1990061da546Spatrick RegisterValue reg_value; 1991061da546Spatrick Status error = reg_context.ReadRegister(reg_info, reg_value); 1992061da546Spatrick if (error.Fail()) { 1993061da546Spatrick LLDB_LOGF(log, 1994061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, read of " 1995061da546Spatrick "requested register %" PRIu32 " (%s) failed: %s", 1996061da546Spatrick __FUNCTION__, reg_index, reg_info->name, error.AsCString()); 1997061da546Spatrick return SendErrorResponse(0x15); 1998061da546Spatrick } 1999061da546Spatrick 2000061da546Spatrick const uint8_t *const data = 2001061da546Spatrick static_cast<const uint8_t *>(reg_value.GetBytes()); 2002061da546Spatrick if (!data) { 2003061da546Spatrick LLDB_LOGF(log, 2004061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed to get data " 2005061da546Spatrick "bytes from requested register %" PRIu32, 2006061da546Spatrick __FUNCTION__, reg_index); 2007061da546Spatrick return SendErrorResponse(0x15); 2008061da546Spatrick } 2009061da546Spatrick 2010061da546Spatrick // FIXME flip as needed to get data in big/little endian format for this host. 2011061da546Spatrick for (uint32_t i = 0; i < reg_value.GetByteSize(); ++i) 2012061da546Spatrick response.PutHex8(data[i]); 2013061da546Spatrick 2014061da546Spatrick return SendPacketNoLock(response.GetString()); 2015061da546Spatrick } 2016061da546Spatrick 2017061da546Spatrick GDBRemoteCommunication::PacketResult 2018061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_P(StringExtractorGDBRemote &packet) { 2019061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2020061da546Spatrick 2021061da546Spatrick // Ensure there is more content. 2022061da546Spatrick if (packet.GetBytesLeft() < 1) 2023061da546Spatrick return SendIllFormedResponse(packet, "Empty P packet"); 2024061da546Spatrick 2025061da546Spatrick // Parse out the register number from the request. 2026061da546Spatrick packet.SetFilePos(strlen("P")); 2027061da546Spatrick const uint32_t reg_index = 2028061da546Spatrick packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); 2029061da546Spatrick if (reg_index == std::numeric_limits<uint32_t>::max()) { 2030061da546Spatrick LLDB_LOGF(log, 2031061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, could not " 2032061da546Spatrick "parse register number from request \"%s\"", 2033061da546Spatrick __FUNCTION__, packet.GetStringRef().data()); 2034061da546Spatrick return SendErrorResponse(0x29); 2035061da546Spatrick } 2036061da546Spatrick 2037061da546Spatrick // Note debugserver would send an E30 here. 2038061da546Spatrick if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != '=')) 2039061da546Spatrick return SendIllFormedResponse( 2040061da546Spatrick packet, "P packet missing '=' char after register number"); 2041061da546Spatrick 2042061da546Spatrick // Parse out the value. 2043*dda28197Spatrick uint8_t reg_bytes[RegisterValue::kMaxRegisterByteSize]; 2044061da546Spatrick size_t reg_size = packet.GetHexBytesAvail(reg_bytes); 2045061da546Spatrick 2046061da546Spatrick // Get the thread to use. 2047061da546Spatrick NativeThreadProtocol *thread = GetThreadFromSuffix(packet); 2048061da546Spatrick if (!thread) { 2049061da546Spatrick LLDB_LOGF(log, 2050061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no thread " 2051061da546Spatrick "available (thread index 0)", 2052061da546Spatrick __FUNCTION__); 2053061da546Spatrick return SendErrorResponse(0x28); 2054061da546Spatrick } 2055061da546Spatrick 2056061da546Spatrick // Get the thread's register context. 2057061da546Spatrick NativeRegisterContext ®_context = thread->GetRegisterContext(); 2058061da546Spatrick const RegisterInfo *reg_info = reg_context.GetRegisterInfoAtIndex(reg_index); 2059061da546Spatrick if (!reg_info) { 2060061da546Spatrick LLDB_LOGF(log, 2061061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, requested " 2062061da546Spatrick "register %" PRIu32 " returned NULL", 2063061da546Spatrick __FUNCTION__, reg_index); 2064061da546Spatrick return SendErrorResponse(0x48); 2065061da546Spatrick } 2066061da546Spatrick 2067061da546Spatrick // Return the end of registers response if we've iterated one past the end of 2068061da546Spatrick // the register set. 2069061da546Spatrick if (reg_index >= reg_context.GetUserRegisterCount()) { 2070061da546Spatrick LLDB_LOGF(log, 2071061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, requested " 2072061da546Spatrick "register %" PRIu32 " beyond register count %" PRIu32, 2073061da546Spatrick __FUNCTION__, reg_index, reg_context.GetUserRegisterCount()); 2074061da546Spatrick return SendErrorResponse(0x47); 2075061da546Spatrick } 2076061da546Spatrick 2077061da546Spatrick // The dwarf expression are evaluate on host site which may cause register 2078061da546Spatrick // size to change Hence the reg_size may not be same as reg_info->bytes_size 2079061da546Spatrick if ((reg_size != reg_info->byte_size) && 2080061da546Spatrick !(reg_info->dynamic_size_dwarf_expr_bytes)) { 2081061da546Spatrick return SendIllFormedResponse(packet, "P packet register size is incorrect"); 2082061da546Spatrick } 2083061da546Spatrick 2084061da546Spatrick // Build the reginfos response. 2085061da546Spatrick StreamGDBRemote response; 2086061da546Spatrick 2087061da546Spatrick RegisterValue reg_value( 2088061da546Spatrick reg_bytes, reg_size, 2089061da546Spatrick m_debugged_process_up->GetArchitecture().GetByteOrder()); 2090061da546Spatrick Status error = reg_context.WriteRegister(reg_info, reg_value); 2091061da546Spatrick if (error.Fail()) { 2092061da546Spatrick LLDB_LOGF(log, 2093061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, write of " 2094061da546Spatrick "requested register %" PRIu32 " (%s) failed: %s", 2095061da546Spatrick __FUNCTION__, reg_index, reg_info->name, error.AsCString()); 2096061da546Spatrick return SendErrorResponse(0x32); 2097061da546Spatrick } 2098061da546Spatrick 2099061da546Spatrick return SendOKResponse(); 2100061da546Spatrick } 2101061da546Spatrick 2102061da546Spatrick GDBRemoteCommunication::PacketResult 2103061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_H(StringExtractorGDBRemote &packet) { 2104061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2105061da546Spatrick 2106061da546Spatrick // Fail if we don't have a current process. 2107061da546Spatrick if (!m_debugged_process_up || 2108061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2109061da546Spatrick LLDB_LOGF( 2110061da546Spatrick log, 2111061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 2112061da546Spatrick __FUNCTION__); 2113061da546Spatrick return SendErrorResponse(0x15); 2114061da546Spatrick } 2115061da546Spatrick 2116061da546Spatrick // Parse out which variant of $H is requested. 2117061da546Spatrick packet.SetFilePos(strlen("H")); 2118061da546Spatrick if (packet.GetBytesLeft() < 1) { 2119061da546Spatrick LLDB_LOGF(log, 2120061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, H command " 2121061da546Spatrick "missing {g,c} variant", 2122061da546Spatrick __FUNCTION__); 2123061da546Spatrick return SendIllFormedResponse(packet, "H command missing {g,c} variant"); 2124061da546Spatrick } 2125061da546Spatrick 2126061da546Spatrick const char h_variant = packet.GetChar(); 2127061da546Spatrick switch (h_variant) { 2128061da546Spatrick case 'g': 2129061da546Spatrick break; 2130061da546Spatrick 2131061da546Spatrick case 'c': 2132061da546Spatrick break; 2133061da546Spatrick 2134061da546Spatrick default: 2135061da546Spatrick LLDB_LOGF( 2136061da546Spatrick log, 2137061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c", 2138061da546Spatrick __FUNCTION__, h_variant); 2139061da546Spatrick return SendIllFormedResponse(packet, 2140061da546Spatrick "H variant unsupported, should be c or g"); 2141061da546Spatrick } 2142061da546Spatrick 2143061da546Spatrick // Parse out the thread number. 2144061da546Spatrick // FIXME return a parse success/fail value. All values are valid here. 2145061da546Spatrick const lldb::tid_t tid = 2146061da546Spatrick packet.GetHexMaxU64(false, std::numeric_limits<lldb::tid_t>::max()); 2147061da546Spatrick 2148061da546Spatrick // Ensure we have the given thread when not specifying -1 (all threads) or 0 2149061da546Spatrick // (any thread). 2150061da546Spatrick if (tid != LLDB_INVALID_THREAD_ID && tid != 0) { 2151061da546Spatrick NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid); 2152061da546Spatrick if (!thread) { 2153061da546Spatrick LLDB_LOGF(log, 2154061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64 2155061da546Spatrick " not found", 2156061da546Spatrick __FUNCTION__, tid); 2157061da546Spatrick return SendErrorResponse(0x15); 2158061da546Spatrick } 2159061da546Spatrick } 2160061da546Spatrick 2161061da546Spatrick // Now switch the given thread type. 2162061da546Spatrick switch (h_variant) { 2163061da546Spatrick case 'g': 2164061da546Spatrick SetCurrentThreadID(tid); 2165061da546Spatrick break; 2166061da546Spatrick 2167061da546Spatrick case 'c': 2168061da546Spatrick SetContinueThreadID(tid); 2169061da546Spatrick break; 2170061da546Spatrick 2171061da546Spatrick default: 2172061da546Spatrick assert(false && "unsupported $H variant - shouldn't get here"); 2173061da546Spatrick return SendIllFormedResponse(packet, 2174061da546Spatrick "H variant unsupported, should be c or g"); 2175061da546Spatrick } 2176061da546Spatrick 2177061da546Spatrick return SendOKResponse(); 2178061da546Spatrick } 2179061da546Spatrick 2180061da546Spatrick GDBRemoteCommunication::PacketResult 2181061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_I(StringExtractorGDBRemote &packet) { 2182061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2183061da546Spatrick 2184061da546Spatrick // Fail if we don't have a current process. 2185061da546Spatrick if (!m_debugged_process_up || 2186061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2187061da546Spatrick LLDB_LOGF( 2188061da546Spatrick log, 2189061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 2190061da546Spatrick __FUNCTION__); 2191061da546Spatrick return SendErrorResponse(0x15); 2192061da546Spatrick } 2193061da546Spatrick 2194061da546Spatrick packet.SetFilePos(::strlen("I")); 2195061da546Spatrick uint8_t tmp[4096]; 2196061da546Spatrick for (;;) { 2197061da546Spatrick size_t read = packet.GetHexBytesAvail(tmp); 2198061da546Spatrick if (read == 0) { 2199061da546Spatrick break; 2200061da546Spatrick } 2201061da546Spatrick // write directly to stdin *this might block if stdin buffer is full* 2202061da546Spatrick // TODO: enqueue this block in circular buffer and send window size to 2203061da546Spatrick // remote host 2204061da546Spatrick ConnectionStatus status; 2205061da546Spatrick Status error; 2206061da546Spatrick m_stdio_communication.Write(tmp, read, status, &error); 2207061da546Spatrick if (error.Fail()) { 2208061da546Spatrick return SendErrorResponse(0x15); 2209061da546Spatrick } 2210061da546Spatrick } 2211061da546Spatrick 2212061da546Spatrick return SendOKResponse(); 2213061da546Spatrick } 2214061da546Spatrick 2215061da546Spatrick GDBRemoteCommunication::PacketResult 2216061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_interrupt( 2217061da546Spatrick StringExtractorGDBRemote &packet) { 2218061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2219061da546Spatrick 2220061da546Spatrick // Fail if we don't have a current process. 2221061da546Spatrick if (!m_debugged_process_up || 2222061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2223061da546Spatrick LLDB_LOG(log, "failed, no process available"); 2224061da546Spatrick return SendErrorResponse(0x15); 2225061da546Spatrick } 2226061da546Spatrick 2227061da546Spatrick // Interrupt the process. 2228061da546Spatrick Status error = m_debugged_process_up->Interrupt(); 2229061da546Spatrick if (error.Fail()) { 2230061da546Spatrick LLDB_LOG(log, "failed for process {0}: {1}", m_debugged_process_up->GetID(), 2231061da546Spatrick error); 2232061da546Spatrick return SendErrorResponse(GDBRemoteServerError::eErrorResume); 2233061da546Spatrick } 2234061da546Spatrick 2235061da546Spatrick LLDB_LOG(log, "stopped process {0}", m_debugged_process_up->GetID()); 2236061da546Spatrick 2237061da546Spatrick // No response required from stop all. 2238061da546Spatrick return PacketResult::Success; 2239061da546Spatrick } 2240061da546Spatrick 2241061da546Spatrick GDBRemoteCommunication::PacketResult 2242061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_memory_read( 2243061da546Spatrick StringExtractorGDBRemote &packet) { 2244061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2245061da546Spatrick 2246061da546Spatrick if (!m_debugged_process_up || 2247061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2248061da546Spatrick LLDB_LOGF( 2249061da546Spatrick log, 2250061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 2251061da546Spatrick __FUNCTION__); 2252061da546Spatrick return SendErrorResponse(0x15); 2253061da546Spatrick } 2254061da546Spatrick 2255061da546Spatrick // Parse out the memory address. 2256061da546Spatrick packet.SetFilePos(strlen("m")); 2257061da546Spatrick if (packet.GetBytesLeft() < 1) 2258061da546Spatrick return SendIllFormedResponse(packet, "Too short m packet"); 2259061da546Spatrick 2260061da546Spatrick // Read the address. Punting on validation. 2261061da546Spatrick // FIXME replace with Hex U64 read with no default value that fails on failed 2262061da546Spatrick // read. 2263061da546Spatrick const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0); 2264061da546Spatrick 2265061da546Spatrick // Validate comma. 2266061da546Spatrick if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ',')) 2267061da546Spatrick return SendIllFormedResponse(packet, "Comma sep missing in m packet"); 2268061da546Spatrick 2269061da546Spatrick // Get # bytes to read. 2270061da546Spatrick if (packet.GetBytesLeft() < 1) 2271061da546Spatrick return SendIllFormedResponse(packet, "Length missing in m packet"); 2272061da546Spatrick 2273061da546Spatrick const uint64_t byte_count = packet.GetHexMaxU64(false, 0); 2274061da546Spatrick if (byte_count == 0) { 2275061da546Spatrick LLDB_LOGF(log, 2276061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s nothing to read: " 2277061da546Spatrick "zero-length packet", 2278061da546Spatrick __FUNCTION__); 2279061da546Spatrick return SendOKResponse(); 2280061da546Spatrick } 2281061da546Spatrick 2282061da546Spatrick // Allocate the response buffer. 2283061da546Spatrick std::string buf(byte_count, '\0'); 2284061da546Spatrick if (buf.empty()) 2285061da546Spatrick return SendErrorResponse(0x78); 2286061da546Spatrick 2287061da546Spatrick // Retrieve the process memory. 2288061da546Spatrick size_t bytes_read = 0; 2289061da546Spatrick Status error = m_debugged_process_up->ReadMemoryWithoutTrap( 2290061da546Spatrick read_addr, &buf[0], byte_count, bytes_read); 2291061da546Spatrick if (error.Fail()) { 2292061da546Spatrick LLDB_LOGF(log, 2293061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 2294061da546Spatrick " mem 0x%" PRIx64 ": failed to read. Error: %s", 2295061da546Spatrick __FUNCTION__, m_debugged_process_up->GetID(), read_addr, 2296061da546Spatrick error.AsCString()); 2297061da546Spatrick return SendErrorResponse(0x08); 2298061da546Spatrick } 2299061da546Spatrick 2300061da546Spatrick if (bytes_read == 0) { 2301061da546Spatrick LLDB_LOGF(log, 2302061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 2303061da546Spatrick " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes", 2304061da546Spatrick __FUNCTION__, m_debugged_process_up->GetID(), read_addr, 2305061da546Spatrick byte_count); 2306061da546Spatrick return SendErrorResponse(0x08); 2307061da546Spatrick } 2308061da546Spatrick 2309061da546Spatrick StreamGDBRemote response; 2310061da546Spatrick packet.SetFilePos(0); 2311061da546Spatrick char kind = packet.GetChar('?'); 2312061da546Spatrick if (kind == 'x') 2313061da546Spatrick response.PutEscapedBytes(buf.data(), byte_count); 2314061da546Spatrick else { 2315061da546Spatrick assert(kind == 'm'); 2316061da546Spatrick for (size_t i = 0; i < bytes_read; ++i) 2317061da546Spatrick response.PutHex8(buf[i]); 2318061da546Spatrick } 2319061da546Spatrick 2320061da546Spatrick return SendPacketNoLock(response.GetString()); 2321061da546Spatrick } 2322061da546Spatrick 2323061da546Spatrick GDBRemoteCommunication::PacketResult 2324061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_M(StringExtractorGDBRemote &packet) { 2325061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2326061da546Spatrick 2327061da546Spatrick if (!m_debugged_process_up || 2328061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2329061da546Spatrick LLDB_LOGF( 2330061da546Spatrick log, 2331061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 2332061da546Spatrick __FUNCTION__); 2333061da546Spatrick return SendErrorResponse(0x15); 2334061da546Spatrick } 2335061da546Spatrick 2336061da546Spatrick // Parse out the memory address. 2337061da546Spatrick packet.SetFilePos(strlen("M")); 2338061da546Spatrick if (packet.GetBytesLeft() < 1) 2339061da546Spatrick return SendIllFormedResponse(packet, "Too short M packet"); 2340061da546Spatrick 2341061da546Spatrick // Read the address. Punting on validation. 2342061da546Spatrick // FIXME replace with Hex U64 read with no default value that fails on failed 2343061da546Spatrick // read. 2344061da546Spatrick const lldb::addr_t write_addr = packet.GetHexMaxU64(false, 0); 2345061da546Spatrick 2346061da546Spatrick // Validate comma. 2347061da546Spatrick if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ',')) 2348061da546Spatrick return SendIllFormedResponse(packet, "Comma sep missing in M packet"); 2349061da546Spatrick 2350061da546Spatrick // Get # bytes to read. 2351061da546Spatrick if (packet.GetBytesLeft() < 1) 2352061da546Spatrick return SendIllFormedResponse(packet, "Length missing in M packet"); 2353061da546Spatrick 2354061da546Spatrick const uint64_t byte_count = packet.GetHexMaxU64(false, 0); 2355061da546Spatrick if (byte_count == 0) { 2356061da546Spatrick LLDB_LOG(log, "nothing to write: zero-length packet"); 2357061da546Spatrick return PacketResult::Success; 2358061da546Spatrick } 2359061da546Spatrick 2360061da546Spatrick // Validate colon. 2361061da546Spatrick if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ':')) 2362061da546Spatrick return SendIllFormedResponse( 2363061da546Spatrick packet, "Comma sep missing in M packet after byte length"); 2364061da546Spatrick 2365061da546Spatrick // Allocate the conversion buffer. 2366061da546Spatrick std::vector<uint8_t> buf(byte_count, 0); 2367061da546Spatrick if (buf.empty()) 2368061da546Spatrick return SendErrorResponse(0x78); 2369061da546Spatrick 2370061da546Spatrick // Convert the hex memory write contents to bytes. 2371061da546Spatrick StreamGDBRemote response; 2372061da546Spatrick const uint64_t convert_count = packet.GetHexBytes(buf, 0); 2373061da546Spatrick if (convert_count != byte_count) { 2374061da546Spatrick LLDB_LOG(log, 2375061da546Spatrick "pid {0} mem {1:x}: asked to write {2} bytes, but only found {3} " 2376061da546Spatrick "to convert.", 2377061da546Spatrick m_debugged_process_up->GetID(), write_addr, byte_count, 2378061da546Spatrick convert_count); 2379061da546Spatrick return SendIllFormedResponse(packet, "M content byte length specified did " 2380061da546Spatrick "not match hex-encoded content " 2381061da546Spatrick "length"); 2382061da546Spatrick } 2383061da546Spatrick 2384061da546Spatrick // Write the process memory. 2385061da546Spatrick size_t bytes_written = 0; 2386061da546Spatrick Status error = m_debugged_process_up->WriteMemory(write_addr, &buf[0], 2387061da546Spatrick byte_count, bytes_written); 2388061da546Spatrick if (error.Fail()) { 2389061da546Spatrick LLDB_LOG(log, "pid {0} mem {1:x}: failed to write. Error: {2}", 2390061da546Spatrick m_debugged_process_up->GetID(), write_addr, error); 2391061da546Spatrick return SendErrorResponse(0x09); 2392061da546Spatrick } 2393061da546Spatrick 2394061da546Spatrick if (bytes_written == 0) { 2395061da546Spatrick LLDB_LOG(log, "pid {0} mem {1:x}: wrote 0 of {2} requested bytes", 2396061da546Spatrick m_debugged_process_up->GetID(), write_addr, byte_count); 2397061da546Spatrick return SendErrorResponse(0x09); 2398061da546Spatrick } 2399061da546Spatrick 2400061da546Spatrick return SendOKResponse(); 2401061da546Spatrick } 2402061da546Spatrick 2403061da546Spatrick GDBRemoteCommunication::PacketResult 2404061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported( 2405061da546Spatrick StringExtractorGDBRemote &packet) { 2406061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2407061da546Spatrick 2408061da546Spatrick // Currently only the NativeProcessProtocol knows if it can handle a 2409061da546Spatrick // qMemoryRegionInfoSupported request, but we're not guaranteed to be 2410061da546Spatrick // attached to a process. For now we'll assume the client only asks this 2411061da546Spatrick // when a process is being debugged. 2412061da546Spatrick 2413061da546Spatrick // Ensure we have a process running; otherwise, we can't figure this out 2414061da546Spatrick // since we won't have a NativeProcessProtocol. 2415061da546Spatrick if (!m_debugged_process_up || 2416061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2417061da546Spatrick LLDB_LOGF( 2418061da546Spatrick log, 2419061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 2420061da546Spatrick __FUNCTION__); 2421061da546Spatrick return SendErrorResponse(0x15); 2422061da546Spatrick } 2423061da546Spatrick 2424061da546Spatrick // Test if we can get any region back when asking for the region around NULL. 2425061da546Spatrick MemoryRegionInfo region_info; 2426061da546Spatrick const Status error = 2427061da546Spatrick m_debugged_process_up->GetMemoryRegionInfo(0, region_info); 2428061da546Spatrick if (error.Fail()) { 2429061da546Spatrick // We don't support memory region info collection for this 2430061da546Spatrick // NativeProcessProtocol. 2431061da546Spatrick return SendUnimplementedResponse(""); 2432061da546Spatrick } 2433061da546Spatrick 2434061da546Spatrick return SendOKResponse(); 2435061da546Spatrick } 2436061da546Spatrick 2437061da546Spatrick GDBRemoteCommunication::PacketResult 2438061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo( 2439061da546Spatrick StringExtractorGDBRemote &packet) { 2440061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2441061da546Spatrick 2442061da546Spatrick // Ensure we have a process. 2443061da546Spatrick if (!m_debugged_process_up || 2444061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2445061da546Spatrick LLDB_LOGF( 2446061da546Spatrick log, 2447061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 2448061da546Spatrick __FUNCTION__); 2449061da546Spatrick return SendErrorResponse(0x15); 2450061da546Spatrick } 2451061da546Spatrick 2452061da546Spatrick // Parse out the memory address. 2453061da546Spatrick packet.SetFilePos(strlen("qMemoryRegionInfo:")); 2454061da546Spatrick if (packet.GetBytesLeft() < 1) 2455061da546Spatrick return SendIllFormedResponse(packet, "Too short qMemoryRegionInfo: packet"); 2456061da546Spatrick 2457061da546Spatrick // Read the address. Punting on validation. 2458061da546Spatrick const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0); 2459061da546Spatrick 2460061da546Spatrick StreamGDBRemote response; 2461061da546Spatrick 2462061da546Spatrick // Get the memory region info for the target address. 2463061da546Spatrick MemoryRegionInfo region_info; 2464061da546Spatrick const Status error = 2465061da546Spatrick m_debugged_process_up->GetMemoryRegionInfo(read_addr, region_info); 2466061da546Spatrick if (error.Fail()) { 2467061da546Spatrick // Return the error message. 2468061da546Spatrick 2469061da546Spatrick response.PutCString("error:"); 2470061da546Spatrick response.PutStringAsRawHex8(error.AsCString()); 2471061da546Spatrick response.PutChar(';'); 2472061da546Spatrick } else { 2473061da546Spatrick // Range start and size. 2474061da546Spatrick response.Printf("start:%" PRIx64 ";size:%" PRIx64 ";", 2475061da546Spatrick region_info.GetRange().GetRangeBase(), 2476061da546Spatrick region_info.GetRange().GetByteSize()); 2477061da546Spatrick 2478061da546Spatrick // Permissions. 2479061da546Spatrick if (region_info.GetReadable() || region_info.GetWritable() || 2480061da546Spatrick region_info.GetExecutable()) { 2481061da546Spatrick // Write permissions info. 2482061da546Spatrick response.PutCString("permissions:"); 2483061da546Spatrick 2484061da546Spatrick if (region_info.GetReadable()) 2485061da546Spatrick response.PutChar('r'); 2486061da546Spatrick if (region_info.GetWritable()) 2487061da546Spatrick response.PutChar('w'); 2488061da546Spatrick if (region_info.GetExecutable()) 2489061da546Spatrick response.PutChar('x'); 2490061da546Spatrick 2491061da546Spatrick response.PutChar(';'); 2492061da546Spatrick } 2493061da546Spatrick 2494061da546Spatrick // Name 2495061da546Spatrick ConstString name = region_info.GetName(); 2496061da546Spatrick if (name) { 2497061da546Spatrick response.PutCString("name:"); 2498*dda28197Spatrick response.PutStringAsRawHex8(name.GetStringRef()); 2499061da546Spatrick response.PutChar(';'); 2500061da546Spatrick } 2501061da546Spatrick } 2502061da546Spatrick 2503061da546Spatrick return SendPacketNoLock(response.GetString()); 2504061da546Spatrick } 2505061da546Spatrick 2506061da546Spatrick GDBRemoteCommunication::PacketResult 2507061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_Z(StringExtractorGDBRemote &packet) { 2508061da546Spatrick // Ensure we have a process. 2509061da546Spatrick if (!m_debugged_process_up || 2510061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2511061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2512061da546Spatrick LLDB_LOG(log, "failed, no process available"); 2513061da546Spatrick return SendErrorResponse(0x15); 2514061da546Spatrick } 2515061da546Spatrick 2516061da546Spatrick // Parse out software or hardware breakpoint or watchpoint requested. 2517061da546Spatrick packet.SetFilePos(strlen("Z")); 2518061da546Spatrick if (packet.GetBytesLeft() < 1) 2519061da546Spatrick return SendIllFormedResponse( 2520061da546Spatrick packet, "Too short Z packet, missing software/hardware specifier"); 2521061da546Spatrick 2522061da546Spatrick bool want_breakpoint = true; 2523061da546Spatrick bool want_hardware = false; 2524061da546Spatrick uint32_t watch_flags = 0; 2525061da546Spatrick 2526061da546Spatrick const GDBStoppointType stoppoint_type = 2527061da546Spatrick GDBStoppointType(packet.GetS32(eStoppointInvalid)); 2528061da546Spatrick switch (stoppoint_type) { 2529061da546Spatrick case eBreakpointSoftware: 2530061da546Spatrick want_hardware = false; 2531061da546Spatrick want_breakpoint = true; 2532061da546Spatrick break; 2533061da546Spatrick case eBreakpointHardware: 2534061da546Spatrick want_hardware = true; 2535061da546Spatrick want_breakpoint = true; 2536061da546Spatrick break; 2537061da546Spatrick case eWatchpointWrite: 2538061da546Spatrick watch_flags = 1; 2539061da546Spatrick want_hardware = true; 2540061da546Spatrick want_breakpoint = false; 2541061da546Spatrick break; 2542061da546Spatrick case eWatchpointRead: 2543061da546Spatrick watch_flags = 2; 2544061da546Spatrick want_hardware = true; 2545061da546Spatrick want_breakpoint = false; 2546061da546Spatrick break; 2547061da546Spatrick case eWatchpointReadWrite: 2548061da546Spatrick watch_flags = 3; 2549061da546Spatrick want_hardware = true; 2550061da546Spatrick want_breakpoint = false; 2551061da546Spatrick break; 2552061da546Spatrick case eStoppointInvalid: 2553061da546Spatrick return SendIllFormedResponse( 2554061da546Spatrick packet, "Z packet had invalid software/hardware specifier"); 2555061da546Spatrick } 2556061da546Spatrick 2557061da546Spatrick if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') 2558061da546Spatrick return SendIllFormedResponse( 2559061da546Spatrick packet, "Malformed Z packet, expecting comma after stoppoint type"); 2560061da546Spatrick 2561061da546Spatrick // Parse out the stoppoint address. 2562061da546Spatrick if (packet.GetBytesLeft() < 1) 2563061da546Spatrick return SendIllFormedResponse(packet, "Too short Z packet, missing address"); 2564061da546Spatrick const lldb::addr_t addr = packet.GetHexMaxU64(false, 0); 2565061da546Spatrick 2566061da546Spatrick if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') 2567061da546Spatrick return SendIllFormedResponse( 2568061da546Spatrick packet, "Malformed Z packet, expecting comma after address"); 2569061da546Spatrick 2570061da546Spatrick // Parse out the stoppoint size (i.e. size hint for opcode size). 2571061da546Spatrick const uint32_t size = 2572061da546Spatrick packet.GetHexMaxU32(false, std::numeric_limits<uint32_t>::max()); 2573061da546Spatrick if (size == std::numeric_limits<uint32_t>::max()) 2574061da546Spatrick return SendIllFormedResponse( 2575061da546Spatrick packet, "Malformed Z packet, failed to parse size argument"); 2576061da546Spatrick 2577061da546Spatrick if (want_breakpoint) { 2578061da546Spatrick // Try to set the breakpoint. 2579061da546Spatrick const Status error = 2580061da546Spatrick m_debugged_process_up->SetBreakpoint(addr, size, want_hardware); 2581061da546Spatrick if (error.Success()) 2582061da546Spatrick return SendOKResponse(); 2583061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 2584061da546Spatrick LLDB_LOG(log, "pid {0} failed to set breakpoint: {1}", 2585061da546Spatrick m_debugged_process_up->GetID(), error); 2586061da546Spatrick return SendErrorResponse(0x09); 2587061da546Spatrick } else { 2588061da546Spatrick // Try to set the watchpoint. 2589061da546Spatrick const Status error = m_debugged_process_up->SetWatchpoint( 2590061da546Spatrick addr, size, watch_flags, want_hardware); 2591061da546Spatrick if (error.Success()) 2592061da546Spatrick return SendOKResponse(); 2593061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 2594061da546Spatrick LLDB_LOG(log, "pid {0} failed to set watchpoint: {1}", 2595061da546Spatrick m_debugged_process_up->GetID(), error); 2596061da546Spatrick return SendErrorResponse(0x09); 2597061da546Spatrick } 2598061da546Spatrick } 2599061da546Spatrick 2600061da546Spatrick GDBRemoteCommunication::PacketResult 2601061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_z(StringExtractorGDBRemote &packet) { 2602061da546Spatrick // Ensure we have a process. 2603061da546Spatrick if (!m_debugged_process_up || 2604061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2605061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2606061da546Spatrick LLDB_LOG(log, "failed, no process available"); 2607061da546Spatrick return SendErrorResponse(0x15); 2608061da546Spatrick } 2609061da546Spatrick 2610061da546Spatrick // Parse out software or hardware breakpoint or watchpoint requested. 2611061da546Spatrick packet.SetFilePos(strlen("z")); 2612061da546Spatrick if (packet.GetBytesLeft() < 1) 2613061da546Spatrick return SendIllFormedResponse( 2614061da546Spatrick packet, "Too short z packet, missing software/hardware specifier"); 2615061da546Spatrick 2616061da546Spatrick bool want_breakpoint = true; 2617061da546Spatrick bool want_hardware = false; 2618061da546Spatrick 2619061da546Spatrick const GDBStoppointType stoppoint_type = 2620061da546Spatrick GDBStoppointType(packet.GetS32(eStoppointInvalid)); 2621061da546Spatrick switch (stoppoint_type) { 2622061da546Spatrick case eBreakpointHardware: 2623061da546Spatrick want_breakpoint = true; 2624061da546Spatrick want_hardware = true; 2625061da546Spatrick break; 2626061da546Spatrick case eBreakpointSoftware: 2627061da546Spatrick want_breakpoint = true; 2628061da546Spatrick break; 2629061da546Spatrick case eWatchpointWrite: 2630061da546Spatrick want_breakpoint = false; 2631061da546Spatrick break; 2632061da546Spatrick case eWatchpointRead: 2633061da546Spatrick want_breakpoint = false; 2634061da546Spatrick break; 2635061da546Spatrick case eWatchpointReadWrite: 2636061da546Spatrick want_breakpoint = false; 2637061da546Spatrick break; 2638061da546Spatrick default: 2639061da546Spatrick return SendIllFormedResponse( 2640061da546Spatrick packet, "z packet had invalid software/hardware specifier"); 2641061da546Spatrick } 2642061da546Spatrick 2643061da546Spatrick if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') 2644061da546Spatrick return SendIllFormedResponse( 2645061da546Spatrick packet, "Malformed z packet, expecting comma after stoppoint type"); 2646061da546Spatrick 2647061da546Spatrick // Parse out the stoppoint address. 2648061da546Spatrick if (packet.GetBytesLeft() < 1) 2649061da546Spatrick return SendIllFormedResponse(packet, "Too short z packet, missing address"); 2650061da546Spatrick const lldb::addr_t addr = packet.GetHexMaxU64(false, 0); 2651061da546Spatrick 2652061da546Spatrick if ((packet.GetBytesLeft() < 1) || packet.GetChar() != ',') 2653061da546Spatrick return SendIllFormedResponse( 2654061da546Spatrick packet, "Malformed z packet, expecting comma after address"); 2655061da546Spatrick 2656061da546Spatrick /* 2657061da546Spatrick // Parse out the stoppoint size (i.e. size hint for opcode size). 2658061da546Spatrick const uint32_t size = packet.GetHexMaxU32 (false, 2659061da546Spatrick std::numeric_limits<uint32_t>::max ()); 2660061da546Spatrick if (size == std::numeric_limits<uint32_t>::max ()) 2661061da546Spatrick return SendIllFormedResponse(packet, "Malformed z packet, failed to parse 2662061da546Spatrick size argument"); 2663061da546Spatrick */ 2664061da546Spatrick 2665061da546Spatrick if (want_breakpoint) { 2666061da546Spatrick // Try to clear the breakpoint. 2667061da546Spatrick const Status error = 2668061da546Spatrick m_debugged_process_up->RemoveBreakpoint(addr, want_hardware); 2669061da546Spatrick if (error.Success()) 2670061da546Spatrick return SendOKResponse(); 2671061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 2672061da546Spatrick LLDB_LOG(log, "pid {0} failed to remove breakpoint: {1}", 2673061da546Spatrick m_debugged_process_up->GetID(), error); 2674061da546Spatrick return SendErrorResponse(0x09); 2675061da546Spatrick } else { 2676061da546Spatrick // Try to clear the watchpoint. 2677061da546Spatrick const Status error = m_debugged_process_up->RemoveWatchpoint(addr); 2678061da546Spatrick if (error.Success()) 2679061da546Spatrick return SendOKResponse(); 2680061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 2681061da546Spatrick LLDB_LOG(log, "pid {0} failed to remove watchpoint: {1}", 2682061da546Spatrick m_debugged_process_up->GetID(), error); 2683061da546Spatrick return SendErrorResponse(0x09); 2684061da546Spatrick } 2685061da546Spatrick } 2686061da546Spatrick 2687061da546Spatrick GDBRemoteCommunication::PacketResult 2688061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_s(StringExtractorGDBRemote &packet) { 2689061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2690061da546Spatrick 2691061da546Spatrick // Ensure we have a process. 2692061da546Spatrick if (!m_debugged_process_up || 2693061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2694061da546Spatrick LLDB_LOGF( 2695061da546Spatrick log, 2696061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 2697061da546Spatrick __FUNCTION__); 2698061da546Spatrick return SendErrorResponse(0x32); 2699061da546Spatrick } 2700061da546Spatrick 2701061da546Spatrick // We first try to use a continue thread id. If any one or any all set, use 2702061da546Spatrick // the current thread. Bail out if we don't have a thread id. 2703061da546Spatrick lldb::tid_t tid = GetContinueThreadID(); 2704061da546Spatrick if (tid == 0 || tid == LLDB_INVALID_THREAD_ID) 2705061da546Spatrick tid = GetCurrentThreadID(); 2706061da546Spatrick if (tid == LLDB_INVALID_THREAD_ID) 2707061da546Spatrick return SendErrorResponse(0x33); 2708061da546Spatrick 2709061da546Spatrick // Double check that we have such a thread. 2710061da546Spatrick // TODO investigate: on MacOSX we might need to do an UpdateThreads () here. 2711061da546Spatrick NativeThreadProtocol *thread = m_debugged_process_up->GetThreadByID(tid); 2712061da546Spatrick if (!thread) 2713061da546Spatrick return SendErrorResponse(0x33); 2714061da546Spatrick 2715061da546Spatrick // Create the step action for the given thread. 2716061da546Spatrick ResumeAction action = {tid, eStateStepping, LLDB_INVALID_SIGNAL_NUMBER}; 2717061da546Spatrick 2718061da546Spatrick // Setup the actions list. 2719061da546Spatrick ResumeActionList actions; 2720061da546Spatrick actions.Append(action); 2721061da546Spatrick 2722061da546Spatrick // All other threads stop while we're single stepping a thread. 2723061da546Spatrick actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0); 2724061da546Spatrick Status error = m_debugged_process_up->Resume(actions); 2725061da546Spatrick if (error.Fail()) { 2726061da546Spatrick LLDB_LOGF(log, 2727061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 2728061da546Spatrick " tid %" PRIu64 " Resume() failed with error: %s", 2729061da546Spatrick __FUNCTION__, m_debugged_process_up->GetID(), tid, 2730061da546Spatrick error.AsCString()); 2731061da546Spatrick return SendErrorResponse(0x49); 2732061da546Spatrick } 2733061da546Spatrick 2734061da546Spatrick // No response here - the stop or exit will come from the resulting action. 2735061da546Spatrick return PacketResult::Success; 2736061da546Spatrick } 2737061da546Spatrick 2738061da546Spatrick llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> 2739*dda28197Spatrick GDBRemoteCommunicationServerLLGS::BuildTargetXml() { 2740*dda28197Spatrick // Ensure we have a thread. 2741*dda28197Spatrick NativeThreadProtocol *thread = m_debugged_process_up->GetThreadAtIndex(0); 2742*dda28197Spatrick if (!thread) 2743*dda28197Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 2744*dda28197Spatrick "No thread available"); 2745*dda28197Spatrick 2746*dda28197Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2747*dda28197Spatrick // Get the register context for the first thread. 2748*dda28197Spatrick NativeRegisterContext ®_context = thread->GetRegisterContext(); 2749*dda28197Spatrick 2750*dda28197Spatrick StreamString response; 2751*dda28197Spatrick 2752*dda28197Spatrick response.Printf("<?xml version=\"1.0\"?>"); 2753*dda28197Spatrick response.Printf("<target version=\"1.0\">"); 2754*dda28197Spatrick 2755*dda28197Spatrick response.Printf("<architecture>%s</architecture>", 2756*dda28197Spatrick m_debugged_process_up->GetArchitecture() 2757*dda28197Spatrick .GetTriple() 2758*dda28197Spatrick .getArchName() 2759*dda28197Spatrick .str() 2760*dda28197Spatrick .c_str()); 2761*dda28197Spatrick 2762*dda28197Spatrick response.Printf("<feature>"); 2763*dda28197Spatrick 2764*dda28197Spatrick const int registers_count = reg_context.GetUserRegisterCount(); 2765*dda28197Spatrick for (int reg_index = 0; reg_index < registers_count; reg_index++) { 2766*dda28197Spatrick const RegisterInfo *reg_info = 2767*dda28197Spatrick reg_context.GetRegisterInfoAtIndex(reg_index); 2768*dda28197Spatrick 2769*dda28197Spatrick if (!reg_info) { 2770*dda28197Spatrick LLDB_LOGF(log, 2771*dda28197Spatrick "%s failed to get register info for register index %" PRIu32, 2772*dda28197Spatrick "target.xml", reg_index); 2773*dda28197Spatrick continue; 2774*dda28197Spatrick } 2775*dda28197Spatrick 2776*dda28197Spatrick response.Printf("<reg name=\"%s\" bitsize=\"%" PRIu32 "\" offset=\"%" PRIu32 2777*dda28197Spatrick "\" regnum=\"%d\" ", 2778*dda28197Spatrick reg_info->name, reg_info->byte_size * 8, 2779*dda28197Spatrick reg_info->byte_offset, reg_index); 2780*dda28197Spatrick 2781*dda28197Spatrick if (reg_info->alt_name && reg_info->alt_name[0]) 2782*dda28197Spatrick response.Printf("altname=\"%s\" ", reg_info->alt_name); 2783*dda28197Spatrick 2784*dda28197Spatrick llvm::StringRef encoding = GetEncodingNameOrEmpty(*reg_info); 2785*dda28197Spatrick if (!encoding.empty()) 2786*dda28197Spatrick response << "encoding=\"" << encoding << "\" "; 2787*dda28197Spatrick 2788*dda28197Spatrick llvm::StringRef format = GetFormatNameOrEmpty(*reg_info); 2789*dda28197Spatrick if (!format.empty()) 2790*dda28197Spatrick response << "format=\"" << format << "\" "; 2791*dda28197Spatrick 2792*dda28197Spatrick const char *const register_set_name = 2793*dda28197Spatrick reg_context.GetRegisterSetNameForRegisterAtIndex(reg_index); 2794*dda28197Spatrick if (register_set_name) 2795*dda28197Spatrick response << "group=\"" << register_set_name << "\" "; 2796*dda28197Spatrick 2797*dda28197Spatrick if (reg_info->kinds[RegisterKind::eRegisterKindEHFrame] != 2798*dda28197Spatrick LLDB_INVALID_REGNUM) 2799*dda28197Spatrick response.Printf("ehframe_regnum=\"%" PRIu32 "\" ", 2800*dda28197Spatrick reg_info->kinds[RegisterKind::eRegisterKindEHFrame]); 2801*dda28197Spatrick 2802*dda28197Spatrick if (reg_info->kinds[RegisterKind::eRegisterKindDWARF] != 2803*dda28197Spatrick LLDB_INVALID_REGNUM) 2804*dda28197Spatrick response.Printf("dwarf_regnum=\"%" PRIu32 "\" ", 2805*dda28197Spatrick reg_info->kinds[RegisterKind::eRegisterKindDWARF]); 2806*dda28197Spatrick 2807*dda28197Spatrick llvm::StringRef kind_generic = GetKindGenericOrEmpty(*reg_info); 2808*dda28197Spatrick if (!kind_generic.empty()) 2809*dda28197Spatrick response << "generic=\"" << kind_generic << "\" "; 2810*dda28197Spatrick 2811*dda28197Spatrick if (reg_info->value_regs && 2812*dda28197Spatrick reg_info->value_regs[0] != LLDB_INVALID_REGNUM) { 2813*dda28197Spatrick response.PutCString("value_regnums=\""); 2814*dda28197Spatrick CollectRegNums(reg_info->value_regs, response, false); 2815*dda28197Spatrick response.Printf("\" "); 2816*dda28197Spatrick } 2817*dda28197Spatrick 2818*dda28197Spatrick if (reg_info->invalidate_regs && reg_info->invalidate_regs[0]) { 2819*dda28197Spatrick response.PutCString("invalidate_regnums=\""); 2820*dda28197Spatrick CollectRegNums(reg_info->invalidate_regs, response, false); 2821*dda28197Spatrick response.Printf("\" "); 2822*dda28197Spatrick } 2823*dda28197Spatrick 2824*dda28197Spatrick if (reg_info->dynamic_size_dwarf_expr_bytes) { 2825*dda28197Spatrick const size_t dwarf_opcode_len = reg_info->dynamic_size_dwarf_len; 2826*dda28197Spatrick response.PutCString("dynamic_size_dwarf_expr_bytes=\""); 2827*dda28197Spatrick for (uint32_t i = 0; i < dwarf_opcode_len; ++i) 2828*dda28197Spatrick response.PutHex8(reg_info->dynamic_size_dwarf_expr_bytes[i]); 2829*dda28197Spatrick response.Printf("\" "); 2830*dda28197Spatrick } 2831*dda28197Spatrick 2832*dda28197Spatrick response.Printf("/>"); 2833*dda28197Spatrick } 2834*dda28197Spatrick 2835*dda28197Spatrick response.Printf("</feature>"); 2836*dda28197Spatrick response.Printf("</target>"); 2837*dda28197Spatrick return MemoryBuffer::getMemBufferCopy(response.GetString(), "target.xml"); 2838*dda28197Spatrick } 2839*dda28197Spatrick 2840*dda28197Spatrick llvm::Expected<std::unique_ptr<llvm::MemoryBuffer>> 2841061da546Spatrick GDBRemoteCommunicationServerLLGS::ReadXferObject(llvm::StringRef object, 2842061da546Spatrick llvm::StringRef annex) { 2843061da546Spatrick // Make sure we have a valid process. 2844061da546Spatrick if (!m_debugged_process_up || 2845061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 2846061da546Spatrick return llvm::createStringError(llvm::inconvertibleErrorCode(), 2847061da546Spatrick "No process available"); 2848061da546Spatrick } 2849061da546Spatrick 2850*dda28197Spatrick if (object == "auxv") { 2851061da546Spatrick // Grab the auxv data. 2852061da546Spatrick auto buffer_or_error = m_debugged_process_up->GetAuxvData(); 2853061da546Spatrick if (!buffer_or_error) 2854061da546Spatrick return llvm::errorCodeToError(buffer_or_error.getError()); 2855061da546Spatrick return std::move(*buffer_or_error); 2856061da546Spatrick } 2857061da546Spatrick 2858061da546Spatrick if (object == "libraries-svr4") { 2859061da546Spatrick auto library_list = m_debugged_process_up->GetLoadedSVR4Libraries(); 2860061da546Spatrick if (!library_list) 2861061da546Spatrick return library_list.takeError(); 2862061da546Spatrick 2863061da546Spatrick StreamString response; 2864061da546Spatrick response.Printf("<library-list-svr4 version=\"1.0\">"); 2865061da546Spatrick for (auto const &library : *library_list) { 2866061da546Spatrick response.Printf("<library name=\"%s\" ", 2867061da546Spatrick XMLEncodeAttributeValue(library.name.c_str()).c_str()); 2868061da546Spatrick response.Printf("lm=\"0x%" PRIx64 "\" ", library.link_map); 2869061da546Spatrick response.Printf("l_addr=\"0x%" PRIx64 "\" ", library.base_addr); 2870061da546Spatrick response.Printf("l_ld=\"0x%" PRIx64 "\" />", library.ld_addr); 2871061da546Spatrick } 2872061da546Spatrick response.Printf("</library-list-svr4>"); 2873061da546Spatrick return MemoryBuffer::getMemBufferCopy(response.GetString(), __FUNCTION__); 2874061da546Spatrick } 2875061da546Spatrick 2876*dda28197Spatrick if (object == "features" && annex == "target.xml") 2877*dda28197Spatrick return BuildTargetXml(); 2878*dda28197Spatrick 2879061da546Spatrick return llvm::make_error<PacketUnimplementedError>( 2880061da546Spatrick "Xfer object not supported"); 2881061da546Spatrick } 2882061da546Spatrick 2883061da546Spatrick GDBRemoteCommunication::PacketResult 2884061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qXfer( 2885061da546Spatrick StringExtractorGDBRemote &packet) { 2886061da546Spatrick SmallVector<StringRef, 5> fields; 2887061da546Spatrick // The packet format is "qXfer:<object>:<action>:<annex>:offset,length" 2888061da546Spatrick StringRef(packet.GetStringRef()).split(fields, ':', 4); 2889061da546Spatrick if (fields.size() != 5) 2890061da546Spatrick return SendIllFormedResponse(packet, "malformed qXfer packet"); 2891061da546Spatrick StringRef &xfer_object = fields[1]; 2892061da546Spatrick StringRef &xfer_action = fields[2]; 2893061da546Spatrick StringRef &xfer_annex = fields[3]; 2894061da546Spatrick StringExtractor offset_data(fields[4]); 2895061da546Spatrick if (xfer_action != "read") 2896061da546Spatrick return SendUnimplementedResponse("qXfer action not supported"); 2897061da546Spatrick // Parse offset. 2898061da546Spatrick const uint64_t xfer_offset = 2899061da546Spatrick offset_data.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max()); 2900061da546Spatrick if (xfer_offset == std::numeric_limits<uint64_t>::max()) 2901061da546Spatrick return SendIllFormedResponse(packet, "qXfer packet missing offset"); 2902061da546Spatrick // Parse out comma. 2903061da546Spatrick if (offset_data.GetChar() != ',') 2904061da546Spatrick return SendIllFormedResponse(packet, 2905061da546Spatrick "qXfer packet missing comma after offset"); 2906061da546Spatrick // Parse out the length. 2907061da546Spatrick const uint64_t xfer_length = 2908061da546Spatrick offset_data.GetHexMaxU64(false, std::numeric_limits<uint64_t>::max()); 2909061da546Spatrick if (xfer_length == std::numeric_limits<uint64_t>::max()) 2910061da546Spatrick return SendIllFormedResponse(packet, "qXfer packet missing length"); 2911061da546Spatrick 2912061da546Spatrick // Get a previously constructed buffer if it exists or create it now. 2913061da546Spatrick std::string buffer_key = (xfer_object + xfer_action + xfer_annex).str(); 2914061da546Spatrick auto buffer_it = m_xfer_buffer_map.find(buffer_key); 2915061da546Spatrick if (buffer_it == m_xfer_buffer_map.end()) { 2916061da546Spatrick auto buffer_up = ReadXferObject(xfer_object, xfer_annex); 2917061da546Spatrick if (!buffer_up) 2918061da546Spatrick return SendErrorResponse(buffer_up.takeError()); 2919061da546Spatrick buffer_it = m_xfer_buffer_map 2920061da546Spatrick .insert(std::make_pair(buffer_key, std::move(*buffer_up))) 2921061da546Spatrick .first; 2922061da546Spatrick } 2923061da546Spatrick 2924061da546Spatrick // Send back the response 2925061da546Spatrick StreamGDBRemote response; 2926061da546Spatrick bool done_with_buffer = false; 2927061da546Spatrick llvm::StringRef buffer = buffer_it->second->getBuffer(); 2928061da546Spatrick if (xfer_offset >= buffer.size()) { 2929061da546Spatrick // We have nothing left to send. Mark the buffer as complete. 2930061da546Spatrick response.PutChar('l'); 2931061da546Spatrick done_with_buffer = true; 2932061da546Spatrick } else { 2933061da546Spatrick // Figure out how many bytes are available starting at the given offset. 2934061da546Spatrick buffer = buffer.drop_front(xfer_offset); 2935061da546Spatrick // Mark the response type according to whether we're reading the remainder 2936061da546Spatrick // of the data. 2937061da546Spatrick if (xfer_length >= buffer.size()) { 2938061da546Spatrick // There will be nothing left to read after this 2939061da546Spatrick response.PutChar('l'); 2940061da546Spatrick done_with_buffer = true; 2941061da546Spatrick } else { 2942061da546Spatrick // There will still be bytes to read after this request. 2943061da546Spatrick response.PutChar('m'); 2944061da546Spatrick buffer = buffer.take_front(xfer_length); 2945061da546Spatrick } 2946061da546Spatrick // Now write the data in encoded binary form. 2947061da546Spatrick response.PutEscapedBytes(buffer.data(), buffer.size()); 2948061da546Spatrick } 2949061da546Spatrick 2950061da546Spatrick if (done_with_buffer) 2951061da546Spatrick m_xfer_buffer_map.erase(buffer_it); 2952061da546Spatrick 2953061da546Spatrick return SendPacketNoLock(response.GetString()); 2954061da546Spatrick } 2955061da546Spatrick 2956061da546Spatrick GDBRemoteCommunication::PacketResult 2957061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState( 2958061da546Spatrick StringExtractorGDBRemote &packet) { 2959061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2960061da546Spatrick 2961061da546Spatrick // Move past packet name. 2962061da546Spatrick packet.SetFilePos(strlen("QSaveRegisterState")); 2963061da546Spatrick 2964061da546Spatrick // Get the thread to use. 2965061da546Spatrick NativeThreadProtocol *thread = GetThreadFromSuffix(packet); 2966061da546Spatrick if (!thread) { 2967061da546Spatrick if (m_thread_suffix_supported) 2968061da546Spatrick return SendIllFormedResponse( 2969061da546Spatrick packet, "No thread specified in QSaveRegisterState packet"); 2970061da546Spatrick else 2971061da546Spatrick return SendIllFormedResponse(packet, 2972061da546Spatrick "No thread was is set with the Hg packet"); 2973061da546Spatrick } 2974061da546Spatrick 2975061da546Spatrick // Grab the register context for the thread. 2976061da546Spatrick NativeRegisterContext& reg_context = thread->GetRegisterContext(); 2977061da546Spatrick 2978061da546Spatrick // Save registers to a buffer. 2979061da546Spatrick DataBufferSP register_data_sp; 2980061da546Spatrick Status error = reg_context.ReadAllRegisterValues(register_data_sp); 2981061da546Spatrick if (error.Fail()) { 2982061da546Spatrick LLDB_LOG(log, "pid {0} failed to save all register values: {1}", 2983061da546Spatrick m_debugged_process_up->GetID(), error); 2984061da546Spatrick return SendErrorResponse(0x75); 2985061da546Spatrick } 2986061da546Spatrick 2987061da546Spatrick // Allocate a new save id. 2988061da546Spatrick const uint32_t save_id = GetNextSavedRegistersID(); 2989061da546Spatrick assert((m_saved_registers_map.find(save_id) == m_saved_registers_map.end()) && 2990061da546Spatrick "GetNextRegisterSaveID() returned an existing register save id"); 2991061da546Spatrick 2992061da546Spatrick // Save the register data buffer under the save id. 2993061da546Spatrick { 2994061da546Spatrick std::lock_guard<std::mutex> guard(m_saved_registers_mutex); 2995061da546Spatrick m_saved_registers_map[save_id] = register_data_sp; 2996061da546Spatrick } 2997061da546Spatrick 2998061da546Spatrick // Write the response. 2999061da546Spatrick StreamGDBRemote response; 3000061da546Spatrick response.Printf("%" PRIu32, save_id); 3001061da546Spatrick return SendPacketNoLock(response.GetString()); 3002061da546Spatrick } 3003061da546Spatrick 3004061da546Spatrick GDBRemoteCommunication::PacketResult 3005061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState( 3006061da546Spatrick StringExtractorGDBRemote &packet) { 3007061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 3008061da546Spatrick 3009061da546Spatrick // Parse out save id. 3010061da546Spatrick packet.SetFilePos(strlen("QRestoreRegisterState:")); 3011061da546Spatrick if (packet.GetBytesLeft() < 1) 3012061da546Spatrick return SendIllFormedResponse( 3013061da546Spatrick packet, "QRestoreRegisterState packet missing register save id"); 3014061da546Spatrick 3015061da546Spatrick const uint32_t save_id = packet.GetU32(0); 3016061da546Spatrick if (save_id == 0) { 3017061da546Spatrick LLDB_LOG(log, "QRestoreRegisterState packet has malformed save id, " 3018061da546Spatrick "expecting decimal uint32_t"); 3019061da546Spatrick return SendErrorResponse(0x76); 3020061da546Spatrick } 3021061da546Spatrick 3022061da546Spatrick // Get the thread to use. 3023061da546Spatrick NativeThreadProtocol *thread = GetThreadFromSuffix(packet); 3024061da546Spatrick if (!thread) { 3025061da546Spatrick if (m_thread_suffix_supported) 3026061da546Spatrick return SendIllFormedResponse( 3027061da546Spatrick packet, "No thread specified in QRestoreRegisterState packet"); 3028061da546Spatrick else 3029061da546Spatrick return SendIllFormedResponse(packet, 3030061da546Spatrick "No thread was is set with the Hg packet"); 3031061da546Spatrick } 3032061da546Spatrick 3033061da546Spatrick // Grab the register context for the thread. 3034061da546Spatrick NativeRegisterContext ®_context = thread->GetRegisterContext(); 3035061da546Spatrick 3036061da546Spatrick // Retrieve register state buffer, then remove from the list. 3037061da546Spatrick DataBufferSP register_data_sp; 3038061da546Spatrick { 3039061da546Spatrick std::lock_guard<std::mutex> guard(m_saved_registers_mutex); 3040061da546Spatrick 3041061da546Spatrick // Find the register set buffer for the given save id. 3042061da546Spatrick auto it = m_saved_registers_map.find(save_id); 3043061da546Spatrick if (it == m_saved_registers_map.end()) { 3044061da546Spatrick LLDB_LOG(log, 3045061da546Spatrick "pid {0} does not have a register set save buffer for id {1}", 3046061da546Spatrick m_debugged_process_up->GetID(), save_id); 3047061da546Spatrick return SendErrorResponse(0x77); 3048061da546Spatrick } 3049061da546Spatrick register_data_sp = it->second; 3050061da546Spatrick 3051061da546Spatrick // Remove it from the map. 3052061da546Spatrick m_saved_registers_map.erase(it); 3053061da546Spatrick } 3054061da546Spatrick 3055061da546Spatrick Status error = reg_context.WriteAllRegisterValues(register_data_sp); 3056061da546Spatrick if (error.Fail()) { 3057061da546Spatrick LLDB_LOG(log, "pid {0} failed to restore all register values: {1}", 3058061da546Spatrick m_debugged_process_up->GetID(), error); 3059061da546Spatrick return SendErrorResponse(0x77); 3060061da546Spatrick } 3061061da546Spatrick 3062061da546Spatrick return SendOKResponse(); 3063061da546Spatrick } 3064061da546Spatrick 3065061da546Spatrick GDBRemoteCommunication::PacketResult 3066061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_vAttach( 3067061da546Spatrick StringExtractorGDBRemote &packet) { 3068061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 3069061da546Spatrick 3070061da546Spatrick // Consume the ';' after vAttach. 3071061da546Spatrick packet.SetFilePos(strlen("vAttach")); 3072061da546Spatrick if (!packet.GetBytesLeft() || packet.GetChar() != ';') 3073061da546Spatrick return SendIllFormedResponse(packet, "vAttach missing expected ';'"); 3074061da546Spatrick 3075061da546Spatrick // Grab the PID to which we will attach (assume hex encoding). 3076061da546Spatrick lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16); 3077061da546Spatrick if (pid == LLDB_INVALID_PROCESS_ID) 3078061da546Spatrick return SendIllFormedResponse(packet, 3079061da546Spatrick "vAttach failed to parse the process id"); 3080061da546Spatrick 3081061da546Spatrick // Attempt to attach. 3082061da546Spatrick LLDB_LOGF(log, 3083061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s attempting to attach to " 3084061da546Spatrick "pid %" PRIu64, 3085061da546Spatrick __FUNCTION__, pid); 3086061da546Spatrick 3087061da546Spatrick Status error = AttachToProcess(pid); 3088061da546Spatrick 3089061da546Spatrick if (error.Fail()) { 3090061da546Spatrick LLDB_LOGF(log, 3091061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed to attach to " 3092061da546Spatrick "pid %" PRIu64 ": %s\n", 3093061da546Spatrick __FUNCTION__, pid, error.AsCString()); 3094061da546Spatrick return SendErrorResponse(error); 3095061da546Spatrick } 3096061da546Spatrick 3097061da546Spatrick // Notify we attached by sending a stop packet. 3098061da546Spatrick return SendStopReasonForState(m_debugged_process_up->GetState()); 3099061da546Spatrick } 3100061da546Spatrick 3101061da546Spatrick GDBRemoteCommunication::PacketResult 3102061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_D(StringExtractorGDBRemote &packet) { 3103061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 3104061da546Spatrick 3105061da546Spatrick StopSTDIOForwarding(); 3106061da546Spatrick 3107061da546Spatrick // Fail if we don't have a current process. 3108061da546Spatrick if (!m_debugged_process_up || 3109061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) { 3110061da546Spatrick LLDB_LOGF( 3111061da546Spatrick log, 3112061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, no process available", 3113061da546Spatrick __FUNCTION__); 3114061da546Spatrick return SendErrorResponse(0x15); 3115061da546Spatrick } 3116061da546Spatrick 3117061da546Spatrick lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 3118061da546Spatrick 3119061da546Spatrick // Consume the ';' after D. 3120061da546Spatrick packet.SetFilePos(1); 3121061da546Spatrick if (packet.GetBytesLeft()) { 3122061da546Spatrick if (packet.GetChar() != ';') 3123061da546Spatrick return SendIllFormedResponse(packet, "D missing expected ';'"); 3124061da546Spatrick 3125061da546Spatrick // Grab the PID from which we will detach (assume hex encoding). 3126061da546Spatrick pid = packet.GetU32(LLDB_INVALID_PROCESS_ID, 16); 3127061da546Spatrick if (pid == LLDB_INVALID_PROCESS_ID) 3128061da546Spatrick return SendIllFormedResponse(packet, "D failed to parse the process id"); 3129061da546Spatrick } 3130061da546Spatrick 3131061da546Spatrick if (pid != LLDB_INVALID_PROCESS_ID && m_debugged_process_up->GetID() != pid) { 3132061da546Spatrick return SendIllFormedResponse(packet, "Invalid pid"); 3133061da546Spatrick } 3134061da546Spatrick 3135061da546Spatrick const Status error = m_debugged_process_up->Detach(); 3136061da546Spatrick if (error.Fail()) { 3137061da546Spatrick LLDB_LOGF(log, 3138061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed to detach from " 3139061da546Spatrick "pid %" PRIu64 ": %s\n", 3140061da546Spatrick __FUNCTION__, m_debugged_process_up->GetID(), error.AsCString()); 3141061da546Spatrick return SendErrorResponse(0x01); 3142061da546Spatrick } 3143061da546Spatrick 3144061da546Spatrick return SendOKResponse(); 3145061da546Spatrick } 3146061da546Spatrick 3147061da546Spatrick GDBRemoteCommunication::PacketResult 3148061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo( 3149061da546Spatrick StringExtractorGDBRemote &packet) { 3150061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 3151061da546Spatrick 3152061da546Spatrick packet.SetFilePos(strlen("qThreadStopInfo")); 3153061da546Spatrick const lldb::tid_t tid = packet.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID); 3154061da546Spatrick if (tid == LLDB_INVALID_THREAD_ID) { 3155061da546Spatrick LLDB_LOGF(log, 3156061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s failed, could not " 3157061da546Spatrick "parse thread id from request \"%s\"", 3158061da546Spatrick __FUNCTION__, packet.GetStringRef().data()); 3159061da546Spatrick return SendErrorResponse(0x15); 3160061da546Spatrick } 3161061da546Spatrick return SendStopReplyPacketForThread(tid); 3162061da546Spatrick } 3163061da546Spatrick 3164061da546Spatrick GDBRemoteCommunication::PacketResult 3165061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo( 3166061da546Spatrick StringExtractorGDBRemote &) { 3167061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 3168061da546Spatrick 3169061da546Spatrick // Ensure we have a debugged process. 3170061da546Spatrick if (!m_debugged_process_up || 3171061da546Spatrick (m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID)) 3172061da546Spatrick return SendErrorResponse(50); 3173061da546Spatrick LLDB_LOG(log, "preparing packet for pid {0}", m_debugged_process_up->GetID()); 3174061da546Spatrick 3175061da546Spatrick StreamString response; 3176061da546Spatrick const bool threads_with_valid_stop_info_only = false; 3177061da546Spatrick llvm::Expected<json::Value> threads_info = GetJSONThreadsInfo( 3178061da546Spatrick *m_debugged_process_up, threads_with_valid_stop_info_only); 3179061da546Spatrick if (!threads_info) { 3180061da546Spatrick LLDB_LOG_ERROR(log, threads_info.takeError(), 3181061da546Spatrick "failed to prepare a packet for pid {1}: {0}", 3182061da546Spatrick m_debugged_process_up->GetID()); 3183061da546Spatrick return SendErrorResponse(52); 3184061da546Spatrick } 3185061da546Spatrick 3186061da546Spatrick response.AsRawOstream() << *threads_info; 3187061da546Spatrick StreamGDBRemote escaped_response; 3188061da546Spatrick escaped_response.PutEscapedBytes(response.GetData(), response.GetSize()); 3189061da546Spatrick return SendPacketNoLock(escaped_response.GetString()); 3190061da546Spatrick } 3191061da546Spatrick 3192061da546Spatrick GDBRemoteCommunication::PacketResult 3193061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo( 3194061da546Spatrick StringExtractorGDBRemote &packet) { 3195061da546Spatrick // Fail if we don't have a current process. 3196061da546Spatrick if (!m_debugged_process_up || 3197061da546Spatrick m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID) 3198061da546Spatrick return SendErrorResponse(68); 3199061da546Spatrick 3200061da546Spatrick packet.SetFilePos(strlen("qWatchpointSupportInfo")); 3201061da546Spatrick if (packet.GetBytesLeft() == 0) 3202061da546Spatrick return SendOKResponse(); 3203061da546Spatrick if (packet.GetChar() != ':') 3204061da546Spatrick return SendErrorResponse(67); 3205061da546Spatrick 3206061da546Spatrick auto hw_debug_cap = m_debugged_process_up->GetHardwareDebugSupportInfo(); 3207061da546Spatrick 3208061da546Spatrick StreamGDBRemote response; 3209061da546Spatrick if (hw_debug_cap == llvm::None) 3210061da546Spatrick response.Printf("num:0;"); 3211061da546Spatrick else 3212061da546Spatrick response.Printf("num:%d;", hw_debug_cap->second); 3213061da546Spatrick 3214061da546Spatrick return SendPacketNoLock(response.GetString()); 3215061da546Spatrick } 3216061da546Spatrick 3217061da546Spatrick GDBRemoteCommunication::PacketResult 3218061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress( 3219061da546Spatrick StringExtractorGDBRemote &packet) { 3220061da546Spatrick // Fail if we don't have a current process. 3221061da546Spatrick if (!m_debugged_process_up || 3222061da546Spatrick m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID) 3223061da546Spatrick return SendErrorResponse(67); 3224061da546Spatrick 3225061da546Spatrick packet.SetFilePos(strlen("qFileLoadAddress:")); 3226061da546Spatrick if (packet.GetBytesLeft() == 0) 3227061da546Spatrick return SendErrorResponse(68); 3228061da546Spatrick 3229061da546Spatrick std::string file_name; 3230061da546Spatrick packet.GetHexByteString(file_name); 3231061da546Spatrick 3232061da546Spatrick lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS; 3233061da546Spatrick Status error = 3234061da546Spatrick m_debugged_process_up->GetFileLoadAddress(file_name, file_load_address); 3235061da546Spatrick if (error.Fail()) 3236061da546Spatrick return SendErrorResponse(69); 3237061da546Spatrick 3238061da546Spatrick if (file_load_address == LLDB_INVALID_ADDRESS) 3239061da546Spatrick return SendErrorResponse(1); // File not loaded 3240061da546Spatrick 3241061da546Spatrick StreamGDBRemote response; 3242061da546Spatrick response.PutHex64(file_load_address); 3243061da546Spatrick return SendPacketNoLock(response.GetString()); 3244061da546Spatrick } 3245061da546Spatrick 3246061da546Spatrick GDBRemoteCommunication::PacketResult 3247061da546Spatrick GDBRemoteCommunicationServerLLGS::Handle_QPassSignals( 3248061da546Spatrick StringExtractorGDBRemote &packet) { 3249061da546Spatrick std::vector<int> signals; 3250061da546Spatrick packet.SetFilePos(strlen("QPassSignals:")); 3251061da546Spatrick 3252061da546Spatrick // Read sequence of hex signal numbers divided by a semicolon and optionally 3253061da546Spatrick // spaces. 3254061da546Spatrick while (packet.GetBytesLeft() > 0) { 3255061da546Spatrick int signal = packet.GetS32(-1, 16); 3256061da546Spatrick if (signal < 0) 3257061da546Spatrick return SendIllFormedResponse(packet, "Failed to parse signal number."); 3258061da546Spatrick signals.push_back(signal); 3259061da546Spatrick 3260061da546Spatrick packet.SkipSpaces(); 3261061da546Spatrick char separator = packet.GetChar(); 3262061da546Spatrick if (separator == '\0') 3263061da546Spatrick break; // End of string 3264061da546Spatrick if (separator != ';') 3265061da546Spatrick return SendIllFormedResponse(packet, "Invalid separator," 3266061da546Spatrick " expected semicolon."); 3267061da546Spatrick } 3268061da546Spatrick 3269061da546Spatrick // Fail if we don't have a current process. 3270061da546Spatrick if (!m_debugged_process_up) 3271061da546Spatrick return SendErrorResponse(68); 3272061da546Spatrick 3273061da546Spatrick Status error = m_debugged_process_up->IgnoreSignals(signals); 3274061da546Spatrick if (error.Fail()) 3275061da546Spatrick return SendErrorResponse(69); 3276061da546Spatrick 3277061da546Spatrick return SendOKResponse(); 3278061da546Spatrick } 3279061da546Spatrick 3280061da546Spatrick void GDBRemoteCommunicationServerLLGS::MaybeCloseInferiorTerminalConnection() { 3281061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 3282061da546Spatrick 3283061da546Spatrick // Tell the stdio connection to shut down. 3284061da546Spatrick if (m_stdio_communication.IsConnected()) { 3285061da546Spatrick auto connection = m_stdio_communication.GetConnection(); 3286061da546Spatrick if (connection) { 3287061da546Spatrick Status error; 3288061da546Spatrick connection->Disconnect(&error); 3289061da546Spatrick 3290061da546Spatrick if (error.Success()) { 3291061da546Spatrick LLDB_LOGF(log, 3292061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s disconnect process " 3293061da546Spatrick "terminal stdio - SUCCESS", 3294061da546Spatrick __FUNCTION__); 3295061da546Spatrick } else { 3296061da546Spatrick LLDB_LOGF(log, 3297061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s disconnect process " 3298061da546Spatrick "terminal stdio - FAIL: %s", 3299061da546Spatrick __FUNCTION__, error.AsCString()); 3300061da546Spatrick } 3301061da546Spatrick } 3302061da546Spatrick } 3303061da546Spatrick } 3304061da546Spatrick 3305061da546Spatrick NativeThreadProtocol *GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix( 3306061da546Spatrick StringExtractorGDBRemote &packet) { 3307061da546Spatrick // We have no thread if we don't have a process. 3308061da546Spatrick if (!m_debugged_process_up || 3309061da546Spatrick m_debugged_process_up->GetID() == LLDB_INVALID_PROCESS_ID) 3310061da546Spatrick return nullptr; 3311061da546Spatrick 3312061da546Spatrick // If the client hasn't asked for thread suffix support, there will not be a 3313061da546Spatrick // thread suffix. Use the current thread in that case. 3314061da546Spatrick if (!m_thread_suffix_supported) { 3315061da546Spatrick const lldb::tid_t current_tid = GetCurrentThreadID(); 3316061da546Spatrick if (current_tid == LLDB_INVALID_THREAD_ID) 3317061da546Spatrick return nullptr; 3318061da546Spatrick else if (current_tid == 0) { 3319061da546Spatrick // Pick a thread. 3320061da546Spatrick return m_debugged_process_up->GetThreadAtIndex(0); 3321061da546Spatrick } else 3322061da546Spatrick return m_debugged_process_up->GetThreadByID(current_tid); 3323061da546Spatrick } 3324061da546Spatrick 3325061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 3326061da546Spatrick 3327061da546Spatrick // Parse out the ';'. 3328061da546Spatrick if (packet.GetBytesLeft() < 1 || packet.GetChar() != ';') { 3329061da546Spatrick LLDB_LOGF(log, 3330061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s gdb-remote parse " 3331061da546Spatrick "error: expected ';' prior to start of thread suffix: packet " 3332061da546Spatrick "contents = '%s'", 3333061da546Spatrick __FUNCTION__, packet.GetStringRef().data()); 3334061da546Spatrick return nullptr; 3335061da546Spatrick } 3336061da546Spatrick 3337061da546Spatrick if (!packet.GetBytesLeft()) 3338061da546Spatrick return nullptr; 3339061da546Spatrick 3340061da546Spatrick // Parse out thread: portion. 3341061da546Spatrick if (strncmp(packet.Peek(), "thread:", strlen("thread:")) != 0) { 3342061da546Spatrick LLDB_LOGF(log, 3343061da546Spatrick "GDBRemoteCommunicationServerLLGS::%s gdb-remote parse " 3344061da546Spatrick "error: expected 'thread:' but not found, packet contents = " 3345061da546Spatrick "'%s'", 3346061da546Spatrick __FUNCTION__, packet.GetStringRef().data()); 3347061da546Spatrick return nullptr; 3348061da546Spatrick } 3349061da546Spatrick packet.SetFilePos(packet.GetFilePos() + strlen("thread:")); 3350061da546Spatrick const lldb::tid_t tid = packet.GetHexMaxU64(false, 0); 3351061da546Spatrick if (tid != 0) 3352061da546Spatrick return m_debugged_process_up->GetThreadByID(tid); 3353061da546Spatrick 3354061da546Spatrick return nullptr; 3355061da546Spatrick } 3356061da546Spatrick 3357061da546Spatrick lldb::tid_t GDBRemoteCommunicationServerLLGS::GetCurrentThreadID() const { 3358061da546Spatrick if (m_current_tid == 0 || m_current_tid == LLDB_INVALID_THREAD_ID) { 3359061da546Spatrick // Use whatever the debug process says is the current thread id since the 3360061da546Spatrick // protocol either didn't specify or specified we want any/all threads 3361061da546Spatrick // marked as the current thread. 3362061da546Spatrick if (!m_debugged_process_up) 3363061da546Spatrick return LLDB_INVALID_THREAD_ID; 3364061da546Spatrick return m_debugged_process_up->GetCurrentThreadID(); 3365061da546Spatrick } 3366061da546Spatrick // Use the specific current thread id set by the gdb remote protocol. 3367061da546Spatrick return m_current_tid; 3368061da546Spatrick } 3369061da546Spatrick 3370061da546Spatrick uint32_t GDBRemoteCommunicationServerLLGS::GetNextSavedRegistersID() { 3371061da546Spatrick std::lock_guard<std::mutex> guard(m_saved_registers_mutex); 3372061da546Spatrick return m_next_saved_registers_id++; 3373061da546Spatrick } 3374061da546Spatrick 3375061da546Spatrick void GDBRemoteCommunicationServerLLGS::ClearProcessSpecificData() { 3376061da546Spatrick Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 3377061da546Spatrick 3378061da546Spatrick LLDB_LOG(log, "clearing {0} xfer buffers", m_xfer_buffer_map.size()); 3379061da546Spatrick m_xfer_buffer_map.clear(); 3380061da546Spatrick } 3381061da546Spatrick 3382061da546Spatrick FileSpec 3383061da546Spatrick GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string &module_path, 3384061da546Spatrick const ArchSpec &arch) { 3385061da546Spatrick if (m_debugged_process_up) { 3386061da546Spatrick FileSpec file_spec; 3387061da546Spatrick if (m_debugged_process_up 3388061da546Spatrick ->GetLoadedModuleFileSpec(module_path.c_str(), file_spec) 3389061da546Spatrick .Success()) { 3390061da546Spatrick if (FileSystem::Instance().Exists(file_spec)) 3391061da546Spatrick return file_spec; 3392061da546Spatrick } 3393061da546Spatrick } 3394061da546Spatrick 3395061da546Spatrick return GDBRemoteCommunicationServerCommon::FindModuleFile(module_path, arch); 3396061da546Spatrick } 3397061da546Spatrick 3398061da546Spatrick std::string GDBRemoteCommunicationServerLLGS::XMLEncodeAttributeValue( 3399061da546Spatrick llvm::StringRef value) { 3400061da546Spatrick std::string result; 3401061da546Spatrick for (const char &c : value) { 3402061da546Spatrick switch (c) { 3403061da546Spatrick case '\'': 3404061da546Spatrick result += "'"; 3405061da546Spatrick break; 3406061da546Spatrick case '"': 3407061da546Spatrick result += """; 3408061da546Spatrick break; 3409061da546Spatrick case '<': 3410061da546Spatrick result += "<"; 3411061da546Spatrick break; 3412061da546Spatrick case '>': 3413061da546Spatrick result += ">"; 3414061da546Spatrick break; 3415061da546Spatrick default: 3416061da546Spatrick result += c; 3417061da546Spatrick break; 3418061da546Spatrick } 3419061da546Spatrick } 3420061da546Spatrick return result; 3421061da546Spatrick } 3422