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