1 //===-- SymbolFileBreakpad.cpp ----------------------------------*- C++ -*-===//
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 
9 #include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h"
10 #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h"
11 #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 
21 using namespace lldb;
22 using namespace lldb_private;
23 using namespace lldb_private::breakpad;
24 
25 namespace {
26 class LineIterator {
27 public:
28   // begin iterator for sections of given type
29   LineIterator(ObjectFile &obj, ConstString section_type)
30       : m_obj(&obj), m_section_type(section_type), m_next_section_idx(0) {
31     ++*this;
32   }
33 
34   // end iterator
35   explicit LineIterator(ObjectFile &obj)
36       : m_obj(&obj),
37         m_next_section_idx(m_obj->GetSectionList()->GetNumSections(0)) {}
38 
39   friend bool operator!=(const LineIterator &lhs, const LineIterator &rhs) {
40     assert(lhs.m_obj == rhs.m_obj);
41     if (lhs.m_next_section_idx != rhs.m_next_section_idx)
42       return true;
43     if (lhs.m_next_text.data() != rhs.m_next_text.data())
44       return true;
45     assert(lhs.m_current_text == rhs.m_current_text);
46     assert(rhs.m_next_text == rhs.m_next_text);
47     return false;
48   }
49 
50   const LineIterator &operator++();
51   llvm::StringRef operator*() const { return m_current_text; }
52 
53 private:
54   ObjectFile *m_obj;
55   ConstString m_section_type;
56   uint32_t m_next_section_idx;
57   llvm::StringRef m_current_text;
58   llvm::StringRef m_next_text;
59 };
60 } // namespace
61 
62 const LineIterator &LineIterator::operator++() {
63   const SectionList &list = *m_obj->GetSectionList();
64   size_t num_sections = list.GetNumSections(0);
65   while (m_next_text.empty() && m_next_section_idx < num_sections) {
66     Section &sect = *list.GetSectionAtIndex(m_next_section_idx++);
67     if (sect.GetName() != m_section_type)
68       continue;
69     DataExtractor data;
70     m_obj->ReadSectionData(&sect, data);
71     m_next_text =
72         llvm::StringRef(reinterpret_cast<const char *>(data.GetDataStart()),
73                         data.GetByteSize());
74   }
75   std::tie(m_current_text, m_next_text) = m_next_text.split('\n');
76   return *this;
77 }
78 
79 static llvm::iterator_range<LineIterator> lines(ObjectFile &obj,
80                                                 ConstString section_type) {
81   return llvm::make_range(LineIterator(obj, section_type), LineIterator(obj));
82 }
83 
84 void SymbolFileBreakpad::Initialize() {
85   PluginManager::RegisterPlugin(GetPluginNameStatic(),
86                                 GetPluginDescriptionStatic(), CreateInstance,
87                                 DebuggerInitialize);
88 }
89 
90 void SymbolFileBreakpad::Terminate() {
91   PluginManager::UnregisterPlugin(CreateInstance);
92 }
93 
94 ConstString SymbolFileBreakpad::GetPluginNameStatic() {
95   static ConstString g_name("breakpad");
96   return g_name;
97 }
98 
99 uint32_t SymbolFileBreakpad::CalculateAbilities() {
100   if (!m_obj_file)
101     return 0;
102   if (m_obj_file->GetPluginName() != ObjectFileBreakpad::GetPluginNameStatic())
103     return 0;
104 
105   return CompileUnits | Functions;
106 }
107 
108 uint32_t SymbolFileBreakpad::GetNumCompileUnits() {
109   // TODO
110   return 0;
111 }
112 
113 CompUnitSP SymbolFileBreakpad::ParseCompileUnitAtIndex(uint32_t index) {
114   // TODO
115   return nullptr;
116 }
117 
118 size_t SymbolFileBreakpad::ParseFunctions(CompileUnit &comp_unit) {
119   // TODO
120   return 0;
121 }
122 
123 bool SymbolFileBreakpad::ParseLineTable(CompileUnit &comp_unit) {
124   // TODO
125   return 0;
126 }
127 
128 uint32_t
129 SymbolFileBreakpad::ResolveSymbolContext(const Address &so_addr,
130                                          SymbolContextItem resolve_scope,
131                                          SymbolContext &sc) {
132   // TODO
133   return 0;
134 }
135 
136 uint32_t SymbolFileBreakpad::FindFunctions(
137     const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
138     FunctionNameType name_type_mask, bool include_inlines, bool append,
139     SymbolContextList &sc_list) {
140   // TODO
141   if (!append)
142     sc_list.Clear();
143   return sc_list.GetSize();
144 }
145 
146 uint32_t SymbolFileBreakpad::FindFunctions(const RegularExpression &regex,
147                                            bool include_inlines, bool append,
148                                            SymbolContextList &sc_list) {
149   // TODO
150   if (!append)
151     sc_list.Clear();
152   return sc_list.GetSize();
153 }
154 
155 uint32_t SymbolFileBreakpad::FindTypes(
156     const ConstString &name, const CompilerDeclContext *parent_decl_ctx,
157     bool append, uint32_t max_matches,
158     llvm::DenseSet<SymbolFile *> &searched_symbol_files, TypeMap &types) {
159   if (!append)
160     types.Clear();
161   return types.GetSize();
162 }
163 
164 size_t
165 SymbolFileBreakpad::FindTypes(const std::vector<CompilerContext> &context,
166                               bool append, TypeMap &types) {
167   if (!append)
168     types.Clear();
169   return types.GetSize();
170 }
171 
172 void SymbolFileBreakpad::AddSymbols(Symtab &symtab) {
173   Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_SYMBOLS);
174   Module &module = *m_obj_file->GetModule();
175   addr_t base = module.GetObjectFile()->GetBaseAddress().GetFileAddress();
176   if (base == LLDB_INVALID_ADDRESS) {
177     LLDB_LOG(log, "Unable to fetch the base address of object file. Skipping "
178                   "symtab population.");
179     return;
180   }
181 
182   const SectionList &list = *module.GetSectionList();
183   for (llvm::StringRef line : lines(*m_obj_file, ConstString("PUBLIC"))) {
184     auto record = PublicRecord::parse(line);
185     if (!record) {
186       LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", line);
187       continue;
188     }
189     addr_t file_address = base + record->getAddress();
190 
191     SectionSP section_sp = list.FindSectionContainingFileAddress(file_address);
192     if (!section_sp) {
193       LLDB_LOG(log,
194                "Ignoring symbol {0}, whose address ({1}) is outside of the "
195                "object file. Mismatched symbol file?",
196                record->getName(), file_address);
197       continue;
198     }
199 
200     symtab.AddSymbol(Symbol(
201         /*symID*/ 0, Mangled(record->getName(), /*is_mangled*/ false),
202         eSymbolTypeCode,
203         /*is_global*/ true, /*is_debug*/ false, /*is_trampoline*/ false,
204         /*is_artificial*/ false,
205         AddressRange(section_sp, file_address - section_sp->GetFileAddress(),
206                      0),
207         /*size_is_valid*/ 0, /*contains_linker_annotations*/ false,
208         /*flags*/ 0));
209   }
210 
211   // TODO: Process FUNC records as well.
212 
213   symtab.CalculateSymbolSizes();
214 }
215