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 #endif 829 830 return SendPacketNoLock(response.GetString()); 831 } 832 833 GDBRemoteCommunication::PacketResult 834 GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported( 835 StringExtractorGDBRemote &packet) { 836 m_thread_suffix_supported = true; 837 return SendOKResponse(); 838 } 839 840 GDBRemoteCommunication::PacketResult 841 GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply( 842 StringExtractorGDBRemote &packet) { 843 m_list_threads_in_stop_reply = true; 844 return SendOKResponse(); 845 } 846 847 GDBRemoteCommunication::PacketResult 848 GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError( 849 StringExtractorGDBRemote &packet) { 850 packet.SetFilePos(::strlen("QSetDetachOnError:")); 851 if (packet.GetU32(0)) 852 m_process_launch_info.GetFlags().Set(eLaunchFlagDetachOnError); 853 else 854 m_process_launch_info.GetFlags().Clear(eLaunchFlagDetachOnError); 855 return SendOKResponse(); 856 } 857 858 GDBRemoteCommunication::PacketResult 859 GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode( 860 StringExtractorGDBRemote &packet) { 861 // Send response first before changing m_send_acks to we ack this packet 862 PacketResult packet_result = SendOKResponse(); 863 m_send_acks = false; 864 return packet_result; 865 } 866 867 GDBRemoteCommunication::PacketResult 868 GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN( 869 StringExtractorGDBRemote &packet) { 870 packet.SetFilePos(::strlen("QSetSTDIN:")); 871 FileAction file_action; 872 std::string path; 873 packet.GetHexByteString(path); 874 const bool read = true; 875 const bool write = false; 876 if (file_action.Open(STDIN_FILENO, FileSpec(path), read, write)) { 877 m_process_launch_info.AppendFileAction(file_action); 878 return SendOKResponse(); 879 } 880 return SendErrorResponse(15); 881 } 882 883 GDBRemoteCommunication::PacketResult 884 GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT( 885 StringExtractorGDBRemote &packet) { 886 packet.SetFilePos(::strlen("QSetSTDOUT:")); 887 FileAction file_action; 888 std::string path; 889 packet.GetHexByteString(path); 890 const bool read = false; 891 const bool write = true; 892 if (file_action.Open(STDOUT_FILENO, FileSpec(path), read, write)) { 893 m_process_launch_info.AppendFileAction(file_action); 894 return SendOKResponse(); 895 } 896 return SendErrorResponse(16); 897 } 898 899 GDBRemoteCommunication::PacketResult 900 GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR( 901 StringExtractorGDBRemote &packet) { 902 packet.SetFilePos(::strlen("QSetSTDERR:")); 903 FileAction file_action; 904 std::string path; 905 packet.GetHexByteString(path); 906 const bool read = false; 907 const bool write = true; 908 if (file_action.Open(STDERR_FILENO, FileSpec(path), read, write)) { 909 m_process_launch_info.AppendFileAction(file_action); 910 return SendOKResponse(); 911 } 912 return SendErrorResponse(17); 913 } 914 915 GDBRemoteCommunication::PacketResult 916 GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess( 917 StringExtractorGDBRemote &packet) { 918 if (m_process_launch_error.Success()) 919 return SendOKResponse(); 920 StreamString response; 921 response.PutChar('E'); 922 response.PutCString(m_process_launch_error.AsCString("<unknown error>")); 923 return SendPacketNoLock(response.GetString()); 924 } 925 926 GDBRemoteCommunication::PacketResult 927 GDBRemoteCommunicationServerCommon::Handle_QEnvironment( 928 StringExtractorGDBRemote &packet) { 929 packet.SetFilePos(::strlen("QEnvironment:")); 930 const uint32_t bytes_left = packet.GetBytesLeft(); 931 if (bytes_left > 0) { 932 m_process_launch_info.GetEnvironment().insert(packet.Peek()); 933 return SendOKResponse(); 934 } 935 return SendErrorResponse(12); 936 } 937 938 GDBRemoteCommunication::PacketResult 939 GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded( 940 StringExtractorGDBRemote &packet) { 941 packet.SetFilePos(::strlen("QEnvironmentHexEncoded:")); 942 const uint32_t bytes_left = packet.GetBytesLeft(); 943 if (bytes_left > 0) { 944 std::string str; 945 packet.GetHexByteString(str); 946 m_process_launch_info.GetEnvironment().insert(str); 947 return SendOKResponse(); 948 } 949 return SendErrorResponse(12); 950 } 951 952 GDBRemoteCommunication::PacketResult 953 GDBRemoteCommunicationServerCommon::Handle_QLaunchArch( 954 StringExtractorGDBRemote &packet) { 955 packet.SetFilePos(::strlen("QLaunchArch:")); 956 const uint32_t bytes_left = packet.GetBytesLeft(); 957 if (bytes_left > 0) { 958 const char *arch_triple = packet.Peek(); 959 m_process_launch_info.SetArchitecture( 960 HostInfo::GetAugmentedArchSpec(arch_triple)); 961 return SendOKResponse(); 962 } 963 return SendErrorResponse(13); 964 } 965 966 GDBRemoteCommunication::PacketResult 967 GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) { 968 // The 'A' packet is the most over designed packet ever here with redundant 969 // argument indexes, redundant argument lengths and needed hex encoded 970 // argument string values. Really all that is needed is a comma separated hex 971 // encoded argument value list, but we will stay true to the documented 972 // version of the 'A' packet here... 973 974 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 975 int actual_arg_index = 0; 976 977 packet.SetFilePos(1); // Skip the 'A' 978 bool success = true; 979 while (success && packet.GetBytesLeft() > 0) { 980 // Decode the decimal argument string length. This length is the number of 981 // hex nibbles in the argument string value. 982 const uint32_t arg_len = packet.GetU32(UINT32_MAX); 983 if (arg_len == UINT32_MAX) 984 success = false; 985 else { 986 // Make sure the argument hex string length is followed by a comma 987 if (packet.GetChar() != ',') 988 success = false; 989 else { 990 // Decode the argument index. We ignore this really because who would 991 // really send down the arguments in a random order??? 992 const uint32_t arg_idx = packet.GetU32(UINT32_MAX); 993 if (arg_idx == UINT32_MAX) 994 success = false; 995 else { 996 // Make sure the argument index is followed by a comma 997 if (packet.GetChar() != ',') 998 success = false; 999 else { 1000 // Decode the argument string value from hex bytes back into a UTF8 1001 // string and make sure the length matches the one supplied in the 1002 // packet 1003 std::string arg; 1004 if (packet.GetHexByteStringFixedLength(arg, arg_len) != 1005 (arg_len / 2)) 1006 success = false; 1007 else { 1008 // If there are any bytes left 1009 if (packet.GetBytesLeft()) { 1010 if (packet.GetChar() != ',') 1011 success = false; 1012 } 1013 1014 if (success) { 1015 if (arg_idx == 0) 1016 m_process_launch_info.GetExecutableFile().SetFile( 1017 arg, FileSpec::Style::native); 1018 m_process_launch_info.GetArguments().AppendArgument(arg); 1019 if (log) 1020 log->Printf("LLGSPacketHandler::%s added arg %d: \"%s\"", 1021 __FUNCTION__, actual_arg_index, arg.c_str()); 1022 ++actual_arg_index; 1023 } 1024 } 1025 } 1026 } 1027 } 1028 } 1029 } 1030 1031 if (success) { 1032 m_process_launch_error = LaunchProcess(); 1033 if (m_process_launch_error.Success()) 1034 return SendOKResponse(); 1035 LLDB_LOG(log, "failed to launch exe: {0}", m_process_launch_error); 1036 } 1037 return SendErrorResponse(8); 1038 } 1039 1040 GDBRemoteCommunication::PacketResult 1041 GDBRemoteCommunicationServerCommon::Handle_qEcho( 1042 StringExtractorGDBRemote &packet) { 1043 // Just echo back the exact same packet for qEcho... 1044 return SendPacketNoLock(packet.GetStringRef()); 1045 } 1046 1047 GDBRemoteCommunication::PacketResult 1048 GDBRemoteCommunicationServerCommon::Handle_qModuleInfo( 1049 StringExtractorGDBRemote &packet) { 1050 packet.SetFilePos(::strlen("qModuleInfo:")); 1051 1052 std::string module_path; 1053 packet.GetHexByteStringTerminatedBy(module_path, ';'); 1054 if (module_path.empty()) 1055 return SendErrorResponse(1); 1056 1057 if (packet.GetChar() != ';') 1058 return SendErrorResponse(2); 1059 1060 std::string triple; 1061 packet.GetHexByteString(triple); 1062 1063 ModuleSpec matched_module_spec = GetModuleInfo(module_path, triple); 1064 if (!matched_module_spec.GetFileSpec()) 1065 return SendErrorResponse(3); 1066 1067 const auto file_offset = matched_module_spec.GetObjectOffset(); 1068 const auto file_size = matched_module_spec.GetObjectSize(); 1069 const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); 1070 1071 StreamGDBRemote response; 1072 1073 if (uuid_str.empty()) { 1074 auto Result = llvm::sys::fs::md5_contents( 1075 matched_module_spec.GetFileSpec().GetPath()); 1076 if (!Result) 1077 return SendErrorResponse(5); 1078 response.PutCString("md5:"); 1079 response.PutStringAsRawHex8(Result->digest()); 1080 } else { 1081 response.PutCString("uuid:"); 1082 response.PutStringAsRawHex8(uuid_str); 1083 } 1084 response.PutChar(';'); 1085 1086 const auto &module_arch = matched_module_spec.GetArchitecture(); 1087 response.PutCString("triple:"); 1088 response.PutStringAsRawHex8(module_arch.GetTriple().getTriple()); 1089 response.PutChar(';'); 1090 1091 response.PutCString("file_path:"); 1092 response.PutStringAsRawHex8(matched_module_spec.GetFileSpec().GetCString()); 1093 response.PutChar(';'); 1094 response.PutCString("file_offset:"); 1095 response.PutHex64(file_offset); 1096 response.PutChar(';'); 1097 response.PutCString("file_size:"); 1098 response.PutHex64(file_size); 1099 response.PutChar(';'); 1100 1101 return SendPacketNoLock(response.GetString()); 1102 } 1103 1104 GDBRemoteCommunication::PacketResult 1105 GDBRemoteCommunicationServerCommon::Handle_jModulesInfo( 1106 StringExtractorGDBRemote &packet) { 1107 packet.SetFilePos(::strlen("jModulesInfo:")); 1108 1109 StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(packet.Peek()); 1110 if (!object_sp) 1111 return SendErrorResponse(1); 1112 1113 StructuredData::Array *packet_array = object_sp->GetAsArray(); 1114 if (!packet_array) 1115 return SendErrorResponse(2); 1116 1117 JSONArray::SP response_array_sp = std::make_shared<JSONArray>(); 1118 for (size_t i = 0; i < packet_array->GetSize(); ++i) { 1119 StructuredData::Dictionary *query = 1120 packet_array->GetItemAtIndex(i)->GetAsDictionary(); 1121 if (!query) 1122 continue; 1123 llvm::StringRef file, triple; 1124 if (!query->GetValueForKeyAsString("file", file) || 1125 !query->GetValueForKeyAsString("triple", triple)) 1126 continue; 1127 1128 ModuleSpec matched_module_spec = GetModuleInfo(file, triple); 1129 if (!matched_module_spec.GetFileSpec()) 1130 continue; 1131 1132 const auto file_offset = matched_module_spec.GetObjectOffset(); 1133 const auto file_size = matched_module_spec.GetObjectSize(); 1134 const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); 1135 1136 if (uuid_str.empty()) 1137 continue; 1138 1139 JSONObject::SP response = std::make_shared<JSONObject>(); 1140 response_array_sp->AppendObject(response); 1141 response->SetObject("uuid", std::make_shared<JSONString>(uuid_str)); 1142 response->SetObject( 1143 "triple", 1144 std::make_shared<JSONString>( 1145 matched_module_spec.GetArchitecture().GetTriple().getTriple())); 1146 response->SetObject("file_path", 1147 std::make_shared<JSONString>( 1148 matched_module_spec.GetFileSpec().GetPath())); 1149 response->SetObject("file_offset", 1150 std::make_shared<JSONNumber>(file_offset)); 1151 response->SetObject("file_size", std::make_shared<JSONNumber>(file_size)); 1152 } 1153 1154 StreamString response; 1155 response_array_sp->Write(response); 1156 StreamGDBRemote escaped_response; 1157 escaped_response.PutEscapedBytes(response.GetString().data(), 1158 response.GetSize()); 1159 return SendPacketNoLock(escaped_response.GetString()); 1160 } 1161 1162 void GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse( 1163 const ProcessInstanceInfo &proc_info, StreamString &response) { 1164 response.Printf( 1165 "pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;", 1166 proc_info.GetProcessID(), proc_info.GetParentProcessID(), 1167 proc_info.GetUserID(), proc_info.GetGroupID(), 1168 proc_info.GetEffectiveUserID(), proc_info.GetEffectiveGroupID()); 1169 response.PutCString("name:"); 1170 response.PutStringAsRawHex8(proc_info.GetExecutableFile().GetCString()); 1171 response.PutChar(';'); 1172 const ArchSpec &proc_arch = proc_info.GetArchitecture(); 1173 if (proc_arch.IsValid()) { 1174 const llvm::Triple &proc_triple = proc_arch.GetTriple(); 1175 response.PutCString("triple:"); 1176 response.PutStringAsRawHex8(proc_triple.getTriple()); 1177 response.PutChar(';'); 1178 } 1179 } 1180 1181 void GDBRemoteCommunicationServerCommon:: 1182 CreateProcessInfoResponse_DebugServerStyle( 1183 const ProcessInstanceInfo &proc_info, StreamString &response) { 1184 response.Printf("pid:%" PRIx64 ";parent-pid:%" PRIx64 1185 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;", 1186 proc_info.GetProcessID(), proc_info.GetParentProcessID(), 1187 proc_info.GetUserID(), proc_info.GetGroupID(), 1188 proc_info.GetEffectiveUserID(), 1189 proc_info.GetEffectiveGroupID()); 1190 1191 const ArchSpec &proc_arch = proc_info.GetArchitecture(); 1192 if (proc_arch.IsValid()) { 1193 const llvm::Triple &proc_triple = proc_arch.GetTriple(); 1194 #if defined(__APPLE__) 1195 // We'll send cputype/cpusubtype. 1196 const uint32_t cpu_type = proc_arch.GetMachOCPUType(); 1197 if (cpu_type != 0) 1198 response.Printf("cputype:%" PRIx32 ";", cpu_type); 1199 1200 const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType(); 1201 if (cpu_subtype != 0) 1202 response.Printf("cpusubtype:%" PRIx32 ";", cpu_subtype); 1203 1204 const std::string vendor = proc_triple.getVendorName(); 1205 if (!vendor.empty()) 1206 response.Printf("vendor:%s;", vendor.c_str()); 1207 #else 1208 // We'll send the triple. 1209 response.PutCString("triple:"); 1210 response.PutStringAsRawHex8(proc_triple.getTriple()); 1211 response.PutChar(';'); 1212 #endif 1213 std::string ostype = proc_triple.getOSName(); 1214 // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64. 1215 if (proc_triple.getVendor() == llvm::Triple::Apple) { 1216 switch (proc_triple.getArch()) { 1217 case llvm::Triple::arm: 1218 case llvm::Triple::thumb: 1219 case llvm::Triple::aarch64: 1220 ostype = "ios"; 1221 break; 1222 default: 1223 // No change. 1224 break; 1225 } 1226 } 1227 response.Printf("ostype:%s;", ostype.c_str()); 1228 1229 switch (proc_arch.GetByteOrder()) { 1230 case lldb::eByteOrderLittle: 1231 response.PutCString("endian:little;"); 1232 break; 1233 case lldb::eByteOrderBig: 1234 response.PutCString("endian:big;"); 1235 break; 1236 case lldb::eByteOrderPDP: 1237 response.PutCString("endian:pdp;"); 1238 break; 1239 default: 1240 // Nothing. 1241 break; 1242 } 1243 // In case of MIPS64, pointer size is depend on ELF ABI For N32 the pointer 1244 // size is 4 and for N64 it is 8 1245 std::string abi = proc_arch.GetTargetABI(); 1246 if (!abi.empty()) 1247 response.Printf("elf_abi:%s;", abi.c_str()); 1248 response.Printf("ptrsize:%d;", proc_arch.GetAddressByteSize()); 1249 } 1250 } 1251 1252 FileSpec GDBRemoteCommunicationServerCommon::FindModuleFile( 1253 const std::string &module_path, const ArchSpec &arch) { 1254 #ifdef __ANDROID__ 1255 return HostInfoAndroid::ResolveLibraryPath(module_path, arch); 1256 #else 1257 FileSpec file_spec(module_path); 1258 FileSystem::Instance().Resolve(file_spec); 1259 return file_spec; 1260 #endif 1261 } 1262 1263 ModuleSpec 1264 GDBRemoteCommunicationServerCommon::GetModuleInfo(llvm::StringRef module_path, 1265 llvm::StringRef triple) { 1266 ArchSpec arch(triple); 1267 1268 FileSpec req_module_path_spec(module_path); 1269 FileSystem::Instance().Resolve(req_module_path_spec); 1270 1271 const FileSpec module_path_spec = 1272 FindModuleFile(req_module_path_spec.GetPath(), arch); 1273 const ModuleSpec module_spec(module_path_spec, arch); 1274 1275 ModuleSpecList module_specs; 1276 if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, 1277 module_specs)) 1278 return ModuleSpec(); 1279 1280 ModuleSpec matched_module_spec; 1281 if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) 1282 return ModuleSpec(); 1283 1284 return matched_module_spec; 1285 } 1286