1 //===-- SymbolFileBreakpad.cpp ----------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.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"
22 using namespace lldb_private;
23 using namespace lldb_private::breakpad;
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) {
35 explicit LineIterator(ObjectFile &obj)
37 m_next_section_idx(m_obj->GetSectionList()->GetNumSections(0)) {}
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)
43 if (lhs.m_next_text.data() != rhs.m_next_text.data())
45 assert(lhs.m_current_text == rhs.m_current_text);
46 assert(rhs.m_next_text == rhs.m_next_text);
50 const LineIterator &operator++();
51 llvm::StringRef operator*() const { return m_current_text; }
55 ConstString m_section_type;
56 uint32_t m_next_section_idx;
57 llvm::StringRef m_current_text;
58 llvm::StringRef m_next_text;
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 § = *list.GetSectionAtIndex(m_next_section_idx++);
67 if (sect.GetName() != m_section_type)
70 m_obj->ReadSectionData(§, data);
72 llvm::StringRef(reinterpret_cast<const char *>(data.GetDataStart()),
75 std::tie(m_current_text, m_next_text) = m_next_text.split('\n');
79 static llvm::iterator_range<LineIterator> lines(ObjectFile &obj,
80 ConstString section_type) {
81 return llvm::make_range(LineIterator(obj, section_type), LineIterator(obj));
84 void SymbolFileBreakpad::Initialize() {
85 PluginManager::RegisterPlugin(GetPluginNameStatic(),
86 GetPluginDescriptionStatic(), CreateInstance,
90 void SymbolFileBreakpad::Terminate() {
91 PluginManager::UnregisterPlugin(CreateInstance);
94 ConstString SymbolFileBreakpad::GetPluginNameStatic() {
95 static ConstString g_name("breakpad");
99 uint32_t SymbolFileBreakpad::CalculateAbilities() {
102 if (m_obj_file->GetPluginName() != ObjectFileBreakpad::GetPluginNameStatic())
105 return CompileUnits | Functions;
108 uint32_t SymbolFileBreakpad::GetNumCompileUnits() {
113 CompUnitSP SymbolFileBreakpad::ParseCompileUnitAtIndex(uint32_t index) {
118 size_t SymbolFileBreakpad::ParseFunctions(CompileUnit &comp_unit) {
123 bool SymbolFileBreakpad::ParseLineTable(CompileUnit &comp_unit) {
129 SymbolFileBreakpad::ResolveSymbolContext(const Address &so_addr,
130 SymbolContextItem resolve_scope,
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) {
143 return sc_list.GetSize();
146 uint32_t SymbolFileBreakpad::FindFunctions(const RegularExpression ®ex,
147 bool include_inlines, bool append,
148 SymbolContextList &sc_list) {
152 return sc_list.GetSize();
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) {
161 return types.GetSize();
165 SymbolFileBreakpad::FindTypes(const std::vector<CompilerContext> &context,
166 bool append, TypeMap &types) {
169 return types.GetSize();
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.");
182 const SectionList &list = *module.GetSectionList();
183 for (llvm::StringRef line : lines(*m_obj_file, ConstString("PUBLIC"))) {
184 // PUBLIC [m] address param_size name
185 // skip PUBLIC keyword
186 line = getToken(line).second;
187 llvm::StringRef token;
188 std::tie(token, line) = getToken(line);
190 std::tie(token, line) = getToken(line);
193 if (!to_integer(token, address, 16))
198 line = getToken(line).second;
200 llvm::StringRef name = line.trim();
202 SectionSP section_sp = list.FindSectionContainingFileAddress(address);
205 "Ignoring symbol {0}, whose address ({1}) is outside of the "
206 "object file. Mismatched symbol file?",
211 symtab.AddSymbol(Symbol(
212 /*symID*/ 0, Mangled(name, /*is_mangled*/ false), eSymbolTypeCode,
213 /*is_global*/ true, /*is_debug*/ false, /*is_trampoline*/ false,
214 /*is_artificial*/ false,
215 AddressRange(section_sp, address - section_sp->GetFileAddress(), 0),
216 /*size_is_valid*/ 0, /*contains_linker_annotations*/ false,
220 // TODO: Process FUNC records as well.
222 symtab.CalculateSymbolSizes();