Path: blob/main/contrib/llvm-project/lldb/source/Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.cpp
39642 views
//===-- ObjectFileBreakpad.cpp --------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"9#include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"10#include "lldb/Core/ModuleSpec.h"11#include "lldb/Core/PluginManager.h"12#include "lldb/Core/Section.h"13#include <optional>1415using namespace lldb;16using namespace lldb_private;17using namespace lldb_private::breakpad;1819LLDB_PLUGIN_DEFINE(ObjectFileBreakpad)2021namespace {22struct Header {23ArchSpec arch;24UUID uuid;25static std::optional<Header> parse(llvm::StringRef text);26};27} // namespace2829std::optional<Header> Header::parse(llvm::StringRef text) {30llvm::StringRef line;31std::tie(line, text) = text.split('\n');32auto Module = ModuleRecord::parse(line);33if (!Module)34return std::nullopt;3536llvm::Triple triple;37triple.setArch(Module->Arch);38triple.setOS(Module->OS);3940std::tie(line, text) = text.split('\n');4142auto Info = InfoRecord::parse(line);43UUID uuid = Info && Info->ID ? Info->ID : Module->ID;44return Header{ArchSpec(triple), std::move(uuid)};45}4647char ObjectFileBreakpad::ID;4849void ObjectFileBreakpad::Initialize() {50PluginManager::RegisterPlugin(GetPluginNameStatic(),51GetPluginDescriptionStatic(), CreateInstance,52CreateMemoryInstance, GetModuleSpecifications);53}5455void ObjectFileBreakpad::Terminate() {56PluginManager::UnregisterPlugin(CreateInstance);57}5859ObjectFile *ObjectFileBreakpad::CreateInstance(60const ModuleSP &module_sp, DataBufferSP data_sp, offset_t data_offset,61const FileSpec *file, offset_t file_offset, offset_t length) {62if (!data_sp) {63data_sp = MapFileData(*file, length, file_offset);64if (!data_sp)65return nullptr;66data_offset = 0;67}68auto text = toStringRef(data_sp->GetData());69std::optional<Header> header = Header::parse(text);70if (!header)71return nullptr;7273// Update the data to contain the entire file if it doesn't already74if (data_sp->GetByteSize() < length) {75data_sp = MapFileData(*file, length, file_offset);76if (!data_sp)77return nullptr;78data_offset = 0;79}8081return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,82file_offset, length, std::move(header->arch),83std::move(header->uuid));84}8586ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(87const ModuleSP &module_sp, WritableDataBufferSP data_sp,88const ProcessSP &process_sp, addr_t header_addr) {89return nullptr;90}9192size_t ObjectFileBreakpad::GetModuleSpecifications(93const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,94offset_t file_offset, offset_t length, ModuleSpecList &specs) {95auto text = toStringRef(data_sp->GetData());96std::optional<Header> header = Header::parse(text);97if (!header)98return 0;99ModuleSpec spec(file, std::move(header->arch));100spec.GetUUID() = std::move(header->uuid);101specs.Append(spec);102return 1;103}104105ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,106DataBufferSP &data_sp,107offset_t data_offset,108const FileSpec *file, offset_t offset,109offset_t length, ArchSpec arch,110UUID uuid)111: ObjectFile(module_sp, file, offset, length, data_sp, data_offset),112m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}113114bool ObjectFileBreakpad::ParseHeader() {115// We already parsed the header during initialization.116return true;117}118119void ObjectFileBreakpad::ParseSymtab(Symtab &symtab) {120// Nothing to do for breakpad files, all information is parsed as debug info121// which means "lldb_private::Function" objects are used, or symbols are added122// by the SymbolFileBreakpad::AddSymbols(...) function in the symbol file.123}124125void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {126if (m_sections_up)127return;128m_sections_up = std::make_unique<SectionList>();129130std::optional<Record::Kind> current_section;131offset_t section_start;132llvm::StringRef text = toStringRef(m_data.GetData());133uint32_t next_section_id = 1;134auto maybe_add_section = [&](const uint8_t *end_ptr) {135if (!current_section)136return; // We have been called before parsing the first line.137138offset_t end_offset = end_ptr - m_data.GetDataStart();139auto section_sp = std::make_shared<Section>(140GetModule(), this, next_section_id++,141ConstString(toString(*current_section)), eSectionTypeOther,142/*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,143end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);144m_sections_up->AddSection(section_sp);145unified_section_list.AddSection(section_sp);146};147while (!text.empty()) {148llvm::StringRef line;149std::tie(line, text) = text.split('\n');150151std::optional<Record::Kind> next_section = Record::classify(line);152if (next_section == Record::Line || next_section == Record::Inline) {153// Line/Inline records logically belong to the preceding Func record, so154// we put them in the same section.155next_section = Record::Func;156}157if (next_section == current_section)158continue;159160// Changing sections, finish off the previous one, if there was any.161maybe_add_section(line.bytes_begin());162// And start a new one.163current_section = next_section;164section_start = line.bytes_begin() - m_data.GetDataStart();165}166// Finally, add the last section.167maybe_add_section(m_data.GetDataEnd());168}169170171