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