xref: /freebsd-src/contrib/llvm-project/lldb/source/Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
15ffd83dbSDimitry Andric //===-- ObjectFileBreakpad.cpp --------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
100b57cec5SDimitry Andric #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
110b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
120b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
130b57cec5SDimitry Andric #include "lldb/Core/Section.h"
14*bdd1243dSDimitry Andric #include <optional>
150b57cec5SDimitry Andric 
160b57cec5SDimitry Andric using namespace lldb;
170b57cec5SDimitry Andric using namespace lldb_private;
180b57cec5SDimitry Andric using namespace lldb_private::breakpad;
190b57cec5SDimitry Andric 
205ffd83dbSDimitry Andric LLDB_PLUGIN_DEFINE(ObjectFileBreakpad)
215ffd83dbSDimitry Andric 
220b57cec5SDimitry Andric namespace {
230b57cec5SDimitry Andric struct Header {
240b57cec5SDimitry Andric   ArchSpec arch;
250b57cec5SDimitry Andric   UUID uuid;
26*bdd1243dSDimitry Andric   static std::optional<Header> parse(llvm::StringRef text);
270b57cec5SDimitry Andric };
280b57cec5SDimitry Andric } // namespace
290b57cec5SDimitry Andric 
parse(llvm::StringRef text)30*bdd1243dSDimitry Andric std::optional<Header> Header::parse(llvm::StringRef text) {
310b57cec5SDimitry Andric   llvm::StringRef line;
320b57cec5SDimitry Andric   std::tie(line, text) = text.split('\n');
330b57cec5SDimitry Andric   auto Module = ModuleRecord::parse(line);
340b57cec5SDimitry Andric   if (!Module)
35*bdd1243dSDimitry Andric     return std::nullopt;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric   llvm::Triple triple;
380b57cec5SDimitry Andric   triple.setArch(Module->Arch);
390b57cec5SDimitry Andric   triple.setOS(Module->OS);
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric   std::tie(line, text) = text.split('\n');
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   auto Info = InfoRecord::parse(line);
440b57cec5SDimitry Andric   UUID uuid = Info && Info->ID ? Info->ID : Module->ID;
450b57cec5SDimitry Andric   return Header{ArchSpec(triple), std::move(uuid)};
460b57cec5SDimitry Andric }
470b57cec5SDimitry Andric 
489dba64beSDimitry Andric char ObjectFileBreakpad::ID;
499dba64beSDimitry Andric 
Initialize()500b57cec5SDimitry Andric void ObjectFileBreakpad::Initialize() {
510b57cec5SDimitry Andric   PluginManager::RegisterPlugin(GetPluginNameStatic(),
520b57cec5SDimitry Andric                                 GetPluginDescriptionStatic(), CreateInstance,
530b57cec5SDimitry Andric                                 CreateMemoryInstance, GetModuleSpecifications);
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric 
Terminate()560b57cec5SDimitry Andric void ObjectFileBreakpad::Terminate() {
570b57cec5SDimitry Andric   PluginManager::UnregisterPlugin(CreateInstance);
580b57cec5SDimitry Andric }
590b57cec5SDimitry Andric 
CreateInstance(const ModuleSP & module_sp,DataBufferSP data_sp,offset_t data_offset,const FileSpec * file,offset_t file_offset,offset_t length)600b57cec5SDimitry Andric ObjectFile *ObjectFileBreakpad::CreateInstance(
6181ad6265SDimitry Andric     const ModuleSP &module_sp, DataBufferSP data_sp, offset_t data_offset,
620b57cec5SDimitry Andric     const FileSpec *file, offset_t file_offset, offset_t length) {
630b57cec5SDimitry Andric   if (!data_sp) {
640b57cec5SDimitry Andric     data_sp = MapFileData(*file, length, file_offset);
650b57cec5SDimitry Andric     if (!data_sp)
660b57cec5SDimitry Andric       return nullptr;
670b57cec5SDimitry Andric     data_offset = 0;
680b57cec5SDimitry Andric   }
690b57cec5SDimitry Andric   auto text = toStringRef(data_sp->GetData());
70*bdd1243dSDimitry Andric   std::optional<Header> header = Header::parse(text);
710b57cec5SDimitry Andric   if (!header)
720b57cec5SDimitry Andric     return nullptr;
730b57cec5SDimitry Andric 
740b57cec5SDimitry Andric   // Update the data to contain the entire file if it doesn't already
750b57cec5SDimitry Andric   if (data_sp->GetByteSize() < length) {
760b57cec5SDimitry Andric     data_sp = MapFileData(*file, length, file_offset);
770b57cec5SDimitry Andric     if (!data_sp)
780b57cec5SDimitry Andric       return nullptr;
790b57cec5SDimitry Andric     data_offset = 0;
800b57cec5SDimitry Andric   }
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,
830b57cec5SDimitry Andric                                 file_offset, length, std::move(header->arch),
840b57cec5SDimitry Andric                                 std::move(header->uuid));
850b57cec5SDimitry Andric }
860b57cec5SDimitry Andric 
CreateMemoryInstance(const ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,addr_t header_addr)870b57cec5SDimitry Andric ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(
8881ad6265SDimitry Andric     const ModuleSP &module_sp, WritableDataBufferSP data_sp,
890b57cec5SDimitry Andric     const ProcessSP &process_sp, addr_t header_addr) {
900b57cec5SDimitry Andric   return nullptr;
910b57cec5SDimitry Andric }
920b57cec5SDimitry Andric 
GetModuleSpecifications(const FileSpec & file,DataBufferSP & data_sp,offset_t data_offset,offset_t file_offset,offset_t length,ModuleSpecList & specs)930b57cec5SDimitry Andric size_t ObjectFileBreakpad::GetModuleSpecifications(
940b57cec5SDimitry Andric     const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
950b57cec5SDimitry Andric     offset_t file_offset, offset_t length, ModuleSpecList &specs) {
960b57cec5SDimitry Andric   auto text = toStringRef(data_sp->GetData());
97*bdd1243dSDimitry Andric   std::optional<Header> header = Header::parse(text);
980b57cec5SDimitry Andric   if (!header)
990b57cec5SDimitry Andric     return 0;
1000b57cec5SDimitry Andric   ModuleSpec spec(file, std::move(header->arch));
1010b57cec5SDimitry Andric   spec.GetUUID() = std::move(header->uuid);
1020b57cec5SDimitry Andric   specs.Append(spec);
1030b57cec5SDimitry Andric   return 1;
1040b57cec5SDimitry Andric }
1050b57cec5SDimitry Andric 
ObjectFileBreakpad(const ModuleSP & module_sp,DataBufferSP & data_sp,offset_t data_offset,const FileSpec * file,offset_t offset,offset_t length,ArchSpec arch,UUID uuid)1060b57cec5SDimitry Andric ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,
1070b57cec5SDimitry Andric                                        DataBufferSP &data_sp,
1080b57cec5SDimitry Andric                                        offset_t data_offset,
1090b57cec5SDimitry Andric                                        const FileSpec *file, offset_t offset,
1100b57cec5SDimitry Andric                                        offset_t length, ArchSpec arch,
1110b57cec5SDimitry Andric                                        UUID uuid)
1120b57cec5SDimitry Andric     : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
1130b57cec5SDimitry Andric       m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
1140b57cec5SDimitry Andric 
ParseHeader()1150b57cec5SDimitry Andric bool ObjectFileBreakpad::ParseHeader() {
1160b57cec5SDimitry Andric   // We already parsed the header during initialization.
1170b57cec5SDimitry Andric   return true;
1180b57cec5SDimitry Andric }
1190b57cec5SDimitry Andric 
ParseSymtab(Symtab & symtab)1204824e7fdSDimitry Andric void ObjectFileBreakpad::ParseSymtab(Symtab &symtab) {
1214824e7fdSDimitry Andric   // Nothing to do for breakpad files, all information is parsed as debug info
1224824e7fdSDimitry Andric   // which means "lldb_private::Function" objects are used, or symbols are added
1234824e7fdSDimitry Andric   // by the SymbolFileBreakpad::AddSymbols(...) function in the symbol file.
1240b57cec5SDimitry Andric }
1250b57cec5SDimitry Andric 
CreateSections(SectionList & unified_section_list)1260b57cec5SDimitry Andric void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {
1270b57cec5SDimitry Andric   if (m_sections_up)
1280b57cec5SDimitry Andric     return;
1299dba64beSDimitry Andric   m_sections_up = std::make_unique<SectionList>();
1300b57cec5SDimitry Andric 
131*bdd1243dSDimitry Andric   std::optional<Record::Kind> current_section;
1320b57cec5SDimitry Andric   offset_t section_start;
1330b57cec5SDimitry Andric   llvm::StringRef text = toStringRef(m_data.GetData());
1340b57cec5SDimitry Andric   uint32_t next_section_id = 1;
1350b57cec5SDimitry Andric   auto maybe_add_section = [&](const uint8_t *end_ptr) {
1360b57cec5SDimitry Andric     if (!current_section)
1370b57cec5SDimitry Andric       return; // We have been called before parsing the first line.
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric     offset_t end_offset = end_ptr - m_data.GetDataStart();
1400b57cec5SDimitry Andric     auto section_sp = std::make_shared<Section>(
1410b57cec5SDimitry Andric         GetModule(), this, next_section_id++,
1420b57cec5SDimitry Andric         ConstString(toString(*current_section)), eSectionTypeOther,
1430b57cec5SDimitry Andric         /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
1440b57cec5SDimitry Andric         end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
1450b57cec5SDimitry Andric     m_sections_up->AddSection(section_sp);
1460b57cec5SDimitry Andric     unified_section_list.AddSection(section_sp);
1470b57cec5SDimitry Andric   };
1480b57cec5SDimitry Andric   while (!text.empty()) {
1490b57cec5SDimitry Andric     llvm::StringRef line;
1500b57cec5SDimitry Andric     std::tie(line, text) = text.split('\n');
1510b57cec5SDimitry Andric 
152*bdd1243dSDimitry Andric     std::optional<Record::Kind> next_section = Record::classify(line);
153349cc55cSDimitry Andric     if (next_section == Record::Line || next_section == Record::Inline) {
154349cc55cSDimitry Andric       // Line/Inline records logically belong to the preceding Func record, so
155349cc55cSDimitry Andric       // we put them in the same section.
1560b57cec5SDimitry Andric       next_section = Record::Func;
1570b57cec5SDimitry Andric     }
1580b57cec5SDimitry Andric     if (next_section == current_section)
1590b57cec5SDimitry Andric       continue;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric     // Changing sections, finish off the previous one, if there was any.
1620b57cec5SDimitry Andric     maybe_add_section(line.bytes_begin());
1630b57cec5SDimitry Andric     // And start a new one.
1640b57cec5SDimitry Andric     current_section = next_section;
1650b57cec5SDimitry Andric     section_start = line.bytes_begin() - m_data.GetDataStart();
1660b57cec5SDimitry Andric   }
1670b57cec5SDimitry Andric   // Finally, add the last section.
1680b57cec5SDimitry Andric   maybe_add_section(m_data.GetDataEnd());
1690b57cec5SDimitry Andric }
170