blob: 37789fd3ec2b57f66247492d37f944ca8536245d [file] [log] [blame]
Pavel Labath1cf23e12019-01-11 11:17:51 +00001//===-- SymbolFileBreakpad.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 Labath1cf23e12019-01-11 11:17:51 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
Pavel Labath2cf54862019-01-18 10:37:04 +000010#include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
Pavel Labath1cf23e12019-01-11 11:17:51 +000011#include "Plugins/ObjectFile/Breakpad/ObjectFileBreakpad.h"
12#include "lldb/Core/Module.h"
13#include "lldb/Core/PluginManager.h"
14#include "lldb/Core/Section.h"
15#include "lldb/Host/FileSystem.h"
16#include "lldb/Symbol/ObjectFile.h"
17#include "lldb/Symbol/TypeMap.h"
18#include "lldb/Utility/Log.h"
19#include "llvm/ADT/StringExtras.h"
20
21using namespace lldb;
22using namespace lldb_private;
23using namespace lldb_private::breakpad;
24
25namespace {
26class LineIterator {
27public:
28 // begin iterator for sections of given type
Pavel Labath06bb3732019-01-22 04:56:31 +000029 LineIterator(ObjectFile &obj, Record::Kind section_type)
30 : m_obj(&obj), m_section_type(toString(section_type)),
31 m_next_section_idx(0) {
Pavel Labath1cf23e12019-01-11 11:17:51 +000032 ++*this;
33 }
34
35 // end iterator
36 explicit LineIterator(ObjectFile &obj)
37 : m_obj(&obj),
38 m_next_section_idx(m_obj->GetSectionList()->GetNumSections(0)) {}
39
40 friend bool operator!=(const LineIterator &lhs, const LineIterator &rhs) {
41 assert(lhs.m_obj == rhs.m_obj);
42 if (lhs.m_next_section_idx != rhs.m_next_section_idx)
43 return true;
44 if (lhs.m_next_text.data() != rhs.m_next_text.data())
45 return true;
46 assert(lhs.m_current_text == rhs.m_current_text);
47 assert(rhs.m_next_text == rhs.m_next_text);
48 return false;
49 }
50
51 const LineIterator &operator++();
52 llvm::StringRef operator*() const { return m_current_text; }
53
54private:
55 ObjectFile *m_obj;
56 ConstString m_section_type;
57 uint32_t m_next_section_idx;
58 llvm::StringRef m_current_text;
59 llvm::StringRef m_next_text;
60};
61} // namespace
62
63const LineIterator &LineIterator::operator++() {
64 const SectionList &list = *m_obj->GetSectionList();
65 size_t num_sections = list.GetNumSections(0);
66 while (m_next_text.empty() && m_next_section_idx < num_sections) {
67 Section &sect = *list.GetSectionAtIndex(m_next_section_idx++);
68 if (sect.GetName() != m_section_type)
69 continue;
70 DataExtractor data;
71 m_obj->ReadSectionData(&sect, data);
72 m_next_text =
73 llvm::StringRef(reinterpret_cast<const char *>(data.GetDataStart()),
74 data.GetByteSize());
75 }
76 std::tie(m_current_text, m_next_text) = m_next_text.split('\n');
77 return *this;
78}
79
80static llvm::iterator_range<LineIterator> lines(ObjectFile &obj,
Pavel Labath06bb3732019-01-22 04:56:31 +000081 Record::Kind section_type) {
Pavel Labath1cf23e12019-01-11 11:17:51 +000082 return llvm::make_range(LineIterator(obj, section_type), LineIterator(obj));
83}
84
85void SymbolFileBreakpad::Initialize() {
86 PluginManager::RegisterPlugin(GetPluginNameStatic(),
87 GetPluginDescriptionStatic(), CreateInstance,
88 DebuggerInitialize);
89}
90
91void SymbolFileBreakpad::Terminate() {
92 PluginManager::UnregisterPlugin(CreateInstance);
93}
94
95ConstString SymbolFileBreakpad::GetPluginNameStatic() {
96 static ConstString g_name("breakpad");
97 return g_name;
98}
99
100uint32_t SymbolFileBreakpad::CalculateAbilities() {
101 if (!m_obj_file)
102 return 0;
103 if (m_obj_file->GetPluginName() != ObjectFileBreakpad::GetPluginNameStatic())
104 return 0;
105
106 return CompileUnits | Functions;
107}
108
109uint32_t SymbolFileBreakpad::GetNumCompileUnits() {
110 // TODO
111 return 0;
112}
113
114CompUnitSP SymbolFileBreakpad::ParseCompileUnitAtIndex(uint32_t index) {
115 // TODO
116 return nullptr;
117}
118
Zachary Turnerce386e32019-01-11 18:35:58 +0000119size_t SymbolFileBreakpad::ParseFunctions(CompileUnit &comp_unit) {
Pavel Labath1cf23e12019-01-11 11:17:51 +0000120 // TODO
121 return 0;
122}
123
Zachary Turnerce386e32019-01-11 18:35:58 +0000124bool SymbolFileBreakpad::ParseLineTable(CompileUnit &comp_unit) {
Pavel Labath1cf23e12019-01-11 11:17:51 +0000125 // TODO
126 return 0;
127}
128
129uint32_t
130SymbolFileBreakpad::ResolveSymbolContext(const Address &so_addr,
131 SymbolContextItem resolve_scope,
132 SymbolContext &sc) {
133 // TODO
134 return 0;
135}
136
137uint32_t SymbolFileBreakpad::FindFunctions(
138 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
139 FunctionNameType name_type_mask, bool include_inlines, bool append,
140 SymbolContextList &sc_list) {
141 // TODO
142 if (!append)
143 sc_list.Clear();
144 return sc_list.GetSize();
145}
146
147uint32_t SymbolFileBreakpad::FindFunctions(const RegularExpression &regex,
148 bool include_inlines, bool append,
149 SymbolContextList &sc_list) {
150 // TODO
151 if (!append)
152 sc_list.Clear();
153 return sc_list.GetSize();
154}
155
156uint32_t SymbolFileBreakpad::FindTypes(
Zachary Turner576495e2019-01-14 22:41:21 +0000157 const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
158 bool append, uint32_t max_matches,
159 llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeMap &types) {
Pavel Labath1cf23e12019-01-11 11:17:51 +0000160 if (!append)
161 types.Clear();
162 return types.GetSize();
163}
164
165size_t
166SymbolFileBreakpad::FindTypes(const std::vector<CompilerContext> &context,
167 bool append, TypeMap &types) {
168 if (!append)
169 types.Clear();
170 return types.GetSize();
171}
172
173void SymbolFileBreakpad::AddSymbols(Symtab &symtab) {
174 Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS);
175 Module &module = *m_obj_file->GetModule();
176 addr_t base = module.GetObjectFile()->GetBaseAddress().GetFileAddress();
177 if (base == LLDB_INVALID_ADDRESS) {
178 LLDB_LOG(log, "Unable to fetch the base address of object file. Skipping "
179 "symtab population.");
180 return;
181 }
182
183 const SectionList &list = *module.GetSectionList();
Pavel Labath06bb3732019-01-22 04:56:31 +0000184 llvm::DenseMap<addr_t, Symbol> symbols;
185 auto add_symbol = [&](addr_t address, llvm::Optional<addr_t> size,
186 llvm::StringRef name) {
187 address += base;
188 SectionSP section_sp = list.FindSectionContainingFileAddress(address);
Pavel Labath1cf23e12019-01-11 11:17:51 +0000189 if (!section_sp) {
190 LLDB_LOG(log,
191 "Ignoring symbol {0}, whose address ({1}) is outside of the "
192 "object file. Mismatched symbol file?",
Pavel Labath06bb3732019-01-22 04:56:31 +0000193 name, address);
194 return;
Pavel Labath1cf23e12019-01-11 11:17:51 +0000195 }
Pavel Labath06bb3732019-01-22 04:56:31 +0000196 symbols.try_emplace(
197 address, /*symID*/ 0, Mangled(name, /*is_mangled*/ false),
198 eSymbolTypeCode, /*is_global*/ true, /*is_debug*/ false,
199 /*is_trampoline*/ false, /*is_artificial*/ false,
200 AddressRange(section_sp, address - section_sp->GetFileAddress(),
201 size.getValueOr(0)),
202 size.hasValue(), /*contains_linker_annotations*/ false, /*flags*/ 0);
203 };
Pavel Labath1cf23e12019-01-11 11:17:51 +0000204
Pavel Labath06bb3732019-01-22 04:56:31 +0000205 for (llvm::StringRef line : lines(*m_obj_file, Record::Func)) {
206 if (auto record = FuncRecord::parse(line))
207 add_symbol(record->getAddress(), record->getSize(), record->getName());
Pavel Labath1cf23e12019-01-11 11:17:51 +0000208 }
209
Pavel Labath06bb3732019-01-22 04:56:31 +0000210 for (llvm::StringRef line : lines(*m_obj_file, Record::Public)) {
211 if (auto record = PublicRecord::parse(line))
212 add_symbol(record->getAddress(), llvm::None, record->getName());
213 else
214 LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", line);
215 }
Pavel Labath1cf23e12019-01-11 11:17:51 +0000216
Pavel Labath06bb3732019-01-22 04:56:31 +0000217 for (auto &KV : symbols)
218 symtab.AddSymbol(std::move(KV.second));
Pavel Labath1cf23e12019-01-11 11:17:51 +0000219 symtab.CalculateSymbolSizes();
220}