blob: 3d360b0c0cbd5a020b366107b79f4cf8fb172c38 [file] [log] [blame]
Pavel Labath1f6b2472018-12-10 17:16:38 +00001//===-- ObjectFileBreakpad.cpp -------------------------------- -*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
Pavel Labath2cf54862019-01-18 10:37:04 +000011#include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
Pavel Labath1f6b2472018-12-10 17:16:38 +000012#include "lldb/Core/ModuleSpec.h"
13#include "lldb/Core/PluginManager.h"
Pavel Labathed42ea42019-01-07 11:14:08 +000014#include "lldb/Core/Section.h"
Pavel Labath1f6b2472018-12-10 17:16:38 +000015
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::breakpad;
19
20namespace {
21struct Header {
22 ArchSpec arch;
23 UUID uuid;
24 static llvm::Optional<Header> parse(llvm::StringRef text);
25};
26} // namespace
27
Pavel Labath1f6b2472018-12-10 17:16:38 +000028llvm::Optional<Header> Header::parse(llvm::StringRef text) {
Pavel Labath2cf54862019-01-18 10:37:04 +000029 llvm::StringRef line;
Pavel Labath1f6b2472018-12-10 17:16:38 +000030 std::tie(line, text) = text.split('\n');
Pavel Labath2cf54862019-01-18 10:37:04 +000031 auto Module = ModuleRecord::parse(line);
32 if (!Module)
Pavel Labath1f6b2472018-12-10 17:16:38 +000033 return llvm::None;
34
Pavel Labath1f6b2472018-12-10 17:16:38 +000035 llvm::Triple triple;
Pavel Labath2cf54862019-01-18 10:37:04 +000036 triple.setArch(Module->getArch());
37 triple.setOS(Module->getOS());
Pavel Labath1f6b2472018-12-10 17:16:38 +000038
39 std::tie(line, text) = text.split('\n');
Pavel Labath1f6b2472018-12-10 17:16:38 +000040
Pavel Labath2cf54862019-01-18 10:37:04 +000041 auto Info = InfoRecord::parse(line);
42 UUID uuid = Info && Info->getID() ? Info->getID() : Module->getID();
43 return Header{ArchSpec(triple), std::move(uuid)};
Pavel Labath1f6b2472018-12-10 17:16:38 +000044}
45
46void ObjectFileBreakpad::Initialize() {
47 PluginManager::RegisterPlugin(GetPluginNameStatic(),
48 GetPluginDescriptionStatic(), CreateInstance,
Pavel Labath871f2b62018-12-10 18:17:53 +000049 CreateMemoryInstance, GetModuleSpecifications);
Pavel Labath1f6b2472018-12-10 17:16:38 +000050}
51
52void ObjectFileBreakpad::Terminate() {
53 PluginManager::UnregisterPlugin(CreateInstance);
54}
55
56ConstString ObjectFileBreakpad::GetPluginNameStatic() {
57 static ConstString g_name("breakpad");
58 return g_name;
59}
60
61ObjectFile *ObjectFileBreakpad::CreateInstance(
62 const ModuleSP &module_sp, DataBufferSP &data_sp, offset_t data_offset,
63 const FileSpec *file, offset_t file_offset, offset_t length) {
64 if (!data_sp) {
65 data_sp = MapFileData(*file, length, file_offset);
66 if (!data_sp)
67 return nullptr;
68 data_offset = 0;
69 }
70 auto text = toStringRef(data_sp->GetData());
71 llvm::Optional<Header> header = Header::parse(text);
72 if (!header)
73 return nullptr;
74
75 // Update the data to contain the entire file if it doesn't already
76 if (data_sp->GetByteSize() < length) {
77 data_sp = MapFileData(*file, length, file_offset);
78 if (!data_sp)
79 return nullptr;
80 data_offset = 0;
81 }
82
83 return new ObjectFileBreakpad(module_sp, data_sp, data_offset, file,
84 file_offset, length, std::move(header->arch),
85 std::move(header->uuid));
86}
87
88ObjectFile *ObjectFileBreakpad::CreateMemoryInstance(
89 const ModuleSP &module_sp, DataBufferSP &data_sp,
90 const ProcessSP &process_sp, addr_t header_addr) {
91 return nullptr;
92}
93
94size_t ObjectFileBreakpad::GetModuleSpecifications(
95 const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
96 offset_t file_offset, offset_t length, ModuleSpecList &specs) {
97 auto text = toStringRef(data_sp->GetData());
98 llvm::Optional<Header> header = Header::parse(text);
99 if (!header)
100 return 0;
101 ModuleSpec spec(file, std::move(header->arch));
102 spec.GetUUID() = std::move(header->uuid);
103 specs.Append(spec);
104 return 1;
105}
106
107ObjectFileBreakpad::ObjectFileBreakpad(const ModuleSP &module_sp,
108 DataBufferSP &data_sp,
109 offset_t data_offset,
110 const FileSpec *file, offset_t offset,
111 offset_t length, ArchSpec arch,
112 UUID uuid)
113 : ObjectFile(module_sp, file, offset, length, data_sp, data_offset),
114 m_arch(std::move(arch)), m_uuid(std::move(uuid)) {}
115
116bool ObjectFileBreakpad::ParseHeader() {
117 // We already parsed the header during initialization.
118 return true;
119}
120
121Symtab *ObjectFileBreakpad::GetSymtab() {
122 // TODO
123 return nullptr;
124}
125
Pavel Labath1f6b2472018-12-10 17:16:38 +0000126bool ObjectFileBreakpad::GetUUID(UUID *uuid) {
127 *uuid = m_uuid;
128 return true;
129}
130
131void ObjectFileBreakpad::CreateSections(SectionList &unified_section_list) {
Pavel Labathed42ea42019-01-07 11:14:08 +0000132 if (m_sections_ap)
133 return;
134 m_sections_ap = llvm::make_unique<SectionList>();
135
Pavel Labath2cf54862019-01-18 10:37:04 +0000136 llvm::Optional<Record::Kind> current_section;
Pavel Labathed42ea42019-01-07 11:14:08 +0000137 offset_t section_start;
138 llvm::StringRef text = toStringRef(m_data.GetData());
139 uint32_t next_section_id = 1;
140 auto maybe_add_section = [&](const uint8_t *end_ptr) {
Pavel Labath2cf54862019-01-18 10:37:04 +0000141 if (!current_section)
Pavel Labathed42ea42019-01-07 11:14:08 +0000142 return; // We have been called before parsing the first line.
143
144 offset_t end_offset = end_ptr - m_data.GetDataStart();
145 auto section_sp = std::make_shared<Section>(
146 GetModule(), this, next_section_id++,
Pavel Labath2cf54862019-01-18 10:37:04 +0000147 ConstString(toString(*current_section)), eSectionTypeOther,
Pavel Labathed42ea42019-01-07 11:14:08 +0000148 /*file_vm_addr*/ 0, /*vm_size*/ 0, section_start,
149 end_offset - section_start, /*log2align*/ 0, /*flags*/ 0);
150 m_sections_ap->AddSection(section_sp);
151 unified_section_list.AddSection(section_sp);
152 };
153 while (!text.empty()) {
154 llvm::StringRef line;
155 std::tie(line, text) = text.split('\n');
156
Pavel Labath2cf54862019-01-18 10:37:04 +0000157 Record::Kind next_section = Record::classify(line);
158 if (next_section == Record::Line) {
159 // Line records logically belong to the preceding Func record, so we put
160 // them in the same section.
161 next_section = Record::Func;
Pavel Labathed42ea42019-01-07 11:14:08 +0000162 }
Pavel Labath2cf54862019-01-18 10:37:04 +0000163 if (next_section == current_section)
Pavel Labathed42ea42019-01-07 11:14:08 +0000164 continue;
165
166 // Changing sections, finish off the previous one, if there was any.
167 maybe_add_section(line.bytes_begin());
168 // And start a new one.
Pavel Labath2cf54862019-01-18 10:37:04 +0000169 current_section = next_section;
Pavel Labathed42ea42019-01-07 11:14:08 +0000170 section_start = line.bytes_begin() - m_data.GetDataStart();
171 }
172 // Finally, add the last section.
173 maybe_add_section(m_data.GetDataEnd());
Pavel Labath1f6b2472018-12-10 17:16:38 +0000174}