From 0e5cdbf07e6d45ca168a76b2bc19b6e385cfae17 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Thu, 13 Apr 2023 13:02:45 -0700 Subject: [lldb] Make ObjectFileJSON loadable as a module This patch adds support for creating modules from JSON object files. This is necessary for the crashlog use case where we don't have either a module or a symbol file. In that case the ObjectFileJSON serves as both. The patch adds support for an object file type (i.e. executable, shared library, etc). It also adds the ability to specify sections, which is necessary in order specify symbols by address. Finally, this patch improves error handling and fixes a bug where we wouldn't read more than the initial 512 bytes in GetModuleSpecifications. Differential revision: https://reviews.llvm.org/D148062 --- .../Plugins/ObjectFile/JSON/ObjectFileJSON.cpp | 73 +++++++++++++++++----- 1 file changed, 58 insertions(+), 15 deletions(-) (limited to 'lldb/source/Plugins/ObjectFile/JSON/ObjectFileJSON.cpp') diff --git a/lldb/source/Plugins/ObjectFile/JSON/ObjectFileJSON.cpp b/lldb/source/Plugins/ObjectFile/JSON/ObjectFileJSON.cpp index 7cd836a..ffbd877 100644 --- a/lldb/source/Plugins/ObjectFile/JSON/ObjectFileJSON.cpp +++ b/lldb/source/Plugins/ObjectFile/JSON/ObjectFileJSON.cpp @@ -49,6 +49,7 @@ ObjectFileJSON::CreateInstance(const ModuleSP &module_sp, DataBufferSP data_sp, if (!MagicBytesMatch(data_sp, 0, data_sp->GetByteSize())) return nullptr; + // Update the data to contain the entire file if it doesn't already. if (data_sp->GetByteSize() < length) { data_sp = MapFileData(*file, length, file_offset); if (!data_sp) @@ -56,30 +57,41 @@ ObjectFileJSON::CreateInstance(const ModuleSP &module_sp, DataBufferSP data_sp, data_offset = 0; } + Log *log = GetLog(LLDBLog::Symbols); + auto text = llvm::StringRef(reinterpret_cast(data_sp->GetBytes())); Expected json = json::parse(text); if (!json) { - llvm::consumeError(json.takeError()); + LLDB_LOG_ERROR(log, json.takeError(), + "failed to parse JSON object file: {0}"); return nullptr; } json::Path::Root root; Header header; - if (!fromJSON(*json, header, root)) + if (!fromJSON(*json, header, root)) { + LLDB_LOG_ERROR(log, root.getError(), + "failed to parse JSON object file header: {0}"); return nullptr; + } ArchSpec arch(header.triple); UUID uuid; uuid.SetFromStringRef(header.uuid); + Type type = header.type.value_or(eTypeDebugInfo); Body body; - fromJSON(*json, body, root); + if (!fromJSON(*json, body, root)) { + LLDB_LOG_ERROR(log, root.getError(), + "failed to parse JSON object file body: {0}"); + return nullptr; + } return new ObjectFileJSON(module_sp, data_sp, data_offset, file, file_offset, - length, std::move(arch), std::move(uuid), - std::move(body.symbols)); + length, std::move(arch), std::move(uuid), type, + std::move(body.symbols), std::move(body.sections)); } ObjectFile *ObjectFileJSON::CreateMemoryInstance(const ModuleSP &module_sp, @@ -92,23 +104,36 @@ ObjectFile *ObjectFileJSON::CreateMemoryInstance(const ModuleSP &module_sp, size_t ObjectFileJSON::GetModuleSpecifications( const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset, offset_t file_offset, offset_t length, ModuleSpecList &specs) { - if (!MagicBytesMatch(data_sp, data_offset, data_sp->GetByteSize())) return 0; + // Update the data to contain the entire file if it doesn't already. + if (data_sp->GetByteSize() < length) { + data_sp = MapFileData(file, length, file_offset); + if (!data_sp) + return 0; + data_offset = 0; + } + + Log *log = GetLog(LLDBLog::Symbols); + auto text = llvm::StringRef(reinterpret_cast(data_sp->GetBytes())); Expected json = json::parse(text); if (!json) { - llvm::consumeError(json.takeError()); + LLDB_LOG_ERROR(log, json.takeError(), + "failed to parse JSON object file: {0}"); return 0; } json::Path::Root root; Header header; - if (!fromJSON(*json, header, root)) + if (!fromJSON(*json, header, root)) { + LLDB_LOG_ERROR(log, root.getError(), + "failed to parse JSON object file header: {0}"); return 0; + } ArchSpec arch(header.triple); UUID uuid; @@ -123,10 +148,12 @@ size_t ObjectFileJSON::GetModuleSpecifications( ObjectFileJSON::ObjectFileJSON(const ModuleSP &module_sp, DataBufferSP &data_sp, offset_t data_offset, const FileSpec *file, offset_t offset, offset_t length, ArchSpec arch, - UUID uuid, std::vector symbols) + UUID uuid, Type type, + std::vector symbols, + std::vector sections) : ObjectFile(module_sp, file, offset, length, data_sp, data_offset), - m_arch(std::move(arch)), m_uuid(std::move(uuid)), - m_symbols(std::move(symbols)) {} + m_arch(std::move(arch)), m_uuid(std::move(uuid)), m_type(type), + m_symbols(std::move(symbols)), m_sections(std::move(sections)) {} bool ObjectFileJSON::ParseHeader() { // We already parsed the header during initialization. @@ -139,7 +166,7 @@ void ObjectFileJSON::ParseSymtab(Symtab &symtab) { for (JSONSymbol json_symbol : m_symbols) { llvm::Expected symbol = Symbol::FromJSON(json_symbol, section_list); if (!symbol) { - LLDB_LOG_ERROR(log, symbol.takeError(), "invalid symbol"); + LLDB_LOG_ERROR(log, symbol.takeError(), "invalid symbol: {0}"); continue; } symtab.AddSymbol(*symbol); @@ -147,7 +174,21 @@ void ObjectFileJSON::ParseSymtab(Symtab &symtab) { symtab.Finalize(); } -void ObjectFileJSON::CreateSections(SectionList &unified_section_list) {} +void ObjectFileJSON::CreateSections(SectionList &unified_section_list) { + if (m_sections_up) + return; + m_sections_up = std::make_unique(); + + lldb::user_id_t id = 1; + for (const auto §ion : m_sections) { + auto section_sp = std::make_shared
( + GetModule(), this, id++, ConstString(section.name), + section.type.value_or(eSectionTypeCode), 0, section.size.value_or(0), 0, + section.size.value_or(0), /*log2align*/ 0, /*flags*/ 0); + m_sections_up->AddSection(section_sp); + unified_section_list.AddSection(section_sp); + } +} bool ObjectFileJSON::MagicBytesMatch(DataBufferSP data_sp, lldb::addr_t data_offset, @@ -164,13 +205,15 @@ namespace lldb_private { bool fromJSON(const json::Value &value, ObjectFileJSON::Header &header, json::Path path) { json::ObjectMapper o(value, path); - return o && o.map("triple", header.triple) && o.map("uuid", header.uuid); + return o && o.map("triple", header.triple) && o.map("uuid", header.uuid) && + o.map("type", header.type); } bool fromJSON(const json::Value &value, ObjectFileJSON::Body &body, json::Path path) { json::ObjectMapper o(value, path); - return o && o.map("symbols", body.symbols); + return o && o.mapOptional("symbols", body.symbols) && + o.mapOptional("sections", body.sections); } } // namespace lldb_private -- cgit v1.1