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