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 Log *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 Log *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_async_packet = false; 544 545 while (state == eStateRunning) 546 { 547 if (!got_async_packet) 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_async_packet = 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 Log * 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_async_packet = 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 got_async_packet = true; 754 std::string input = response.GetStringRef().substr(1); // '1' to move beyond 'A' 755 if (m_partial_profile_data.length() > 0) 756 { 757 m_partial_profile_data.append(input); 758 input = m_partial_profile_data; 759 m_partial_profile_data.clear(); 760 } 761 762 size_t found, pos = 0, len = input.length(); 763 while ((found = input.find(end_delimiter, pos)) != std::string::npos) 764 { 765 StringExtractorGDBRemote profileDataExtractor(input.substr(pos, found).c_str()); 766 const std::string& profile_data = HarmonizeThreadIdsForProfileData(process, profileDataExtractor); 767 process->BroadcastAsyncProfileData (profile_data.c_str(), profile_data.length()); 768 769 pos = found + end_delimiter_len; 770 } 771 772 if (pos < len) 773 { 774 // Last incomplete chunk. 775 m_partial_profile_data = input.substr(pos); 776 } 777 } 778 break; 779 780 case 'E': 781 // ERROR 782 state = eStateInvalid; 783 break; 784 785 default: 786 if (log) 787 log->Printf ("GDBRemoteCommunicationClient::%s () unrecognized async packet", __FUNCTION__); 788 state = eStateInvalid; 789 break; 790 } 791 } 792 } 793 else 794 { 795 if (log) 796 log->Printf ("GDBRemoteCommunicationClient::%s () WaitForPacket(...) => false", __FUNCTION__); 797 state = eStateInvalid; 798 } 799 } 800 if (log) 801 log->Printf ("GDBRemoteCommunicationClient::%s () => %s", __FUNCTION__, StateAsCString(state)); 802 response.SetFilePos(0); 803 m_private_is_running.SetValue (false, eBroadcastAlways); 804 m_public_is_running.SetValue (false, eBroadcastAlways); 805 return state; 806 } 807 808 bool 809 GDBRemoteCommunicationClient::SendAsyncSignal (int signo) 810 { 811 Mutex::Locker async_locker (m_async_mutex); 812 m_async_signal = signo; 813 bool timed_out = false; 814 Mutex::Locker locker; 815 if (SendInterrupt (locker, 1, timed_out)) 816 return true; 817 m_async_signal = -1; 818 return false; 819 } 820 821 // This function takes a mutex locker as a parameter in case the GetSequenceMutex 822 // actually succeeds. If it doesn't succeed in acquiring the sequence mutex 823 // (the expected result), then it will send the halt packet. If it does succeed 824 // then the caller that requested the interrupt will want to keep the sequence 825 // locked down so that no one else can send packets while the caller has control. 826 // This function usually gets called when we are running and need to stop the 827 // target. It can also be used when we are running and and we need to do something 828 // else (like read/write memory), so we need to interrupt the running process 829 // (gdb remote protocol requires this), and do what we need to do, then resume. 830 831 bool 832 GDBRemoteCommunicationClient::SendInterrupt 833 ( 834 Mutex::Locker& locker, 835 uint32_t seconds_to_wait_for_stop, 836 bool &timed_out 837 ) 838 { 839 timed_out = false; 840 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); 841 842 if (IsRunning()) 843 { 844 // Only send an interrupt if our debugserver is running... 845 if (GetSequenceMutex (locker)) 846 { 847 if (log) 848 log->Printf ("SendInterrupt () - got sequence mutex without having to interrupt"); 849 } 850 else 851 { 852 // Someone has the mutex locked waiting for a response or for the 853 // inferior to stop, so send the interrupt on the down low... 854 char ctrl_c = '\x03'; 855 ConnectionStatus status = eConnectionStatusSuccess; 856 size_t bytes_written = Write (&ctrl_c, 1, status, NULL); 857 if (log) 858 log->PutCString("send packet: \\x03"); 859 if (bytes_written > 0) 860 { 861 m_interrupt_sent = true; 862 if (seconds_to_wait_for_stop) 863 { 864 TimeValue timeout; 865 if (seconds_to_wait_for_stop) 866 { 867 timeout = TimeValue::Now(); 868 timeout.OffsetWithSeconds (seconds_to_wait_for_stop); 869 } 870 if (m_private_is_running.WaitForValueEqualTo (false, &timeout, &timed_out)) 871 { 872 if (log) 873 log->PutCString ("SendInterrupt () - sent interrupt, private state stopped"); 874 return true; 875 } 876 else 877 { 878 if (log) 879 log->Printf ("SendInterrupt () - sent interrupt, timed out wating for async thread resume"); 880 } 881 } 882 else 883 { 884 if (log) 885 log->Printf ("SendInterrupt () - sent interrupt, not waiting for stop..."); 886 return true; 887 } 888 } 889 else 890 { 891 if (log) 892 log->Printf ("SendInterrupt () - failed to write interrupt"); 893 } 894 return false; 895 } 896 } 897 else 898 { 899 if (log) 900 log->Printf ("SendInterrupt () - not running"); 901 } 902 return true; 903 } 904 905 lldb::pid_t 906 GDBRemoteCommunicationClient::GetCurrentProcessID () 907 { 908 StringExtractorGDBRemote response; 909 if (SendPacketAndWaitForResponse("qC", strlen("qC"), response, false)) 910 { 911 if (response.GetChar() == 'Q') 912 if (response.GetChar() == 'C') 913 return response.GetHexMaxU32 (false, LLDB_INVALID_PROCESS_ID); 914 } 915 return LLDB_INVALID_PROCESS_ID; 916 } 917 918 bool 919 GDBRemoteCommunicationClient::GetLaunchSuccess (std::string &error_str) 920 { 921 error_str.clear(); 922 StringExtractorGDBRemote response; 923 if (SendPacketAndWaitForResponse("qLaunchSuccess", strlen("qLaunchSuccess"), response, false)) 924 { 925 if (response.IsOKResponse()) 926 return true; 927 if (response.GetChar() == 'E') 928 { 929 // A string the describes what failed when launching... 930 error_str = response.GetStringRef().substr(1); 931 } 932 else 933 { 934 error_str.assign ("unknown error occurred launching process"); 935 } 936 } 937 else 938 { 939 error_str.assign ("timed out waiting for app to launch"); 940 } 941 return false; 942 } 943 944 int 945 GDBRemoteCommunicationClient::SendArgumentsPacket (char const *argv[]) 946 { 947 if (argv && argv[0]) 948 { 949 StreamString packet; 950 packet.PutChar('A'); 951 const char *arg; 952 for (uint32_t i = 0; (arg = argv[i]) != NULL; ++i) 953 { 954 const int arg_len = strlen(arg); 955 if (i > 0) 956 packet.PutChar(','); 957 packet.Printf("%i,%i,", arg_len * 2, i); 958 packet.PutBytesAsRawHex8 (arg, arg_len); 959 } 960 961 StringExtractorGDBRemote response; 962 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 963 { 964 if (response.IsOKResponse()) 965 return 0; 966 uint8_t error = response.GetError(); 967 if (error) 968 return error; 969 } 970 } 971 return -1; 972 } 973 974 int 975 GDBRemoteCommunicationClient::SendEnvironmentPacket (char const *name_equal_value) 976 { 977 if (name_equal_value && name_equal_value[0]) 978 { 979 StreamString packet; 980 packet.Printf("QEnvironment:%s", name_equal_value); 981 StringExtractorGDBRemote response; 982 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 983 { 984 if (response.IsOKResponse()) 985 return 0; 986 uint8_t error = response.GetError(); 987 if (error) 988 return error; 989 } 990 } 991 return -1; 992 } 993 994 int 995 GDBRemoteCommunicationClient::SendLaunchArchPacket (char const *arch) 996 { 997 if (arch && arch[0]) 998 { 999 StreamString packet; 1000 packet.Printf("QLaunchArch:%s", arch); 1001 StringExtractorGDBRemote response; 1002 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1003 { 1004 if (response.IsOKResponse()) 1005 return 0; 1006 uint8_t error = response.GetError(); 1007 if (error) 1008 return error; 1009 } 1010 } 1011 return -1; 1012 } 1013 1014 bool 1015 GDBRemoteCommunicationClient::GetOSVersion (uint32_t &major, 1016 uint32_t &minor, 1017 uint32_t &update) 1018 { 1019 if (GetHostInfo ()) 1020 { 1021 if (m_os_version_major != UINT32_MAX) 1022 { 1023 major = m_os_version_major; 1024 minor = m_os_version_minor; 1025 update = m_os_version_update; 1026 return true; 1027 } 1028 } 1029 return false; 1030 } 1031 1032 bool 1033 GDBRemoteCommunicationClient::GetOSBuildString (std::string &s) 1034 { 1035 if (GetHostInfo ()) 1036 { 1037 if (!m_os_build.empty()) 1038 { 1039 s = m_os_build; 1040 return true; 1041 } 1042 } 1043 s.clear(); 1044 return false; 1045 } 1046 1047 1048 bool 1049 GDBRemoteCommunicationClient::GetOSKernelDescription (std::string &s) 1050 { 1051 if (GetHostInfo ()) 1052 { 1053 if (!m_os_kernel.empty()) 1054 { 1055 s = m_os_kernel; 1056 return true; 1057 } 1058 } 1059 s.clear(); 1060 return false; 1061 } 1062 1063 bool 1064 GDBRemoteCommunicationClient::GetHostname (std::string &s) 1065 { 1066 if (GetHostInfo ()) 1067 { 1068 if (!m_hostname.empty()) 1069 { 1070 s = m_hostname; 1071 return true; 1072 } 1073 } 1074 s.clear(); 1075 return false; 1076 } 1077 1078 ArchSpec 1079 GDBRemoteCommunicationClient::GetSystemArchitecture () 1080 { 1081 if (GetHostInfo ()) 1082 return m_host_arch; 1083 return ArchSpec(); 1084 } 1085 1086 const lldb_private::ArchSpec & 1087 GDBRemoteCommunicationClient::GetProcessArchitecture () 1088 { 1089 if (m_qProcessInfo_is_valid == eLazyBoolCalculate) 1090 GetCurrentProcessInfo (); 1091 return m_process_arch; 1092 } 1093 1094 1095 bool 1096 GDBRemoteCommunicationClient::GetHostInfo (bool force) 1097 { 1098 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) 1099 { 1100 m_qHostInfo_is_valid = eLazyBoolNo; 1101 StringExtractorGDBRemote response; 1102 if (SendPacketAndWaitForResponse ("qHostInfo", response, false)) 1103 { 1104 if (response.IsNormalResponse()) 1105 { 1106 std::string name; 1107 std::string value; 1108 uint32_t cpu = LLDB_INVALID_CPUTYPE; 1109 uint32_t sub = 0; 1110 std::string arch_name; 1111 std::string os_name; 1112 std::string vendor_name; 1113 std::string triple; 1114 uint32_t pointer_byte_size = 0; 1115 StringExtractor extractor; 1116 ByteOrder byte_order = eByteOrderInvalid; 1117 uint32_t num_keys_decoded = 0; 1118 while (response.GetNameColonValue(name, value)) 1119 { 1120 if (name.compare("cputype") == 0) 1121 { 1122 // exception type in big endian hex 1123 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 0); 1124 if (cpu != LLDB_INVALID_CPUTYPE) 1125 ++num_keys_decoded; 1126 } 1127 else if (name.compare("cpusubtype") == 0) 1128 { 1129 // exception count in big endian hex 1130 sub = Args::StringToUInt32 (value.c_str(), 0, 0); 1131 if (sub != 0) 1132 ++num_keys_decoded; 1133 } 1134 else if (name.compare("arch") == 0) 1135 { 1136 arch_name.swap (value); 1137 ++num_keys_decoded; 1138 } 1139 else if (name.compare("triple") == 0) 1140 { 1141 // The triple comes as ASCII hex bytes since it contains '-' chars 1142 extractor.GetStringRef().swap(value); 1143 extractor.SetFilePos(0); 1144 extractor.GetHexByteString (triple); 1145 ++num_keys_decoded; 1146 } 1147 else if (name.compare("os_build") == 0) 1148 { 1149 extractor.GetStringRef().swap(value); 1150 extractor.SetFilePos(0); 1151 extractor.GetHexByteString (m_os_build); 1152 ++num_keys_decoded; 1153 } 1154 else if (name.compare("hostname") == 0) 1155 { 1156 extractor.GetStringRef().swap(value); 1157 extractor.SetFilePos(0); 1158 extractor.GetHexByteString (m_hostname); 1159 ++num_keys_decoded; 1160 } 1161 else if (name.compare("os_kernel") == 0) 1162 { 1163 extractor.GetStringRef().swap(value); 1164 extractor.SetFilePos(0); 1165 extractor.GetHexByteString (m_os_kernel); 1166 ++num_keys_decoded; 1167 } 1168 else if (name.compare("ostype") == 0) 1169 { 1170 os_name.swap (value); 1171 ++num_keys_decoded; 1172 } 1173 else if (name.compare("vendor") == 0) 1174 { 1175 vendor_name.swap(value); 1176 ++num_keys_decoded; 1177 } 1178 else if (name.compare("endian") == 0) 1179 { 1180 ++num_keys_decoded; 1181 if (value.compare("little") == 0) 1182 byte_order = eByteOrderLittle; 1183 else if (value.compare("big") == 0) 1184 byte_order = eByteOrderBig; 1185 else if (value.compare("pdp") == 0) 1186 byte_order = eByteOrderPDP; 1187 else 1188 --num_keys_decoded; 1189 } 1190 else if (name.compare("ptrsize") == 0) 1191 { 1192 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 0); 1193 if (pointer_byte_size != 0) 1194 ++num_keys_decoded; 1195 } 1196 else if (name.compare("os_version") == 0) 1197 { 1198 Args::StringToVersion (value.c_str(), 1199 m_os_version_major, 1200 m_os_version_minor, 1201 m_os_version_update); 1202 if (m_os_version_major != UINT32_MAX) 1203 ++num_keys_decoded; 1204 } 1205 else if (name.compare("watchpoint_exceptions_received") == 0) 1206 { 1207 ++num_keys_decoded; 1208 if (strcmp(value.c_str(),"before") == 0) 1209 m_watchpoints_trigger_after_instruction = eLazyBoolNo; 1210 else if (strcmp(value.c_str(),"after") == 0) 1211 m_watchpoints_trigger_after_instruction = eLazyBoolYes; 1212 else 1213 --num_keys_decoded; 1214 } 1215 1216 } 1217 1218 if (num_keys_decoded > 0) 1219 m_qHostInfo_is_valid = eLazyBoolYes; 1220 1221 if (triple.empty()) 1222 { 1223 if (arch_name.empty()) 1224 { 1225 if (cpu != LLDB_INVALID_CPUTYPE) 1226 { 1227 m_host_arch.SetArchitecture (eArchTypeMachO, cpu, sub); 1228 if (pointer_byte_size) 1229 { 1230 assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); 1231 } 1232 if (byte_order != eByteOrderInvalid) 1233 { 1234 assert (byte_order == m_host_arch.GetByteOrder()); 1235 } 1236 1237 if (!os_name.empty() && vendor_name.compare("apple") == 0 && os_name.find("darwin") == 0) 1238 { 1239 switch (m_host_arch.GetMachine()) 1240 { 1241 case llvm::Triple::arm: 1242 case llvm::Triple::thumb: 1243 os_name = "ios"; 1244 break; 1245 default: 1246 os_name = "macosx"; 1247 break; 1248 } 1249 } 1250 if (!vendor_name.empty()) 1251 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); 1252 if (!os_name.empty()) 1253 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); 1254 1255 } 1256 } 1257 else 1258 { 1259 std::string triple; 1260 triple += arch_name; 1261 if (!vendor_name.empty() || !os_name.empty()) 1262 { 1263 triple += '-'; 1264 if (vendor_name.empty()) 1265 triple += "unknown"; 1266 else 1267 triple += vendor_name; 1268 triple += '-'; 1269 if (os_name.empty()) 1270 triple += "unknown"; 1271 else 1272 triple += os_name; 1273 } 1274 m_host_arch.SetTriple (triple.c_str()); 1275 1276 llvm::Triple &host_triple = m_host_arch.GetTriple(); 1277 if (host_triple.getVendor() == llvm::Triple::Apple && host_triple.getOS() == llvm::Triple::Darwin) 1278 { 1279 switch (m_host_arch.GetMachine()) 1280 { 1281 case llvm::Triple::arm: 1282 case llvm::Triple::thumb: 1283 host_triple.setOS(llvm::Triple::IOS); 1284 break; 1285 default: 1286 host_triple.setOS(llvm::Triple::MacOSX); 1287 break; 1288 } 1289 } 1290 if (pointer_byte_size) 1291 { 1292 assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); 1293 } 1294 if (byte_order != eByteOrderInvalid) 1295 { 1296 assert (byte_order == m_host_arch.GetByteOrder()); 1297 } 1298 1299 } 1300 } 1301 else 1302 { 1303 m_host_arch.SetTriple (triple.c_str()); 1304 if (pointer_byte_size) 1305 { 1306 assert (pointer_byte_size == m_host_arch.GetAddressByteSize()); 1307 } 1308 if (byte_order != eByteOrderInvalid) 1309 { 1310 assert (byte_order == m_host_arch.GetByteOrder()); 1311 } 1312 } 1313 } 1314 } 1315 } 1316 return m_qHostInfo_is_valid == eLazyBoolYes; 1317 } 1318 1319 int 1320 GDBRemoteCommunicationClient::SendAttach 1321 ( 1322 lldb::pid_t pid, 1323 StringExtractorGDBRemote& response 1324 ) 1325 { 1326 if (pid != LLDB_INVALID_PROCESS_ID) 1327 { 1328 char packet[64]; 1329 const int packet_len = ::snprintf (packet, sizeof(packet), "vAttach;%" PRIx64, pid); 1330 assert (packet_len < sizeof(packet)); 1331 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1332 { 1333 if (response.IsErrorResponse()) 1334 return response.GetError(); 1335 return 0; 1336 } 1337 } 1338 return -1; 1339 } 1340 1341 const lldb_private::ArchSpec & 1342 GDBRemoteCommunicationClient::GetHostArchitecture () 1343 { 1344 if (m_qHostInfo_is_valid == eLazyBoolCalculate) 1345 GetHostInfo (); 1346 return m_host_arch; 1347 } 1348 1349 addr_t 1350 GDBRemoteCommunicationClient::AllocateMemory (size_t size, uint32_t permissions) 1351 { 1352 if (m_supports_alloc_dealloc_memory != eLazyBoolNo) 1353 { 1354 m_supports_alloc_dealloc_memory = eLazyBoolYes; 1355 char packet[64]; 1356 const int packet_len = ::snprintf (packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s", 1357 (uint64_t)size, 1358 permissions & lldb::ePermissionsReadable ? "r" : "", 1359 permissions & lldb::ePermissionsWritable ? "w" : "", 1360 permissions & lldb::ePermissionsExecutable ? "x" : ""); 1361 assert (packet_len < sizeof(packet)); 1362 StringExtractorGDBRemote response; 1363 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1364 { 1365 if (!response.IsErrorResponse()) 1366 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 1367 } 1368 else 1369 { 1370 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1371 } 1372 } 1373 return LLDB_INVALID_ADDRESS; 1374 } 1375 1376 bool 1377 GDBRemoteCommunicationClient::DeallocateMemory (addr_t addr) 1378 { 1379 if (m_supports_alloc_dealloc_memory != eLazyBoolNo) 1380 { 1381 m_supports_alloc_dealloc_memory = eLazyBoolYes; 1382 char packet[64]; 1383 const int packet_len = ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr); 1384 assert (packet_len < sizeof(packet)); 1385 StringExtractorGDBRemote response; 1386 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1387 { 1388 if (response.IsOKResponse()) 1389 return true; 1390 } 1391 else 1392 { 1393 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1394 } 1395 } 1396 return false; 1397 } 1398 1399 bool 1400 GDBRemoteCommunicationClient::Detach () 1401 { 1402 return SendPacket ("D", 1) > 0; 1403 } 1404 1405 Error 1406 GDBRemoteCommunicationClient::GetMemoryRegionInfo (lldb::addr_t addr, 1407 lldb_private::MemoryRegionInfo ®ion_info) 1408 { 1409 Error error; 1410 region_info.Clear(); 1411 1412 if (m_supports_memory_region_info != eLazyBoolNo) 1413 { 1414 m_supports_memory_region_info = eLazyBoolYes; 1415 char packet[64]; 1416 const int packet_len = ::snprintf(packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr); 1417 assert (packet_len < sizeof(packet)); 1418 StringExtractorGDBRemote response; 1419 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1420 { 1421 std::string name; 1422 std::string value; 1423 addr_t addr_value; 1424 bool success = true; 1425 bool saw_permissions = false; 1426 while (success && response.GetNameColonValue(name, value)) 1427 { 1428 if (name.compare ("start") == 0) 1429 { 1430 addr_value = Args::StringToUInt64(value.c_str(), LLDB_INVALID_ADDRESS, 16, &success); 1431 if (success) 1432 region_info.GetRange().SetRangeBase(addr_value); 1433 } 1434 else if (name.compare ("size") == 0) 1435 { 1436 addr_value = Args::StringToUInt64(value.c_str(), 0, 16, &success); 1437 if (success) 1438 region_info.GetRange().SetByteSize (addr_value); 1439 } 1440 else if (name.compare ("permissions") == 0 && region_info.GetRange().IsValid()) 1441 { 1442 saw_permissions = true; 1443 if (region_info.GetRange().Contains (addr)) 1444 { 1445 if (value.find('r') != std::string::npos) 1446 region_info.SetReadable (MemoryRegionInfo::eYes); 1447 else 1448 region_info.SetReadable (MemoryRegionInfo::eNo); 1449 1450 if (value.find('w') != std::string::npos) 1451 region_info.SetWritable (MemoryRegionInfo::eYes); 1452 else 1453 region_info.SetWritable (MemoryRegionInfo::eNo); 1454 1455 if (value.find('x') != std::string::npos) 1456 region_info.SetExecutable (MemoryRegionInfo::eYes); 1457 else 1458 region_info.SetExecutable (MemoryRegionInfo::eNo); 1459 } 1460 else 1461 { 1462 // The reported region does not contain this address -- we're looking at an unmapped page 1463 region_info.SetReadable (MemoryRegionInfo::eNo); 1464 region_info.SetWritable (MemoryRegionInfo::eNo); 1465 region_info.SetExecutable (MemoryRegionInfo::eNo); 1466 } 1467 } 1468 else if (name.compare ("error") == 0) 1469 { 1470 StringExtractorGDBRemote name_extractor; 1471 // Swap "value" over into "name_extractor" 1472 name_extractor.GetStringRef().swap(value); 1473 // Now convert the HEX bytes into a string value 1474 name_extractor.GetHexByteString (value); 1475 error.SetErrorString(value.c_str()); 1476 } 1477 } 1478 1479 // We got a valid address range back but no permissions -- which means this is an unmapped page 1480 if (region_info.GetRange().IsValid() && saw_permissions == false) 1481 { 1482 region_info.SetReadable (MemoryRegionInfo::eNo); 1483 region_info.SetWritable (MemoryRegionInfo::eNo); 1484 region_info.SetExecutable (MemoryRegionInfo::eNo); 1485 } 1486 } 1487 else 1488 { 1489 m_supports_memory_region_info = eLazyBoolNo; 1490 } 1491 } 1492 1493 if (m_supports_memory_region_info == eLazyBoolNo) 1494 { 1495 error.SetErrorString("qMemoryRegionInfo is not supported"); 1496 } 1497 if (error.Fail()) 1498 region_info.Clear(); 1499 return error; 1500 1501 } 1502 1503 Error 1504 GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num) 1505 { 1506 Error error; 1507 1508 if (m_supports_watchpoint_support_info == eLazyBoolYes) 1509 { 1510 num = m_num_supported_hardware_watchpoints; 1511 return error; 1512 } 1513 1514 // Set num to 0 first. 1515 num = 0; 1516 if (m_supports_watchpoint_support_info != eLazyBoolNo) 1517 { 1518 char packet[64]; 1519 const int packet_len = ::snprintf(packet, sizeof(packet), "qWatchpointSupportInfo:"); 1520 assert (packet_len < sizeof(packet)); 1521 StringExtractorGDBRemote response; 1522 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1523 { 1524 m_supports_watchpoint_support_info = eLazyBoolYes; 1525 std::string name; 1526 std::string value; 1527 while (response.GetNameColonValue(name, value)) 1528 { 1529 if (name.compare ("num") == 0) 1530 { 1531 num = Args::StringToUInt32(value.c_str(), 0, 0); 1532 m_num_supported_hardware_watchpoints = num; 1533 } 1534 } 1535 } 1536 else 1537 { 1538 m_supports_watchpoint_support_info = eLazyBoolNo; 1539 } 1540 } 1541 1542 if (m_supports_watchpoint_support_info == eLazyBoolNo) 1543 { 1544 error.SetErrorString("qWatchpointSupportInfo is not supported"); 1545 } 1546 return error; 1547 1548 } 1549 1550 lldb_private::Error 1551 GDBRemoteCommunicationClient::GetWatchpointSupportInfo (uint32_t &num, bool& after) 1552 { 1553 Error error(GetWatchpointSupportInfo(num)); 1554 if (error.Success()) 1555 error = GetWatchpointsTriggerAfterInstruction(after); 1556 return error; 1557 } 1558 1559 lldb_private::Error 1560 GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction (bool &after) 1561 { 1562 Error error; 1563 1564 // we assume watchpoints will happen after running the relevant opcode 1565 // and we only want to override this behavior if we have explicitly 1566 // received a qHostInfo telling us otherwise 1567 if (m_qHostInfo_is_valid != eLazyBoolYes) 1568 after = true; 1569 else 1570 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo); 1571 return error; 1572 } 1573 1574 int 1575 GDBRemoteCommunicationClient::SetSTDIN (char const *path) 1576 { 1577 if (path && path[0]) 1578 { 1579 StreamString packet; 1580 packet.PutCString("QSetSTDIN:"); 1581 packet.PutBytesAsRawHex8(path, strlen(path)); 1582 1583 StringExtractorGDBRemote response; 1584 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1585 { 1586 if (response.IsOKResponse()) 1587 return 0; 1588 uint8_t error = response.GetError(); 1589 if (error) 1590 return error; 1591 } 1592 } 1593 return -1; 1594 } 1595 1596 int 1597 GDBRemoteCommunicationClient::SetSTDOUT (char const *path) 1598 { 1599 if (path && path[0]) 1600 { 1601 StreamString packet; 1602 packet.PutCString("QSetSTDOUT:"); 1603 packet.PutBytesAsRawHex8(path, strlen(path)); 1604 1605 StringExtractorGDBRemote response; 1606 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1607 { 1608 if (response.IsOKResponse()) 1609 return 0; 1610 uint8_t error = response.GetError(); 1611 if (error) 1612 return error; 1613 } 1614 } 1615 return -1; 1616 } 1617 1618 int 1619 GDBRemoteCommunicationClient::SetSTDERR (char const *path) 1620 { 1621 if (path && path[0]) 1622 { 1623 StreamString packet; 1624 packet.PutCString("QSetSTDERR:"); 1625 packet.PutBytesAsRawHex8(path, strlen(path)); 1626 1627 StringExtractorGDBRemote response; 1628 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1629 { 1630 if (response.IsOKResponse()) 1631 return 0; 1632 uint8_t error = response.GetError(); 1633 if (error) 1634 return error; 1635 } 1636 } 1637 return -1; 1638 } 1639 1640 int 1641 GDBRemoteCommunicationClient::SetWorkingDir (char const *path) 1642 { 1643 if (path && path[0]) 1644 { 1645 StreamString packet; 1646 packet.PutCString("QSetWorkingDir:"); 1647 packet.PutBytesAsRawHex8(path, strlen(path)); 1648 1649 StringExtractorGDBRemote response; 1650 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1651 { 1652 if (response.IsOKResponse()) 1653 return 0; 1654 uint8_t error = response.GetError(); 1655 if (error) 1656 return error; 1657 } 1658 } 1659 return -1; 1660 } 1661 1662 int 1663 GDBRemoteCommunicationClient::SetDisableASLR (bool enable) 1664 { 1665 char packet[32]; 1666 const int packet_len = ::snprintf (packet, sizeof (packet), "QSetDisableASLR:%i", enable ? 1 : 0); 1667 assert (packet_len < sizeof(packet)); 1668 StringExtractorGDBRemote response; 1669 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1670 { 1671 if (response.IsOKResponse()) 1672 return 0; 1673 uint8_t error = response.GetError(); 1674 if (error) 1675 return error; 1676 } 1677 return -1; 1678 } 1679 1680 bool 1681 GDBRemoteCommunicationClient::DecodeProcessInfoResponse (StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) 1682 { 1683 if (response.IsNormalResponse()) 1684 { 1685 std::string name; 1686 std::string value; 1687 StringExtractor extractor; 1688 1689 while (response.GetNameColonValue(name, value)) 1690 { 1691 if (name.compare("pid") == 0) 1692 { 1693 process_info.SetProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); 1694 } 1695 else if (name.compare("ppid") == 0) 1696 { 1697 process_info.SetParentProcessID (Args::StringToUInt32 (value.c_str(), LLDB_INVALID_PROCESS_ID, 0)); 1698 } 1699 else if (name.compare("uid") == 0) 1700 { 1701 process_info.SetUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1702 } 1703 else if (name.compare("euid") == 0) 1704 { 1705 process_info.SetEffectiveUserID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1706 } 1707 else if (name.compare("gid") == 0) 1708 { 1709 process_info.SetGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1710 } 1711 else if (name.compare("egid") == 0) 1712 { 1713 process_info.SetEffectiveGroupID (Args::StringToUInt32 (value.c_str(), UINT32_MAX, 0)); 1714 } 1715 else if (name.compare("triple") == 0) 1716 { 1717 // The triple comes as ASCII hex bytes since it contains '-' chars 1718 extractor.GetStringRef().swap(value); 1719 extractor.SetFilePos(0); 1720 extractor.GetHexByteString (value); 1721 process_info.GetArchitecture ().SetTriple (value.c_str()); 1722 } 1723 else if (name.compare("name") == 0) 1724 { 1725 StringExtractor extractor; 1726 // The process name from ASCII hex bytes since we can't 1727 // control the characters in a process name 1728 extractor.GetStringRef().swap(value); 1729 extractor.SetFilePos(0); 1730 extractor.GetHexByteString (value); 1731 process_info.GetExecutableFile().SetFile (value.c_str(), false); 1732 } 1733 } 1734 1735 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) 1736 return true; 1737 } 1738 return false; 1739 } 1740 1741 bool 1742 GDBRemoteCommunicationClient::GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info) 1743 { 1744 process_info.Clear(); 1745 1746 if (m_supports_qProcessInfoPID) 1747 { 1748 char packet[32]; 1749 const int packet_len = ::snprintf (packet, sizeof (packet), "qProcessInfoPID:%" PRIu64, pid); 1750 assert (packet_len < sizeof(packet)); 1751 StringExtractorGDBRemote response; 1752 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1753 { 1754 return DecodeProcessInfoResponse (response, process_info); 1755 } 1756 else 1757 { 1758 m_supports_qProcessInfoPID = false; 1759 return false; 1760 } 1761 } 1762 return false; 1763 } 1764 1765 bool 1766 GDBRemoteCommunicationClient::GetCurrentProcessInfo () 1767 { 1768 if (m_qProcessInfo_is_valid == eLazyBoolYes) 1769 return true; 1770 if (m_qProcessInfo_is_valid == eLazyBoolNo) 1771 return false; 1772 1773 GetHostInfo (); 1774 1775 StringExtractorGDBRemote response; 1776 if (SendPacketAndWaitForResponse ("qProcessInfo", response, false)) 1777 { 1778 if (response.IsNormalResponse()) 1779 { 1780 std::string name; 1781 std::string value; 1782 uint32_t cpu = LLDB_INVALID_CPUTYPE; 1783 uint32_t sub = 0; 1784 std::string arch_name; 1785 std::string os_name; 1786 std::string vendor_name; 1787 std::string triple; 1788 uint32_t pointer_byte_size = 0; 1789 StringExtractor extractor; 1790 ByteOrder byte_order = eByteOrderInvalid; 1791 uint32_t num_keys_decoded = 0; 1792 while (response.GetNameColonValue(name, value)) 1793 { 1794 if (name.compare("cputype") == 0) 1795 { 1796 cpu = Args::StringToUInt32 (value.c_str(), LLDB_INVALID_CPUTYPE, 16); 1797 if (cpu != LLDB_INVALID_CPUTYPE) 1798 ++num_keys_decoded; 1799 } 1800 else if (name.compare("cpusubtype") == 0) 1801 { 1802 sub = Args::StringToUInt32 (value.c_str(), 0, 16); 1803 if (sub != 0) 1804 ++num_keys_decoded; 1805 } 1806 else if (name.compare("ostype") == 0) 1807 { 1808 os_name.swap (value); 1809 ++num_keys_decoded; 1810 } 1811 else if (name.compare("vendor") == 0) 1812 { 1813 vendor_name.swap(value); 1814 ++num_keys_decoded; 1815 } 1816 else if (name.compare("endian") == 0) 1817 { 1818 ++num_keys_decoded; 1819 if (value.compare("little") == 0) 1820 byte_order = eByteOrderLittle; 1821 else if (value.compare("big") == 0) 1822 byte_order = eByteOrderBig; 1823 else if (value.compare("pdp") == 0) 1824 byte_order = eByteOrderPDP; 1825 else 1826 --num_keys_decoded; 1827 } 1828 else if (name.compare("ptrsize") == 0) 1829 { 1830 pointer_byte_size = Args::StringToUInt32 (value.c_str(), 0, 16); 1831 if (pointer_byte_size != 0) 1832 ++num_keys_decoded; 1833 } 1834 } 1835 if (num_keys_decoded > 0) 1836 m_qProcessInfo_is_valid = eLazyBoolYes; 1837 if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && !vendor_name.empty()) 1838 { 1839 m_process_arch.SetArchitecture (eArchTypeMachO, cpu, sub); 1840 if (pointer_byte_size) 1841 { 1842 assert (pointer_byte_size == m_process_arch.GetAddressByteSize()); 1843 } 1844 m_host_arch.GetTriple().setVendorName (llvm::StringRef (vendor_name)); 1845 m_host_arch.GetTriple().setOSName (llvm::StringRef (os_name)); 1846 return true; 1847 } 1848 } 1849 } 1850 else 1851 { 1852 m_qProcessInfo_is_valid = eLazyBoolNo; 1853 } 1854 1855 return false; 1856 } 1857 1858 1859 uint32_t 1860 GDBRemoteCommunicationClient::FindProcesses (const ProcessInstanceInfoMatch &match_info, 1861 ProcessInstanceInfoList &process_infos) 1862 { 1863 process_infos.Clear(); 1864 1865 if (m_supports_qfProcessInfo) 1866 { 1867 StreamString packet; 1868 packet.PutCString ("qfProcessInfo"); 1869 if (!match_info.MatchAllProcesses()) 1870 { 1871 packet.PutChar (':'); 1872 const char *name = match_info.GetProcessInfo().GetName(); 1873 bool has_name_match = false; 1874 if (name && name[0]) 1875 { 1876 has_name_match = true; 1877 NameMatchType name_match_type = match_info.GetNameMatchType(); 1878 switch (name_match_type) 1879 { 1880 case eNameMatchIgnore: 1881 has_name_match = false; 1882 break; 1883 1884 case eNameMatchEquals: 1885 packet.PutCString ("name_match:equals;"); 1886 break; 1887 1888 case eNameMatchContains: 1889 packet.PutCString ("name_match:contains;"); 1890 break; 1891 1892 case eNameMatchStartsWith: 1893 packet.PutCString ("name_match:starts_with;"); 1894 break; 1895 1896 case eNameMatchEndsWith: 1897 packet.PutCString ("name_match:ends_with;"); 1898 break; 1899 1900 case eNameMatchRegularExpression: 1901 packet.PutCString ("name_match:regex;"); 1902 break; 1903 } 1904 if (has_name_match) 1905 { 1906 packet.PutCString ("name:"); 1907 packet.PutBytesAsRawHex8(name, ::strlen(name)); 1908 packet.PutChar (';'); 1909 } 1910 } 1911 1912 if (match_info.GetProcessInfo().ProcessIDIsValid()) 1913 packet.Printf("pid:%" PRIu64 ";",match_info.GetProcessInfo().GetProcessID()); 1914 if (match_info.GetProcessInfo().ParentProcessIDIsValid()) 1915 packet.Printf("parent_pid:%" PRIu64 ";",match_info.GetProcessInfo().GetParentProcessID()); 1916 if (match_info.GetProcessInfo().UserIDIsValid()) 1917 packet.Printf("uid:%u;",match_info.GetProcessInfo().GetUserID()); 1918 if (match_info.GetProcessInfo().GroupIDIsValid()) 1919 packet.Printf("gid:%u;",match_info.GetProcessInfo().GetGroupID()); 1920 if (match_info.GetProcessInfo().EffectiveUserIDIsValid()) 1921 packet.Printf("euid:%u;",match_info.GetProcessInfo().GetEffectiveUserID()); 1922 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) 1923 packet.Printf("egid:%u;",match_info.GetProcessInfo().GetEffectiveGroupID()); 1924 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) 1925 packet.Printf("all_users:%u;",match_info.GetMatchAllUsers() ? 1 : 0); 1926 if (match_info.GetProcessInfo().GetArchitecture().IsValid()) 1927 { 1928 const ArchSpec &match_arch = match_info.GetProcessInfo().GetArchitecture(); 1929 const llvm::Triple &triple = match_arch.GetTriple(); 1930 packet.PutCString("triple:"); 1931 packet.PutCStringAsRawHex8(triple.getTriple().c_str()); 1932 packet.PutChar (';'); 1933 } 1934 } 1935 StringExtractorGDBRemote response; 1936 if (SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false)) 1937 { 1938 do 1939 { 1940 ProcessInstanceInfo process_info; 1941 if (!DecodeProcessInfoResponse (response, process_info)) 1942 break; 1943 process_infos.Append(process_info); 1944 response.GetStringRef().clear(); 1945 response.SetFilePos(0); 1946 } while (SendPacketAndWaitForResponse ("qsProcessInfo", strlen ("qsProcessInfo"), response, false)); 1947 } 1948 else 1949 { 1950 m_supports_qfProcessInfo = false; 1951 return 0; 1952 } 1953 } 1954 return process_infos.GetSize(); 1955 1956 } 1957 1958 bool 1959 GDBRemoteCommunicationClient::GetUserName (uint32_t uid, std::string &name) 1960 { 1961 if (m_supports_qUserName) 1962 { 1963 char packet[32]; 1964 const int packet_len = ::snprintf (packet, sizeof (packet), "qUserName:%i", uid); 1965 assert (packet_len < sizeof(packet)); 1966 StringExtractorGDBRemote response; 1967 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1968 { 1969 if (response.IsNormalResponse()) 1970 { 1971 // Make sure we parsed the right number of characters. The response is 1972 // the hex encoded user name and should make up the entire packet. 1973 // If there are any non-hex ASCII bytes, the length won't match below.. 1974 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) 1975 return true; 1976 } 1977 } 1978 else 1979 { 1980 m_supports_qUserName = false; 1981 return false; 1982 } 1983 } 1984 return false; 1985 1986 } 1987 1988 bool 1989 GDBRemoteCommunicationClient::GetGroupName (uint32_t gid, std::string &name) 1990 { 1991 if (m_supports_qGroupName) 1992 { 1993 char packet[32]; 1994 const int packet_len = ::snprintf (packet, sizeof (packet), "qGroupName:%i", gid); 1995 assert (packet_len < sizeof(packet)); 1996 StringExtractorGDBRemote response; 1997 if (SendPacketAndWaitForResponse (packet, packet_len, response, false)) 1998 { 1999 if (response.IsNormalResponse()) 2000 { 2001 // Make sure we parsed the right number of characters. The response is 2002 // the hex encoded group name and should make up the entire packet. 2003 // If there are any non-hex ASCII bytes, the length won't match below.. 2004 if (response.GetHexByteString (name) * 2 == response.GetStringRef().size()) 2005 return true; 2006 } 2007 } 2008 else 2009 { 2010 m_supports_qGroupName = false; 2011 return false; 2012 } 2013 } 2014 return false; 2015 } 2016 2017 void 2018 GDBRemoteCommunicationClient::TestPacketSpeed (const uint32_t num_packets) 2019 { 2020 uint32_t i; 2021 TimeValue start_time, end_time; 2022 uint64_t total_time_nsec; 2023 float packets_per_second; 2024 if (SendSpeedTestPacket (0, 0)) 2025 { 2026 for (uint32_t send_size = 0; send_size <= 1024; send_size *= 2) 2027 { 2028 for (uint32_t recv_size = 0; recv_size <= 1024; recv_size *= 2) 2029 { 2030 start_time = TimeValue::Now(); 2031 for (i=0; i<num_packets; ++i) 2032 { 2033 SendSpeedTestPacket (send_size, recv_size); 2034 } 2035 end_time = TimeValue::Now(); 2036 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); 2037 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; 2038 printf ("%u qSpeedTest(send=%-5u, recv=%-5u) in %" PRIu64 ".%9.9" PRIu64 " sec for %f packets/sec.\n", 2039 num_packets, 2040 send_size, 2041 recv_size, 2042 total_time_nsec / TimeValue::NanoSecPerSec, 2043 total_time_nsec % TimeValue::NanoSecPerSec, 2044 packets_per_second); 2045 if (recv_size == 0) 2046 recv_size = 32; 2047 } 2048 if (send_size == 0) 2049 send_size = 32; 2050 } 2051 } 2052 else 2053 { 2054 start_time = TimeValue::Now(); 2055 for (i=0; i<num_packets; ++i) 2056 { 2057 GetCurrentProcessID (); 2058 } 2059 end_time = TimeValue::Now(); 2060 total_time_nsec = end_time.GetAsNanoSecondsSinceJan1_1970() - start_time.GetAsNanoSecondsSinceJan1_1970(); 2061 packets_per_second = (((float)num_packets)/(float)total_time_nsec) * (float)TimeValue::NanoSecPerSec; 2062 printf ("%u 'qC' packets packets in 0x%" PRIu64 "%9.9" PRIu64 " sec for %f packets/sec.\n", 2063 num_packets, 2064 total_time_nsec / TimeValue::NanoSecPerSec, 2065 total_time_nsec % TimeValue::NanoSecPerSec, 2066 packets_per_second); 2067 } 2068 } 2069 2070 bool 2071 GDBRemoteCommunicationClient::SendSpeedTestPacket (uint32_t send_size, uint32_t recv_size) 2072 { 2073 StreamString packet; 2074 packet.Printf ("qSpeedTest:response_size:%i;data:", recv_size); 2075 uint32_t bytes_left = send_size; 2076 while (bytes_left > 0) 2077 { 2078 if (bytes_left >= 26) 2079 { 2080 packet.PutCString("abcdefghijklmnopqrstuvwxyz"); 2081 bytes_left -= 26; 2082 } 2083 else 2084 { 2085 packet.Printf ("%*.*s;", bytes_left, bytes_left, "abcdefghijklmnopqrstuvwxyz"); 2086 bytes_left = 0; 2087 } 2088 } 2089 2090 StringExtractorGDBRemote response; 2091 return SendPacketAndWaitForResponse (packet.GetData(), packet.GetSize(), response, false) > 0; 2092 return false; 2093 } 2094 2095 uint16_t 2096 GDBRemoteCommunicationClient::LaunchGDBserverAndGetPort () 2097 { 2098 StringExtractorGDBRemote response; 2099 if (SendPacketAndWaitForResponse("qLaunchGDBServer", strlen("qLaunchGDBServer"), response, false)) 2100 { 2101 std::string name; 2102 std::string value; 2103 uint16_t port = 0; 2104 //lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 2105 while (response.GetNameColonValue(name, value)) 2106 { 2107 if (name.size() == 4 && name.compare("port") == 0) 2108 port = Args::StringToUInt32(value.c_str(), 0, 0); 2109 // if (name.size() == 3 && name.compare("pid") == 0) 2110 // pid = Args::StringToUInt32(value.c_str(), LLDB_INVALID_PROCESS_ID, 0); 2111 } 2112 return port; 2113 } 2114 return 0; 2115 } 2116 2117 bool 2118 GDBRemoteCommunicationClient::SetCurrentThread (uint64_t tid) 2119 { 2120 if (m_curr_tid == tid) 2121 return true; 2122 2123 char packet[32]; 2124 int packet_len; 2125 if (tid == UINT64_MAX) 2126 packet_len = ::snprintf (packet, sizeof(packet), "Hg-1"); 2127 else 2128 packet_len = ::snprintf (packet, sizeof(packet), "Hg%" PRIx64, tid); 2129 assert (packet_len + 1 < sizeof(packet)); 2130 StringExtractorGDBRemote response; 2131 if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) 2132 { 2133 if (response.IsOKResponse()) 2134 { 2135 m_curr_tid = tid; 2136 return true; 2137 } 2138 } 2139 return false; 2140 } 2141 2142 bool 2143 GDBRemoteCommunicationClient::SetCurrentThreadForRun (uint64_t tid) 2144 { 2145 if (m_curr_tid_run == tid) 2146 return true; 2147 2148 char packet[32]; 2149 int packet_len; 2150 if (tid == UINT64_MAX) 2151 packet_len = ::snprintf (packet, sizeof(packet), "Hc-1"); 2152 else 2153 packet_len = ::snprintf (packet, sizeof(packet), "Hc%" PRIx64, tid); 2154 2155 assert (packet_len + 1 < sizeof(packet)); 2156 StringExtractorGDBRemote response; 2157 if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) 2158 { 2159 if (response.IsOKResponse()) 2160 { 2161 m_curr_tid_run = tid; 2162 return true; 2163 } 2164 } 2165 return false; 2166 } 2167 2168 bool 2169 GDBRemoteCommunicationClient::GetStopReply (StringExtractorGDBRemote &response) 2170 { 2171 if (SendPacketAndWaitForResponse("?", 1, response, false)) 2172 return response.IsNormalResponse(); 2173 return false; 2174 } 2175 2176 bool 2177 GDBRemoteCommunicationClient::GetThreadStopInfo (lldb::tid_t tid, StringExtractorGDBRemote &response) 2178 { 2179 if (m_supports_qThreadStopInfo) 2180 { 2181 char packet[256]; 2182 int packet_len = ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid); 2183 assert (packet_len < sizeof(packet)); 2184 if (SendPacketAndWaitForResponse(packet, packet_len, response, false)) 2185 { 2186 if (response.IsNormalResponse()) 2187 return true; 2188 else 2189 return false; 2190 } 2191 else 2192 { 2193 m_supports_qThreadStopInfo = false; 2194 } 2195 } 2196 // if (SetCurrentThread (tid)) 2197 // return GetStopReply (response); 2198 return false; 2199 } 2200 2201 2202 uint8_t 2203 GDBRemoteCommunicationClient::SendGDBStoppointTypePacket (GDBStoppointType type, bool insert, addr_t addr, uint32_t length) 2204 { 2205 switch (type) 2206 { 2207 case eBreakpointSoftware: if (!m_supports_z0) return UINT8_MAX; break; 2208 case eBreakpointHardware: if (!m_supports_z1) return UINT8_MAX; break; 2209 case eWatchpointWrite: if (!m_supports_z2) return UINT8_MAX; break; 2210 case eWatchpointRead: if (!m_supports_z3) return UINT8_MAX; break; 2211 case eWatchpointReadWrite: if (!m_supports_z4) return UINT8_MAX; break; 2212 } 2213 2214 char packet[64]; 2215 const int packet_len = ::snprintf (packet, 2216 sizeof(packet), 2217 "%c%i,%" PRIx64 ",%x", 2218 insert ? 'Z' : 'z', 2219 type, 2220 addr, 2221 length); 2222 2223 assert (packet_len + 1 < sizeof(packet)); 2224 StringExtractorGDBRemote response; 2225 if (SendPacketAndWaitForResponse(packet, packet_len, response, true)) 2226 { 2227 if (response.IsOKResponse()) 2228 return 0; 2229 else if (response.IsErrorResponse()) 2230 return response.GetError(); 2231 } 2232 else 2233 { 2234 switch (type) 2235 { 2236 case eBreakpointSoftware: m_supports_z0 = false; break; 2237 case eBreakpointHardware: m_supports_z1 = false; break; 2238 case eWatchpointWrite: m_supports_z2 = false; break; 2239 case eWatchpointRead: m_supports_z3 = false; break; 2240 case eWatchpointReadWrite: m_supports_z4 = false; break; 2241 } 2242 } 2243 2244 return UINT8_MAX; 2245 } 2246 2247 size_t 2248 GDBRemoteCommunicationClient::GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids, 2249 bool &sequence_mutex_unavailable) 2250 { 2251 Mutex::Locker locker; 2252 thread_ids.clear(); 2253 2254 if (GetSequenceMutex (locker, "ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex")) 2255 { 2256 sequence_mutex_unavailable = false; 2257 StringExtractorGDBRemote response; 2258 2259 for (SendPacketNoLock ("qfThreadInfo", strlen("qfThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ()); 2260 response.IsNormalResponse(); 2261 SendPacketNoLock ("qsThreadInfo", strlen("qsThreadInfo")) && WaitForPacketWithTimeoutMicroSecondsNoLock (response, GetPacketTimeoutInMicroSeconds ())) 2262 { 2263 char ch = response.GetChar(); 2264 if (ch == 'l') 2265 break; 2266 if (ch == 'm') 2267 { 2268 do 2269 { 2270 tid_t tid = response.GetHexMaxU64(false, LLDB_INVALID_THREAD_ID); 2271 2272 if (tid != LLDB_INVALID_THREAD_ID) 2273 { 2274 thread_ids.push_back (tid); 2275 } 2276 ch = response.GetChar(); // Skip the command separator 2277 } while (ch == ','); // Make sure we got a comma separator 2278 } 2279 } 2280 } 2281 else 2282 { 2283 #if defined (LLDB_CONFIGURATION_DEBUG) 2284 // assert(!"ProcessGDBRemote::UpdateThreadList() failed due to not getting the sequence mutex"); 2285 #else 2286 Log *log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_PROCESS | GDBR_LOG_PACKETS)); 2287 if (log) 2288 log->Printf("error: failed to get packet sequence mutex, not sending packet 'qfThreadInfo'"); 2289 #endif 2290 sequence_mutex_unavailable = true; 2291 } 2292 return thread_ids.size(); 2293 } 2294 2295 lldb::addr_t 2296 GDBRemoteCommunicationClient::GetShlibInfoAddr() 2297 { 2298 if (!IsRunning()) 2299 { 2300 StringExtractorGDBRemote response; 2301 if (SendPacketAndWaitForResponse("qShlibInfoAddr", ::strlen ("qShlibInfoAddr"), response, false)) 2302 { 2303 if (response.IsNormalResponse()) 2304 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 2305 } 2306 } 2307 return LLDB_INVALID_ADDRESS; 2308 } 2309 2310