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