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