1 //===-- GDBRemoteCommunicationClient.h --------------------------*- 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 #ifndef liblldb_GDBRemoteCommunicationClient_h_ 11 #define liblldb_GDBRemoteCommunicationClient_h_ 12 13 // C Includes 14 // C++ Includes 15 #include <vector> 16 17 // Other libraries and framework includes 18 // Project includes 19 #include "lldb/Core/ArchSpec.h" 20 #include "lldb/Core/StructuredData.h" 21 #include "lldb/Target/Process.h" 22 23 #include "GDBRemoteCommunication.h" 24 25 namespace lldb_private { 26 namespace process_gdb_remote { 27 28 class GDBRemoteCommunicationClient : public GDBRemoteCommunication 29 { 30 public: 31 //------------------------------------------------------------------ 32 // Constructors and Destructors 33 //------------------------------------------------------------------ 34 GDBRemoteCommunicationClient(); 35 36 ~GDBRemoteCommunicationClient(); 37 38 //------------------------------------------------------------------ 39 // After connecting, send the handshake to the server to make sure 40 // we are communicating with it. 41 //------------------------------------------------------------------ 42 bool 43 HandshakeWithServer (Error *error_ptr); 44 45 PacketResult 46 SendPacketAndWaitForResponse (const char *send_payload, 47 StringExtractorGDBRemote &response, 48 bool send_async); 49 50 PacketResult 51 SendPacketAndWaitForResponse (const char *send_payload, 52 size_t send_length, 53 StringExtractorGDBRemote &response, 54 bool send_async); 55 56 // For packets which specify a range of output to be returned, 57 // return all of the output via a series of request packets of the form 58 // <prefix>0,<size> 59 // <prefix><size>,<size> 60 // <prefix><size>*2,<size> 61 // <prefix><size>*3,<size> 62 // ... 63 // until a "$l..." packet is received, indicating the end. 64 // (size is in hex; this format is used by a standard gdbserver to 65 // return the given portion of the output specified by <prefix>; 66 // for example, "qXfer:libraries-svr4:read::fff,1000" means 67 // "return a chunk of the xml description file for shared 68 // library load addresses, where the chunk starts at offset 0xfff 69 // and continues for 0x1000 bytes"). 70 // Concatenate the resulting server response packets together and 71 // return in response_string. If any packet fails, the return value 72 // indicates that failure and the returned string value is undefined. 73 PacketResult 74 SendPacketsAndConcatenateResponses (const char *send_payload_prefix, 75 std::string &response_string); 76 77 lldb::StateType 78 SendContinuePacketAndWaitForResponse (ProcessGDBRemote *process, 79 const char *packet_payload, 80 size_t packet_length, 81 StringExtractorGDBRemote &response); 82 bool 83 SendvContPacket (ProcessGDBRemote *process, 84 const char *payload, 85 size_t packet_length, 86 StringExtractorGDBRemote &response); 87 88 bool 89 GetThreadSuffixSupported () override; 90 91 // This packet is usually sent first and the boolean return value 92 // indicates if the packet was send and any response was received 93 // even in the response is UNIMPLEMENTED. If the packet failed to 94 // get a response, then false is returned. This quickly tells us 95 // if we were able to connect and communicate with the remote GDB 96 // server 97 bool 98 QueryNoAckModeSupported (); 99 100 void 101 GetListThreadsInStopReplySupported (); 102 103 bool 104 SendAsyncSignal (int signo); 105 106 bool 107 SendInterrupt (Mutex::Locker &locker, 108 uint32_t seconds_to_wait_for_stop, 109 bool &timed_out); 110 111 lldb::pid_t 112 GetCurrentProcessID (bool allow_lazy = true); 113 114 bool 115 GetLaunchSuccess (std::string &error_str); 116 117 uint16_t 118 LaunchGDBserverAndGetPort (lldb::pid_t &pid, const char *remote_accept_hostname); 119 120 bool 121 KillSpawnedProcess (lldb::pid_t pid); 122 123 //------------------------------------------------------------------ 124 /// Sends a GDB remote protocol 'A' packet that delivers program 125 /// arguments to the remote server. 126 /// 127 /// @param[in] argv 128 /// A NULL terminated array of const C strings to use as the 129 /// arguments. 130 /// 131 /// @return 132 /// Zero if the response was "OK", a positive value if the 133 /// the response was "Exx" where xx are two hex digits, or 134 /// -1 if the call is unsupported or any other unexpected 135 /// response was received. 136 //------------------------------------------------------------------ 137 int 138 SendArgumentsPacket (const ProcessLaunchInfo &launch_info); 139 140 //------------------------------------------------------------------ 141 /// Sends a "QEnvironment:NAME=VALUE" packet that will build up the 142 /// environment that will get used when launching an application 143 /// in conjunction with the 'A' packet. This function can be called 144 /// multiple times in a row in order to pass on the desired 145 /// environment that the inferior should be launched with. 146 /// 147 /// @param[in] name_equal_value 148 /// A NULL terminated C string that contains a single environment 149 /// in the format "NAME=VALUE". 150 /// 151 /// @return 152 /// Zero if the response was "OK", a positive value if the 153 /// the response was "Exx" where xx are two hex digits, or 154 /// -1 if the call is unsupported or any other unexpected 155 /// response was received. 156 //------------------------------------------------------------------ 157 int 158 SendEnvironmentPacket (char const *name_equal_value); 159 160 int 161 SendLaunchArchPacket (const char *arch); 162 163 int 164 SendLaunchEventDataPacket (const char *data, bool *was_supported = NULL); 165 166 //------------------------------------------------------------------ 167 /// Sends a "vAttach:PID" where PID is in hex. 168 /// 169 /// @param[in] pid 170 /// A process ID for the remote gdb server to attach to. 171 /// 172 /// @param[out] response 173 /// The response received from the gdb server. If the return 174 /// value is zero, \a response will contain a stop reply 175 /// packet. 176 /// 177 /// @return 178 /// Zero if the attach was successful, or an error indicating 179 /// an error code. 180 //------------------------------------------------------------------ 181 int 182 SendAttach (lldb::pid_t pid, 183 StringExtractorGDBRemote& response); 184 185 186 //------------------------------------------------------------------ 187 /// Sends a GDB remote protocol 'I' packet that delivers stdin 188 /// data to the remote process. 189 /// 190 /// @param[in] data 191 /// A pointer to stdin data. 192 /// 193 /// @param[in] data_len 194 /// The number of bytes available at \a data. 195 /// 196 /// @return 197 /// Zero if the attach was successful, or an error indicating 198 /// an error code. 199 //------------------------------------------------------------------ 200 int 201 SendStdinNotification(const char* data, size_t data_len); 202 203 //------------------------------------------------------------------ 204 /// Sets the path to use for stdin/out/err for a process 205 /// that will be launched with the 'A' packet. 206 /// 207 /// @param[in] path 208 /// The path to use for stdin/out/err 209 /// 210 /// @return 211 /// Zero if the for success, or an error code for failure. 212 //------------------------------------------------------------------ 213 int 214 SetSTDIN(const FileSpec &file_spec); 215 int 216 SetSTDOUT(const FileSpec &file_spec); 217 int 218 SetSTDERR(const FileSpec &file_spec); 219 220 //------------------------------------------------------------------ 221 /// Sets the disable ASLR flag to \a enable for a process that will 222 /// be launched with the 'A' packet. 223 /// 224 /// @param[in] enable 225 /// A boolean value indicating whether to disable ASLR or not. 226 /// 227 /// @return 228 /// Zero if the for success, or an error code for failure. 229 //------------------------------------------------------------------ 230 int 231 SetDisableASLR (bool enable); 232 233 //------------------------------------------------------------------ 234 /// Sets the DetachOnError flag to \a enable for the process controlled by the stub. 235 /// 236 /// @param[in] enable 237 /// A boolean value indicating whether to detach on error or not. 238 /// 239 /// @return 240 /// Zero if the for success, or an error code for failure. 241 //------------------------------------------------------------------ 242 int 243 SetDetachOnError (bool enable); 244 245 //------------------------------------------------------------------ 246 /// Sets the working directory to \a path for a process that will 247 /// be launched with the 'A' packet for non platform based 248 /// connections. If this packet is sent to a GDB server that 249 /// implements the platform, it will change the current working 250 /// directory for the platform process. 251 /// 252 /// @param[in] working_dir 253 /// The path to a directory to use when launching our process 254 /// 255 /// @return 256 /// Zero if the for success, or an error code for failure. 257 //------------------------------------------------------------------ 258 int 259 SetWorkingDir(const FileSpec &working_dir); 260 261 //------------------------------------------------------------------ 262 /// Gets the current working directory of a remote platform GDB 263 /// server. 264 /// 265 /// @param[out] working_dir 266 /// The current working directory on the remote platform. 267 /// 268 /// @return 269 /// Boolean for success 270 //------------------------------------------------------------------ 271 bool 272 GetWorkingDir(FileSpec &working_dir); 273 274 lldb::addr_t 275 AllocateMemory (size_t size, uint32_t permissions); 276 277 bool 278 DeallocateMemory (lldb::addr_t addr); 279 280 Error 281 Detach (bool keep_stopped); 282 283 Error 284 GetMemoryRegionInfo (lldb::addr_t addr, MemoryRegionInfo &range_info); 285 286 Error 287 GetWatchpointSupportInfo (uint32_t &num); 288 289 Error 290 GetWatchpointSupportInfo (uint32_t &num, bool& after); 291 292 Error 293 GetWatchpointsTriggerAfterInstruction (bool &after); 294 295 const ArchSpec & 296 GetHostArchitecture (); 297 298 uint32_t 299 GetHostDefaultPacketTimeout(); 300 301 const ArchSpec & 302 GetProcessArchitecture (); 303 304 void 305 GetRemoteQSupported(); 306 307 bool 308 GetVContSupported (char flavor); 309 310 bool 311 GetpPacketSupported (lldb::tid_t tid); 312 313 bool 314 GetxPacketSupported (); 315 316 bool 317 GetVAttachOrWaitSupported (); 318 319 bool 320 GetSyncThreadStateSupported(); 321 322 void 323 ResetDiscoverableSettings (bool did_exec); 324 325 bool 326 GetHostInfo (bool force = false); 327 328 bool 329 GetDefaultThreadId (lldb::tid_t &tid); 330 331 bool 332 GetOSVersion (uint32_t &major, 333 uint32_t &minor, 334 uint32_t &update); 335 336 bool 337 GetOSBuildString (std::string &s); 338 339 bool 340 GetOSKernelDescription (std::string &s); 341 342 ArchSpec 343 GetSystemArchitecture (); 344 345 bool 346 GetHostname (std::string &s); 347 348 lldb::addr_t 349 GetShlibInfoAddr(); 350 351 bool 352 GetSupportsThreadSuffix (); 353 354 bool 355 GetProcessInfo (lldb::pid_t pid, ProcessInstanceInfo &process_info); 356 357 uint32_t 358 FindProcesses (const ProcessInstanceInfoMatch &process_match_info, 359 ProcessInstanceInfoList &process_infos); 360 361 bool 362 GetUserName (uint32_t uid, std::string &name); 363 364 bool 365 GetGroupName (uint32_t gid, std::string &name); 366 367 bool 368 HasFullVContSupport () 369 { 370 return GetVContSupported ('A'); 371 } 372 373 bool 374 HasAnyVContSupport () 375 { 376 return GetVContSupported ('a'); 377 } 378 379 bool 380 GetStopReply (StringExtractorGDBRemote &response); 381 382 bool 383 GetThreadStopInfo (lldb::tid_t tid, 384 StringExtractorGDBRemote &response); 385 386 bool 387 SupportsGDBStoppointPacket (GDBStoppointType type) 388 { 389 switch (type) 390 { 391 case eBreakpointSoftware: return m_supports_z0; 392 case eBreakpointHardware: return m_supports_z1; 393 case eWatchpointWrite: return m_supports_z2; 394 case eWatchpointRead: return m_supports_z3; 395 case eWatchpointReadWrite: return m_supports_z4; 396 default: return false; 397 } 398 } 399 uint8_t 400 SendGDBStoppointTypePacket (GDBStoppointType type, // Type of breakpoint or watchpoint 401 bool insert, // Insert or remove? 402 lldb::addr_t addr, // Address of breakpoint or watchpoint 403 uint32_t length); // Byte Size of breakpoint or watchpoint 404 405 bool 406 SetNonStopMode (const bool enable); 407 408 void 409 TestPacketSpeed (const uint32_t num_packets, uint32_t max_send, uint32_t max_recv, bool json, Stream &strm); 410 411 // This packet is for testing the speed of the interface only. Both 412 // the client and server need to support it, but this allows us to 413 // measure the packet speed without any other work being done on the 414 // other end and avoids any of that work affecting the packet send 415 // and response times. 416 bool 417 SendSpeedTestPacket (uint32_t send_size, 418 uint32_t recv_size); 419 420 bool 421 SetCurrentThread (uint64_t tid); 422 423 bool 424 SetCurrentThreadForRun (uint64_t tid); 425 426 bool 427 GetQXferAuxvReadSupported (); 428 429 bool 430 GetQXferLibrariesReadSupported (); 431 432 bool 433 GetQXferLibrariesSVR4ReadSupported (); 434 435 uint64_t 436 GetRemoteMaxPacketSize(); 437 438 bool 439 GetEchoSupported (); 440 441 bool 442 GetAugmentedLibrariesSVR4ReadSupported (); 443 444 bool 445 GetQXferFeaturesReadSupported (); 446 447 LazyBool 448 SupportsAllocDeallocMemory () // const 449 { 450 // Uncomment this to have lldb pretend the debug server doesn't respond to alloc/dealloc memory packets. 451 // m_supports_alloc_dealloc_memory = lldb_private::eLazyBoolNo; 452 return m_supports_alloc_dealloc_memory; 453 } 454 455 size_t 456 GetCurrentThreadIDs (std::vector<lldb::tid_t> &thread_ids, 457 bool &sequence_mutex_unavailable); 458 459 bool 460 GetInterruptWasSent () const 461 { 462 return m_interrupt_sent; 463 } 464 465 lldb::user_id_t 466 OpenFile (const FileSpec& file_spec, uint32_t flags, mode_t mode, Error &error); 467 468 bool 469 CloseFile (lldb::user_id_t fd, Error &error); 470 471 lldb::user_id_t 472 GetFileSize (const FileSpec& file_spec); 473 474 Error 475 GetFilePermissions(const FileSpec &file_spec, uint32_t &file_permissions); 476 477 Error 478 SetFilePermissions(const FileSpec &file_spec, uint32_t file_permissions); 479 480 uint64_t 481 ReadFile (lldb::user_id_t fd, 482 uint64_t offset, 483 void *dst, 484 uint64_t dst_len, 485 Error &error); 486 487 uint64_t 488 WriteFile (lldb::user_id_t fd, 489 uint64_t offset, 490 const void* src, 491 uint64_t src_len, 492 Error &error); 493 494 Error 495 CreateSymlink(const FileSpec &src, 496 const FileSpec &dst); 497 498 Error 499 Unlink(const FileSpec &file_spec); 500 501 Error 502 MakeDirectory(const FileSpec &file_spec, uint32_t mode); 503 504 bool 505 GetFileExists (const FileSpec& file_spec); 506 507 Error 508 RunShellCommand(const char *command, // Shouldn't be NULL 509 const FileSpec &working_dir, // Pass empty FileSpec to use the current working directory 510 int *status_ptr, // Pass NULL if you don't want the process exit status 511 int *signo_ptr, // Pass NULL if you don't want the signal that caused the process to exit 512 std::string *command_output, // Pass NULL if you don't want the command output 513 uint32_t timeout_sec); // Timeout in seconds to wait for shell program to finish 514 515 bool 516 CalculateMD5 (const FileSpec& file_spec, uint64_t &high, uint64_t &low); 517 518 std::string 519 HarmonizeThreadIdsForProfileData (ProcessGDBRemote *process, 520 StringExtractorGDBRemote &inputStringExtractor); 521 522 bool 523 ReadRegister(lldb::tid_t tid, 524 uint32_t reg_num, 525 StringExtractorGDBRemote &response); 526 527 bool 528 ReadAllRegisters (lldb::tid_t tid, 529 StringExtractorGDBRemote &response); 530 531 bool 532 SaveRegisterState (lldb::tid_t tid, uint32_t &save_id); 533 534 bool 535 RestoreRegisterState (lldb::tid_t tid, uint32_t save_id); 536 537 const char * 538 GetGDBServerProgramName(); 539 540 uint32_t 541 GetGDBServerProgramVersion(); 542 543 bool 544 AvoidGPackets(ProcessGDBRemote *process); 545 546 StructuredData::ObjectSP 547 GetThreadsInfo(); 548 549 bool 550 GetThreadExtendedInfoSupported(); 551 552 bool 553 GetLoadedDynamicLibrariesInfosSupported(); 554 555 bool 556 GetModuleInfo (const FileSpec& module_file_spec, 557 const ArchSpec& arch_spec, 558 ModuleSpec &module_spec); 559 560 bool 561 ReadExtFeature (const lldb_private::ConstString object, 562 const lldb_private::ConstString annex, 563 std::string & out, 564 lldb_private::Error & err); 565 566 void 567 ServeSymbolLookups(lldb_private::Process *process); 568 569 protected: 570 571 PacketResult 572 SendPacketAndWaitForResponseNoLock (const char *payload, 573 size_t payload_length, 574 StringExtractorGDBRemote &response); 575 576 bool 577 GetCurrentProcessInfo (bool allow_lazy_pid = true); 578 579 bool 580 GetGDBServerVersion(); 581 582 // Given the list of compression types that the remote debug stub can support, 583 // possibly enable compression if we find an encoding we can handle. 584 void 585 MaybeEnableCompression (std::vector<std::string> supported_compressions); 586 587 //------------------------------------------------------------------ 588 // Classes that inherit from GDBRemoteCommunicationClient can see and modify these 589 //------------------------------------------------------------------ 590 LazyBool m_supports_not_sending_acks; 591 LazyBool m_supports_thread_suffix; 592 LazyBool m_supports_threads_in_stop_reply; 593 LazyBool m_supports_vCont_all; 594 LazyBool m_supports_vCont_any; 595 LazyBool m_supports_vCont_c; 596 LazyBool m_supports_vCont_C; 597 LazyBool m_supports_vCont_s; 598 LazyBool m_supports_vCont_S; 599 LazyBool m_qHostInfo_is_valid; 600 LazyBool m_curr_pid_is_valid; 601 LazyBool m_qProcessInfo_is_valid; 602 LazyBool m_qGDBServerVersion_is_valid; 603 LazyBool m_supports_alloc_dealloc_memory; 604 LazyBool m_supports_memory_region_info; 605 LazyBool m_supports_watchpoint_support_info; 606 LazyBool m_supports_detach_stay_stopped; 607 LazyBool m_watchpoints_trigger_after_instruction; 608 LazyBool m_attach_or_wait_reply; 609 LazyBool m_prepare_for_reg_writing_reply; 610 LazyBool m_supports_p; 611 LazyBool m_supports_x; 612 LazyBool m_avoid_g_packets; 613 LazyBool m_supports_QSaveRegisterState; 614 LazyBool m_supports_qXfer_auxv_read; 615 LazyBool m_supports_qXfer_libraries_read; 616 LazyBool m_supports_qXfer_libraries_svr4_read; 617 LazyBool m_supports_qXfer_features_read; 618 LazyBool m_supports_augmented_libraries_svr4_read; 619 LazyBool m_supports_jThreadExtendedInfo; 620 LazyBool m_supports_jLoadedDynamicLibrariesInfos; 621 622 bool 623 m_supports_qProcessInfoPID:1, 624 m_supports_qfProcessInfo:1, 625 m_supports_qUserName:1, 626 m_supports_qGroupName:1, 627 m_supports_qThreadStopInfo:1, 628 m_supports_z0:1, 629 m_supports_z1:1, 630 m_supports_z2:1, 631 m_supports_z3:1, 632 m_supports_z4:1, 633 m_supports_QEnvironment:1, 634 m_supports_QEnvironmentHexEncoded:1, 635 m_supports_qSymbol:1, 636 m_supports_jThreadsInfo:1; 637 638 lldb::pid_t m_curr_pid; 639 lldb::tid_t m_curr_tid; // Current gdb remote protocol thread index for all other operations 640 lldb::tid_t m_curr_tid_run; // Current gdb remote protocol thread index for continue, step, etc 641 642 643 uint32_t m_num_supported_hardware_watchpoints; 644 645 // If we need to send a packet while the target is running, the m_async_XXX 646 // member variables take care of making this happen. 647 Mutex m_async_mutex; 648 Predicate<bool> m_async_packet_predicate; 649 std::string m_async_packet; 650 PacketResult m_async_result; 651 StringExtractorGDBRemote m_async_response; 652 int m_async_signal; // We were asked to deliver a signal to the inferior process. 653 bool m_interrupt_sent; 654 std::string m_partial_profile_data; 655 std::map<uint64_t, uint32_t> m_thread_id_to_used_usec_map; 656 657 ArchSpec m_host_arch; 658 ArchSpec m_process_arch; 659 uint32_t m_os_version_major; 660 uint32_t m_os_version_minor; 661 uint32_t m_os_version_update; 662 std::string m_os_build; 663 std::string m_os_kernel; 664 std::string m_hostname; 665 std::string m_gdb_server_name; // from reply to qGDBServerVersion, empty if qGDBServerVersion is not supported 666 uint32_t m_gdb_server_version; // from reply to qGDBServerVersion, zero if qGDBServerVersion is not supported 667 uint32_t m_default_packet_timeout; 668 uint64_t m_max_packet_size; // as returned by qSupported 669 670 671 bool 672 DecodeProcessInfoResponse (StringExtractorGDBRemote &response, 673 ProcessInstanceInfo &process_info); 674 private: 675 //------------------------------------------------------------------ 676 // For GDBRemoteCommunicationClient only 677 //------------------------------------------------------------------ 678 DISALLOW_COPY_AND_ASSIGN (GDBRemoteCommunicationClient); 679 }; 680 681 } // namespace process_gdb_remote 682 } // namespace lldb_private 683 684 #endif // liblldb_GDBRemoteCommunicationClient_h_ 685