diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp @@ -1897,91 +1897,65 @@ bool GDBRemoteCommunicationClient::DecodeProcessInfoResponse( StringExtractorGDBRemote &response, ProcessInstanceInfo &process_info) { if (response.IsNormalResponse()) { - llvm::StringRef name; - llvm::StringRef value; - StringExtractor extractor; - - uint32_t cpu = LLDB_INVALID_CPUTYPE; - uint32_t sub = 0; - std::string vendor; - std::string os_type; - - while (response.GetNameColonValue(name, value)) { - if (name.equals("pid")) { - lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; - value.getAsInteger(0, pid); - process_info.SetProcessID(pid); - } else if (name.equals("ppid")) { - lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; - value.getAsInteger(0, pid); - process_info.SetParentProcessID(pid); - } else if (name.equals("uid")) { - uint32_t uid = UINT32_MAX; - value.getAsInteger(0, uid); - process_info.SetUserID(uid); - } else if (name.equals("euid")) { - uint32_t uid = UINT32_MAX; - value.getAsInteger(0, uid); - process_info.SetEffectiveUserID(uid); - } else if (name.equals("gid")) { - uint32_t gid = UINT32_MAX; - value.getAsInteger(0, gid); - process_info.SetGroupID(gid); - } else if (name.equals("egid")) { - uint32_t gid = UINT32_MAX; - value.getAsInteger(0, gid); - process_info.SetEffectiveGroupID(gid); - } else if (name.equals("triple")) { - StringExtractor extractor(value); - std::string triple; - extractor.GetHexByteString(triple); - process_info.GetArchitecture().SetTriple(triple.c_str()); - } else if (name.equals("name")) { - StringExtractor extractor(value); - // The process name from ASCII hex bytes since we can't control the - // characters in a process name - std::string name; - extractor.GetHexByteString(name); - process_info.GetExecutableFile().SetFile(name, FileSpec::Style::native); - } else if (name.equals("args")) { - llvm::StringRef encoded_args(value), hex_arg; - - bool is_arg0 = true; - while (!encoded_args.empty()) { - std::tie(hex_arg, encoded_args) = encoded_args.split('-'); - std::string arg; - StringExtractor extractor(hex_arg); - if (extractor.GetHexByteString(arg) * 2 != hex_arg.size()) { - // In case of wrong encoding, we discard all the arguments - process_info.GetArguments().Clear(); - process_info.SetArg0(""); - break; - } - if (is_arg0) - process_info.SetArg0(arg); - else - process_info.GetArguments().AppendArgument(arg); - is_arg0 = false; + KeyValueExtractorGDBRemote extractor(response); + + process_info.SetProcessID( + extractor.GetWithDefault("pid", LLDB_INVALID_PROCESS_ID)); + process_info.SetParentProcessID( + extractor.GetWithDefault("ppid", LLDB_INVALID_PROCESS_ID)); + process_info.SetUserID( + extractor.GetWithDefault("uid", UINT32_MAX)); + process_info.SetEffectiveUserID( + extractor.GetWithDefault("euid", UINT32_MAX)); + process_info.SetGroupID( + extractor.GetWithDefault("gid", UINT32_MAX)); + process_info.SetEffectiveGroupID( + extractor.GetWithDefault("egid", UINT32_MAX)); + + if (llvm::Optional triple = + extractor.GetHexByteString("triple")) + process_info.GetArchitecture().SetTriple(triple->c_str()); + + // The process name from ASCII hex bytes since we can't control the + // characters in a process name + if (llvm::Optional name = extractor.GetHexByteString("name")) + process_info.GetExecutableFile().SetFile(*name, FileSpec::Style::native); + + if (llvm::Optional args = extractor.Get("args")) { + llvm::StringRef encoded_args(*args), hex_arg; + + bool is_arg0 = true; + while (encoded_args.size()) { + std::tie(hex_arg, encoded_args) = encoded_args.split('-'); + std::string arg; + StringExtractor extractor(hex_arg); + if (extractor.GetHexByteString(arg) * 2 != hex_arg.size()) { + // In case of wrong encoding, we discard all the arguments + process_info.GetArguments().Clear(); + process_info.SetArg0(""); + break; } - } else if (name.equals("cputype")) { - value.getAsInteger(0, cpu); - } else if (name.equals("cpusubtype")) { - value.getAsInteger(0, sub); - } else if (name.equals("vendor")) { - vendor = std::string(value); - } else if (name.equals("ostype")) { - os_type = std::string(value); + if (is_arg0) + process_info.SetArg0(arg); + else + process_info.GetArguments().AppendArgument(arg); + is_arg0 = false; } } - if (cpu != LLDB_INVALID_CPUTYPE && !vendor.empty() && !os_type.empty()) { - if (vendor == "apple") { - process_info.GetArchitecture().SetArchitecture(eArchTypeMachO, cpu, + llvm::Optional cpu = extractor.Get("cputype"); + llvm::Optional vendor = extractor.Get("vendor"); + llvm::Optional os_type = extractor.Get("ostype"); + + if (cpu && vendor && vendor->size() && os_type && os_type->size()) { + if (*vendor == "apple") { + uint32_t sub = extractor.GetWithDefault("cpusubtype", 0); + process_info.GetArchitecture().SetArchitecture(eArchTypeMachO, *cpu, sub); process_info.GetArchitecture().GetTriple().setVendorName( - llvm::StringRef(vendor)); + llvm::StringRef(*vendor)); process_info.GetArchitecture().GetTriple().setOSName( - llvm::StringRef(os_type)); + llvm::StringRef(*os_type)); } }