blob: f895e9cbc2e58cc020cccfa436097ecd8851d91f [file] [log] [blame]
Pavel Labath1f6b2472018-12-10 17:16:38 +00001//===-- ObjectFileBreakpad.cpp -------------------------------- -*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Pavel Labath1f6b2472018-12-10 17:16:38 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
Pavel Labath2cf54862019-01-18 10:37:04 +000010#include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
Pavel Labath1f6b2472018-12-10 17:16:38 +000011#include "lldb/Core/ModuleSpec.h"
12#include "lldb/Core/PluginManager.h"
Pavel Labathed42ea42019-01-07 11:14:08 +000013#include "lldb/Core/Section.h"
Pavel Labath1f6b2472018-12-10 17:16:38 +000014
15using namespace lldb;
16using namespace lldb_private;
17using namespace lldb_private::breakpad;
18
19namespace {
20struct Header {
21 ArchSpec arch;
22 UUID uuid;
23 static llvm::Optional<Header> parse(llvm::StringRef text);
24};
25} // namespace
26
Pavel Labath1f6b2472018-12-10 17:16:38 +000027llvm::Optional<Header> Header::parse(llvm::StringRef text) {
Pavel Labath2cf54862019-01-18 10:37:04 +000028 llvm::StringRef line;
Pavel Labath1f6b2472018-12-10 17:16:38 +000029 std::tie(line, text) = text.split('\n');
Pavel Labath2cf54862019-01-18 10:37:04 +000030 auto Module = ModuleRecord::parse(line);
31 if (!Module)
Pavel Labath1f6b2472018-12-10 17:16:38 +000032 return llvm::None;
33
Pavel Labath1f6b2472018-12-10 17:16:38 +000034 llvm::Triple triple;
Pavel Labath5b18ddb2019-01-24 04:17:59 +000035 triple.setArch(Module->Arch);
36 triple.setOS(Module->OS);
Pavel Labath1f6b2472018-12-10 17:16:38 +000037
38 std::tie(line, text) = text.split('\n');
Pavel Labath1f6b2472018-12-10 17:16:38 +000039
Pavel Labath2cf54862019-01-18 10:37:04 +000040 auto Info = InfoRecord::parse(line);
Pavel Labath5b18ddb2019-01-24 04:17:59 +000041 UUID uuid = Info && Info->ID ? Info->ID : Module->ID;
Pavel Labath2cf54862019-01-18 10:37:04 +000042 return Header{ArchSpec(triple), std::move(uuid)};
Pavel Labath1f6b2472018-12-10 17:16:38 +000043}
44
Pavel Labathe84f7842019-07-31 11:57:34 +000045char ObjectFileBreakpad::ID;
46
Pavel Labath1f6b2472018-12-10 17:16:38 +000047void ObjectFileBreakpad::Initialize() {
48 PluginManager::RegisterPlugin(GetPluginNameStatic(),
49 GetPluginDescriptionStatic(), CreateInstance,
Pavel Labath871f2b62018-12-10 18:17:53 +000050 CreateMemoryInstance, GetModuleSpecifications);
Pavel Labath1f6b2472018-12-10 17:16:38 +000051}
52
53void ObjectFileBreakpad::Terminate() {
54 PluginManager::UnregisterPlugin(CreateInstance);
55}
56
57ConstString ObjectFileBreakpad::GetPluginNameStatic() {
58 static ConstString g_name("breakpad");
59 return g_name;
60}
61
62ObjectFile *ObjectFileBreakpad::CreateInstance(
63 const ModuleSP &module_sp, DataBufferSP &data_sp, offset_t data_offset,
64 const FileSpec *file, offset_t file_offset, offset_t length) {
65 if (!data_sp) {
66 data_sp = MapFileData(*file, length, file_offset);
67 if (!data_sp)
68 return nullptr;
69 data_offset = 0;
70 }
71 auto text = toStringRef(data_sp->GetData());
72 llvm::Optional<Header> header = Header::parse(text);
73 if (!header)
74 return nullptr;
75
76 // Update the data to contain the entire file if it doesn't already
77 if (data_sp->GetByteSize() < length) {
78 data_sp = MapFileData(*file, length, file_offset);
79 if (!data_sp)
80 return nullptr;
81 data_offset = 0;
82 }
83
84 return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,
85 file_offset, length, std::move(header->arch),
86 std::move(header->uuid));
87}
88
89ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(
90 const ModuleSP &module_sp, DataBufferSP &data_sp,
91 const ProcessSP &process_sp, addr_t header_addr) {
92 return nullptr;
93}
94
95size_t ObjectFileBreakpad::GetModuleSpecifications(
96 const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
97 offset_t file_offset, offset_t length, ModuleSpecList &specs) {
98 auto text = toStringRef(data_sp->GetData());
99 llvm::Optional<Header> header = Header::parse(text);
100 if (!header)
101 return 0;
102 ModuleSpec spec(file, std::move(header->arch));
103 spec.GetUUID() = std::move(header->uuid);
104 specs.Append(spec);
105 return 1;
106}
107
108ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,
109 DataBufferSP &data_sp,
110 offset_t data_offset,
111 const FileSpec *file, offset_t offset,
112 offset_t length, ArchSpec arch,
113 UUID uuid)
114 : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
115 m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
116
117bool ObjectFileBreakpad::ParseHeader() {
118 // We already parsed the header during initialization.
119 return true;
120}
121
122Symtab *ObjectFileBreakpad::GetSymtab() {
123 // TODO
124 return nullptr;
125}
126
Pavel Labath1f6b2472018-12-10 17:16:38 +0000127void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000128 if (m_sections_up)
Pavel Labathed42ea42019-01-07 11:14:08 +0000129 return;
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000130 m_sections_up = llvm::make_unique<SectionList>();
Pavel Labathed42ea42019-01-07 11:14:08 +0000131
Pavel Labath2cf54862019-01-18 10:37:04 +0000132 llvm::Optional<Record::Kind> current_section;
Pavel Labathed42ea42019-01-07 11:14:08 +0000133 offset_t section_start;
134 llvm::StringRef text = toStringRef(m_data.GetData());
135 uint32_t next_section_id = 1;
136 auto maybe_add_section = [&](const uint8_t *end_ptr) {
Pavel Labath2cf54862019-01-18 10:37:04 +0000137 if (!current_section)
Pavel Labathed42ea42019-01-07 11:14:08 +0000138 return; // We have been called before parsing the first line.
139
140 offset_t end_offset = end_ptr - m_data.GetDataStart();
141 auto section_sp = std::make_shared<Section>(
142 GetModule(), this, next_section_id++,
Pavel Labath2cf54862019-01-18 10:37:04 +0000143 ConstString(toString(*current_section)), eSectionTypeOther,
Pavel Labathed42ea42019-01-07 11:14:08 +0000144 /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
145 end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
Jonas Devlieghered5b44032019-02-13 06:25:41 +0000146 m_sections_up->AddSection(section_sp);
Pavel Labathed42ea42019-01-07 11:14:08 +0000147 unified_section_list.AddSection(section_sp);
148 };
149 while (!text.empty()) {
150 llvm::StringRef line;
151 std::tie(line, text) = text.split('\n');
152
Pavel Labathdfaafbc2019-04-04 13:23:25 +0000153 llvm::Optional<Record::Kind> next_section = Record::classify(line);
Pavel Labath2cf54862019-01-18 10:37:04 +0000154 if (next_section == Record::Line) {
155 // Line records logically belong to the preceding Func record, so we put
156 // them in the same section.
157 next_section = Record::Func;
Pavel Labathed42ea42019-01-07 11:14:08 +0000158 }
Pavel Labath2cf54862019-01-18 10:37:04 +0000159 if (next_section == current_section)
Pavel Labathed42ea42019-01-07 11:14:08 +0000160 continue;
161
162 // Changing sections, finish off the previous one, if there was any.
163 maybe_add_section(line.bytes_begin());
164 // And start a new one.
Pavel Labath2cf54862019-01-18 10:37:04 +0000165 current_section = next_section;
Pavel Labathed42ea42019-01-07 11:14:08 +0000166 section_start = line.bytes_begin() - m_data.GetDataStart();
167 }
168 // Finally, add the last section.
169 maybe_add_section(m_data.GetDataEnd());
Pavel Labath1f6b2472018-12-10 17:16:38 +0000170}