1 //===-- GDBRemoteCommunicationClient.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 11 #include "GDBRemoteCommunicationClient.h" 12 13 // C Includes 14 // C++ Includes 15 #include <sstream> 16 17 // Other libraries and framework includes 18 #include "llvm/ADT/Triple.h" 19 #include "lldb/Interpreter/Args.h" 20 #include "lldb/Core/ConnectionFileDescriptor.h" 21 #include "lldb/Core/Log.h" 22 #include "lldb/Core/State.h" 23 #include "lldb/Core/StreamString.h" 24 #include "lldb/Host/Endian.h" 25 #include "lldb/Host/Host.h" 26 #include "lldb/Host/TimeValue.h" 27 28 // Project includes 29 #include "Utility/StringExtractorGDBRemote.h" 30 #include "ProcessGDBRemote.h" 31 #include "ProcessGDBRemoteLog.h" 32 33 using namespace lldb; 34 using namespace lldb_private; 35 36 //---------------------------------------------------------------------- 37 // GDBRemoteCommunicationClient constructor 38 //---------------------------------------------------------------------- 39 GDBRemoteCommunicationClient::GDBRemoteCommunicationClient(bool is_platform) : 40 GDBRemoteCommunication("gdb-remote.client", "gdb-remote.client.rx_packet", is_platform), 41 m_supports_not_sending_acks (eLazyBoolCalculate), 42 m_supports_thread_suffix (eLazyBoolCalculate), 43 m_supports_threads_in_stop_reply (eLazyBoolCalculate), 44 m_supports_vCont_all (eLazyBoolCalculate), 45 m_supports_vCont_any (eLazyBoolCalculate), 46 m_supports_vCont_c (eLazyBoolCalculate), 47 m_supports_vCont_C (eLazyBoolCalculate), 48 m_supports_vCont_s (eLazyBoolCalculate), 49 m_supports_vCont_S (eLazyBoolCalculate), 50 m_qHostInfo_is_valid (eLazyBoolCalculate), 51 m_qProcessInfo_is_valid (eLazyBoolCalculate), 52 m_supports_alloc_dealloc_memory (eLazyBoolCalculate), 53 m_supports_memory_region_info (eLazyBoolCalculate), 54 m_supports_watchpoint_support_info (eLazyBoolCalculate), 55 m_watchpoints_trigger_after_instruction(eLazyBoolCalculate), 56 m_attach_or_wait_reply(eLazyBoolCalculate), 57 m_prepare_for_reg_writing_reply (eLazyBoolCalculate), 58 m_supports_qProcessInfoPID (true), 59 m_supports_qfProcessInfo (true), 60 m_supports_qUserName (true), 61 m_supports_qGroupName (true), 62 m_supports_qThreadStopInfo (true), 63 m_supports_z0 (true), 64 m_supports_z1 (true), 65 m_supports_z2 (true), 66 m_supports_z3 (true), 67 m_supports_z4 (true), 68 m_curr_tid (LLDB_INVALID_THREAD_ID), 69 m_curr_tid_run (LLDB_INVALID_THREAD_ID), 70 m_num_supported_hardware_watchpoints (0), 71 m_async_mutex (Mutex::eMutexTypeRecursive), 72 m_async_packet_predicate (false), 73 m_async_packet (), 74 m_async_response (), 75 m_async_signal (-1), 76 m_thread_id_to_used_usec_map (), 77 m_host_arch(), 78 m_process_arch(), 79 m_os_version_major (UINT32_MAX), 80 m_os_version_minor (UINT32_MAX), 81 m_os_version_update (UINT32_MAX) 82 { 83 } 84 85 //---------------------------------------------------------------------- 86 // Destructor 87 //---------------------------------------------------------------------- 88 GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient() 89 { 90 if (IsConnected()) 91 Disconnect(); 92 } 93 94 bool 95 GDBRemoteCommunicationClient::HandshakeWithServer (Error *error_ptr) 96 { 97 // Start the read thread after we send the handshake ack since if we 98 // fail to send the handshake ack, there is no reason to continue... 99 if (SendAck()) 100 return true; 101 102 if (error_ptr) 103 error_ptr->SetErrorString("failed to send the handshake ack"); 104 return false; 105 } 106 107 void 108 GDBRemoteCommunicationClient::QueryNoAckModeSupported () 109 { 110 if (m_supports_not_sending_acks == eLazyBoolCalculate) 111 { 112 m_send_acks = true; 113 m_supports_not_sending_acks = eLazyBoolNo; 114 115 StringExtractorGDBRemote response; 116 if (SendPacketAndWaitForResponse("QStartNoAckMode", response, false)) 117 { 118 if (response.IsOKResponse()) 119 { 120 m_send_acks = false; 121 m_supports_not_sending_acks = eLazyBoolYes; 122 } 123 } 124 } 125 } 126 127 void 128 GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported () 129 { 130 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate) 131 { 132 m_supports_threads_in_stop_reply = eLazyBoolNo; 133 134 StringExtractorGDBRemote response; 135 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response, false)) 136 { 137 if (response.IsOKResponse()) 138 m_supports_threads_in_stop_reply = eLazyBoolYes; 139 } 140 } 141 } 142 143 bool 144 GDBRemoteCommunicationClient::GetVAttachOrWaitSupported () 145 { 146 if (m_attach_or_wait_reply == eLazyBoolCalculate) 147 { 148 m_attach_or_wait_reply = eLazyBoolNo; 149 150 StringExtractorGDBRemote response; 151 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response, false)) 152 { 153 if (response.IsOKResponse()) 154 m_attach_or_wait_reply = eLazyBoolYes; 155 } 156 } 157 if (m_attach_or_wait_reply == eLazyBoolYes) 158 return true; 159 else 160 return false; 161 } 162 163 bool 164 GDBRemoteCommunicationClient::GetSyncThreadStateSupported () 165 { 166 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate) 167 { 168 m_prepare_for_reg_writing_reply = eLazyBoolNo; 169 170 StringExtractorGDBRemote response; 171 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response, false)) 172 { 173 if (response.IsOKResponse()) 174 m_prepare_for_reg_writing_reply = eLazyBoolYes; 175 } 176 } 177 if (m_prepare_for_reg_writing_reply == eLazyBoolYes) 178 return true; 179 else 180 return false; 181 } 182 183 184 void 185 GDBRemoteCommunicationClient::ResetDiscoverableSettings() 186 { 187 m_supports_not_sending_acks = eLazyBoolCalculate; 188 m_supports_thread_suffix = eLazyBoolCalculate; 189 m_supports_threads_in_stop_reply = eLazyBoolCalculate; 190 m_supports_vCont_c = eLazyBoolCalculate; 191 m_supports_vCont_C = eLazyBoolCalculate; 192 m_supports_vCont_s = eLazyBoolCalculate; 193 m_supports_vCont_S = eLazyBoolCalculate; 194 m_qHostInfo_is_valid = eLazyBoolCalculate; 195 m_qProcessInfo_is_valid = eLazyBoolCalculate; 196 m_supports_alloc_dealloc_memory = eLazyBoolCalculate; 197 m_supports_memory_region_info = eLazyBoolCalculate; 198 m_prepare_for_reg_writing_reply = eLazyBoolCalculate; 199 m_attach_or_wait_reply = eLazyBoolCalculate; 200 201 m_supports_qProcessInfoPID = true; 202 m_supports_qfProcessInfo = true; 203 m_supports_qUserName = true; 204 m_supports_qGroupName = true; 205 m_supports_qThreadStopInfo = true; 206 m_supports_z0 = true; 207 m_supports_z1 = true; 208 m_supports_z2 = true; 209 m_supports_z3 = true; 210 m_supports_z4 = true; 211 m_host_arch.Clear(); 212 m_process_arch.Clear(); 213 } 214 215 216 bool 217 GDBRemoteCommunicationClient::GetThreadSuffixSupported () 218 { 219 if (m_supports_thread_suffix == eLazyBoolCalculate) 220 { 221 StringExtractorGDBRemote response; 222 m_supports_thread_suffix = eLazyBoolNo; 223 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response, false)) 224 { 225 if (response.IsOKResponse()) 226 m_supports_thread_suffix = eLazyBoolYes; 227 } 228 } 229 return m_supports_thread_suffix; 230 } 231 bool 232 GDBRemoteCommunicationClient::GetVContSupported (char flavor) 233 { 234 if (m_supports_vCont_c == eLazyBoolCalculate) 235 { 236 StringExtractorGDBRemote response; 237 m_supports_vCont_any = eLazyBoolNo; 238 m_supports_vCont_all = eLazyBoolNo; 239 m_supports_vCont_c = eLazyBoolNo; 240 m_supports_vCont_C = eLazyBoolNo; 241 m_supports_vCont_s = eLazyBoolNo; 242 m_supports_vCont_S = eLazyBoolNo; 243 if (SendPacketAndWaitForResponse("vCont?", response, false)) 244 { 245 const char *response_cstr = response.GetStringRef().c_str(); 246 if (::strstr (response_cstr, ";c")) 247 m_supports_vCont_c = eLazyBoolYes; 248 249 if (::strstr (response_cstr, ";C")) 250 m_supports_vCont_C = eLazyBoolYes; 251 252 if (::strstr (response_cstr, ";s")) 253 m_supports_vCont_s = eLazyBoolYes; 254 255 if (::strstr (response_cstr, ";S")) 256 m_supports_vCont_S = eLazyBoolYes; 257 258 if (m_supports_vCont_c == eLazyBoolYes && 259 m_supports_vCont_C == eLazyBoolYes && 260 m_supports_vCont_s == eLazyBoolYes && 261 m_supports_vCont_S == eLazyBoolYes) 262 { 263 m_supports_vCont_all = eLazyBoolYes; 264 } 265 266 if (m_supports_vCont_c == eLazyBoolYes || 267 m_supports_vCont_C == eLazyBoolYes || 268 m_supports_vCont_s == eLazyBoolYes || 269 m_supports_vCont_S == eLazyBoolYes) 270 { 271 m_supports_vCont_any = eLazyBoolYes; 272 } 273 } 274 } 275 276 switch (flavor) 277 { 278 case 'a': return m_supports_vCont_any; 279 case 'A': return m_supports_vCont_all; 280 case 'c': return m_supports_vCont_c; 281 case 'C': return m_supports_vCont_C; 282 case 's': return m_supports_vCont_s; 283 case 'S': return m_supports_vCont_S; 284 default: break; 285 } 286 return false; 287 } 288 289 290 size_t 291 GDBRemoteCommunicationClient::SendPacketAndWaitForResponse 292 ( 293 const char *payload, 294 StringExtractorGDBRemote &response, 295 bool send_async 296 ) 297 { 298 return SendPacketAndWaitForResponse (payload, 299 ::strlen (payload), 300 response, 301 send_async); 302 } 303 304 size_t 305 GDBRemoteCommunicationClient::SendPacketAndWaitForResponse 306 ( 307 const char *payload, 308 size_t payload_length, 309 StringExtractorGDBRemote &response, 310 bool send_async 311 ) 312 { 313 Mutex::Locker locker; 314 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); 315 size_t response_len = 0; 316 if (GetSequenceMutex (locker)) 317 { 318 if (SendPacketNoLock (payload, payload_length)) 319 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); 320 else 321 { 322 if (log) 323 log->Printf("error: failed to send '%*s'", (int) payload_length, payload); 324 } 325 } 326 else 327 { 328 if (send_async) 329 { 330 if (IsRunning()) 331 { 332 Mutex::Locker async_locker (m_async_mutex); 333 m_async_packet.assign(payload, payload_length); 334 m_async_packet_predicate.SetValue (true, eBroadcastNever); 335 336 if (log) 337 log->Printf ("async: async packet = %s", m_async_packet.c_str()); 338 339 bool timed_out = false; 340 if (SendInterrupt(locker, 2, timed_out)) 341 { 342 if (m_interrupt_sent) 343 { 344 m_interrupt_sent = false; 345 TimeValue timeout_time; 346 timeout_time = TimeValue::Now(); 347 timeout_time.OffsetWithSeconds (m_packet_timeout); 348 349 if (log) 350 log->Printf ("async: sent interrupt"); 351 352 if (m_async_packet_predicate.WaitForValueEqualTo (false, &timeout_time, &timed_out)) 353 { 354 if (log) 355 log->Printf ("async: got response"); 356 357 // Swap the response buffer to avoid malloc and string copy 358 response.GetStringRef().swap (m_async_response.GetStringRef()); 359 response_len = response.GetStringRef().size(); 360 } 361 else 362 { 363 if (log) 364 log->Printf ("async: timed out waiting for response"); 365 } 366 367 // Make sure we wait until the continue packet has been sent again... 368 if (m_private_is_running.WaitForValueEqualTo (true, &timeout_time, &timed_out)) 369 { 370 if (log) 371 { 372 if (timed_out) 373 log->Printf ("async: timed out waiting for process to resume, but process was resumed"); 374 else 375 log->Printf ("async: async packet sent"); 376 } 377 } 378 else 379 { 380 if (log) 381 log->Printf ("async: timed out waiting for process to resume"); 382 } 383 } 384 else 385 { 386 // We had a racy condition where we went to send the interrupt 387 // yet we were able to get the lock, so the process must have 388 // just stopped? 389 if (log) 390 log->Printf ("async: got lock without sending interrupt"); 391 // Send the packet normally since we got the lock 392 if (SendPacketNoLock (payload, payload_length)) 393 response_len = WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); 394 else 395 { 396 if (log) 397 log->Printf("error: failed to send '%*s'", (int) payload_length, payload); 398 } 399 } 400 } 401 else 402 { 403 if (log) 404 log->Printf ("async: failed to interrupt"); 405 } 406 } 407 else 408 { 409 if (log) 410 log->Printf ("async: not running, async is ignored"); 411 } 412 } 413 else 414 { 415 if (log) 416 log->Printf("error: failed to get packet sequence mutex, not sending packet '%*s'", (int) payload_length, payload); 417 } 418 } 419 if (response_len == 0) 420 { 421 if (log) 422 log->Printf("error: failed to get response for '%*s'", (int) payload_length, payload); 423 } 424 return response_len; 425 } 426 427 static const char *end_delimiter = "--end--;"; 428 static const int end_delimiter_len = 8; 429 430 std::string 431 GDBRemoteCommunicationClient::HarmonizeThreadIdsForProfileData 432 ( ProcessGDBRemote *process, 433 StringExtractorGDBRemote& profileDataExtractor 434 ) 435 { 436 std::map<uint64_t, uint32_t> new_thread_id_to_used_usec_map; 437 std::stringstream final_output; 438 std::string name, value; 439 440 // Going to assuming thread_used_usec comes first, else bail out. 441 while (profileDataExtractor.GetNameColonValue(name, value)) 442 { 443 if (name.compare("thread_used_id") == 0) 444 { 445 StringExtractor threadIDHexExtractor(value.c_str()); 446 uint64_t thread_id = threadIDHexExtractor.GetHexMaxU64(false, 0); 447 448 bool has_used_usec = false; 449 uint32_t curr_used_usec = 0; 450 std::string usec_name, usec_value; 451 uint32_t input_file_pos = profileDataExtractor.GetFilePos(); 452 if (profileDataExtractor.GetNameColonValue(usec_name, usec_value)) 453 { 454 if (usec_name.compare("thread_used_usec") == 0) 455 { 456 has_used_usec = true; 457 curr_used_usec = strtoull(usec_value.c_str(), NULL, 0); 458 } 459 else 460 { 461 // We didn't find what we want, it is probably 462 // an older version. Bail out. 463 profileDataExtractor.SetFilePos(input_file_pos); 464 } 465 } 466 467 if (has_used_usec) 468 { 469 uint32_t prev_used_usec = 0; 470 std::map<uint64_t, uint32_t>::iterator iterator = m_thread_id_to_used_usec_map.find(thread_id); 471 if (iterator != m_thread_id_to_used_usec_map.end()) 472 { 473 prev_used_usec = m_thread_id_to_used_usec_map[thread_id]; 474 } 475 476 uint32_t real_used_usec = curr_used_usec - prev_used_usec; 477 // A good first time record is one that runs for at least 0.25 sec 478 bool good_first_time = (prev_used_usec == 0) && (real_used_usec > 250000); 479 bool good_subsequent_time = (prev_used_usec > 0) && 480 ((real_used_usec > 0) || (process->HasAssignedIndexIDToThread(thread_id))); 481 482 if (good_first_time || good_subsequent_time) 483 { 484 // We try to avoid doing too many index id reservation, 485 // resulting in fast increase of index ids. 486 487 final_output << name << ":"; 488 int32_t index_id = process->AssignIndexIDToThread(thread_id); 489 final_output << index_id << ";"; 490 491 final_output << usec_name << ":" << usec_value << ";"; 492 } 493 else 494 { 495 // Skip past 'thread_used_name'. 496 std::string local_name, local_value; 497 profileDataExtractor.GetNameColonValue(local_name, local_value); 498 } 499 500 // Store current time as previous time so that they can be compared later. 501 new_thread_id_to_used_usec_map[thread_id] = curr_used_usec; 502 } 503 else 504 { 505 // Bail out and use old string. 506 final_output << name << ":" << value << ";"; 507 } 508 } 509 else 510 { 511 final_output << name << ":" << value << ";"; 512 } 513 } 514 final_output << end_delimiter; 515 m_thread_id_to_used_usec_map = new_thread_id_to_used_usec_map; 516 517 return final_output.str(); 518 } 519 520 StateType 521 GDBRemoteCommunicationClient::SendContinuePacketAndWaitForResponse 522 ( 523 ProcessGDBRemote *process, 524 const char *payload, 525 size_t packet_length, 526 StringExtractorGDBRemote &response 527 ) 528 { 529 m_curr_tid = LLDB_INVALID_THREAD_ID; 530 LogSP log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PROCESS)); 531 if (log) 532 log->Printf ("GDBRemoteCommunicationClient::%s ()", __FUNCTION__); 533 534 Mutex::Locker locker(m_sequence_mutex); 535 StateType state = eStateRunning; 536 537 BroadcastEvent(eBroadcastBitRunPacketSent, NULL); 538 m_public_is_running.SetValue (true, eBroadcastNever); 539 // Set the starting continue packet into "continue_packet". This packet 540 // may change if we are interrupted and we continue after an async packet... 541 std::string continue_packet(payload, packet_length); 542 543 bool got_stdout = false; 544 545 while (state == eStateRunning) 546 { 547 if (!got_stdout) 548 { 549 if (log) 550 log->Printf ("GDBRemoteCommunicationClient::%s () sending continue packet: %s", __FUNCTION__, continue_packet.c_str()); 551 if (SendPacketNoLock(continue_packet.c_str(), continue_packet.size()) == 0) 552 state = eStateInvalid; 553 554 m_private_is_running.SetValue (true, eBroadcastAlways); 555 } 556 557 got_stdout = false; 558 559 if (log) 560 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(%s)", __FUNCTION__, continue_packet.c_str()); 561 562 if (WaitForPacketWithTimeoutMicroSecondsNoLock(response, UINT32_MAX)) 563 { 564 if (response.Empty()) 565 state = eStateInvalid; 566 else 567 { 568 const char stop_type = response.GetChar(); 569 if (log) 570 log->Printf ("GDBRemoteCommunicationClient::%s () got packet: %s", __FUNCTION__, response.GetStringRef().c_str()); 571 switch (stop_type) 572 { 573 case 'T': 574 case 'S': 575 { 576 if (process->GetStopID() == 0) 577 { 578 if (process->GetID() == LLDB_INVALID_PROCESS_ID) 579 { 580 lldb::pid_t pid = GetCurrentProcessID (); 581 if (pid != LLDB_INVALID_PROCESS_ID) 582 process->SetID (pid); 583 } 584 process->BuildDynamicRegisterInfo (true); 585 } 586 587 // Privately notify any internal threads that we have stopped 588 // in case we wanted to interrupt our process, yet we might 589 // send a packet and continue without returning control to the 590 // user. 591 m_private_is_running.SetValue (false, eBroadcastAlways); 592 593 const uint8_t signo = response.GetHexU8 (UINT8_MAX); 594 595 bool continue_after_async = m_async_signal != -1 || m_async_packet_predicate.GetValue(); 596 if (continue_after_async || m_interrupt_sent) 597 { 598 // We sent an interrupt packet to stop the inferior process 599 // for an async signal or to send an async packet while running 600 // but we might have been single stepping and received the 601 // stop packet for the step instead of for the interrupt packet. 602 // Typically when an interrupt is sent a SIGINT or SIGSTOP 603 // is used, so if we get anything else, we need to try and 604 // get another stop reply packet that may have been sent 605 // due to sending the interrupt when the target is stopped 606 // which will just re-send a copy of the last stop reply 607 // packet. If we don't do this, then the reply for our 608 // async packet will be the repeat stop reply packet and cause 609 // a lot of trouble for us! 610 if (signo != SIGINT && signo != SIGSTOP) 611 { 612 continue_after_async = false; 613 614 // We didn't get a a SIGINT or SIGSTOP, so try for a 615 // very brief time (1 ms) to get another stop reply 616 // packet to make sure it doesn't get in the way 617 StringExtractorGDBRemote extra_stop_reply_packet; 618 uint32_t timeout_usec = 1000; 619 if (WaitForPacketWithTimeoutMicroSecondsNoLock (extra_stop_reply_packet, timeout_usec)) 620 { 621 switch (extra_stop_reply_packet.GetChar()) 622 { 623 case 'T': 624 case 'S': 625 // We did get an extra stop reply, which means 626 // our interrupt didn't stop the target so we 627 // shouldn't continue after the async signal 628 // or packet is sent... 629 continue_after_async = false; 630 break; 631 } 632 } 633 } 634 } 635 636 if (m_async_signal != -1) 637 { 638 if (log) 639 log->Printf ("async: send signo = %s", Host::GetSignalAsCString (m_async_signal)); 640 641 // Save off the async signal we are supposed to send 642 const int async_signal = m_async_signal; 643 // Clear the async signal member so we don't end up 644 // sending the signal multiple times... 645 m_async_signal = -1; 646 // Check which signal we stopped with 647 if (signo == async_signal) 648 { 649 if (log) 650 log->Printf ("async: stopped with signal %s, we are done running", Host::GetSignalAsCString (signo)); 651 652 // We already stopped with a signal that we wanted 653 // to stop with, so we are done 654 } 655 else 656 { 657 // We stopped with a different signal that the one 658 // we wanted to stop with, so now we must resume 659 // with the signal we want 660 char signal_packet[32]; 661 int signal_packet_len = 0; 662 signal_packet_len = ::snprintf (signal_packet, 663 sizeof (signal_packet), 664 "C%2.2x", 665 async_signal); 666 667 if (log) 668 log->Printf ("async: stopped with signal %s, resume with %s", 669 Host::GetSignalAsCString (signo), 670 Host::GetSignalAsCString (async_signal)); 671 672 // Set the continue packet to resume even if the 673 // interrupt didn't cause our stop (ignore continue_after_async) 674 continue_packet.assign(signal_packet, signal_packet_len); 675 continue; 676 } 677 } 678 else if (m_async_packet_predicate.GetValue()) 679 { 680 LogSP packet_log (ProcessGDBRemoteLog::GetLogIfAllCategoriesSet (GDBR_LOG_PACKETS)); 681 682 // We are supposed to send an asynchronous packet while 683 // we are running. 684 m_async_response.Clear(); 685 if (m_async_packet.empty()) 686 { 687 if (packet_log) 688 packet_log->Printf ("async: error: empty async packet"); 689 690 } 691 else 692 { 693 if (packet_log) 694 packet_log->Printf ("async: sending packet"); 695 696 SendPacketAndWaitForResponse (&m_async_packet[0], 697 m_async_packet.size(), 698 m_async_response, 699 false); 700 } 701 // Let the other thread that was trying to send the async 702 // packet know that the packet has been sent and response is 703 // ready... 704 m_async_packet_predicate.SetValue(false, eBroadcastAlways); 705 706 if (packet_log) 707 packet_log->Printf ("async: sent packet, continue_after_async = %i", continue_after_async); 708 709 // Set the continue packet to resume if our interrupt 710 // for the async packet did cause the stop 711 if (continue_after_async) 712 { 713 // Reverting this for now as it is causing deadlocks 714 // in programs (<rdar://problem/11529853>). In the future 715 // we should check our thread list and "do the right thing" 716 // for new threads that show up while we stop and run async 717 // packets. Setting the packet to 'c' to continue all threads 718 // is the right thing to do 99.99% of the time because if a 719 // thread was single stepping, and we sent an interrupt, we 720 // will notice above that we didn't stop due to an interrupt 721 // but stopped due to stepping and we would _not_ continue. 722 continue_packet.assign (1, 'c'); 723 continue; 724 } 725 } 726 // Stop with signal and thread info 727 state = eStateStopped; 728 } 729 break; 730 731 case 'W': 732 case 'X': 733 // process exited 734 state = eStateExited; 735 break; 736 737 case 'O': 738 // STDOUT 739 { 740 got_stdout = true; 741 std::string inferior_stdout; 742 inferior_stdout.reserve(response.GetBytesLeft () / 2); 743 char ch; 744 while ((ch = response.GetHexU8()) != '\0') 745 inferior_stdout.append(1, ch); 746 process->AppendSTDOUT (inferior_stdout.c_str(), inferior_stdout.size()); 747 } 748 break; 749 750 case 'A': 751 // Async miscellaneous reply. Right now, only profile data is coming through this channel. 752 { 753 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A' 754 if (m_partial_profile_data.length() > 0) 755 { 756 m_partial_profile_data.append(input); 757 input = m_partial_profile_data; 758 m_partial_profile_data.clear(); 759 } 760 761 size_t found, pos = 0, len = input.length(); 762 while ((found = input.find(end_delimiter, pos)) != std::string::npos) 763 { 764 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str()); 765 const std::string& profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor); 766 process->BroadcastAsyncProfileData (profile_data.c_str(), profile_data.length()); 767 768 pos = found + end_delimiter_len; 769 } 770 771 if (pos < len) 772 { 773 // Last incomplete chunk. 774 m_partial_profile_data = input.substr(pos); 775 } 776 } 777 break; 778 779 case 'E': 780 // ERROR 781 state = eStateInvalid; 782 break; 783 784 default: 785 if (log) 786 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__); 787 state = eStateInvalid; 788 break; 789 } 790 } 791 } 792 else 793 { 794 if (log) 795 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__); 796 state = eStateInvalid; 797 } 798 } 799 if (log) 800 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state)); 801 response.SetFilePos(0); 802 m_private_is_running.SetValue (false, eBroadcastAlways); 803 m_public_is_running.SetValue (false, eBroadcastAlways); 804 return state; 805 } 806 807 bool 808 GDBRemoteCommunicationClient::SendAsyncSignal (int signo) 809 { 810 Mutex::Locker async_locker (m_async_mutex); 811 m_async_signal = signo; 812 bool timed_out = false; 813 Mutex::Locker locker; 814 if (SendInterrupt (locker, 1, timed_out)) 815 return true; 816 m_async_signal = -1; 817 return false; 818 } 819 820 // This function takes a mutex locker as a parameter in case the GetSequenceMutex 821 // actually succeeds. If it doesn't succeed in acquiring the sequence mutex 822 // (the expected result), then it will send the halt packet. If it does succeed 823 // then the caller that requested the interrupt will want to keep the sequence 824 // locked down so that no one else can send packets while the caller has control. 825 // This function usually gets called when we are running and need to stop the 826 // target. It can also be used when we are running and and we need to do something 827 // else (like read/write memory), so we need to interrupt the running process 828 // (gdb remote protocol requires this), and do what we need to do, then resume. 829 830 bool 831 GDBRemoteCommunicationClient::SendInterrupt 832 ( 833 Mutex::Locker& locker, 834 uint32_t seconds_to_wait_for_stop, 835 bool &timed_out 836 ) 837 { 838 timed_out = false; 839 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); 840 841 if (IsRunning()) 842 { 843 // Only send an interrupt if our debugserver is running... 844 if (GetSequenceMutex (locker)) 845 { 846 if (log) 847 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt"); 848 } 849 else 850 { 851 // Someone has the mutex locked waiting for a response or for the 852 // inferior to stop, so send the interrupt on the down low... 853 char ctrl_c = '\x03'; 854 ConnectionStatus status = eConnectionStatusSuccess; 855 size_t bytes_written = Write (&ctrl_c, 1, status, NULL); 856 if (log) 857 log->PutCString("send packet: \\x03"); 858 if (bytes_written > 0) 859 { 860 m_interrupt_sent = true; 861 if (seconds_to_wait_for_stop) 862 { 863 TimeValue timeout; 864 if (seconds_to_wait_for_stop) 865 { 866 timeout = TimeValue::Now(); 867 timeout.OffsetWithSeconds (seconds_to_wait_for_stop); 868 } 869 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out)) 870 { 871 if (log) 872 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped"); 873 return true; 874 } 875 else 876 { 877 if (log) 878 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume"); 879 } 880 } 881 else 882 { 883 if (log) 884 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop..."); 885 return true; 886 } 887 } 888 else 889 { 890 if (log) 891 log->Printf ("SendInterrupt () - failed to write interrupt"); 892 } 893 return false; 894 } 895 } 896 else 897 { 898 if (log) 899 log->Printf ("SendInterrupt () - not running"); 900 } 901 return true; 902 } 903 904 lldb::pid_t 905 GDBRemoteCommunicationClient::GetCurrentProcessID () 906 { 907 StringExtractorGDBRemote response; 908 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false)) 909 { 910 if (response.GetChar() == 'Q') 911 if (response.GetChar() == 'C') 912 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID); 913 } 914 return LLDB_INVALID_PROCESS_ID; 915 } 916 917 bool 918 GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str) 919 { 920 error_str.clear(); 921 StringExtractorGDBRemote response; 922 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false)) 923 { 924 if (response.IsOKResponse()) 925 return true; 926 if (response.GetChar() == 'E') 927 { 928 // A string the describes what failed when launching... 929 error_str = response.GetStringRef().substr(1); 930 } 931 else 932 { 933 error_str.assign ("unknown error occurred launching process"); 934 } 935 } 936 else 937 { 938 error_str.assign ("timed out waiting for app to launch"); 939 } 940 return false; 941 } 942 943 int 944 GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[]) 945 { 946 if (argv && argv[0]) 947 { 948 StreamString packet; 949 packet.PutChar('A'); 950 const char *arg; 951 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i) 952 { 953 const int arg_len = strlen(arg); 954 if (i > 0) 955 packet.PutChar(','); 956 packet.Printf("%i,%i,", arg_len * 2, i); 957 packet.PutBytesAsRawHex8 (arg, arg_len); 958 } 959 960 StringExtractorGDBRemote response; 961 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 962 { 963 if (response.IsOKResponse()) 964 return 0; 965 uint8_t error = response.GetError(); 966 if (error) 967 return error; 968 } 969 } 970 return -1; 971 } 972 973 int 974 GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value) 975 { 976 if (name_equal_value && name_equal_value[0]) 977 { 978 StreamString packet; 979 packet.Printf("QEnvironment:%s", name_equal_value); 980 StringExtractorGDBRemote response; 981 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 982 { 983 if (response.IsOKResponse()) 984 return 0; 985 uint8_t error = response.GetError(); 986 if (error) 987 return error; 988 } 989 } 990 return -1; 991 } 992 993 int 994 GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch) 995 { 996 if (arch && arch[0]) 997 { 998 StreamString packet; 999 packet.Printf("QLaunchArch:%s", arch); 1000 StringExtractorGDBRemote response; 1001 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1002 { 1003 if (response.IsOKResponse()) 1004 return 0; 1005 uint8_t error = response.GetError(); 1006 if (error) 1007 return error; 1008 } 1009 } 1010 return -1; 1011 } 1012 1013 bool 1014 GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major, 1015 uint32_t &minor, 1016 uint32_t &update) 1017 { 1018 if (GetHostInfo ()) 1019 { 1020 if (m_os_version_major != UINT32_MAX) 1021 { 1022 major = m_os_version_major; 1023 minor = m_os_version_minor; 1024 update = m_os_version_update; 1025 return true; 1026 } 1027 } 1028 return false; 1029 } 1030 1031 bool 1032 GDBRemoteCommunicationClient::GetOSBuildString (std::string &s) 1033 { 1034 if (GetHostInfo ()) 1035 { 1036 if (!m_os_build.empty()) 1037 { 1038 s = m_os_build; 1039 return true; 1040 } 1041 } 1042 s.clear(); 1043 return false; 1044 } 1045 1046 1047 bool 1048 GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s) 1049 { 1050 if (GetHostInfo ()) 1051 { 1052 if (!m_os_kernel.empty()) 1053 { 1054 s = m_os_kernel; 1055 return true; 1056 } 1057 } 1058 s.clear(); 1059 return false; 1060 } 1061 1062 bool 1063 GDBRemoteCommunicationClient::GetHostname (std::string &s) 1064 { 1065 if (GetHostInfo ()) 1066 { 1067 if (!m_hostname.empty()) 1068 { 1069 s = m_hostname; 1070 return true; 1071 } 1072 } 1073 s.clear(); 1074 return false; 1075 } 1076 1077 ArchSpec 1078 GDBRemoteCommunicationClient::GetSystemArchitecture () 1079 { 1080 if (GetHostInfo ()) 1081 return m_host_arch; 1082 return ArchSpec(); 1083 } 1084 1085 const lldb_private::ArchSpec & 1086 GDBRemoteCommunicationClient::GetProcessArchitecture () 1087 { 1088 if (m_qProcessInfo_is_valid == eLazyBoolCalculate) 1089 GetCurrentProcessInfo (); 1090 return m_process_arch; 1091 } 1092 1093 1094 bool 1095 GDBRemoteCommunicationClient::GetHostInfo (bool force) 1096 { 1097 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) 1098 { 1099 m_qHostInfo_is_valid = eLazyBoolNo; 1100 StringExtractorGDBRemote response; 1101 if (SendPacketAndWaitForResponse ("qHostInfo", response, false)) 1102 { 1103 if (response.IsNormalResponse()) 1104 { 1105 std::string name; 1106 std::string value; 1107 uint32_t cpu = LLDB_INVALID_CPUTYPE; 1108 uint32_t sub = 0; 1109 std::string arch_name; 1110 std::string os_name; 1111 std::string vendor_name; 1112 std::string triple; 1113 uint32_t pointer_byte_size = 0; 1114 StringExtractor extractor; 1115 ByteOrder byte_order = eByteOrderInvalid; 1116 uint32_t num_keys_decoded = 0; 1117 while (response.GetNameColonValue(name, value)) 1118 { 1119 if (name.compare("cputype") == 0) 1120 { 1121 // exception type in big endian hex 1122 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0); 1123 if (cpu != LLDB_INVALID_CPUTYPE) 1124 ++num_keys_decoded; 1125 } 1126 else if (name.compare("cpusubtype") == 0) 1127 { 1128 // exception count in big endian hex 1129 sub = Args::StringToUInt32 (value.c_str(), 0, 0); 1130 if (sub != 0) 1131 ++num_keys_decoded; 1132 } 1133 else if (name.compare("arch") == 0) 1134 { 1135 arch_name.swap (value); 1136 ++num_keys_decoded; 1137 } 1138 else if (name.compare("triple") == 0) 1139 { 1140 // The triple comes as ASCII hex bytes since it contains '-' chars 1141 extractor.GetStringRef().swap(value); 1142 extractor.SetFilePos(0); 1143 extractor.GetHexByteString (triple); 1144 ++num_keys_decoded; 1145 } 1146 else if (name.compare("os_build") == 0) 1147 { 1148 extractor.GetStringRef().swap(value); 1149 extractor.SetFilePos(0); 1150 extractor.GetHexByteString (m_os_build); 1151 ++num_keys_decoded; 1152 } 1153 else if (name.compare("hostname") == 0) 1154 { 1155 extractor.GetStringRef().swap(value); 1156 extractor.SetFilePos(0); 1157 extractor.GetHexByteString (m_hostname); 1158 ++num_keys_decoded; 1159 } 1160 else if (name.compare("os_kernel") == 0) 1161 { 1162 extractor.GetStringRef().swap(value); 1163 extractor.SetFilePos(0); 1164 extractor.GetHexByteString (m_os_kernel); 1165 ++num_keys_decoded; 1166 } 1167 else if (name.compare("ostype") == 0) 1168 { 1169 os_name.swap (value); 1170 ++num_keys_decoded; 1171 } 1172 else if (name.compare("vendor") == 0) 1173 { 1174 vendor_name.swap(value); 1175 ++num_keys_decoded; 1176 } 1177 else if (name.compare("endian") == 0) 1178 { 1179 ++num_keys_decoded; 1180 if (value.compare("little") == 0) 1181 byte_order = eByteOrderLittle; 1182 else if (value.compare("big") == 0) 1183 byte_order = eByteOrderBig; 1184 else if (value.compare("pdp") == 0) 1185 byte_order = eByteOrderPDP; 1186 else 1187 --num_keys_decoded; 1188 } 1189 else if (name.compare("ptrsize") == 0) 1190 { 1191 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0); 1192 if (pointer_byte_size != 0) 1193 ++num_keys_decoded; 1194 } 1195 else if (name.compare("os_version") == 0) 1196 { 1197 Args::StringToVersion (value.c_str(), 1198 m_os_version_major, 1199 m_os_version_minor, 1200 m_os_version_update); 1201 if (m_os_version_major != UINT32_MAX) 1202 ++num_keys_decoded; 1203 } 1204 else if (name.compare("watchpoint_exceptions_received") == 0) 1205 { 1206 ++num_keys_decoded; 1207 if (strcmp(value.c_str(),"before") == 0) 1208 m_watchpoints_trigger_after_instruction = eLazyBoolNo; 1209 else if (strcmp(value.c_str(),"after") == 0) 1210 m_watchpoints_trigger_after_instruction = eLazyBoolYes; 1211 else 1212 --num_keys_decoded; 1213 } 1214 1215 } 1216 1217 if (num_keys_decoded > 0) 1218 m_qHostInfo_is_valid = eLazyBoolYes; 1219 1220 if (triple.empty()) 1221 { 1222 if (arch_name.empty()) 1223 { 1224 if (cpu != LLDB_INVALID_CPUTYPE) 1225 { 1226 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub); 1227 if (pointer_byte_size) 1228 { 1229 assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); 1230 } 1231 if (byte_order != eByteOrderInvalid) 1232 { 1233 assert (byte_order == m_host_arch.GetByteOrder()); 1234 } 1235 1236 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0) 1237 { 1238 switch (m_host_arch.GetMachine()) 1239 { 1240 case llvm::Triple::arm: 1241 case llvm::Triple::thumb: 1242 os_name = "ios"; 1243 break; 1244 default: 1245 os_name = "macosx"; 1246 break; 1247 } 1248 } 1249 if (!vendor_name.empty()) 1250 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); 1251 if (!os_name.empty()) 1252 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); 1253 1254 } 1255 } 1256 else 1257 { 1258 std::string triple; 1259 triple += arch_name; 1260 if (!vendor_name.empty() || !os_name.empty()) 1261 { 1262 triple += '-'; 1263 if (vendor_name.empty()) 1264 triple += "unknown"; 1265 else 1266 triple += vendor_name; 1267 triple += '-'; 1268 if (os_name.empty()) 1269 triple += "unknown"; 1270 else 1271 triple += os_name; 1272 } 1273 m_host_arch.SetTriple (triple.c_str()); 1274 1275 llvm::Triple &host_triple = m_host_arch.GetTriple(); 1276 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin) 1277 { 1278 switch (m_host_arch.GetMachine()) 1279 { 1280 case llvm::Triple::arm: 1281 case llvm::Triple::thumb: 1282 host_triple.setOS(llvm::Triple::IOS); 1283 break; 1284 default: 1285 host_triple.setOS(llvm::Triple::MacOSX); 1286 break; 1287 } 1288 } 1289 if (pointer_byte_size) 1290 { 1291 assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); 1292 } 1293 if (byte_order != eByteOrderInvalid) 1294 { 1295 assert (byte_order == m_host_arch.GetByteOrder()); 1296 } 1297 1298 } 1299 } 1300 else 1301 { 1302 m_host_arch.SetTriple (triple.c_str()); 1303 if (pointer_byte_size) 1304 { 1305 assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); 1306 } 1307 if (byte_order != eByteOrderInvalid) 1308 { 1309 assert (byte_order == m_host_arch.GetByteOrder()); 1310 } 1311 } 1312 } 1313 } 1314 } 1315 return m_qHostInfo_is_valid == eLazyBoolYes; 1316 } 1317 1318 int 1319 GDBRemoteCommunicationClient::SendAttach 1320 ( 1321 lldb::pid_t pid, 1322 StringExtractorGDBRemote& response 1323 ) 1324 { 1325 if (pid != LLDB_INVALID_PROCESS_ID) 1326 { 1327 char packet[64]; 1328 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid); 1329 assert (packet_len < sizeof(packet)); 1330 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1331 { 1332 if (response.IsErrorResponse()) 1333 return response.GetError(); 1334 return 0; 1335 } 1336 } 1337 return -1; 1338 } 1339 1340 const lldb_private::ArchSpec & 1341 GDBRemoteCommunicationClient::GetHostArchitecture () 1342 { 1343 if (m_qHostInfo_is_valid == eLazyBoolCalculate) 1344 GetHostInfo (); 1345 return m_host_arch; 1346 } 1347 1348 addr_t 1349 GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions) 1350 { 1351 if (m_supports_alloc_dealloc_memory != eLazyBoolNo) 1352 { 1353 m_supports_alloc_dealloc_memory = eLazyBoolYes; 1354 char packet[64]; 1355 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s", 1356 (uint64_t)size, 1357 permissions & lldb::ePermissionsReadable ? "r" : "", 1358 permissions & lldb::ePermissionsWritable ? "w" : "", 1359 permissions & lldb::ePermissionsExecutable ? "x" : ""); 1360 assert (packet_len < sizeof(packet)); 1361 StringExtractorGDBRemote response; 1362 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1363 { 1364 if (!response.IsErrorResponse()) 1365 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 1366 } 1367 else 1368 { 1369 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1370 } 1371 } 1372 return LLDB_INVALID_ADDRESS; 1373 } 1374 1375 bool 1376 GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr) 1377 { 1378 if (m_supports_alloc_dealloc_memory != eLazyBoolNo) 1379 { 1380 m_supports_alloc_dealloc_memory = eLazyBoolYes; 1381 char packet[64]; 1382 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr); 1383 assert (packet_len < sizeof(packet)); 1384 StringExtractorGDBRemote response; 1385 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1386 { 1387 if (response.IsOKResponse()) 1388 return true; 1389 } 1390 else 1391 { 1392 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1393 } 1394 } 1395 return false; 1396 } 1397 1398 bool 1399 GDBRemoteCommunicationClient::Detach () 1400 { 1401 return SendPacket ("D", 1) > 0; 1402 } 1403 1404 Error 1405 GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr, 1406 lldb_private::MemoryRegionInfo ®ion_info) 1407 { 1408 Error error; 1409 region_info.Clear(); 1410 1411 if (m_supports_memory_region_info != eLazyBoolNo) 1412 { 1413 m_supports_memory_region_info = eLazyBoolYes; 1414 char packet[64]; 1415 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr); 1416 assert (packet_len < sizeof(packet)); 1417 StringExtractorGDBRemote response; 1418 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1419 { 1420 std::string name; 1421 std::string value; 1422 addr_t addr_value; 1423 bool success = true; 1424 bool saw_permissions = false; 1425 while (success && response.GetNameColonValue(name, value)) 1426 { 1427 if (name.compare ("start") == 0) 1428 { 1429 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success); 1430 if (success) 1431 region_info.GetRange().SetRangeBase(addr_value); 1432 } 1433 else if (name.compare ("size") == 0) 1434 { 1435 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success); 1436 if (success) 1437 region_info.GetRange().SetByteSize (addr_value); 1438 } 1439 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid()) 1440 { 1441 saw_permissions = true; 1442 if (region_info.GetRange().Contains (addr)) 1443 { 1444 if (value.find('r') != std::string::npos) 1445 region_info.SetReadable (MemoryRegionInfo::eYes); 1446 else 1447 region_info.SetReadable (MemoryRegionInfo::eNo); 1448 1449 if (value.find('w') != std::string::npos) 1450 region_info.SetWritable (MemoryRegionInfo::eYes); 1451 else 1452 region_info.SetWritable (MemoryRegionInfo::eNo); 1453 1454 if (value.find('x') != std::string::npos) 1455 region_info.SetExecutable (MemoryRegionInfo::eYes); 1456 else 1457 region_info.SetExecutable (MemoryRegionInfo::eNo); 1458 } 1459 else 1460 { 1461 // The reported region does not contain this address -- we're looking at an unmapped page 1462 region_info.SetReadable (MemoryRegionInfo::eNo); 1463 region_info.SetWritable (MemoryRegionInfo::eNo); 1464 region_info.SetExecutable (MemoryRegionInfo::eNo); 1465 } 1466 } 1467 else if (name.compare ("error") == 0) 1468 { 1469 StringExtractorGDBRemote name_extractor; 1470 // Swap "value" over into "name_extractor" 1471 name_extractor.GetStringRef().swap(value); 1472 // Now convert the HEX bytes into a string value 1473 name_extractor.GetHexByteString (value); 1474 error.SetErrorString(value.c_str()); 1475 } 1476 } 1477 1478 // We got a valid address range back but no permissions -- which means this is an unmapped page 1479 if (region_info.GetRange().IsValid() && saw_permissions == false) 1480 { 1481 region_info.SetReadable (MemoryRegionInfo::eNo); 1482 region_info.SetWritable (MemoryRegionInfo::eNo); 1483 region_info.SetExecutable (MemoryRegionInfo::eNo); 1484 } 1485 } 1486 else 1487 { 1488 m_supports_memory_region_info = eLazyBoolNo; 1489 } 1490 } 1491 1492 if (m_supports_memory_region_info == eLazyBoolNo) 1493 { 1494 error.SetErrorString("qMemoryRegionInfo is not supported"); 1495 } 1496 if (error.Fail()) 1497 region_info.Clear(); 1498 return error; 1499 1500 } 1501 1502 Error 1503 GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num) 1504 { 1505 Error error; 1506 1507 if (m_supports_watchpoint_support_info == eLazyBoolYes) 1508 { 1509 num = m_num_supported_hardware_watchpoints; 1510 return error; 1511 } 1512 1513 // Set num to 0 first. 1514 num = 0; 1515 if (m_supports_watchpoint_support_info != eLazyBoolNo) 1516 { 1517 char packet[64]; 1518 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:"); 1519 assert (packet_len < sizeof(packet)); 1520 StringExtractorGDBRemote response; 1521 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1522 { 1523 m_supports_watchpoint_support_info = eLazyBoolYes; 1524 std::string name; 1525 std::string value; 1526 while (response.GetNameColonValue(name, value)) 1527 { 1528 if (name.compare ("num") == 0) 1529 { 1530 num = Args::StringToUInt32(value.c_str(), 0, 0); 1531 m_num_supported_hardware_watchpoints = num; 1532 } 1533 } 1534 } 1535 else 1536 { 1537 m_supports_watchpoint_support_info = eLazyBoolNo; 1538 } 1539 } 1540 1541 if (m_supports_watchpoint_support_info == eLazyBoolNo) 1542 { 1543 error.SetErrorString("qWatchpointSupportInfo is not supported"); 1544 } 1545 return error; 1546 1547 } 1548 1549 lldb_private::Error 1550 GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after) 1551 { 1552 Error error(GetWatchpointSupportInfo(num)); 1553 if (error.Success()) 1554 error = GetWatchpointsTriggerAfterInstruction(after); 1555 return error; 1556 } 1557 1558 lldb_private::Error 1559 GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after) 1560 { 1561 Error error; 1562 1563 // we assume watchpoints will happen after running the relevant opcode 1564 // and we only want to override this behavior if we have explicitly 1565 // received a qHostInfo telling us otherwise 1566 if (m_qHostInfo_is_valid != eLazyBoolYes) 1567 after = true; 1568 else 1569 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo); 1570 return error; 1571 } 1572 1573 int 1574 GDBRemoteCommunicationClient::SetSTDIN (char const *path) 1575 { 1576 if (path && path[0]) 1577 { 1578 StreamString packet; 1579 packet.PutCString("QSetSTDIN:"); 1580 packet.PutBytesAsRawHex8(path, strlen(path)); 1581 1582 StringExtractorGDBRemote response; 1583 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1584 { 1585 if (response.IsOKResponse()) 1586 return 0; 1587 uint8_t error = response.GetError(); 1588 if (error) 1589 return error; 1590 } 1591 } 1592 return -1; 1593 } 1594 1595 int 1596 GDBRemoteCommunicationClient::SetSTDOUT (char const *path) 1597 { 1598 if (path && path[0]) 1599 { 1600 StreamString packet; 1601 packet.PutCString("QSetSTDOUT:"); 1602 packet.PutBytesAsRawHex8(path, strlen(path)); 1603 1604 StringExtractorGDBRemote response; 1605 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1606 { 1607 if (response.IsOKResponse()) 1608 return 0; 1609 uint8_t error = response.GetError(); 1610 if (error) 1611 return error; 1612 } 1613 } 1614 return -1; 1615 } 1616 1617 int 1618 GDBRemoteCommunicationClient::SetSTDERR (char const *path) 1619 { 1620 if (path && path[0]) 1621 { 1622 StreamString packet; 1623 packet.PutCString("QSetSTDERR:"); 1624 packet.PutBytesAsRawHex8(path, strlen(path)); 1625 1626 StringExtractorGDBRemote response; 1627 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1628 { 1629 if (response.IsOKResponse()) 1630 return 0; 1631 uint8_t error = response.GetError(); 1632 if (error) 1633 return error; 1634 } 1635 } 1636 return -1; 1637 } 1638 1639 int 1640 GDBRemoteCommunicationClient::SetWorkingDir (char const *path) 1641 { 1642 if (path && path[0]) 1643 { 1644 StreamString packet; 1645 packet.PutCString("QSetWorkingDir:"); 1646 packet.PutBytesAsRawHex8(path, strlen(path)); 1647 1648 StringExtractorGDBRemote response; 1649 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1650 { 1651 if (response.IsOKResponse()) 1652 return 0; 1653 uint8_t error = response.GetError(); 1654 if (error) 1655 return error; 1656 } 1657 } 1658 return -1; 1659 } 1660 1661 int 1662 GDBRemoteCommunicationClient::SetDisableASLR (bool enable) 1663 { 1664 char packet[32]; 1665 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0); 1666 assert (packet_len < sizeof(packet)); 1667 StringExtractorGDBRemote response; 1668 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1669 { 1670 if (response.IsOKResponse()) 1671 return 0; 1672 uint8_t error = response.GetError(); 1673 if (error) 1674 return error; 1675 } 1676 return -1; 1677 } 1678 1679 bool 1680 GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) 1681 { 1682 if (response.IsNormalResponse()) 1683 { 1684 std::string name; 1685 std::string value; 1686 StringExtractor extractor; 1687 1688 while (response.GetNameColonValue(name, value)) 1689 { 1690 if (name.compare("pid") == 0) 1691 { 1692 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); 1693 } 1694 else if (name.compare("ppid") == 0) 1695 { 1696 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); 1697 } 1698 else if (name.compare("uid") == 0) 1699 { 1700 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1701 } 1702 else if (name.compare("euid") == 0) 1703 { 1704 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1705 } 1706 else if (name.compare("gid") == 0) 1707 { 1708 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1709 } 1710 else if (name.compare("egid") == 0) 1711 { 1712 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1713 } 1714 else if (name.compare("triple") == 0) 1715 { 1716 // The triple comes as ASCII hex bytes since it contains '-' chars 1717 extractor.GetStringRef().swap(value); 1718 extractor.SetFilePos(0); 1719 extractor.GetHexByteString (value); 1720 process_info.GetArchitecture ().SetTriple (value.c_str()); 1721 } 1722 else if (name.compare("name") == 0) 1723 { 1724 StringExtractor extractor; 1725 // The process name from ASCII hex bytes since we can't 1726 // control the characters in a process name 1727 extractor.GetStringRef().swap(value); 1728 extractor.SetFilePos(0); 1729 extractor.GetHexByteString (value); 1730 process_info.GetExecutableFile().SetFile (value.c_str(), false); 1731 } 1732 } 1733 1734 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) 1735 return true; 1736 } 1737 return false; 1738 } 1739 1740 bool 1741 GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 1742 { 1743 process_info.Clear(); 1744 1745 if (m_supports_qProcessInfoPID) 1746 { 1747 char packet[32]; 1748 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid); 1749 assert (packet_len < sizeof(packet)); 1750 StringExtractorGDBRemote response; 1751 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1752 { 1753 return DecodeProcessInfoResponse (response, process_info); 1754 } 1755 else 1756 { 1757 m_supports_qProcessInfoPID = false; 1758 return false; 1759 } 1760 } 1761 return false; 1762 } 1763 1764 bool 1765 GDBRemoteCommunicationClient::GetCurrentProcessInfo () 1766 { 1767 if (m_qProcessInfo_is_valid == eLazyBoolYes) 1768 return true; 1769 if (m_qProcessInfo_is_valid == eLazyBoolNo) 1770 return false; 1771 1772 GetHostInfo (); 1773 1774 StringExtractorGDBRemote response; 1775 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false)) 1776 { 1777 if (response.IsNormalResponse()) 1778 { 1779 std::string name; 1780 std::string value; 1781 uint32_t cpu = LLDB_INVALID_CPUTYPE; 1782 uint32_t sub = 0; 1783 std::string arch_name; 1784 std::string os_name; 1785 std::string vendor_name; 1786 std::string triple; 1787 uint32_t pointer_byte_size = 0; 1788 StringExtractor extractor; 1789 ByteOrder byte_order = eByteOrderInvalid; 1790 uint32_t num_keys_decoded = 0; 1791 while (response.GetNameColonValue(name, value)) 1792 { 1793 if (name.compare("cputype") == 0) 1794 { 1795 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16); 1796 if (cpu != LLDB_INVALID_CPUTYPE) 1797 ++num_keys_decoded; 1798 } 1799 else if (name.compare("cpusubtype") == 0) 1800 { 1801 sub = Args::StringToUInt32 (value.c_str(), 0, 16); 1802 if (sub != 0) 1803 ++num_keys_decoded; 1804 } 1805 else if (name.compare("ostype") == 0) 1806 { 1807 os_name.swap (value); 1808 ++num_keys_decoded; 1809 } 1810 else if (name.compare("vendor") == 0) 1811 { 1812 vendor_name.swap(value); 1813 ++num_keys_decoded; 1814 } 1815 else if (name.compare("endian") == 0) 1816 { 1817 ++num_keys_decoded; 1818 if (value.compare("little") == 0) 1819 byte_order = eByteOrderLittle; 1820 else if (value.compare("big") == 0) 1821 byte_order = eByteOrderBig; 1822 else if (value.compare("pdp") == 0) 1823 byte_order = eByteOrderPDP; 1824 else 1825 --num_keys_decoded; 1826 } 1827 else if (name.compare("ptrsize") == 0) 1828 { 1829 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16); 1830 if (pointer_byte_size != 0) 1831 ++num_keys_decoded; 1832 } 1833 } 1834 if (num_keys_decoded > 0) 1835 m_qProcessInfo_is_valid = eLazyBoolYes; 1836 if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty()) 1837 { 1838 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub); 1839 if (pointer_byte_size) 1840 { 1841 assert (pointer_byte_size == m_process_arch.GetAddressByteSize()); 1842 } 1843 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); 1844 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); 1845 return true; 1846 } 1847 } 1848 } 1849 else 1850 { 1851 m_qProcessInfo_is_valid = eLazyBoolNo; 1852 } 1853 1854 return false; 1855 } 1856 1857 1858 uint32_t 1859 GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info, 1860 ProcessInstanceInfoList &process_infos) 1861 { 1862 process_infos.Clear(); 1863 1864 if (m_supports_qfProcessInfo) 1865 { 1866 StreamString packet; 1867 packet.PutCString ("qfProcessInfo"); 1868 if (!match_info.MatchAllProcesses()) 1869 { 1870 packet.PutChar (':'); 1871 const char *name = match_info.GetProcessInfo().GetName(); 1872 bool has_name_match = false; 1873 if (name && name[0]) 1874 { 1875 has_name_match = true; 1876 NameMatchType name_match_type = match_info.GetNameMatchType(); 1877 switch (name_match_type) 1878 { 1879 case eNameMatchIgnore: 1880 has_name_match = false; 1881 break; 1882 1883 case eNameMatchEquals: 1884 packet.PutCString ("name_match:equals;"); 1885 break; 1886 1887 case eNameMatchContains: 1888 packet.PutCString ("name_match:contains;"); 1889 break; 1890 1891 case eNameMatchStartsWith: 1892 packet.PutCString ("name_match:starts_with;"); 1893 break; 1894 1895 case eNameMatchEndsWith: 1896 packet.PutCString ("name_match:ends_with;"); 1897 break; 1898 1899 case eNameMatchRegularExpression: 1900 packet.PutCString ("name_match:regex;"); 1901 break; 1902 } 1903 if (has_name_match) 1904 { 1905 packet.PutCString ("name:"); 1906 packet.PutBytesAsRawHex8(name, ::strlen(name)); 1907 packet.PutChar (';'); 1908 } 1909 } 1910 1911 if (match_info.GetProcessInfo().ProcessIDIsValid()) 1912 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID()); 1913 if (match_info.GetProcessInfo().ParentProcessIDIsValid()) 1914 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID()); 1915 if (match_info.GetProcessInfo().UserIDIsValid()) 1916 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID()); 1917 if (match_info.GetProcessInfo().GroupIDIsValid()) 1918 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID()); 1919 if (match_info.GetProcessInfo().EffectiveUserIDIsValid()) 1920 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID()); 1921 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) 1922 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID()); 1923 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) 1924 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0); 1925 if (match_info.GetProcessInfo().GetArchitecture().IsValid()) 1926 { 1927 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture(); 1928 const llvm::Triple &triple = match_arch.GetTriple(); 1929 packet.PutCString("triple:"); 1930 packet.PutCStringAsRawHex8(triple.getTriple().c_str()); 1931 packet.PutChar (';'); 1932 } 1933 } 1934 StringExtractorGDBRemote response; 1935 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1936 { 1937 do 1938 { 1939 ProcessInstanceInfo process_info; 1940 if (!DecodeProcessInfoResponse (response, process_info)) 1941 break; 1942 process_infos.Append(process_info); 1943 response.GetStringRef().clear(); 1944 response.SetFilePos(0); 1945 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false)); 1946 } 1947 else 1948 { 1949 m_supports_qfProcessInfo = false; 1950 return 0; 1951 } 1952 } 1953 return process_infos.GetSize(); 1954 1955 } 1956 1957 bool 1958 GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name) 1959 { 1960 if (m_supports_qUserName) 1961 { 1962 char packet[32]; 1963 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid); 1964 assert (packet_len < sizeof(packet)); 1965 StringExtractorGDBRemote response; 1966 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1967 { 1968 if (response.IsNormalResponse()) 1969 { 1970 // Make sure we parsed the right number of characters. The response is 1971 // the hex encoded user name and should make up the entire packet. 1972 // If there are any non-hex ASCII bytes, the length won't match below.. 1973 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) 1974 return true; 1975 } 1976 } 1977 else 1978 { 1979 m_supports_qUserName = false; 1980 return false; 1981 } 1982 } 1983 return false; 1984 1985 } 1986 1987 bool 1988 GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name) 1989 { 1990 if (m_supports_qGroupName) 1991 { 1992 char packet[32]; 1993 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid); 1994 assert (packet_len < sizeof(packet)); 1995 StringExtractorGDBRemote response; 1996 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1997 { 1998 if (response.IsNormalResponse()) 1999 { 2000 // Make sure we parsed the right number of characters. The response is 2001 // the hex encoded group name and should make up the entire packet. 2002 // If there are any non-hex ASCII bytes, the length won't match below.. 2003 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) 2004 return true; 2005 } 2006 } 2007 else 2008 { 2009 m_supports_qGroupName = false; 2010 return false; 2011 } 2012 } 2013 return false; 2014 } 2015 2016 void 2017 GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets) 2018 { 2019 uint32_t i; 2020 TimeValue start_time, end_time; 2021 uint64_t total_time_nsec; 2022 float packets_per_second; 2023 if (SendSpeedTestPacket (0, 0)) 2024 { 2025 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2) 2026 { 2027 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2) 2028 { 2029 start_time = TimeValue::Now(); 2030 for (i=0; i<num_packets; ++i) 2031 { 2032 SendSpeedTestPacket (send_size, recv_size); 2033 } 2034 end_time = TimeValue::Now(); 2035 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); 2036 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; 2037 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n", 2038 num_packets, 2039 send_size, 2040 recv_size, 2041 total_time_nsec / TimeValue::NanoSecPerSec, 2042 total_time_nsec % TimeValue::NanoSecPerSec, 2043 packets_per_second); 2044 if (recv_size == 0) 2045 recv_size = 32; 2046 } 2047 if (send_size == 0) 2048 send_size = 32; 2049 } 2050 } 2051 else 2052 { 2053 start_time = TimeValue::Now(); 2054 for (i=0; i<num_packets; ++i) 2055 { 2056 GetCurrentProcessID (); 2057 } 2058 end_time = TimeValue::Now(); 2059 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); 2060 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; 2061 printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n", 2062 num_packets, 2063 total_time_nsec / TimeValue::NanoSecPerSec, 2064 total_time_nsec % TimeValue::NanoSecPerSec, 2065 packets_per_second); 2066 } 2067 } 2068 2069 bool 2070 GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size) 2071 { 2072 StreamString packet; 2073 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size); 2074 uint32_t bytes_left = send_size; 2075 while (bytes_left > 0) 2076 { 2077 if (bytes_left >= 26) 2078 { 2079 packet.PutCString("abcdefghijklmnopqrstuvwxyz"); 2080 bytes_left -= 26; 2081 } 2082 else 2083 { 2084 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz"); 2085 bytes_left = 0; 2086 } 2087 } 2088 2089 StringExtractorGDBRemote response; 2090 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0; 2091 return false; 2092 } 2093 2094 uint16_t 2095 GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort () 2096 { 2097 StringExtractorGDBRemote response; 2098 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false)) 2099 { 2100 std::string name; 2101 std::string value; 2102 uint16_t port = 0; 2103 //lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 2104 while (response.GetNameColonValue(name, value)) 2105 { 2106 if (name.size() == 4 && name.compare("port") == 0) 2107 port = Args::StringToUInt32(value.c_str(), 0, 0); 2108 // if (name.size() == 3 && name.compare("pid") == 0) 2109 // pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0); 2110 } 2111 return port; 2112 } 2113 return 0; 2114 } 2115 2116 bool 2117 GDBRemoteCommunicationClient::SetCurrentThread (int tid) 2118 { 2119 if (m_curr_tid == tid) 2120 return true; 2121 2122 char packet[32]; 2123 int packet_len; 2124 if (tid <= 0) 2125 packet_len = ::snprintf (packet, sizeof(packet), "Hg%i", tid); 2126 else 2127 packet_len = ::snprintf (packet, sizeof(packet), "Hg%x", tid); 2128 assert (packet_len + 1 < sizeof(packet)); 2129 StringExtractorGDBRemote response; 2130 if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) 2131 { 2132 if (response.IsOKResponse()) 2133 { 2134 m_curr_tid = tid; 2135 return true; 2136 } 2137 } 2138 return false; 2139 } 2140 2141 bool 2142 GDBRemoteCommunicationClient::SetCurrentThreadForRun (int tid) 2143 { 2144 if (m_curr_tid_run == tid) 2145 return true; 2146 2147 char packet[32]; 2148 int packet_len; 2149 if (tid <= 0) 2150 packet_len = ::snprintf (packet, sizeof(packet), "Hc%i", tid); 2151 else 2152 packet_len = ::snprintf (packet, sizeof(packet), "Hc%x", tid); 2153 2154 assert (packet_len + 1 < sizeof(packet)); 2155 StringExtractorGDBRemote response; 2156 if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) 2157 { 2158 if (response.IsOKResponse()) 2159 { 2160 m_curr_tid_run = tid; 2161 return true; 2162 } 2163 } 2164 return false; 2165 } 2166 2167 bool 2168 GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response) 2169 { 2170 if (SendPacketAndWaitForResponse("?", 1, response, false)) 2171 return response.IsNormalResponse(); 2172 return false; 2173 } 2174 2175 bool 2176 GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response) 2177 { 2178 if (m_supports_qThreadStopInfo) 2179 { 2180 char packet[256]; 2181 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid); 2182 assert (packet_len < sizeof(packet)); 2183 if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) 2184 { 2185 if (response.IsNormalResponse()) 2186 return true; 2187 else 2188 return false; 2189 } 2190 else 2191 { 2192 m_supports_qThreadStopInfo = false; 2193 } 2194 } 2195 // if (SetCurrentThread (tid)) 2196 // return GetStopReply (response); 2197 return false; 2198 } 2199 2200 2201 uint8_t 2202 GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length) 2203 { 2204 switch (type) 2205 { 2206 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break; 2207 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break; 2208 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break; 2209 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break; 2210 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break; 2211 } 2212 2213 char packet[64]; 2214 const int packet_len = ::snprintf (packet, 2215 sizeof(packet), 2216 "%c%i,%" PRIx64 ",%x", 2217 insert ? 'Z' : 'z', 2218 type, 2219 addr, 2220 length); 2221 2222 assert (packet_len + 1 < sizeof(packet)); 2223 StringExtractorGDBRemote response; 2224 if (SendPacketAndWaitForResponse(packet, packet_len, response, true)) 2225 { 2226 if (response.IsOKResponse()) 2227 return 0; 2228 else if (response.IsErrorResponse()) 2229 return response.GetError(); 2230 } 2231 else 2232 { 2233 switch (type) 2234 { 2235 case eBreakpointSoftware: m_supports_z0 = false; break; 2236 case eBreakpointHardware: m_supports_z1 = false; break; 2237 case eWatchpointWrite: m_supports_z2 = false; break; 2238 case eWatchpointRead: m_supports_z3 = false; break; 2239 case eWatchpointReadWrite: m_supports_z4 = false; break; 2240 } 2241 } 2242 2243 return UINT8_MAX; 2244 } 2245 2246 size_t 2247 GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids, 2248 bool &sequence_mutex_unavailable) 2249 { 2250 Mutex::Locker locker; 2251 thread_ids.clear(); 2252 2253 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex")) 2254 { 2255 sequence_mutex_unavailable = false; 2256 StringExtractorGDBRemote response; 2257 2258 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); 2259 response.IsNormalResponse(); 2260 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ())) 2261 { 2262 char ch = response.GetChar(); 2263 if (ch == 'l') 2264 break; 2265 if (ch == 'm') 2266 { 2267 do 2268 { 2269 tid_t tid = response.GetHexMaxU32(false, LLDB_INVALID_THREAD_ID); 2270 2271 if (tid != LLDB_INVALID_THREAD_ID) 2272 { 2273 thread_ids.push_back (tid); 2274 } 2275 ch = response.GetChar(); // Skip the command separator 2276 } while (ch == ','); // Make sure we got a comma separator 2277 } 2278 } 2279 } 2280 else 2281 { 2282 #if defined (LLDB_CONFIGURATION_DEBUG) 2283 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"); 2284 #else 2285 LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); 2286 if (log) 2287 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'"); 2288 #endif 2289 sequence_mutex_unavailable = true; 2290 } 2291 return thread_ids.size(); 2292 } 2293 2294 lldb::addr_t 2295 GDBRemoteCommunicationClient::GetShlibInfoAddr() 2296 { 2297 if (!IsRunning()) 2298 { 2299 StringExtractorGDBRemote response; 2300 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false)) 2301 { 2302 if (response.IsNormalResponse()) 2303 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 2304 } 2305 } 2306 return LLDB_INVALID_ADDRESS; 2307 } 2308 2309