LLDB mainline
ObjectFileBreakpad.cpp
Go to the documentation of this file.
1//===-- ObjectFileBreakpad.cpp --------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
13#include "lldb/Core/Section.h"
14#include <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::breakpad;
19
21
22namespace {
23struct Header {
24 ArchSpec arch;
25 UUID uuid;
26 static std::optional<Header> parse(llvm::StringRef text);
27};
28} // namespace
29
30std::optional<Header> Header::parse(llvm::StringRef text) {
31 llvm::StringRef line;
32 std::tie(line, text) = text.split('\n');
33 auto Module = ModuleRecord::parse(line);
34 if (!Module)
35 return std::nullopt;
36
37 llvm::Triple triple;
38 triple.setArch(Module->Arch);
39 triple.setOS(Module->OS);
40
41 std::tie(line, text) = text.split('\n');
42
43 auto Info = InfoRecord::parse(line);
44 UUID uuid = Info && Info->ID ? Info->ID : Module->ID;
45 return Header{ArchSpec(triple), std::move(uuid)};
46}
47
49
55
59
61 DataExtractorSP extractor_sp,
62 offset_t data_offset,
63 const FileSpec *file,
64 offset_t file_offset,
65 offset_t length) {
66 if (!extractor_sp || !extractor_sp->HasData()) {
67 DataBufferSP data_sp = MapFileData(*file, length, file_offset);
68 if (!data_sp)
69 return nullptr;
70 extractor_sp = std::make_shared<DataExtractor>(data_sp);
71 data_offset = 0;
72 }
73 // If this is operating on a VirtualDataExtractor, it can have
74 // gaps between valid bytes in the DataBuffer. We extract an
75 // ArrayRef of the raw bytes, and can segfault.
76 DataExtractorSP contiguous_extractor_sp =
77 extractor_sp->GetContiguousDataExtractorSP();
78 auto text = toStringRef(contiguous_extractor_sp->GetData());
79 std::optional<Header> header = Header::parse(text);
80 if (!header)
81 return nullptr;
82
83 // Update the data to contain the entire file if it doesn't already
84 if (contiguous_extractor_sp->GetByteSize() < length) {
85 DataBufferSP data_sp = MapFileData(*file, length, file_offset);
86 data_sp = MapFileData(*file, length, file_offset);
87 if (!data_sp)
88 return nullptr;
89 contiguous_extractor_sp = std::make_shared<DataExtractor>(data_sp);
90 data_offset = 0;
91 }
92
93 return new ObjectFileBreakpad(
94 module_sp, contiguous_extractor_sp, data_offset, file, file_offset,
95 length, std::move(header->arch), std::move(header->uuid));
96}
97
99 const ModuleSP &module_sp, WritableDataBufferSP data_sp,
100 const ProcessSP &process_sp, addr_t header_addr) {
101 return nullptr;
102}
103
105 const FileSpec &file, DataExtractorSP &extractor_sp, offset_t file_offset,
106 offset_t length) {
107 if (!extractor_sp || !extractor_sp->HasData())
108 return {};
109 // If this is opearting on a VirtualDataExtractor, it can have
110 // gaps between valid bytes in the DataBuffer. We extract an
111 // ArrayRef of the raw bytes, and can segfault.
112 DataExtractorSP contiguous_extractor_sp =
113 extractor_sp->GetContiguousDataExtractorSP();
114 auto text = toStringRef(contiguous_extractor_sp->GetData());
115 std::optional<Header> header = Header::parse(text);
116 if (!header)
117 return {};
118 ModuleSpec spec(file, std::move(header->arch));
119 spec.GetUUID() = std::move(header->uuid);
120 ModuleSpecList specs;
121 specs.Append(spec);
122 return specs;
123}
124
126 DataExtractorSP extractor_sp,
127 offset_t data_offset,
128 const FileSpec *file, offset_t offset,
129 offset_t length, ArchSpec arch,
130 UUID uuid)
131 : ObjectFile(module_sp, file, offset, length, extractor_sp, data_offset),
132 m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
133
135 // We already parsed the header during initialization.
136 return true;
137}
138
140 // Nothing to do for breakpad files, all information is parsed as debug info
141 // which means "lldb_private::Function" objects are used, or symbols are added
142 // by the SymbolFileBreakpad::AddSymbols(...) function in the symbol file.
143}
144
146 if (m_sections_up)
147 return;
148 m_sections_up = std::make_unique<SectionList>();
149
150 std::optional<Record::Kind> current_section;
151 offset_t section_start;
152 llvm::StringRef text = toStringRef(m_data_nsp->GetData());
153 uint32_t next_section_id = 1;
154 auto maybe_add_section = [&](const uint8_t *end_ptr) {
155 if (!current_section)
156 return; // We have been called before parsing the first line.
157
158 offset_t end_offset = end_ptr - m_data_nsp->GetDataStart();
159 auto section_sp = std::make_shared<Section>(
160 GetModule(), this, next_section_id++,
161 ConstString(toString(*current_section)), eSectionTypeOther,
162 /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
163 end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
164 m_sections_up->AddSection(section_sp);
165 unified_section_list.AddSection(section_sp);
166 };
167 while (!text.empty()) {
168 llvm::StringRef line;
169 std::tie(line, text) = text.split('\n');
170
171 std::optional<Record::Kind> next_section = Record::classify(line);
172 if (next_section == Record::Line || next_section == Record::Inline) {
173 // Line/Inline records logically belong to the preceding Func record, so
174 // we put them in the same section.
175 next_section = Record::Func;
176 }
177 if (next_section == current_section)
178 continue;
179
180 // Changing sections, finish off the previous one, if there was any.
181 maybe_add_section(line.bytes_begin());
182 // And start a new one.
183 current_section = next_section;
184 section_start = line.bytes_begin() - m_data_nsp->GetDataStart();
185 }
186 // Finally, add the last section.
187 maybe_add_section(m_data_nsp->GetDataEnd());
188}
#define LLDB_PLUGIN_DEFINE(PluginName)
An architecture specification class.
Definition ArchSpec.h:32
A uniqued constant string class.
Definition ConstString.h:40
A file utility class.
Definition FileSpec.h:57
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
void Append(const ModuleSpec &spec)
Definition ModuleSpec.h:341
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition ObjectFile.h:774
static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size, uint64_t Offset)
DataExtractorNSP m_data_nsp
The data for this object file so things can be parsed lazily.
Definition ObjectFile.h:767
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
size_t AddSection(const lldb::SectionSP &section_sp)
Definition Section.cpp:483
Represents UUID's of various sizes.
Definition UUID.h:27
static std::optional< InfoRecord > parse(llvm::StringRef Line)
static std::optional< ModuleRecord > parse(llvm::StringRef Line)
static ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
ObjectFileBreakpad(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t offset, lldb::offset_t length, ArchSpec arch, UUID uuid)
void ParseSymtab(lldb_private::Symtab &symtab) override
Parse the symbol table into the provides symbol table object.
bool ParseHeader() override
Attempts to parse the object header.
static ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
void CreateSections(SectionList &unified_section_list) override
static ModuleSpecList GetModuleSpecifications(const FileSpec &file, lldb::DataExtractorSP &extractor_sp, lldb::offset_t file_offset, lldb::offset_t length)
static std::optional< Kind > classify(llvm::StringRef Line)
Attempt to guess the kind of the record present in the argument without doing a full parse.
llvm::StringRef toString(Record::Kind K)
A class that represents a running process on the host machine.
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP