1dda28197Spatrick //===-- ObjectFileBreakpad.cpp --------------------------------------------===//
2061da546Spatrick //
3061da546Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4061da546Spatrick // See https://llvm.org/LICENSE.txt for license information.
5061da546Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6061da546Spatrick //
7061da546Spatrick //===----------------------------------------------------------------------===//
8061da546Spatrick
9061da546Spatrick #include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
10061da546Spatrick #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
11061da546Spatrick #include "lldb/Core/ModuleSpec.h"
12061da546Spatrick #include "lldb/Core/PluginManager.h"
13061da546Spatrick #include "lldb/Core/Section.h"
14*f6aab3d8Srobert #include <optional>
15061da546Spatrick
16061da546Spatrick using namespace lldb;
17061da546Spatrick using namespace lldb_private;
18061da546Spatrick using namespace lldb_private::breakpad;
19061da546Spatrick
20dda28197Spatrick LLDB_PLUGIN_DEFINE(ObjectFileBreakpad)
21dda28197Spatrick
22061da546Spatrick namespace {
23061da546Spatrick struct Header {
24061da546Spatrick ArchSpec arch;
25061da546Spatrick UUID uuid;
26*f6aab3d8Srobert static std::optional<Header> parse(llvm::StringRef text);
27061da546Spatrick };
28061da546Spatrick } // namespace
29061da546Spatrick
parse(llvm::StringRef text)30*f6aab3d8Srobert std::optional<Header> Header::parse(llvm::StringRef text) {
31061da546Spatrick llvm::StringRef line;
32061da546Spatrick std::tie(line, text) = text.split('\n');
33061da546Spatrick auto Module = ModuleRecord::parse(line);
34061da546Spatrick if (!Module)
35*f6aab3d8Srobert return std::nullopt;
36061da546Spatrick
37061da546Spatrick llvm::Triple triple;
38061da546Spatrick triple.setArch(Module->Arch);
39061da546Spatrick triple.setOS(Module->OS);
40061da546Spatrick
41061da546Spatrick std::tie(line, text) = text.split('\n');
42061da546Spatrick
43061da546Spatrick auto Info = InfoRecord::parse(line);
44061da546Spatrick UUID uuid = Info && Info->ID ? Info->ID : Module->ID;
45061da546Spatrick return Header{ArchSpec(triple), std::move(uuid)};
46061da546Spatrick }
47061da546Spatrick
48061da546Spatrick char ObjectFileBreakpad::ID;
49061da546Spatrick
Initialize()50061da546Spatrick void ObjectFileBreakpad::Initialize() {
51061da546Spatrick PluginManager::RegisterPlugin(GetPluginNameStatic(),
52061da546Spatrick GetPluginDescriptionStatic(), CreateInstance,
53061da546Spatrick CreateMemoryInstance, GetModuleSpecifications);
54061da546Spatrick }
55061da546Spatrick
Terminate()56061da546Spatrick void ObjectFileBreakpad::Terminate() {
57061da546Spatrick PluginManager::UnregisterPlugin(CreateInstance);
58061da546Spatrick }
59061da546Spatrick
CreateInstance(const ModuleSP & module_sp,DataBufferSP data_sp,offset_t data_offset,const FileSpec * file,offset_t file_offset,offset_t length)60061da546Spatrick ObjectFile *ObjectFileBreakpad::CreateInstance(
61*f6aab3d8Srobert const ModuleSP &module_sp, DataBufferSP data_sp, offset_t data_offset,
62061da546Spatrick const FileSpec *file, offset_t file_offset, offset_t length) {
63061da546Spatrick if (!data_sp) {
64061da546Spatrick data_sp = MapFileData(*file, length, file_offset);
65061da546Spatrick if (!data_sp)
66061da546Spatrick return nullptr;
67061da546Spatrick data_offset = 0;
68061da546Spatrick }
69061da546Spatrick auto text = toStringRef(data_sp->GetData());
70*f6aab3d8Srobert std::optional<Header> header = Header::parse(text);
71061da546Spatrick if (!header)
72061da546Spatrick return nullptr;
73061da546Spatrick
74061da546Spatrick // Update the data to contain the entire file if it doesn't already
75061da546Spatrick if (data_sp->GetByteSize() < length) {
76061da546Spatrick data_sp = MapFileData(*file, length, file_offset);
77061da546Spatrick if (!data_sp)
78061da546Spatrick return nullptr;
79061da546Spatrick data_offset = 0;
80061da546Spatrick }
81061da546Spatrick
82061da546Spatrick return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,
83061da546Spatrick file_offset, length, std::move(header->arch),
84061da546Spatrick std::move(header->uuid));
85061da546Spatrick }
86061da546Spatrick
CreateMemoryInstance(const ModuleSP & module_sp,WritableDataBufferSP data_sp,const ProcessSP & process_sp,addr_t header_addr)87061da546Spatrick ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(
88*f6aab3d8Srobert const ModuleSP &module_sp, WritableDataBufferSP data_sp,
89061da546Spatrick const ProcessSP &process_sp, addr_t header_addr) {
90061da546Spatrick return nullptr;
91061da546Spatrick }
92061da546Spatrick
GetModuleSpecifications(const FileSpec & file,DataBufferSP & data_sp,offset_t data_offset,offset_t file_offset,offset_t length,ModuleSpecList & specs)93061da546Spatrick size_t ObjectFileBreakpad::GetModuleSpecifications(
94061da546Spatrick const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
95061da546Spatrick offset_t file_offset, offset_t length, ModuleSpecList &specs) {
96061da546Spatrick auto text = toStringRef(data_sp->GetData());
97*f6aab3d8Srobert std::optional<Header> header = Header::parse(text);
98061da546Spatrick if (!header)
99061da546Spatrick return 0;
100061da546Spatrick ModuleSpec spec(file, std::move(header->arch));
101061da546Spatrick spec.GetUUID() = std::move(header->uuid);
102061da546Spatrick specs.Append(spec);
103061da546Spatrick return 1;
104061da546Spatrick }
105061da546Spatrick
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)106061da546Spatrick ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,
107061da546Spatrick DataBufferSP &data_sp,
108061da546Spatrick offset_t data_offset,
109061da546Spatrick const FileSpec *file, offset_t offset,
110061da546Spatrick offset_t length, ArchSpec arch,
111061da546Spatrick UUID uuid)
112061da546Spatrick : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
113061da546Spatrick m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
114061da546Spatrick
ParseHeader()115061da546Spatrick bool ObjectFileBreakpad::ParseHeader() {
116061da546Spatrick // We already parsed the header during initialization.
117061da546Spatrick return true;
118061da546Spatrick }
119061da546Spatrick
ParseSymtab(Symtab & symtab)120*f6aab3d8Srobert void ObjectFileBreakpad::ParseSymtab(Symtab &symtab) {
121*f6aab3d8Srobert // Nothing to do for breakpad files, all information is parsed as debug info
122*f6aab3d8Srobert // which means "lldb_private::Function" objects are used, or symbols are added
123*f6aab3d8Srobert // by the SymbolFileBreakpad::AddSymbols(...) function in the symbol file.
124061da546Spatrick }
125061da546Spatrick
CreateSections(SectionList & unified_section_list)126061da546Spatrick void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {
127061da546Spatrick if (m_sections_up)
128061da546Spatrick return;
129061da546Spatrick m_sections_up = std::make_unique<SectionList>();
130061da546Spatrick
131*f6aab3d8Srobert std::optional<Record::Kind> current_section;
132061da546Spatrick offset_t section_start;
133061da546Spatrick llvm::StringRef text = toStringRef(m_data.GetData());
134061da546Spatrick uint32_t next_section_id = 1;
135061da546Spatrick auto maybe_add_section = [&](const uint8_t *end_ptr) {
136061da546Spatrick if (!current_section)
137061da546Spatrick return; // We have been called before parsing the first line.
138061da546Spatrick
139061da546Spatrick offset_t end_offset = end_ptr - m_data.GetDataStart();
140061da546Spatrick auto section_sp = std::make_shared<Section>(
141061da546Spatrick GetModule(), this, next_section_id++,
142061da546Spatrick ConstString(toString(*current_section)), eSectionTypeOther,
143061da546Spatrick /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
144061da546Spatrick end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
145061da546Spatrick m_sections_up->AddSection(section_sp);
146061da546Spatrick unified_section_list.AddSection(section_sp);
147061da546Spatrick };
148061da546Spatrick while (!text.empty()) {
149061da546Spatrick llvm::StringRef line;
150061da546Spatrick std::tie(line, text) = text.split('\n');
151061da546Spatrick
152*f6aab3d8Srobert std::optional<Record::Kind> next_section = Record::classify(line);
153*f6aab3d8Srobert if (next_section == Record::Line || next_section == Record::Inline) {
154*f6aab3d8Srobert // Line/Inline records logically belong to the preceding Func record, so
155*f6aab3d8Srobert // we put them in the same section.
156061da546Spatrick next_section = Record::Func;
157061da546Spatrick }
158061da546Spatrick if (next_section == current_section)
159061da546Spatrick continue;
160061da546Spatrick
161061da546Spatrick // Changing sections, finish off the previous one, if there was any.
162061da546Spatrick maybe_add_section(line.bytes_begin());
163061da546Spatrick // And start a new one.
164061da546Spatrick current_section = next_section;
165061da546Spatrick section_start = line.bytes_begin() - m_data.GetDataStart();
166061da546Spatrick }
167061da546Spatrick // Finally, add the last section.
168061da546Spatrick maybe_add_section(m_data.GetDataEnd());
169061da546Spatrick }
170