1 //===-- GDBRemoteCommunicationClient.cpp ----------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "GDBRemoteCommunicationClient.h" 10 11 #include <cmath> 12 #include <sys/stat.h> 13 14 #include <numeric> 15 #include <optional> 16 #include <sstream> 17 18 #include "lldb/Core/ModuleSpec.h" 19 #include "lldb/Host/HostInfo.h" 20 #include "lldb/Host/XML.h" 21 #include "lldb/Symbol/Symbol.h" 22 #include "lldb/Target/MemoryRegionInfo.h" 23 #include "lldb/Target/Target.h" 24 #include "lldb/Target/UnixSignals.h" 25 #include "lldb/Utility/Args.h" 26 #include "lldb/Utility/DataBufferHeap.h" 27 #include "lldb/Utility/LLDBAssert.h" 28 #include "lldb/Utility/LLDBLog.h" 29 #include "lldb/Utility/Log.h" 30 #include "lldb/Utility/State.h" 31 #include "lldb/Utility/StreamString.h" 32 33 #include "ProcessGDBRemote.h" 34 #include "ProcessGDBRemoteLog.h" 35 #include "lldb/Host/Config.h" 36 #include "lldb/Utility/StringExtractorGDBRemote.h" 37 38 #include "llvm/ADT/StringSwitch.h" 39 #include "llvm/Support/JSON.h" 40 41 #if defined(HAVE_LIBCOMPRESSION) 42 #include <compression.h> 43 #endif 44 45 using namespace lldb; 46 using namespace lldb_private::process_gdb_remote; 47 using namespace lldb_private; 48 using namespace std::chrono; 49 50 llvm::raw_ostream &process_gdb_remote::operator<<(llvm::raw_ostream &os, 51 const QOffsets &offsets) { 52 return os << llvm::formatv( 53 "QOffsets({0}, [{1:@[x]}])", offsets.segments, 54 llvm::make_range(offsets.offsets.begin(), offsets.offsets.end())); 55 } 56 57 // GDBRemoteCommunicationClient constructor 58 GDBRemoteCommunicationClient::GDBRemoteCommunicationClient() 59 : GDBRemoteClientBase("gdb-remote.client"), 60 61 m_supports_qProcessInfoPID(true), m_supports_qfProcessInfo(true), 62 m_supports_qUserName(true), m_supports_qGroupName(true), 63 m_supports_qThreadStopInfo(true), m_supports_z0(true), 64 m_supports_z1(true), m_supports_z2(true), m_supports_z3(true), 65 m_supports_z4(true), m_supports_QEnvironment(true), 66 m_supports_QEnvironmentHexEncoded(true), m_supports_qSymbol(true), 67 m_qSymbol_requests_done(false), m_supports_qModuleInfo(true), 68 m_supports_jThreadsInfo(true), m_supports_jModulesInfo(true), 69 m_supports_vFileSize(true), m_supports_vFileMode(true), 70 m_supports_vFileExists(true), m_supports_vRun(true), 71 72 m_host_arch(), m_process_arch(), m_os_build(), m_os_kernel(), 73 m_hostname(), m_gdb_server_name(), m_default_packet_timeout(0), 74 m_qSupported_response(), m_supported_async_json_packets_sp(), 75 m_qXfer_memory_map() {} 76 77 // Destructor 78 GDBRemoteCommunicationClient::~GDBRemoteCommunicationClient() { 79 if (IsConnected()) 80 Disconnect(); 81 } 82 83 bool GDBRemoteCommunicationClient::HandshakeWithServer(Status *error_ptr) { 84 ResetDiscoverableSettings(false); 85 86 // Start the read thread after we send the handshake ack since if we fail to 87 // send the handshake ack, there is no reason to continue... 88 std::chrono::steady_clock::time_point start_of_handshake = 89 std::chrono::steady_clock::now(); 90 if (SendAck()) { 91 // The return value from QueryNoAckModeSupported() is true if the packet 92 // was sent and _any_ response (including UNIMPLEMENTED) was received), or 93 // false if no response was received. This quickly tells us if we have a 94 // live connection to a remote GDB server... 95 if (QueryNoAckModeSupported()) { 96 return true; 97 } else { 98 std::chrono::steady_clock::time_point end_of_handshake = 99 std::chrono::steady_clock::now(); 100 auto handshake_timeout = 101 std::chrono::duration<double>(end_of_handshake - start_of_handshake) 102 .count(); 103 if (error_ptr) { 104 if (!IsConnected()) 105 error_ptr->SetErrorString("Connection shut down by remote side " 106 "while waiting for reply to initial " 107 "handshake packet"); 108 else 109 error_ptr->SetErrorStringWithFormat( 110 "failed to get reply to handshake packet within timeout of " 111 "%.1f seconds", 112 handshake_timeout); 113 } 114 } 115 } else { 116 if (error_ptr) 117 error_ptr->SetErrorString("failed to send the handshake ack"); 118 } 119 return false; 120 } 121 122 bool GDBRemoteCommunicationClient::GetEchoSupported() { 123 if (m_supports_qEcho == eLazyBoolCalculate) { 124 GetRemoteQSupported(); 125 } 126 return m_supports_qEcho == eLazyBoolYes; 127 } 128 129 bool GDBRemoteCommunicationClient::GetQPassSignalsSupported() { 130 if (m_supports_QPassSignals == eLazyBoolCalculate) { 131 GetRemoteQSupported(); 132 } 133 return m_supports_QPassSignals == eLazyBoolYes; 134 } 135 136 bool GDBRemoteCommunicationClient::GetAugmentedLibrariesSVR4ReadSupported() { 137 if (m_supports_augmented_libraries_svr4_read == eLazyBoolCalculate) { 138 GetRemoteQSupported(); 139 } 140 return m_supports_augmented_libraries_svr4_read == eLazyBoolYes; 141 } 142 143 bool GDBRemoteCommunicationClient::GetQXferLibrariesSVR4ReadSupported() { 144 if (m_supports_qXfer_libraries_svr4_read == eLazyBoolCalculate) { 145 GetRemoteQSupported(); 146 } 147 return m_supports_qXfer_libraries_svr4_read == eLazyBoolYes; 148 } 149 150 bool GDBRemoteCommunicationClient::GetQXferLibrariesReadSupported() { 151 if (m_supports_qXfer_libraries_read == eLazyBoolCalculate) { 152 GetRemoteQSupported(); 153 } 154 return m_supports_qXfer_libraries_read == eLazyBoolYes; 155 } 156 157 bool GDBRemoteCommunicationClient::GetQXferAuxvReadSupported() { 158 if (m_supports_qXfer_auxv_read == eLazyBoolCalculate) { 159 GetRemoteQSupported(); 160 } 161 return m_supports_qXfer_auxv_read == eLazyBoolYes; 162 } 163 164 bool GDBRemoteCommunicationClient::GetQXferFeaturesReadSupported() { 165 if (m_supports_qXfer_features_read == eLazyBoolCalculate) { 166 GetRemoteQSupported(); 167 } 168 return m_supports_qXfer_features_read == eLazyBoolYes; 169 } 170 171 bool GDBRemoteCommunicationClient::GetQXferMemoryMapReadSupported() { 172 if (m_supports_qXfer_memory_map_read == eLazyBoolCalculate) { 173 GetRemoteQSupported(); 174 } 175 return m_supports_qXfer_memory_map_read == eLazyBoolYes; 176 } 177 178 bool GDBRemoteCommunicationClient::GetQXferSigInfoReadSupported() { 179 if (m_supports_qXfer_siginfo_read == eLazyBoolCalculate) { 180 GetRemoteQSupported(); 181 } 182 return m_supports_qXfer_siginfo_read == eLazyBoolYes; 183 } 184 185 bool GDBRemoteCommunicationClient::GetMultiprocessSupported() { 186 if (m_supports_memory_tagging == eLazyBoolCalculate) 187 GetRemoteQSupported(); 188 return m_supports_multiprocess == eLazyBoolYes; 189 } 190 191 uint64_t GDBRemoteCommunicationClient::GetRemoteMaxPacketSize() { 192 if (m_max_packet_size == 0) { 193 GetRemoteQSupported(); 194 } 195 return m_max_packet_size; 196 } 197 198 bool GDBRemoteCommunicationClient::QueryNoAckModeSupported() { 199 if (m_supports_not_sending_acks == eLazyBoolCalculate) { 200 m_send_acks = true; 201 m_supports_not_sending_acks = eLazyBoolNo; 202 203 // This is the first real packet that we'll send in a debug session and it 204 // may take a little longer than normal to receive a reply. Wait at least 205 // 6 seconds for a reply to this packet. 206 207 ScopedTimeout timeout(*this, std::max(GetPacketTimeout(), seconds(6))); 208 209 StringExtractorGDBRemote response; 210 if (SendPacketAndWaitForResponse("QStartNoAckMode", response) == 211 PacketResult::Success) { 212 if (response.IsOKResponse()) { 213 m_send_acks = false; 214 m_supports_not_sending_acks = eLazyBoolYes; 215 } 216 return true; 217 } 218 } 219 return false; 220 } 221 222 void GDBRemoteCommunicationClient::GetListThreadsInStopReplySupported() { 223 if (m_supports_threads_in_stop_reply == eLazyBoolCalculate) { 224 m_supports_threads_in_stop_reply = eLazyBoolNo; 225 226 StringExtractorGDBRemote response; 227 if (SendPacketAndWaitForResponse("QListThreadsInStopReply", response) == 228 PacketResult::Success) { 229 if (response.IsOKResponse()) 230 m_supports_threads_in_stop_reply = eLazyBoolYes; 231 } 232 } 233 } 234 235 bool GDBRemoteCommunicationClient::GetVAttachOrWaitSupported() { 236 if (m_attach_or_wait_reply == eLazyBoolCalculate) { 237 m_attach_or_wait_reply = eLazyBoolNo; 238 239 StringExtractorGDBRemote response; 240 if (SendPacketAndWaitForResponse("qVAttachOrWaitSupported", response) == 241 PacketResult::Success) { 242 if (response.IsOKResponse()) 243 m_attach_or_wait_reply = eLazyBoolYes; 244 } 245 } 246 return m_attach_or_wait_reply == eLazyBoolYes; 247 } 248 249 bool GDBRemoteCommunicationClient::GetSyncThreadStateSupported() { 250 if (m_prepare_for_reg_writing_reply == eLazyBoolCalculate) { 251 m_prepare_for_reg_writing_reply = eLazyBoolNo; 252 253 StringExtractorGDBRemote response; 254 if (SendPacketAndWaitForResponse("qSyncThreadStateSupported", response) == 255 PacketResult::Success) { 256 if (response.IsOKResponse()) 257 m_prepare_for_reg_writing_reply = eLazyBoolYes; 258 } 259 } 260 return m_prepare_for_reg_writing_reply == eLazyBoolYes; 261 } 262 263 void GDBRemoteCommunicationClient::ResetDiscoverableSettings(bool did_exec) { 264 if (!did_exec) { 265 // Hard reset everything, this is when we first connect to a GDB server 266 m_supports_not_sending_acks = eLazyBoolCalculate; 267 m_supports_thread_suffix = eLazyBoolCalculate; 268 m_supports_threads_in_stop_reply = eLazyBoolCalculate; 269 m_supports_vCont_c = eLazyBoolCalculate; 270 m_supports_vCont_C = eLazyBoolCalculate; 271 m_supports_vCont_s = eLazyBoolCalculate; 272 m_supports_vCont_S = eLazyBoolCalculate; 273 m_supports_p = eLazyBoolCalculate; 274 m_supports_x = eLazyBoolCalculate; 275 m_supports_QSaveRegisterState = eLazyBoolCalculate; 276 m_qHostInfo_is_valid = eLazyBoolCalculate; 277 m_curr_pid_is_valid = eLazyBoolCalculate; 278 m_qGDBServerVersion_is_valid = eLazyBoolCalculate; 279 m_supports_alloc_dealloc_memory = eLazyBoolCalculate; 280 m_supports_memory_region_info = eLazyBoolCalculate; 281 m_prepare_for_reg_writing_reply = eLazyBoolCalculate; 282 m_attach_or_wait_reply = eLazyBoolCalculate; 283 m_avoid_g_packets = eLazyBoolCalculate; 284 m_supports_multiprocess = eLazyBoolCalculate; 285 m_supports_qSaveCore = eLazyBoolCalculate; 286 m_supports_qXfer_auxv_read = eLazyBoolCalculate; 287 m_supports_qXfer_libraries_read = eLazyBoolCalculate; 288 m_supports_qXfer_libraries_svr4_read = eLazyBoolCalculate; 289 m_supports_qXfer_features_read = eLazyBoolCalculate; 290 m_supports_qXfer_memory_map_read = eLazyBoolCalculate; 291 m_supports_qXfer_siginfo_read = eLazyBoolCalculate; 292 m_supports_augmented_libraries_svr4_read = eLazyBoolCalculate; 293 m_uses_native_signals = eLazyBoolCalculate; 294 m_supports_qProcessInfoPID = true; 295 m_supports_qfProcessInfo = true; 296 m_supports_qUserName = true; 297 m_supports_qGroupName = true; 298 m_supports_qThreadStopInfo = true; 299 m_supports_z0 = true; 300 m_supports_z1 = true; 301 m_supports_z2 = true; 302 m_supports_z3 = true; 303 m_supports_z4 = true; 304 m_supports_QEnvironment = true; 305 m_supports_QEnvironmentHexEncoded = true; 306 m_supports_qSymbol = true; 307 m_qSymbol_requests_done = false; 308 m_supports_qModuleInfo = true; 309 m_host_arch.Clear(); 310 m_os_version = llvm::VersionTuple(); 311 m_os_build.clear(); 312 m_os_kernel.clear(); 313 m_hostname.clear(); 314 m_gdb_server_name.clear(); 315 m_gdb_server_version = UINT32_MAX; 316 m_default_packet_timeout = seconds(0); 317 m_target_vm_page_size = 0; 318 m_max_packet_size = 0; 319 m_qSupported_response.clear(); 320 m_supported_async_json_packets_is_valid = false; 321 m_supported_async_json_packets_sp.reset(); 322 m_supports_jModulesInfo = true; 323 } 324 325 // These flags should be reset when we first connect to a GDB server and when 326 // our inferior process execs 327 m_qProcessInfo_is_valid = eLazyBoolCalculate; 328 m_process_arch.Clear(); 329 } 330 331 void GDBRemoteCommunicationClient::GetRemoteQSupported() { 332 // Clear out any capabilities we expect to see in the qSupported response 333 m_supports_qXfer_auxv_read = eLazyBoolNo; 334 m_supports_qXfer_libraries_read = eLazyBoolNo; 335 m_supports_qXfer_libraries_svr4_read = eLazyBoolNo; 336 m_supports_augmented_libraries_svr4_read = eLazyBoolNo; 337 m_supports_qXfer_features_read = eLazyBoolNo; 338 m_supports_qXfer_memory_map_read = eLazyBoolNo; 339 m_supports_qXfer_siginfo_read = eLazyBoolNo; 340 m_supports_multiprocess = eLazyBoolNo; 341 m_supports_qEcho = eLazyBoolNo; 342 m_supports_QPassSignals = eLazyBoolNo; 343 m_supports_memory_tagging = eLazyBoolNo; 344 m_supports_qSaveCore = eLazyBoolNo; 345 m_uses_native_signals = eLazyBoolNo; 346 347 m_max_packet_size = UINT64_MAX; // It's supposed to always be there, but if 348 // not, we assume no limit 349 350 // build the qSupported packet 351 std::vector<std::string> features = {"xmlRegisters=i386,arm,mips,arc", 352 "multiprocess+", "fork-events+", 353 "vfork-events+"}; 354 StreamString packet; 355 packet.PutCString("qSupported"); 356 for (uint32_t i = 0; i < features.size(); ++i) { 357 packet.PutCString(i == 0 ? ":" : ";"); 358 packet.PutCString(features[i]); 359 } 360 361 StringExtractorGDBRemote response; 362 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 363 PacketResult::Success) { 364 // Hang on to the qSupported packet, so that platforms can do custom 365 // configuration of the transport before attaching/launching the process. 366 m_qSupported_response = response.GetStringRef().str(); 367 368 for (llvm::StringRef x : llvm::split(response.GetStringRef(), ';')) { 369 if (x == "qXfer:auxv:read+") 370 m_supports_qXfer_auxv_read = eLazyBoolYes; 371 else if (x == "qXfer:libraries-svr4:read+") 372 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; 373 else if (x == "augmented-libraries-svr4-read") { 374 m_supports_qXfer_libraries_svr4_read = eLazyBoolYes; // implied 375 m_supports_augmented_libraries_svr4_read = eLazyBoolYes; 376 } else if (x == "qXfer:libraries:read+") 377 m_supports_qXfer_libraries_read = eLazyBoolYes; 378 else if (x == "qXfer:features:read+") 379 m_supports_qXfer_features_read = eLazyBoolYes; 380 else if (x == "qXfer:memory-map:read+") 381 m_supports_qXfer_memory_map_read = eLazyBoolYes; 382 else if (x == "qXfer:siginfo:read+") 383 m_supports_qXfer_siginfo_read = eLazyBoolYes; 384 else if (x == "qEcho") 385 m_supports_qEcho = eLazyBoolYes; 386 else if (x == "QPassSignals+") 387 m_supports_QPassSignals = eLazyBoolYes; 388 else if (x == "multiprocess+") 389 m_supports_multiprocess = eLazyBoolYes; 390 else if (x == "memory-tagging+") 391 m_supports_memory_tagging = eLazyBoolYes; 392 else if (x == "qSaveCore+") 393 m_supports_qSaveCore = eLazyBoolYes; 394 else if (x == "native-signals+") 395 m_uses_native_signals = eLazyBoolYes; 396 // Look for a list of compressions in the features list e.g. 397 // qXfer:features:read+;PacketSize=20000;qEcho+;SupportedCompressions=zlib- 398 // deflate,lzma 399 else if (x.consume_front("SupportedCompressions=")) { 400 llvm::SmallVector<llvm::StringRef, 4> compressions; 401 x.split(compressions, ','); 402 if (!compressions.empty()) 403 MaybeEnableCompression(compressions); 404 } else if (x.consume_front("PacketSize=")) { 405 StringExtractorGDBRemote packet_response(x); 406 m_max_packet_size = 407 packet_response.GetHexMaxU64(/*little_endian=*/false, UINT64_MAX); 408 if (m_max_packet_size == 0) { 409 m_max_packet_size = UINT64_MAX; // Must have been a garbled response 410 Log *log(GetLog(GDBRLog::Process)); 411 LLDB_LOGF(log, "Garbled PacketSize spec in qSupported response"); 412 } 413 } 414 } 415 } 416 } 417 418 bool GDBRemoteCommunicationClient::GetThreadSuffixSupported() { 419 if (m_supports_thread_suffix == eLazyBoolCalculate) { 420 StringExtractorGDBRemote response; 421 m_supports_thread_suffix = eLazyBoolNo; 422 if (SendPacketAndWaitForResponse("QThreadSuffixSupported", response) == 423 PacketResult::Success) { 424 if (response.IsOKResponse()) 425 m_supports_thread_suffix = eLazyBoolYes; 426 } 427 } 428 return m_supports_thread_suffix; 429 } 430 bool GDBRemoteCommunicationClient::GetVContSupported(char flavor) { 431 if (m_supports_vCont_c == eLazyBoolCalculate) { 432 StringExtractorGDBRemote response; 433 m_supports_vCont_any = eLazyBoolNo; 434 m_supports_vCont_all = eLazyBoolNo; 435 m_supports_vCont_c = eLazyBoolNo; 436 m_supports_vCont_C = eLazyBoolNo; 437 m_supports_vCont_s = eLazyBoolNo; 438 m_supports_vCont_S = eLazyBoolNo; 439 if (SendPacketAndWaitForResponse("vCont?", response) == 440 PacketResult::Success) { 441 const char *response_cstr = response.GetStringRef().data(); 442 if (::strstr(response_cstr, ";c")) 443 m_supports_vCont_c = eLazyBoolYes; 444 445 if (::strstr(response_cstr, ";C")) 446 m_supports_vCont_C = eLazyBoolYes; 447 448 if (::strstr(response_cstr, ";s")) 449 m_supports_vCont_s = eLazyBoolYes; 450 451 if (::strstr(response_cstr, ";S")) 452 m_supports_vCont_S = eLazyBoolYes; 453 454 if (m_supports_vCont_c == eLazyBoolYes && 455 m_supports_vCont_C == eLazyBoolYes && 456 m_supports_vCont_s == eLazyBoolYes && 457 m_supports_vCont_S == eLazyBoolYes) { 458 m_supports_vCont_all = eLazyBoolYes; 459 } 460 461 if (m_supports_vCont_c == eLazyBoolYes || 462 m_supports_vCont_C == eLazyBoolYes || 463 m_supports_vCont_s == eLazyBoolYes || 464 m_supports_vCont_S == eLazyBoolYes) { 465 m_supports_vCont_any = eLazyBoolYes; 466 } 467 } 468 } 469 470 switch (flavor) { 471 case 'a': 472 return m_supports_vCont_any; 473 case 'A': 474 return m_supports_vCont_all; 475 case 'c': 476 return m_supports_vCont_c; 477 case 'C': 478 return m_supports_vCont_C; 479 case 's': 480 return m_supports_vCont_s; 481 case 'S': 482 return m_supports_vCont_S; 483 default: 484 break; 485 } 486 return false; 487 } 488 489 GDBRemoteCommunication::PacketResult 490 GDBRemoteCommunicationClient::SendThreadSpecificPacketAndWaitForResponse( 491 lldb::tid_t tid, StreamString &&payload, 492 StringExtractorGDBRemote &response) { 493 Lock lock(*this); 494 if (!lock) { 495 if (Log *log = GetLog(GDBRLog::Process | GDBRLog::Packets)) 496 LLDB_LOGF(log, 497 "GDBRemoteCommunicationClient::%s: Didn't get sequence mutex " 498 "for %s packet.", 499 __FUNCTION__, payload.GetData()); 500 return PacketResult::ErrorNoSequenceLock; 501 } 502 503 if (GetThreadSuffixSupported()) 504 payload.Printf(";thread:%4.4" PRIx64 ";", tid); 505 else { 506 if (!SetCurrentThread(tid)) 507 return PacketResult::ErrorSendFailed; 508 } 509 510 return SendPacketAndWaitForResponseNoLock(payload.GetString(), response); 511 } 512 513 // Check if the target supports 'p' packet. It sends out a 'p' packet and 514 // checks the response. A normal packet will tell us that support is available. 515 // 516 // Takes a valid thread ID because p needs to apply to a thread. 517 bool GDBRemoteCommunicationClient::GetpPacketSupported(lldb::tid_t tid) { 518 if (m_supports_p == eLazyBoolCalculate) 519 m_supports_p = GetThreadPacketSupported(tid, "p0"); 520 return m_supports_p; 521 } 522 523 LazyBool GDBRemoteCommunicationClient::GetThreadPacketSupported( 524 lldb::tid_t tid, llvm::StringRef packetStr) { 525 StreamString payload; 526 payload.PutCString(packetStr); 527 StringExtractorGDBRemote response; 528 if (SendThreadSpecificPacketAndWaitForResponse( 529 tid, std::move(payload), response) == PacketResult::Success && 530 response.IsNormalResponse()) { 531 return eLazyBoolYes; 532 } 533 return eLazyBoolNo; 534 } 535 536 bool GDBRemoteCommunicationClient::GetSaveCoreSupported() const { 537 return m_supports_qSaveCore == eLazyBoolYes; 538 } 539 540 StructuredData::ObjectSP GDBRemoteCommunicationClient::GetThreadsInfo() { 541 // Get information on all threads at one using the "jThreadsInfo" packet 542 StructuredData::ObjectSP object_sp; 543 544 if (m_supports_jThreadsInfo) { 545 StringExtractorGDBRemote response; 546 response.SetResponseValidatorToJSON(); 547 if (SendPacketAndWaitForResponse("jThreadsInfo", response) == 548 PacketResult::Success) { 549 if (response.IsUnsupportedResponse()) { 550 m_supports_jThreadsInfo = false; 551 } else if (!response.Empty()) { 552 object_sp = 553 StructuredData::ParseJSON(std::string(response.GetStringRef())); 554 } 555 } 556 } 557 return object_sp; 558 } 559 560 bool GDBRemoteCommunicationClient::GetThreadExtendedInfoSupported() { 561 if (m_supports_jThreadExtendedInfo == eLazyBoolCalculate) { 562 StringExtractorGDBRemote response; 563 m_supports_jThreadExtendedInfo = eLazyBoolNo; 564 if (SendPacketAndWaitForResponse("jThreadExtendedInfo:", response) == 565 PacketResult::Success) { 566 if (response.IsOKResponse()) { 567 m_supports_jThreadExtendedInfo = eLazyBoolYes; 568 } 569 } 570 } 571 return m_supports_jThreadExtendedInfo; 572 } 573 574 void GDBRemoteCommunicationClient::EnableErrorStringInPacket() { 575 if (m_supports_error_string_reply == eLazyBoolCalculate) { 576 StringExtractorGDBRemote response; 577 // We try to enable error strings in remote packets but if we fail, we just 578 // work in the older way. 579 m_supports_error_string_reply = eLazyBoolNo; 580 if (SendPacketAndWaitForResponse("QEnableErrorStrings", response) == 581 PacketResult::Success) { 582 if (response.IsOKResponse()) { 583 m_supports_error_string_reply = eLazyBoolYes; 584 } 585 } 586 } 587 } 588 589 bool GDBRemoteCommunicationClient::GetLoadedDynamicLibrariesInfosSupported() { 590 if (m_supports_jLoadedDynamicLibrariesInfos == eLazyBoolCalculate) { 591 StringExtractorGDBRemote response; 592 m_supports_jLoadedDynamicLibrariesInfos = eLazyBoolNo; 593 if (SendPacketAndWaitForResponse("jGetLoadedDynamicLibrariesInfos:", 594 response) == PacketResult::Success) { 595 if (response.IsOKResponse()) { 596 m_supports_jLoadedDynamicLibrariesInfos = eLazyBoolYes; 597 } 598 } 599 } 600 return m_supports_jLoadedDynamicLibrariesInfos; 601 } 602 603 bool GDBRemoteCommunicationClient::GetSharedCacheInfoSupported() { 604 if (m_supports_jGetSharedCacheInfo == eLazyBoolCalculate) { 605 StringExtractorGDBRemote response; 606 m_supports_jGetSharedCacheInfo = eLazyBoolNo; 607 if (SendPacketAndWaitForResponse("jGetSharedCacheInfo:", response) == 608 PacketResult::Success) { 609 if (response.IsOKResponse()) { 610 m_supports_jGetSharedCacheInfo = eLazyBoolYes; 611 } 612 } 613 } 614 return m_supports_jGetSharedCacheInfo; 615 } 616 617 bool GDBRemoteCommunicationClient::GetDynamicLoaderProcessStateSupported() { 618 if (m_supports_jGetDyldProcessState == eLazyBoolCalculate) { 619 StringExtractorGDBRemote response; 620 m_supports_jGetDyldProcessState = eLazyBoolNo; 621 if (SendPacketAndWaitForResponse("jGetDyldProcessState", response) == 622 PacketResult::Success) { 623 if (!response.IsUnsupportedResponse()) 624 m_supports_jGetDyldProcessState = eLazyBoolYes; 625 } 626 } 627 return m_supports_jGetDyldProcessState; 628 } 629 630 bool GDBRemoteCommunicationClient::GetMemoryTaggingSupported() { 631 if (m_supports_memory_tagging == eLazyBoolCalculate) { 632 GetRemoteQSupported(); 633 } 634 return m_supports_memory_tagging == eLazyBoolYes; 635 } 636 637 DataBufferSP GDBRemoteCommunicationClient::ReadMemoryTags(lldb::addr_t addr, 638 size_t len, 639 int32_t type) { 640 StreamString packet; 641 packet.Printf("qMemTags:%" PRIx64 ",%zx:%" PRIx32, addr, len, type); 642 StringExtractorGDBRemote response; 643 644 Log *log = GetLog(GDBRLog::Memory); 645 646 if (SendPacketAndWaitForResponse(packet.GetString(), response) != 647 PacketResult::Success || 648 !response.IsNormalResponse()) { 649 LLDB_LOGF(log, "GDBRemoteCommunicationClient::%s: qMemTags packet failed", 650 __FUNCTION__); 651 return nullptr; 652 } 653 654 // We are expecting 655 // m<hex encoded bytes> 656 657 if (response.GetChar() != 'm') { 658 LLDB_LOGF(log, 659 "GDBRemoteCommunicationClient::%s: qMemTags response did not " 660 "begin with \"m\"", 661 __FUNCTION__); 662 return nullptr; 663 } 664 665 size_t expected_bytes = response.GetBytesLeft() / 2; 666 WritableDataBufferSP buffer_sp(new DataBufferHeap(expected_bytes, 0)); 667 size_t got_bytes = response.GetHexBytesAvail(buffer_sp->GetData()); 668 // Check both because in some situations chars are consumed even 669 // if the decoding fails. 670 if (response.GetBytesLeft() || (expected_bytes != got_bytes)) { 671 LLDB_LOGF( 672 log, 673 "GDBRemoteCommunicationClient::%s: Invalid data in qMemTags response", 674 __FUNCTION__); 675 return nullptr; 676 } 677 678 return buffer_sp; 679 } 680 681 Status GDBRemoteCommunicationClient::WriteMemoryTags( 682 lldb::addr_t addr, size_t len, int32_t type, 683 const std::vector<uint8_t> &tags) { 684 // Format QMemTags:address,length:type:tags 685 StreamString packet; 686 packet.Printf("QMemTags:%" PRIx64 ",%zx:%" PRIx32 ":", addr, len, type); 687 packet.PutBytesAsRawHex8(tags.data(), tags.size()); 688 689 Status status; 690 StringExtractorGDBRemote response; 691 if (SendPacketAndWaitForResponse(packet.GetString(), response) != 692 PacketResult::Success || 693 !response.IsOKResponse()) { 694 status.SetErrorString("QMemTags packet failed"); 695 } 696 return status; 697 } 698 699 bool GDBRemoteCommunicationClient::GetxPacketSupported() { 700 if (m_supports_x == eLazyBoolCalculate) { 701 StringExtractorGDBRemote response; 702 m_supports_x = eLazyBoolNo; 703 char packet[256]; 704 snprintf(packet, sizeof(packet), "x0,0"); 705 if (SendPacketAndWaitForResponse(packet, response) == 706 PacketResult::Success) { 707 if (response.IsOKResponse()) 708 m_supports_x = eLazyBoolYes; 709 } 710 } 711 return m_supports_x; 712 } 713 714 lldb::pid_t GDBRemoteCommunicationClient::GetCurrentProcessID(bool allow_lazy) { 715 if (allow_lazy && m_curr_pid_is_valid == eLazyBoolYes) 716 return m_curr_pid; 717 718 // First try to retrieve the pid via the qProcessInfo request. 719 GetCurrentProcessInfo(allow_lazy); 720 if (m_curr_pid_is_valid == eLazyBoolYes) { 721 // We really got it. 722 return m_curr_pid; 723 } else { 724 // If we don't get a response for qProcessInfo, check if $qC gives us a 725 // result. $qC only returns a real process id on older debugserver and 726 // lldb-platform stubs. The gdb remote protocol documents $qC as returning 727 // the thread id, which newer debugserver and lldb-gdbserver stubs return 728 // correctly. 729 StringExtractorGDBRemote response; 730 if (SendPacketAndWaitForResponse("qC", response) == PacketResult::Success) { 731 if (response.GetChar() == 'Q') { 732 if (response.GetChar() == 'C') { 733 m_curr_pid_run = m_curr_pid = 734 response.GetHexMaxU64(false, LLDB_INVALID_PROCESS_ID); 735 if (m_curr_pid != LLDB_INVALID_PROCESS_ID) { 736 m_curr_pid_is_valid = eLazyBoolYes; 737 return m_curr_pid; 738 } 739 } 740 } 741 } 742 743 // If we don't get a response for $qC, check if $qfThreadID gives us a 744 // result. 745 if (m_curr_pid == LLDB_INVALID_PROCESS_ID) { 746 bool sequence_mutex_unavailable; 747 auto ids = GetCurrentProcessAndThreadIDs(sequence_mutex_unavailable); 748 if (!ids.empty() && !sequence_mutex_unavailable) { 749 // If server returned an explicit PID, use that. 750 m_curr_pid_run = m_curr_pid = ids.front().first; 751 // Otherwise, use the TID of the first thread (Linux hack). 752 if (m_curr_pid == LLDB_INVALID_PROCESS_ID) 753 m_curr_pid_run = m_curr_pid = ids.front().second; 754 m_curr_pid_is_valid = eLazyBoolYes; 755 return m_curr_pid; 756 } 757 } 758 } 759 760 return LLDB_INVALID_PROCESS_ID; 761 } 762 763 llvm::Error GDBRemoteCommunicationClient::LaunchProcess(const Args &args) { 764 if (!args.GetArgumentAtIndex(0)) 765 return llvm::createStringError(llvm::inconvertibleErrorCode(), 766 "Nothing to launch"); 767 // try vRun first 768 if (m_supports_vRun) { 769 StreamString packet; 770 packet.PutCString("vRun"); 771 for (const Args::ArgEntry &arg : args) { 772 packet.PutChar(';'); 773 packet.PutStringAsRawHex8(arg.ref()); 774 } 775 776 StringExtractorGDBRemote response; 777 if (SendPacketAndWaitForResponse(packet.GetString(), response) != 778 PacketResult::Success) 779 return llvm::createStringError(llvm::inconvertibleErrorCode(), 780 "Sending vRun packet failed"); 781 782 if (response.IsErrorResponse()) 783 return response.GetStatus().ToError(); 784 785 // vRun replies with a stop reason packet 786 // FIXME: right now we just discard the packet and LLDB queries 787 // for stop reason again 788 if (!response.IsUnsupportedResponse()) 789 return llvm::Error::success(); 790 791 m_supports_vRun = false; 792 } 793 794 // fallback to A 795 StreamString packet; 796 packet.PutChar('A'); 797 llvm::ListSeparator LS(","); 798 for (const auto &arg : llvm::enumerate(args)) { 799 packet << LS; 800 packet.Format("{0},{1},", arg.value().ref().size() * 2, arg.index()); 801 packet.PutStringAsRawHex8(arg.value().ref()); 802 } 803 804 StringExtractorGDBRemote response; 805 if (SendPacketAndWaitForResponse(packet.GetString(), response) != 806 PacketResult::Success) { 807 return llvm::createStringError(llvm::inconvertibleErrorCode(), 808 "Sending A packet failed"); 809 } 810 if (!response.IsOKResponse()) 811 return response.GetStatus().ToError(); 812 813 if (SendPacketAndWaitForResponse("qLaunchSuccess", response) != 814 PacketResult::Success) { 815 return llvm::createStringError(llvm::inconvertibleErrorCode(), 816 "Sending qLaunchSuccess packet failed"); 817 } 818 if (response.IsOKResponse()) 819 return llvm::Error::success(); 820 if (response.GetChar() == 'E') { 821 return llvm::createStringError(llvm::inconvertibleErrorCode(), 822 response.GetStringRef().substr(1)); 823 } 824 return llvm::createStringError(llvm::inconvertibleErrorCode(), 825 "unknown error occurred launching process"); 826 } 827 828 int GDBRemoteCommunicationClient::SendEnvironment(const Environment &env) { 829 for (const auto &KV : env) { 830 int r = SendEnvironmentPacket(Environment::compose(KV).c_str()); 831 if (r != 0) 832 return r; 833 } 834 return 0; 835 } 836 837 int GDBRemoteCommunicationClient::SendEnvironmentPacket( 838 char const *name_equal_value) { 839 if (name_equal_value && name_equal_value[0]) { 840 bool send_hex_encoding = false; 841 for (const char *p = name_equal_value; *p != '\0' && !send_hex_encoding; 842 ++p) { 843 if (llvm::isPrint(*p)) { 844 switch (*p) { 845 case '$': 846 case '#': 847 case '*': 848 case '}': 849 send_hex_encoding = true; 850 break; 851 default: 852 break; 853 } 854 } else { 855 // We have non printable characters, lets hex encode this... 856 send_hex_encoding = true; 857 } 858 } 859 860 StringExtractorGDBRemote response; 861 // Prefer sending unencoded, if possible and the server supports it. 862 if (!send_hex_encoding && m_supports_QEnvironment) { 863 StreamString packet; 864 packet.Printf("QEnvironment:%s", name_equal_value); 865 if (SendPacketAndWaitForResponse(packet.GetString(), response) != 866 PacketResult::Success) 867 return -1; 868 869 if (response.IsOKResponse()) 870 return 0; 871 if (response.IsUnsupportedResponse()) 872 m_supports_QEnvironment = false; 873 else { 874 uint8_t error = response.GetError(); 875 if (error) 876 return error; 877 return -1; 878 } 879 } 880 881 if (m_supports_QEnvironmentHexEncoded) { 882 StreamString packet; 883 packet.PutCString("QEnvironmentHexEncoded:"); 884 packet.PutBytesAsRawHex8(name_equal_value, strlen(name_equal_value)); 885 if (SendPacketAndWaitForResponse(packet.GetString(), response) != 886 PacketResult::Success) 887 return -1; 888 889 if (response.IsOKResponse()) 890 return 0; 891 if (response.IsUnsupportedResponse()) 892 m_supports_QEnvironmentHexEncoded = false; 893 else { 894 uint8_t error = response.GetError(); 895 if (error) 896 return error; 897 return -1; 898 } 899 } 900 } 901 return -1; 902 } 903 904 int GDBRemoteCommunicationClient::SendLaunchArchPacket(char const *arch) { 905 if (arch && arch[0]) { 906 StreamString packet; 907 packet.Printf("QLaunchArch:%s", arch); 908 StringExtractorGDBRemote response; 909 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 910 PacketResult::Success) { 911 if (response.IsOKResponse()) 912 return 0; 913 uint8_t error = response.GetError(); 914 if (error) 915 return error; 916 } 917 } 918 return -1; 919 } 920 921 int GDBRemoteCommunicationClient::SendLaunchEventDataPacket( 922 char const *data, bool *was_supported) { 923 if (data && *data != '\0') { 924 StreamString packet; 925 packet.Printf("QSetProcessEvent:%s", data); 926 StringExtractorGDBRemote response; 927 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 928 PacketResult::Success) { 929 if (response.IsOKResponse()) { 930 if (was_supported) 931 *was_supported = true; 932 return 0; 933 } else if (response.IsUnsupportedResponse()) { 934 if (was_supported) 935 *was_supported = false; 936 return -1; 937 } else { 938 uint8_t error = response.GetError(); 939 if (was_supported) 940 *was_supported = true; 941 if (error) 942 return error; 943 } 944 } 945 } 946 return -1; 947 } 948 949 llvm::VersionTuple GDBRemoteCommunicationClient::GetOSVersion() { 950 GetHostInfo(); 951 return m_os_version; 952 } 953 954 llvm::VersionTuple GDBRemoteCommunicationClient::GetMacCatalystVersion() { 955 GetHostInfo(); 956 return m_maccatalyst_version; 957 } 958 959 llvm::Optional<std::string> GDBRemoteCommunicationClient::GetOSBuildString() { 960 if (GetHostInfo()) { 961 if (!m_os_build.empty()) 962 return m_os_build; 963 } 964 return std::nullopt; 965 } 966 967 llvm::Optional<std::string> 968 GDBRemoteCommunicationClient::GetOSKernelDescription() { 969 if (GetHostInfo()) { 970 if (!m_os_kernel.empty()) 971 return m_os_kernel; 972 } 973 return std::nullopt; 974 } 975 976 bool GDBRemoteCommunicationClient::GetHostname(std::string &s) { 977 if (GetHostInfo()) { 978 if (!m_hostname.empty()) { 979 s = m_hostname; 980 return true; 981 } 982 } 983 s.clear(); 984 return false; 985 } 986 987 ArchSpec GDBRemoteCommunicationClient::GetSystemArchitecture() { 988 if (GetHostInfo()) 989 return m_host_arch; 990 return ArchSpec(); 991 } 992 993 const lldb_private::ArchSpec & 994 GDBRemoteCommunicationClient::GetProcessArchitecture() { 995 if (m_qProcessInfo_is_valid == eLazyBoolCalculate) 996 GetCurrentProcessInfo(); 997 return m_process_arch; 998 } 999 1000 bool GDBRemoteCommunicationClient::GetProcessStandaloneBinary( 1001 UUID &uuid, addr_t &value, bool &value_is_offset) { 1002 if (m_qProcessInfo_is_valid == eLazyBoolCalculate) 1003 GetCurrentProcessInfo(); 1004 1005 // Return true if we have a UUID or an address/offset of the 1006 // main standalone / firmware binary being used. 1007 if (!m_process_standalone_uuid.IsValid() && 1008 m_process_standalone_value == LLDB_INVALID_ADDRESS) 1009 return false; 1010 1011 uuid = m_process_standalone_uuid; 1012 value = m_process_standalone_value; 1013 value_is_offset = m_process_standalone_value_is_offset; 1014 return true; 1015 } 1016 1017 std::vector<addr_t> 1018 GDBRemoteCommunicationClient::GetProcessStandaloneBinaries() { 1019 if (m_qProcessInfo_is_valid == eLazyBoolCalculate) 1020 GetCurrentProcessInfo(); 1021 return m_binary_addresses; 1022 } 1023 1024 bool GDBRemoteCommunicationClient::GetGDBServerVersion() { 1025 if (m_qGDBServerVersion_is_valid == eLazyBoolCalculate) { 1026 m_gdb_server_name.clear(); 1027 m_gdb_server_version = 0; 1028 m_qGDBServerVersion_is_valid = eLazyBoolNo; 1029 1030 StringExtractorGDBRemote response; 1031 if (SendPacketAndWaitForResponse("qGDBServerVersion", response) == 1032 PacketResult::Success) { 1033 if (response.IsNormalResponse()) { 1034 llvm::StringRef name, value; 1035 bool success = false; 1036 while (response.GetNameColonValue(name, value)) { 1037 if (name.equals("name")) { 1038 success = true; 1039 m_gdb_server_name = std::string(value); 1040 } else if (name.equals("version")) { 1041 llvm::StringRef major, minor; 1042 std::tie(major, minor) = value.split('.'); 1043 if (!major.getAsInteger(0, m_gdb_server_version)) 1044 success = true; 1045 } 1046 } 1047 if (success) 1048 m_qGDBServerVersion_is_valid = eLazyBoolYes; 1049 } 1050 } 1051 } 1052 return m_qGDBServerVersion_is_valid == eLazyBoolYes; 1053 } 1054 1055 void GDBRemoteCommunicationClient::MaybeEnableCompression( 1056 llvm::ArrayRef<llvm::StringRef> supported_compressions) { 1057 CompressionType avail_type = CompressionType::None; 1058 llvm::StringRef avail_name; 1059 1060 #if defined(HAVE_LIBCOMPRESSION) 1061 if (avail_type == CompressionType::None) { 1062 for (auto compression : supported_compressions) { 1063 if (compression == "lzfse") { 1064 avail_type = CompressionType::LZFSE; 1065 avail_name = compression; 1066 break; 1067 } 1068 } 1069 } 1070 #endif 1071 1072 #if defined(HAVE_LIBCOMPRESSION) 1073 if (avail_type == CompressionType::None) { 1074 for (auto compression : supported_compressions) { 1075 if (compression == "zlib-deflate") { 1076 avail_type = CompressionType::ZlibDeflate; 1077 avail_name = compression; 1078 break; 1079 } 1080 } 1081 } 1082 #endif 1083 1084 #if LLVM_ENABLE_ZLIB 1085 if (avail_type == CompressionType::None) { 1086 for (auto compression : supported_compressions) { 1087 if (compression == "zlib-deflate") { 1088 avail_type = CompressionType::ZlibDeflate; 1089 avail_name = compression; 1090 break; 1091 } 1092 } 1093 } 1094 #endif 1095 1096 #if defined(HAVE_LIBCOMPRESSION) 1097 if (avail_type == CompressionType::None) { 1098 for (auto compression : supported_compressions) { 1099 if (compression == "lz4") { 1100 avail_type = CompressionType::LZ4; 1101 avail_name = compression; 1102 break; 1103 } 1104 } 1105 } 1106 #endif 1107 1108 #if defined(HAVE_LIBCOMPRESSION) 1109 if (avail_type == CompressionType::None) { 1110 for (auto compression : supported_compressions) { 1111 if (compression == "lzma") { 1112 avail_type = CompressionType::LZMA; 1113 avail_name = compression; 1114 break; 1115 } 1116 } 1117 } 1118 #endif 1119 1120 if (avail_type != CompressionType::None) { 1121 StringExtractorGDBRemote response; 1122 std::string packet = "QEnableCompression:type:" + avail_name.str() + ";"; 1123 if (SendPacketAndWaitForResponse(packet, response) != PacketResult::Success) 1124 return; 1125 1126 if (response.IsOKResponse()) { 1127 m_compression_type = avail_type; 1128 } 1129 } 1130 } 1131 1132 const char *GDBRemoteCommunicationClient::GetGDBServerProgramName() { 1133 if (GetGDBServerVersion()) { 1134 if (!m_gdb_server_name.empty()) 1135 return m_gdb_server_name.c_str(); 1136 } 1137 return nullptr; 1138 } 1139 1140 uint32_t GDBRemoteCommunicationClient::GetGDBServerProgramVersion() { 1141 if (GetGDBServerVersion()) 1142 return m_gdb_server_version; 1143 return 0; 1144 } 1145 1146 bool GDBRemoteCommunicationClient::GetDefaultThreadId(lldb::tid_t &tid) { 1147 StringExtractorGDBRemote response; 1148 if (SendPacketAndWaitForResponse("qC", response) != PacketResult::Success) 1149 return false; 1150 1151 if (!response.IsNormalResponse()) 1152 return false; 1153 1154 if (response.GetChar() == 'Q' && response.GetChar() == 'C') { 1155 auto pid_tid = response.GetPidTid(0); 1156 if (!pid_tid) 1157 return false; 1158 1159 lldb::pid_t pid = pid_tid->first; 1160 // invalid 1161 if (pid == StringExtractorGDBRemote::AllProcesses) 1162 return false; 1163 1164 // if we get pid as well, update m_curr_pid 1165 if (pid != 0) { 1166 m_curr_pid_run = m_curr_pid = pid; 1167 m_curr_pid_is_valid = eLazyBoolYes; 1168 } 1169 tid = pid_tid->second; 1170 } 1171 1172 return true; 1173 } 1174 1175 static void ParseOSType(llvm::StringRef value, std::string &os_name, 1176 std::string &environment) { 1177 if (value.equals("iossimulator") || value.equals("tvossimulator") || 1178 value.equals("watchossimulator")) { 1179 environment = "simulator"; 1180 os_name = value.drop_back(environment.size()).str(); 1181 } else if (value.equals("maccatalyst")) { 1182 os_name = "ios"; 1183 environment = "macabi"; 1184 } else { 1185 os_name = value.str(); 1186 } 1187 } 1188 1189 bool GDBRemoteCommunicationClient::GetHostInfo(bool force) { 1190 Log *log = GetLog(GDBRLog::Process); 1191 1192 if (force || m_qHostInfo_is_valid == eLazyBoolCalculate) { 1193 // host info computation can require DNS traffic and shelling out to external processes. 1194 // Increase the timeout to account for that. 1195 ScopedTimeout timeout(*this, seconds(10)); 1196 m_qHostInfo_is_valid = eLazyBoolNo; 1197 StringExtractorGDBRemote response; 1198 if (SendPacketAndWaitForResponse("qHostInfo", response) == 1199 PacketResult::Success) { 1200 if (response.IsNormalResponse()) { 1201 llvm::StringRef name; 1202 llvm::StringRef value; 1203 uint32_t cpu = LLDB_INVALID_CPUTYPE; 1204 uint32_t sub = 0; 1205 std::string arch_name; 1206 std::string os_name; 1207 std::string environment; 1208 std::string vendor_name; 1209 std::string triple; 1210 std::string distribution_id; 1211 uint32_t pointer_byte_size = 0; 1212 ByteOrder byte_order = eByteOrderInvalid; 1213 uint32_t num_keys_decoded = 0; 1214 while (response.GetNameColonValue(name, value)) { 1215 if (name.equals("cputype")) { 1216 // exception type in big endian hex 1217 if (!value.getAsInteger(0, cpu)) 1218 ++num_keys_decoded; 1219 } else if (name.equals("cpusubtype")) { 1220 // exception count in big endian hex 1221 if (!value.getAsInteger(0, sub)) 1222 ++num_keys_decoded; 1223 } else if (name.equals("arch")) { 1224 arch_name = std::string(value); 1225 ++num_keys_decoded; 1226 } else if (name.equals("triple")) { 1227 StringExtractor extractor(value); 1228 extractor.GetHexByteString(triple); 1229 ++num_keys_decoded; 1230 } else if (name.equals("distribution_id")) { 1231 StringExtractor extractor(value); 1232 extractor.GetHexByteString(distribution_id); 1233 ++num_keys_decoded; 1234 } else if (name.equals("os_build")) { 1235 StringExtractor extractor(value); 1236 extractor.GetHexByteString(m_os_build); 1237 ++num_keys_decoded; 1238 } else if (name.equals("hostname")) { 1239 StringExtractor extractor(value); 1240 extractor.GetHexByteString(m_hostname); 1241 ++num_keys_decoded; 1242 } else if (name.equals("os_kernel")) { 1243 StringExtractor extractor(value); 1244 extractor.GetHexByteString(m_os_kernel); 1245 ++num_keys_decoded; 1246 } else if (name.equals("ostype")) { 1247 ParseOSType(value, os_name, environment); 1248 ++num_keys_decoded; 1249 } else if (name.equals("vendor")) { 1250 vendor_name = std::string(value); 1251 ++num_keys_decoded; 1252 } else if (name.equals("endian")) { 1253 byte_order = llvm::StringSwitch<lldb::ByteOrder>(value) 1254 .Case("little", eByteOrderLittle) 1255 .Case("big", eByteOrderBig) 1256 .Case("pdp", eByteOrderPDP) 1257 .Default(eByteOrderInvalid); 1258 if (byte_order != eByteOrderInvalid) 1259 ++num_keys_decoded; 1260 } else if (name.equals("ptrsize")) { 1261 if (!value.getAsInteger(0, pointer_byte_size)) 1262 ++num_keys_decoded; 1263 } else if (name.equals("addressing_bits")) { 1264 if (!value.getAsInteger(0, m_addressing_bits)) 1265 ++num_keys_decoded; 1266 } else if (name.equals("os_version") || 1267 name.equals("version")) // Older debugserver binaries used 1268 // the "version" key instead of 1269 // "os_version"... 1270 { 1271 if (!m_os_version.tryParse(value)) 1272 ++num_keys_decoded; 1273 } else if (name.equals("maccatalyst_version")) { 1274 if (!m_maccatalyst_version.tryParse(value)) 1275 ++num_keys_decoded; 1276 } else if (name.equals("watchpoint_exceptions_received")) { 1277 m_watchpoints_trigger_after_instruction = 1278 llvm::StringSwitch<LazyBool>(value) 1279 .Case("before", eLazyBoolNo) 1280 .Case("after", eLazyBoolYes) 1281 .Default(eLazyBoolCalculate); 1282 if (m_watchpoints_trigger_after_instruction != eLazyBoolCalculate) 1283 ++num_keys_decoded; 1284 } else if (name.equals("default_packet_timeout")) { 1285 uint32_t timeout_seconds; 1286 if (!value.getAsInteger(0, timeout_seconds)) { 1287 m_default_packet_timeout = seconds(timeout_seconds); 1288 SetPacketTimeout(m_default_packet_timeout); 1289 ++num_keys_decoded; 1290 } 1291 } else if (name.equals("vm-page-size")) { 1292 int page_size; 1293 if (!value.getAsInteger(0, page_size)) { 1294 m_target_vm_page_size = page_size; 1295 ++num_keys_decoded; 1296 } 1297 } 1298 } 1299 1300 if (num_keys_decoded > 0) 1301 m_qHostInfo_is_valid = eLazyBoolYes; 1302 1303 if (triple.empty()) { 1304 if (arch_name.empty()) { 1305 if (cpu != LLDB_INVALID_CPUTYPE) { 1306 m_host_arch.SetArchitecture(eArchTypeMachO, cpu, sub); 1307 if (pointer_byte_size) { 1308 assert(pointer_byte_size == m_host_arch.GetAddressByteSize()); 1309 } 1310 if (byte_order != eByteOrderInvalid) { 1311 assert(byte_order == m_host_arch.GetByteOrder()); 1312 } 1313 1314 if (!vendor_name.empty()) 1315 m_host_arch.GetTriple().setVendorName( 1316 llvm::StringRef(vendor_name)); 1317 if (!os_name.empty()) 1318 m_host_arch.GetTriple().setOSName(llvm::StringRef(os_name)); 1319 if (!environment.empty()) 1320 m_host_arch.GetTriple().setEnvironmentName(environment); 1321 } 1322 } else { 1323 std::string triple; 1324 triple += arch_name; 1325 if (!vendor_name.empty() || !os_name.empty()) { 1326 triple += '-'; 1327 if (vendor_name.empty()) 1328 triple += "unknown"; 1329 else 1330 triple += vendor_name; 1331 triple += '-'; 1332 if (os_name.empty()) 1333 triple += "unknown"; 1334 else 1335 triple += os_name; 1336 } 1337 m_host_arch.SetTriple(triple.c_str()); 1338 1339 llvm::Triple &host_triple = m_host_arch.GetTriple(); 1340 if (host_triple.getVendor() == llvm::Triple::Apple && 1341 host_triple.getOS() == llvm::Triple::Darwin) { 1342 switch (m_host_arch.GetMachine()) { 1343 case llvm::Triple::aarch64: 1344 case llvm::Triple::aarch64_32: 1345 case llvm::Triple::arm: 1346 case llvm::Triple::thumb: 1347 host_triple.setOS(llvm::Triple::IOS); 1348 break; 1349 default: 1350 host_triple.setOS(llvm::Triple::MacOSX); 1351 break; 1352 } 1353 } 1354 if (pointer_byte_size) { 1355 assert(pointer_byte_size == m_host_arch.GetAddressByteSize()); 1356 } 1357 if (byte_order != eByteOrderInvalid) { 1358 assert(byte_order == m_host_arch.GetByteOrder()); 1359 } 1360 } 1361 } else { 1362 m_host_arch.SetTriple(triple.c_str()); 1363 if (pointer_byte_size) { 1364 assert(pointer_byte_size == m_host_arch.GetAddressByteSize()); 1365 } 1366 if (byte_order != eByteOrderInvalid) { 1367 assert(byte_order == m_host_arch.GetByteOrder()); 1368 } 1369 1370 LLDB_LOGF(log, 1371 "GDBRemoteCommunicationClient::%s parsed host " 1372 "architecture as %s, triple as %s from triple text %s", 1373 __FUNCTION__, 1374 m_host_arch.GetArchitectureName() 1375 ? m_host_arch.GetArchitectureName() 1376 : "<null-arch-name>", 1377 m_host_arch.GetTriple().getTriple().c_str(), 1378 triple.c_str()); 1379 } 1380 if (!distribution_id.empty()) 1381 m_host_arch.SetDistributionId(distribution_id.c_str()); 1382 } 1383 } 1384 } 1385 return m_qHostInfo_is_valid == eLazyBoolYes; 1386 } 1387 1388 int GDBRemoteCommunicationClient::SendStdinNotification(const char *data, 1389 size_t data_len) { 1390 StreamString packet; 1391 packet.PutCString("I"); 1392 packet.PutBytesAsRawHex8(data, data_len); 1393 StringExtractorGDBRemote response; 1394 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 1395 PacketResult::Success) { 1396 return 0; 1397 } 1398 return response.GetError(); 1399 } 1400 1401 const lldb_private::ArchSpec & 1402 GDBRemoteCommunicationClient::GetHostArchitecture() { 1403 if (m_qHostInfo_is_valid == eLazyBoolCalculate) 1404 GetHostInfo(); 1405 return m_host_arch; 1406 } 1407 1408 uint32_t GDBRemoteCommunicationClient::GetAddressingBits() { 1409 if (m_qHostInfo_is_valid == eLazyBoolCalculate) 1410 GetHostInfo(); 1411 return m_addressing_bits; 1412 } 1413 seconds GDBRemoteCommunicationClient::GetHostDefaultPacketTimeout() { 1414 if (m_qHostInfo_is_valid == eLazyBoolCalculate) 1415 GetHostInfo(); 1416 return m_default_packet_timeout; 1417 } 1418 1419 addr_t GDBRemoteCommunicationClient::AllocateMemory(size_t size, 1420 uint32_t permissions) { 1421 if (m_supports_alloc_dealloc_memory != eLazyBoolNo) { 1422 m_supports_alloc_dealloc_memory = eLazyBoolYes; 1423 char packet[64]; 1424 const int packet_len = ::snprintf( 1425 packet, sizeof(packet), "_M%" PRIx64 ",%s%s%s", (uint64_t)size, 1426 permissions & lldb::ePermissionsReadable ? "r" : "", 1427 permissions & lldb::ePermissionsWritable ? "w" : "", 1428 permissions & lldb::ePermissionsExecutable ? "x" : ""); 1429 assert(packet_len < (int)sizeof(packet)); 1430 UNUSED_IF_ASSERT_DISABLED(packet_len); 1431 StringExtractorGDBRemote response; 1432 if (SendPacketAndWaitForResponse(packet, response) == 1433 PacketResult::Success) { 1434 if (response.IsUnsupportedResponse()) 1435 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1436 else if (!response.IsErrorResponse()) 1437 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 1438 } else { 1439 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1440 } 1441 } 1442 return LLDB_INVALID_ADDRESS; 1443 } 1444 1445 bool GDBRemoteCommunicationClient::DeallocateMemory(addr_t addr) { 1446 if (m_supports_alloc_dealloc_memory != eLazyBoolNo) { 1447 m_supports_alloc_dealloc_memory = eLazyBoolYes; 1448 char packet[64]; 1449 const int packet_len = 1450 ::snprintf(packet, sizeof(packet), "_m%" PRIx64, (uint64_t)addr); 1451 assert(packet_len < (int)sizeof(packet)); 1452 UNUSED_IF_ASSERT_DISABLED(packet_len); 1453 StringExtractorGDBRemote response; 1454 if (SendPacketAndWaitForResponse(packet, response) == 1455 PacketResult::Success) { 1456 if (response.IsUnsupportedResponse()) 1457 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1458 else if (response.IsOKResponse()) 1459 return true; 1460 } else { 1461 m_supports_alloc_dealloc_memory = eLazyBoolNo; 1462 } 1463 } 1464 return false; 1465 } 1466 1467 Status GDBRemoteCommunicationClient::Detach(bool keep_stopped, 1468 lldb::pid_t pid) { 1469 Status error; 1470 lldb_private::StreamString packet; 1471 1472 packet.PutChar('D'); 1473 if (keep_stopped) { 1474 if (m_supports_detach_stay_stopped == eLazyBoolCalculate) { 1475 char packet[64]; 1476 const int packet_len = 1477 ::snprintf(packet, sizeof(packet), "qSupportsDetachAndStayStopped:"); 1478 assert(packet_len < (int)sizeof(packet)); 1479 UNUSED_IF_ASSERT_DISABLED(packet_len); 1480 StringExtractorGDBRemote response; 1481 if (SendPacketAndWaitForResponse(packet, response) == 1482 PacketResult::Success && 1483 response.IsOKResponse()) { 1484 m_supports_detach_stay_stopped = eLazyBoolYes; 1485 } else { 1486 m_supports_detach_stay_stopped = eLazyBoolNo; 1487 } 1488 } 1489 1490 if (m_supports_detach_stay_stopped == eLazyBoolNo) { 1491 error.SetErrorString("Stays stopped not supported by this target."); 1492 return error; 1493 } else { 1494 packet.PutChar('1'); 1495 } 1496 } 1497 1498 if (GetMultiprocessSupported()) { 1499 // Some servers (e.g. qemu) require specifying the PID even if only a single 1500 // process is running. 1501 if (pid == LLDB_INVALID_PROCESS_ID) 1502 pid = GetCurrentProcessID(); 1503 packet.PutChar(';'); 1504 packet.PutHex64(pid); 1505 } else if (pid != LLDB_INVALID_PROCESS_ID) { 1506 error.SetErrorString("Multiprocess extension not supported by the server."); 1507 return error; 1508 } 1509 1510 StringExtractorGDBRemote response; 1511 PacketResult packet_result = 1512 SendPacketAndWaitForResponse(packet.GetString(), response); 1513 if (packet_result != PacketResult::Success) 1514 error.SetErrorString("Sending isconnect packet failed."); 1515 return error; 1516 } 1517 1518 Status GDBRemoteCommunicationClient::GetMemoryRegionInfo( 1519 lldb::addr_t addr, lldb_private::MemoryRegionInfo ®ion_info) { 1520 Status error; 1521 region_info.Clear(); 1522 1523 if (m_supports_memory_region_info != eLazyBoolNo) { 1524 m_supports_memory_region_info = eLazyBoolYes; 1525 char packet[64]; 1526 const int packet_len = ::snprintf( 1527 packet, sizeof(packet), "qMemoryRegionInfo:%" PRIx64, (uint64_t)addr); 1528 assert(packet_len < (int)sizeof(packet)); 1529 UNUSED_IF_ASSERT_DISABLED(packet_len); 1530 StringExtractorGDBRemote response; 1531 if (SendPacketAndWaitForResponse(packet, response) == 1532 PacketResult::Success && 1533 response.GetResponseType() == StringExtractorGDBRemote::eResponse) { 1534 llvm::StringRef name; 1535 llvm::StringRef value; 1536 addr_t addr_value = LLDB_INVALID_ADDRESS; 1537 bool success = true; 1538 bool saw_permissions = false; 1539 while (success && response.GetNameColonValue(name, value)) { 1540 if (name.equals("start")) { 1541 if (!value.getAsInteger(16, addr_value)) 1542 region_info.GetRange().SetRangeBase(addr_value); 1543 } else if (name.equals("size")) { 1544 if (!value.getAsInteger(16, addr_value)) { 1545 region_info.GetRange().SetByteSize(addr_value); 1546 if (region_info.GetRange().GetRangeEnd() < 1547 region_info.GetRange().GetRangeBase()) { 1548 // Range size overflowed, truncate it. 1549 region_info.GetRange().SetRangeEnd(LLDB_INVALID_ADDRESS); 1550 } 1551 } 1552 } else if (name.equals("permissions") && 1553 region_info.GetRange().IsValid()) { 1554 saw_permissions = true; 1555 if (region_info.GetRange().Contains(addr)) { 1556 if (value.contains('r')) 1557 region_info.SetReadable(MemoryRegionInfo::eYes); 1558 else 1559 region_info.SetReadable(MemoryRegionInfo::eNo); 1560 1561 if (value.contains('w')) 1562 region_info.SetWritable(MemoryRegionInfo::eYes); 1563 else 1564 region_info.SetWritable(MemoryRegionInfo::eNo); 1565 1566 if (value.contains('x')) 1567 region_info.SetExecutable(MemoryRegionInfo::eYes); 1568 else 1569 region_info.SetExecutable(MemoryRegionInfo::eNo); 1570 1571 region_info.SetMapped(MemoryRegionInfo::eYes); 1572 } else { 1573 // The reported region does not contain this address -- we're 1574 // looking at an unmapped page 1575 region_info.SetReadable(MemoryRegionInfo::eNo); 1576 region_info.SetWritable(MemoryRegionInfo::eNo); 1577 region_info.SetExecutable(MemoryRegionInfo::eNo); 1578 region_info.SetMapped(MemoryRegionInfo::eNo); 1579 } 1580 } else if (name.equals("name")) { 1581 StringExtractorGDBRemote name_extractor(value); 1582 std::string name; 1583 name_extractor.GetHexByteString(name); 1584 region_info.SetName(name.c_str()); 1585 } else if (name.equals("flags")) { 1586 region_info.SetMemoryTagged(MemoryRegionInfo::eNo); 1587 1588 llvm::StringRef flags = value; 1589 llvm::StringRef flag; 1590 while (flags.size()) { 1591 flags = flags.ltrim(); 1592 std::tie(flag, flags) = flags.split(' '); 1593 // To account for trailing whitespace 1594 if (flag.size()) { 1595 if (flag == "mt") { 1596 region_info.SetMemoryTagged(MemoryRegionInfo::eYes); 1597 break; 1598 } 1599 } 1600 } 1601 } else if (name.equals("type")) { 1602 std::string comma_sep_str = value.str(); 1603 size_t comma_pos; 1604 while ((comma_pos = comma_sep_str.find(',')) != std::string::npos) { 1605 comma_sep_str[comma_pos] = '\0'; 1606 if (comma_sep_str == "stack") { 1607 region_info.SetIsStackMemory(MemoryRegionInfo::eYes); 1608 } 1609 } 1610 // handle final (or only) type of "stack" 1611 if (comma_sep_str == "stack") { 1612 region_info.SetIsStackMemory(MemoryRegionInfo::eYes); 1613 } 1614 } else if (name.equals("error")) { 1615 StringExtractorGDBRemote error_extractor(value); 1616 std::string error_string; 1617 // Now convert the HEX bytes into a string value 1618 error_extractor.GetHexByteString(error_string); 1619 error.SetErrorString(error_string.c_str()); 1620 } else if (name.equals("dirty-pages")) { 1621 std::vector<addr_t> dirty_page_list; 1622 for (llvm::StringRef x : llvm::split(value, ',')) { 1623 addr_t page; 1624 x.consume_front("0x"); 1625 if (llvm::to_integer(x, page, 16)) 1626 dirty_page_list.push_back(page); 1627 } 1628 region_info.SetDirtyPageList(dirty_page_list); 1629 } 1630 } 1631 1632 if (m_target_vm_page_size != 0) 1633 region_info.SetPageSize(m_target_vm_page_size); 1634 1635 if (region_info.GetRange().IsValid()) { 1636 // We got a valid address range back but no permissions -- which means 1637 // this is an unmapped page 1638 if (!saw_permissions) { 1639 region_info.SetReadable(MemoryRegionInfo::eNo); 1640 region_info.SetWritable(MemoryRegionInfo::eNo); 1641 region_info.SetExecutable(MemoryRegionInfo::eNo); 1642 region_info.SetMapped(MemoryRegionInfo::eNo); 1643 } 1644 } else { 1645 // We got an invalid address range back 1646 error.SetErrorString("Server returned invalid range"); 1647 } 1648 } else { 1649 m_supports_memory_region_info = eLazyBoolNo; 1650 } 1651 } 1652 1653 if (m_supports_memory_region_info == eLazyBoolNo) { 1654 error.SetErrorString("qMemoryRegionInfo is not supported"); 1655 } 1656 1657 // Try qXfer:memory-map:read to get region information not included in 1658 // qMemoryRegionInfo 1659 MemoryRegionInfo qXfer_region_info; 1660 Status qXfer_error = GetQXferMemoryMapRegionInfo(addr, qXfer_region_info); 1661 1662 if (error.Fail()) { 1663 // If qMemoryRegionInfo failed, but qXfer:memory-map:read succeeded, use 1664 // the qXfer result as a fallback 1665 if (qXfer_error.Success()) { 1666 region_info = qXfer_region_info; 1667 error.Clear(); 1668 } else { 1669 region_info.Clear(); 1670 } 1671 } else if (qXfer_error.Success()) { 1672 // If both qMemoryRegionInfo and qXfer:memory-map:read succeeded, and if 1673 // both regions are the same range, update the result to include the flash- 1674 // memory information that is specific to the qXfer result. 1675 if (region_info.GetRange() == qXfer_region_info.GetRange()) { 1676 region_info.SetFlash(qXfer_region_info.GetFlash()); 1677 region_info.SetBlocksize(qXfer_region_info.GetBlocksize()); 1678 } 1679 } 1680 return error; 1681 } 1682 1683 Status GDBRemoteCommunicationClient::GetQXferMemoryMapRegionInfo( 1684 lldb::addr_t addr, MemoryRegionInfo ®ion) { 1685 Status error = LoadQXferMemoryMap(); 1686 if (!error.Success()) 1687 return error; 1688 for (const auto &map_region : m_qXfer_memory_map) { 1689 if (map_region.GetRange().Contains(addr)) { 1690 region = map_region; 1691 return error; 1692 } 1693 } 1694 error.SetErrorString("Region not found"); 1695 return error; 1696 } 1697 1698 Status GDBRemoteCommunicationClient::LoadQXferMemoryMap() { 1699 1700 Status error; 1701 1702 if (m_qXfer_memory_map_loaded) 1703 // Already loaded, return success 1704 return error; 1705 1706 if (!XMLDocument::XMLEnabled()) { 1707 error.SetErrorString("XML is not supported"); 1708 return error; 1709 } 1710 1711 if (!GetQXferMemoryMapReadSupported()) { 1712 error.SetErrorString("Memory map is not supported"); 1713 return error; 1714 } 1715 1716 llvm::Expected<std::string> xml = ReadExtFeature("memory-map", ""); 1717 if (!xml) 1718 return Status(xml.takeError()); 1719 1720 XMLDocument xml_document; 1721 1722 if (!xml_document.ParseMemory(xml->c_str(), xml->size())) { 1723 error.SetErrorString("Failed to parse memory map xml"); 1724 return error; 1725 } 1726 1727 XMLNode map_node = xml_document.GetRootElement("memory-map"); 1728 if (!map_node) { 1729 error.SetErrorString("Invalid root node in memory map xml"); 1730 return error; 1731 } 1732 1733 m_qXfer_memory_map.clear(); 1734 1735 map_node.ForEachChildElement([this](const XMLNode &memory_node) -> bool { 1736 if (!memory_node.IsElement()) 1737 return true; 1738 if (memory_node.GetName() != "memory") 1739 return true; 1740 auto type = memory_node.GetAttributeValue("type", ""); 1741 uint64_t start; 1742 uint64_t length; 1743 if (!memory_node.GetAttributeValueAsUnsigned("start", start)) 1744 return true; 1745 if (!memory_node.GetAttributeValueAsUnsigned("length", length)) 1746 return true; 1747 MemoryRegionInfo region; 1748 region.GetRange().SetRangeBase(start); 1749 region.GetRange().SetByteSize(length); 1750 if (type == "rom") { 1751 region.SetReadable(MemoryRegionInfo::eYes); 1752 this->m_qXfer_memory_map.push_back(region); 1753 } else if (type == "ram") { 1754 region.SetReadable(MemoryRegionInfo::eYes); 1755 region.SetWritable(MemoryRegionInfo::eYes); 1756 this->m_qXfer_memory_map.push_back(region); 1757 } else if (type == "flash") { 1758 region.SetFlash(MemoryRegionInfo::eYes); 1759 memory_node.ForEachChildElement( 1760 [®ion](const XMLNode &prop_node) -> bool { 1761 if (!prop_node.IsElement()) 1762 return true; 1763 if (prop_node.GetName() != "property") 1764 return true; 1765 auto propname = prop_node.GetAttributeValue("name", ""); 1766 if (propname == "blocksize") { 1767 uint64_t blocksize; 1768 if (prop_node.GetElementTextAsUnsigned(blocksize)) 1769 region.SetBlocksize(blocksize); 1770 } 1771 return true; 1772 }); 1773 this->m_qXfer_memory_map.push_back(region); 1774 } 1775 return true; 1776 }); 1777 1778 m_qXfer_memory_map_loaded = true; 1779 1780 return error; 1781 } 1782 1783 Status GDBRemoteCommunicationClient::GetWatchpointSupportInfo(uint32_t &num) { 1784 Status error; 1785 1786 if (m_supports_watchpoint_support_info == eLazyBoolYes) { 1787 num = m_num_supported_hardware_watchpoints; 1788 return error; 1789 } 1790 1791 // Set num to 0 first. 1792 num = 0; 1793 if (m_supports_watchpoint_support_info != eLazyBoolNo) { 1794 StringExtractorGDBRemote response; 1795 if (SendPacketAndWaitForResponse("qWatchpointSupportInfo:", response) == 1796 PacketResult::Success) { 1797 m_supports_watchpoint_support_info = eLazyBoolYes; 1798 llvm::StringRef name; 1799 llvm::StringRef value; 1800 bool found_num_field = false; 1801 while (response.GetNameColonValue(name, value)) { 1802 if (name.equals("num")) { 1803 value.getAsInteger(0, m_num_supported_hardware_watchpoints); 1804 num = m_num_supported_hardware_watchpoints; 1805 found_num_field = true; 1806 } 1807 } 1808 if (!found_num_field) { 1809 m_supports_watchpoint_support_info = eLazyBoolNo; 1810 } 1811 } else { 1812 m_supports_watchpoint_support_info = eLazyBoolNo; 1813 } 1814 } 1815 1816 if (m_supports_watchpoint_support_info == eLazyBoolNo) { 1817 error.SetErrorString("qWatchpointSupportInfo is not supported"); 1818 } 1819 return error; 1820 } 1821 1822 lldb_private::Status GDBRemoteCommunicationClient::GetWatchpointSupportInfo( 1823 uint32_t &num, bool &after, const ArchSpec &arch) { 1824 Status error(GetWatchpointSupportInfo(num)); 1825 if (error.Success()) 1826 error = GetWatchpointsTriggerAfterInstruction(after, arch); 1827 return error; 1828 } 1829 1830 lldb_private::Status 1831 GDBRemoteCommunicationClient::GetWatchpointsTriggerAfterInstruction( 1832 bool &after, const ArchSpec &arch) { 1833 Status error; 1834 llvm::Triple triple = arch.GetTriple(); 1835 1836 // we assume watchpoints will happen after running the relevant opcode and we 1837 // only want to override this behavior if we have explicitly received a 1838 // qHostInfo telling us otherwise 1839 if (m_qHostInfo_is_valid != eLazyBoolYes) { 1840 // On targets like MIPS and ppc64, watchpoint exceptions are always 1841 // generated before the instruction is executed. The connected target may 1842 // not support qHostInfo or qWatchpointSupportInfo packets. 1843 after = !(triple.isMIPS() || triple.isPPC64()); 1844 } else { 1845 // For MIPS and ppc64, set m_watchpoints_trigger_after_instruction to 1846 // eLazyBoolNo if it is not calculated before. 1847 if (m_watchpoints_trigger_after_instruction == eLazyBoolCalculate && 1848 (triple.isMIPS() || triple.isPPC64())) 1849 m_watchpoints_trigger_after_instruction = eLazyBoolNo; 1850 1851 after = (m_watchpoints_trigger_after_instruction != eLazyBoolNo); 1852 } 1853 return error; 1854 } 1855 1856 int GDBRemoteCommunicationClient::SetSTDIN(const FileSpec &file_spec) { 1857 if (file_spec) { 1858 std::string path{file_spec.GetPath(false)}; 1859 StreamString packet; 1860 packet.PutCString("QSetSTDIN:"); 1861 packet.PutStringAsRawHex8(path); 1862 1863 StringExtractorGDBRemote response; 1864 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 1865 PacketResult::Success) { 1866 if (response.IsOKResponse()) 1867 return 0; 1868 uint8_t error = response.GetError(); 1869 if (error) 1870 return error; 1871 } 1872 } 1873 return -1; 1874 } 1875 1876 int GDBRemoteCommunicationClient::SetSTDOUT(const FileSpec &file_spec) { 1877 if (file_spec) { 1878 std::string path{file_spec.GetPath(false)}; 1879 StreamString packet; 1880 packet.PutCString("QSetSTDOUT:"); 1881 packet.PutStringAsRawHex8(path); 1882 1883 StringExtractorGDBRemote response; 1884 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 1885 PacketResult::Success) { 1886 if (response.IsOKResponse()) 1887 return 0; 1888 uint8_t error = response.GetError(); 1889 if (error) 1890 return error; 1891 } 1892 } 1893 return -1; 1894 } 1895 1896 int GDBRemoteCommunicationClient::SetSTDERR(const FileSpec &file_spec) { 1897 if (file_spec) { 1898 std::string path{file_spec.GetPath(false)}; 1899 StreamString packet; 1900 packet.PutCString("QSetSTDERR:"); 1901 packet.PutStringAsRawHex8(path); 1902 1903 StringExtractorGDBRemote response; 1904 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 1905 PacketResult::Success) { 1906 if (response.IsOKResponse()) 1907 return 0; 1908 uint8_t error = response.GetError(); 1909 if (error) 1910 return error; 1911 } 1912 } 1913 return -1; 1914 } 1915 1916 bool GDBRemoteCommunicationClient::GetWorkingDir(FileSpec &working_dir) { 1917 StringExtractorGDBRemote response; 1918 if (SendPacketAndWaitForResponse("qGetWorkingDir", response) == 1919 PacketResult::Success) { 1920 if (response.IsUnsupportedResponse()) 1921 return false; 1922 if (response.IsErrorResponse()) 1923 return false; 1924 std::string cwd; 1925 response.GetHexByteString(cwd); 1926 working_dir.SetFile(cwd, GetHostArchitecture().GetTriple()); 1927 return !cwd.empty(); 1928 } 1929 return false; 1930 } 1931 1932 int GDBRemoteCommunicationClient::SetWorkingDir(const FileSpec &working_dir) { 1933 if (working_dir) { 1934 std::string path{working_dir.GetPath(false)}; 1935 StreamString packet; 1936 packet.PutCString("QSetWorkingDir:"); 1937 packet.PutStringAsRawHex8(path); 1938 1939 StringExtractorGDBRemote response; 1940 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 1941 PacketResult::Success) { 1942 if (response.IsOKResponse()) 1943 return 0; 1944 uint8_t error = response.GetError(); 1945 if (error) 1946 return error; 1947 } 1948 } 1949 return -1; 1950 } 1951 1952 int GDBRemoteCommunicationClient::SetDisableASLR(bool enable) { 1953 char packet[32]; 1954 const int packet_len = 1955 ::snprintf(packet, sizeof(packet), "QSetDisableASLR:%i", enable ? 1 : 0); 1956 assert(packet_len < (int)sizeof(packet)); 1957 UNUSED_IF_ASSERT_DISABLED(packet_len); 1958 StringExtractorGDBRemote response; 1959 if (SendPacketAndWaitForResponse(packet, response) == PacketResult::Success) { 1960 if (response.IsOKResponse()) 1961 return 0; 1962 uint8_t error = response.GetError(); 1963 if (error) 1964 return error; 1965 } 1966 return -1; 1967 } 1968 1969 int GDBRemoteCommunicationClient::SetDetachOnError(bool enable) { 1970 char packet[32]; 1971 const int packet_len = ::snprintf(packet, sizeof(packet), 1972 "QSetDetachOnError:%i", enable ? 1 : 0); 1973 assert(packet_len < (int)sizeof(packet)); 1974 UNUSED_IF_ASSERT_DISABLED(packet_len); 1975 StringExtractorGDBRemote response; 1976 if (SendPacketAndWaitForResponse(packet, response) == PacketResult::Success) { 1977 if (response.IsOKResponse()) 1978 return 0; 1979 uint8_t error = response.GetError(); 1980 if (error) 1981 return error; 1982 } 1983 return -1; 1984 } 1985 1986 bool GDBRemoteCommunicationClient::DecodeProcessInfoResponse( 1987 StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) { 1988 if (response.IsNormalResponse()) { 1989 llvm::StringRef name; 1990 llvm::StringRef value; 1991 StringExtractor extractor; 1992 1993 uint32_t cpu = LLDB_INVALID_CPUTYPE; 1994 uint32_t sub = 0; 1995 std::string vendor; 1996 std::string os_type; 1997 1998 while (response.GetNameColonValue(name, value)) { 1999 if (name.equals("pid")) { 2000 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 2001 value.getAsInteger(0, pid); 2002 process_info.SetProcessID(pid); 2003 } else if (name.equals("ppid")) { 2004 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 2005 value.getAsInteger(0, pid); 2006 process_info.SetParentProcessID(pid); 2007 } else if (name.equals("uid")) { 2008 uint32_t uid = UINT32_MAX; 2009 value.getAsInteger(0, uid); 2010 process_info.SetUserID(uid); 2011 } else if (name.equals("euid")) { 2012 uint32_t uid = UINT32_MAX; 2013 value.getAsInteger(0, uid); 2014 process_info.SetEffectiveUserID(uid); 2015 } else if (name.equals("gid")) { 2016 uint32_t gid = UINT32_MAX; 2017 value.getAsInteger(0, gid); 2018 process_info.SetGroupID(gid); 2019 } else if (name.equals("egid")) { 2020 uint32_t gid = UINT32_MAX; 2021 value.getAsInteger(0, gid); 2022 process_info.SetEffectiveGroupID(gid); 2023 } else if (name.equals("triple")) { 2024 StringExtractor extractor(value); 2025 std::string triple; 2026 extractor.GetHexByteString(triple); 2027 process_info.GetArchitecture().SetTriple(triple.c_str()); 2028 } else if (name.equals("name")) { 2029 StringExtractor extractor(value); 2030 // The process name from ASCII hex bytes since we can't control the 2031 // characters in a process name 2032 std::string name; 2033 extractor.GetHexByteString(name); 2034 process_info.GetExecutableFile().SetFile(name, FileSpec::Style::native); 2035 } else if (name.equals("args")) { 2036 llvm::StringRef encoded_args(value), hex_arg; 2037 2038 bool is_arg0 = true; 2039 while (!encoded_args.empty()) { 2040 std::tie(hex_arg, encoded_args) = encoded_args.split('-'); 2041 std::string arg; 2042 StringExtractor extractor(hex_arg); 2043 if (extractor.GetHexByteString(arg) * 2 != hex_arg.size()) { 2044 // In case of wrong encoding, we discard all the arguments 2045 process_info.GetArguments().Clear(); 2046 process_info.SetArg0(""); 2047 break; 2048 } 2049 if (is_arg0) 2050 process_info.SetArg0(arg); 2051 else 2052 process_info.GetArguments().AppendArgument(arg); 2053 is_arg0 = false; 2054 } 2055 } else if (name.equals("cputype")) { 2056 value.getAsInteger(0, cpu); 2057 } else if (name.equals("cpusubtype")) { 2058 value.getAsInteger(0, sub); 2059 } else if (name.equals("vendor")) { 2060 vendor = std::string(value); 2061 } else if (name.equals("ostype")) { 2062 os_type = std::string(value); 2063 } 2064 } 2065 2066 if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty()) { 2067 if (vendor == "apple") { 2068 process_info.GetArchitecture().SetArchitecture(eArchTypeMachO, cpu, 2069 sub); 2070 process_info.GetArchitecture().GetTriple().setVendorName( 2071 llvm::StringRef(vendor)); 2072 process_info.GetArchitecture().GetTriple().setOSName( 2073 llvm::StringRef(os_type)); 2074 } 2075 } 2076 2077 if (process_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) 2078 return true; 2079 } 2080 return false; 2081 } 2082 2083 bool GDBRemoteCommunicationClient::GetProcessInfo( 2084 lldb::pid_t pid, ProcessInstanceInfo &process_info) { 2085 process_info.Clear(); 2086 2087 if (m_supports_qProcessInfoPID) { 2088 char packet[32]; 2089 const int packet_len = 2090 ::snprintf(packet, sizeof(packet), "qProcessInfoPID:%" PRIu64, pid); 2091 assert(packet_len < (int)sizeof(packet)); 2092 UNUSED_IF_ASSERT_DISABLED(packet_len); 2093 StringExtractorGDBRemote response; 2094 if (SendPacketAndWaitForResponse(packet, response) == 2095 PacketResult::Success) { 2096 return DecodeProcessInfoResponse(response, process_info); 2097 } else { 2098 m_supports_qProcessInfoPID = false; 2099 return false; 2100 } 2101 } 2102 return false; 2103 } 2104 2105 bool GDBRemoteCommunicationClient::GetCurrentProcessInfo(bool allow_lazy) { 2106 Log *log(GetLog(GDBRLog::Process | GDBRLog::Packets)); 2107 2108 if (allow_lazy) { 2109 if (m_qProcessInfo_is_valid == eLazyBoolYes) 2110 return true; 2111 if (m_qProcessInfo_is_valid == eLazyBoolNo) 2112 return false; 2113 } 2114 2115 GetHostInfo(); 2116 2117 StringExtractorGDBRemote response; 2118 if (SendPacketAndWaitForResponse("qProcessInfo", response) == 2119 PacketResult::Success) { 2120 if (response.IsNormalResponse()) { 2121 llvm::StringRef name; 2122 llvm::StringRef value; 2123 uint32_t cpu = LLDB_INVALID_CPUTYPE; 2124 uint32_t sub = 0; 2125 std::string arch_name; 2126 std::string os_name; 2127 std::string environment; 2128 std::string vendor_name; 2129 std::string triple; 2130 std::string elf_abi; 2131 uint32_t pointer_byte_size = 0; 2132 StringExtractor extractor; 2133 ByteOrder byte_order = eByteOrderInvalid; 2134 uint32_t num_keys_decoded = 0; 2135 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 2136 while (response.GetNameColonValue(name, value)) { 2137 if (name.equals("cputype")) { 2138 if (!value.getAsInteger(16, cpu)) 2139 ++num_keys_decoded; 2140 } else if (name.equals("cpusubtype")) { 2141 if (!value.getAsInteger(16, sub)) 2142 ++num_keys_decoded; 2143 } else if (name.equals("triple")) { 2144 StringExtractor extractor(value); 2145 extractor.GetHexByteString(triple); 2146 ++num_keys_decoded; 2147 } else if (name.equals("ostype")) { 2148 ParseOSType(value, os_name, environment); 2149 ++num_keys_decoded; 2150 } else if (name.equals("vendor")) { 2151 vendor_name = std::string(value); 2152 ++num_keys_decoded; 2153 } else if (name.equals("endian")) { 2154 byte_order = llvm::StringSwitch<lldb::ByteOrder>(value) 2155 .Case("little", eByteOrderLittle) 2156 .Case("big", eByteOrderBig) 2157 .Case("pdp", eByteOrderPDP) 2158 .Default(eByteOrderInvalid); 2159 if (byte_order != eByteOrderInvalid) 2160 ++num_keys_decoded; 2161 } else if (name.equals("ptrsize")) { 2162 if (!value.getAsInteger(16, pointer_byte_size)) 2163 ++num_keys_decoded; 2164 } else if (name.equals("pid")) { 2165 if (!value.getAsInteger(16, pid)) 2166 ++num_keys_decoded; 2167 } else if (name.equals("elf_abi")) { 2168 elf_abi = std::string(value); 2169 ++num_keys_decoded; 2170 } else if (name.equals("main-binary-uuid")) { 2171 m_process_standalone_uuid.SetFromStringRef(value); 2172 ++num_keys_decoded; 2173 } else if (name.equals("main-binary-slide")) { 2174 StringExtractor extractor(value); 2175 m_process_standalone_value = 2176 extractor.GetU64(LLDB_INVALID_ADDRESS, 16); 2177 if (m_process_standalone_value != LLDB_INVALID_ADDRESS) { 2178 m_process_standalone_value_is_offset = true; 2179 ++num_keys_decoded; 2180 } 2181 } else if (name.equals("main-binary-address")) { 2182 StringExtractor extractor(value); 2183 m_process_standalone_value = 2184 extractor.GetU64(LLDB_INVALID_ADDRESS, 16); 2185 if (m_process_standalone_value != LLDB_INVALID_ADDRESS) { 2186 m_process_standalone_value_is_offset = false; 2187 ++num_keys_decoded; 2188 } 2189 } else if (name.equals("binary-addresses")) { 2190 m_binary_addresses.clear(); 2191 ++num_keys_decoded; 2192 for (llvm::StringRef x : llvm::split(value, ',')) { 2193 addr_t vmaddr; 2194 x.consume_front("0x"); 2195 if (llvm::to_integer(x, vmaddr, 16)) 2196 m_binary_addresses.push_back(vmaddr); 2197 } 2198 } 2199 } 2200 if (num_keys_decoded > 0) 2201 m_qProcessInfo_is_valid = eLazyBoolYes; 2202 if (pid != LLDB_INVALID_PROCESS_ID) { 2203 m_curr_pid_is_valid = eLazyBoolYes; 2204 m_curr_pid_run = m_curr_pid = pid; 2205 } 2206 2207 // Set the ArchSpec from the triple if we have it. 2208 if (!triple.empty()) { 2209 m_process_arch.SetTriple(triple.c_str()); 2210 m_process_arch.SetFlags(elf_abi); 2211 if (pointer_byte_size) { 2212 assert(pointer_byte_size == m_process_arch.GetAddressByteSize()); 2213 } 2214 } else if (cpu != LLDB_INVALID_CPUTYPE && !os_name.empty() && 2215 !vendor_name.empty()) { 2216 llvm::Triple triple(llvm::Twine("-") + vendor_name + "-" + os_name); 2217 if (!environment.empty()) 2218 triple.setEnvironmentName(environment); 2219 2220 assert(triple.getObjectFormat() != llvm::Triple::UnknownObjectFormat); 2221 assert(triple.getObjectFormat() != llvm::Triple::Wasm); 2222 assert(triple.getObjectFormat() != llvm::Triple::XCOFF); 2223 switch (triple.getObjectFormat()) { 2224 case llvm::Triple::MachO: 2225 m_process_arch.SetArchitecture(eArchTypeMachO, cpu, sub); 2226 break; 2227 case llvm::Triple::ELF: 2228 m_process_arch.SetArchitecture(eArchTypeELF, cpu, sub); 2229 break; 2230 case llvm::Triple::COFF: 2231 m_process_arch.SetArchitecture(eArchTypeCOFF, cpu, sub); 2232 break; 2233 case llvm::Triple::GOFF: 2234 case llvm::Triple::SPIRV: 2235 case llvm::Triple::Wasm: 2236 case llvm::Triple::XCOFF: 2237 case llvm::Triple::DXContainer: 2238 LLDB_LOGF(log, "error: not supported target architecture"); 2239 return false; 2240 case llvm::Triple::UnknownObjectFormat: 2241 LLDB_LOGF(log, "error: failed to determine target architecture"); 2242 return false; 2243 } 2244 2245 if (pointer_byte_size) { 2246 assert(pointer_byte_size == m_process_arch.GetAddressByteSize()); 2247 } 2248 if (byte_order != eByteOrderInvalid) { 2249 assert(byte_order == m_process_arch.GetByteOrder()); 2250 } 2251 m_process_arch.GetTriple().setVendorName(llvm::StringRef(vendor_name)); 2252 m_process_arch.GetTriple().setOSName(llvm::StringRef(os_name)); 2253 m_process_arch.GetTriple().setEnvironmentName(llvm::StringRef(environment)); 2254 } 2255 return true; 2256 } 2257 } else { 2258 m_qProcessInfo_is_valid = eLazyBoolNo; 2259 } 2260 2261 return false; 2262 } 2263 2264 uint32_t GDBRemoteCommunicationClient::FindProcesses( 2265 const ProcessInstanceInfoMatch &match_info, 2266 ProcessInstanceInfoList &process_infos) { 2267 process_infos.clear(); 2268 2269 if (m_supports_qfProcessInfo) { 2270 StreamString packet; 2271 packet.PutCString("qfProcessInfo"); 2272 if (!match_info.MatchAllProcesses()) { 2273 packet.PutChar(':'); 2274 const char *name = match_info.GetProcessInfo().GetName(); 2275 bool has_name_match = false; 2276 if (name && name[0]) { 2277 has_name_match = true; 2278 NameMatch name_match_type = match_info.GetNameMatchType(); 2279 switch (name_match_type) { 2280 case NameMatch::Ignore: 2281 has_name_match = false; 2282 break; 2283 2284 case NameMatch::Equals: 2285 packet.PutCString("name_match:equals;"); 2286 break; 2287 2288 case NameMatch::Contains: 2289 packet.PutCString("name_match:contains;"); 2290 break; 2291 2292 case NameMatch::StartsWith: 2293 packet.PutCString("name_match:starts_with;"); 2294 break; 2295 2296 case NameMatch::EndsWith: 2297 packet.PutCString("name_match:ends_with;"); 2298 break; 2299 2300 case NameMatch::RegularExpression: 2301 packet.PutCString("name_match:regex;"); 2302 break; 2303 } 2304 if (has_name_match) { 2305 packet.PutCString("name:"); 2306 packet.PutBytesAsRawHex8(name, ::strlen(name)); 2307 packet.PutChar(';'); 2308 } 2309 } 2310 2311 if (match_info.GetProcessInfo().ProcessIDIsValid()) 2312 packet.Printf("pid:%" PRIu64 ";", 2313 match_info.GetProcessInfo().GetProcessID()); 2314 if (match_info.GetProcessInfo().ParentProcessIDIsValid()) 2315 packet.Printf("parent_pid:%" PRIu64 ";", 2316 match_info.GetProcessInfo().GetParentProcessID()); 2317 if (match_info.GetProcessInfo().UserIDIsValid()) 2318 packet.Printf("uid:%u;", match_info.GetProcessInfo().GetUserID()); 2319 if (match_info.GetProcessInfo().GroupIDIsValid()) 2320 packet.Printf("gid:%u;", match_info.GetProcessInfo().GetGroupID()); 2321 if (match_info.GetProcessInfo().EffectiveUserIDIsValid()) 2322 packet.Printf("euid:%u;", 2323 match_info.GetProcessInfo().GetEffectiveUserID()); 2324 if (match_info.GetProcessInfo().EffectiveGroupIDIsValid()) 2325 packet.Printf("egid:%u;", 2326 match_info.GetProcessInfo().GetEffectiveGroupID()); 2327 packet.Printf("all_users:%u;", match_info.GetMatchAllUsers() ? 1 : 0); 2328 if (match_info.GetProcessInfo().GetArchitecture().IsValid()) { 2329 const ArchSpec &match_arch = 2330 match_info.GetProcessInfo().GetArchitecture(); 2331 const llvm::Triple &triple = match_arch.GetTriple(); 2332 packet.PutCString("triple:"); 2333 packet.PutCString(triple.getTriple()); 2334 packet.PutChar(';'); 2335 } 2336 } 2337 StringExtractorGDBRemote response; 2338 // Increase timeout as the first qfProcessInfo packet takes a long time on 2339 // Android. The value of 1min was arrived at empirically. 2340 ScopedTimeout timeout(*this, minutes(1)); 2341 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 2342 PacketResult::Success) { 2343 do { 2344 ProcessInstanceInfo process_info; 2345 if (!DecodeProcessInfoResponse(response, process_info)) 2346 break; 2347 process_infos.push_back(process_info); 2348 response = StringExtractorGDBRemote(); 2349 } while (SendPacketAndWaitForResponse("qsProcessInfo", response) == 2350 PacketResult::Success); 2351 } else { 2352 m_supports_qfProcessInfo = false; 2353 return 0; 2354 } 2355 } 2356 return process_infos.size(); 2357 } 2358 2359 bool GDBRemoteCommunicationClient::GetUserName(uint32_t uid, 2360 std::string &name) { 2361 if (m_supports_qUserName) { 2362 char packet[32]; 2363 const int packet_len = 2364 ::snprintf(packet, sizeof(packet), "qUserName:%i", uid); 2365 assert(packet_len < (int)sizeof(packet)); 2366 UNUSED_IF_ASSERT_DISABLED(packet_len); 2367 StringExtractorGDBRemote response; 2368 if (SendPacketAndWaitForResponse(packet, response) == 2369 PacketResult::Success) { 2370 if (response.IsNormalResponse()) { 2371 // Make sure we parsed the right number of characters. The response is 2372 // the hex encoded user name and should make up the entire packet. If 2373 // there are any non-hex ASCII bytes, the length won't match below.. 2374 if (response.GetHexByteString(name) * 2 == 2375 response.GetStringRef().size()) 2376 return true; 2377 } 2378 } else { 2379 m_supports_qUserName = false; 2380 return false; 2381 } 2382 } 2383 return false; 2384 } 2385 2386 bool GDBRemoteCommunicationClient::GetGroupName(uint32_t gid, 2387 std::string &name) { 2388 if (m_supports_qGroupName) { 2389 char packet[32]; 2390 const int packet_len = 2391 ::snprintf(packet, sizeof(packet), "qGroupName:%i", gid); 2392 assert(packet_len < (int)sizeof(packet)); 2393 UNUSED_IF_ASSERT_DISABLED(packet_len); 2394 StringExtractorGDBRemote response; 2395 if (SendPacketAndWaitForResponse(packet, response) == 2396 PacketResult::Success) { 2397 if (response.IsNormalResponse()) { 2398 // Make sure we parsed the right number of characters. The response is 2399 // the hex encoded group name and should make up the entire packet. If 2400 // there are any non-hex ASCII bytes, the length won't match below.. 2401 if (response.GetHexByteString(name) * 2 == 2402 response.GetStringRef().size()) 2403 return true; 2404 } 2405 } else { 2406 m_supports_qGroupName = false; 2407 return false; 2408 } 2409 } 2410 return false; 2411 } 2412 2413 static void MakeSpeedTestPacket(StreamString &packet, uint32_t send_size, 2414 uint32_t recv_size) { 2415 packet.Clear(); 2416 packet.Printf("qSpeedTest:response_size:%i;data:", recv_size); 2417 uint32_t bytes_left = send_size; 2418 while (bytes_left > 0) { 2419 if (bytes_left >= 26) { 2420 packet.PutCString("abcdefghijklmnopqrstuvwxyz"); 2421 bytes_left -= 26; 2422 } else { 2423 packet.Printf("%*.*s;", bytes_left, bytes_left, 2424 "abcdefghijklmnopqrstuvwxyz"); 2425 bytes_left = 0; 2426 } 2427 } 2428 } 2429 2430 duration<float> 2431 calculate_standard_deviation(const std::vector<duration<float>> &v) { 2432 if (v.size() == 0) 2433 return duration<float>::zero(); 2434 using Dur = duration<float>; 2435 Dur sum = std::accumulate(std::begin(v), std::end(v), Dur()); 2436 Dur mean = sum / v.size(); 2437 float accum = 0; 2438 for (auto d : v) { 2439 float delta = (d - mean).count(); 2440 accum += delta * delta; 2441 }; 2442 2443 return Dur(sqrtf(accum / (v.size() - 1))); 2444 } 2445 2446 void GDBRemoteCommunicationClient::TestPacketSpeed(const uint32_t num_packets, 2447 uint32_t max_send, 2448 uint32_t max_recv, 2449 uint64_t recv_amount, 2450 bool json, Stream &strm) { 2451 2452 if (SendSpeedTestPacket(0, 0)) { 2453 StreamString packet; 2454 if (json) 2455 strm.Printf("{ \"packet_speeds\" : {\n \"num_packets\" : %u,\n " 2456 "\"results\" : [", 2457 num_packets); 2458 else 2459 strm.Printf("Testing sending %u packets of various sizes:\n", 2460 num_packets); 2461 strm.Flush(); 2462 2463 uint32_t result_idx = 0; 2464 uint32_t send_size; 2465 std::vector<duration<float>> packet_times; 2466 2467 for (send_size = 0; send_size <= max_send; 2468 send_size ? send_size *= 2 : send_size = 4) { 2469 for (uint32_t recv_size = 0; recv_size <= max_recv; 2470 recv_size ? recv_size *= 2 : recv_size = 4) { 2471 MakeSpeedTestPacket(packet, send_size, recv_size); 2472 2473 packet_times.clear(); 2474 // Test how long it takes to send 'num_packets' packets 2475 const auto start_time = steady_clock::now(); 2476 for (uint32_t i = 0; i < num_packets; ++i) { 2477 const auto packet_start_time = steady_clock::now(); 2478 StringExtractorGDBRemote response; 2479 SendPacketAndWaitForResponse(packet.GetString(), response); 2480 const auto packet_end_time = steady_clock::now(); 2481 packet_times.push_back(packet_end_time - packet_start_time); 2482 } 2483 const auto end_time = steady_clock::now(); 2484 const auto total_time = end_time - start_time; 2485 2486 float packets_per_second = 2487 ((float)num_packets) / duration<float>(total_time).count(); 2488 auto average_per_packet = num_packets > 0 ? total_time / num_packets 2489 : duration<float>::zero(); 2490 const duration<float> standard_deviation = 2491 calculate_standard_deviation(packet_times); 2492 if (json) { 2493 strm.Format("{0}\n {{\"send_size\" : {1,6}, \"recv_size\" : " 2494 "{2,6}, \"total_time_nsec\" : {3,12:ns-}, " 2495 "\"standard_deviation_nsec\" : {4,9:ns-f0}}", 2496 result_idx > 0 ? "," : "", send_size, recv_size, 2497 total_time, standard_deviation); 2498 ++result_idx; 2499 } else { 2500 strm.Format("qSpeedTest(send={0,7}, recv={1,7}) in {2:s+f9} for " 2501 "{3,9:f2} packets/s ({4,10:ms+f6} per packet) with " 2502 "standard deviation of {5,10:ms+f6}\n", 2503 send_size, recv_size, duration<float>(total_time), 2504 packets_per_second, duration<float>(average_per_packet), 2505 standard_deviation); 2506 } 2507 strm.Flush(); 2508 } 2509 } 2510 2511 const float k_recv_amount_mb = (float)recv_amount / (1024.0f * 1024.0f); 2512 if (json) 2513 strm.Printf("\n ]\n },\n \"download_speed\" : {\n \"byte_size\" " 2514 ": %" PRIu64 ",\n \"results\" : [", 2515 recv_amount); 2516 else 2517 strm.Printf("Testing receiving %2.1fMB of data using varying receive " 2518 "packet sizes:\n", 2519 k_recv_amount_mb); 2520 strm.Flush(); 2521 send_size = 0; 2522 result_idx = 0; 2523 for (uint32_t recv_size = 32; recv_size <= max_recv; recv_size *= 2) { 2524 MakeSpeedTestPacket(packet, send_size, recv_size); 2525 2526 // If we have a receive size, test how long it takes to receive 4MB of 2527 // data 2528 if (recv_size > 0) { 2529 const auto start_time = steady_clock::now(); 2530 uint32_t bytes_read = 0; 2531 uint32_t packet_count = 0; 2532 while (bytes_read < recv_amount) { 2533 StringExtractorGDBRemote response; 2534 SendPacketAndWaitForResponse(packet.GetString(), response); 2535 bytes_read += recv_size; 2536 ++packet_count; 2537 } 2538 const auto end_time = steady_clock::now(); 2539 const auto total_time = end_time - start_time; 2540 float mb_second = ((float)recv_amount) / 2541 duration<float>(total_time).count() / 2542 (1024.0 * 1024.0); 2543 float packets_per_second = 2544 ((float)packet_count) / duration<float>(total_time).count(); 2545 const auto average_per_packet = packet_count > 0 2546 ? total_time / packet_count 2547 : duration<float>::zero(); 2548 2549 if (json) { 2550 strm.Format("{0}\n {{\"send_size\" : {1,6}, \"recv_size\" : " 2551 "{2,6}, \"total_time_nsec\" : {3,12:ns-}}", 2552 result_idx > 0 ? "," : "", send_size, recv_size, 2553 total_time); 2554 ++result_idx; 2555 } else { 2556 strm.Format("qSpeedTest(send={0,7}, recv={1,7}) {2,6} packets needed " 2557 "to receive {3:f1}MB in {4:s+f9} for {5} MB/sec for " 2558 "{6,9:f2} packets/sec ({7,10:ms+f6} per packet)\n", 2559 send_size, recv_size, packet_count, k_recv_amount_mb, 2560 duration<float>(total_time), mb_second, 2561 packets_per_second, duration<float>(average_per_packet)); 2562 } 2563 strm.Flush(); 2564 } 2565 } 2566 if (json) 2567 strm.Printf("\n ]\n }\n}\n"); 2568 else 2569 strm.EOL(); 2570 } 2571 } 2572 2573 bool GDBRemoteCommunicationClient::SendSpeedTestPacket(uint32_t send_size, 2574 uint32_t recv_size) { 2575 StreamString packet; 2576 packet.Printf("qSpeedTest:response_size:%i;data:", recv_size); 2577 uint32_t bytes_left = send_size; 2578 while (bytes_left > 0) { 2579 if (bytes_left >= 26) { 2580 packet.PutCString("abcdefghijklmnopqrstuvwxyz"); 2581 bytes_left -= 26; 2582 } else { 2583 packet.Printf("%*.*s;", bytes_left, bytes_left, 2584 "abcdefghijklmnopqrstuvwxyz"); 2585 bytes_left = 0; 2586 } 2587 } 2588 2589 StringExtractorGDBRemote response; 2590 return SendPacketAndWaitForResponse(packet.GetString(), response) == 2591 PacketResult::Success; 2592 } 2593 2594 bool GDBRemoteCommunicationClient::LaunchGDBServer( 2595 const char *remote_accept_hostname, lldb::pid_t &pid, uint16_t &port, 2596 std::string &socket_name) { 2597 pid = LLDB_INVALID_PROCESS_ID; 2598 port = 0; 2599 socket_name.clear(); 2600 2601 StringExtractorGDBRemote response; 2602 StreamString stream; 2603 stream.PutCString("qLaunchGDBServer;"); 2604 std::string hostname; 2605 if (remote_accept_hostname && remote_accept_hostname[0]) 2606 hostname = remote_accept_hostname; 2607 else { 2608 if (HostInfo::GetHostname(hostname)) { 2609 // Make the GDB server we launch only accept connections from this host 2610 stream.Printf("host:%s;", hostname.c_str()); 2611 } else { 2612 // Make the GDB server we launch accept connections from any host since 2613 // we can't figure out the hostname 2614 stream.Printf("host:*;"); 2615 } 2616 } 2617 // give the process a few seconds to startup 2618 ScopedTimeout timeout(*this, seconds(10)); 2619 2620 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 2621 PacketResult::Success) { 2622 llvm::StringRef name; 2623 llvm::StringRef value; 2624 while (response.GetNameColonValue(name, value)) { 2625 if (name.equals("port")) 2626 value.getAsInteger(0, port); 2627 else if (name.equals("pid")) 2628 value.getAsInteger(0, pid); 2629 else if (name.compare("socket_name") == 0) { 2630 StringExtractor extractor(value); 2631 extractor.GetHexByteString(socket_name); 2632 } 2633 } 2634 return true; 2635 } 2636 return false; 2637 } 2638 2639 size_t GDBRemoteCommunicationClient::QueryGDBServer( 2640 std::vector<std::pair<uint16_t, std::string>> &connection_urls) { 2641 connection_urls.clear(); 2642 2643 StringExtractorGDBRemote response; 2644 if (SendPacketAndWaitForResponse("qQueryGDBServer", response) != 2645 PacketResult::Success) 2646 return 0; 2647 2648 StructuredData::ObjectSP data = 2649 StructuredData::ParseJSON(std::string(response.GetStringRef())); 2650 if (!data) 2651 return 0; 2652 2653 StructuredData::Array *array = data->GetAsArray(); 2654 if (!array) 2655 return 0; 2656 2657 for (size_t i = 0, count = array->GetSize(); i < count; ++i) { 2658 StructuredData::Dictionary *element = nullptr; 2659 if (!array->GetItemAtIndexAsDictionary(i, element)) 2660 continue; 2661 2662 uint16_t port = 0; 2663 if (StructuredData::ObjectSP port_osp = 2664 element->GetValueForKey(llvm::StringRef("port"))) 2665 port = port_osp->GetIntegerValue(0); 2666 2667 std::string socket_name; 2668 if (StructuredData::ObjectSP socket_name_osp = 2669 element->GetValueForKey(llvm::StringRef("socket_name"))) 2670 socket_name = std::string(socket_name_osp->GetStringValue()); 2671 2672 if (port != 0 || !socket_name.empty()) 2673 connection_urls.emplace_back(port, socket_name); 2674 } 2675 return connection_urls.size(); 2676 } 2677 2678 bool GDBRemoteCommunicationClient::KillSpawnedProcess(lldb::pid_t pid) { 2679 StreamString stream; 2680 stream.Printf("qKillSpawnedProcess:%" PRId64, pid); 2681 2682 StringExtractorGDBRemote response; 2683 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 2684 PacketResult::Success) { 2685 if (response.IsOKResponse()) 2686 return true; 2687 } 2688 return false; 2689 } 2690 2691 llvm::Optional<PidTid> 2692 GDBRemoteCommunicationClient::SendSetCurrentThreadPacket(uint64_t tid, 2693 uint64_t pid, 2694 char op) { 2695 lldb_private::StreamString packet; 2696 packet.PutChar('H'); 2697 packet.PutChar(op); 2698 2699 if (pid != LLDB_INVALID_PROCESS_ID) 2700 packet.Printf("p%" PRIx64 ".", pid); 2701 2702 if (tid == UINT64_MAX) 2703 packet.PutCString("-1"); 2704 else 2705 packet.Printf("%" PRIx64, tid); 2706 2707 StringExtractorGDBRemote response; 2708 if (SendPacketAndWaitForResponse(packet.GetString(), response) == 2709 PacketResult::Success) { 2710 if (response.IsOKResponse()) 2711 return {{pid, tid}}; 2712 2713 /* 2714 * Connected bare-iron target (like YAMON gdb-stub) may not have support for 2715 * Hg packet. 2716 * The reply from '?' packet could be as simple as 'S05'. There is no packet 2717 * which can 2718 * give us pid and/or tid. Assume pid=tid=1 in such cases. 2719 */ 2720 if (response.IsUnsupportedResponse() && IsConnected()) 2721 return {{1, 1}}; 2722 } 2723 return std::nullopt; 2724 } 2725 2726 bool GDBRemoteCommunicationClient::SetCurrentThread(uint64_t tid, 2727 uint64_t pid) { 2728 if (m_curr_tid == tid && 2729 (m_curr_pid == pid || LLDB_INVALID_PROCESS_ID == pid)) 2730 return true; 2731 2732 llvm::Optional<PidTid> ret = SendSetCurrentThreadPacket(tid, pid, 'g'); 2733 if (ret) { 2734 if (ret->pid != LLDB_INVALID_PROCESS_ID) 2735 m_curr_pid = ret->pid; 2736 m_curr_tid = ret->tid; 2737 } 2738 return ret.has_value(); 2739 } 2740 2741 bool GDBRemoteCommunicationClient::SetCurrentThreadForRun(uint64_t tid, 2742 uint64_t pid) { 2743 if (m_curr_tid_run == tid && 2744 (m_curr_pid_run == pid || LLDB_INVALID_PROCESS_ID == pid)) 2745 return true; 2746 2747 llvm::Optional<PidTid> ret = SendSetCurrentThreadPacket(tid, pid, 'c'); 2748 if (ret) { 2749 if (ret->pid != LLDB_INVALID_PROCESS_ID) 2750 m_curr_pid_run = ret->pid; 2751 m_curr_tid_run = ret->tid; 2752 } 2753 return ret.has_value(); 2754 } 2755 2756 bool GDBRemoteCommunicationClient::GetStopReply( 2757 StringExtractorGDBRemote &response) { 2758 if (SendPacketAndWaitForResponse("?", response) == PacketResult::Success) 2759 return response.IsNormalResponse(); 2760 return false; 2761 } 2762 2763 bool GDBRemoteCommunicationClient::GetThreadStopInfo( 2764 lldb::tid_t tid, StringExtractorGDBRemote &response) { 2765 if (m_supports_qThreadStopInfo) { 2766 char packet[256]; 2767 int packet_len = 2768 ::snprintf(packet, sizeof(packet), "qThreadStopInfo%" PRIx64, tid); 2769 assert(packet_len < (int)sizeof(packet)); 2770 UNUSED_IF_ASSERT_DISABLED(packet_len); 2771 if (SendPacketAndWaitForResponse(packet, response) == 2772 PacketResult::Success) { 2773 if (response.IsUnsupportedResponse()) 2774 m_supports_qThreadStopInfo = false; 2775 else if (response.IsNormalResponse()) 2776 return true; 2777 else 2778 return false; 2779 } else { 2780 m_supports_qThreadStopInfo = false; 2781 } 2782 } 2783 return false; 2784 } 2785 2786 uint8_t GDBRemoteCommunicationClient::SendGDBStoppointTypePacket( 2787 GDBStoppointType type, bool insert, addr_t addr, uint32_t length, 2788 std::chrono::seconds timeout) { 2789 Log *log = GetLog(LLDBLog::Breakpoints); 2790 LLDB_LOGF(log, "GDBRemoteCommunicationClient::%s() %s at addr = 0x%" PRIx64, 2791 __FUNCTION__, insert ? "add" : "remove", addr); 2792 2793 // Check if the stub is known not to support this breakpoint type 2794 if (!SupportsGDBStoppointPacket(type)) 2795 return UINT8_MAX; 2796 // Construct the breakpoint packet 2797 char packet[64]; 2798 const int packet_len = 2799 ::snprintf(packet, sizeof(packet), "%c%i,%" PRIx64 ",%x", 2800 insert ? 'Z' : 'z', type, addr, length); 2801 // Check we haven't overwritten the end of the packet buffer 2802 assert(packet_len + 1 < (int)sizeof(packet)); 2803 UNUSED_IF_ASSERT_DISABLED(packet_len); 2804 StringExtractorGDBRemote response; 2805 // Make sure the response is either "OK", "EXX" where XX are two hex digits, 2806 // or "" (unsupported) 2807 response.SetResponseValidatorToOKErrorNotSupported(); 2808 // Try to send the breakpoint packet, and check that it was correctly sent 2809 if (SendPacketAndWaitForResponse(packet, response, timeout) == 2810 PacketResult::Success) { 2811 // Receive and OK packet when the breakpoint successfully placed 2812 if (response.IsOKResponse()) 2813 return 0; 2814 2815 // Status while setting breakpoint, send back specific error 2816 if (response.IsErrorResponse()) 2817 return response.GetError(); 2818 2819 // Empty packet informs us that breakpoint is not supported 2820 if (response.IsUnsupportedResponse()) { 2821 // Disable this breakpoint type since it is unsupported 2822 switch (type) { 2823 case eBreakpointSoftware: 2824 m_supports_z0 = false; 2825 break; 2826 case eBreakpointHardware: 2827 m_supports_z1 = false; 2828 break; 2829 case eWatchpointWrite: 2830 m_supports_z2 = false; 2831 break; 2832 case eWatchpointRead: 2833 m_supports_z3 = false; 2834 break; 2835 case eWatchpointReadWrite: 2836 m_supports_z4 = false; 2837 break; 2838 case eStoppointInvalid: 2839 return UINT8_MAX; 2840 } 2841 } 2842 } 2843 // Signal generic failure 2844 return UINT8_MAX; 2845 } 2846 2847 std::vector<std::pair<lldb::pid_t, lldb::tid_t>> 2848 GDBRemoteCommunicationClient::GetCurrentProcessAndThreadIDs( 2849 bool &sequence_mutex_unavailable) { 2850 std::vector<std::pair<lldb::pid_t, lldb::tid_t>> ids; 2851 2852 Lock lock(*this); 2853 if (lock) { 2854 sequence_mutex_unavailable = false; 2855 StringExtractorGDBRemote response; 2856 2857 PacketResult packet_result; 2858 for (packet_result = 2859 SendPacketAndWaitForResponseNoLock("qfThreadInfo", response); 2860 packet_result == PacketResult::Success && response.IsNormalResponse(); 2861 packet_result = 2862 SendPacketAndWaitForResponseNoLock("qsThreadInfo", response)) { 2863 char ch = response.GetChar(); 2864 if (ch == 'l') 2865 break; 2866 if (ch == 'm') { 2867 do { 2868 auto pid_tid = response.GetPidTid(LLDB_INVALID_PROCESS_ID); 2869 // If we get an invalid response, break out of the loop. 2870 // If there are valid tids, they have been added to ids. 2871 // If there are no valid tids, we'll fall through to the 2872 // bare-iron target handling below. 2873 if (!pid_tid) 2874 break; 2875 2876 ids.push_back(*pid_tid); 2877 ch = response.GetChar(); // Skip the command separator 2878 } while (ch == ','); // Make sure we got a comma separator 2879 } 2880 } 2881 2882 /* 2883 * Connected bare-iron target (like YAMON gdb-stub) may not have support for 2884 * qProcessInfo, qC and qfThreadInfo packets. The reply from '?' packet 2885 * could 2886 * be as simple as 'S05'. There is no packet which can give us pid and/or 2887 * tid. 2888 * Assume pid=tid=1 in such cases. 2889 */ 2890 if ((response.IsUnsupportedResponse() || response.IsNormalResponse()) && 2891 ids.size() == 0 && IsConnected()) { 2892 ids.emplace_back(1, 1); 2893 } 2894 } else { 2895 Log *log(GetLog(GDBRLog::Process | GDBRLog::Packets)); 2896 LLDB_LOG(log, "error: failed to get packet sequence mutex, not sending " 2897 "packet 'qfThreadInfo'"); 2898 sequence_mutex_unavailable = true; 2899 } 2900 2901 return ids; 2902 } 2903 2904 size_t GDBRemoteCommunicationClient::GetCurrentThreadIDs( 2905 std::vector<lldb::tid_t> &thread_ids, bool &sequence_mutex_unavailable) { 2906 lldb::pid_t pid = GetCurrentProcessID(); 2907 thread_ids.clear(); 2908 2909 auto ids = GetCurrentProcessAndThreadIDs(sequence_mutex_unavailable); 2910 if (ids.empty() || sequence_mutex_unavailable) 2911 return 0; 2912 2913 for (auto id : ids) { 2914 // skip threads that do not belong to the current process 2915 if (id.first != LLDB_INVALID_PROCESS_ID && id.first != pid) 2916 continue; 2917 if (id.second != LLDB_INVALID_THREAD_ID && 2918 id.second != StringExtractorGDBRemote::AllThreads) 2919 thread_ids.push_back(id.second); 2920 } 2921 2922 return thread_ids.size(); 2923 } 2924 2925 lldb::addr_t GDBRemoteCommunicationClient::GetShlibInfoAddr() { 2926 StringExtractorGDBRemote response; 2927 if (SendPacketAndWaitForResponse("qShlibInfoAddr", response) != 2928 PacketResult::Success || 2929 !response.IsNormalResponse()) 2930 return LLDB_INVALID_ADDRESS; 2931 return response.GetHexMaxU64(false, LLDB_INVALID_ADDRESS); 2932 } 2933 2934 lldb_private::Status GDBRemoteCommunicationClient::RunShellCommand( 2935 llvm::StringRef command, 2936 const FileSpec & 2937 working_dir, // Pass empty FileSpec to use the current working directory 2938 int *status_ptr, // Pass NULL if you don't want the process exit status 2939 int *signo_ptr, // Pass NULL if you don't want the signal that caused the 2940 // process to exit 2941 std::string 2942 *command_output, // Pass NULL if you don't want the command output 2943 const Timeout<std::micro> &timeout) { 2944 lldb_private::StreamString stream; 2945 stream.PutCString("qPlatform_shell:"); 2946 stream.PutBytesAsRawHex8(command.data(), command.size()); 2947 stream.PutChar(','); 2948 uint32_t timeout_sec = UINT32_MAX; 2949 if (timeout) { 2950 // TODO: Use chrono version of std::ceil once c++17 is available. 2951 timeout_sec = std::ceil(std::chrono::duration<double>(*timeout).count()); 2952 } 2953 stream.PutHex32(timeout_sec); 2954 if (working_dir) { 2955 std::string path{working_dir.GetPath(false)}; 2956 stream.PutChar(','); 2957 stream.PutStringAsRawHex8(path); 2958 } 2959 StringExtractorGDBRemote response; 2960 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 2961 PacketResult::Success) { 2962 if (response.GetChar() != 'F') 2963 return Status("malformed reply"); 2964 if (response.GetChar() != ',') 2965 return Status("malformed reply"); 2966 uint32_t exitcode = response.GetHexMaxU32(false, UINT32_MAX); 2967 if (exitcode == UINT32_MAX) 2968 return Status("unable to run remote process"); 2969 else if (status_ptr) 2970 *status_ptr = exitcode; 2971 if (response.GetChar() != ',') 2972 return Status("malformed reply"); 2973 uint32_t signo = response.GetHexMaxU32(false, UINT32_MAX); 2974 if (signo_ptr) 2975 *signo_ptr = signo; 2976 if (response.GetChar() != ',') 2977 return Status("malformed reply"); 2978 std::string output; 2979 response.GetEscapedBinaryData(output); 2980 if (command_output) 2981 command_output->assign(output); 2982 return Status(); 2983 } 2984 return Status("unable to send packet"); 2985 } 2986 2987 Status GDBRemoteCommunicationClient::MakeDirectory(const FileSpec &file_spec, 2988 uint32_t file_permissions) { 2989 std::string path{file_spec.GetPath(false)}; 2990 lldb_private::StreamString stream; 2991 stream.PutCString("qPlatform_mkdir:"); 2992 stream.PutHex32(file_permissions); 2993 stream.PutChar(','); 2994 stream.PutStringAsRawHex8(path); 2995 llvm::StringRef packet = stream.GetString(); 2996 StringExtractorGDBRemote response; 2997 2998 if (SendPacketAndWaitForResponse(packet, response) != PacketResult::Success) 2999 return Status("failed to send '%s' packet", packet.str().c_str()); 3000 3001 if (response.GetChar() != 'F') 3002 return Status("invalid response to '%s' packet", packet.str().c_str()); 3003 3004 return Status(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX); 3005 } 3006 3007 Status 3008 GDBRemoteCommunicationClient::SetFilePermissions(const FileSpec &file_spec, 3009 uint32_t file_permissions) { 3010 std::string path{file_spec.GetPath(false)}; 3011 lldb_private::StreamString stream; 3012 stream.PutCString("qPlatform_chmod:"); 3013 stream.PutHex32(file_permissions); 3014 stream.PutChar(','); 3015 stream.PutStringAsRawHex8(path); 3016 llvm::StringRef packet = stream.GetString(); 3017 StringExtractorGDBRemote response; 3018 3019 if (SendPacketAndWaitForResponse(packet, response) != PacketResult::Success) 3020 return Status("failed to send '%s' packet", stream.GetData()); 3021 3022 if (response.GetChar() != 'F') 3023 return Status("invalid response to '%s' packet", stream.GetData()); 3024 3025 return Status(response.GetHexMaxU32(false, UINT32_MAX), eErrorTypePOSIX); 3026 } 3027 3028 static int gdb_errno_to_system(int err) { 3029 switch (err) { 3030 #define HANDLE_ERRNO(name, value) \ 3031 case GDB_##name: \ 3032 return name; 3033 #include "Plugins/Process/gdb-remote/GDBRemoteErrno.def" 3034 default: 3035 return -1; 3036 } 3037 } 3038 3039 static uint64_t ParseHostIOPacketResponse(StringExtractorGDBRemote &response, 3040 uint64_t fail_result, Status &error) { 3041 response.SetFilePos(0); 3042 if (response.GetChar() != 'F') 3043 return fail_result; 3044 int32_t result = response.GetS32(-2, 16); 3045 if (result == -2) 3046 return fail_result; 3047 if (response.GetChar() == ',') { 3048 int result_errno = gdb_errno_to_system(response.GetS32(-1, 16)); 3049 if (result_errno != -1) 3050 error.SetError(result_errno, eErrorTypePOSIX); 3051 else 3052 error.SetError(-1, eErrorTypeGeneric); 3053 } else 3054 error.Clear(); 3055 return result; 3056 } 3057 lldb::user_id_t 3058 GDBRemoteCommunicationClient::OpenFile(const lldb_private::FileSpec &file_spec, 3059 File::OpenOptions flags, mode_t mode, 3060 Status &error) { 3061 std::string path(file_spec.GetPath(false)); 3062 lldb_private::StreamString stream; 3063 stream.PutCString("vFile:open:"); 3064 if (path.empty()) 3065 return UINT64_MAX; 3066 stream.PutStringAsRawHex8(path); 3067 stream.PutChar(','); 3068 stream.PutHex32(flags); 3069 stream.PutChar(','); 3070 stream.PutHex32(mode); 3071 StringExtractorGDBRemote response; 3072 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3073 PacketResult::Success) { 3074 return ParseHostIOPacketResponse(response, UINT64_MAX, error); 3075 } 3076 return UINT64_MAX; 3077 } 3078 3079 bool GDBRemoteCommunicationClient::CloseFile(lldb::user_id_t fd, 3080 Status &error) { 3081 lldb_private::StreamString stream; 3082 stream.Printf("vFile:close:%x", (int)fd); 3083 StringExtractorGDBRemote response; 3084 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3085 PacketResult::Success) { 3086 return ParseHostIOPacketResponse(response, -1, error) == 0; 3087 } 3088 return false; 3089 } 3090 3091 llvm::Optional<GDBRemoteFStatData> 3092 GDBRemoteCommunicationClient::FStat(lldb::user_id_t fd) { 3093 lldb_private::StreamString stream; 3094 stream.Printf("vFile:fstat:%" PRIx64, fd); 3095 StringExtractorGDBRemote response; 3096 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3097 PacketResult::Success) { 3098 if (response.GetChar() != 'F') 3099 return std::nullopt; 3100 int64_t size = response.GetS64(-1, 16); 3101 if (size > 0 && response.GetChar() == ';') { 3102 std::string buffer; 3103 if (response.GetEscapedBinaryData(buffer)) { 3104 GDBRemoteFStatData out; 3105 if (buffer.size() != sizeof(out)) 3106 return std::nullopt; 3107 memcpy(&out, buffer.data(), sizeof(out)); 3108 return out; 3109 } 3110 } 3111 } 3112 return std::nullopt; 3113 } 3114 3115 llvm::Optional<GDBRemoteFStatData> 3116 GDBRemoteCommunicationClient::Stat(const lldb_private::FileSpec &file_spec) { 3117 Status error; 3118 lldb::user_id_t fd = OpenFile(file_spec, File::eOpenOptionReadOnly, 0, error); 3119 if (fd == UINT64_MAX) 3120 return std::nullopt; 3121 llvm::Optional<GDBRemoteFStatData> st = FStat(fd); 3122 CloseFile(fd, error); 3123 return st; 3124 } 3125 3126 // Extension of host I/O packets to get the file size. 3127 lldb::user_id_t GDBRemoteCommunicationClient::GetFileSize( 3128 const lldb_private::FileSpec &file_spec) { 3129 if (m_supports_vFileSize) { 3130 std::string path(file_spec.GetPath(false)); 3131 lldb_private::StreamString stream; 3132 stream.PutCString("vFile:size:"); 3133 stream.PutStringAsRawHex8(path); 3134 StringExtractorGDBRemote response; 3135 if (SendPacketAndWaitForResponse(stream.GetString(), response) != 3136 PacketResult::Success) 3137 return UINT64_MAX; 3138 3139 if (!response.IsUnsupportedResponse()) { 3140 if (response.GetChar() != 'F') 3141 return UINT64_MAX; 3142 uint32_t retcode = response.GetHexMaxU64(false, UINT64_MAX); 3143 return retcode; 3144 } 3145 m_supports_vFileSize = false; 3146 } 3147 3148 // Fallback to fstat. 3149 llvm::Optional<GDBRemoteFStatData> st = Stat(file_spec); 3150 return st ? st->gdb_st_size : UINT64_MAX; 3151 } 3152 3153 void GDBRemoteCommunicationClient::AutoCompleteDiskFileOrDirectory( 3154 CompletionRequest &request, bool only_dir) { 3155 lldb_private::StreamString stream; 3156 stream.PutCString("qPathComplete:"); 3157 stream.PutHex32(only_dir ? 1 : 0); 3158 stream.PutChar(','); 3159 stream.PutStringAsRawHex8(request.GetCursorArgumentPrefix()); 3160 StringExtractorGDBRemote response; 3161 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3162 PacketResult::Success) { 3163 StreamString strm; 3164 char ch = response.GetChar(); 3165 if (ch != 'M') 3166 return; 3167 while (response.Peek()) { 3168 strm.Clear(); 3169 while ((ch = response.GetHexU8(0, false)) != '\0') 3170 strm.PutChar(ch); 3171 request.AddCompletion(strm.GetString()); 3172 if (response.GetChar() != ',') 3173 break; 3174 } 3175 } 3176 } 3177 3178 Status 3179 GDBRemoteCommunicationClient::GetFilePermissions(const FileSpec &file_spec, 3180 uint32_t &file_permissions) { 3181 if (m_supports_vFileMode) { 3182 std::string path{file_spec.GetPath(false)}; 3183 Status error; 3184 lldb_private::StreamString stream; 3185 stream.PutCString("vFile:mode:"); 3186 stream.PutStringAsRawHex8(path); 3187 StringExtractorGDBRemote response; 3188 if (SendPacketAndWaitForResponse(stream.GetString(), response) != 3189 PacketResult::Success) { 3190 error.SetErrorStringWithFormat("failed to send '%s' packet", 3191 stream.GetData()); 3192 return error; 3193 } 3194 if (!response.IsUnsupportedResponse()) { 3195 if (response.GetChar() != 'F') { 3196 error.SetErrorStringWithFormat("invalid response to '%s' packet", 3197 stream.GetData()); 3198 } else { 3199 const uint32_t mode = response.GetS32(-1, 16); 3200 if (static_cast<int32_t>(mode) == -1) { 3201 if (response.GetChar() == ',') { 3202 int response_errno = gdb_errno_to_system(response.GetS32(-1, 16)); 3203 if (response_errno > 0) 3204 error.SetError(response_errno, lldb::eErrorTypePOSIX); 3205 else 3206 error.SetErrorToGenericError(); 3207 } else 3208 error.SetErrorToGenericError(); 3209 } else { 3210 file_permissions = mode & (S_IRWXU | S_IRWXG | S_IRWXO); 3211 } 3212 } 3213 return error; 3214 } else { // response.IsUnsupportedResponse() 3215 m_supports_vFileMode = false; 3216 } 3217 } 3218 3219 // Fallback to fstat. 3220 if (llvm::Optional<GDBRemoteFStatData> st = Stat(file_spec)) { 3221 file_permissions = st->gdb_st_mode & (S_IRWXU | S_IRWXG | S_IRWXO); 3222 return Status(); 3223 } 3224 return Status("fstat failed"); 3225 } 3226 3227 uint64_t GDBRemoteCommunicationClient::ReadFile(lldb::user_id_t fd, 3228 uint64_t offset, void *dst, 3229 uint64_t dst_len, 3230 Status &error) { 3231 lldb_private::StreamString stream; 3232 stream.Printf("vFile:pread:%x,%" PRIx64 ",%" PRIx64, (int)fd, dst_len, 3233 offset); 3234 StringExtractorGDBRemote response; 3235 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3236 PacketResult::Success) { 3237 if (response.GetChar() != 'F') 3238 return 0; 3239 int64_t retcode = response.GetS64(-1, 16); 3240 if (retcode == -1) { 3241 error.SetErrorToGenericError(); 3242 if (response.GetChar() == ',') { 3243 int response_errno = gdb_errno_to_system(response.GetS32(-1, 16)); 3244 if (response_errno > 0) 3245 error.SetError(response_errno, lldb::eErrorTypePOSIX); 3246 } 3247 return -1; 3248 } 3249 const char next = (response.Peek() ? *response.Peek() : 0); 3250 if (next == ',') 3251 return 0; 3252 if (next == ';') { 3253 response.GetChar(); // skip the semicolon 3254 std::string buffer; 3255 if (response.GetEscapedBinaryData(buffer)) { 3256 const uint64_t data_to_write = 3257 std::min<uint64_t>(dst_len, buffer.size()); 3258 if (data_to_write > 0) 3259 memcpy(dst, &buffer[0], data_to_write); 3260 return data_to_write; 3261 } 3262 } 3263 } 3264 return 0; 3265 } 3266 3267 uint64_t GDBRemoteCommunicationClient::WriteFile(lldb::user_id_t fd, 3268 uint64_t offset, 3269 const void *src, 3270 uint64_t src_len, 3271 Status &error) { 3272 lldb_private::StreamGDBRemote stream; 3273 stream.Printf("vFile:pwrite:%x,%" PRIx64 ",", (int)fd, offset); 3274 stream.PutEscapedBytes(src, src_len); 3275 StringExtractorGDBRemote response; 3276 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3277 PacketResult::Success) { 3278 if (response.GetChar() != 'F') { 3279 error.SetErrorStringWithFormat("write file failed"); 3280 return 0; 3281 } 3282 int64_t bytes_written = response.GetS64(-1, 16); 3283 if (bytes_written == -1) { 3284 error.SetErrorToGenericError(); 3285 if (response.GetChar() == ',') { 3286 int response_errno = gdb_errno_to_system(response.GetS32(-1, 16)); 3287 if (response_errno > 0) 3288 error.SetError(response_errno, lldb::eErrorTypePOSIX); 3289 } 3290 return -1; 3291 } 3292 return bytes_written; 3293 } else { 3294 error.SetErrorString("failed to send vFile:pwrite packet"); 3295 } 3296 return 0; 3297 } 3298 3299 Status GDBRemoteCommunicationClient::CreateSymlink(const FileSpec &src, 3300 const FileSpec &dst) { 3301 std::string src_path{src.GetPath(false)}, dst_path{dst.GetPath(false)}; 3302 Status error; 3303 lldb_private::StreamGDBRemote stream; 3304 stream.PutCString("vFile:symlink:"); 3305 // the unix symlink() command reverses its parameters where the dst if first, 3306 // so we follow suit here 3307 stream.PutStringAsRawHex8(dst_path); 3308 stream.PutChar(','); 3309 stream.PutStringAsRawHex8(src_path); 3310 StringExtractorGDBRemote response; 3311 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3312 PacketResult::Success) { 3313 if (response.GetChar() == 'F') { 3314 uint32_t result = response.GetHexMaxU32(false, UINT32_MAX); 3315 if (result != 0) { 3316 error.SetErrorToGenericError(); 3317 if (response.GetChar() == ',') { 3318 int response_errno = gdb_errno_to_system(response.GetS32(-1, 16)); 3319 if (response_errno > 0) 3320 error.SetError(response_errno, lldb::eErrorTypePOSIX); 3321 } 3322 } 3323 } else { 3324 // Should have returned with 'F<result>[,<errno>]' 3325 error.SetErrorStringWithFormat("symlink failed"); 3326 } 3327 } else { 3328 error.SetErrorString("failed to send vFile:symlink packet"); 3329 } 3330 return error; 3331 } 3332 3333 Status GDBRemoteCommunicationClient::Unlink(const FileSpec &file_spec) { 3334 std::string path{file_spec.GetPath(false)}; 3335 Status error; 3336 lldb_private::StreamGDBRemote stream; 3337 stream.PutCString("vFile:unlink:"); 3338 // the unix symlink() command reverses its parameters where the dst if first, 3339 // so we follow suit here 3340 stream.PutStringAsRawHex8(path); 3341 StringExtractorGDBRemote response; 3342 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3343 PacketResult::Success) { 3344 if (response.GetChar() == 'F') { 3345 uint32_t result = response.GetHexMaxU32(false, UINT32_MAX); 3346 if (result != 0) { 3347 error.SetErrorToGenericError(); 3348 if (response.GetChar() == ',') { 3349 int response_errno = gdb_errno_to_system(response.GetS32(-1, 16)); 3350 if (response_errno > 0) 3351 error.SetError(response_errno, lldb::eErrorTypePOSIX); 3352 } 3353 } 3354 } else { 3355 // Should have returned with 'F<result>[,<errno>]' 3356 error.SetErrorStringWithFormat("unlink failed"); 3357 } 3358 } else { 3359 error.SetErrorString("failed to send vFile:unlink packet"); 3360 } 3361 return error; 3362 } 3363 3364 // Extension of host I/O packets to get whether a file exists. 3365 bool GDBRemoteCommunicationClient::GetFileExists( 3366 const lldb_private::FileSpec &file_spec) { 3367 if (m_supports_vFileExists) { 3368 std::string path(file_spec.GetPath(false)); 3369 lldb_private::StreamString stream; 3370 stream.PutCString("vFile:exists:"); 3371 stream.PutStringAsRawHex8(path); 3372 StringExtractorGDBRemote response; 3373 if (SendPacketAndWaitForResponse(stream.GetString(), response) != 3374 PacketResult::Success) 3375 return false; 3376 if (!response.IsUnsupportedResponse()) { 3377 if (response.GetChar() != 'F') 3378 return false; 3379 if (response.GetChar() != ',') 3380 return false; 3381 bool retcode = (response.GetChar() != '0'); 3382 return retcode; 3383 } else 3384 m_supports_vFileExists = false; 3385 } 3386 3387 // Fallback to open. 3388 Status error; 3389 lldb::user_id_t fd = OpenFile(file_spec, File::eOpenOptionReadOnly, 0, error); 3390 if (fd == UINT64_MAX) 3391 return false; 3392 CloseFile(fd, error); 3393 return true; 3394 } 3395 3396 bool GDBRemoteCommunicationClient::CalculateMD5( 3397 const lldb_private::FileSpec &file_spec, uint64_t &high, uint64_t &low) { 3398 std::string path(file_spec.GetPath(false)); 3399 lldb_private::StreamString stream; 3400 stream.PutCString("vFile:MD5:"); 3401 stream.PutStringAsRawHex8(path); 3402 StringExtractorGDBRemote response; 3403 if (SendPacketAndWaitForResponse(stream.GetString(), response) == 3404 PacketResult::Success) { 3405 if (response.GetChar() != 'F') 3406 return false; 3407 if (response.GetChar() != ',') 3408 return false; 3409 if (response.Peek() && *response.Peek() == 'x') 3410 return false; 3411 low = response.GetHexMaxU64(false, UINT64_MAX); 3412 high = response.GetHexMaxU64(false, UINT64_MAX); 3413 return true; 3414 } 3415 return false; 3416 } 3417 3418 bool GDBRemoteCommunicationClient::AvoidGPackets(ProcessGDBRemote *process) { 3419 // Some targets have issues with g/G packets and we need to avoid using them 3420 if (m_avoid_g_packets == eLazyBoolCalculate) { 3421 if (process) { 3422 m_avoid_g_packets = eLazyBoolNo; 3423 const ArchSpec &arch = process->GetTarget().GetArchitecture(); 3424 if (arch.IsValid() && 3425 arch.GetTriple().getVendor() == llvm::Triple::Apple && 3426 arch.GetTriple().getOS() == llvm::Triple::IOS && 3427 (arch.GetTriple().getArch() == llvm::Triple::aarch64 || 3428 arch.GetTriple().getArch() == llvm::Triple::aarch64_32)) { 3429 m_avoid_g_packets = eLazyBoolYes; 3430 uint32_t gdb_server_version = GetGDBServerProgramVersion(); 3431 if (gdb_server_version != 0) { 3432 const char *gdb_server_name = GetGDBServerProgramName(); 3433 if (gdb_server_name && strcmp(gdb_server_name, "debugserver") == 0) { 3434 if (gdb_server_version >= 310) 3435 m_avoid_g_packets = eLazyBoolNo; 3436 } 3437 } 3438 } 3439 } 3440 } 3441 return m_avoid_g_packets == eLazyBoolYes; 3442 } 3443 3444 DataBufferSP GDBRemoteCommunicationClient::ReadRegister(lldb::tid_t tid, 3445 uint32_t reg) { 3446 StreamString payload; 3447 payload.Printf("p%x", reg); 3448 StringExtractorGDBRemote response; 3449 if (SendThreadSpecificPacketAndWaitForResponse( 3450 tid, std::move(payload), response) != PacketResult::Success || 3451 !response.IsNormalResponse()) 3452 return nullptr; 3453 3454 WritableDataBufferSP buffer_sp( 3455 new DataBufferHeap(response.GetStringRef().size() / 2, 0)); 3456 response.GetHexBytes(buffer_sp->GetData(), '\xcc'); 3457 return buffer_sp; 3458 } 3459 3460 DataBufferSP GDBRemoteCommunicationClient::ReadAllRegisters(lldb::tid_t tid) { 3461 StreamString payload; 3462 payload.PutChar('g'); 3463 StringExtractorGDBRemote response; 3464 if (SendThreadSpecificPacketAndWaitForResponse( 3465 tid, std::move(payload), response) != PacketResult::Success || 3466 !response.IsNormalResponse()) 3467 return nullptr; 3468 3469 WritableDataBufferSP buffer_sp( 3470 new DataBufferHeap(response.GetStringRef().size() / 2, 0)); 3471 response.GetHexBytes(buffer_sp->GetData(), '\xcc'); 3472 return buffer_sp; 3473 } 3474 3475 bool GDBRemoteCommunicationClient::WriteRegister(lldb::tid_t tid, 3476 uint32_t reg_num, 3477 llvm::ArrayRef<uint8_t> data) { 3478 StreamString payload; 3479 payload.Printf("P%x=", reg_num); 3480 payload.PutBytesAsRawHex8(data.data(), data.size(), 3481 endian::InlHostByteOrder(), 3482 endian::InlHostByteOrder()); 3483 StringExtractorGDBRemote response; 3484 return SendThreadSpecificPacketAndWaitForResponse( 3485 tid, std::move(payload), response) == PacketResult::Success && 3486 response.IsOKResponse(); 3487 } 3488 3489 bool GDBRemoteCommunicationClient::WriteAllRegisters( 3490 lldb::tid_t tid, llvm::ArrayRef<uint8_t> data) { 3491 StreamString payload; 3492 payload.PutChar('G'); 3493 payload.PutBytesAsRawHex8(data.data(), data.size(), 3494 endian::InlHostByteOrder(), 3495 endian::InlHostByteOrder()); 3496 StringExtractorGDBRemote response; 3497 return SendThreadSpecificPacketAndWaitForResponse( 3498 tid, std::move(payload), response) == PacketResult::Success && 3499 response.IsOKResponse(); 3500 } 3501 3502 bool GDBRemoteCommunicationClient::SaveRegisterState(lldb::tid_t tid, 3503 uint32_t &save_id) { 3504 save_id = 0; // Set to invalid save ID 3505 if (m_supports_QSaveRegisterState == eLazyBoolNo) 3506 return false; 3507 3508 m_supports_QSaveRegisterState = eLazyBoolYes; 3509 StreamString payload; 3510 payload.PutCString("QSaveRegisterState"); 3511 StringExtractorGDBRemote response; 3512 if (SendThreadSpecificPacketAndWaitForResponse( 3513 tid, std::move(payload), response) != PacketResult::Success) 3514 return false; 3515 3516 if (response.IsUnsupportedResponse()) 3517 m_supports_QSaveRegisterState = eLazyBoolNo; 3518 3519 const uint32_t response_save_id = response.GetU32(0); 3520 if (response_save_id == 0) 3521 return false; 3522 3523 save_id = response_save_id; 3524 return true; 3525 } 3526 3527 bool GDBRemoteCommunicationClient::RestoreRegisterState(lldb::tid_t tid, 3528 uint32_t save_id) { 3529 // We use the "m_supports_QSaveRegisterState" variable here because the 3530 // QSaveRegisterState and QRestoreRegisterState packets must both be 3531 // supported in order to be useful 3532 if (m_supports_QSaveRegisterState == eLazyBoolNo) 3533 return false; 3534 3535 StreamString payload; 3536 payload.Printf("QRestoreRegisterState:%u", save_id); 3537 StringExtractorGDBRemote response; 3538 if (SendThreadSpecificPacketAndWaitForResponse( 3539 tid, std::move(payload), response) != PacketResult::Success) 3540 return false; 3541 3542 if (response.IsOKResponse()) 3543 return true; 3544 3545 if (response.IsUnsupportedResponse()) 3546 m_supports_QSaveRegisterState = eLazyBoolNo; 3547 return false; 3548 } 3549 3550 bool GDBRemoteCommunicationClient::SyncThreadState(lldb::tid_t tid) { 3551 if (!GetSyncThreadStateSupported()) 3552 return false; 3553 3554 StreamString packet; 3555 StringExtractorGDBRemote response; 3556 packet.Printf("QSyncThreadState:%4.4" PRIx64 ";", tid); 3557 return SendPacketAndWaitForResponse(packet.GetString(), response) == 3558 GDBRemoteCommunication::PacketResult::Success && 3559 response.IsOKResponse(); 3560 } 3561 3562 llvm::Expected<TraceSupportedResponse> 3563 GDBRemoteCommunicationClient::SendTraceSupported(std::chrono::seconds timeout) { 3564 Log *log = GetLog(GDBRLog::Process); 3565 3566 StreamGDBRemote escaped_packet; 3567 escaped_packet.PutCString("jLLDBTraceSupported"); 3568 3569 StringExtractorGDBRemote response; 3570 if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, 3571 timeout) == 3572 GDBRemoteCommunication::PacketResult::Success) { 3573 if (response.IsErrorResponse()) 3574 return response.GetStatus().ToError(); 3575 if (response.IsUnsupportedResponse()) 3576 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3577 "jLLDBTraceSupported is unsupported"); 3578 3579 return llvm::json::parse<TraceSupportedResponse>(response.Peek(), 3580 "TraceSupportedResponse"); 3581 } 3582 LLDB_LOG(log, "failed to send packet: jLLDBTraceSupported"); 3583 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3584 "failed to send packet: jLLDBTraceSupported"); 3585 } 3586 3587 llvm::Error 3588 GDBRemoteCommunicationClient::SendTraceStop(const TraceStopRequest &request, 3589 std::chrono::seconds timeout) { 3590 Log *log = GetLog(GDBRLog::Process); 3591 3592 StreamGDBRemote escaped_packet; 3593 escaped_packet.PutCString("jLLDBTraceStop:"); 3594 3595 std::string json_string; 3596 llvm::raw_string_ostream os(json_string); 3597 os << toJSON(request); 3598 os.flush(); 3599 3600 escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size()); 3601 3602 StringExtractorGDBRemote response; 3603 if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, 3604 timeout) == 3605 GDBRemoteCommunication::PacketResult::Success) { 3606 if (response.IsErrorResponse()) 3607 return response.GetStatus().ToError(); 3608 if (response.IsUnsupportedResponse()) 3609 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3610 "jLLDBTraceStop is unsupported"); 3611 if (response.IsOKResponse()) 3612 return llvm::Error::success(); 3613 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3614 "Invalid jLLDBTraceStart response"); 3615 } 3616 LLDB_LOG(log, "failed to send packet: jLLDBTraceStop"); 3617 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3618 "failed to send packet: jLLDBTraceStop '%s'", 3619 escaped_packet.GetData()); 3620 } 3621 3622 llvm::Error 3623 GDBRemoteCommunicationClient::SendTraceStart(const llvm::json::Value ¶ms, 3624 std::chrono::seconds timeout) { 3625 Log *log = GetLog(GDBRLog::Process); 3626 3627 StreamGDBRemote escaped_packet; 3628 escaped_packet.PutCString("jLLDBTraceStart:"); 3629 3630 std::string json_string; 3631 llvm::raw_string_ostream os(json_string); 3632 os << params; 3633 os.flush(); 3634 3635 escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size()); 3636 3637 StringExtractorGDBRemote response; 3638 if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, 3639 timeout) == 3640 GDBRemoteCommunication::PacketResult::Success) { 3641 if (response.IsErrorResponse()) 3642 return response.GetStatus().ToError(); 3643 if (response.IsUnsupportedResponse()) 3644 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3645 "jLLDBTraceStart is unsupported"); 3646 if (response.IsOKResponse()) 3647 return llvm::Error::success(); 3648 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3649 "Invalid jLLDBTraceStart response"); 3650 } 3651 LLDB_LOG(log, "failed to send packet: jLLDBTraceStart"); 3652 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3653 "failed to send packet: jLLDBTraceStart '%s'", 3654 escaped_packet.GetData()); 3655 } 3656 3657 llvm::Expected<std::string> 3658 GDBRemoteCommunicationClient::SendTraceGetState(llvm::StringRef type, 3659 std::chrono::seconds timeout) { 3660 Log *log = GetLog(GDBRLog::Process); 3661 3662 StreamGDBRemote escaped_packet; 3663 escaped_packet.PutCString("jLLDBTraceGetState:"); 3664 3665 std::string json_string; 3666 llvm::raw_string_ostream os(json_string); 3667 os << toJSON(TraceGetStateRequest{type.str()}); 3668 os.flush(); 3669 3670 escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size()); 3671 3672 StringExtractorGDBRemote response; 3673 if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, 3674 timeout) == 3675 GDBRemoteCommunication::PacketResult::Success) { 3676 if (response.IsErrorResponse()) 3677 return response.GetStatus().ToError(); 3678 if (response.IsUnsupportedResponse()) 3679 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3680 "jLLDBTraceGetState is unsupported"); 3681 return std::string(response.Peek()); 3682 } 3683 3684 LLDB_LOG(log, "failed to send packet: jLLDBTraceGetState"); 3685 return llvm::createStringError( 3686 llvm::inconvertibleErrorCode(), 3687 "failed to send packet: jLLDBTraceGetState '%s'", 3688 escaped_packet.GetData()); 3689 } 3690 3691 llvm::Expected<std::vector<uint8_t>> 3692 GDBRemoteCommunicationClient::SendTraceGetBinaryData( 3693 const TraceGetBinaryDataRequest &request, std::chrono::seconds timeout) { 3694 Log *log = GetLog(GDBRLog::Process); 3695 3696 StreamGDBRemote escaped_packet; 3697 escaped_packet.PutCString("jLLDBTraceGetBinaryData:"); 3698 3699 std::string json_string; 3700 llvm::raw_string_ostream os(json_string); 3701 os << toJSON(request); 3702 os.flush(); 3703 3704 escaped_packet.PutEscapedBytes(json_string.c_str(), json_string.size()); 3705 3706 StringExtractorGDBRemote response; 3707 if (SendPacketAndWaitForResponse(escaped_packet.GetString(), response, 3708 timeout) == 3709 GDBRemoteCommunication::PacketResult::Success) { 3710 if (response.IsErrorResponse()) 3711 return response.GetStatus().ToError(); 3712 std::string data; 3713 response.GetEscapedBinaryData(data); 3714 return std::vector<uint8_t>(data.begin(), data.end()); 3715 } 3716 LLDB_LOG(log, "failed to send packet: jLLDBTraceGetBinaryData"); 3717 return llvm::createStringError( 3718 llvm::inconvertibleErrorCode(), 3719 "failed to send packet: jLLDBTraceGetBinaryData '%s'", 3720 escaped_packet.GetData()); 3721 } 3722 3723 llvm::Optional<QOffsets> GDBRemoteCommunicationClient::GetQOffsets() { 3724 StringExtractorGDBRemote response; 3725 if (SendPacketAndWaitForResponse("qOffsets", response) != 3726 PacketResult::Success) 3727 return std::nullopt; 3728 if (!response.IsNormalResponse()) 3729 return std::nullopt; 3730 3731 QOffsets result; 3732 llvm::StringRef ref = response.GetStringRef(); 3733 const auto &GetOffset = [&] { 3734 addr_t offset; 3735 if (ref.consumeInteger(16, offset)) 3736 return false; 3737 result.offsets.push_back(offset); 3738 return true; 3739 }; 3740 3741 if (ref.consume_front("Text=")) { 3742 result.segments = false; 3743 if (!GetOffset()) 3744 return std::nullopt; 3745 if (!ref.consume_front(";Data=") || !GetOffset()) 3746 return std::nullopt; 3747 if (ref.empty()) 3748 return result; 3749 if (ref.consume_front(";Bss=") && GetOffset() && ref.empty()) 3750 return result; 3751 } else if (ref.consume_front("TextSeg=")) { 3752 result.segments = true; 3753 if (!GetOffset()) 3754 return std::nullopt; 3755 if (ref.empty()) 3756 return result; 3757 if (ref.consume_front(";DataSeg=") && GetOffset() && ref.empty()) 3758 return result; 3759 } 3760 return std::nullopt; 3761 } 3762 3763 bool GDBRemoteCommunicationClient::GetModuleInfo( 3764 const FileSpec &module_file_spec, const lldb_private::ArchSpec &arch_spec, 3765 ModuleSpec &module_spec) { 3766 if (!m_supports_qModuleInfo) 3767 return false; 3768 3769 std::string module_path = module_file_spec.GetPath(false); 3770 if (module_path.empty()) 3771 return false; 3772 3773 StreamString packet; 3774 packet.PutCString("qModuleInfo:"); 3775 packet.PutStringAsRawHex8(module_path); 3776 packet.PutCString(";"); 3777 const auto &triple = arch_spec.GetTriple().getTriple(); 3778 packet.PutStringAsRawHex8(triple); 3779 3780 StringExtractorGDBRemote response; 3781 if (SendPacketAndWaitForResponse(packet.GetString(), response) != 3782 PacketResult::Success) 3783 return false; 3784 3785 if (response.IsErrorResponse()) 3786 return false; 3787 3788 if (response.IsUnsupportedResponse()) { 3789 m_supports_qModuleInfo = false; 3790 return false; 3791 } 3792 3793 llvm::StringRef name; 3794 llvm::StringRef value; 3795 3796 module_spec.Clear(); 3797 module_spec.GetFileSpec() = module_file_spec; 3798 3799 while (response.GetNameColonValue(name, value)) { 3800 if (name == "uuid" || name == "md5") { 3801 StringExtractor extractor(value); 3802 std::string uuid; 3803 extractor.GetHexByteString(uuid); 3804 module_spec.GetUUID().SetFromStringRef(uuid); 3805 } else if (name == "triple") { 3806 StringExtractor extractor(value); 3807 std::string triple; 3808 extractor.GetHexByteString(triple); 3809 module_spec.GetArchitecture().SetTriple(triple.c_str()); 3810 } else if (name == "file_offset") { 3811 uint64_t ival = 0; 3812 if (!value.getAsInteger(16, ival)) 3813 module_spec.SetObjectOffset(ival); 3814 } else if (name == "file_size") { 3815 uint64_t ival = 0; 3816 if (!value.getAsInteger(16, ival)) 3817 module_spec.SetObjectSize(ival); 3818 } else if (name == "file_path") { 3819 StringExtractor extractor(value); 3820 std::string path; 3821 extractor.GetHexByteString(path); 3822 module_spec.GetFileSpec() = FileSpec(path, arch_spec.GetTriple()); 3823 } 3824 } 3825 3826 return true; 3827 } 3828 3829 static llvm::Optional<ModuleSpec> 3830 ParseModuleSpec(StructuredData::Dictionary *dict) { 3831 ModuleSpec result; 3832 if (!dict) 3833 return std::nullopt; 3834 3835 llvm::StringRef string; 3836 uint64_t integer; 3837 3838 if (!dict->GetValueForKeyAsString("uuid", string)) 3839 return std::nullopt; 3840 if (!result.GetUUID().SetFromStringRef(string)) 3841 return std::nullopt; 3842 3843 if (!dict->GetValueForKeyAsInteger("file_offset", integer)) 3844 return std::nullopt; 3845 result.SetObjectOffset(integer); 3846 3847 if (!dict->GetValueForKeyAsInteger("file_size", integer)) 3848 return std::nullopt; 3849 result.SetObjectSize(integer); 3850 3851 if (!dict->GetValueForKeyAsString("triple", string)) 3852 return std::nullopt; 3853 result.GetArchitecture().SetTriple(string); 3854 3855 if (!dict->GetValueForKeyAsString("file_path", string)) 3856 return std::nullopt; 3857 result.GetFileSpec() = FileSpec(string, result.GetArchitecture().GetTriple()); 3858 3859 return result; 3860 } 3861 3862 llvm::Optional<std::vector<ModuleSpec>> 3863 GDBRemoteCommunicationClient::GetModulesInfo( 3864 llvm::ArrayRef<FileSpec> module_file_specs, const llvm::Triple &triple) { 3865 namespace json = llvm::json; 3866 3867 if (!m_supports_jModulesInfo) 3868 return std::nullopt; 3869 3870 json::Array module_array; 3871 for (const FileSpec &module_file_spec : module_file_specs) { 3872 module_array.push_back( 3873 json::Object{{"file", module_file_spec.GetPath(false)}, 3874 {"triple", triple.getTriple()}}); 3875 } 3876 StreamString unescaped_payload; 3877 unescaped_payload.PutCString("jModulesInfo:"); 3878 unescaped_payload.AsRawOstream() << std::move(module_array); 3879 3880 StreamGDBRemote payload; 3881 payload.PutEscapedBytes(unescaped_payload.GetString().data(), 3882 unescaped_payload.GetSize()); 3883 3884 // Increase the timeout for jModulesInfo since this packet can take longer. 3885 ScopedTimeout timeout(*this, std::chrono::seconds(10)); 3886 3887 StringExtractorGDBRemote response; 3888 if (SendPacketAndWaitForResponse(payload.GetString(), response) != 3889 PacketResult::Success || 3890 response.IsErrorResponse()) 3891 return std::nullopt; 3892 3893 if (response.IsUnsupportedResponse()) { 3894 m_supports_jModulesInfo = false; 3895 return std::nullopt; 3896 } 3897 3898 StructuredData::ObjectSP response_object_sp = 3899 StructuredData::ParseJSON(std::string(response.GetStringRef())); 3900 if (!response_object_sp) 3901 return std::nullopt; 3902 3903 StructuredData::Array *response_array = response_object_sp->GetAsArray(); 3904 if (!response_array) 3905 return std::nullopt; 3906 3907 std::vector<ModuleSpec> result; 3908 for (size_t i = 0; i < response_array->GetSize(); ++i) { 3909 if (llvm::Optional<ModuleSpec> module_spec = ParseModuleSpec( 3910 response_array->GetItemAtIndex(i)->GetAsDictionary())) 3911 result.push_back(*module_spec); 3912 } 3913 3914 return result; 3915 } 3916 3917 // query the target remote for extended information using the qXfer packet 3918 // 3919 // example: object='features', annex='target.xml' 3920 // return: <xml output> or error 3921 llvm::Expected<std::string> 3922 GDBRemoteCommunicationClient::ReadExtFeature(llvm::StringRef object, 3923 llvm::StringRef annex) { 3924 3925 std::string output; 3926 llvm::raw_string_ostream output_stream(output); 3927 StringExtractorGDBRemote chunk; 3928 3929 uint64_t size = GetRemoteMaxPacketSize(); 3930 if (size == 0) 3931 size = 0x1000; 3932 size = size - 1; // Leave space for the 'm' or 'l' character in the response 3933 int offset = 0; 3934 bool active = true; 3935 3936 // loop until all data has been read 3937 while (active) { 3938 3939 // send query extended feature packet 3940 std::string packet = 3941 ("qXfer:" + object + ":read:" + annex + ":" + 3942 llvm::Twine::utohexstr(offset) + "," + llvm::Twine::utohexstr(size)) 3943 .str(); 3944 3945 GDBRemoteCommunication::PacketResult res = 3946 SendPacketAndWaitForResponse(packet, chunk); 3947 3948 if (res != GDBRemoteCommunication::PacketResult::Success || 3949 chunk.GetStringRef().empty()) { 3950 return llvm::createStringError(llvm::inconvertibleErrorCode(), 3951 "Error sending $qXfer packet"); 3952 } 3953 3954 // check packet code 3955 switch (chunk.GetStringRef()[0]) { 3956 // last chunk 3957 case ('l'): 3958 active = false; 3959 [[fallthrough]]; 3960 3961 // more chunks 3962 case ('m'): 3963 output_stream << chunk.GetStringRef().drop_front(); 3964 offset += chunk.GetStringRef().size() - 1; 3965 break; 3966 3967 // unknown chunk 3968 default: 3969 return llvm::createStringError( 3970 llvm::inconvertibleErrorCode(), 3971 "Invalid continuation code from $qXfer packet"); 3972 } 3973 } 3974 3975 return output_stream.str(); 3976 } 3977 3978 // Notify the target that gdb is prepared to serve symbol lookup requests. 3979 // packet: "qSymbol::" 3980 // reply: 3981 // OK The target does not need to look up any (more) symbols. 3982 // qSymbol:<sym_name> The target requests the value of symbol sym_name (hex 3983 // encoded). 3984 // LLDB may provide the value by sending another qSymbol 3985 // packet 3986 // in the form of"qSymbol:<sym_value>:<sym_name>". 3987 // 3988 // Three examples: 3989 // 3990 // lldb sends: qSymbol:: 3991 // lldb receives: OK 3992 // Remote gdb stub does not need to know the addresses of any symbols, lldb 3993 // does not 3994 // need to ask again in this session. 3995 // 3996 // lldb sends: qSymbol:: 3997 // lldb receives: qSymbol:64697370617463685f71756575655f6f666673657473 3998 // lldb sends: qSymbol::64697370617463685f71756575655f6f666673657473 3999 // lldb receives: OK 4000 // Remote gdb stub asks for address of 'dispatch_queue_offsets'. lldb does 4001 // not know 4002 // the address at this time. lldb needs to send qSymbol:: again when it has 4003 // more 4004 // solibs loaded. 4005 // 4006 // lldb sends: qSymbol:: 4007 // lldb receives: qSymbol:64697370617463685f71756575655f6f666673657473 4008 // lldb sends: qSymbol:2bc97554:64697370617463685f71756575655f6f666673657473 4009 // lldb receives: OK 4010 // Remote gdb stub asks for address of 'dispatch_queue_offsets'. lldb says 4011 // that it 4012 // is at address 0x2bc97554. Remote gdb stub sends 'OK' indicating that it 4013 // does not 4014 // need any more symbols. lldb does not need to ask again in this session. 4015 4016 void GDBRemoteCommunicationClient::ServeSymbolLookups( 4017 lldb_private::Process *process) { 4018 // Set to true once we've resolved a symbol to an address for the remote 4019 // stub. If we get an 'OK' response after this, the remote stub doesn't need 4020 // any more symbols and we can stop asking. 4021 bool symbol_response_provided = false; 4022 4023 // Is this the initial qSymbol:: packet? 4024 bool first_qsymbol_query = true; 4025 4026 if (m_supports_qSymbol && !m_qSymbol_requests_done) { 4027 Lock lock(*this); 4028 if (lock) { 4029 StreamString packet; 4030 packet.PutCString("qSymbol::"); 4031 StringExtractorGDBRemote response; 4032 while (SendPacketAndWaitForResponseNoLock(packet.GetString(), response) == 4033 PacketResult::Success) { 4034 if (response.IsOKResponse()) { 4035 if (symbol_response_provided || first_qsymbol_query) { 4036 m_qSymbol_requests_done = true; 4037 } 4038 4039 // We are done serving symbols requests 4040 return; 4041 } 4042 first_qsymbol_query = false; 4043 4044 if (response.IsUnsupportedResponse()) { 4045 // qSymbol is not supported by the current GDB server we are 4046 // connected to 4047 m_supports_qSymbol = false; 4048 return; 4049 } else { 4050 llvm::StringRef response_str(response.GetStringRef()); 4051 if (response_str.startswith("qSymbol:")) { 4052 response.SetFilePos(strlen("qSymbol:")); 4053 std::string symbol_name; 4054 if (response.GetHexByteString(symbol_name)) { 4055 if (symbol_name.empty()) 4056 return; 4057 4058 addr_t symbol_load_addr = LLDB_INVALID_ADDRESS; 4059 lldb_private::SymbolContextList sc_list; 4060 process->GetTarget().GetImages().FindSymbolsWithNameAndType( 4061 ConstString(symbol_name), eSymbolTypeAny, sc_list); 4062 if (!sc_list.IsEmpty()) { 4063 const size_t num_scs = sc_list.GetSize(); 4064 for (size_t sc_idx = 0; 4065 sc_idx < num_scs && 4066 symbol_load_addr == LLDB_INVALID_ADDRESS; 4067 ++sc_idx) { 4068 SymbolContext sc; 4069 if (sc_list.GetContextAtIndex(sc_idx, sc)) { 4070 if (sc.symbol) { 4071 switch (sc.symbol->GetType()) { 4072 case eSymbolTypeInvalid: 4073 case eSymbolTypeAbsolute: 4074 case eSymbolTypeUndefined: 4075 case eSymbolTypeSourceFile: 4076 case eSymbolTypeHeaderFile: 4077 case eSymbolTypeObjectFile: 4078 case eSymbolTypeCommonBlock: 4079 case eSymbolTypeBlock: 4080 case eSymbolTypeLocal: 4081 case eSymbolTypeParam: 4082 case eSymbolTypeVariable: 4083 case eSymbolTypeVariableType: 4084 case eSymbolTypeLineEntry: 4085 case eSymbolTypeLineHeader: 4086 case eSymbolTypeScopeBegin: 4087 case eSymbolTypeScopeEnd: 4088 case eSymbolTypeAdditional: 4089 case eSymbolTypeCompiler: 4090 case eSymbolTypeInstrumentation: 4091 case eSymbolTypeTrampoline: 4092 break; 4093 4094 case eSymbolTypeCode: 4095 case eSymbolTypeResolver: 4096 case eSymbolTypeData: 4097 case eSymbolTypeRuntime: 4098 case eSymbolTypeException: 4099 case eSymbolTypeObjCClass: 4100 case eSymbolTypeObjCMetaClass: 4101 case eSymbolTypeObjCIVar: 4102 case eSymbolTypeReExported: 4103 symbol_load_addr = 4104 sc.symbol->GetLoadAddress(&process->GetTarget()); 4105 break; 4106 } 4107 } 4108 } 4109 } 4110 } 4111 // This is the normal path where our symbol lookup was successful 4112 // and we want to send a packet with the new symbol value and see 4113 // if another lookup needs to be done. 4114 4115 // Change "packet" to contain the requested symbol value and name 4116 packet.Clear(); 4117 packet.PutCString("qSymbol:"); 4118 if (symbol_load_addr != LLDB_INVALID_ADDRESS) { 4119 packet.Printf("%" PRIx64, symbol_load_addr); 4120 symbol_response_provided = true; 4121 } else { 4122 symbol_response_provided = false; 4123 } 4124 packet.PutCString(":"); 4125 packet.PutBytesAsRawHex8(symbol_name.data(), symbol_name.size()); 4126 continue; // go back to the while loop and send "packet" and wait 4127 // for another response 4128 } 4129 } 4130 } 4131 } 4132 // If we make it here, the symbol request packet response wasn't valid or 4133 // our symbol lookup failed so we must abort 4134 return; 4135 4136 } else if (Log *log = GetLog(GDBRLog::Process | GDBRLog::Packets)) { 4137 LLDB_LOGF(log, 4138 "GDBRemoteCommunicationClient::%s: Didn't get sequence mutex.", 4139 __FUNCTION__); 4140 } 4141 } 4142 } 4143 4144 StructuredData::Array * 4145 GDBRemoteCommunicationClient::GetSupportedStructuredDataPlugins() { 4146 if (!m_supported_async_json_packets_is_valid) { 4147 // Query the server for the array of supported asynchronous JSON packets. 4148 m_supported_async_json_packets_is_valid = true; 4149 4150 Log *log = GetLog(GDBRLog::Process); 4151 4152 // Poll it now. 4153 StringExtractorGDBRemote response; 4154 if (SendPacketAndWaitForResponse("qStructuredDataPlugins", response) == 4155 PacketResult::Success) { 4156 m_supported_async_json_packets_sp = 4157 StructuredData::ParseJSON(std::string(response.GetStringRef())); 4158 if (m_supported_async_json_packets_sp && 4159 !m_supported_async_json_packets_sp->GetAsArray()) { 4160 // We were returned something other than a JSON array. This is 4161 // invalid. Clear it out. 4162 LLDB_LOGF(log, 4163 "GDBRemoteCommunicationClient::%s(): " 4164 "QSupportedAsyncJSONPackets returned invalid " 4165 "result: %s", 4166 __FUNCTION__, response.GetStringRef().data()); 4167 m_supported_async_json_packets_sp.reset(); 4168 } 4169 } else { 4170 LLDB_LOGF(log, 4171 "GDBRemoteCommunicationClient::%s(): " 4172 "QSupportedAsyncJSONPackets unsupported", 4173 __FUNCTION__); 4174 } 4175 4176 if (log && m_supported_async_json_packets_sp) { 4177 StreamString stream; 4178 m_supported_async_json_packets_sp->Dump(stream); 4179 LLDB_LOGF(log, 4180 "GDBRemoteCommunicationClient::%s(): supported async " 4181 "JSON packets: %s", 4182 __FUNCTION__, stream.GetData()); 4183 } 4184 } 4185 4186 return m_supported_async_json_packets_sp 4187 ? m_supported_async_json_packets_sp->GetAsArray() 4188 : nullptr; 4189 } 4190 4191 Status GDBRemoteCommunicationClient::SendSignalsToIgnore( 4192 llvm::ArrayRef<int32_t> signals) { 4193 // Format packet: 4194 // QPassSignals:<hex_sig1>;<hex_sig2>...;<hex_sigN> 4195 auto range = llvm::make_range(signals.begin(), signals.end()); 4196 std::string packet = formatv("QPassSignals:{0:$[;]@(x-2)}", range).str(); 4197 4198 StringExtractorGDBRemote response; 4199 auto send_status = SendPacketAndWaitForResponse(packet, response); 4200 4201 if (send_status != GDBRemoteCommunication::PacketResult::Success) 4202 return Status("Sending QPassSignals packet failed"); 4203 4204 if (response.IsOKResponse()) { 4205 return Status(); 4206 } else { 4207 return Status("Unknown error happened during sending QPassSignals packet."); 4208 } 4209 } 4210 4211 Status GDBRemoteCommunicationClient::ConfigureRemoteStructuredData( 4212 ConstString type_name, const StructuredData::ObjectSP &config_sp) { 4213 Status error; 4214 4215 if (type_name.GetLength() == 0) { 4216 error.SetErrorString("invalid type_name argument"); 4217 return error; 4218 } 4219 4220 // Build command: Configure{type_name}: serialized config data. 4221 StreamGDBRemote stream; 4222 stream.PutCString("QConfigure"); 4223 stream.PutCString(type_name.GetStringRef()); 4224 stream.PutChar(':'); 4225 if (config_sp) { 4226 // Gather the plain-text version of the configuration data. 4227 StreamString unescaped_stream; 4228 config_sp->Dump(unescaped_stream); 4229 unescaped_stream.Flush(); 4230 4231 // Add it to the stream in escaped fashion. 4232 stream.PutEscapedBytes(unescaped_stream.GetString().data(), 4233 unescaped_stream.GetSize()); 4234 } 4235 stream.Flush(); 4236 4237 // Send the packet. 4238 StringExtractorGDBRemote response; 4239 auto result = SendPacketAndWaitForResponse(stream.GetString(), response); 4240 if (result == PacketResult::Success) { 4241 // We failed if the config result comes back other than OK. 4242 if (strcmp(response.GetStringRef().data(), "OK") == 0) { 4243 // Okay! 4244 error.Clear(); 4245 } else { 4246 error.SetErrorStringWithFormat("configuring StructuredData feature " 4247 "%s failed with error %s", 4248 type_name.AsCString(), 4249 response.GetStringRef().data()); 4250 } 4251 } else { 4252 // Can we get more data here on the failure? 4253 error.SetErrorStringWithFormat("configuring StructuredData feature %s " 4254 "failed when sending packet: " 4255 "PacketResult=%d", 4256 type_name.AsCString(), (int)result); 4257 } 4258 return error; 4259 } 4260 4261 void GDBRemoteCommunicationClient::OnRunPacketSent(bool first) { 4262 GDBRemoteClientBase::OnRunPacketSent(first); 4263 m_curr_tid = LLDB_INVALID_THREAD_ID; 4264 } 4265 4266 bool GDBRemoteCommunicationClient::UsesNativeSignals() { 4267 if (m_uses_native_signals == eLazyBoolCalculate) 4268 GetRemoteQSupported(); 4269 if (m_uses_native_signals == eLazyBoolYes) 4270 return true; 4271 4272 // If the remote didn't indicate native-signal support explicitly, 4273 // check whether it is an old version of lldb-server. 4274 return GetThreadSuffixSupported(); 4275 } 4276 4277 llvm::Expected<int> GDBRemoteCommunicationClient::KillProcess(lldb::pid_t pid) { 4278 StringExtractorGDBRemote response; 4279 GDBRemoteCommunication::ScopedTimeout(*this, seconds(3)); 4280 4281 if (SendPacketAndWaitForResponse("k", response, GetPacketTimeout()) != 4282 PacketResult::Success) 4283 return llvm::createStringError(llvm::inconvertibleErrorCode(), 4284 "failed to send k packet"); 4285 4286 char packet_cmd = response.GetChar(0); 4287 if (packet_cmd == 'W' || packet_cmd == 'X') 4288 return response.GetHexU8(); 4289 4290 return llvm::createStringError(llvm::inconvertibleErrorCode(), 4291 "unexpected response to k packet: %s", 4292 response.GetStringRef().str().c_str()); 4293 } 4294