180814287SRaphael Isemann //===-- ObjectFileBreakpad.cpp --------------------------------------------===//
21f6b2477SPavel Labath //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61f6b2477SPavel Labath //
71f6b2477SPavel Labath //===----------------------------------------------------------------------===//
81f6b2477SPavel Labath
91f6b2477SPavel Labath #include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
102cf5486cSPavel Labath #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
111f6b2477SPavel Labath #include "lldb/Core/ModuleSpec.h"
121f6b2477SPavel Labath #include "lldb/Core/PluginManager.h"
13ed42ea47SPavel Labath #include "lldb/Core/Section.h"
141f6b2477SPavel Labath
151f6b2477SPavel Labath using namespace lldb;
161f6b2477SPavel Labath using namespace lldb_private;
171f6b2477SPavel Labath using namespace lldb_private::breakpad;
181f6b2477SPavel Labath
19bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFileBreakpad)
20fbb4d1e4SJonas Devlieghere
211f6b2477SPavel Labath namespace {
221f6b2477SPavel Labath struct Header {
231f6b2477SPavel Labath ArchSpec arch;
241f6b2477SPavel Labath UUID uuid;
251f6b2477SPavel Labath static llvm::Optional<Header> parse(llvm::StringRef text);
261f6b2477SPavel Labath };
271f6b2477SPavel Labath } // namespace
281f6b2477SPavel Labath
parse(llvm::StringRef text)291f6b2477SPavel Labath llvm::Optional<Header> Header::parse(llvm::StringRef text) {
302cf5486cSPavel Labath llvm::StringRef line;
311f6b2477SPavel Labath std::tie(line, text) = text.split('\n');
322cf5486cSPavel Labath auto Module = ModuleRecord::parse(line);
332cf5486cSPavel Labath if (!Module)
341f6b2477SPavel Labath return llvm::None;
351f6b2477SPavel Labath
361f6b2477SPavel Labath llvm::Triple triple;
375b18ddb6SPavel Labath triple.setArch(Module->Arch);
385b18ddb6SPavel Labath triple.setOS(Module->OS);
391f6b2477SPavel Labath
401f6b2477SPavel Labath std::tie(line, text) = text.split('\n');
411f6b2477SPavel Labath
422cf5486cSPavel Labath auto Info = InfoRecord::parse(line);
435b18ddb6SPavel Labath UUID uuid = Info && Info->ID ? Info->ID : Module->ID;
442cf5486cSPavel Labath return Header{ArchSpec(triple), std::move(uuid)};
451f6b2477SPavel Labath }
461f6b2477SPavel Labath
47e84f7841SPavel Labath char ObjectFileBreakpad::ID;
48e84f7841SPavel Labath
Initialize()491f6b2477SPavel Labath void ObjectFileBreakpad::Initialize() {
501f6b2477SPavel Labath PluginManager::RegisterPlugin(GetPluginNameStatic(),
511f6b2477SPavel Labath GetPluginDescriptionStatic(), CreateInstance,
52871f2b65SPavel Labath CreateMemoryInstance, GetModuleSpecifications);
531f6b2477SPavel Labath }
541f6b2477SPavel Labath
Terminate()551f6b2477SPavel Labath void ObjectFileBreakpad::Terminate() {
561f6b2477SPavel Labath PluginManager::UnregisterPlugin(CreateInstance);
571f6b2477SPavel Labath }
581f6b2477SPavel Labath
CreateInstance(const ModuleSP & module_sp,DataBufferSP data_sp,offset_t data_offset,const FileSpec * file,offset_t file_offset,offset_t length)591f6b2477SPavel Labath ObjectFile *ObjectFileBreakpad::CreateInstance(
60c69307e5SJonas Devlieghere const ModuleSP &module_sp, DataBufferSP data_sp, offset_t data_offset,
611f6b2477SPavel Labath const FileSpec *file, offset_t file_offset, offset_t length) {
621f6b2477SPavel Labath if (!data_sp) {
631f6b2477SPavel Labath data_sp = MapFileData(*file, length, file_offset);
641f6b2477SPavel Labath if (!data_sp)
651f6b2477SPavel Labath return nullptr;
661f6b2477SPavel Labath data_offset = 0;
671f6b2477SPavel Labath }
681f6b2477SPavel Labath auto text = toStringRef(data_sp->GetData());
691f6b2477SPavel Labath llvm::Optional<Header> header = Header::parse(text);
701f6b2477SPavel Labath if (!header)
711f6b2477SPavel Labath return nullptr;
721f6b2477SPavel Labath
731f6b2477SPavel Labath // Update the data to contain the entire file if it doesn't already
741f6b2477SPavel Labath if (data_sp->GetByteSize() < length) {
751f6b2477SPavel Labath data_sp = MapFileData(*file, length, file_offset);
761f6b2477SPavel Labath if (!data_sp)
771f6b2477SPavel Labath return nullptr;
781f6b2477SPavel Labath data_offset = 0;
791f6b2477SPavel Labath }
801f6b2477SPavel Labath
811f6b2477SPavel Labath return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,
821f6b2477SPavel Labath file_offset, length, std::move(header->arch),
831f6b2477SPavel Labath std::move(header->uuid));
841f6b2477SPavel Labath }
851f6b2477SPavel Labath
CreateMemoryInstance(const ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,addr_t header_addr)861f6b2477SPavel Labath ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(
87*f2ea125eSJonas Devlieghere const ModuleSP &module_sp, WritableDataBufferSP data_sp,
881f6b2477SPavel Labath const ProcessSP &process_sp, addr_t header_addr) {
891f6b2477SPavel Labath return nullptr;
901f6b2477SPavel Labath }
911f6b2477SPavel Labath
GetModuleSpecifications(const FileSpec & file,DataBufferSP & data_sp,offset_t data_offset,offset_t file_offset,offset_t length,ModuleSpecList & specs)921f6b2477SPavel Labath size_t ObjectFileBreakpad::GetModuleSpecifications(
931f6b2477SPavel Labath const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
941f6b2477SPavel Labath offset_t file_offset, offset_t length, ModuleSpecList &specs) {
951f6b2477SPavel Labath auto text = toStringRef(data_sp->GetData());
961f6b2477SPavel Labath llvm::Optional<Header> header = Header::parse(text);
971f6b2477SPavel Labath if (!header)
981f6b2477SPavel Labath return 0;
991f6b2477SPavel Labath ModuleSpec spec(file, std::move(header->arch));
1001f6b2477SPavel Labath spec.GetUUID() = std::move(header->uuid);
1011f6b2477SPavel Labath specs.Append(spec);
1021f6b2477SPavel Labath return 1;
1031f6b2477SPavel Labath }
1041f6b2477SPavel Labath
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)1051f6b2477SPavel Labath ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,
1061f6b2477SPavel Labath DataBufferSP &data_sp,
1071f6b2477SPavel Labath offset_t data_offset,
1081f6b2477SPavel Labath const FileSpec *file, offset_t offset,
1091f6b2477SPavel Labath offset_t length, ArchSpec arch,
1101f6b2477SPavel Labath UUID uuid)
1111f6b2477SPavel Labath : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
1121f6b2477SPavel Labath m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
1131f6b2477SPavel Labath
ParseHeader()1141f6b2477SPavel Labath bool ObjectFileBreakpad::ParseHeader() {
1151f6b2477SPavel Labath // We already parsed the header during initialization.
1161f6b2477SPavel Labath return true;
1171f6b2477SPavel Labath }
1181f6b2477SPavel Labath
ParseSymtab(Symtab & symtab)1197e6df41fSGreg Clayton void ObjectFileBreakpad::ParseSymtab(Symtab &symtab) {
1207e6df41fSGreg Clayton // Nothing to do for breakpad files, all information is parsed as debug info
1217e6df41fSGreg Clayton // which means "lldb_private::Function" objects are used, or symbols are added
1227e6df41fSGreg Clayton // by the SymbolFileBreakpad::AddSymbols(...) function in the symbol file.
1231f6b2477SPavel Labath }
1241f6b2477SPavel Labath
CreateSections(SectionList & unified_section_list)1251f6b2477SPavel Labath void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {
126d5b44036SJonas Devlieghere if (m_sections_up)
127ed42ea47SPavel Labath return;
128a8f3ae7cSJonas Devlieghere m_sections_up = std::make_unique<SectionList>();
129ed42ea47SPavel Labath
1302cf5486cSPavel Labath llvm::Optional<Record::Kind> current_section;
131ed42ea47SPavel Labath offset_t section_start;
132ed42ea47SPavel Labath llvm::StringRef text = toStringRef(m_data.GetData());
133ed42ea47SPavel Labath uint32_t next_section_id = 1;
134ed42ea47SPavel Labath auto maybe_add_section = [&](const uint8_t *end_ptr) {
1352cf5486cSPavel Labath if (!current_section)
136ed42ea47SPavel Labath return; // We have been called before parsing the first line.
137ed42ea47SPavel Labath
138ed42ea47SPavel Labath offset_t end_offset = end_ptr - m_data.GetDataStart();
139ed42ea47SPavel Labath auto section_sp = std::make_shared<Section>(
140ed42ea47SPavel Labath GetModule(), this, next_section_id++,
1412cf5486cSPavel Labath ConstString(toString(*current_section)), eSectionTypeOther,
142ed42ea47SPavel Labath /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
143ed42ea47SPavel Labath end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
144d5b44036SJonas Devlieghere m_sections_up->AddSection(section_sp);
145ed42ea47SPavel Labath unified_section_list.AddSection(section_sp);
146ed42ea47SPavel Labath };
147ed42ea47SPavel Labath while (!text.empty()) {
148ed42ea47SPavel Labath llvm::StringRef line;
149ed42ea47SPavel Labath std::tie(line, text) = text.split('\n');
150ed42ea47SPavel Labath
151dfaafbcfSPavel Labath llvm::Optional<Record::Kind> next_section = Record::classify(line);
152cc9ced0eSZequan Wu if (next_section == Record::Line || next_section == Record::Inline) {
153cc9ced0eSZequan Wu // Line/Inline records logically belong to the preceding Func record, so
154cc9ced0eSZequan Wu // we put them in the same section.
1552cf5486cSPavel Labath next_section = Record::Func;
156ed42ea47SPavel Labath }
1572cf5486cSPavel Labath if (next_section == current_section)
158ed42ea47SPavel Labath continue;
159ed42ea47SPavel Labath
160ed42ea47SPavel Labath // Changing sections, finish off the previous one, if there was any.
161ed42ea47SPavel Labath maybe_add_section(line.bytes_begin());
162ed42ea47SPavel Labath // And start a new one.
1632cf5486cSPavel Labath current_section = next_section;
164ed42ea47SPavel Labath section_start = line.bytes_begin() - m_data.GetDataStart();
165ed42ea47SPavel Labath }
166ed42ea47SPavel Labath // Finally, add the last section.
167ed42ea47SPavel Labath maybe_add_section(m_data.GetDataEnd());
1681f6b2477SPavel Labath }
169