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