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"
14f190ce62SKazu Hirata #include <optional>
151f6b2477SPavel Labath
161f6b2477SPavel Labath using namespace lldb;
171f6b2477SPavel Labath using namespace lldb_private;
181f6b2477SPavel Labath using namespace lldb_private::breakpad;
191f6b2477SPavel Labath
20bba9ba8dSJonas Devlieghere LLDB_PLUGIN_DEFINE(ObjectFileBreakpad)
21fbb4d1e4SJonas Devlieghere
221f6b2477SPavel Labath namespace {
231f6b2477SPavel Labath struct Header {
241f6b2477SPavel Labath ArchSpec arch;
251f6b2477SPavel Labath UUID uuid;
26*2fe83274SKazu Hirata static std::optional<Header> parse(llvm::StringRef text);
271f6b2477SPavel Labath };
281f6b2477SPavel Labath } // namespace
291f6b2477SPavel Labath
parse(llvm::StringRef text)30*2fe83274SKazu Hirata std::optional<Header> Header::parse(llvm::StringRef text) {
312cf5486cSPavel Labath llvm::StringRef line;
321f6b2477SPavel Labath std::tie(line, text) = text.split('\n');
332cf5486cSPavel Labath auto Module = ModuleRecord::parse(line);
342cf5486cSPavel Labath if (!Module)
35343523d0SKazu Hirata return std::nullopt;
361f6b2477SPavel Labath
371f6b2477SPavel Labath llvm::Triple triple;
385b18ddb6SPavel Labath triple.setArch(Module->Arch);
395b18ddb6SPavel Labath triple.setOS(Module->OS);
401f6b2477SPavel Labath
411f6b2477SPavel Labath std::tie(line, text) = text.split('\n');
421f6b2477SPavel Labath
432cf5486cSPavel Labath auto Info = InfoRecord::parse(line);
445b18ddb6SPavel Labath UUID uuid = Info && Info->ID ? Info->ID : Module->ID;
452cf5486cSPavel Labath return Header{ArchSpec(triple), std::move(uuid)};
461f6b2477SPavel Labath }
471f6b2477SPavel Labath
48e84f7841SPavel Labath char ObjectFileBreakpad::ID;
49e84f7841SPavel Labath
Initialize()501f6b2477SPavel Labath void ObjectFileBreakpad::Initialize() {
511f6b2477SPavel Labath PluginManager::RegisterPlugin(GetPluginNameStatic(),
521f6b2477SPavel Labath GetPluginDescriptionStatic(), CreateInstance,
53871f2b65SPavel Labath CreateMemoryInstance, GetModuleSpecifications);
541f6b2477SPavel Labath }
551f6b2477SPavel Labath
Terminate()561f6b2477SPavel Labath void ObjectFileBreakpad::Terminate() {
571f6b2477SPavel Labath PluginManager::UnregisterPlugin(CreateInstance);
581f6b2477SPavel Labath }
591f6b2477SPavel Labath
CreateInstance(const ModuleSP & module_sp,DataBufferSP data_sp,offset_t data_offset,const FileSpec * file,offset_t file_offset,offset_t length)601f6b2477SPavel Labath ObjectFile *ObjectFileBreakpad::CreateInstance(
61c69307e5SJonas Devlieghere const ModuleSP &module_sp, DataBufferSP data_sp, offset_t data_offset,
621f6b2477SPavel Labath const FileSpec *file, offset_t file_offset, offset_t length) {
631f6b2477SPavel Labath if (!data_sp) {
641f6b2477SPavel Labath data_sp = MapFileData(*file, length, file_offset);
651f6b2477SPavel Labath if (!data_sp)
661f6b2477SPavel Labath return nullptr;
671f6b2477SPavel Labath data_offset = 0;
681f6b2477SPavel Labath }
691f6b2477SPavel Labath auto text = toStringRef(data_sp->GetData());
70*2fe83274SKazu Hirata std::optional<Header> header = Header::parse(text);
711f6b2477SPavel Labath if (!header)
721f6b2477SPavel Labath return nullptr;
731f6b2477SPavel Labath
741f6b2477SPavel Labath // Update the data to contain the entire file if it doesn't already
751f6b2477SPavel Labath if (data_sp->GetByteSize() < length) {
761f6b2477SPavel Labath data_sp = MapFileData(*file, length, file_offset);
771f6b2477SPavel Labath if (!data_sp)
781f6b2477SPavel Labath return nullptr;
791f6b2477SPavel Labath data_offset = 0;
801f6b2477SPavel Labath }
811f6b2477SPavel Labath
821f6b2477SPavel Labath return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,
831f6b2477SPavel Labath file_offset, length, std::move(header->arch),
841f6b2477SPavel Labath std::move(header->uuid));
851f6b2477SPavel Labath }
861f6b2477SPavel Labath
CreateMemoryInstance(const ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,addr_t header_addr)871f6b2477SPavel Labath ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(
88f2ea125eSJonas Devlieghere const ModuleSP &module_sp, WritableDataBufferSP data_sp,
891f6b2477SPavel Labath const ProcessSP &process_sp, addr_t header_addr) {
901f6b2477SPavel Labath return nullptr;
911f6b2477SPavel Labath }
921f6b2477SPavel Labath
GetModuleSpecifications(const FileSpec & file,DataBufferSP & data_sp,offset_t data_offset,offset_t file_offset,offset_t length,ModuleSpecList & specs)931f6b2477SPavel Labath size_t ObjectFileBreakpad::GetModuleSpecifications(
941f6b2477SPavel Labath const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
951f6b2477SPavel Labath offset_t file_offset, offset_t length, ModuleSpecList &specs) {
961f6b2477SPavel Labath auto text = toStringRef(data_sp->GetData());
97*2fe83274SKazu Hirata std::optional<Header> header = Header::parse(text);
981f6b2477SPavel Labath if (!header)
991f6b2477SPavel Labath return 0;
1001f6b2477SPavel Labath ModuleSpec spec(file, std::move(header->arch));
1011f6b2477SPavel Labath spec.GetUUID() = std::move(header->uuid);
1021f6b2477SPavel Labath specs.Append(spec);
1031f6b2477SPavel Labath return 1;
1041f6b2477SPavel Labath }
1051f6b2477SPavel 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)1061f6b2477SPavel Labath ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,
1071f6b2477SPavel Labath DataBufferSP &data_sp,
1081f6b2477SPavel Labath offset_t data_offset,
1091f6b2477SPavel Labath const FileSpec *file, offset_t offset,
1101f6b2477SPavel Labath offset_t length, ArchSpec arch,
1111f6b2477SPavel Labath UUID uuid)
1121f6b2477SPavel Labath : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
1131f6b2477SPavel Labath m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
1141f6b2477SPavel Labath
ParseHeader()1151f6b2477SPavel Labath bool ObjectFileBreakpad::ParseHeader() {
1161f6b2477SPavel Labath // We already parsed the header during initialization.
1171f6b2477SPavel Labath return true;
1181f6b2477SPavel Labath }
1191f6b2477SPavel Labath
ParseSymtab(Symtab & symtab)1207e6df41fSGreg Clayton void ObjectFileBreakpad::ParseSymtab(Symtab &symtab) {
1217e6df41fSGreg Clayton // Nothing to do for breakpad files, all information is parsed as debug info
1227e6df41fSGreg Clayton // which means "lldb_private::Function" objects are used, or symbols are added
1237e6df41fSGreg Clayton // by the SymbolFileBreakpad::AddSymbols(...) function in the symbol file.
1241f6b2477SPavel Labath }
1251f6b2477SPavel Labath
CreateSections(SectionList & unified_section_list)1261f6b2477SPavel Labath void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {
127d5b44036SJonas Devlieghere if (m_sections_up)
128ed42ea47SPavel Labath return;
129a8f3ae7cSJonas Devlieghere m_sections_up = std::make_unique<SectionList>();
130ed42ea47SPavel Labath
131*2fe83274SKazu Hirata std::optional<Record::Kind> current_section;
132ed42ea47SPavel Labath offset_t section_start;
133ed42ea47SPavel Labath llvm::StringRef text = toStringRef(m_data.GetData());
134ed42ea47SPavel Labath uint32_t next_section_id = 1;
135ed42ea47SPavel Labath auto maybe_add_section = [&](const uint8_t *end_ptr) {
1362cf5486cSPavel Labath if (!current_section)
137ed42ea47SPavel Labath return; // We have been called before parsing the first line.
138ed42ea47SPavel Labath
139ed42ea47SPavel Labath offset_t end_offset = end_ptr - m_data.GetDataStart();
140ed42ea47SPavel Labath auto section_sp = std::make_shared<Section>(
141ed42ea47SPavel Labath GetModule(), this, next_section_id++,
1422cf5486cSPavel Labath ConstString(toString(*current_section)), eSectionTypeOther,
143ed42ea47SPavel Labath /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
144ed42ea47SPavel Labath end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
145d5b44036SJonas Devlieghere m_sections_up->AddSection(section_sp);
146ed42ea47SPavel Labath unified_section_list.AddSection(section_sp);
147ed42ea47SPavel Labath };
148ed42ea47SPavel Labath while (!text.empty()) {
149ed42ea47SPavel Labath llvm::StringRef line;
150ed42ea47SPavel Labath std::tie(line, text) = text.split('\n');
151ed42ea47SPavel Labath
152*2fe83274SKazu Hirata std::optional<Record::Kind> next_section = Record::classify(line);
153cc9ced0eSZequan Wu if (next_section == Record::Line || next_section == Record::Inline) {
154cc9ced0eSZequan Wu // Line/Inline records logically belong to the preceding Func record, so
155cc9ced0eSZequan Wu // we put them in the same section.
1562cf5486cSPavel Labath next_section = Record::Func;
157ed42ea47SPavel Labath }
1582cf5486cSPavel Labath if (next_section == current_section)
159ed42ea47SPavel Labath continue;
160ed42ea47SPavel Labath
161ed42ea47SPavel Labath // Changing sections, finish off the previous one, if there was any.
162ed42ea47SPavel Labath maybe_add_section(line.bytes_begin());
163ed42ea47SPavel Labath // And start a new one.
1642cf5486cSPavel Labath current_section = next_section;
165ed42ea47SPavel Labath section_start = line.bytes_begin() - m_data.GetDataStart();
166ed42ea47SPavel Labath }
167ed42ea47SPavel Labath // Finally, add the last section.
168ed42ea47SPavel Labath maybe_add_section(m_data.GetDataEnd());
1691f6b2477SPavel Labath }
170