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