1 //===-- GDBRemoteCommunicationServerCommon.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 "GDBRemoteCommunicationServerCommon.h" 10 11 #include <cerrno> 12 13 #ifdef __APPLE__ 14 #include <TargetConditionals.h> 15 #endif 16 17 #include <chrono> 18 #include <cstring> 19 20 #include "lldb/Core/ModuleSpec.h" 21 #include "lldb/Host/Config.h" 22 #include "lldb/Host/File.h" 23 #include "lldb/Host/FileAction.h" 24 #include "lldb/Host/FileSystem.h" 25 #include "lldb/Host/Host.h" 26 #include "lldb/Host/HostInfo.h" 27 #include "lldb/Host/SafeMachO.h" 28 #include "lldb/Interpreter/OptionArgParser.h" 29 #include "lldb/Symbol/ObjectFile.h" 30 #include "lldb/Target/Platform.h" 31 #include "lldb/Utility/Endian.h" 32 #include "lldb/Utility/GDBRemote.h" 33 #include "lldb/Utility/LLDBLog.h" 34 #include "lldb/Utility/Log.h" 35 #include "lldb/Utility/StreamString.h" 36 #include "lldb/Utility/StructuredData.h" 37 #include "llvm/ADT/StringSwitch.h" 38 #include "llvm/ADT/Triple.h" 39 #include "llvm/Support/JSON.h" 40 41 #include "ProcessGDBRemoteLog.h" 42 #include "lldb/Utility/StringExtractorGDBRemote.h" 43 44 #ifdef __ANDROID__ 45 #include "lldb/Host/android/HostInfoAndroid.h" 46 #endif 47 48 49 using namespace lldb; 50 using namespace lldb_private::process_gdb_remote; 51 using namespace lldb_private; 52 53 #ifdef __ANDROID__ 54 const static uint32_t g_default_packet_timeout_sec = 20; // seconds 55 #else 56 const static uint32_t g_default_packet_timeout_sec = 0; // not specified 57 #endif 58 59 // GDBRemoteCommunicationServerCommon constructor 60 GDBRemoteCommunicationServerCommon::GDBRemoteCommunicationServerCommon() 61 : GDBRemoteCommunicationServer(), m_process_launch_info(), 62 m_process_launch_error(), m_proc_infos(), m_proc_infos_index(0) { 63 RegisterMemberFunctionHandler(StringExtractorGDBRemote::eServerPacketType_A, 64 &GDBRemoteCommunicationServerCommon::Handle_A); 65 RegisterMemberFunctionHandler( 66 StringExtractorGDBRemote::eServerPacketType_QEnvironment, 67 &GDBRemoteCommunicationServerCommon::Handle_QEnvironment); 68 RegisterMemberFunctionHandler( 69 StringExtractorGDBRemote::eServerPacketType_QEnvironmentHexEncoded, 70 &GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded); 71 RegisterMemberFunctionHandler( 72 StringExtractorGDBRemote::eServerPacketType_qfProcessInfo, 73 &GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo); 74 RegisterMemberFunctionHandler( 75 StringExtractorGDBRemote::eServerPacketType_qGroupName, 76 &GDBRemoteCommunicationServerCommon::Handle_qGroupName); 77 RegisterMemberFunctionHandler( 78 StringExtractorGDBRemote::eServerPacketType_qHostInfo, 79 &GDBRemoteCommunicationServerCommon::Handle_qHostInfo); 80 RegisterMemberFunctionHandler( 81 StringExtractorGDBRemote::eServerPacketType_QLaunchArch, 82 &GDBRemoteCommunicationServerCommon::Handle_QLaunchArch); 83 RegisterMemberFunctionHandler( 84 StringExtractorGDBRemote::eServerPacketType_qLaunchSuccess, 85 &GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess); 86 RegisterMemberFunctionHandler( 87 StringExtractorGDBRemote::eServerPacketType_qEcho, 88 &GDBRemoteCommunicationServerCommon::Handle_qEcho); 89 RegisterMemberFunctionHandler( 90 StringExtractorGDBRemote::eServerPacketType_qModuleInfo, 91 &GDBRemoteCommunicationServerCommon::Handle_qModuleInfo); 92 RegisterMemberFunctionHandler( 93 StringExtractorGDBRemote::eServerPacketType_jModulesInfo, 94 &GDBRemoteCommunicationServerCommon::Handle_jModulesInfo); 95 RegisterMemberFunctionHandler( 96 StringExtractorGDBRemote::eServerPacketType_qPlatform_chmod, 97 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod); 98 RegisterMemberFunctionHandler( 99 StringExtractorGDBRemote::eServerPacketType_qPlatform_mkdir, 100 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir); 101 RegisterMemberFunctionHandler( 102 StringExtractorGDBRemote::eServerPacketType_qPlatform_shell, 103 &GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell); 104 RegisterMemberFunctionHandler( 105 StringExtractorGDBRemote::eServerPacketType_qProcessInfoPID, 106 &GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID); 107 RegisterMemberFunctionHandler( 108 StringExtractorGDBRemote::eServerPacketType_QSetDetachOnError, 109 &GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError); 110 RegisterMemberFunctionHandler( 111 StringExtractorGDBRemote::eServerPacketType_QSetSTDERR, 112 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR); 113 RegisterMemberFunctionHandler( 114 StringExtractorGDBRemote::eServerPacketType_QSetSTDIN, 115 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN); 116 RegisterMemberFunctionHandler( 117 StringExtractorGDBRemote::eServerPacketType_QSetSTDOUT, 118 &GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT); 119 RegisterMemberFunctionHandler( 120 StringExtractorGDBRemote::eServerPacketType_qSpeedTest, 121 &GDBRemoteCommunicationServerCommon::Handle_qSpeedTest); 122 RegisterMemberFunctionHandler( 123 StringExtractorGDBRemote::eServerPacketType_qsProcessInfo, 124 &GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo); 125 RegisterMemberFunctionHandler( 126 StringExtractorGDBRemote::eServerPacketType_QStartNoAckMode, 127 &GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode); 128 RegisterMemberFunctionHandler( 129 StringExtractorGDBRemote::eServerPacketType_qSupported, 130 &GDBRemoteCommunicationServerCommon::Handle_qSupported); 131 RegisterMemberFunctionHandler( 132 StringExtractorGDBRemote::eServerPacketType_qUserName, 133 &GDBRemoteCommunicationServerCommon::Handle_qUserName); 134 RegisterMemberFunctionHandler( 135 StringExtractorGDBRemote::eServerPacketType_vFile_close, 136 &GDBRemoteCommunicationServerCommon::Handle_vFile_Close); 137 RegisterMemberFunctionHandler( 138 StringExtractorGDBRemote::eServerPacketType_vFile_exists, 139 &GDBRemoteCommunicationServerCommon::Handle_vFile_Exists); 140 RegisterMemberFunctionHandler( 141 StringExtractorGDBRemote::eServerPacketType_vFile_md5, 142 &GDBRemoteCommunicationServerCommon::Handle_vFile_MD5); 143 RegisterMemberFunctionHandler( 144 StringExtractorGDBRemote::eServerPacketType_vFile_mode, 145 &GDBRemoteCommunicationServerCommon::Handle_vFile_Mode); 146 RegisterMemberFunctionHandler( 147 StringExtractorGDBRemote::eServerPacketType_vFile_open, 148 &GDBRemoteCommunicationServerCommon::Handle_vFile_Open); 149 RegisterMemberFunctionHandler( 150 StringExtractorGDBRemote::eServerPacketType_vFile_pread, 151 &GDBRemoteCommunicationServerCommon::Handle_vFile_pRead); 152 RegisterMemberFunctionHandler( 153 StringExtractorGDBRemote::eServerPacketType_vFile_pwrite, 154 &GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite); 155 RegisterMemberFunctionHandler( 156 StringExtractorGDBRemote::eServerPacketType_vFile_size, 157 &GDBRemoteCommunicationServerCommon::Handle_vFile_Size); 158 RegisterMemberFunctionHandler( 159 StringExtractorGDBRemote::eServerPacketType_vFile_fstat, 160 &GDBRemoteCommunicationServerCommon::Handle_vFile_FStat); 161 RegisterMemberFunctionHandler( 162 StringExtractorGDBRemote::eServerPacketType_vFile_stat, 163 &GDBRemoteCommunicationServerCommon::Handle_vFile_Stat); 164 RegisterMemberFunctionHandler( 165 StringExtractorGDBRemote::eServerPacketType_vFile_symlink, 166 &GDBRemoteCommunicationServerCommon::Handle_vFile_symlink); 167 RegisterMemberFunctionHandler( 168 StringExtractorGDBRemote::eServerPacketType_vFile_unlink, 169 &GDBRemoteCommunicationServerCommon::Handle_vFile_unlink); 170 } 171 172 // Destructor 173 GDBRemoteCommunicationServerCommon::~GDBRemoteCommunicationServerCommon() = 174 default; 175 176 GDBRemoteCommunication::PacketResult 177 GDBRemoteCommunicationServerCommon::Handle_qHostInfo( 178 StringExtractorGDBRemote &packet) { 179 StreamString response; 180 181 // $cputype:16777223;cpusubtype:3;ostype:Darwin;vendor:apple;endian:little;ptrsize:8;#00 182 183 ArchSpec host_arch(HostInfo::GetArchitecture()); 184 const llvm::Triple &host_triple = host_arch.GetTriple(); 185 response.PutCString("triple:"); 186 response.PutStringAsRawHex8(host_triple.getTriple()); 187 response.Printf(";ptrsize:%u;", host_arch.GetAddressByteSize()); 188 189 const char *distribution_id = host_arch.GetDistributionId().AsCString(); 190 if (distribution_id) { 191 response.PutCString("distribution_id:"); 192 response.PutStringAsRawHex8(distribution_id); 193 response.PutCString(";"); 194 } 195 196 #if defined(__APPLE__) 197 // For parity with debugserver, we'll include the vendor key. 198 response.PutCString("vendor:apple;"); 199 200 // Send out MachO info. 201 uint32_t cpu = host_arch.GetMachOCPUType(); 202 uint32_t sub = host_arch.GetMachOCPUSubType(); 203 if (cpu != LLDB_INVALID_CPUTYPE) 204 response.Printf("cputype:%u;", cpu); 205 if (sub != LLDB_INVALID_CPUTYPE) 206 response.Printf("cpusubtype:%u;", sub); 207 208 if (cpu == llvm::MachO::CPU_TYPE_ARM || cpu == llvm::MachO::CPU_TYPE_ARM64) { 209 // Indicate the OS type. 210 #if defined(TARGET_OS_TV) && TARGET_OS_TV == 1 211 response.PutCString("ostype:tvos;"); 212 #elif defined(TARGET_OS_WATCH) && TARGET_OS_WATCH == 1 213 response.PutCString("ostype:watchos;"); 214 #elif defined(TARGET_OS_BRIDGE) && TARGET_OS_BRIDGE == 1 215 response.PutCString("ostype:bridgeos;"); 216 #else 217 response.PutCString("ostype:ios;"); 218 #endif 219 220 // On arm, we use "synchronous" watchpoints which means the exception is 221 // delivered before the instruction executes. 222 response.PutCString("watchpoint_exceptions_received:before;"); 223 } else { 224 response.PutCString("ostype:macosx;"); 225 response.Printf("watchpoint_exceptions_received:after;"); 226 } 227 228 #else 229 if (host_arch.GetMachine() == llvm::Triple::aarch64 || 230 host_arch.GetMachine() == llvm::Triple::aarch64_32 || 231 host_arch.GetMachine() == llvm::Triple::aarch64_be || 232 host_arch.GetMachine() == llvm::Triple::arm || 233 host_arch.GetMachine() == llvm::Triple::armeb || host_arch.IsMIPS()) 234 response.Printf("watchpoint_exceptions_received:before;"); 235 else 236 response.Printf("watchpoint_exceptions_received:after;"); 237 #endif 238 239 switch (endian::InlHostByteOrder()) { 240 case eByteOrderBig: 241 response.PutCString("endian:big;"); 242 break; 243 case eByteOrderLittle: 244 response.PutCString("endian:little;"); 245 break; 246 case eByteOrderPDP: 247 response.PutCString("endian:pdp;"); 248 break; 249 default: 250 response.PutCString("endian:unknown;"); 251 break; 252 } 253 254 llvm::VersionTuple version = HostInfo::GetOSVersion(); 255 if (!version.empty()) { 256 response.Format("os_version:{0}", version.getAsString()); 257 response.PutChar(';'); 258 } 259 260 #if defined(__APPLE__) 261 llvm::VersionTuple maccatalyst_version = HostInfo::GetMacCatalystVersion(); 262 if (!maccatalyst_version.empty()) { 263 response.Format("maccatalyst_version:{0}", 264 maccatalyst_version.getAsString()); 265 response.PutChar(';'); 266 } 267 #endif 268 269 if (llvm::Optional<std::string> s = HostInfo::GetOSBuildString()) { 270 response.PutCString("os_build:"); 271 response.PutStringAsRawHex8(*s); 272 response.PutChar(';'); 273 } 274 if (llvm::Optional<std::string> s = HostInfo::GetOSKernelDescription()) { 275 response.PutCString("os_kernel:"); 276 response.PutStringAsRawHex8(*s); 277 response.PutChar(';'); 278 } 279 280 std::string s; 281 #if defined(__APPLE__) 282 283 #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 284 // For iOS devices, we are connected through a USB Mux so we never pretend to 285 // actually have a hostname as far as the remote lldb that is connecting to 286 // this lldb-platform is concerned 287 response.PutCString("hostname:"); 288 response.PutStringAsRawHex8("127.0.0.1"); 289 response.PutChar(';'); 290 #else // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 291 if (HostInfo::GetHostname(s)) { 292 response.PutCString("hostname:"); 293 response.PutStringAsRawHex8(s); 294 response.PutChar(';'); 295 } 296 #endif // #if defined(__arm__) || defined(__arm64__) || defined(__aarch64__) 297 298 #else // #if defined(__APPLE__) 299 if (HostInfo::GetHostname(s)) { 300 response.PutCString("hostname:"); 301 response.PutStringAsRawHex8(s); 302 response.PutChar(';'); 303 } 304 #endif // #if defined(__APPLE__) 305 // coverity[unsigned_compare] 306 if (g_default_packet_timeout_sec > 0) 307 response.Printf("default_packet_timeout:%u;", g_default_packet_timeout_sec); 308 309 return SendPacketNoLock(response.GetString()); 310 } 311 312 GDBRemoteCommunication::PacketResult 313 GDBRemoteCommunicationServerCommon::Handle_qProcessInfoPID( 314 StringExtractorGDBRemote &packet) { 315 // Packet format: "qProcessInfoPID:%i" where %i is the pid 316 packet.SetFilePos(::strlen("qProcessInfoPID:")); 317 lldb::pid_t pid = packet.GetU32(LLDB_INVALID_PROCESS_ID); 318 if (pid != LLDB_INVALID_PROCESS_ID) { 319 ProcessInstanceInfo proc_info; 320 if (Host::GetProcessInfo(pid, proc_info)) { 321 StreamString response; 322 CreateProcessInfoResponse(proc_info, response); 323 return SendPacketNoLock(response.GetString()); 324 } 325 } 326 return SendErrorResponse(1); 327 } 328 329 GDBRemoteCommunication::PacketResult 330 GDBRemoteCommunicationServerCommon::Handle_qfProcessInfo( 331 StringExtractorGDBRemote &packet) { 332 m_proc_infos_index = 0; 333 m_proc_infos.clear(); 334 335 ProcessInstanceInfoMatch match_info; 336 packet.SetFilePos(::strlen("qfProcessInfo")); 337 if (packet.GetChar() == ':') { 338 llvm::StringRef key; 339 llvm::StringRef value; 340 while (packet.GetNameColonValue(key, value)) { 341 bool success = true; 342 if (key.equals("name")) { 343 StringExtractor extractor(value); 344 std::string file; 345 extractor.GetHexByteString(file); 346 match_info.GetProcessInfo().GetExecutableFile().SetFile( 347 file, FileSpec::Style::native); 348 } else if (key.equals("name_match")) { 349 NameMatch name_match = llvm::StringSwitch<NameMatch>(value) 350 .Case("equals", NameMatch::Equals) 351 .Case("starts_with", NameMatch::StartsWith) 352 .Case("ends_with", NameMatch::EndsWith) 353 .Case("contains", NameMatch::Contains) 354 .Case("regex", NameMatch::RegularExpression) 355 .Default(NameMatch::Ignore); 356 match_info.SetNameMatchType(name_match); 357 if (name_match == NameMatch::Ignore) 358 return SendErrorResponse(2); 359 } else if (key.equals("pid")) { 360 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 361 if (value.getAsInteger(0, pid)) 362 return SendErrorResponse(2); 363 match_info.GetProcessInfo().SetProcessID(pid); 364 } else if (key.equals("parent_pid")) { 365 lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; 366 if (value.getAsInteger(0, pid)) 367 return SendErrorResponse(2); 368 match_info.GetProcessInfo().SetParentProcessID(pid); 369 } else if (key.equals("uid")) { 370 uint32_t uid = UINT32_MAX; 371 if (value.getAsInteger(0, uid)) 372 return SendErrorResponse(2); 373 match_info.GetProcessInfo().SetUserID(uid); 374 } else if (key.equals("gid")) { 375 uint32_t gid = UINT32_MAX; 376 if (value.getAsInteger(0, gid)) 377 return SendErrorResponse(2); 378 match_info.GetProcessInfo().SetGroupID(gid); 379 } else if (key.equals("euid")) { 380 uint32_t uid = UINT32_MAX; 381 if (value.getAsInteger(0, uid)) 382 return SendErrorResponse(2); 383 match_info.GetProcessInfo().SetEffectiveUserID(uid); 384 } else if (key.equals("egid")) { 385 uint32_t gid = UINT32_MAX; 386 if (value.getAsInteger(0, gid)) 387 return SendErrorResponse(2); 388 match_info.GetProcessInfo().SetEffectiveGroupID(gid); 389 } else if (key.equals("all_users")) { 390 match_info.SetMatchAllUsers( 391 OptionArgParser::ToBoolean(value, false, &success)); 392 } else if (key.equals("triple")) { 393 match_info.GetProcessInfo().GetArchitecture() = 394 HostInfo::GetAugmentedArchSpec(value); 395 } else { 396 success = false; 397 } 398 399 if (!success) 400 return SendErrorResponse(2); 401 } 402 } 403 404 if (Host::FindProcesses(match_info, m_proc_infos)) { 405 // We found something, return the first item by calling the get subsequent 406 // process info packet handler... 407 return Handle_qsProcessInfo(packet); 408 } 409 return SendErrorResponse(3); 410 } 411 412 GDBRemoteCommunication::PacketResult 413 GDBRemoteCommunicationServerCommon::Handle_qsProcessInfo( 414 StringExtractorGDBRemote &packet) { 415 if (m_proc_infos_index < m_proc_infos.size()) { 416 StreamString response; 417 CreateProcessInfoResponse(m_proc_infos[m_proc_infos_index], response); 418 ++m_proc_infos_index; 419 return SendPacketNoLock(response.GetString()); 420 } 421 return SendErrorResponse(4); 422 } 423 424 GDBRemoteCommunication::PacketResult 425 GDBRemoteCommunicationServerCommon::Handle_qUserName( 426 StringExtractorGDBRemote &packet) { 427 #if LLDB_ENABLE_POSIX 428 Log *log = GetLog(LLDBLog::Process); 429 LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s begin", __FUNCTION__); 430 431 // Packet format: "qUserName:%i" where %i is the uid 432 packet.SetFilePos(::strlen("qUserName:")); 433 uint32_t uid = packet.GetU32(UINT32_MAX); 434 if (uid != UINT32_MAX) { 435 if (llvm::Optional<llvm::StringRef> name = 436 HostInfo::GetUserIDResolver().GetUserName(uid)) { 437 StreamString response; 438 response.PutStringAsRawHex8(*name); 439 return SendPacketNoLock(response.GetString()); 440 } 441 } 442 LLDB_LOGF(log, "GDBRemoteCommunicationServerCommon::%s end", __FUNCTION__); 443 #endif 444 return SendErrorResponse(5); 445 } 446 447 GDBRemoteCommunication::PacketResult 448 GDBRemoteCommunicationServerCommon::Handle_qGroupName( 449 StringExtractorGDBRemote &packet) { 450 #if LLDB_ENABLE_POSIX 451 // Packet format: "qGroupName:%i" where %i is the gid 452 packet.SetFilePos(::strlen("qGroupName:")); 453 uint32_t gid = packet.GetU32(UINT32_MAX); 454 if (gid != UINT32_MAX) { 455 if (llvm::Optional<llvm::StringRef> name = 456 HostInfo::GetUserIDResolver().GetGroupName(gid)) { 457 StreamString response; 458 response.PutStringAsRawHex8(*name); 459 return SendPacketNoLock(response.GetString()); 460 } 461 } 462 #endif 463 return SendErrorResponse(6); 464 } 465 466 GDBRemoteCommunication::PacketResult 467 GDBRemoteCommunicationServerCommon::Handle_qSpeedTest( 468 StringExtractorGDBRemote &packet) { 469 packet.SetFilePos(::strlen("qSpeedTest:")); 470 471 llvm::StringRef key; 472 llvm::StringRef value; 473 bool success = packet.GetNameColonValue(key, value); 474 if (success && key.equals("response_size")) { 475 uint32_t response_size = 0; 476 if (!value.getAsInteger(0, response_size)) { 477 if (response_size == 0) 478 return SendOKResponse(); 479 StreamString response; 480 uint32_t bytes_left = response_size; 481 response.PutCString("data:"); 482 while (bytes_left > 0) { 483 if (bytes_left >= 26) { 484 response.PutCString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 485 bytes_left -= 26; 486 } else { 487 response.Printf("%*.*s;", bytes_left, bytes_left, 488 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"); 489 bytes_left = 0; 490 } 491 } 492 return SendPacketNoLock(response.GetString()); 493 } 494 } 495 return SendErrorResponse(7); 496 } 497 498 GDBRemoteCommunication::PacketResult 499 GDBRemoteCommunicationServerCommon::Handle_vFile_Open( 500 StringExtractorGDBRemote &packet) { 501 packet.SetFilePos(::strlen("vFile:open:")); 502 std::string path; 503 packet.GetHexByteStringTerminatedBy(path, ','); 504 if (!path.empty()) { 505 if (packet.GetChar() == ',') { 506 auto flags = File::OpenOptions(packet.GetHexMaxU32(false, 0)); 507 if (packet.GetChar() == ',') { 508 mode_t mode = packet.GetHexMaxU32(false, 0600); 509 FileSpec path_spec(path); 510 FileSystem::Instance().Resolve(path_spec); 511 // Do not close fd. 512 auto file = FileSystem::Instance().Open(path_spec, flags, mode, false); 513 514 StreamString response; 515 response.PutChar('F'); 516 517 int descriptor = File::kInvalidDescriptor; 518 if (file) { 519 descriptor = file.get()->GetDescriptor(); 520 response.Printf("%x", descriptor); 521 } else { 522 response.PutCString("-1"); 523 std::error_code code = errorToErrorCode(file.takeError()); 524 if (code.category() == std::system_category()) { 525 response.Printf(",%x", code.value()); 526 } 527 } 528 529 return SendPacketNoLock(response.GetString()); 530 } 531 } 532 } 533 return SendErrorResponse(18); 534 } 535 536 static GDBErrno system_errno_to_gdb(int err) { 537 switch (err) { 538 #define HANDLE_ERRNO(name, value) \ 539 case name: \ 540 return GDB_##name; 541 #include "Plugins/Process/gdb-remote/GDBRemoteErrno.def" 542 default: 543 return GDB_EUNKNOWN; 544 } 545 } 546 547 GDBRemoteCommunication::PacketResult 548 GDBRemoteCommunicationServerCommon::Handle_vFile_Close( 549 StringExtractorGDBRemote &packet) { 550 packet.SetFilePos(::strlen("vFile:close:")); 551 int fd = packet.GetS32(-1, 16); 552 int err = -1; 553 int save_errno = 0; 554 if (fd >= 0) { 555 NativeFile file(fd, File::OpenOptions(0), true); 556 Status error = file.Close(); 557 err = 0; 558 save_errno = error.GetError(); 559 } else { 560 save_errno = EINVAL; 561 } 562 StreamString response; 563 response.PutChar('F'); 564 response.Printf("%x", err); 565 if (save_errno) 566 response.Printf(",%x", system_errno_to_gdb(save_errno)); 567 return SendPacketNoLock(response.GetString()); 568 } 569 570 GDBRemoteCommunication::PacketResult 571 GDBRemoteCommunicationServerCommon::Handle_vFile_pRead( 572 StringExtractorGDBRemote &packet) { 573 StreamGDBRemote response; 574 packet.SetFilePos(::strlen("vFile:pread:")); 575 int fd = packet.GetS32(-1, 16); 576 if (packet.GetChar() == ',') { 577 size_t count = packet.GetHexMaxU64(false, SIZE_MAX); 578 if (packet.GetChar() == ',') { 579 off_t offset = packet.GetHexMaxU32(false, UINT32_MAX); 580 if (count == SIZE_MAX) { 581 response.Printf("F-1:%x", EINVAL); 582 return SendPacketNoLock(response.GetString()); 583 } 584 585 std::string buffer(count, 0); 586 NativeFile file(fd, File::eOpenOptionReadOnly, false); 587 Status error = file.Read(static_cast<void *>(&buffer[0]), count, offset); 588 const int save_errno = error.GetError(); 589 response.PutChar('F'); 590 if (error.Success()) { 591 response.Printf("%zx", count); 592 response.PutChar(';'); 593 response.PutEscapedBytes(&buffer[0], count); 594 } else { 595 response.PutCString("-1"); 596 if (save_errno) 597 response.Printf(",%x", system_errno_to_gdb(save_errno)); 598 } 599 return SendPacketNoLock(response.GetString()); 600 } 601 } 602 return SendErrorResponse(21); 603 } 604 605 GDBRemoteCommunication::PacketResult 606 GDBRemoteCommunicationServerCommon::Handle_vFile_pWrite( 607 StringExtractorGDBRemote &packet) { 608 packet.SetFilePos(::strlen("vFile:pwrite:")); 609 610 StreamGDBRemote response; 611 response.PutChar('F'); 612 613 int fd = packet.GetS32(-1, 16); 614 if (packet.GetChar() == ',') { 615 off_t offset = packet.GetHexMaxU32(false, UINT32_MAX); 616 if (packet.GetChar() == ',') { 617 std::string buffer; 618 if (packet.GetEscapedBinaryData(buffer)) { 619 NativeFile file(fd, File::eOpenOptionWriteOnly, false); 620 size_t count = buffer.size(); 621 Status error = 622 file.Write(static_cast<const void *>(&buffer[0]), count, offset); 623 const int save_errno = error.GetError(); 624 if (error.Success()) 625 response.Printf("%zx", count); 626 else { 627 response.PutCString("-1"); 628 if (save_errno) 629 response.Printf(",%x", system_errno_to_gdb(save_errno)); 630 } 631 } else { 632 response.Printf("-1,%x", EINVAL); 633 } 634 return SendPacketNoLock(response.GetString()); 635 } 636 } 637 return SendErrorResponse(27); 638 } 639 640 GDBRemoteCommunication::PacketResult 641 GDBRemoteCommunicationServerCommon::Handle_vFile_Size( 642 StringExtractorGDBRemote &packet) { 643 packet.SetFilePos(::strlen("vFile:size:")); 644 std::string path; 645 packet.GetHexByteString(path); 646 if (!path.empty()) { 647 uint64_t Size; 648 if (llvm::sys::fs::file_size(path, Size)) 649 return SendErrorResponse(5); 650 StreamString response; 651 response.PutChar('F'); 652 response.PutHex64(Size); 653 if (Size == UINT64_MAX) { 654 response.PutChar(','); 655 response.PutHex64(Size); // TODO: replace with Host::GetSyswideErrorCode() 656 } 657 return SendPacketNoLock(response.GetString()); 658 } 659 return SendErrorResponse(22); 660 } 661 662 GDBRemoteCommunication::PacketResult 663 GDBRemoteCommunicationServerCommon::Handle_vFile_Mode( 664 StringExtractorGDBRemote &packet) { 665 packet.SetFilePos(::strlen("vFile:mode:")); 666 std::string path; 667 packet.GetHexByteString(path); 668 if (!path.empty()) { 669 FileSpec file_spec(path); 670 FileSystem::Instance().Resolve(file_spec); 671 std::error_code ec; 672 const uint32_t mode = FileSystem::Instance().GetPermissions(file_spec, ec); 673 StreamString response; 674 if (mode != llvm::sys::fs::perms_not_known) 675 response.Printf("F%x", mode); 676 else 677 response.Printf("F-1,%x", (int)Status(ec).GetError()); 678 return SendPacketNoLock(response.GetString()); 679 } 680 return SendErrorResponse(23); 681 } 682 683 GDBRemoteCommunication::PacketResult 684 GDBRemoteCommunicationServerCommon::Handle_vFile_Exists( 685 StringExtractorGDBRemote &packet) { 686 packet.SetFilePos(::strlen("vFile:exists:")); 687 std::string path; 688 packet.GetHexByteString(path); 689 if (!path.empty()) { 690 bool retcode = llvm::sys::fs::exists(path); 691 StreamString response; 692 response.PutChar('F'); 693 response.PutChar(','); 694 if (retcode) 695 response.PutChar('1'); 696 else 697 response.PutChar('0'); 698 return SendPacketNoLock(response.GetString()); 699 } 700 return SendErrorResponse(24); 701 } 702 703 GDBRemoteCommunication::PacketResult 704 GDBRemoteCommunicationServerCommon::Handle_vFile_symlink( 705 StringExtractorGDBRemote &packet) { 706 packet.SetFilePos(::strlen("vFile:symlink:")); 707 std::string dst, src; 708 packet.GetHexByteStringTerminatedBy(dst, ','); 709 packet.GetChar(); // Skip ',' char 710 packet.GetHexByteString(src); 711 712 FileSpec src_spec(src); 713 FileSystem::Instance().Resolve(src_spec); 714 Status error = FileSystem::Instance().Symlink(src_spec, FileSpec(dst)); 715 716 StreamString response; 717 response.Printf("F%x,%x", error.GetError(), error.GetError()); 718 return SendPacketNoLock(response.GetString()); 719 } 720 721 GDBRemoteCommunication::PacketResult 722 GDBRemoteCommunicationServerCommon::Handle_vFile_unlink( 723 StringExtractorGDBRemote &packet) { 724 packet.SetFilePos(::strlen("vFile:unlink:")); 725 std::string path; 726 packet.GetHexByteString(path); 727 Status error(llvm::sys::fs::remove(path)); 728 StreamString response; 729 response.Printf("F%x,%x", error.GetError(), error.GetError()); 730 return SendPacketNoLock(response.GetString()); 731 } 732 733 GDBRemoteCommunication::PacketResult 734 GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell( 735 StringExtractorGDBRemote &packet) { 736 packet.SetFilePos(::strlen("qPlatform_shell:")); 737 std::string path; 738 std::string working_dir; 739 packet.GetHexByteStringTerminatedBy(path, ','); 740 if (!path.empty()) { 741 if (packet.GetChar() == ',') { 742 // FIXME: add timeout to qPlatform_shell packet 743 // uint32_t timeout = packet.GetHexMaxU32(false, 32); 744 if (packet.GetChar() == ',') 745 packet.GetHexByteString(working_dir); 746 int status, signo; 747 std::string output; 748 FileSpec working_spec(working_dir); 749 FileSystem::Instance().Resolve(working_spec); 750 Status err = Host::RunShellCommand(path, working_spec, &status, &signo, 751 &output, std::chrono::seconds(10)); 752 StreamGDBRemote response; 753 if (err.Fail()) { 754 response.PutCString("F,"); 755 response.PutHex32(UINT32_MAX); 756 } else { 757 response.PutCString("F,"); 758 response.PutHex32(status); 759 response.PutChar(','); 760 response.PutHex32(signo); 761 response.PutChar(','); 762 response.PutEscapedBytes(output.c_str(), output.size()); 763 } 764 return SendPacketNoLock(response.GetString()); 765 } 766 } 767 return SendErrorResponse(24); 768 } 769 770 template <typename T, typename U> 771 static void fill_clamp(T &dest, U src, typename T::value_type fallback) { 772 static_assert(std::is_unsigned<typename T::value_type>::value, 773 "Destination type must be unsigned."); 774 using UU = std::make_unsigned_t<U>; 775 constexpr auto T_max = std::numeric_limits<typename T::value_type>::max(); 776 dest = src >= 0 && static_cast<UU>(src) <= T_max ? src : fallback; 777 } 778 779 GDBRemoteCommunication::PacketResult 780 GDBRemoteCommunicationServerCommon::Handle_vFile_FStat( 781 StringExtractorGDBRemote &packet) { 782 StreamGDBRemote response; 783 packet.SetFilePos(::strlen("vFile:fstat:")); 784 int fd = packet.GetS32(-1, 16); 785 786 struct stat file_stats; 787 if (::fstat(fd, &file_stats) == -1) { 788 const int save_errno = errno; 789 response.Printf("F-1,%x", system_errno_to_gdb(save_errno)); 790 return SendPacketNoLock(response.GetString()); 791 } 792 793 GDBRemoteFStatData data; 794 fill_clamp(data.gdb_st_dev, file_stats.st_dev, 0); 795 fill_clamp(data.gdb_st_ino, file_stats.st_ino, 0); 796 data.gdb_st_mode = file_stats.st_mode; 797 fill_clamp(data.gdb_st_nlink, file_stats.st_nlink, UINT32_MAX); 798 fill_clamp(data.gdb_st_uid, file_stats.st_uid, 0); 799 fill_clamp(data.gdb_st_gid, file_stats.st_gid, 0); 800 fill_clamp(data.gdb_st_rdev, file_stats.st_rdev, 0); 801 data.gdb_st_size = file_stats.st_size; 802 #if !defined(_WIN32) 803 data.gdb_st_blksize = file_stats.st_blksize; 804 data.gdb_st_blocks = file_stats.st_blocks; 805 #else 806 data.gdb_st_blksize = 0; 807 data.gdb_st_blocks = 0; 808 #endif 809 fill_clamp(data.gdb_st_atime, file_stats.st_atime, 0); 810 fill_clamp(data.gdb_st_mtime, file_stats.st_mtime, 0); 811 fill_clamp(data.gdb_st_ctime, file_stats.st_ctime, 0); 812 813 response.Printf("F%zx;", sizeof(data)); 814 response.PutEscapedBytes(&data, sizeof(data)); 815 return SendPacketNoLock(response.GetString()); 816 } 817 818 GDBRemoteCommunication::PacketResult 819 GDBRemoteCommunicationServerCommon::Handle_vFile_Stat( 820 StringExtractorGDBRemote &packet) { 821 return SendUnimplementedResponse( 822 "GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented"); 823 } 824 825 GDBRemoteCommunication::PacketResult 826 GDBRemoteCommunicationServerCommon::Handle_vFile_MD5( 827 StringExtractorGDBRemote &packet) { 828 packet.SetFilePos(::strlen("vFile:MD5:")); 829 std::string path; 830 packet.GetHexByteString(path); 831 if (!path.empty()) { 832 StreamGDBRemote response; 833 auto Result = llvm::sys::fs::md5_contents(path); 834 if (!Result) { 835 response.PutCString("F,"); 836 response.PutCString("x"); 837 } else { 838 response.PutCString("F,"); 839 response.PutHex64(Result->low()); 840 response.PutHex64(Result->high()); 841 } 842 return SendPacketNoLock(response.GetString()); 843 } 844 return SendErrorResponse(25); 845 } 846 847 GDBRemoteCommunication::PacketResult 848 GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir( 849 StringExtractorGDBRemote &packet) { 850 packet.SetFilePos(::strlen("qPlatform_mkdir:")); 851 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX); 852 if (packet.GetChar() == ',') { 853 std::string path; 854 packet.GetHexByteString(path); 855 Status error(llvm::sys::fs::create_directory(path, mode)); 856 857 StreamGDBRemote response; 858 response.Printf("F%x", error.GetError()); 859 860 return SendPacketNoLock(response.GetString()); 861 } 862 return SendErrorResponse(20); 863 } 864 865 GDBRemoteCommunication::PacketResult 866 GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod( 867 StringExtractorGDBRemote &packet) { 868 packet.SetFilePos(::strlen("qPlatform_chmod:")); 869 870 auto perms = 871 static_cast<llvm::sys::fs::perms>(packet.GetHexMaxU32(false, UINT32_MAX)); 872 if (packet.GetChar() == ',') { 873 std::string path; 874 packet.GetHexByteString(path); 875 Status error(llvm::sys::fs::setPermissions(path, perms)); 876 877 StreamGDBRemote response; 878 response.Printf("F%x", error.GetError()); 879 880 return SendPacketNoLock(response.GetString()); 881 } 882 return SendErrorResponse(19); 883 } 884 885 GDBRemoteCommunication::PacketResult 886 GDBRemoteCommunicationServerCommon::Handle_qSupported( 887 StringExtractorGDBRemote &packet) { 888 // Parse client-indicated features. 889 llvm::SmallVector<llvm::StringRef, 4> client_features; 890 packet.GetStringRef().split(client_features, ';'); 891 return SendPacketNoLock(llvm::join(HandleFeatures(client_features), ";")); 892 } 893 894 GDBRemoteCommunication::PacketResult 895 GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError( 896 StringExtractorGDBRemote &packet) { 897 packet.SetFilePos(::strlen("QSetDetachOnError:")); 898 if (packet.GetU32(0)) 899 m_process_launch_info.GetFlags().Set(eLaunchFlagDetachOnError); 900 else 901 m_process_launch_info.GetFlags().Clear(eLaunchFlagDetachOnError); 902 return SendOKResponse(); 903 } 904 905 GDBRemoteCommunication::PacketResult 906 GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode( 907 StringExtractorGDBRemote &packet) { 908 // Send response first before changing m_send_acks to we ack this packet 909 PacketResult packet_result = SendOKResponse(); 910 m_send_acks = false; 911 return packet_result; 912 } 913 914 GDBRemoteCommunication::PacketResult 915 GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN( 916 StringExtractorGDBRemote &packet) { 917 packet.SetFilePos(::strlen("QSetSTDIN:")); 918 FileAction file_action; 919 std::string path; 920 packet.GetHexByteString(path); 921 const bool read = true; 922 const bool write = false; 923 if (file_action.Open(STDIN_FILENO, FileSpec(path), read, write)) { 924 m_process_launch_info.AppendFileAction(file_action); 925 return SendOKResponse(); 926 } 927 return SendErrorResponse(15); 928 } 929 930 GDBRemoteCommunication::PacketResult 931 GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT( 932 StringExtractorGDBRemote &packet) { 933 packet.SetFilePos(::strlen("QSetSTDOUT:")); 934 FileAction file_action; 935 std::string path; 936 packet.GetHexByteString(path); 937 const bool read = false; 938 const bool write = true; 939 if (file_action.Open(STDOUT_FILENO, FileSpec(path), read, write)) { 940 m_process_launch_info.AppendFileAction(file_action); 941 return SendOKResponse(); 942 } 943 return SendErrorResponse(16); 944 } 945 946 GDBRemoteCommunication::PacketResult 947 GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR( 948 StringExtractorGDBRemote &packet) { 949 packet.SetFilePos(::strlen("QSetSTDERR:")); 950 FileAction file_action; 951 std::string path; 952 packet.GetHexByteString(path); 953 const bool read = false; 954 const bool write = true; 955 if (file_action.Open(STDERR_FILENO, FileSpec(path), read, write)) { 956 m_process_launch_info.AppendFileAction(file_action); 957 return SendOKResponse(); 958 } 959 return SendErrorResponse(17); 960 } 961 962 GDBRemoteCommunication::PacketResult 963 GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess( 964 StringExtractorGDBRemote &packet) { 965 if (m_process_launch_error.Success()) 966 return SendOKResponse(); 967 StreamString response; 968 response.PutChar('E'); 969 response.PutCString(m_process_launch_error.AsCString("<unknown error>")); 970 return SendPacketNoLock(response.GetString()); 971 } 972 973 GDBRemoteCommunication::PacketResult 974 GDBRemoteCommunicationServerCommon::Handle_QEnvironment( 975 StringExtractorGDBRemote &packet) { 976 packet.SetFilePos(::strlen("QEnvironment:")); 977 const uint32_t bytes_left = packet.GetBytesLeft(); 978 if (bytes_left > 0) { 979 m_process_launch_info.GetEnvironment().insert(packet.Peek()); 980 return SendOKResponse(); 981 } 982 return SendErrorResponse(12); 983 } 984 985 GDBRemoteCommunication::PacketResult 986 GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded( 987 StringExtractorGDBRemote &packet) { 988 packet.SetFilePos(::strlen("QEnvironmentHexEncoded:")); 989 const uint32_t bytes_left = packet.GetBytesLeft(); 990 if (bytes_left > 0) { 991 std::string str; 992 packet.GetHexByteString(str); 993 m_process_launch_info.GetEnvironment().insert(str); 994 return SendOKResponse(); 995 } 996 return SendErrorResponse(12); 997 } 998 999 GDBRemoteCommunication::PacketResult 1000 GDBRemoteCommunicationServerCommon::Handle_QLaunchArch( 1001 StringExtractorGDBRemote &packet) { 1002 packet.SetFilePos(::strlen("QLaunchArch:")); 1003 const uint32_t bytes_left = packet.GetBytesLeft(); 1004 if (bytes_left > 0) { 1005 const char *arch_triple = packet.Peek(); 1006 m_process_launch_info.SetArchitecture( 1007 HostInfo::GetAugmentedArchSpec(arch_triple)); 1008 return SendOKResponse(); 1009 } 1010 return SendErrorResponse(13); 1011 } 1012 1013 GDBRemoteCommunication::PacketResult 1014 GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) { 1015 // The 'A' packet is the most over designed packet ever here with redundant 1016 // argument indexes, redundant argument lengths and needed hex encoded 1017 // argument string values. Really all that is needed is a comma separated hex 1018 // encoded argument value list, but we will stay true to the documented 1019 // version of the 'A' packet here... 1020 1021 Log *log = GetLog(LLDBLog::Process); 1022 int actual_arg_index = 0; 1023 1024 packet.SetFilePos(1); // Skip the 'A' 1025 bool success = true; 1026 while (success && packet.GetBytesLeft() > 0) { 1027 // Decode the decimal argument string length. This length is the number of 1028 // hex nibbles in the argument string value. 1029 const uint32_t arg_len = packet.GetU32(UINT32_MAX); 1030 if (arg_len == UINT32_MAX) 1031 success = false; 1032 else { 1033 // Make sure the argument hex string length is followed by a comma 1034 if (packet.GetChar() != ',') 1035 success = false; 1036 else { 1037 // Decode the argument index. We ignore this really because who would 1038 // really send down the arguments in a random order??? 1039 const uint32_t arg_idx = packet.GetU32(UINT32_MAX); 1040 if (arg_idx == UINT32_MAX) 1041 success = false; 1042 else { 1043 // Make sure the argument index is followed by a comma 1044 if (packet.GetChar() != ',') 1045 success = false; 1046 else { 1047 // Decode the argument string value from hex bytes back into a UTF8 1048 // string and make sure the length matches the one supplied in the 1049 // packet 1050 std::string arg; 1051 if (packet.GetHexByteStringFixedLength(arg, arg_len) != 1052 (arg_len / 2)) 1053 success = false; 1054 else { 1055 // If there are any bytes left 1056 if (packet.GetBytesLeft()) { 1057 if (packet.GetChar() != ',') 1058 success = false; 1059 } 1060 1061 if (success) { 1062 if (arg_idx == 0) 1063 m_process_launch_info.GetExecutableFile().SetFile( 1064 arg, FileSpec::Style::native); 1065 m_process_launch_info.GetArguments().AppendArgument(arg); 1066 LLDB_LOGF(log, "LLGSPacketHandler::%s added arg %d: \"%s\"", 1067 __FUNCTION__, actual_arg_index, arg.c_str()); 1068 ++actual_arg_index; 1069 } 1070 } 1071 } 1072 } 1073 } 1074 } 1075 } 1076 1077 if (success) { 1078 m_process_launch_error = LaunchProcess(); 1079 if (m_process_launch_error.Success()) 1080 return SendOKResponse(); 1081 LLDB_LOG(log, "failed to launch exe: {0}", m_process_launch_error); 1082 } 1083 return SendErrorResponse(8); 1084 } 1085 1086 GDBRemoteCommunication::PacketResult 1087 GDBRemoteCommunicationServerCommon::Handle_qEcho( 1088 StringExtractorGDBRemote &packet) { 1089 // Just echo back the exact same packet for qEcho... 1090 return SendPacketNoLock(packet.GetStringRef()); 1091 } 1092 1093 GDBRemoteCommunication::PacketResult 1094 GDBRemoteCommunicationServerCommon::Handle_qModuleInfo( 1095 StringExtractorGDBRemote &packet) { 1096 packet.SetFilePos(::strlen("qModuleInfo:")); 1097 1098 std::string module_path; 1099 packet.GetHexByteStringTerminatedBy(module_path, ';'); 1100 if (module_path.empty()) 1101 return SendErrorResponse(1); 1102 1103 if (packet.GetChar() != ';') 1104 return SendErrorResponse(2); 1105 1106 std::string triple; 1107 packet.GetHexByteString(triple); 1108 1109 ModuleSpec matched_module_spec = GetModuleInfo(module_path, triple); 1110 if (!matched_module_spec.GetFileSpec()) 1111 return SendErrorResponse(3); 1112 1113 const auto file_offset = matched_module_spec.GetObjectOffset(); 1114 const auto file_size = matched_module_spec.GetObjectSize(); 1115 const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); 1116 1117 StreamGDBRemote response; 1118 1119 if (uuid_str.empty()) { 1120 auto Result = llvm::sys::fs::md5_contents( 1121 matched_module_spec.GetFileSpec().GetPath()); 1122 if (!Result) 1123 return SendErrorResponse(5); 1124 response.PutCString("md5:"); 1125 response.PutStringAsRawHex8(Result->digest()); 1126 } else { 1127 response.PutCString("uuid:"); 1128 response.PutStringAsRawHex8(uuid_str); 1129 } 1130 response.PutChar(';'); 1131 1132 const auto &module_arch = matched_module_spec.GetArchitecture(); 1133 response.PutCString("triple:"); 1134 response.PutStringAsRawHex8(module_arch.GetTriple().getTriple()); 1135 response.PutChar(';'); 1136 1137 response.PutCString("file_path:"); 1138 response.PutStringAsRawHex8(matched_module_spec.GetFileSpec().GetPath()); 1139 response.PutChar(';'); 1140 response.PutCString("file_offset:"); 1141 response.PutHex64(file_offset); 1142 response.PutChar(';'); 1143 response.PutCString("file_size:"); 1144 response.PutHex64(file_size); 1145 response.PutChar(';'); 1146 1147 return SendPacketNoLock(response.GetString()); 1148 } 1149 1150 GDBRemoteCommunication::PacketResult 1151 GDBRemoteCommunicationServerCommon::Handle_jModulesInfo( 1152 StringExtractorGDBRemote &packet) { 1153 namespace json = llvm::json; 1154 1155 packet.SetFilePos(::strlen("jModulesInfo:")); 1156 1157 StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(packet.Peek()); 1158 if (!object_sp) 1159 return SendErrorResponse(1); 1160 1161 StructuredData::Array *packet_array = object_sp->GetAsArray(); 1162 if (!packet_array) 1163 return SendErrorResponse(2); 1164 1165 json::Array response_array; 1166 for (size_t i = 0; i < packet_array->GetSize(); ++i) { 1167 StructuredData::Dictionary *query = 1168 packet_array->GetItemAtIndex(i)->GetAsDictionary(); 1169 if (!query) 1170 continue; 1171 llvm::StringRef file, triple; 1172 if (!query->GetValueForKeyAsString("file", file) || 1173 !query->GetValueForKeyAsString("triple", triple)) 1174 continue; 1175 1176 ModuleSpec matched_module_spec = GetModuleInfo(file, triple); 1177 if (!matched_module_spec.GetFileSpec()) 1178 continue; 1179 1180 const auto file_offset = matched_module_spec.GetObjectOffset(); 1181 const auto file_size = matched_module_spec.GetObjectSize(); 1182 const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); 1183 if (uuid_str.empty()) 1184 continue; 1185 const auto triple_str = 1186 matched_module_spec.GetArchitecture().GetTriple().getTriple(); 1187 const auto file_path = matched_module_spec.GetFileSpec().GetPath(); 1188 1189 json::Object response{{"uuid", uuid_str}, 1190 {"triple", triple_str}, 1191 {"file_path", file_path}, 1192 {"file_offset", static_cast<int64_t>(file_offset)}, 1193 {"file_size", static_cast<int64_t>(file_size)}}; 1194 response_array.push_back(std::move(response)); 1195 } 1196 1197 StreamString response; 1198 response.AsRawOstream() << std::move(response_array); 1199 StreamGDBRemote escaped_response; 1200 escaped_response.PutEscapedBytes(response.GetString().data(), 1201 response.GetSize()); 1202 return SendPacketNoLock(escaped_response.GetString()); 1203 } 1204 1205 void GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse( 1206 const ProcessInstanceInfo &proc_info, StreamString &response) { 1207 response.Printf( 1208 "pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;", 1209 proc_info.GetProcessID(), proc_info.GetParentProcessID(), 1210 proc_info.GetUserID(), proc_info.GetGroupID(), 1211 proc_info.GetEffectiveUserID(), proc_info.GetEffectiveGroupID()); 1212 response.PutCString("name:"); 1213 response.PutStringAsRawHex8(proc_info.GetExecutableFile().GetPath()); 1214 1215 response.PutChar(';'); 1216 response.PutCString("args:"); 1217 response.PutStringAsRawHex8(proc_info.GetArg0()); 1218 for (auto &arg : proc_info.GetArguments()) { 1219 response.PutChar('-'); 1220 response.PutStringAsRawHex8(arg.ref()); 1221 } 1222 1223 response.PutChar(';'); 1224 const ArchSpec &proc_arch = proc_info.GetArchitecture(); 1225 if (proc_arch.IsValid()) { 1226 const llvm::Triple &proc_triple = proc_arch.GetTriple(); 1227 response.PutCString("triple:"); 1228 response.PutStringAsRawHex8(proc_triple.getTriple()); 1229 response.PutChar(';'); 1230 } 1231 } 1232 1233 void GDBRemoteCommunicationServerCommon:: 1234 CreateProcessInfoResponse_DebugServerStyle( 1235 const ProcessInstanceInfo &proc_info, StreamString &response) { 1236 response.Printf("pid:%" PRIx64 ";parent-pid:%" PRIx64 1237 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;", 1238 proc_info.GetProcessID(), proc_info.GetParentProcessID(), 1239 proc_info.GetUserID(), proc_info.GetGroupID(), 1240 proc_info.GetEffectiveUserID(), 1241 proc_info.GetEffectiveGroupID()); 1242 1243 const ArchSpec &proc_arch = proc_info.GetArchitecture(); 1244 if (proc_arch.IsValid()) { 1245 const llvm::Triple &proc_triple = proc_arch.GetTriple(); 1246 #if defined(__APPLE__) 1247 // We'll send cputype/cpusubtype. 1248 const uint32_t cpu_type = proc_arch.GetMachOCPUType(); 1249 if (cpu_type != 0) 1250 response.Printf("cputype:%" PRIx32 ";", cpu_type); 1251 1252 const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType(); 1253 if (cpu_subtype != 0) 1254 response.Printf("cpusubtype:%" PRIx32 ";", cpu_subtype); 1255 1256 const std::string vendor = proc_triple.getVendorName().str(); 1257 if (!vendor.empty()) 1258 response.Printf("vendor:%s;", vendor.c_str()); 1259 #else 1260 // We'll send the triple. 1261 response.PutCString("triple:"); 1262 response.PutStringAsRawHex8(proc_triple.getTriple()); 1263 response.PutChar(';'); 1264 #endif 1265 std::string ostype = std::string(proc_triple.getOSName()); 1266 // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64. 1267 if (proc_triple.getVendor() == llvm::Triple::Apple) { 1268 switch (proc_triple.getArch()) { 1269 case llvm::Triple::arm: 1270 case llvm::Triple::thumb: 1271 case llvm::Triple::aarch64: 1272 case llvm::Triple::aarch64_32: 1273 ostype = "ios"; 1274 break; 1275 default: 1276 // No change. 1277 break; 1278 } 1279 } 1280 response.Printf("ostype:%s;", ostype.c_str()); 1281 1282 switch (proc_arch.GetByteOrder()) { 1283 case lldb::eByteOrderLittle: 1284 response.PutCString("endian:little;"); 1285 break; 1286 case lldb::eByteOrderBig: 1287 response.PutCString("endian:big;"); 1288 break; 1289 case lldb::eByteOrderPDP: 1290 response.PutCString("endian:pdp;"); 1291 break; 1292 default: 1293 // Nothing. 1294 break; 1295 } 1296 // In case of MIPS64, pointer size is depend on ELF ABI For N32 the pointer 1297 // size is 4 and for N64 it is 8 1298 std::string abi = proc_arch.GetTargetABI(); 1299 if (!abi.empty()) 1300 response.Printf("elf_abi:%s;", abi.c_str()); 1301 response.Printf("ptrsize:%d;", proc_arch.GetAddressByteSize()); 1302 } 1303 } 1304 1305 FileSpec GDBRemoteCommunicationServerCommon::FindModuleFile( 1306 const std::string &module_path, const ArchSpec &arch) { 1307 #ifdef __ANDROID__ 1308 return HostInfoAndroid::ResolveLibraryPath(module_path, arch); 1309 #else 1310 FileSpec file_spec(module_path); 1311 FileSystem::Instance().Resolve(file_spec); 1312 return file_spec; 1313 #endif 1314 } 1315 1316 ModuleSpec 1317 GDBRemoteCommunicationServerCommon::GetModuleInfo(llvm::StringRef module_path, 1318 llvm::StringRef triple) { 1319 ArchSpec arch(triple); 1320 1321 FileSpec req_module_path_spec(module_path); 1322 FileSystem::Instance().Resolve(req_module_path_spec); 1323 1324 const FileSpec module_path_spec = 1325 FindModuleFile(req_module_path_spec.GetPath(), arch); 1326 const ModuleSpec module_spec(module_path_spec, arch); 1327 1328 ModuleSpecList module_specs; 1329 if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, 1330 module_specs)) 1331 return ModuleSpec(); 1332 1333 ModuleSpec matched_module_spec; 1334 if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) 1335 return ModuleSpec(); 1336 1337 return matched_module_spec; 1338 } 1339 1340 std::vector<std::string> GDBRemoteCommunicationServerCommon::HandleFeatures( 1341 const llvm::ArrayRef<llvm::StringRef> client_features) { 1342 // 128KBytes is a reasonable max packet size--debugger can always use less. 1343 constexpr uint32_t max_packet_size = 128 * 1024; 1344 1345 // Features common to platform server and llgs. 1346 return { 1347 llvm::formatv("PacketSize={0}", max_packet_size), 1348 "QStartNoAckMode+", 1349 "qEcho+", 1350 "native-signals+", 1351 }; 1352 } 1353