1 //===-- GDBRemoteCommunicationServerLLGS.cpp --------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include <errno.h> 11 12 #include "lldb/Host/Config.h" 13 14 #include "GDBRemoteCommunicationServerLLGS.h" 15 #include "lldb/Core/StreamGDBRemote.h" 16 17 // C Includes 18 // C++ Includes 19 #include <cstring> 20 #include <chrono> 21 #include <thread> 22 23 // Other libraries and framework includes 24 #include "llvm/ADT/Triple.h" 25 #include "lldb/Interpreter/Args.h" 26 #include "lldb/Core/DataBuffer.h" 27 #include "lldb/Core/Log.h" 28 #include "lldb/Core/RegisterValue.h" 29 #include "lldb/Core/State.h" 30 #include "lldb/Core/StreamString.h" 31 #include "lldb/Host/ConnectionFileDescriptor.h" 32 #include "lldb/Host/Debug.h" 33 #include "lldb/Host/Endian.h" 34 #include "lldb/Host/File.h" 35 #include "lldb/Host/FileSystem.h" 36 #include "lldb/Host/Host.h" 37 #include "lldb/Host/HostInfo.h" 38 #include "lldb/Host/StringConvert.h" 39 #include "lldb/Host/TimeValue.h" 40 #include "lldb/Target/FileAction.h" 41 #include "lldb/Target/MemoryRegionInfo.h" 42 #include "lldb/Target/Platform.h" 43 #include "lldb/Host/common/NativeRegisterContext.h" 44 #include "lldb/Host/common/NativeProcessProtocol.h" 45 #include "lldb/Host/common/NativeThreadProtocol.h" 46 #include "lldb/Utility/JSON.h" 47 48 // Project includes 49 #include "Utility/StringExtractorGDBRemote.h" 50 #include "Utility/UriParser.h" 51 #include "ProcessGDBRemote.h" 52 #include "ProcessGDBRemoteLog.h" 53 54 using namespace lldb; 55 using namespace lldb_private; 56 using namespace lldb_private::process_gdb_remote; 57 using namespace llvm; 58 59 //---------------------------------------------------------------------- 60 // GDBRemote Errors 61 //---------------------------------------------------------------------- 62 63 namespace 64 { 65 enum GDBRemoteServerError 66 { 67 // Set to the first unused error number in literal form below 68 eErrorFirst = 29, 69 eErrorNoProcess = eErrorFirst, 70 eErrorResume, 71 eErrorExitStatus 72 }; 73 } 74 75 //---------------------------------------------------------------------- 76 // GDBRemoteCommunicationServerLLGS constructor 77 //---------------------------------------------------------------------- 78 GDBRemoteCommunicationServerLLGS::GDBRemoteCommunicationServerLLGS( 79 const lldb::PlatformSP& platform_sp, 80 MainLoop &mainloop) : 81 GDBRemoteCommunicationServerCommon ("gdb-remote.server", "gdb-remote.server.rx_packet"), 82 m_platform_sp (platform_sp), 83 m_mainloop (mainloop), 84 m_current_tid (LLDB_INVALID_THREAD_ID), 85 m_continue_tid (LLDB_INVALID_THREAD_ID), 86 m_debugged_process_mutex (Mutex::eMutexTypeRecursive), 87 m_debugged_process_sp (), 88 m_stdio_communication ("process.stdio"), 89 m_inferior_prev_state (StateType::eStateInvalid), 90 m_active_auxv_buffer_sp (), 91 m_saved_registers_mutex (), 92 m_saved_registers_map (), 93 m_next_saved_registers_id (1), 94 m_handshake_completed (false) 95 { 96 assert(platform_sp); 97 RegisterPacketHandlers(); 98 } 99 100 //---------------------------------------------------------------------- 101 // Destructor 102 //---------------------------------------------------------------------- 103 GDBRemoteCommunicationServerLLGS::~GDBRemoteCommunicationServerLLGS() 104 { 105 Mutex::Locker locker (m_debugged_process_mutex); 106 107 if (m_debugged_process_sp) 108 { 109 m_debugged_process_sp->Terminate (); 110 m_debugged_process_sp.reset (); 111 } 112 } 113 114 void 115 GDBRemoteCommunicationServerLLGS::RegisterPacketHandlers() 116 { 117 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_C, 118 &GDBRemoteCommunicationServerLLGS::Handle_C); 119 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_c, 120 &GDBRemoteCommunicationServerLLGS::Handle_c); 121 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_D, 122 &GDBRemoteCommunicationServerLLGS::Handle_D); 123 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_H, 124 &GDBRemoteCommunicationServerLLGS::Handle_H); 125 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_I, 126 &GDBRemoteCommunicationServerLLGS::Handle_I); 127 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_interrupt, 128 &GDBRemoteCommunicationServerLLGS::Handle_interrupt); 129 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_m, 130 &GDBRemoteCommunicationServerLLGS::Handle_m); 131 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_M, 132 &GDBRemoteCommunicationServerLLGS::Handle_M); 133 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_p, 134 &GDBRemoteCommunicationServerLLGS::Handle_p); 135 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_P, 136 &GDBRemoteCommunicationServerLLGS::Handle_P); 137 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qC, 138 &GDBRemoteCommunicationServerLLGS::Handle_qC); 139 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qfThreadInfo, 140 &GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo); 141 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qFileLoadAddress, 142 &GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress); 143 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qGetWorkingDir, 144 &GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir); 145 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfo, 146 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo); 147 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qMemoryRegionInfoSupported, 148 &GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported); 149 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qProcessInfo, 150 &GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo); 151 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qRegisterInfo, 152 &GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo); 153 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QRestoreRegisterState, 154 &GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState); 155 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSaveRegisterState, 156 &GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState); 157 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetDisableASLR, 158 &GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR); 159 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_QSetWorkingDir, 160 &GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir); 161 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qsThreadInfo, 162 &GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo); 163 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qThreadStopInfo, 164 &GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo); 165 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_jThreadsInfo, 166 &GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo); 167 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qWatchpointSupportInfo, 168 &GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo); 169 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_qXfer_auxv_read, 170 &GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read); 171 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_s, 172 &GDBRemoteCommunicationServerLLGS::Handle_s); 173 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_stop_reason, 174 &GDBRemoteCommunicationServerLLGS::Handle_stop_reason); // ? 175 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vAttach, 176 &GDBRemoteCommunicationServerLLGS::Handle_vAttach); 177 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vCont, 178 &GDBRemoteCommunicationServerLLGS::Handle_vCont); 179 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_vCont_actions, 180 &GDBRemoteCommunicationServerLLGS::Handle_vCont_actions); 181 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_Z, 182 &GDBRemoteCommunicationServerLLGS::Handle_Z); 183 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_z, 184 &GDBRemoteCommunicationServerLLGS::Handle_z); 185 186 RegisterPacketHandler(StringExtractorGDBRemote::eServerPacketType_k, 187 [this](StringExtractorGDBRemote packet, 188 Error &error, 189 bool &interrupt, 190 bool &quit) 191 { 192 quit = true; 193 return this->Handle_k (packet); 194 }); 195 } 196 197 Error 198 GDBRemoteCommunicationServerLLGS::SetLaunchArguments (const char *const args[], int argc) 199 { 200 if ((argc < 1) || !args || !args[0] || !args[0][0]) 201 return Error ("%s: no process command line specified to launch", __FUNCTION__); 202 203 m_process_launch_info.SetArguments (const_cast<const char**> (args), true); 204 return Error (); 205 } 206 207 Error 208 GDBRemoteCommunicationServerLLGS::SetLaunchFlags (unsigned int launch_flags) 209 { 210 m_process_launch_info.GetFlags ().Set (launch_flags); 211 return Error (); 212 } 213 214 Error 215 GDBRemoteCommunicationServerLLGS::LaunchProcess () 216 { 217 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 218 219 if (!m_process_launch_info.GetArguments ().GetArgumentCount ()) 220 return Error ("%s: no process command line specified to launch", __FUNCTION__); 221 222 Error error; 223 { 224 Mutex::Locker locker (m_debugged_process_mutex); 225 assert (!m_debugged_process_sp && "lldb-gdbserver creating debugged process but one already exists"); 226 error = NativeProcessProtocol::Launch( 227 m_process_launch_info, 228 *this, 229 m_debugged_process_sp); 230 } 231 232 if (!error.Success ()) 233 { 234 fprintf (stderr, "%s: failed to launch executable %s", __FUNCTION__, m_process_launch_info.GetArguments ().GetArgumentAtIndex (0)); 235 return error; 236 } 237 238 // Handle mirroring of inferior stdout/stderr over the gdb-remote protocol 239 // as needed. 240 // llgs local-process debugging may specify PTY paths, which will make these 241 // file actions non-null 242 // process launch -i/e/o will also make these file actions non-null 243 // nullptr means that the traffic is expected to flow over gdb-remote protocol 244 if ( 245 m_process_launch_info.GetFileActionForFD(STDIN_FILENO) == nullptr || 246 m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr || 247 m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr 248 ) 249 { 250 // nullptr means it's not redirected to file or pty (in case of LLGS local) 251 // at least one of stdio will be transferred pty<->gdb-remote 252 // we need to give the pty master handle to this object to read and/or write 253 if (log) 254 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " setting up stdout/stderr redirection via $O gdb-remote commands", __FUNCTION__, m_debugged_process_sp->GetID ()); 255 256 // Setup stdout/stderr mapping from inferior to $O 257 auto terminal_fd = m_debugged_process_sp->GetTerminalFileDescriptor (); 258 if (terminal_fd >= 0) 259 { 260 if (log) 261 log->Printf ("ProcessGDBRemoteCommunicationServerLLGS::%s setting inferior STDIO fd to %d", __FUNCTION__, terminal_fd); 262 error = SetSTDIOFileDescriptor (terminal_fd); 263 if (error.Fail ()) 264 return error; 265 } 266 else 267 { 268 if (log) 269 log->Printf ("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring inferior STDIO since terminal fd reported as %d", __FUNCTION__, terminal_fd); 270 } 271 } 272 else 273 { 274 if (log) 275 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " skipping stdout/stderr redirection via $O: inferior will communicate over client-provided file descriptors", __FUNCTION__, m_debugged_process_sp->GetID ()); 276 } 277 278 printf ("Launched '%s' as process %" PRIu64 "...\n", m_process_launch_info.GetArguments ().GetArgumentAtIndex (0), m_process_launch_info.GetProcessID ()); 279 280 // Add to list of spawned processes. 281 lldb::pid_t pid; 282 if ((pid = m_process_launch_info.GetProcessID ()) != LLDB_INVALID_PROCESS_ID) 283 { 284 // add to spawned pids 285 Mutex::Locker locker (m_spawned_pids_mutex); 286 // On an lldb-gdbserver, we would expect there to be only one. 287 assert (m_spawned_pids.empty () && "lldb-gdbserver adding tracked process but one already existed"); 288 m_spawned_pids.insert (pid); 289 } 290 291 return error; 292 } 293 294 Error 295 GDBRemoteCommunicationServerLLGS::AttachToProcess (lldb::pid_t pid) 296 { 297 Error error; 298 299 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 300 if (log) 301 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64, __FUNCTION__, pid); 302 303 // Scope for mutex locker. 304 { 305 // Before we try to attach, make sure we aren't already monitoring something else. 306 Mutex::Locker locker (m_spawned_pids_mutex); 307 if (!m_spawned_pids.empty ()) 308 { 309 error.SetErrorStringWithFormat ("cannot attach to a process %" PRIu64 " when another process with pid %" PRIu64 " is being debugged.", pid, *m_spawned_pids.begin()); 310 return error; 311 } 312 313 // Try to attach. 314 error = NativeProcessProtocol::Attach(pid, *this, m_debugged_process_sp); 315 if (!error.Success ()) 316 { 317 fprintf (stderr, "%s: failed to attach to process %" PRIu64 ": %s", __FUNCTION__, pid, error.AsCString ()); 318 return error; 319 } 320 321 // Setup stdout/stderr mapping from inferior. 322 auto terminal_fd = m_debugged_process_sp->GetTerminalFileDescriptor (); 323 if (terminal_fd >= 0) 324 { 325 if (log) 326 log->Printf ("ProcessGDBRemoteCommunicationServerLLGS::%s setting inferior STDIO fd to %d", __FUNCTION__, terminal_fd); 327 error = SetSTDIOFileDescriptor (terminal_fd); 328 if (error.Fail ()) 329 return error; 330 } 331 else 332 { 333 if (log) 334 log->Printf ("ProcessGDBRemoteCommunicationServerLLGS::%s ignoring inferior STDIO since terminal fd reported as %d", __FUNCTION__, terminal_fd); 335 } 336 337 printf ("Attached to process %" PRIu64 "...\n", pid); 338 339 // Add to list of spawned processes. 340 assert (m_spawned_pids.empty () && "lldb-gdbserver adding tracked process but one already existed"); 341 m_spawned_pids.insert (pid); 342 343 return error; 344 } 345 } 346 347 void 348 GDBRemoteCommunicationServerLLGS::InitializeDelegate (NativeProcessProtocol *process) 349 { 350 assert (process && "process cannot be NULL"); 351 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 352 if (log) 353 { 354 log->Printf ("GDBRemoteCommunicationServerLLGS::%s called with NativeProcessProtocol pid %" PRIu64 ", current state: %s", 355 __FUNCTION__, 356 process->GetID (), 357 StateAsCString (process->GetState ())); 358 } 359 } 360 361 GDBRemoteCommunication::PacketResult 362 GDBRemoteCommunicationServerLLGS::SendWResponse (NativeProcessProtocol *process) 363 { 364 assert (process && "process cannot be NULL"); 365 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 366 367 // send W notification 368 ExitType exit_type = ExitType::eExitTypeInvalid; 369 int return_code = 0; 370 std::string exit_description; 371 372 const bool got_exit_info = process->GetExitStatus (&exit_type, &return_code, exit_description); 373 if (!got_exit_info) 374 { 375 if (log) 376 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 ", failed to retrieve process exit status", __FUNCTION__, process->GetID ()); 377 378 StreamGDBRemote response; 379 response.PutChar ('E'); 380 response.PutHex8 (GDBRemoteServerError::eErrorExitStatus); 381 return SendPacketNoLock(response.GetData(), response.GetSize()); 382 } 383 else 384 { 385 if (log) 386 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 ", returning exit type %d, return code %d [%s]", __FUNCTION__, process->GetID (), exit_type, return_code, exit_description.c_str ()); 387 388 StreamGDBRemote response; 389 390 char return_type_code; 391 switch (exit_type) 392 { 393 case ExitType::eExitTypeExit: 394 return_type_code = 'W'; 395 break; 396 case ExitType::eExitTypeSignal: 397 return_type_code = 'X'; 398 break; 399 case ExitType::eExitTypeStop: 400 return_type_code = 'S'; 401 break; 402 case ExitType::eExitTypeInvalid: 403 return_type_code = 'E'; 404 break; 405 } 406 response.PutChar (return_type_code); 407 408 // POSIX exit status limited to unsigned 8 bits. 409 response.PutHex8 (return_code); 410 411 return SendPacketNoLock(response.GetData(), response.GetSize()); 412 } 413 } 414 415 static void 416 AppendHexValue (StreamString &response, const uint8_t* buf, uint32_t buf_size, bool swap) 417 { 418 int64_t i; 419 if (swap) 420 { 421 for (i = buf_size-1; i >= 0; i--) 422 response.PutHex8 (buf[i]); 423 } 424 else 425 { 426 for (i = 0; i < buf_size; i++) 427 response.PutHex8 (buf[i]); 428 } 429 } 430 431 static void 432 WriteRegisterValueInHexFixedWidth (StreamString &response, 433 NativeRegisterContextSP ®_ctx_sp, 434 const RegisterInfo ®_info, 435 const RegisterValue *reg_value_p) 436 { 437 RegisterValue reg_value; 438 if (!reg_value_p) 439 { 440 Error error = reg_ctx_sp->ReadRegister (®_info, reg_value); 441 if (error.Success ()) 442 reg_value_p = ®_value; 443 // else log. 444 } 445 446 if (reg_value_p) 447 { 448 AppendHexValue (response, (const uint8_t*) reg_value_p->GetBytes (), reg_value_p->GetByteSize (), false); 449 } 450 else 451 { 452 // Zero-out any unreadable values. 453 if (reg_info.byte_size > 0) 454 { 455 std::basic_string<uint8_t> zeros(reg_info.byte_size, '\0'); 456 AppendHexValue (response, zeros.data(), zeros.size(), false); 457 } 458 } 459 } 460 461 static JSONObject::SP 462 GetRegistersAsJSON(NativeThreadProtocol &thread) 463 { 464 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_THREAD)); 465 466 NativeRegisterContextSP reg_ctx_sp = thread.GetRegisterContext (); 467 if (! reg_ctx_sp) 468 return nullptr; 469 470 JSONObject::SP register_object_sp = std::make_shared<JSONObject>(); 471 472 #ifdef LLDB_JTHREADSINFO_FULL_REGISTER_SET 473 // Expedite all registers in the first register set (i.e. should be GPRs) that are not contained in other registers. 474 const RegisterSet *reg_set_p = reg_ctx_sp->GetRegisterSet(0); 475 if (! reg_set_p) 476 return nullptr; 477 for (const uint32_t *reg_num_p = reg_set_p->registers; *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) 478 { 479 uint32_t reg_num = *reg_num_p; 480 #else 481 // Expedite only a couple of registers until we figure out why sending registers is 482 // expensive. 483 static const uint32_t k_expedited_registers[] = { 484 LLDB_REGNUM_GENERIC_PC, LLDB_REGNUM_GENERIC_SP, LLDB_REGNUM_GENERIC_FP, LLDB_REGNUM_GENERIC_RA 485 }; 486 for (uint32_t generic_reg: k_expedited_registers) 487 { 488 uint32_t reg_num = reg_ctx_sp->ConvertRegisterKindToRegisterNumber(eRegisterKindGeneric, generic_reg); 489 if (reg_num == LLDB_INVALID_REGNUM) 490 continue; // Target does not support the given register. 491 #endif 492 493 const RegisterInfo *const reg_info_p = reg_ctx_sp->GetRegisterInfoAtIndex(reg_num); 494 if (reg_info_p == nullptr) 495 { 496 if (log) 497 log->Printf("%s failed to get register info for register index %" PRIu32, 498 __FUNCTION__, reg_num); 499 continue; 500 } 501 502 if (reg_info_p->value_regs != nullptr) 503 continue; // Only expedite registers that are not contained in other registers. 504 505 RegisterValue reg_value; 506 Error error = reg_ctx_sp->ReadRegister(reg_info_p, reg_value); 507 if (error.Fail()) 508 { 509 if (log) 510 log->Printf("%s failed to read register '%s' index %" PRIu32 ": %s", __FUNCTION__, 511 reg_info_p->name ? reg_info_p->name : "<unnamed-register>", reg_num, 512 error.AsCString ()); 513 continue; 514 } 515 516 StreamString stream; 517 WriteRegisterValueInHexFixedWidth(stream, reg_ctx_sp, *reg_info_p, ®_value); 518 519 register_object_sp->SetObject(std::to_string(reg_num), 520 std::make_shared<JSONString>(stream.GetString())); 521 } 522 523 return register_object_sp; 524 } 525 526 static const char * 527 GetStopReasonString(StopReason stop_reason) 528 { 529 switch (stop_reason) 530 { 531 case eStopReasonTrace: 532 return "trace"; 533 case eStopReasonBreakpoint: 534 return "breakpoint"; 535 case eStopReasonWatchpoint: 536 return "watchpoint"; 537 case eStopReasonSignal: 538 return "signal"; 539 case eStopReasonException: 540 return "exception"; 541 case eStopReasonExec: 542 return "exec"; 543 case eStopReasonInstrumentation: 544 case eStopReasonInvalid: 545 case eStopReasonPlanComplete: 546 case eStopReasonThreadExiting: 547 case eStopReasonNone: 548 break; // ignored 549 } 550 return nullptr; 551 } 552 553 GDBRemoteCommunication::PacketResult 554 GDBRemoteCommunicationServerLLGS::SendStopReplyPacketForThread (lldb::tid_t tid) 555 { 556 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 557 558 // Ensure we have a debugged process. 559 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 560 return SendErrorResponse (50); 561 562 if (log) 563 log->Printf ("GDBRemoteCommunicationServerLLGS::%s preparing packet for pid %" PRIu64 " tid %" PRIu64, 564 __FUNCTION__, m_debugged_process_sp->GetID (), tid); 565 566 // Ensure we can get info on the given thread. 567 NativeThreadProtocolSP thread_sp (m_debugged_process_sp->GetThreadByID (tid)); 568 if (!thread_sp) 569 return SendErrorResponse (51); 570 571 // Grab the reason this thread stopped. 572 struct ThreadStopInfo tid_stop_info; 573 std::string description; 574 if (!thread_sp->GetStopReason (tid_stop_info, description)) 575 return SendErrorResponse (52); 576 577 // FIXME implement register handling for exec'd inferiors. 578 // if (tid_stop_info.reason == eStopReasonExec) 579 // { 580 // const bool force = true; 581 // InitializeRegisters(force); 582 // } 583 584 StreamString response; 585 // Output the T packet with the thread 586 response.PutChar ('T'); 587 int signum = tid_stop_info.details.signal.signo; 588 if (log) 589 { 590 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " got signal signo = %d, reason = %d, exc_type = %" PRIu64, 591 __FUNCTION__, 592 m_debugged_process_sp->GetID (), 593 tid, 594 signum, 595 tid_stop_info.reason, 596 tid_stop_info.details.exception.type); 597 } 598 599 // Print the signal number. 600 response.PutHex8 (signum & 0xff); 601 602 // Include the tid. 603 response.Printf ("thread:%" PRIx64 ";", tid); 604 605 // Include the thread name if there is one. 606 const std::string thread_name = thread_sp->GetName (); 607 if (!thread_name.empty ()) 608 { 609 size_t thread_name_len = thread_name.length (); 610 611 if (::strcspn (thread_name.c_str (), "$#+-;:") == thread_name_len) 612 { 613 response.PutCString ("name:"); 614 response.PutCString (thread_name.c_str ()); 615 } 616 else 617 { 618 // The thread name contains special chars, send as hex bytes. 619 response.PutCString ("hexname:"); 620 response.PutCStringAsRawHex8 (thread_name.c_str ()); 621 } 622 response.PutChar (';'); 623 } 624 625 // If a 'QListThreadsInStopReply' was sent to enable this feature, we 626 // will send all thread IDs back in the "threads" key whose value is 627 // a list of hex thread IDs separated by commas: 628 // "threads:10a,10b,10c;" 629 // This will save the debugger from having to send a pair of qfThreadInfo 630 // and qsThreadInfo packets, but it also might take a lot of room in the 631 // stop reply packet, so it must be enabled only on systems where there 632 // are no limits on packet lengths. 633 if (m_list_threads_in_stop_reply) 634 { 635 response.PutCString ("threads:"); 636 637 uint32_t thread_index = 0; 638 NativeThreadProtocolSP listed_thread_sp; 639 for (listed_thread_sp = m_debugged_process_sp->GetThreadAtIndex (thread_index); listed_thread_sp; ++thread_index, listed_thread_sp = m_debugged_process_sp->GetThreadAtIndex (thread_index)) 640 { 641 if (thread_index > 0) 642 response.PutChar (','); 643 response.Printf ("%" PRIx64, listed_thread_sp->GetID ()); 644 } 645 response.PutChar (';'); 646 } 647 648 // 649 // Expedite registers. 650 // 651 652 // Grab the register context. 653 NativeRegisterContextSP reg_ctx_sp = thread_sp->GetRegisterContext (); 654 if (reg_ctx_sp) 655 { 656 // Expedite all registers in the first register set (i.e. should be GPRs) that are not contained in other registers. 657 const RegisterSet *reg_set_p; 658 if (reg_ctx_sp->GetRegisterSetCount () > 0 && ((reg_set_p = reg_ctx_sp->GetRegisterSet (0)) != nullptr)) 659 { 660 if (log) 661 log->Printf ("GDBRemoteCommunicationServerLLGS::%s expediting registers from set '%s' (registers set count: %zu)", __FUNCTION__, reg_set_p->name ? reg_set_p->name : "<unnamed-set>", reg_set_p->num_registers); 662 663 for (const uint32_t *reg_num_p = reg_set_p->registers; *reg_num_p != LLDB_INVALID_REGNUM; ++reg_num_p) 664 { 665 const RegisterInfo *const reg_info_p = reg_ctx_sp->GetRegisterInfoAtIndex (*reg_num_p); 666 if (reg_info_p == nullptr) 667 { 668 if (log) 669 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to get register info for register set '%s', register index %" PRIu32, __FUNCTION__, reg_set_p->name ? reg_set_p->name : "<unnamed-set>", *reg_num_p); 670 } 671 else if (reg_info_p->value_regs == nullptr) 672 { 673 // Only expediate registers that are not contained in other registers. 674 RegisterValue reg_value; 675 Error error = reg_ctx_sp->ReadRegister (reg_info_p, reg_value); 676 if (error.Success ()) 677 { 678 response.Printf ("%.02x:", *reg_num_p); 679 WriteRegisterValueInHexFixedWidth(response, reg_ctx_sp, *reg_info_p, ®_value); 680 response.PutChar (';'); 681 } 682 else 683 { 684 if (log) 685 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to read register '%s' index %" PRIu32 ": %s", __FUNCTION__, reg_info_p->name ? reg_info_p->name : "<unnamed-register>", *reg_num_p, error.AsCString ()); 686 687 } 688 } 689 } 690 } 691 } 692 693 const char* reason_str = GetStopReasonString(tid_stop_info.reason); 694 if (reason_str != nullptr) 695 { 696 response.Printf ("reason:%s;", reason_str); 697 } 698 699 if (!description.empty()) 700 { 701 // Description may contains special chars, send as hex bytes. 702 response.PutCString ("description:"); 703 response.PutCStringAsRawHex8 (description.c_str ()); 704 response.PutChar (';'); 705 } 706 else if ((tid_stop_info.reason == eStopReasonException) && tid_stop_info.details.exception.type) 707 { 708 response.PutCString ("metype:"); 709 response.PutHex64 (tid_stop_info.details.exception.type); 710 response.PutCString (";mecount:"); 711 response.PutHex32 (tid_stop_info.details.exception.data_count); 712 response.PutChar (';'); 713 714 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; ++i) 715 { 716 response.PutCString ("medata:"); 717 response.PutHex64 (tid_stop_info.details.exception.data[i]); 718 response.PutChar (';'); 719 } 720 } 721 722 return SendPacketNoLock (response.GetData(), response.GetSize()); 723 } 724 725 void 726 GDBRemoteCommunicationServerLLGS::HandleInferiorState_Exited (NativeProcessProtocol *process) 727 { 728 assert (process && "process cannot be NULL"); 729 730 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 731 if (log) 732 log->Printf ("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 733 734 // Send the exit result, and don't flush output. 735 // Note: flushing output here would join the inferior stdio reflection thread, which 736 // would gunk up the waitpid monitor thread that is calling this. 737 PacketResult result = SendStopReasonForState (StateType::eStateExited, false); 738 if (result != PacketResult::Success) 739 { 740 if (log) 741 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to send stop notification for PID %" PRIu64 ", state: eStateExited", __FUNCTION__, process->GetID ()); 742 } 743 744 // Remove the process from the list of spawned pids. 745 { 746 Mutex::Locker locker (m_spawned_pids_mutex); 747 if (m_spawned_pids.erase (process->GetID ()) < 1) 748 { 749 if (log) 750 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to remove PID %" PRIu64 " from the spawned pids list", __FUNCTION__, process->GetID ()); 751 752 } 753 } 754 755 // FIXME can't do this yet - since process state propagation is currently 756 // synchronous, it is running off the NativeProcessProtocol's innards and 757 // will tear down the NPP while it still has code to execute. 758 #if 0 759 // Clear the NativeProcessProtocol pointer. 760 { 761 Mutex::Locker locker (m_debugged_process_mutex); 762 m_debugged_process_sp.reset(); 763 } 764 #endif 765 766 // Close the pipe to the inferior terminal i/o if we launched it 767 // and set one up. Otherwise, 'k' and its flush of stdio could 768 // end up waiting on a thread join that will never end. Consider 769 // adding a timeout to the connection thread join call so we 770 // can avoid that scenario altogether. 771 MaybeCloseInferiorTerminalConnection (); 772 773 // We are ready to exit the debug monitor. 774 m_exit_now = true; 775 } 776 777 void 778 GDBRemoteCommunicationServerLLGS::HandleInferiorState_Stopped (NativeProcessProtocol *process) 779 { 780 assert (process && "process cannot be NULL"); 781 782 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 783 if (log) 784 log->Printf ("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 785 786 // Send the stop reason unless this is the stop after the 787 // launch or attach. 788 switch (m_inferior_prev_state) 789 { 790 case eStateLaunching: 791 case eStateAttaching: 792 // Don't send anything per debugserver behavior. 793 break; 794 default: 795 // In all other cases, send the stop reason. 796 PacketResult result = SendStopReasonForState (StateType::eStateStopped, false); 797 if (result != PacketResult::Success) 798 { 799 if (log) 800 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to send stop notification for PID %" PRIu64 ", state: eStateExited", __FUNCTION__, process->GetID ()); 801 } 802 break; 803 } 804 } 805 806 void 807 GDBRemoteCommunicationServerLLGS::ProcessStateChanged (NativeProcessProtocol *process, lldb::StateType state) 808 { 809 assert (process && "process cannot be NULL"); 810 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 811 if (log) 812 { 813 log->Printf ("GDBRemoteCommunicationServerLLGS::%s called with NativeProcessProtocol pid %" PRIu64 ", state: %s", 814 __FUNCTION__, 815 process->GetID (), 816 StateAsCString (state)); 817 } 818 819 // Make sure we get all of the pending stdout/stderr from the inferior 820 // and send it to the lldb host before we send the state change 821 // notification 822 m_stdio_communication.SynchronizeWithReadThread(); 823 824 switch (state) 825 { 826 case StateType::eStateExited: 827 HandleInferiorState_Exited (process); 828 break; 829 830 case StateType::eStateStopped: 831 HandleInferiorState_Stopped (process); 832 break; 833 834 default: 835 if (log) 836 { 837 log->Printf ("GDBRemoteCommunicationServerLLGS::%s didn't handle state change for pid %" PRIu64 ", new state: %s", 838 __FUNCTION__, 839 process->GetID (), 840 StateAsCString (state)); 841 } 842 break; 843 } 844 845 // Remember the previous state reported to us. 846 m_inferior_prev_state = state; 847 } 848 849 void 850 GDBRemoteCommunicationServerLLGS::DidExec (NativeProcessProtocol *process) 851 { 852 ClearProcessSpecificData (); 853 } 854 855 void 856 GDBRemoteCommunicationServerLLGS::DataAvailableCallback () 857 { 858 Log *log (GetLogIfAnyCategoriesSet(GDBR_LOG_COMM)); 859 860 if (! m_handshake_completed) 861 { 862 if (! HandshakeWithClient()) 863 { 864 if(log) 865 log->Printf("GDBRemoteCommunicationServerLLGS::%s handshake with client failed, exiting", 866 __FUNCTION__); 867 m_read_handle_up.reset(); 868 m_mainloop.RequestTermination(); 869 return; 870 } 871 m_handshake_completed = true; 872 } 873 874 bool interrupt = false; 875 bool done = false; 876 Error error; 877 while (true) 878 { 879 const PacketResult result = GetPacketAndSendResponse (0, error, interrupt, done); 880 if (result == PacketResult::ErrorReplyTimeout) 881 break; // No more packets in the queue 882 883 if ((result != PacketResult::Success)) 884 { 885 if(log) 886 log->Printf("GDBRemoteCommunicationServerLLGS::%s processing a packet failed: %s", 887 __FUNCTION__, error.AsCString()); 888 m_read_handle_up.reset(); 889 m_mainloop.RequestTermination(); 890 break; 891 } 892 } 893 } 894 895 Error 896 GDBRemoteCommunicationServerLLGS::InitializeConnection (std::unique_ptr<Connection> &&connection) 897 { 898 IOObjectSP read_object_sp = connection->GetReadObject(); 899 GDBRemoteCommunicationServer::SetConnection(connection.release()); 900 901 Error error; 902 m_read_handle_up = m_mainloop.RegisterReadObject(read_object_sp, 903 [this] (MainLoopBase &) { DataAvailableCallback(); }, error); 904 return error; 905 } 906 907 GDBRemoteCommunication::PacketResult 908 GDBRemoteCommunicationServerLLGS::SendONotification (const char *buffer, uint32_t len) 909 { 910 if ((buffer == nullptr) || (len == 0)) 911 { 912 // Nothing to send. 913 return PacketResult::Success; 914 } 915 916 StreamString response; 917 response.PutChar ('O'); 918 response.PutBytesAsRawHex8 (buffer, len); 919 920 return SendPacketNoLock (response.GetData (), response.GetSize ()); 921 } 922 923 Error 924 GDBRemoteCommunicationServerLLGS::SetSTDIOFileDescriptor (int fd) 925 { 926 Error error; 927 928 // Set up the Read Thread for reading/handling process I/O 929 std::unique_ptr<ConnectionFileDescriptor> conn_up (new ConnectionFileDescriptor (fd, true)); 930 if (!conn_up) 931 { 932 error.SetErrorString ("failed to create ConnectionFileDescriptor"); 933 return error; 934 } 935 936 m_stdio_communication.SetCloseOnEOF (false); 937 m_stdio_communication.SetConnection (conn_up.release()); 938 if (!m_stdio_communication.IsConnected ()) 939 { 940 error.SetErrorString ("failed to set connection for inferior I/O communication"); 941 return error; 942 } 943 944 // llgs local-process debugging may specify PTY paths, which will make these 945 // file actions non-null 946 // process launch -e/o will also make these file actions non-null 947 // nullptr means that the traffic is expected to flow over gdb-remote protocol 948 if ( 949 m_process_launch_info.GetFileActionForFD(STDOUT_FILENO) == nullptr || 950 m_process_launch_info.GetFileActionForFD(STDERR_FILENO) == nullptr 951 ) 952 { 953 // output from the process must be forwarded over gdb-remote 954 // create a thread to read the handle and send the data 955 m_stdio_communication.SetReadThreadBytesReceivedCallback (STDIOReadThreadBytesReceived, this); 956 m_stdio_communication.StartReadThread(); 957 } 958 959 return error; 960 } 961 962 void 963 GDBRemoteCommunicationServerLLGS::STDIOReadThreadBytesReceived (void *baton, const void *src, size_t src_len) 964 { 965 GDBRemoteCommunicationServerLLGS *server = reinterpret_cast<GDBRemoteCommunicationServerLLGS*> (baton); 966 static_cast<void> (server->SendONotification (static_cast<const char *>(src), src_len)); 967 } 968 969 GDBRemoteCommunication::PacketResult 970 GDBRemoteCommunicationServerLLGS::Handle_qProcessInfo (StringExtractorGDBRemote &packet) 971 { 972 // Fail if we don't have a current process. 973 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 974 return SendErrorResponse (68); 975 976 lldb::pid_t pid = m_debugged_process_sp->GetID (); 977 978 if (pid == LLDB_INVALID_PROCESS_ID) 979 return SendErrorResponse (1); 980 981 ProcessInstanceInfo proc_info; 982 if (!Host::GetProcessInfo (pid, proc_info)) 983 return SendErrorResponse (1); 984 985 StreamString response; 986 CreateProcessInfoResponse_DebugServerStyle(proc_info, response); 987 return SendPacketNoLock (response.GetData (), response.GetSize ()); 988 } 989 990 GDBRemoteCommunication::PacketResult 991 GDBRemoteCommunicationServerLLGS::Handle_qC (StringExtractorGDBRemote &packet) 992 { 993 // Fail if we don't have a current process. 994 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 995 return SendErrorResponse (68); 996 997 // Make sure we set the current thread so g and p packets return 998 // the data the gdb will expect. 999 lldb::tid_t tid = m_debugged_process_sp->GetCurrentThreadID (); 1000 SetCurrentThreadID (tid); 1001 1002 NativeThreadProtocolSP thread_sp = m_debugged_process_sp->GetCurrentThread (); 1003 if (!thread_sp) 1004 return SendErrorResponse (69); 1005 1006 StreamString response; 1007 response.Printf ("QC%" PRIx64, thread_sp->GetID ()); 1008 1009 return SendPacketNoLock (response.GetData(), response.GetSize()); 1010 } 1011 1012 bool 1013 GDBRemoteCommunicationServerLLGS::DebuggedProcessReaped (lldb::pid_t pid) 1014 { 1015 // reap a process that we were debugging (but not debugserver) 1016 Mutex::Locker locker (m_spawned_pids_mutex); 1017 return m_spawned_pids.erase(pid) > 0; 1018 } 1019 1020 bool 1021 GDBRemoteCommunicationServerLLGS::ReapDebuggedProcess (void *callback_baton, 1022 lldb::pid_t pid, 1023 bool exited, 1024 int signal, // Zero for no signal 1025 int status) // Exit value of process if signal is zero 1026 { 1027 GDBRemoteCommunicationServerLLGS *server = (GDBRemoteCommunicationServerLLGS *)callback_baton; 1028 server->DebuggedProcessReaped (pid); 1029 return true; 1030 } 1031 1032 GDBRemoteCommunication::PacketResult 1033 GDBRemoteCommunicationServerLLGS::Handle_k (StringExtractorGDBRemote &packet) 1034 { 1035 // shutdown all spawned processes 1036 std::set<lldb::pid_t> spawned_pids_copy; 1037 1038 // copy pids 1039 { 1040 Mutex::Locker locker (m_spawned_pids_mutex); 1041 spawned_pids_copy.insert (m_spawned_pids.begin (), m_spawned_pids.end ()); 1042 } 1043 1044 // nuke the spawned processes 1045 for (auto it = spawned_pids_copy.begin (); it != spawned_pids_copy.end (); ++it) 1046 { 1047 lldb::pid_t spawned_pid = *it; 1048 if (!KillSpawnedProcess (spawned_pid)) 1049 { 1050 fprintf (stderr, "%s: failed to kill spawned pid %" PRIu64 ", ignoring.\n", __FUNCTION__, spawned_pid); 1051 } 1052 } 1053 1054 FlushInferiorOutput (); 1055 1056 // No OK response for kill packet. 1057 // return SendOKResponse (); 1058 return PacketResult::Success; 1059 } 1060 1061 GDBRemoteCommunication::PacketResult 1062 GDBRemoteCommunicationServerLLGS::Handle_QSetDisableASLR (StringExtractorGDBRemote &packet) 1063 { 1064 packet.SetFilePos(::strlen ("QSetDisableASLR:")); 1065 if (packet.GetU32(0)) 1066 m_process_launch_info.GetFlags().Set (eLaunchFlagDisableASLR); 1067 else 1068 m_process_launch_info.GetFlags().Clear (eLaunchFlagDisableASLR); 1069 return SendOKResponse (); 1070 } 1071 1072 GDBRemoteCommunication::PacketResult 1073 GDBRemoteCommunicationServerLLGS::Handle_QSetWorkingDir (StringExtractorGDBRemote &packet) 1074 { 1075 packet.SetFilePos (::strlen ("QSetWorkingDir:")); 1076 std::string path; 1077 packet.GetHexByteString (path); 1078 m_process_launch_info.SetWorkingDirectory(FileSpec{path, true}); 1079 return SendOKResponse (); 1080 } 1081 1082 GDBRemoteCommunication::PacketResult 1083 GDBRemoteCommunicationServerLLGS::Handle_qGetWorkingDir (StringExtractorGDBRemote &packet) 1084 { 1085 FileSpec working_dir{m_process_launch_info.GetWorkingDirectory()}; 1086 if (working_dir) 1087 { 1088 StreamString response; 1089 response.PutCStringAsRawHex8(working_dir.GetCString()); 1090 return SendPacketNoLock(response.GetData(), response.GetSize()); 1091 } 1092 1093 return SendErrorResponse(14); 1094 } 1095 1096 GDBRemoteCommunication::PacketResult 1097 GDBRemoteCommunicationServerLLGS::Handle_C (StringExtractorGDBRemote &packet) 1098 { 1099 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_THREAD)); 1100 if (log) 1101 log->Printf ("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 1102 1103 // Ensure we have a native process. 1104 if (!m_debugged_process_sp) 1105 { 1106 if (log) 1107 log->Printf ("GDBRemoteCommunicationServerLLGS::%s no debugged process shared pointer", __FUNCTION__); 1108 return SendErrorResponse (0x36); 1109 } 1110 1111 // Pull out the signal number. 1112 packet.SetFilePos (::strlen ("C")); 1113 if (packet.GetBytesLeft () < 1) 1114 { 1115 // Shouldn't be using a C without a signal. 1116 return SendIllFormedResponse (packet, "C packet specified without signal."); 1117 } 1118 const uint32_t signo = packet.GetHexMaxU32 (false, std::numeric_limits<uint32_t>::max ()); 1119 if (signo == std::numeric_limits<uint32_t>::max ()) 1120 return SendIllFormedResponse (packet, "failed to parse signal number"); 1121 1122 // Handle optional continue address. 1123 if (packet.GetBytesLeft () > 0) 1124 { 1125 // FIXME add continue at address support for $C{signo}[;{continue-address}]. 1126 if (*packet.Peek () == ';') 1127 return SendUnimplementedResponse (packet.GetStringRef().c_str()); 1128 else 1129 return SendIllFormedResponse (packet, "unexpected content after $C{signal-number}"); 1130 } 1131 1132 ResumeActionList resume_actions (StateType::eStateRunning, 0); 1133 Error error; 1134 1135 // We have two branches: what to do if a continue thread is specified (in which case we target 1136 // sending the signal to that thread), or when we don't have a continue thread set (in which 1137 // case we send a signal to the process). 1138 1139 // TODO discuss with Greg Clayton, make sure this makes sense. 1140 1141 lldb::tid_t signal_tid = GetContinueThreadID (); 1142 if (signal_tid != LLDB_INVALID_THREAD_ID) 1143 { 1144 // The resume action for the continue thread (or all threads if a continue thread is not set). 1145 ResumeAction action = { GetContinueThreadID (), StateType::eStateRunning, static_cast<int> (signo) }; 1146 1147 // Add the action for the continue thread (or all threads when the continue thread isn't present). 1148 resume_actions.Append (action); 1149 } 1150 else 1151 { 1152 // Send the signal to the process since we weren't targeting a specific continue thread with the signal. 1153 error = m_debugged_process_sp->Signal (signo); 1154 if (error.Fail ()) 1155 { 1156 if (log) 1157 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to send signal for process %" PRIu64 ": %s", 1158 __FUNCTION__, 1159 m_debugged_process_sp->GetID (), 1160 error.AsCString ()); 1161 1162 return SendErrorResponse (0x52); 1163 } 1164 } 1165 1166 // Resume the threads. 1167 error = m_debugged_process_sp->Resume (resume_actions); 1168 if (error.Fail ()) 1169 { 1170 if (log) 1171 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to resume threads for process %" PRIu64 ": %s", 1172 __FUNCTION__, 1173 m_debugged_process_sp->GetID (), 1174 error.AsCString ()); 1175 1176 return SendErrorResponse (0x38); 1177 } 1178 1179 // Don't send an "OK" packet; response is the stopped/exited message. 1180 return PacketResult::Success; 1181 } 1182 1183 GDBRemoteCommunication::PacketResult 1184 GDBRemoteCommunicationServerLLGS::Handle_c (StringExtractorGDBRemote &packet) 1185 { 1186 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_THREAD)); 1187 if (log) 1188 log->Printf ("GDBRemoteCommunicationServerLLGS::%s called", __FUNCTION__); 1189 1190 packet.SetFilePos (packet.GetFilePos() + ::strlen ("c")); 1191 1192 // For now just support all continue. 1193 const bool has_continue_address = (packet.GetBytesLeft () > 0); 1194 if (has_continue_address) 1195 { 1196 if (log) 1197 log->Printf ("GDBRemoteCommunicationServerLLGS::%s not implemented for c{address} variant [%s remains]", __FUNCTION__, packet.Peek ()); 1198 return SendUnimplementedResponse (packet.GetStringRef().c_str()); 1199 } 1200 1201 // Ensure we have a native process. 1202 if (!m_debugged_process_sp) 1203 { 1204 if (log) 1205 log->Printf ("GDBRemoteCommunicationServerLLGS::%s no debugged process shared pointer", __FUNCTION__); 1206 return SendErrorResponse (0x36); 1207 } 1208 1209 // Build the ResumeActionList 1210 ResumeActionList actions (StateType::eStateRunning, 0); 1211 1212 Error error = m_debugged_process_sp->Resume (actions); 1213 if (error.Fail ()) 1214 { 1215 if (log) 1216 { 1217 log->Printf ("GDBRemoteCommunicationServerLLGS::%s c failed for process %" PRIu64 ": %s", 1218 __FUNCTION__, 1219 m_debugged_process_sp->GetID (), 1220 error.AsCString ()); 1221 } 1222 return SendErrorResponse (GDBRemoteServerError::eErrorResume); 1223 } 1224 1225 if (log) 1226 log->Printf ("GDBRemoteCommunicationServerLLGS::%s continued process %" PRIu64, __FUNCTION__, m_debugged_process_sp->GetID ()); 1227 1228 // No response required from continue. 1229 return PacketResult::Success; 1230 } 1231 1232 GDBRemoteCommunication::PacketResult 1233 GDBRemoteCommunicationServerLLGS::Handle_vCont_actions (StringExtractorGDBRemote &packet) 1234 { 1235 StreamString response; 1236 response.Printf("vCont;c;C;s;S"); 1237 1238 return SendPacketNoLock(response.GetData(), response.GetSize()); 1239 } 1240 1241 GDBRemoteCommunication::PacketResult 1242 GDBRemoteCommunicationServerLLGS::Handle_vCont (StringExtractorGDBRemote &packet) 1243 { 1244 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1245 if (log) 1246 log->Printf ("GDBRemoteCommunicationServerLLGS::%s handling vCont packet", __FUNCTION__); 1247 1248 packet.SetFilePos (::strlen ("vCont")); 1249 1250 if (packet.GetBytesLeft() == 0) 1251 { 1252 if (log) 1253 log->Printf ("GDBRemoteCommunicationServerLLGS::%s missing action from vCont package", __FUNCTION__); 1254 return SendIllFormedResponse (packet, "Missing action from vCont package"); 1255 } 1256 1257 // Check if this is all continue (no options or ";c"). 1258 if (::strcmp (packet.Peek (), ";c") == 0) 1259 { 1260 // Move past the ';', then do a simple 'c'. 1261 packet.SetFilePos (packet.GetFilePos () + 1); 1262 return Handle_c (packet); 1263 } 1264 else if (::strcmp (packet.Peek (), ";s") == 0) 1265 { 1266 // Move past the ';', then do a simple 's'. 1267 packet.SetFilePos (packet.GetFilePos () + 1); 1268 return Handle_s (packet); 1269 } 1270 1271 // Ensure we have a native process. 1272 if (!m_debugged_process_sp) 1273 { 1274 if (log) 1275 log->Printf ("GDBRemoteCommunicationServerLLGS::%s no debugged process shared pointer", __FUNCTION__); 1276 return SendErrorResponse (0x36); 1277 } 1278 1279 ResumeActionList thread_actions; 1280 1281 while (packet.GetBytesLeft () && *packet.Peek () == ';') 1282 { 1283 // Skip the semi-colon. 1284 packet.GetChar (); 1285 1286 // Build up the thread action. 1287 ResumeAction thread_action; 1288 thread_action.tid = LLDB_INVALID_THREAD_ID; 1289 thread_action.state = eStateInvalid; 1290 thread_action.signal = 0; 1291 1292 const char action = packet.GetChar (); 1293 switch (action) 1294 { 1295 case 'C': 1296 thread_action.signal = packet.GetHexMaxU32 (false, 0); 1297 if (thread_action.signal == 0) 1298 return SendIllFormedResponse (packet, "Could not parse signal in vCont packet C action"); 1299 // Fall through to next case... 1300 1301 case 'c': 1302 // Continue 1303 thread_action.state = eStateRunning; 1304 break; 1305 1306 case 'S': 1307 thread_action.signal = packet.GetHexMaxU32 (false, 0); 1308 if (thread_action.signal == 0) 1309 return SendIllFormedResponse (packet, "Could not parse signal in vCont packet S action"); 1310 // Fall through to next case... 1311 1312 case 's': 1313 // Step 1314 thread_action.state = eStateStepping; 1315 break; 1316 1317 default: 1318 return SendIllFormedResponse (packet, "Unsupported vCont action"); 1319 break; 1320 } 1321 1322 // Parse out optional :{thread-id} value. 1323 if (packet.GetBytesLeft () && (*packet.Peek () == ':')) 1324 { 1325 // Consume the separator. 1326 packet.GetChar (); 1327 1328 thread_action.tid = packet.GetHexMaxU32 (false, LLDB_INVALID_THREAD_ID); 1329 if (thread_action.tid == LLDB_INVALID_THREAD_ID) 1330 return SendIllFormedResponse (packet, "Could not parse thread number in vCont packet"); 1331 } 1332 1333 thread_actions.Append (thread_action); 1334 } 1335 1336 Error error = m_debugged_process_sp->Resume (thread_actions); 1337 if (error.Fail ()) 1338 { 1339 if (log) 1340 { 1341 log->Printf ("GDBRemoteCommunicationServerLLGS::%s vCont failed for process %" PRIu64 ": %s", 1342 __FUNCTION__, 1343 m_debugged_process_sp->GetID (), 1344 error.AsCString ()); 1345 } 1346 return SendErrorResponse (GDBRemoteServerError::eErrorResume); 1347 } 1348 1349 if (log) 1350 log->Printf ("GDBRemoteCommunicationServerLLGS::%s continued process %" PRIu64, __FUNCTION__, m_debugged_process_sp->GetID ()); 1351 1352 // No response required from vCont. 1353 return PacketResult::Success; 1354 } 1355 1356 void 1357 GDBRemoteCommunicationServerLLGS::SetCurrentThreadID (lldb::tid_t tid) 1358 { 1359 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_THREAD)); 1360 if (log) 1361 log->Printf ("GDBRemoteCommunicationServerLLGS::%s setting current thread id to %" PRIu64, __FUNCTION__, tid); 1362 1363 m_current_tid = tid; 1364 if (m_debugged_process_sp) 1365 m_debugged_process_sp->SetCurrentThreadID (m_current_tid); 1366 } 1367 1368 void 1369 GDBRemoteCommunicationServerLLGS::SetContinueThreadID (lldb::tid_t tid) 1370 { 1371 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_THREAD)); 1372 if (log) 1373 log->Printf ("GDBRemoteCommunicationServerLLGS::%s setting continue thread id to %" PRIu64, __FUNCTION__, tid); 1374 1375 m_continue_tid = tid; 1376 } 1377 1378 GDBRemoteCommunication::PacketResult 1379 GDBRemoteCommunicationServerLLGS::Handle_stop_reason (StringExtractorGDBRemote &packet) 1380 { 1381 // Handle the $? gdbremote command. 1382 1383 // If no process, indicate error 1384 if (!m_debugged_process_sp) 1385 return SendErrorResponse (02); 1386 1387 return SendStopReasonForState (m_debugged_process_sp->GetState (), true); 1388 } 1389 1390 GDBRemoteCommunication::PacketResult 1391 GDBRemoteCommunicationServerLLGS::SendStopReasonForState (lldb::StateType process_state, bool flush_on_exit) 1392 { 1393 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1394 1395 switch (process_state) 1396 { 1397 case eStateAttaching: 1398 case eStateLaunching: 1399 case eStateRunning: 1400 case eStateStepping: 1401 case eStateDetached: 1402 // NOTE: gdb protocol doc looks like it should return $OK 1403 // when everything is running (i.e. no stopped result). 1404 return PacketResult::Success; // Ignore 1405 1406 case eStateSuspended: 1407 case eStateStopped: 1408 case eStateCrashed: 1409 { 1410 lldb::tid_t tid = m_debugged_process_sp->GetCurrentThreadID (); 1411 // Make sure we set the current thread so g and p packets return 1412 // the data the gdb will expect. 1413 SetCurrentThreadID (tid); 1414 return SendStopReplyPacketForThread (tid); 1415 } 1416 1417 case eStateInvalid: 1418 case eStateUnloaded: 1419 case eStateExited: 1420 if (flush_on_exit) 1421 FlushInferiorOutput (); 1422 return SendWResponse(m_debugged_process_sp.get()); 1423 1424 default: 1425 if (log) 1426 { 1427 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 ", current state reporting not handled: %s", 1428 __FUNCTION__, 1429 m_debugged_process_sp->GetID (), 1430 StateAsCString (process_state)); 1431 } 1432 break; 1433 } 1434 1435 return SendErrorResponse (0); 1436 } 1437 1438 GDBRemoteCommunication::PacketResult 1439 GDBRemoteCommunicationServerLLGS::Handle_qRegisterInfo (StringExtractorGDBRemote &packet) 1440 { 1441 // Fail if we don't have a current process. 1442 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 1443 return SendErrorResponse (68); 1444 1445 // Ensure we have a thread. 1446 NativeThreadProtocolSP thread_sp (m_debugged_process_sp->GetThreadAtIndex (0)); 1447 if (!thread_sp) 1448 return SendErrorResponse (69); 1449 1450 // Get the register context for the first thread. 1451 NativeRegisterContextSP reg_context_sp (thread_sp->GetRegisterContext ()); 1452 if (!reg_context_sp) 1453 return SendErrorResponse (69); 1454 1455 // Parse out the register number from the request. 1456 packet.SetFilePos (strlen("qRegisterInfo")); 1457 const uint32_t reg_index = packet.GetHexMaxU32 (false, std::numeric_limits<uint32_t>::max ()); 1458 if (reg_index == std::numeric_limits<uint32_t>::max ()) 1459 return SendErrorResponse (69); 1460 1461 // Return the end of registers response if we've iterated one past the end of the register set. 1462 if (reg_index >= reg_context_sp->GetUserRegisterCount ()) 1463 return SendErrorResponse (69); 1464 1465 const RegisterInfo *reg_info = reg_context_sp->GetRegisterInfoAtIndex(reg_index); 1466 if (!reg_info) 1467 return SendErrorResponse (69); 1468 1469 // Build the reginfos response. 1470 StreamGDBRemote response; 1471 1472 response.PutCString ("name:"); 1473 response.PutCString (reg_info->name); 1474 response.PutChar (';'); 1475 1476 if (reg_info->alt_name && reg_info->alt_name[0]) 1477 { 1478 response.PutCString ("alt-name:"); 1479 response.PutCString (reg_info->alt_name); 1480 response.PutChar (';'); 1481 } 1482 1483 response.Printf ("bitsize:%" PRIu32 ";offset:%" PRIu32 ";", reg_info->byte_size * 8, reg_info->byte_offset); 1484 1485 switch (reg_info->encoding) 1486 { 1487 case eEncodingUint: response.PutCString ("encoding:uint;"); break; 1488 case eEncodingSint: response.PutCString ("encoding:sint;"); break; 1489 case eEncodingIEEE754: response.PutCString ("encoding:ieee754;"); break; 1490 case eEncodingVector: response.PutCString ("encoding:vector;"); break; 1491 default: break; 1492 } 1493 1494 switch (reg_info->format) 1495 { 1496 case eFormatBinary: response.PutCString ("format:binary;"); break; 1497 case eFormatDecimal: response.PutCString ("format:decimal;"); break; 1498 case eFormatHex: response.PutCString ("format:hex;"); break; 1499 case eFormatFloat: response.PutCString ("format:float;"); break; 1500 case eFormatVectorOfSInt8: response.PutCString ("format:vector-sint8;"); break; 1501 case eFormatVectorOfUInt8: response.PutCString ("format:vector-uint8;"); break; 1502 case eFormatVectorOfSInt16: response.PutCString ("format:vector-sint16;"); break; 1503 case eFormatVectorOfUInt16: response.PutCString ("format:vector-uint16;"); break; 1504 case eFormatVectorOfSInt32: response.PutCString ("format:vector-sint32;"); break; 1505 case eFormatVectorOfUInt32: response.PutCString ("format:vector-uint32;"); break; 1506 case eFormatVectorOfFloat32: response.PutCString ("format:vector-float32;"); break; 1507 case eFormatVectorOfUInt128: response.PutCString ("format:vector-uint128;"); break; 1508 default: break; 1509 }; 1510 1511 const char *const register_set_name = reg_context_sp->GetRegisterSetNameForRegisterAtIndex(reg_index); 1512 if (register_set_name) 1513 { 1514 response.PutCString ("set:"); 1515 response.PutCString (register_set_name); 1516 response.PutChar (';'); 1517 } 1518 1519 if (reg_info->kinds[RegisterKind::eRegisterKindGCC] != LLDB_INVALID_REGNUM) 1520 response.Printf ("gcc:%" PRIu32 ";", reg_info->kinds[RegisterKind::eRegisterKindGCC]); 1521 1522 if (reg_info->kinds[RegisterKind::eRegisterKindDWARF] != LLDB_INVALID_REGNUM) 1523 response.Printf ("dwarf:%" PRIu32 ";", reg_info->kinds[RegisterKind::eRegisterKindDWARF]); 1524 1525 switch (reg_info->kinds[RegisterKind::eRegisterKindGeneric]) 1526 { 1527 case LLDB_REGNUM_GENERIC_PC: response.PutCString("generic:pc;"); break; 1528 case LLDB_REGNUM_GENERIC_SP: response.PutCString("generic:sp;"); break; 1529 case LLDB_REGNUM_GENERIC_FP: response.PutCString("generic:fp;"); break; 1530 case LLDB_REGNUM_GENERIC_RA: response.PutCString("generic:ra;"); break; 1531 case LLDB_REGNUM_GENERIC_FLAGS: response.PutCString("generic:flags;"); break; 1532 case LLDB_REGNUM_GENERIC_ARG1: response.PutCString("generic:arg1;"); break; 1533 case LLDB_REGNUM_GENERIC_ARG2: response.PutCString("generic:arg2;"); break; 1534 case LLDB_REGNUM_GENERIC_ARG3: response.PutCString("generic:arg3;"); break; 1535 case LLDB_REGNUM_GENERIC_ARG4: response.PutCString("generic:arg4;"); break; 1536 case LLDB_REGNUM_GENERIC_ARG5: response.PutCString("generic:arg5;"); break; 1537 case LLDB_REGNUM_GENERIC_ARG6: response.PutCString("generic:arg6;"); break; 1538 case LLDB_REGNUM_GENERIC_ARG7: response.PutCString("generic:arg7;"); break; 1539 case LLDB_REGNUM_GENERIC_ARG8: response.PutCString("generic:arg8;"); break; 1540 default: break; 1541 } 1542 1543 if (reg_info->value_regs && reg_info->value_regs[0] != LLDB_INVALID_REGNUM) 1544 { 1545 response.PutCString ("container-regs:"); 1546 int i = 0; 1547 for (const uint32_t *reg_num = reg_info->value_regs; *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) 1548 { 1549 if (i > 0) 1550 response.PutChar (','); 1551 response.Printf ("%" PRIx32, *reg_num); 1552 } 1553 response.PutChar (';'); 1554 } 1555 1556 if (reg_info->invalidate_regs && reg_info->invalidate_regs[0]) 1557 { 1558 response.PutCString ("invalidate-regs:"); 1559 int i = 0; 1560 for (const uint32_t *reg_num = reg_info->invalidate_regs; *reg_num != LLDB_INVALID_REGNUM; ++reg_num, ++i) 1561 { 1562 if (i > 0) 1563 response.PutChar (','); 1564 response.Printf ("%" PRIx32, *reg_num); 1565 } 1566 response.PutChar (';'); 1567 } 1568 1569 return SendPacketNoLock(response.GetData(), response.GetSize()); 1570 } 1571 1572 GDBRemoteCommunication::PacketResult 1573 GDBRemoteCommunicationServerLLGS::Handle_qfThreadInfo (StringExtractorGDBRemote &packet) 1574 { 1575 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1576 1577 // Fail if we don't have a current process. 1578 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 1579 { 1580 if (log) 1581 log->Printf ("GDBRemoteCommunicationServerLLGS::%s() no process (%s), returning OK", __FUNCTION__, m_debugged_process_sp ? "invalid process id" : "null m_debugged_process_sp"); 1582 return SendOKResponse (); 1583 } 1584 1585 StreamGDBRemote response; 1586 response.PutChar ('m'); 1587 1588 if (log) 1589 log->Printf ("GDBRemoteCommunicationServerLLGS::%s() starting thread iteration", __FUNCTION__); 1590 1591 NativeThreadProtocolSP thread_sp; 1592 uint32_t thread_index; 1593 for (thread_index = 0, thread_sp = m_debugged_process_sp->GetThreadAtIndex (thread_index); 1594 thread_sp; 1595 ++thread_index, thread_sp = m_debugged_process_sp->GetThreadAtIndex (thread_index)) 1596 { 1597 if (log) 1598 log->Printf ("GDBRemoteCommunicationServerLLGS::%s() iterated thread %" PRIu32 "(%s, tid=0x%" PRIx64 ")", __FUNCTION__, thread_index, thread_sp ? "is not null" : "null", thread_sp ? thread_sp->GetID () : LLDB_INVALID_THREAD_ID); 1599 if (thread_index > 0) 1600 response.PutChar(','); 1601 response.Printf ("%" PRIx64, thread_sp->GetID ()); 1602 } 1603 1604 if (log) 1605 log->Printf ("GDBRemoteCommunicationServerLLGS::%s() finished thread iteration", __FUNCTION__); 1606 1607 return SendPacketNoLock(response.GetData(), response.GetSize()); 1608 } 1609 1610 GDBRemoteCommunication::PacketResult 1611 GDBRemoteCommunicationServerLLGS::Handle_qsThreadInfo (StringExtractorGDBRemote &packet) 1612 { 1613 // FIXME for now we return the full thread list in the initial packet and always do nothing here. 1614 return SendPacketNoLock ("l", 1); 1615 } 1616 1617 GDBRemoteCommunication::PacketResult 1618 GDBRemoteCommunicationServerLLGS::Handle_p (StringExtractorGDBRemote &packet) 1619 { 1620 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1621 1622 // Parse out the register number from the request. 1623 packet.SetFilePos (strlen("p")); 1624 const uint32_t reg_index = packet.GetHexMaxU32 (false, std::numeric_limits<uint32_t>::max ()); 1625 if (reg_index == std::numeric_limits<uint32_t>::max ()) 1626 { 1627 if (log) 1628 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, could not parse register number from request \"%s\"", __FUNCTION__, packet.GetStringRef ().c_str ()); 1629 return SendErrorResponse (0x15); 1630 } 1631 1632 // Get the thread to use. 1633 NativeThreadProtocolSP thread_sp = GetThreadFromSuffix (packet); 1634 if (!thread_sp) 1635 { 1636 if (log) 1637 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no thread available", __FUNCTION__); 1638 return SendErrorResponse (0x15); 1639 } 1640 1641 // Get the thread's register context. 1642 NativeRegisterContextSP reg_context_sp (thread_sp->GetRegisterContext ()); 1643 if (!reg_context_sp) 1644 { 1645 if (log) 1646 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " failed, no register context available for the thread", __FUNCTION__, m_debugged_process_sp->GetID (), thread_sp->GetID ()); 1647 return SendErrorResponse (0x15); 1648 } 1649 1650 // Return the end of registers response if we've iterated one past the end of the register set. 1651 if (reg_index >= reg_context_sp->GetUserRegisterCount ()) 1652 { 1653 if (log) 1654 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, requested register %" PRIu32 " beyond register count %" PRIu32, __FUNCTION__, reg_index, reg_context_sp->GetUserRegisterCount ()); 1655 return SendErrorResponse (0x15); 1656 } 1657 1658 const RegisterInfo *reg_info = reg_context_sp->GetRegisterInfoAtIndex(reg_index); 1659 if (!reg_info) 1660 { 1661 if (log) 1662 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, requested register %" PRIu32 " returned NULL", __FUNCTION__, reg_index); 1663 return SendErrorResponse (0x15); 1664 } 1665 1666 // Build the reginfos response. 1667 StreamGDBRemote response; 1668 1669 // Retrieve the value 1670 RegisterValue reg_value; 1671 Error error = reg_context_sp->ReadRegister (reg_info, reg_value); 1672 if (error.Fail ()) 1673 { 1674 if (log) 1675 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, read of requested register %" PRIu32 " (%s) failed: %s", __FUNCTION__, reg_index, reg_info->name, error.AsCString ()); 1676 return SendErrorResponse (0x15); 1677 } 1678 1679 const uint8_t *const data = reinterpret_cast<const uint8_t*> (reg_value.GetBytes ()); 1680 if (!data) 1681 { 1682 if (log) 1683 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to get data bytes from requested register %" PRIu32, __FUNCTION__, reg_index); 1684 return SendErrorResponse (0x15); 1685 } 1686 1687 // FIXME flip as needed to get data in big/little endian format for this host. 1688 for (uint32_t i = 0; i < reg_value.GetByteSize (); ++i) 1689 response.PutHex8 (data[i]); 1690 1691 return SendPacketNoLock (response.GetData (), response.GetSize ()); 1692 } 1693 1694 GDBRemoteCommunication::PacketResult 1695 GDBRemoteCommunicationServerLLGS::Handle_P (StringExtractorGDBRemote &packet) 1696 { 1697 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1698 1699 // Ensure there is more content. 1700 if (packet.GetBytesLeft () < 1) 1701 return SendIllFormedResponse (packet, "Empty P packet"); 1702 1703 // Parse out the register number from the request. 1704 packet.SetFilePos (strlen("P")); 1705 const uint32_t reg_index = packet.GetHexMaxU32 (false, std::numeric_limits<uint32_t>::max ()); 1706 if (reg_index == std::numeric_limits<uint32_t>::max ()) 1707 { 1708 if (log) 1709 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, could not parse register number from request \"%s\"", __FUNCTION__, packet.GetStringRef ().c_str ()); 1710 return SendErrorResponse (0x29); 1711 } 1712 1713 // Note debugserver would send an E30 here. 1714 if ((packet.GetBytesLeft () < 1) || (packet.GetChar () != '=')) 1715 return SendIllFormedResponse (packet, "P packet missing '=' char after register number"); 1716 1717 // Get process architecture. 1718 ArchSpec process_arch; 1719 if (!m_debugged_process_sp || !m_debugged_process_sp->GetArchitecture (process_arch)) 1720 { 1721 if (log) 1722 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to retrieve inferior architecture", __FUNCTION__); 1723 return SendErrorResponse (0x49); 1724 } 1725 1726 // Parse out the value. 1727 uint8_t reg_bytes[32]; // big enough to support up to 256 bit ymmN register 1728 size_t reg_size = packet.GetHexBytesAvail (reg_bytes, sizeof(reg_bytes)); 1729 1730 // Get the thread to use. 1731 NativeThreadProtocolSP thread_sp = GetThreadFromSuffix (packet); 1732 if (!thread_sp) 1733 { 1734 if (log) 1735 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no thread available (thread index 0)", __FUNCTION__); 1736 return SendErrorResponse (0x28); 1737 } 1738 1739 // Get the thread's register context. 1740 NativeRegisterContextSP reg_context_sp (thread_sp->GetRegisterContext ()); 1741 if (!reg_context_sp) 1742 { 1743 if (log) 1744 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " failed, no register context available for the thread", __FUNCTION__, m_debugged_process_sp->GetID (), thread_sp->GetID ()); 1745 return SendErrorResponse (0x15); 1746 } 1747 1748 const RegisterInfo *reg_info = reg_context_sp->GetRegisterInfoAtIndex (reg_index); 1749 if (!reg_info) 1750 { 1751 if (log) 1752 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, requested register %" PRIu32 " returned NULL", __FUNCTION__, reg_index); 1753 return SendErrorResponse (0x48); 1754 } 1755 1756 // Return the end of registers response if we've iterated one past the end of the register set. 1757 if (reg_index >= reg_context_sp->GetUserRegisterCount ()) 1758 { 1759 if (log) 1760 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, requested register %" PRIu32 " beyond register count %" PRIu32, __FUNCTION__, reg_index, reg_context_sp->GetUserRegisterCount ()); 1761 return SendErrorResponse (0x47); 1762 } 1763 1764 if (reg_size != reg_info->byte_size) 1765 { 1766 return SendIllFormedResponse (packet, "P packet register size is incorrect"); 1767 } 1768 1769 // Build the reginfos response. 1770 StreamGDBRemote response; 1771 1772 RegisterValue reg_value (reg_bytes, reg_size, process_arch.GetByteOrder ()); 1773 Error error = reg_context_sp->WriteRegister (reg_info, reg_value); 1774 if (error.Fail ()) 1775 { 1776 if (log) 1777 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, write of requested register %" PRIu32 " (%s) failed: %s", __FUNCTION__, reg_index, reg_info->name, error.AsCString ()); 1778 return SendErrorResponse (0x32); 1779 } 1780 1781 return SendOKResponse(); 1782 } 1783 1784 GDBRemoteCommunication::PacketResult 1785 GDBRemoteCommunicationServerLLGS::Handle_H (StringExtractorGDBRemote &packet) 1786 { 1787 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1788 1789 // Fail if we don't have a current process. 1790 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 1791 { 1792 if (log) 1793 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 1794 return SendErrorResponse (0x15); 1795 } 1796 1797 // Parse out which variant of $H is requested. 1798 packet.SetFilePos (strlen("H")); 1799 if (packet.GetBytesLeft () < 1) 1800 { 1801 if (log) 1802 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, H command missing {g,c} variant", __FUNCTION__); 1803 return SendIllFormedResponse (packet, "H command missing {g,c} variant"); 1804 } 1805 1806 const char h_variant = packet.GetChar (); 1807 switch (h_variant) 1808 { 1809 case 'g': 1810 break; 1811 1812 case 'c': 1813 break; 1814 1815 default: 1816 if (log) 1817 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, invalid $H variant %c", __FUNCTION__, h_variant); 1818 return SendIllFormedResponse (packet, "H variant unsupported, should be c or g"); 1819 } 1820 1821 // Parse out the thread number. 1822 // FIXME return a parse success/fail value. All values are valid here. 1823 const lldb::tid_t tid = packet.GetHexMaxU64 (false, std::numeric_limits<lldb::tid_t>::max ()); 1824 1825 // Ensure we have the given thread when not specifying -1 (all threads) or 0 (any thread). 1826 if (tid != LLDB_INVALID_THREAD_ID && tid != 0) 1827 { 1828 NativeThreadProtocolSP thread_sp (m_debugged_process_sp->GetThreadByID (tid)); 1829 if (!thread_sp) 1830 { 1831 if (log) 1832 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, tid %" PRIu64 " not found", __FUNCTION__, tid); 1833 return SendErrorResponse (0x15); 1834 } 1835 } 1836 1837 // Now switch the given thread type. 1838 switch (h_variant) 1839 { 1840 case 'g': 1841 SetCurrentThreadID (tid); 1842 break; 1843 1844 case 'c': 1845 SetContinueThreadID (tid); 1846 break; 1847 1848 default: 1849 assert (false && "unsupported $H variant - shouldn't get here"); 1850 return SendIllFormedResponse (packet, "H variant unsupported, should be c or g"); 1851 } 1852 1853 return SendOKResponse(); 1854 } 1855 1856 GDBRemoteCommunication::PacketResult 1857 GDBRemoteCommunicationServerLLGS::Handle_I (StringExtractorGDBRemote &packet) 1858 { 1859 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 1860 1861 // Fail if we don't have a current process. 1862 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 1863 { 1864 if (log) 1865 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 1866 return SendErrorResponse (0x15); 1867 } 1868 1869 packet.SetFilePos (::strlen("I")); 1870 char tmp[4096]; 1871 for (;;) 1872 { 1873 size_t read = packet.GetHexBytesAvail(tmp, sizeof(tmp)); 1874 if (read == 0) 1875 { 1876 break; 1877 } 1878 // write directly to stdin *this might block if stdin buffer is full* 1879 // TODO: enqueue this block in circular buffer and send window size to remote host 1880 ConnectionStatus status; 1881 Error error; 1882 m_stdio_communication.Write(tmp, read, status, &error); 1883 if (error.Fail()) 1884 { 1885 return SendErrorResponse (0x15); 1886 } 1887 } 1888 1889 return SendOKResponse(); 1890 } 1891 1892 GDBRemoteCommunication::PacketResult 1893 GDBRemoteCommunicationServerLLGS::Handle_interrupt (StringExtractorGDBRemote &packet) 1894 { 1895 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 1896 1897 // Fail if we don't have a current process. 1898 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 1899 { 1900 if (log) 1901 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 1902 return SendErrorResponse (0x15); 1903 } 1904 1905 // Interrupt the process. 1906 Error error = m_debugged_process_sp->Interrupt (); 1907 if (error.Fail ()) 1908 { 1909 if (log) 1910 { 1911 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed for process %" PRIu64 ": %s", 1912 __FUNCTION__, 1913 m_debugged_process_sp->GetID (), 1914 error.AsCString ()); 1915 } 1916 return SendErrorResponse (GDBRemoteServerError::eErrorResume); 1917 } 1918 1919 if (log) 1920 log->Printf ("GDBRemoteCommunicationServerLLGS::%s stopped process %" PRIu64, __FUNCTION__, m_debugged_process_sp->GetID ()); 1921 1922 // No response required from stop all. 1923 return PacketResult::Success; 1924 } 1925 1926 GDBRemoteCommunication::PacketResult 1927 GDBRemoteCommunicationServerLLGS::Handle_m (StringExtractorGDBRemote &packet) 1928 { 1929 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1930 1931 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 1932 { 1933 if (log) 1934 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 1935 return SendErrorResponse (0x15); 1936 } 1937 1938 // Parse out the memory address. 1939 packet.SetFilePos (strlen("m")); 1940 if (packet.GetBytesLeft() < 1) 1941 return SendIllFormedResponse(packet, "Too short m packet"); 1942 1943 // Read the address. Punting on validation. 1944 // FIXME replace with Hex U64 read with no default value that fails on failed read. 1945 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0); 1946 1947 // Validate comma. 1948 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ',')) 1949 return SendIllFormedResponse(packet, "Comma sep missing in m packet"); 1950 1951 // Get # bytes to read. 1952 if (packet.GetBytesLeft() < 1) 1953 return SendIllFormedResponse(packet, "Length missing in m packet"); 1954 1955 const uint64_t byte_count = packet.GetHexMaxU64(false, 0); 1956 if (byte_count == 0) 1957 { 1958 if (log) 1959 log->Printf ("GDBRemoteCommunicationServerLLGS::%s nothing to read: zero-length packet", __FUNCTION__); 1960 return PacketResult::Success; 1961 } 1962 1963 // Allocate the response buffer. 1964 std::string buf(byte_count, '\0'); 1965 if (buf.empty()) 1966 return SendErrorResponse (0x78); 1967 1968 1969 // Retrieve the process memory. 1970 size_t bytes_read = 0; 1971 Error error = m_debugged_process_sp->ReadMemoryWithoutTrap(read_addr, &buf[0], byte_count, bytes_read); 1972 if (error.Fail ()) 1973 { 1974 if (log) 1975 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " mem 0x%" PRIx64 ": failed to read. Error: %s", __FUNCTION__, m_debugged_process_sp->GetID (), read_addr, error.AsCString ()); 1976 return SendErrorResponse (0x08); 1977 } 1978 1979 if (bytes_read == 0) 1980 { 1981 if (log) 1982 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " mem 0x%" PRIx64 ": read 0 of %" PRIu64 " requested bytes", __FUNCTION__, m_debugged_process_sp->GetID (), read_addr, byte_count); 1983 return SendErrorResponse (0x08); 1984 } 1985 1986 StreamGDBRemote response; 1987 for (size_t i = 0; i < bytes_read; ++i) 1988 response.PutHex8(buf[i]); 1989 1990 return SendPacketNoLock(response.GetData(), response.GetSize()); 1991 } 1992 1993 GDBRemoteCommunication::PacketResult 1994 GDBRemoteCommunicationServerLLGS::Handle_M (StringExtractorGDBRemote &packet) 1995 { 1996 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1997 1998 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 1999 { 2000 if (log) 2001 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2002 return SendErrorResponse (0x15); 2003 } 2004 2005 // Parse out the memory address. 2006 packet.SetFilePos (strlen("M")); 2007 if (packet.GetBytesLeft() < 1) 2008 return SendIllFormedResponse(packet, "Too short M packet"); 2009 2010 // Read the address. Punting on validation. 2011 // FIXME replace with Hex U64 read with no default value that fails on failed read. 2012 const lldb::addr_t write_addr = packet.GetHexMaxU64(false, 0); 2013 2014 // Validate comma. 2015 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ',')) 2016 return SendIllFormedResponse(packet, "Comma sep missing in M packet"); 2017 2018 // Get # bytes to read. 2019 if (packet.GetBytesLeft() < 1) 2020 return SendIllFormedResponse(packet, "Length missing in M packet"); 2021 2022 const uint64_t byte_count = packet.GetHexMaxU64(false, 0); 2023 if (byte_count == 0) 2024 { 2025 if (log) 2026 log->Printf ("GDBRemoteCommunicationServerLLGS::%s nothing to write: zero-length packet", __FUNCTION__); 2027 return PacketResult::Success; 2028 } 2029 2030 // Validate colon. 2031 if ((packet.GetBytesLeft() < 1) || (packet.GetChar() != ':')) 2032 return SendIllFormedResponse(packet, "Comma sep missing in M packet after byte length"); 2033 2034 // Allocate the conversion buffer. 2035 std::vector<uint8_t> buf(byte_count, 0); 2036 if (buf.empty()) 2037 return SendErrorResponse (0x78); 2038 2039 // Convert the hex memory write contents to bytes. 2040 StreamGDBRemote response; 2041 const uint64_t convert_count = packet.GetHexBytes(&buf[0], byte_count, 0); 2042 if (convert_count != byte_count) 2043 { 2044 if (log) 2045 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " mem 0x%" PRIx64 ": asked to write %" PRIu64 " bytes, but only found %" PRIu64 " to convert.", __FUNCTION__, m_debugged_process_sp->GetID (), write_addr, byte_count, convert_count); 2046 return SendIllFormedResponse (packet, "M content byte length specified did not match hex-encoded content length"); 2047 } 2048 2049 // Write the process memory. 2050 size_t bytes_written = 0; 2051 Error error = m_debugged_process_sp->WriteMemory (write_addr, &buf[0], byte_count, bytes_written); 2052 if (error.Fail ()) 2053 { 2054 if (log) 2055 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " mem 0x%" PRIx64 ": failed to write. Error: %s", __FUNCTION__, m_debugged_process_sp->GetID (), write_addr, error.AsCString ()); 2056 return SendErrorResponse (0x09); 2057 } 2058 2059 if (bytes_written == 0) 2060 { 2061 if (log) 2062 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " mem 0x%" PRIx64 ": wrote 0 of %" PRIu64 " requested bytes", __FUNCTION__, m_debugged_process_sp->GetID (), write_addr, byte_count); 2063 return SendErrorResponse (0x09); 2064 } 2065 2066 return SendOKResponse (); 2067 } 2068 2069 GDBRemoteCommunication::PacketResult 2070 GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfoSupported (StringExtractorGDBRemote &packet) 2071 { 2072 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2073 2074 // Currently only the NativeProcessProtocol knows if it can handle a qMemoryRegionInfoSupported 2075 // request, but we're not guaranteed to be attached to a process. For now we'll assume the 2076 // client only asks this when a process is being debugged. 2077 2078 // Ensure we have a process running; otherwise, we can't figure this out 2079 // since we won't have a NativeProcessProtocol. 2080 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2081 { 2082 if (log) 2083 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2084 return SendErrorResponse (0x15); 2085 } 2086 2087 // Test if we can get any region back when asking for the region around NULL. 2088 MemoryRegionInfo region_info; 2089 const Error error = m_debugged_process_sp->GetMemoryRegionInfo (0, region_info); 2090 if (error.Fail ()) 2091 { 2092 // We don't support memory region info collection for this NativeProcessProtocol. 2093 return SendUnimplementedResponse (""); 2094 } 2095 2096 return SendOKResponse(); 2097 } 2098 2099 GDBRemoteCommunication::PacketResult 2100 GDBRemoteCommunicationServerLLGS::Handle_qMemoryRegionInfo (StringExtractorGDBRemote &packet) 2101 { 2102 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2103 2104 // Ensure we have a process. 2105 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2106 { 2107 if (log) 2108 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2109 return SendErrorResponse (0x15); 2110 } 2111 2112 // Parse out the memory address. 2113 packet.SetFilePos (strlen("qMemoryRegionInfo:")); 2114 if (packet.GetBytesLeft() < 1) 2115 return SendIllFormedResponse(packet, "Too short qMemoryRegionInfo: packet"); 2116 2117 // Read the address. Punting on validation. 2118 const lldb::addr_t read_addr = packet.GetHexMaxU64(false, 0); 2119 2120 StreamGDBRemote response; 2121 2122 // Get the memory region info for the target address. 2123 MemoryRegionInfo region_info; 2124 const Error error = m_debugged_process_sp->GetMemoryRegionInfo (read_addr, region_info); 2125 if (error.Fail ()) 2126 { 2127 // Return the error message. 2128 2129 response.PutCString ("error:"); 2130 response.PutCStringAsRawHex8 (error.AsCString ()); 2131 response.PutChar (';'); 2132 } 2133 else 2134 { 2135 // Range start and size. 2136 response.Printf ("start:%" PRIx64 ";size:%" PRIx64 ";", region_info.GetRange ().GetRangeBase (), region_info.GetRange ().GetByteSize ()); 2137 2138 // Permissions. 2139 if (region_info.GetReadable () || 2140 region_info.GetWritable () || 2141 region_info.GetExecutable ()) 2142 { 2143 // Write permissions info. 2144 response.PutCString ("permissions:"); 2145 2146 if (region_info.GetReadable ()) 2147 response.PutChar ('r'); 2148 if (region_info.GetWritable ()) 2149 response.PutChar('w'); 2150 if (region_info.GetExecutable()) 2151 response.PutChar ('x'); 2152 2153 response.PutChar (';'); 2154 } 2155 } 2156 2157 return SendPacketNoLock(response.GetData(), response.GetSize()); 2158 } 2159 2160 GDBRemoteCommunication::PacketResult 2161 GDBRemoteCommunicationServerLLGS::Handle_Z (StringExtractorGDBRemote &packet) 2162 { 2163 // Ensure we have a process. 2164 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2165 { 2166 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2167 if (log) 2168 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2169 return SendErrorResponse (0x15); 2170 } 2171 2172 // Parse out software or hardware breakpoint or watchpoint requested. 2173 packet.SetFilePos (strlen("Z")); 2174 if (packet.GetBytesLeft() < 1) 2175 return SendIllFormedResponse(packet, "Too short Z packet, missing software/hardware specifier"); 2176 2177 bool want_breakpoint = true; 2178 bool want_hardware = false; 2179 2180 const GDBStoppointType stoppoint_type = 2181 GDBStoppointType(packet.GetS32 (eStoppointInvalid)); 2182 switch (stoppoint_type) 2183 { 2184 case eBreakpointSoftware: 2185 want_hardware = false; want_breakpoint = true; break; 2186 case eBreakpointHardware: 2187 want_hardware = true; want_breakpoint = true; break; 2188 case eWatchpointWrite: 2189 want_hardware = true; want_breakpoint = false; break; 2190 case eWatchpointRead: 2191 want_hardware = true; want_breakpoint = false; break; 2192 case eWatchpointReadWrite: 2193 want_hardware = true; want_breakpoint = false; break; 2194 case eStoppointInvalid: 2195 return SendIllFormedResponse(packet, "Z packet had invalid software/hardware specifier"); 2196 2197 } 2198 2199 if ((packet.GetBytesLeft() < 1) || packet.GetChar () != ',') 2200 return SendIllFormedResponse(packet, "Malformed Z packet, expecting comma after stoppoint type"); 2201 2202 // Parse out the stoppoint address. 2203 if (packet.GetBytesLeft() < 1) 2204 return SendIllFormedResponse(packet, "Too short Z packet, missing address"); 2205 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0); 2206 2207 if ((packet.GetBytesLeft() < 1) || packet.GetChar () != ',') 2208 return SendIllFormedResponse(packet, "Malformed Z packet, expecting comma after address"); 2209 2210 // Parse out the stoppoint size (i.e. size hint for opcode size). 2211 const uint32_t size = packet.GetHexMaxU32 (false, std::numeric_limits<uint32_t>::max ()); 2212 if (size == std::numeric_limits<uint32_t>::max ()) 2213 return SendIllFormedResponse(packet, "Malformed Z packet, failed to parse size argument"); 2214 2215 if (want_breakpoint) 2216 { 2217 // Try to set the breakpoint. 2218 const Error error = m_debugged_process_sp->SetBreakpoint (addr, size, want_hardware); 2219 if (error.Success ()) 2220 return SendOKResponse (); 2221 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 2222 if (log) 2223 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 2224 " failed to set breakpoint: %s", 2225 __FUNCTION__, 2226 m_debugged_process_sp->GetID (), 2227 error.AsCString ()); 2228 return SendErrorResponse (0x09); 2229 } 2230 else 2231 { 2232 uint32_t watch_flags = 2233 stoppoint_type == eWatchpointWrite 2234 ? 0x1 // Write 2235 : 0x3; // ReadWrite 2236 2237 // Try to set the watchpoint. 2238 const Error error = m_debugged_process_sp->SetWatchpoint ( 2239 addr, size, watch_flags, want_hardware); 2240 if (error.Success ()) 2241 return SendOKResponse (); 2242 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 2243 if (log) 2244 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 2245 " failed to set watchpoint: %s", 2246 __FUNCTION__, 2247 m_debugged_process_sp->GetID (), 2248 error.AsCString ()); 2249 return SendErrorResponse (0x09); 2250 } 2251 } 2252 2253 GDBRemoteCommunication::PacketResult 2254 GDBRemoteCommunicationServerLLGS::Handle_z (StringExtractorGDBRemote &packet) 2255 { 2256 // Ensure we have a process. 2257 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2258 { 2259 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2260 if (log) 2261 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2262 return SendErrorResponse (0x15); 2263 } 2264 2265 // Parse out software or hardware breakpoint or watchpoint requested. 2266 packet.SetFilePos (strlen("z")); 2267 if (packet.GetBytesLeft() < 1) 2268 return SendIllFormedResponse(packet, "Too short z packet, missing software/hardware specifier"); 2269 2270 bool want_breakpoint = true; 2271 2272 const GDBStoppointType stoppoint_type = 2273 GDBStoppointType(packet.GetS32 (eStoppointInvalid)); 2274 switch (stoppoint_type) 2275 { 2276 case eBreakpointHardware: want_breakpoint = true; break; 2277 case eBreakpointSoftware: want_breakpoint = true; break; 2278 case eWatchpointWrite: want_breakpoint = false; break; 2279 case eWatchpointRead: want_breakpoint = false; break; 2280 case eWatchpointReadWrite: want_breakpoint = false; break; 2281 default: 2282 return SendIllFormedResponse(packet, "z packet had invalid software/hardware specifier"); 2283 2284 } 2285 2286 if ((packet.GetBytesLeft() < 1) || packet.GetChar () != ',') 2287 return SendIllFormedResponse(packet, "Malformed z packet, expecting comma after stoppoint type"); 2288 2289 // Parse out the stoppoint address. 2290 if (packet.GetBytesLeft() < 1) 2291 return SendIllFormedResponse(packet, "Too short z packet, missing address"); 2292 const lldb::addr_t addr = packet.GetHexMaxU64(false, 0); 2293 2294 if ((packet.GetBytesLeft() < 1) || packet.GetChar () != ',') 2295 return SendIllFormedResponse(packet, "Malformed z packet, expecting comma after address"); 2296 2297 /* 2298 // Parse out the stoppoint size (i.e. size hint for opcode size). 2299 const uint32_t size = packet.GetHexMaxU32 (false, std::numeric_limits<uint32_t>::max ()); 2300 if (size == std::numeric_limits<uint32_t>::max ()) 2301 return SendIllFormedResponse(packet, "Malformed z packet, failed to parse size argument"); 2302 */ 2303 2304 if (want_breakpoint) 2305 { 2306 // Try to clear the breakpoint. 2307 const Error error = m_debugged_process_sp->RemoveBreakpoint (addr); 2308 if (error.Success ()) 2309 return SendOKResponse (); 2310 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_BREAKPOINTS)); 2311 if (log) 2312 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 2313 " failed to remove breakpoint: %s", 2314 __FUNCTION__, 2315 m_debugged_process_sp->GetID (), 2316 error.AsCString ()); 2317 return SendErrorResponse (0x09); 2318 } 2319 else 2320 { 2321 // Try to clear the watchpoint. 2322 const Error error = m_debugged_process_sp->RemoveWatchpoint (addr); 2323 if (error.Success ()) 2324 return SendOKResponse (); 2325 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_WATCHPOINTS)); 2326 if (log) 2327 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 2328 " failed to remove watchpoint: %s", 2329 __FUNCTION__, 2330 m_debugged_process_sp->GetID (), 2331 error.AsCString ()); 2332 return SendErrorResponse (0x09); 2333 } 2334 } 2335 2336 GDBRemoteCommunication::PacketResult 2337 GDBRemoteCommunicationServerLLGS::Handle_s (StringExtractorGDBRemote &packet) 2338 { 2339 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS|LIBLLDB_LOG_THREAD)); 2340 2341 // Ensure we have a process. 2342 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2343 { 2344 if (log) 2345 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2346 return SendErrorResponse (0x32); 2347 } 2348 2349 // We first try to use a continue thread id. If any one or any all set, use the current thread. 2350 // Bail out if we don't have a thread id. 2351 lldb::tid_t tid = GetContinueThreadID (); 2352 if (tid == 0 || tid == LLDB_INVALID_THREAD_ID) 2353 tid = GetCurrentThreadID (); 2354 if (tid == LLDB_INVALID_THREAD_ID) 2355 return SendErrorResponse (0x33); 2356 2357 // Double check that we have such a thread. 2358 // TODO investigate: on MacOSX we might need to do an UpdateThreads () here. 2359 NativeThreadProtocolSP thread_sp = m_debugged_process_sp->GetThreadByID (tid); 2360 if (!thread_sp || thread_sp->GetID () != tid) 2361 return SendErrorResponse (0x33); 2362 2363 // Create the step action for the given thread. 2364 ResumeAction action = { tid, eStateStepping, 0 }; 2365 2366 // Setup the actions list. 2367 ResumeActionList actions; 2368 actions.Append (action); 2369 2370 // All other threads stop while we're single stepping a thread. 2371 actions.SetDefaultThreadActionIfNeeded(eStateStopped, 0); 2372 Error error = m_debugged_process_sp->Resume (actions); 2373 if (error.Fail ()) 2374 { 2375 if (log) 2376 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " Resume() failed with error: %s", __FUNCTION__, m_debugged_process_sp->GetID (), tid, error.AsCString ()); 2377 return SendErrorResponse(0x49); 2378 } 2379 2380 // No response here - the stop or exit will come from the resulting action. 2381 return PacketResult::Success; 2382 } 2383 2384 GDBRemoteCommunication::PacketResult 2385 GDBRemoteCommunicationServerLLGS::Handle_qXfer_auxv_read (StringExtractorGDBRemote &packet) 2386 { 2387 // *BSD impls should be able to do this too. 2388 #if defined(__linux__) 2389 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2390 2391 // Parse out the offset. 2392 packet.SetFilePos (strlen("qXfer:auxv:read::")); 2393 if (packet.GetBytesLeft () < 1) 2394 return SendIllFormedResponse (packet, "qXfer:auxv:read:: packet missing offset"); 2395 2396 const uint64_t auxv_offset = packet.GetHexMaxU64 (false, std::numeric_limits<uint64_t>::max ()); 2397 if (auxv_offset == std::numeric_limits<uint64_t>::max ()) 2398 return SendIllFormedResponse (packet, "qXfer:auxv:read:: packet missing offset"); 2399 2400 // Parse out comma. 2401 if (packet.GetBytesLeft () < 1 || packet.GetChar () != ',') 2402 return SendIllFormedResponse (packet, "qXfer:auxv:read:: packet missing comma after offset"); 2403 2404 // Parse out the length. 2405 const uint64_t auxv_length = packet.GetHexMaxU64 (false, std::numeric_limits<uint64_t>::max ()); 2406 if (auxv_length == std::numeric_limits<uint64_t>::max ()) 2407 return SendIllFormedResponse (packet, "qXfer:auxv:read:: packet missing length"); 2408 2409 // Grab the auxv data if we need it. 2410 if (!m_active_auxv_buffer_sp) 2411 { 2412 // Make sure we have a valid process. 2413 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2414 { 2415 if (log) 2416 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2417 return SendErrorResponse (0x10); 2418 } 2419 2420 // Grab the auxv data. 2421 m_active_auxv_buffer_sp = Host::GetAuxvData (m_debugged_process_sp->GetID ()); 2422 if (!m_active_auxv_buffer_sp || m_active_auxv_buffer_sp->GetByteSize () == 0) 2423 { 2424 // Hmm, no auxv data, call that an error. 2425 if (log) 2426 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no auxv data retrieved", __FUNCTION__); 2427 m_active_auxv_buffer_sp.reset (); 2428 return SendErrorResponse (0x11); 2429 } 2430 } 2431 2432 // FIXME find out if/how I lock the stream here. 2433 2434 StreamGDBRemote response; 2435 bool done_with_buffer = false; 2436 2437 if (auxv_offset >= m_active_auxv_buffer_sp->GetByteSize ()) 2438 { 2439 // We have nothing left to send. Mark the buffer as complete. 2440 response.PutChar ('l'); 2441 done_with_buffer = true; 2442 } 2443 else 2444 { 2445 // Figure out how many bytes are available starting at the given offset. 2446 const uint64_t bytes_remaining = m_active_auxv_buffer_sp->GetByteSize () - auxv_offset; 2447 2448 // Figure out how many bytes we're going to read. 2449 const uint64_t bytes_to_read = (auxv_length > bytes_remaining) ? bytes_remaining : auxv_length; 2450 2451 // Mark the response type according to whether we're reading the remainder of the auxv data. 2452 if (bytes_to_read >= bytes_remaining) 2453 { 2454 // There will be nothing left to read after this 2455 response.PutChar ('l'); 2456 done_with_buffer = true; 2457 } 2458 else 2459 { 2460 // There will still be bytes to read after this request. 2461 response.PutChar ('m'); 2462 } 2463 2464 // Now write the data in encoded binary form. 2465 response.PutEscapedBytes (m_active_auxv_buffer_sp->GetBytes () + auxv_offset, bytes_to_read); 2466 } 2467 2468 if (done_with_buffer) 2469 m_active_auxv_buffer_sp.reset (); 2470 2471 return SendPacketNoLock(response.GetData(), response.GetSize()); 2472 #else 2473 return SendUnimplementedResponse ("not implemented on this platform"); 2474 #endif 2475 } 2476 2477 GDBRemoteCommunication::PacketResult 2478 GDBRemoteCommunicationServerLLGS::Handle_QSaveRegisterState (StringExtractorGDBRemote &packet) 2479 { 2480 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2481 2482 // Move past packet name. 2483 packet.SetFilePos (strlen ("QSaveRegisterState")); 2484 2485 // Get the thread to use. 2486 NativeThreadProtocolSP thread_sp = GetThreadFromSuffix (packet); 2487 if (!thread_sp) 2488 { 2489 if (m_thread_suffix_supported) 2490 return SendIllFormedResponse (packet, "No thread specified in QSaveRegisterState packet"); 2491 else 2492 return SendIllFormedResponse (packet, "No thread was is set with the Hg packet"); 2493 } 2494 2495 // Grab the register context for the thread. 2496 NativeRegisterContextSP reg_context_sp (thread_sp->GetRegisterContext ()); 2497 if (!reg_context_sp) 2498 { 2499 if (log) 2500 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " failed, no register context available for the thread", __FUNCTION__, m_debugged_process_sp->GetID (), thread_sp->GetID ()); 2501 return SendErrorResponse (0x15); 2502 } 2503 2504 // Save registers to a buffer. 2505 DataBufferSP register_data_sp; 2506 Error error = reg_context_sp->ReadAllRegisterValues (register_data_sp); 2507 if (error.Fail ()) 2508 { 2509 if (log) 2510 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " failed to save all register values: %s", __FUNCTION__, m_debugged_process_sp->GetID (), error.AsCString ()); 2511 return SendErrorResponse (0x75); 2512 } 2513 2514 // Allocate a new save id. 2515 const uint32_t save_id = GetNextSavedRegistersID (); 2516 assert ((m_saved_registers_map.find (save_id) == m_saved_registers_map.end ()) && "GetNextRegisterSaveID() returned an existing register save id"); 2517 2518 // Save the register data buffer under the save id. 2519 { 2520 Mutex::Locker locker (m_saved_registers_mutex); 2521 m_saved_registers_map[save_id] = register_data_sp; 2522 } 2523 2524 // Write the response. 2525 StreamGDBRemote response; 2526 response.Printf ("%" PRIu32, save_id); 2527 return SendPacketNoLock(response.GetData(), response.GetSize()); 2528 } 2529 2530 GDBRemoteCommunication::PacketResult 2531 GDBRemoteCommunicationServerLLGS::Handle_QRestoreRegisterState (StringExtractorGDBRemote &packet) 2532 { 2533 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2534 2535 // Parse out save id. 2536 packet.SetFilePos (strlen ("QRestoreRegisterState:")); 2537 if (packet.GetBytesLeft () < 1) 2538 return SendIllFormedResponse (packet, "QRestoreRegisterState packet missing register save id"); 2539 2540 const uint32_t save_id = packet.GetU32 (0); 2541 if (save_id == 0) 2542 { 2543 if (log) 2544 log->Printf ("GDBRemoteCommunicationServerLLGS::%s QRestoreRegisterState packet has malformed save id, expecting decimal uint32_t", __FUNCTION__); 2545 return SendErrorResponse (0x76); 2546 } 2547 2548 // Get the thread to use. 2549 NativeThreadProtocolSP thread_sp = GetThreadFromSuffix (packet); 2550 if (!thread_sp) 2551 { 2552 if (m_thread_suffix_supported) 2553 return SendIllFormedResponse (packet, "No thread specified in QRestoreRegisterState packet"); 2554 else 2555 return SendIllFormedResponse (packet, "No thread was is set with the Hg packet"); 2556 } 2557 2558 // Grab the register context for the thread. 2559 NativeRegisterContextSP reg_context_sp (thread_sp->GetRegisterContext ()); 2560 if (!reg_context_sp) 2561 { 2562 if (log) 2563 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " failed, no register context available for the thread", __FUNCTION__, m_debugged_process_sp->GetID (), thread_sp->GetID ()); 2564 return SendErrorResponse (0x15); 2565 } 2566 2567 // Retrieve register state buffer, then remove from the list. 2568 DataBufferSP register_data_sp; 2569 { 2570 Mutex::Locker locker (m_saved_registers_mutex); 2571 2572 // Find the register set buffer for the given save id. 2573 auto it = m_saved_registers_map.find (save_id); 2574 if (it == m_saved_registers_map.end ()) 2575 { 2576 if (log) 2577 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " does not have a register set save buffer for id %" PRIu32, __FUNCTION__, m_debugged_process_sp->GetID (), save_id); 2578 return SendErrorResponse (0x77); 2579 } 2580 register_data_sp = it->second; 2581 2582 // Remove it from the map. 2583 m_saved_registers_map.erase (it); 2584 } 2585 2586 Error error = reg_context_sp->WriteAllRegisterValues (register_data_sp); 2587 if (error.Fail ()) 2588 { 2589 if (log) 2590 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " failed to restore all register values: %s", __FUNCTION__, m_debugged_process_sp->GetID (), error.AsCString ()); 2591 return SendErrorResponse (0x77); 2592 } 2593 2594 return SendOKResponse(); 2595 } 2596 2597 GDBRemoteCommunication::PacketResult 2598 GDBRemoteCommunicationServerLLGS::Handle_vAttach (StringExtractorGDBRemote &packet) 2599 { 2600 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2601 2602 // Consume the ';' after vAttach. 2603 packet.SetFilePos (strlen ("vAttach")); 2604 if (!packet.GetBytesLeft () || packet.GetChar () != ';') 2605 return SendIllFormedResponse (packet, "vAttach missing expected ';'"); 2606 2607 // Grab the PID to which we will attach (assume hex encoding). 2608 lldb::pid_t pid = packet.GetU32 (LLDB_INVALID_PROCESS_ID, 16); 2609 if (pid == LLDB_INVALID_PROCESS_ID) 2610 return SendIllFormedResponse (packet, "vAttach failed to parse the process id"); 2611 2612 // Attempt to attach. 2613 if (log) 2614 log->Printf ("GDBRemoteCommunicationServerLLGS::%s attempting to attach to pid %" PRIu64, __FUNCTION__, pid); 2615 2616 Error error = AttachToProcess (pid); 2617 2618 if (error.Fail ()) 2619 { 2620 if (log) 2621 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to attach to pid %" PRIu64 ": %s\n", __FUNCTION__, pid, error.AsCString()); 2622 return SendErrorResponse (0x01); 2623 } 2624 2625 // Notify we attached by sending a stop packet. 2626 return SendStopReasonForState (m_debugged_process_sp->GetState (), true); 2627 } 2628 2629 GDBRemoteCommunication::PacketResult 2630 GDBRemoteCommunicationServerLLGS::Handle_D (StringExtractorGDBRemote &packet) 2631 { 2632 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS)); 2633 2634 // Scope for mutex locker. 2635 Mutex::Locker locker (m_spawned_pids_mutex); 2636 2637 // Fail if we don't have a current process. 2638 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2639 { 2640 if (log) 2641 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, no process available", __FUNCTION__); 2642 return SendErrorResponse (0x15); 2643 } 2644 2645 if (m_spawned_pids.find(m_debugged_process_sp->GetID ()) == m_spawned_pids.end()) 2646 { 2647 if (log) 2648 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to find PID %" PRIu64 " in spawned pids list", 2649 __FUNCTION__, m_debugged_process_sp->GetID ()); 2650 return SendErrorResponse (0x1); 2651 } 2652 2653 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 2654 2655 // Consume the ';' after D. 2656 packet.SetFilePos (1); 2657 if (packet.GetBytesLeft ()) 2658 { 2659 if (packet.GetChar () != ';') 2660 return SendIllFormedResponse (packet, "D missing expected ';'"); 2661 2662 // Grab the PID from which we will detach (assume hex encoding). 2663 pid = packet.GetU32 (LLDB_INVALID_PROCESS_ID, 16); 2664 if (pid == LLDB_INVALID_PROCESS_ID) 2665 return SendIllFormedResponse (packet, "D failed to parse the process id"); 2666 } 2667 2668 if (pid != LLDB_INVALID_PROCESS_ID && 2669 m_debugged_process_sp->GetID () != pid) 2670 { 2671 return SendIllFormedResponse (packet, "Invalid pid"); 2672 } 2673 2674 if (m_stdio_communication.IsConnected ()) 2675 { 2676 m_stdio_communication.StopReadThread (); 2677 } 2678 2679 const Error error = m_debugged_process_sp->Detach (); 2680 if (error.Fail ()) 2681 { 2682 if (log) 2683 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed to detach from pid %" PRIu64 ": %s\n", 2684 __FUNCTION__, m_debugged_process_sp->GetID (), error.AsCString ()); 2685 return SendErrorResponse (0x01); 2686 } 2687 2688 m_spawned_pids.erase (m_debugged_process_sp->GetID ()); 2689 return SendOKResponse (); 2690 } 2691 2692 GDBRemoteCommunication::PacketResult 2693 GDBRemoteCommunicationServerLLGS::Handle_qThreadStopInfo (StringExtractorGDBRemote &packet) 2694 { 2695 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2696 2697 packet.SetFilePos (strlen("qThreadStopInfo")); 2698 const lldb::tid_t tid = packet.GetHexMaxU32 (false, LLDB_INVALID_THREAD_ID); 2699 if (tid == LLDB_INVALID_THREAD_ID) 2700 { 2701 if (log) 2702 log->Printf ("GDBRemoteCommunicationServerLLGS::%s failed, could not parse thread id from request \"%s\"", __FUNCTION__, packet.GetStringRef ().c_str ()); 2703 return SendErrorResponse (0x15); 2704 } 2705 return SendStopReplyPacketForThread (tid); 2706 } 2707 2708 GDBRemoteCommunication::PacketResult 2709 GDBRemoteCommunicationServerLLGS::Handle_jThreadsInfo (StringExtractorGDBRemote &) 2710 { 2711 Log *log (GetLogIfAnyCategoriesSet (LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2712 2713 // Ensure we have a debugged process. 2714 if (!m_debugged_process_sp || (m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID)) 2715 return SendErrorResponse (50); 2716 2717 if (log) 2718 log->Printf ("GDBRemoteCommunicationServerLLGS::%s preparing packet for pid %" PRIu64, 2719 __FUNCTION__, m_debugged_process_sp->GetID()); 2720 2721 JSONArray threads_array; 2722 2723 // Ensure we can get info on the given thread. 2724 uint32_t thread_idx = 0; 2725 for ( NativeThreadProtocolSP thread_sp; 2726 (thread_sp = m_debugged_process_sp->GetThreadAtIndex(thread_idx)) != nullptr; 2727 ++thread_idx) 2728 { 2729 2730 JSONObject::SP thread_obj_sp = std::make_shared<JSONObject>(); 2731 2732 lldb::tid_t tid = thread_sp->GetID(); 2733 2734 // Grab the reason this thread stopped. 2735 struct ThreadStopInfo tid_stop_info; 2736 std::string description; 2737 if (!thread_sp->GetStopReason (tid_stop_info, description)) 2738 return SendErrorResponse (52); 2739 2740 const int signum = tid_stop_info.details.signal.signo; 2741 if (log) 2742 { 2743 log->Printf ("GDBRemoteCommunicationServerLLGS::%s pid %" PRIu64 " tid %" PRIu64 " got signal signo = %d, reason = %d, exc_type = %" PRIu64, 2744 __FUNCTION__, 2745 m_debugged_process_sp->GetID (), 2746 tid, 2747 signum, 2748 tid_stop_info.reason, 2749 tid_stop_info.details.exception.type); 2750 } 2751 2752 thread_obj_sp->SetObject("tid", std::make_shared<JSONNumber>(tid)); 2753 if (signum != LLDB_INVALID_SIGNAL_NUMBER) 2754 thread_obj_sp->SetObject("signal", std::make_shared<JSONNumber>(uint64_t(signum))); 2755 2756 const std::string thread_name = thread_sp->GetName (); 2757 if (! thread_name.empty()) 2758 thread_obj_sp->SetObject("name", std::make_shared<JSONString>(thread_name)); 2759 2760 if (JSONObject::SP registers_sp = GetRegistersAsJSON(*thread_sp)) 2761 thread_obj_sp->SetObject("registers", registers_sp); 2762 2763 if (const char *stop_reason_str = GetStopReasonString(tid_stop_info.reason)) 2764 thread_obj_sp->SetObject("reason", std::make_shared<JSONString>(stop_reason_str)); 2765 2766 if (! description.empty()) 2767 thread_obj_sp->SetObject("description", std::make_shared<JSONString>(description)); 2768 2769 if ((tid_stop_info.reason == eStopReasonException) && tid_stop_info.details.exception.type) 2770 { 2771 thread_obj_sp->SetObject("metype", 2772 std::make_shared<JSONNumber>(tid_stop_info.details.exception.type)); 2773 2774 JSONArray::SP medata_array_sp = std::make_shared<JSONArray>(); 2775 for (uint32_t i = 0; i < tid_stop_info.details.exception.data_count; ++i) 2776 { 2777 medata_array_sp->AppendObject(std::make_shared<JSONNumber>( 2778 tid_stop_info.details.exception.data[i])); 2779 } 2780 thread_obj_sp->SetObject("medata", medata_array_sp); 2781 } 2782 2783 threads_array.AppendObject(thread_obj_sp); 2784 } 2785 // TODO: Expedite interesting regions of inferior memory 2786 2787 StreamString response; 2788 threads_array.Write(response); 2789 StreamGDBRemote escaped_response; 2790 escaped_response.PutEscapedBytes(response.GetData(), response.GetSize()); 2791 return SendPacketNoLock (escaped_response.GetData(), escaped_response.GetSize()); 2792 } 2793 2794 GDBRemoteCommunication::PacketResult 2795 GDBRemoteCommunicationServerLLGS::Handle_qWatchpointSupportInfo (StringExtractorGDBRemote &packet) 2796 { 2797 // Fail if we don't have a current process. 2798 if (!m_debugged_process_sp || 2799 m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID) 2800 return SendErrorResponse (68); 2801 2802 packet.SetFilePos(strlen("qWatchpointSupportInfo")); 2803 if (packet.GetBytesLeft() == 0) 2804 return SendOKResponse(); 2805 if (packet.GetChar() != ':') 2806 return SendErrorResponse(67); 2807 2808 uint32_t num = m_debugged_process_sp->GetMaxWatchpoints(); 2809 StreamGDBRemote response; 2810 response.Printf ("num:%d;", num); 2811 return SendPacketNoLock(response.GetData(), response.GetSize()); 2812 } 2813 2814 GDBRemoteCommunication::PacketResult 2815 GDBRemoteCommunicationServerLLGS::Handle_qFileLoadAddress (StringExtractorGDBRemote &packet) 2816 { 2817 // Fail if we don't have a current process. 2818 if (!m_debugged_process_sp || 2819 m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID) 2820 return SendErrorResponse(67); 2821 2822 packet.SetFilePos(strlen("qFileLoadAddress:")); 2823 if (packet.GetBytesLeft() == 0) 2824 return SendErrorResponse(68); 2825 2826 std::string file_name; 2827 packet.GetHexByteString(file_name); 2828 2829 lldb::addr_t file_load_address = LLDB_INVALID_ADDRESS; 2830 Error error = m_debugged_process_sp->GetFileLoadAddress(file_name, file_load_address); 2831 if (error.Fail()) 2832 return SendErrorResponse(69); 2833 2834 if (file_load_address == LLDB_INVALID_ADDRESS) 2835 return SendErrorResponse(1); // File not loaded 2836 2837 StreamGDBRemote response; 2838 response.PutHex64(file_load_address); 2839 return SendPacketNoLock(response.GetData(), response.GetSize()); 2840 } 2841 2842 void 2843 GDBRemoteCommunicationServerLLGS::FlushInferiorOutput () 2844 { 2845 // If we're not monitoring an inferior's terminal, ignore this. 2846 if (!m_stdio_communication.IsConnected()) 2847 return; 2848 2849 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS | LIBLLDB_LOG_THREAD)); 2850 if (log) 2851 log->Printf ("GDBRemoteCommunicationServerLLGS::%s() called", __FUNCTION__); 2852 2853 // FIXME implement a timeout on the join. 2854 m_stdio_communication.JoinReadThread(); 2855 } 2856 2857 void 2858 GDBRemoteCommunicationServerLLGS::MaybeCloseInferiorTerminalConnection () 2859 { 2860 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 2861 2862 // Tell the stdio connection to shut down. 2863 if (m_stdio_communication.IsConnected()) 2864 { 2865 auto connection = m_stdio_communication.GetConnection(); 2866 if (connection) 2867 { 2868 Error error; 2869 connection->Disconnect (&error); 2870 2871 if (error.Success ()) 2872 { 2873 if (log) 2874 log->Printf ("GDBRemoteCommunicationServerLLGS::%s disconnect process terminal stdio - SUCCESS", __FUNCTION__); 2875 } 2876 else 2877 { 2878 if (log) 2879 log->Printf ("GDBRemoteCommunicationServerLLGS::%s disconnect process terminal stdio - FAIL: %s", __FUNCTION__, error.AsCString ()); 2880 } 2881 } 2882 } 2883 } 2884 2885 2886 NativeThreadProtocolSP 2887 GDBRemoteCommunicationServerLLGS::GetThreadFromSuffix (StringExtractorGDBRemote &packet) 2888 { 2889 NativeThreadProtocolSP thread_sp; 2890 2891 // We have no thread if we don't have a process. 2892 if (!m_debugged_process_sp || m_debugged_process_sp->GetID () == LLDB_INVALID_PROCESS_ID) 2893 return thread_sp; 2894 2895 // If the client hasn't asked for thread suffix support, there will not be a thread suffix. 2896 // Use the current thread in that case. 2897 if (!m_thread_suffix_supported) 2898 { 2899 const lldb::tid_t current_tid = GetCurrentThreadID (); 2900 if (current_tid == LLDB_INVALID_THREAD_ID) 2901 return thread_sp; 2902 else if (current_tid == 0) 2903 { 2904 // Pick a thread. 2905 return m_debugged_process_sp->GetThreadAtIndex (0); 2906 } 2907 else 2908 return m_debugged_process_sp->GetThreadByID (current_tid); 2909 } 2910 2911 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_THREAD)); 2912 2913 // Parse out the ';'. 2914 if (packet.GetBytesLeft () < 1 || packet.GetChar () != ';') 2915 { 2916 if (log) 2917 log->Printf ("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse error: expected ';' prior to start of thread suffix: packet contents = '%s'", __FUNCTION__, packet.GetStringRef ().c_str ()); 2918 return thread_sp; 2919 } 2920 2921 if (!packet.GetBytesLeft ()) 2922 return thread_sp; 2923 2924 // Parse out thread: portion. 2925 if (strncmp (packet.Peek (), "thread:", strlen("thread:")) != 0) 2926 { 2927 if (log) 2928 log->Printf ("GDBRemoteCommunicationServerLLGS::%s gdb-remote parse error: expected 'thread:' but not found, packet contents = '%s'", __FUNCTION__, packet.GetStringRef ().c_str ()); 2929 return thread_sp; 2930 } 2931 packet.SetFilePos (packet.GetFilePos () + strlen("thread:")); 2932 const lldb::tid_t tid = packet.GetHexMaxU64(false, 0); 2933 if (tid != 0) 2934 return m_debugged_process_sp->GetThreadByID (tid); 2935 2936 return thread_sp; 2937 } 2938 2939 lldb::tid_t 2940 GDBRemoteCommunicationServerLLGS::GetCurrentThreadID () const 2941 { 2942 if (m_current_tid == 0 || m_current_tid == LLDB_INVALID_THREAD_ID) 2943 { 2944 // Use whatever the debug process says is the current thread id 2945 // since the protocol either didn't specify or specified we want 2946 // any/all threads marked as the current thread. 2947 if (!m_debugged_process_sp) 2948 return LLDB_INVALID_THREAD_ID; 2949 return m_debugged_process_sp->GetCurrentThreadID (); 2950 } 2951 // Use the specific current thread id set by the gdb remote protocol. 2952 return m_current_tid; 2953 } 2954 2955 uint32_t 2956 GDBRemoteCommunicationServerLLGS::GetNextSavedRegistersID () 2957 { 2958 Mutex::Locker locker (m_saved_registers_mutex); 2959 return m_next_saved_registers_id++; 2960 } 2961 2962 void 2963 GDBRemoteCommunicationServerLLGS::ClearProcessSpecificData () 2964 { 2965 Log *log (GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS|GDBR_LOG_PROCESS)); 2966 if (log) 2967 log->Printf ("GDBRemoteCommunicationServerLLGS::%s()", __FUNCTION__); 2968 2969 // Clear any auxv cached data. 2970 // *BSD impls should be able to do this too. 2971 #if defined(__linux__) 2972 if (log) 2973 log->Printf ("GDBRemoteCommunicationServerLLGS::%s clearing auxv buffer (previously %s)", 2974 __FUNCTION__, 2975 m_active_auxv_buffer_sp ? "was set" : "was not set"); 2976 m_active_auxv_buffer_sp.reset (); 2977 #endif 2978 } 2979 2980 FileSpec 2981 GDBRemoteCommunicationServerLLGS::FindModuleFile(const std::string& module_path, 2982 const ArchSpec& arch) 2983 { 2984 if (m_debugged_process_sp) 2985 { 2986 FileSpec file_spec; 2987 if (m_debugged_process_sp->GetLoadedModuleFileSpec(module_path.c_str(), file_spec).Success()) 2988 { 2989 if (file_spec.Exists()) 2990 return file_spec; 2991 } 2992 } 2993 2994 return GDBRemoteCommunicationServerCommon::FindModuleFile(module_path, arch); 2995 } 2996