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