1*0b57cec5SDimitry Andric //===-- ObjectFileBreakpad.cpp --------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
10*0b57cec5SDimitry Andric #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
11*0b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
12*0b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
13*0b57cec5SDimitry Andric #include "lldb/Core/Section.h"
14*0b57cec5SDimitry Andric #include <optional>
15*0b57cec5SDimitry Andric
16*0b57cec5SDimitry Andric using namespace lldb;
17*0b57cec5SDimitry Andric using namespace lldb_private;
18*0b57cec5SDimitry Andric using namespace lldb_private::breakpad;
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric LLDB_PLUGIN_DEFINE(ObjectFileBreakpad)
21*0b57cec5SDimitry Andric
22*0b57cec5SDimitry Andric namespace {
23*0b57cec5SDimitry Andric struct Header {
24*0b57cec5SDimitry Andric ArchSpec arch;
25*0b57cec5SDimitry Andric UUID uuid;
26*0b57cec5SDimitry Andric static std::optional<Header> parse(llvm::StringRef text);
27*0b57cec5SDimitry Andric };
28*0b57cec5SDimitry Andric } // namespace
29*0b57cec5SDimitry Andric
parse(llvm::StringRef text)30*0b57cec5SDimitry Andric std::optional<Header> Header::parse(llvm::StringRef text) {
31*0b57cec5SDimitry Andric llvm::StringRef line;
32*0b57cec5SDimitry Andric std::tie(line, text) = text.split('\n');
33*0b57cec5SDimitry Andric auto Module = ModuleRecord::parse(line);
34*0b57cec5SDimitry Andric if (!Module)
35*0b57cec5SDimitry Andric return std::nullopt;
36*0b57cec5SDimitry Andric
37*0b57cec5SDimitry Andric llvm::Triple triple;
38*0b57cec5SDimitry Andric triple.setArch(Module->Arch);
39*0b57cec5SDimitry Andric triple.setOS(Module->OS);
40*0b57cec5SDimitry Andric
41*0b57cec5SDimitry Andric std::tie(line, text) = text.split('\n');
42*0b57cec5SDimitry Andric
43*0b57cec5SDimitry Andric auto Info = InfoRecord::parse(line);
44*0b57cec5SDimitry Andric UUID uuid = Info && Info->ID ? Info->ID : Module->ID;
45*0b57cec5SDimitry Andric return Header{ArchSpec(triple), std::move(uuid)};
46*0b57cec5SDimitry Andric }
47*0b57cec5SDimitry Andric
48*0b57cec5SDimitry Andric char ObjectFileBreakpad::ID;
49*0b57cec5SDimitry Andric
Initialize()50*0b57cec5SDimitry Andric void ObjectFileBreakpad::Initialize() {
51*0b57cec5SDimitry Andric PluginManager::RegisterPlugin(GetPluginNameStatic(),
52*0b57cec5SDimitry Andric GetPluginDescriptionStatic(), CreateInstance,
53*0b57cec5SDimitry Andric CreateMemoryInstance, GetModuleSpecifications);
54*0b57cec5SDimitry Andric }
55*0b57cec5SDimitry Andric
Terminate()56*0b57cec5SDimitry Andric void ObjectFileBreakpad::Terminate() {
57*0b57cec5SDimitry Andric PluginManager::UnregisterPlugin(CreateInstance);
58*0b57cec5SDimitry Andric }
59*0b57cec5SDimitry Andric
CreateInstance(const ModuleSP & module_sp,DataBufferSP data_sp,offset_t data_offset,const FileSpec * file,offset_t file_offset,offset_t length)60*0b57cec5SDimitry Andric ObjectFile *ObjectFileBreakpad::CreateInstance(
61*0b57cec5SDimitry Andric const ModuleSP &module_sp, DataBufferSP data_sp, offset_t data_offset,
62*0b57cec5SDimitry Andric const FileSpec *file, offset_t file_offset, offset_t length) {
63*0b57cec5SDimitry Andric if (!data_sp) {
64*0b57cec5SDimitry Andric data_sp = MapFileData(*file, length, file_offset);
65*0b57cec5SDimitry Andric if (!data_sp)
66*0b57cec5SDimitry Andric return nullptr;
67*0b57cec5SDimitry Andric data_offset = 0;
68*0b57cec5SDimitry Andric }
69*0b57cec5SDimitry Andric auto text = toStringRef(data_sp->GetData());
70*0b57cec5SDimitry Andric std::optional<Header> header = Header::parse(text);
71*0b57cec5SDimitry Andric if (!header)
72*0b57cec5SDimitry Andric return nullptr;
73*0b57cec5SDimitry Andric
74*0b57cec5SDimitry Andric // Update the data to contain the entire file if it doesn't already
75*0b57cec5SDimitry Andric if (data_sp->GetByteSize() < length) {
76*0b57cec5SDimitry Andric data_sp = MapFileData(*file, length, file_offset);
77*0b57cec5SDimitry Andric if (!data_sp)
78*0b57cec5SDimitry Andric return nullptr;
79*0b57cec5SDimitry Andric data_offset = 0;
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric
82*0b57cec5SDimitry Andric return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,
83*0b57cec5SDimitry Andric file_offset, length, std::move(header->arch),
84*0b57cec5SDimitry Andric std::move(header->uuid));
85*0b57cec5SDimitry Andric }
86*0b57cec5SDimitry Andric
CreateMemoryInstance(const ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,addr_t header_addr)87*0b57cec5SDimitry Andric ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(
88*0b57cec5SDimitry Andric const ModuleSP &module_sp, WritableDataBufferSP data_sp,
89*0b57cec5SDimitry Andric const ProcessSP &process_sp, addr_t header_addr) {
90*0b57cec5SDimitry Andric return nullptr;
91*0b57cec5SDimitry Andric }
92*0b57cec5SDimitry Andric
GetModuleSpecifications(const FileSpec & file,DataBufferSP & data_sp,offset_t data_offset,offset_t file_offset,offset_t length,ModuleSpecList & specs)93*0b57cec5SDimitry Andric size_t ObjectFileBreakpad::GetModuleSpecifications(
94*0b57cec5SDimitry Andric const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
95*0b57cec5SDimitry Andric offset_t file_offset, offset_t length, ModuleSpecList &specs) {
96*0b57cec5SDimitry Andric auto text = toStringRef(data_sp->GetData());
97*0b57cec5SDimitry Andric std::optional<Header> header = Header::parse(text);
98*0b57cec5SDimitry Andric if (!header)
99*0b57cec5SDimitry Andric return 0;
100*0b57cec5SDimitry Andric ModuleSpec spec(file, std::move(header->arch));
101*0b57cec5SDimitry Andric spec.GetUUID() = std::move(header->uuid);
102*0b57cec5SDimitry Andric specs.Append(spec);
103*0b57cec5SDimitry Andric return 1;
104*0b57cec5SDimitry Andric }
105*0b57cec5SDimitry 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)106*0b57cec5SDimitry Andric ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,
107*0b57cec5SDimitry Andric DataBufferSP &data_sp,
108*0b57cec5SDimitry Andric offset_t data_offset,
109*0b57cec5SDimitry Andric const FileSpec *file, offset_t offset,
110*0b57cec5SDimitry Andric offset_t length, ArchSpec arch,
111*0b57cec5SDimitry Andric UUID uuid)
112*0b57cec5SDimitry Andric : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
113*0b57cec5SDimitry Andric m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
114*0b57cec5SDimitry Andric
ParseHeader()115*0b57cec5SDimitry Andric bool ObjectFileBreakpad::ParseHeader() {
116*0b57cec5SDimitry Andric // We already parsed the header during initialization.
117*0b57cec5SDimitry Andric return true;
118*0b57cec5SDimitry Andric }
119*0b57cec5SDimitry Andric
ParseSymtab(Symtab & symtab)120*0b57cec5SDimitry Andric void ObjectFileBreakpad::ParseSymtab(Symtab &symtab) {
121*0b57cec5SDimitry Andric // Nothing to do for breakpad files, all information is parsed as debug info
122*0b57cec5SDimitry Andric // which means "lldb_private::Function" objects are used, or symbols are added
123*0b57cec5SDimitry Andric // by the SymbolFileBreakpad::AddSymbols(...) function in the symbol file.
124*0b57cec5SDimitry Andric }
125*0b57cec5SDimitry Andric
CreateSections(SectionList & unified_section_list)126*0b57cec5SDimitry Andric void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {
127*0b57cec5SDimitry Andric if (m_sections_up)
128*0b57cec5SDimitry Andric return;
129*0b57cec5SDimitry Andric m_sections_up = std::make_unique<SectionList>();
130*0b57cec5SDimitry Andric
131*0b57cec5SDimitry Andric std::optional<Record::Kind> current_section;
132*0b57cec5SDimitry Andric offset_t section_start;
133*0b57cec5SDimitry Andric llvm::StringRef text = toStringRef(m_data.GetData());
134*0b57cec5SDimitry Andric uint32_t next_section_id = 1;
135*0b57cec5SDimitry Andric auto maybe_add_section = [&](const uint8_t *end_ptr) {
136*0b57cec5SDimitry Andric if (!current_section)
137*0b57cec5SDimitry Andric return; // We have been called before parsing the first line.
138*0b57cec5SDimitry Andric
139*0b57cec5SDimitry Andric offset_t end_offset = end_ptr - m_data.GetDataStart();
140*0b57cec5SDimitry Andric auto section_sp = std::make_shared<Section>(
141*0b57cec5SDimitry Andric GetModule(), this, next_section_id++,
142*0b57cec5SDimitry Andric ConstString(toString(*current_section)), eSectionTypeOther,
143*0b57cec5SDimitry Andric /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
144*0b57cec5SDimitry Andric end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
145*0b57cec5SDimitry Andric m_sections_up->AddSection(section_sp);
146*0b57cec5SDimitry Andric unified_section_list.AddSection(section_sp);
147*0b57cec5SDimitry Andric };
148*0b57cec5SDimitry Andric while (!text.empty()) {
149*0b57cec5SDimitry Andric llvm::StringRef line;
150*0b57cec5SDimitry Andric std::tie(line, text) = text.split('\n');
151*0b57cec5SDimitry Andric
152*0b57cec5SDimitry Andric std::optional<Record::Kind> next_section = Record::classify(line);
153*0b57cec5SDimitry Andric if (next_section == Record::Line || next_section == Record::Inline) {
154*0b57cec5SDimitry Andric // Line/Inline records logically belong to the preceding Func record, so
155*0b57cec5SDimitry Andric // we put them in the same section.
156*0b57cec5SDimitry Andric next_section = Record::Func;
157*0b57cec5SDimitry Andric }
158*0b57cec5SDimitry Andric if (next_section == current_section)
159*0b57cec5SDimitry Andric continue;
160*0b57cec5SDimitry Andric
161*0b57cec5SDimitry Andric // Changing sections, finish off the previous one, if there was any.
162*0b57cec5SDimitry Andric maybe_add_section(line.bytes_begin());
163*0b57cec5SDimitry Andric // And start a new one.
164*0b57cec5SDimitry Andric current_section = next_section;
165*0b57cec5SDimitry Andric section_start = line.bytes_begin() - m_data.GetDataStart();
166*0b57cec5SDimitry Andric }
167*0b57cec5SDimitry Andric // Finally, add the last section.
168*0b57cec5SDimitry Andric maybe_add_section(m_data.GetDataEnd());
169 }
170