diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp index e49bf22512cf3..c68fcc1789d80 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.cpp @@ -1703,17 +1703,13 @@ Status GDBRemoteCommunicationClient::LoadQXferMemoryMap() { return error; } - std::string xml; - lldb_private::Status lldberr; - if (!ReadExtFeature(ConstString("memory-map"), ConstString(""), xml, - lldberr)) { - error.SetErrorString("Failed to read memory map"); - return error; - } + llvm::Expected xml = ReadExtFeature("memory-map", ""); + if (!xml) + return Status(xml.takeError()); XMLDocument xml_document; - if (!xml_document.ParseMemory(xml.c_str(), xml.size())) { + if (!xml_document.ParseMemory(xml->c_str(), xml->size())) { error.SetErrorString("Failed to parse memory map xml"); return error; } @@ -3883,15 +3879,14 @@ GDBRemoteCommunicationClient::GetModulesInfo( // query the target remote for extended information using the qXfer packet // -// example: object='features', annex='target.xml', out= return: -// 'true' on success -// 'false' on failure (err set) -bool GDBRemoteCommunicationClient::ReadExtFeature( - const lldb_private::ConstString object, - const lldb_private::ConstString annex, std::string &out, - lldb_private::Status &err) { - - std::stringstream output; +// example: object='features', annex='target.xml' +// return: or error +llvm::Expected +GDBRemoteCommunicationClient::ReadExtFeature(llvm::StringRef object, + llvm::StringRef annex) { + + std::string output; + llvm::raw_string_ostream output_stream(output); StringExtractorGDBRemote chunk; uint64_t size = GetRemoteMaxPacketSize(); @@ -3905,28 +3900,22 @@ bool GDBRemoteCommunicationClient::ReadExtFeature( while (active) { // send query extended feature packet - std::stringstream packet; - packet << "qXfer:" << object.AsCString("") - << ":read:" << annex.AsCString("") << ":" << std::hex << offset - << "," << std::hex << size; + std::string packet = + ("qXfer:" + object + ":read:" + annex + ":" + + llvm::Twine::utohexstr(offset) + "," + llvm::Twine::utohexstr(size)) + .str(); GDBRemoteCommunication::PacketResult res = - SendPacketAndWaitForResponse(packet.str(), chunk); + SendPacketAndWaitForResponse(packet, chunk); - if (res != GDBRemoteCommunication::PacketResult::Success) { - err.SetErrorString("Error sending $qXfer packet"); - return false; - } - - const std::string &str = std::string(chunk.GetStringRef()); - if (str.length() == 0) { - // should have some data in chunk - err.SetErrorString("Empty response from $qXfer packet"); - return false; + if (res != GDBRemoteCommunication::PacketResult::Success || + chunk.GetStringRef().empty()) { + return llvm::createStringError(llvm::inconvertibleErrorCode(), + "Error sending $qXfer packet"); } // check packet code - switch (str[0]) { + switch (chunk.GetStringRef()[0]) { // last chunk case ('l'): active = false; @@ -3934,20 +3923,19 @@ bool GDBRemoteCommunicationClient::ReadExtFeature( // more chunks case ('m'): - output << str.substr(1); - offset += str.length() - 1; + output_stream << chunk.GetStringRef().drop_front(); + offset += chunk.GetStringRef().size() - 1; break; // unknown chunk default: - err.SetErrorString("Invalid continuation code from $qXfer packet"); - return false; + return llvm::createStringError( + llvm::inconvertibleErrorCode(), + "Invalid continuation code from $qXfer packet"); } } - out = output.str(); - err.Success(); - return true; + return output_stream.str(); } // Notify the target that gdb is prepared to serve symbol lookup requests. diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h index 9b8b4cb887056..a199ef9708ac1 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationClient.h @@ -451,9 +451,8 @@ class GDBRemoteCommunicationClient : public GDBRemoteClientBase { GetModulesInfo(llvm::ArrayRef module_file_specs, const llvm::Triple &triple); - bool ReadExtFeature(const lldb_private::ConstString object, - const lldb_private::ConstString annex, std::string &out, - lldb_private::Status &err); + llvm::Expected ReadExtFeature(llvm::StringRef object, + llvm::StringRef annex); void ServeSymbolLookups(lldb_private::Process *process); diff --git a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp index 0f34cb42e9865..74bebcd1df4c3 100644 --- a/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp @@ -3921,12 +3921,14 @@ Status ProcessGDBRemote::SendEventData(const char *data) { DataExtractor ProcessGDBRemote::GetAuxvData() { DataBufferSP buf; if (m_gdb_comm.GetQXferAuxvReadSupported()) { - std::string response_string; - Status ST; - if (m_gdb_comm.ReadExtFeature(ConstString("auxv"), ConstString(""), - response_string, ST)) - buf = std::make_shared(response_string.c_str(), - response_string.length()); + llvm::Expected response = m_gdb_comm.ReadExtFeature("auxv", ""); + if (response) + buf = std::make_shared(response->c_str(), + response->length()); + else + LLDB_LOG_ERROR( + ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet(GDBR_LOG_PROCESS), + response.takeError(), "{0}"); } return DataExtractor(buf, GetByteOrder(), GetAddressByteSize()); } @@ -4356,17 +4358,14 @@ bool ParseRegisters(XMLNode feature_node, GdbServerTargetInfo &target_info, bool ProcessGDBRemote::GetGDBServerRegisterInfoXMLAndProcess( ArchSpec &arch_to_use, std::string xml_filename, std::vector ®isters) { // request the target xml file - std::string raw; - lldb_private::Status lldberr; - if (!m_gdb_comm.ReadExtFeature(ConstString("features"), - ConstString(xml_filename.c_str()), raw, - lldberr)) { + llvm::Expected raw = m_gdb_comm.ReadExtFeature("features", xml_filename); + if (errorToBool(raw.takeError())) return false; - } XMLDocument xml_document; - if (xml_document.ParseMemory(raw.c_str(), raw.size(), xml_filename.c_str())) { + if (xml_document.ParseMemory(raw->c_str(), raw->size(), + xml_filename.c_str())) { GdbServerTargetInfo target_info; std::vector feature_nodes; @@ -4565,19 +4564,15 @@ llvm::Expected ProcessGDBRemote::GetLoadedModuleList() { // check that we have extended feature read support if (can_use_svr4 && comm.GetQXferLibrariesSVR4ReadSupported()) { // request the loaded library list - std::string raw; - lldb_private::Status lldberr; - - if (!comm.ReadExtFeature(ConstString("libraries-svr4"), ConstString(""), - raw, lldberr)) - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "Error in libraries-svr4 packet"); + llvm::Expected raw = comm.ReadExtFeature("libraries-svr4", ""); + if (!raw) + return raw.takeError(); // parse the xml file in memory - LLDB_LOGF(log, "parsing: %s", raw.c_str()); + LLDB_LOGF(log, "parsing: %s", raw->c_str()); XMLDocument doc; - if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml")) + if (!doc.ParseMemory(raw->c_str(), raw->size(), "noname.xml")) return llvm::createStringError(llvm::inconvertibleErrorCode(), "Error reading noname.xml"); @@ -4656,18 +4651,15 @@ llvm::Expected ProcessGDBRemote::GetLoadedModuleList() { return list; } else if (comm.GetQXferLibrariesReadSupported()) { // request the loaded library list - std::string raw; - lldb_private::Status lldberr; + llvm::Expected raw = comm.ReadExtFeature("libraries", ""); - if (!comm.ReadExtFeature(ConstString("libraries"), ConstString(""), raw, - lldberr)) - return llvm::createStringError(llvm::inconvertibleErrorCode(), - "Error in libraries packet"); + if (!raw) + return raw.takeError(); - LLDB_LOGF(log, "parsing: %s", raw.c_str()); + LLDB_LOGF(log, "parsing: %s", raw->c_str()); XMLDocument doc; - if (!doc.ParseMemory(raw.c_str(), raw.size(), "noname.xml")) + if (!doc.ParseMemory(raw->c_str(), raw->size(), "noname.xml")) return llvm::createStringError(llvm::inconvertibleErrorCode(), "Error reading noname.xml");