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/Log.h" 26 #include "lldb/Core/ModuleSpec.h" 27 #include "lldb/Core/StreamGDBRemote.h" 28 #include "lldb/Host/Config.h" 29 #include "lldb/Host/File.h" 30 #include "lldb/Host/FileSystem.h" 31 #include "lldb/Host/Host.h" 32 #include "lldb/Host/HostInfo.h" 33 #include "lldb/Host/StringConvert.h" 34 #include "lldb/Interpreter/Args.h" 35 #include "lldb/Symbol/ObjectFile.h" 36 #include "lldb/Target/FileAction.h" 37 #include "lldb/Target/Platform.h" 38 #include "lldb/Target/Process.h" 39 #include "lldb/Utility/Endian.h" 40 #include "lldb/Utility/JSON.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 lldb::user_id_t retcode = FileSystem::GetFileSize(FileSpec(path, false)); 646 StreamString response; 647 response.PutChar('F'); 648 response.PutHex64(retcode); 649 if (retcode == UINT64_MAX) { 650 response.PutChar(','); 651 response.PutHex64( 652 retcode); // TODO: replace with Host::GetSyswideErrorCode() 653 } 654 return SendPacketNoLock(response.GetString()); 655 } 656 return SendErrorResponse(22); 657 } 658 659 GDBRemoteCommunication::PacketResult 660 GDBRemoteCommunicationServerCommon::Handle_vFile_Mode( 661 StringExtractorGDBRemote &packet) { 662 packet.SetFilePos(::strlen("vFile:mode:")); 663 std::string path; 664 packet.GetHexByteString(path); 665 if (!path.empty()) { 666 Error error; 667 const uint32_t mode = File::GetPermissions(FileSpec{path, true}, error); 668 StreamString response; 669 response.Printf("F%u", mode); 670 if (mode == 0 || error.Fail()) 671 response.Printf(",%i", (int)error.GetError()); 672 return SendPacketNoLock(response.GetString()); 673 } 674 return SendErrorResponse(23); 675 } 676 677 GDBRemoteCommunication::PacketResult 678 GDBRemoteCommunicationServerCommon::Handle_vFile_Exists( 679 StringExtractorGDBRemote &packet) { 680 packet.SetFilePos(::strlen("vFile:exists:")); 681 std::string path; 682 packet.GetHexByteString(path); 683 if (!path.empty()) { 684 bool retcode = FileSystem::GetFileExists(FileSpec(path, false)); 685 StreamString response; 686 response.PutChar('F'); 687 response.PutChar(','); 688 if (retcode) 689 response.PutChar('1'); 690 else 691 response.PutChar('0'); 692 return SendPacketNoLock(response.GetString()); 693 } 694 return SendErrorResponse(24); 695 } 696 697 GDBRemoteCommunication::PacketResult 698 GDBRemoteCommunicationServerCommon::Handle_vFile_symlink( 699 StringExtractorGDBRemote &packet) { 700 packet.SetFilePos(::strlen("vFile:symlink:")); 701 std::string dst, src; 702 packet.GetHexByteStringTerminatedBy(dst, ','); 703 packet.GetChar(); // Skip ',' char 704 packet.GetHexByteString(src); 705 Error error = FileSystem::Symlink(FileSpec{src, true}, FileSpec{dst, false}); 706 StreamString response; 707 response.Printf("F%u,%u", error.GetError(), error.GetError()); 708 return SendPacketNoLock(response.GetString()); 709 } 710 711 GDBRemoteCommunication::PacketResult 712 GDBRemoteCommunicationServerCommon::Handle_vFile_unlink( 713 StringExtractorGDBRemote &packet) { 714 packet.SetFilePos(::strlen("vFile:unlink:")); 715 std::string path; 716 packet.GetHexByteString(path); 717 Error error = FileSystem::Unlink(FileSpec{path, true}); 718 StreamString response; 719 response.Printf("F%u,%u", error.GetError(), error.GetError()); 720 return SendPacketNoLock(response.GetString()); 721 } 722 723 GDBRemoteCommunication::PacketResult 724 GDBRemoteCommunicationServerCommon::Handle_qPlatform_shell( 725 StringExtractorGDBRemote &packet) { 726 packet.SetFilePos(::strlen("qPlatform_shell:")); 727 std::string path; 728 std::string working_dir; 729 packet.GetHexByteStringTerminatedBy(path, ','); 730 if (!path.empty()) { 731 if (packet.GetChar() == ',') { 732 // FIXME: add timeout to qPlatform_shell packet 733 // uint32_t timeout = packet.GetHexMaxU32(false, 32); 734 uint32_t timeout = 10; 735 if (packet.GetChar() == ',') 736 packet.GetHexByteString(working_dir); 737 int status, signo; 738 std::string output; 739 Error err = 740 Host::RunShellCommand(path.c_str(), FileSpec{working_dir, true}, 741 &status, &signo, &output, timeout); 742 StreamGDBRemote response; 743 if (err.Fail()) { 744 response.PutCString("F,"); 745 response.PutHex32(UINT32_MAX); 746 } else { 747 response.PutCString("F,"); 748 response.PutHex32(status); 749 response.PutChar(','); 750 response.PutHex32(signo); 751 response.PutChar(','); 752 response.PutEscapedBytes(output.c_str(), output.size()); 753 } 754 return SendPacketNoLock(response.GetString()); 755 } 756 } 757 return SendErrorResponse(24); 758 } 759 760 GDBRemoteCommunication::PacketResult 761 GDBRemoteCommunicationServerCommon::Handle_vFile_Stat( 762 StringExtractorGDBRemote &packet) { 763 return SendUnimplementedResponse( 764 "GDBRemoteCommunicationServerCommon::Handle_vFile_Stat() unimplemented"); 765 } 766 767 GDBRemoteCommunication::PacketResult 768 GDBRemoteCommunicationServerCommon::Handle_vFile_MD5( 769 StringExtractorGDBRemote &packet) { 770 packet.SetFilePos(::strlen("vFile:MD5:")); 771 std::string path; 772 packet.GetHexByteString(path); 773 if (!path.empty()) { 774 uint64_t a, b; 775 StreamGDBRemote response; 776 if (!FileSystem::CalculateMD5(FileSpec(path, false), a, b)) { 777 response.PutCString("F,"); 778 response.PutCString("x"); 779 } else { 780 response.PutCString("F,"); 781 response.PutHex64(a); 782 response.PutHex64(b); 783 } 784 return SendPacketNoLock(response.GetString()); 785 } 786 return SendErrorResponse(25); 787 } 788 789 GDBRemoteCommunication::PacketResult 790 GDBRemoteCommunicationServerCommon::Handle_qPlatform_mkdir( 791 StringExtractorGDBRemote &packet) { 792 packet.SetFilePos(::strlen("qPlatform_mkdir:")); 793 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX); 794 if (packet.GetChar() == ',') { 795 std::string path; 796 packet.GetHexByteString(path); 797 Error error = FileSystem::MakeDirectory(FileSpec{path, false}, mode); 798 799 StreamGDBRemote response; 800 response.Printf("F%u", error.GetError()); 801 802 return SendPacketNoLock(response.GetString()); 803 } 804 return SendErrorResponse(20); 805 } 806 807 GDBRemoteCommunication::PacketResult 808 GDBRemoteCommunicationServerCommon::Handle_qPlatform_chmod( 809 StringExtractorGDBRemote &packet) { 810 packet.SetFilePos(::strlen("qPlatform_chmod:")); 811 812 mode_t mode = packet.GetHexMaxU32(false, UINT32_MAX); 813 if (packet.GetChar() == ',') { 814 std::string path; 815 packet.GetHexByteString(path); 816 Error error = FileSystem::SetFilePermissions(FileSpec{path, true}, mode); 817 818 StreamGDBRemote response; 819 response.Printf("F%u", error.GetError()); 820 821 return SendPacketNoLock(response.GetString()); 822 } 823 return SendErrorResponse(19); 824 } 825 826 GDBRemoteCommunication::PacketResult 827 GDBRemoteCommunicationServerCommon::Handle_qSupported( 828 StringExtractorGDBRemote &packet) { 829 StreamGDBRemote response; 830 831 // Features common to lldb-platform and llgs. 832 uint32_t max_packet_size = 128 * 1024; // 128KBytes is a reasonable max packet 833 // size--debugger can always use less 834 response.Printf("PacketSize=%x", max_packet_size); 835 836 response.PutCString(";QStartNoAckMode+"); 837 response.PutCString(";QThreadSuffixSupported+"); 838 response.PutCString(";QListThreadsInStopReply+"); 839 response.PutCString(";qEcho+"); 840 #if defined(__linux__) 841 response.PutCString(";qXfer:auxv:read+"); 842 #endif 843 844 return SendPacketNoLock(response.GetString()); 845 } 846 847 GDBRemoteCommunication::PacketResult 848 GDBRemoteCommunicationServerCommon::Handle_QThreadSuffixSupported( 849 StringExtractorGDBRemote &packet) { 850 m_thread_suffix_supported = true; 851 return SendOKResponse(); 852 } 853 854 GDBRemoteCommunication::PacketResult 855 GDBRemoteCommunicationServerCommon::Handle_QListThreadsInStopReply( 856 StringExtractorGDBRemote &packet) { 857 m_list_threads_in_stop_reply = true; 858 return SendOKResponse(); 859 } 860 861 GDBRemoteCommunication::PacketResult 862 GDBRemoteCommunicationServerCommon::Handle_QSetDetachOnError( 863 StringExtractorGDBRemote &packet) { 864 packet.SetFilePos(::strlen("QSetDetachOnError:")); 865 if (packet.GetU32(0)) 866 m_process_launch_info.GetFlags().Set(eLaunchFlagDetachOnError); 867 else 868 m_process_launch_info.GetFlags().Clear(eLaunchFlagDetachOnError); 869 return SendOKResponse(); 870 } 871 872 GDBRemoteCommunication::PacketResult 873 GDBRemoteCommunicationServerCommon::Handle_QStartNoAckMode( 874 StringExtractorGDBRemote &packet) { 875 // Send response first before changing m_send_acks to we ack this packet 876 PacketResult packet_result = SendOKResponse(); 877 m_send_acks = false; 878 return packet_result; 879 } 880 881 GDBRemoteCommunication::PacketResult 882 GDBRemoteCommunicationServerCommon::Handle_QSetSTDIN( 883 StringExtractorGDBRemote &packet) { 884 packet.SetFilePos(::strlen("QSetSTDIN:")); 885 FileAction file_action; 886 std::string path; 887 packet.GetHexByteString(path); 888 const bool read = true; 889 const bool write = false; 890 if (file_action.Open(STDIN_FILENO, FileSpec{path, false}, read, write)) { 891 m_process_launch_info.AppendFileAction(file_action); 892 return SendOKResponse(); 893 } 894 return SendErrorResponse(15); 895 } 896 897 GDBRemoteCommunication::PacketResult 898 GDBRemoteCommunicationServerCommon::Handle_QSetSTDOUT( 899 StringExtractorGDBRemote &packet) { 900 packet.SetFilePos(::strlen("QSetSTDOUT:")); 901 FileAction file_action; 902 std::string path; 903 packet.GetHexByteString(path); 904 const bool read = false; 905 const bool write = true; 906 if (file_action.Open(STDOUT_FILENO, FileSpec{path, false}, read, write)) { 907 m_process_launch_info.AppendFileAction(file_action); 908 return SendOKResponse(); 909 } 910 return SendErrorResponse(16); 911 } 912 913 GDBRemoteCommunication::PacketResult 914 GDBRemoteCommunicationServerCommon::Handle_QSetSTDERR( 915 StringExtractorGDBRemote &packet) { 916 packet.SetFilePos(::strlen("QSetSTDERR:")); 917 FileAction file_action; 918 std::string path; 919 packet.GetHexByteString(path); 920 const bool read = false; 921 const bool write = true; 922 if (file_action.Open(STDERR_FILENO, FileSpec{path, false}, read, write)) { 923 m_process_launch_info.AppendFileAction(file_action); 924 return SendOKResponse(); 925 } 926 return SendErrorResponse(17); 927 } 928 929 GDBRemoteCommunication::PacketResult 930 GDBRemoteCommunicationServerCommon::Handle_qLaunchSuccess( 931 StringExtractorGDBRemote &packet) { 932 if (m_process_launch_error.Success()) 933 return SendOKResponse(); 934 StreamString response; 935 response.PutChar('E'); 936 response.PutCString(m_process_launch_error.AsCString("<unknown error>")); 937 return SendPacketNoLock(response.GetString()); 938 } 939 940 GDBRemoteCommunication::PacketResult 941 GDBRemoteCommunicationServerCommon::Handle_QEnvironment( 942 StringExtractorGDBRemote &packet) { 943 packet.SetFilePos(::strlen("QEnvironment:")); 944 const uint32_t bytes_left = packet.GetBytesLeft(); 945 if (bytes_left > 0) { 946 m_process_launch_info.GetEnvironmentEntries().AppendArgument( 947 llvm::StringRef::withNullAsEmpty(packet.Peek())); 948 return SendOKResponse(); 949 } 950 return SendErrorResponse(12); 951 } 952 953 GDBRemoteCommunication::PacketResult 954 GDBRemoteCommunicationServerCommon::Handle_QEnvironmentHexEncoded( 955 StringExtractorGDBRemote &packet) { 956 packet.SetFilePos(::strlen("QEnvironmentHexEncoded:")); 957 const uint32_t bytes_left = packet.GetBytesLeft(); 958 if (bytes_left > 0) { 959 std::string str; 960 packet.GetHexByteString(str); 961 m_process_launch_info.GetEnvironmentEntries().AppendArgument(str); 962 return SendOKResponse(); 963 } 964 return SendErrorResponse(12); 965 } 966 967 GDBRemoteCommunication::PacketResult 968 GDBRemoteCommunicationServerCommon::Handle_QLaunchArch( 969 StringExtractorGDBRemote &packet) { 970 packet.SetFilePos(::strlen("QLaunchArch:")); 971 const uint32_t bytes_left = packet.GetBytesLeft(); 972 if (bytes_left > 0) { 973 const char *arch_triple = packet.Peek(); 974 ArchSpec arch_spec(arch_triple, NULL); 975 m_process_launch_info.SetArchitecture(arch_spec); 976 return SendOKResponse(); 977 } 978 return SendErrorResponse(13); 979 } 980 981 GDBRemoteCommunication::PacketResult 982 GDBRemoteCommunicationServerCommon::Handle_A(StringExtractorGDBRemote &packet) { 983 // The 'A' packet is the most over designed packet ever here with 984 // redundant argument indexes, redundant argument lengths and needed hex 985 // encoded argument string values. Really all that is needed is a comma 986 // separated hex encoded argument value list, but we will stay true to the 987 // documented version of the 'A' packet here... 988 989 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 990 int actual_arg_index = 0; 991 992 packet.SetFilePos(1); // Skip the 'A' 993 bool success = true; 994 while (success && packet.GetBytesLeft() > 0) { 995 // Decode the decimal argument string length. This length is the 996 // number of hex nibbles in the argument string value. 997 const uint32_t arg_len = packet.GetU32(UINT32_MAX); 998 if (arg_len == UINT32_MAX) 999 success = false; 1000 else { 1001 // Make sure the argument hex string length is followed by a comma 1002 if (packet.GetChar() != ',') 1003 success = false; 1004 else { 1005 // Decode the argument index. We ignore this really because 1006 // who would really send down the arguments in a random order??? 1007 const uint32_t arg_idx = packet.GetU32(UINT32_MAX); 1008 if (arg_idx == UINT32_MAX) 1009 success = false; 1010 else { 1011 // Make sure the argument index is followed by a comma 1012 if (packet.GetChar() != ',') 1013 success = false; 1014 else { 1015 // Decode the argument string value from hex bytes 1016 // back into a UTF8 string and make sure the length 1017 // matches the one supplied in the packet 1018 std::string arg; 1019 if (packet.GetHexByteStringFixedLength(arg, arg_len) != 1020 (arg_len / 2)) 1021 success = false; 1022 else { 1023 // If there are any bytes left 1024 if (packet.GetBytesLeft()) { 1025 if (packet.GetChar() != ',') 1026 success = false; 1027 } 1028 1029 if (success) { 1030 if (arg_idx == 0) 1031 m_process_launch_info.GetExecutableFile().SetFile(arg, false); 1032 m_process_launch_info.GetArguments().AppendArgument(arg); 1033 if (log) 1034 log->Printf("LLGSPacketHandler::%s added arg %d: \"%s\"", 1035 __FUNCTION__, actual_arg_index, arg.c_str()); 1036 ++actual_arg_index; 1037 } 1038 } 1039 } 1040 } 1041 } 1042 } 1043 } 1044 1045 if (success) { 1046 m_process_launch_error = LaunchProcess(); 1047 if (m_process_launch_info.GetProcessID() != LLDB_INVALID_PROCESS_ID) { 1048 return SendOKResponse(); 1049 } else { 1050 Log *log(GetLogIfAnyCategoriesSet(LIBLLDB_LOG_PROCESS)); 1051 if (log) 1052 log->Printf("LLGSPacketHandler::%s failed to launch exe: %s", 1053 __FUNCTION__, m_process_launch_error.AsCString()); 1054 } 1055 } 1056 return SendErrorResponse(8); 1057 } 1058 1059 GDBRemoteCommunication::PacketResult 1060 GDBRemoteCommunicationServerCommon::Handle_qEcho( 1061 StringExtractorGDBRemote &packet) { 1062 // Just echo back the exact same packet for qEcho... 1063 return SendPacketNoLock(packet.GetStringRef()); 1064 } 1065 1066 GDBRemoteCommunication::PacketResult 1067 GDBRemoteCommunicationServerCommon::Handle_qModuleInfo( 1068 StringExtractorGDBRemote &packet) { 1069 packet.SetFilePos(::strlen("qModuleInfo:")); 1070 1071 std::string module_path; 1072 packet.GetHexByteStringTerminatedBy(module_path, ';'); 1073 if (module_path.empty()) 1074 return SendErrorResponse(1); 1075 1076 if (packet.GetChar() != ';') 1077 return SendErrorResponse(2); 1078 1079 std::string triple; 1080 packet.GetHexByteString(triple); 1081 1082 ModuleSpec matched_module_spec = GetModuleInfo(module_path, triple); 1083 if (!matched_module_spec.GetFileSpec()) 1084 return SendErrorResponse(3); 1085 1086 const auto file_offset = matched_module_spec.GetObjectOffset(); 1087 const auto file_size = matched_module_spec.GetObjectSize(); 1088 const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); 1089 1090 StreamGDBRemote response; 1091 1092 if (uuid_str.empty()) { 1093 std::string md5_hash; 1094 if (!FileSystem::CalculateMD5AsString(matched_module_spec.GetFileSpec(), 1095 file_offset, file_size, md5_hash)) 1096 return SendErrorResponse(5); 1097 response.PutCString("md5:"); 1098 response.PutCStringAsRawHex8(md5_hash.c_str()); 1099 } else { 1100 response.PutCString("uuid:"); 1101 response.PutCStringAsRawHex8(uuid_str.c_str()); 1102 } 1103 response.PutChar(';'); 1104 1105 const auto &module_arch = matched_module_spec.GetArchitecture(); 1106 response.PutCString("triple:"); 1107 response.PutCStringAsRawHex8(module_arch.GetTriple().getTriple().c_str()); 1108 response.PutChar(';'); 1109 1110 response.PutCString("file_path:"); 1111 response.PutCStringAsRawHex8(matched_module_spec.GetFileSpec().GetCString()); 1112 response.PutChar(';'); 1113 response.PutCString("file_offset:"); 1114 response.PutHex64(file_offset); 1115 response.PutChar(';'); 1116 response.PutCString("file_size:"); 1117 response.PutHex64(file_size); 1118 response.PutChar(';'); 1119 1120 return SendPacketNoLock(response.GetString()); 1121 } 1122 1123 GDBRemoteCommunication::PacketResult 1124 GDBRemoteCommunicationServerCommon::Handle_jModulesInfo( 1125 StringExtractorGDBRemote &packet) { 1126 packet.SetFilePos(::strlen("jModulesInfo:")); 1127 1128 StructuredData::ObjectSP object_sp = StructuredData::ParseJSON(packet.Peek()); 1129 if (!object_sp) 1130 return SendErrorResponse(1); 1131 1132 StructuredData::Array *packet_array = object_sp->GetAsArray(); 1133 if (!packet_array) 1134 return SendErrorResponse(2); 1135 1136 JSONArray::SP response_array_sp = std::make_shared<JSONArray>(); 1137 for (size_t i = 0; i < packet_array->GetSize(); ++i) { 1138 StructuredData::Dictionary *query = 1139 packet_array->GetItemAtIndex(i)->GetAsDictionary(); 1140 if (!query) 1141 continue; 1142 std::string file, triple; 1143 if (!query->GetValueForKeyAsString("file", file) || 1144 !query->GetValueForKeyAsString("triple", triple)) 1145 continue; 1146 1147 ModuleSpec matched_module_spec = GetModuleInfo(file, triple); 1148 if (!matched_module_spec.GetFileSpec()) 1149 continue; 1150 1151 const auto file_offset = matched_module_spec.GetObjectOffset(); 1152 const auto file_size = matched_module_spec.GetObjectSize(); 1153 const auto uuid_str = matched_module_spec.GetUUID().GetAsString(""); 1154 1155 if (uuid_str.empty()) 1156 continue; 1157 1158 JSONObject::SP response = std::make_shared<JSONObject>(); 1159 response_array_sp->AppendObject(response); 1160 response->SetObject("uuid", std::make_shared<JSONString>(uuid_str)); 1161 response->SetObject( 1162 "triple", 1163 std::make_shared<JSONString>( 1164 matched_module_spec.GetArchitecture().GetTriple().getTriple())); 1165 response->SetObject("file_path", 1166 std::make_shared<JSONString>( 1167 matched_module_spec.GetFileSpec().GetPath())); 1168 response->SetObject("file_offset", 1169 std::make_shared<JSONNumber>(file_offset)); 1170 response->SetObject("file_size", std::make_shared<JSONNumber>(file_size)); 1171 } 1172 1173 StreamString response; 1174 response_array_sp->Write(response); 1175 StreamGDBRemote escaped_response; 1176 escaped_response.PutEscapedBytes(response.GetString().data(), 1177 response.GetSize()); 1178 return SendPacketNoLock(escaped_response.GetString()); 1179 } 1180 1181 void GDBRemoteCommunicationServerCommon::CreateProcessInfoResponse( 1182 const ProcessInstanceInfo &proc_info, StreamString &response) { 1183 response.Printf( 1184 "pid:%" PRIu64 ";ppid:%" PRIu64 ";uid:%i;gid:%i;euid:%i;egid:%i;", 1185 proc_info.GetProcessID(), proc_info.GetParentProcessID(), 1186 proc_info.GetUserID(), proc_info.GetGroupID(), 1187 proc_info.GetEffectiveUserID(), proc_info.GetEffectiveGroupID()); 1188 response.PutCString("name:"); 1189 response.PutCStringAsRawHex8(proc_info.GetExecutableFile().GetCString()); 1190 response.PutChar(';'); 1191 const ArchSpec &proc_arch = proc_info.GetArchitecture(); 1192 if (proc_arch.IsValid()) { 1193 const llvm::Triple &proc_triple = proc_arch.GetTriple(); 1194 response.PutCString("triple:"); 1195 response.PutCStringAsRawHex8(proc_triple.getTriple().c_str()); 1196 response.PutChar(';'); 1197 } 1198 } 1199 1200 void GDBRemoteCommunicationServerCommon:: 1201 CreateProcessInfoResponse_DebugServerStyle( 1202 const ProcessInstanceInfo &proc_info, StreamString &response) { 1203 response.Printf("pid:%" PRIx64 ";parent-pid:%" PRIx64 1204 ";real-uid:%x;real-gid:%x;effective-uid:%x;effective-gid:%x;", 1205 proc_info.GetProcessID(), proc_info.GetParentProcessID(), 1206 proc_info.GetUserID(), proc_info.GetGroupID(), 1207 proc_info.GetEffectiveUserID(), 1208 proc_info.GetEffectiveGroupID()); 1209 1210 const ArchSpec &proc_arch = proc_info.GetArchitecture(); 1211 if (proc_arch.IsValid()) { 1212 const llvm::Triple &proc_triple = proc_arch.GetTriple(); 1213 #if defined(__APPLE__) 1214 // We'll send cputype/cpusubtype. 1215 const uint32_t cpu_type = proc_arch.GetMachOCPUType(); 1216 if (cpu_type != 0) 1217 response.Printf("cputype:%" PRIx32 ";", cpu_type); 1218 1219 const uint32_t cpu_subtype = proc_arch.GetMachOCPUSubType(); 1220 if (cpu_subtype != 0) 1221 response.Printf("cpusubtype:%" PRIx32 ";", cpu_subtype); 1222 1223 const std::string vendor = proc_triple.getVendorName(); 1224 if (!vendor.empty()) 1225 response.Printf("vendor:%s;", vendor.c_str()); 1226 #else 1227 // We'll send the triple. 1228 response.PutCString("triple:"); 1229 response.PutCStringAsRawHex8(proc_triple.getTriple().c_str()); 1230 response.PutChar(';'); 1231 #endif 1232 std::string ostype = proc_triple.getOSName(); 1233 // Adjust so ostype reports ios for Apple/ARM and Apple/ARM64. 1234 if (proc_triple.getVendor() == llvm::Triple::Apple) { 1235 switch (proc_triple.getArch()) { 1236 case llvm::Triple::arm: 1237 case llvm::Triple::thumb: 1238 case llvm::Triple::aarch64: 1239 ostype = "ios"; 1240 break; 1241 default: 1242 // No change. 1243 break; 1244 } 1245 } 1246 response.Printf("ostype:%s;", ostype.c_str()); 1247 1248 switch (proc_arch.GetByteOrder()) { 1249 case lldb::eByteOrderLittle: 1250 response.PutCString("endian:little;"); 1251 break; 1252 case lldb::eByteOrderBig: 1253 response.PutCString("endian:big;"); 1254 break; 1255 case lldb::eByteOrderPDP: 1256 response.PutCString("endian:pdp;"); 1257 break; 1258 default: 1259 // Nothing. 1260 break; 1261 } 1262 // In case of MIPS64, pointer size is depend on ELF ABI 1263 // For N32 the pointer size is 4 and for N64 it is 8 1264 std::string abi = proc_arch.GetTargetABI(); 1265 if (!abi.empty()) 1266 response.Printf("elf_abi:%s;", abi.c_str()); 1267 response.Printf("ptrsize:%d;", proc_arch.GetAddressByteSize()); 1268 } 1269 } 1270 1271 FileSpec GDBRemoteCommunicationServerCommon::FindModuleFile( 1272 const std::string &module_path, const ArchSpec &arch) { 1273 #ifdef __ANDROID__ 1274 return HostInfoAndroid::ResolveLibraryPath(module_path, arch); 1275 #else 1276 return FileSpec(module_path, true); 1277 #endif 1278 } 1279 1280 ModuleSpec GDBRemoteCommunicationServerCommon::GetModuleInfo( 1281 const std::string &module_path, const std::string &triple) { 1282 ArchSpec arch(triple.c_str()); 1283 1284 const FileSpec req_module_path_spec(module_path, true); 1285 const FileSpec module_path_spec = 1286 FindModuleFile(req_module_path_spec.GetPath(), arch); 1287 const ModuleSpec module_spec(module_path_spec, arch); 1288 1289 ModuleSpecList module_specs; 1290 if (!ObjectFile::GetModuleSpecifications(module_path_spec, 0, 0, 1291 module_specs)) 1292 return ModuleSpec(); 1293 1294 ModuleSpec matched_module_spec; 1295 if (!module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) 1296 return ModuleSpec(); 1297 1298 return matched_module_spec; 1299 } 1300