|
| 1 | +//===-- SymbolFileBreakpad.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/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" |
| 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 § = *list.GetSectionAtIndex(m_next_section_idx++); |
| 67 | + if (sect.GetName() != m_section_type) |
| 68 | + continue; |
| 69 | + DataExtractor data; |
| 70 | + m_obj->ReadSectionData(§, 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::ParseCompileUnitFunctions(const SymbolContext &sc) { |
| 119 | + // TODO |
| 120 | + return 0; |
| 121 | +} |
| 122 | + |
| 123 | +bool SymbolFileBreakpad::ParseCompileUnitLineTable(const SymbolContext &sc) { |
| 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 ®ex, |
| 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 SymbolContext &sc, const ConstString &name, |
| 157 | + const CompilerDeclContext *parent_decl_ctx, bool append, |
| 158 | + uint32_t max_matches, llvm::DenseSet<SymbolFile *> &searched_symbol_files, |
| 159 | + TypeMap &types) { |
| 160 | + if (!append) |
| 161 | + types.Clear(); |
| 162 | + return types.GetSize(); |
| 163 | +} |
| 164 | + |
| 165 | +size_t |
| 166 | +SymbolFileBreakpad::FindTypes(const std::vector<CompilerContext> &context, |
| 167 | + bool append, TypeMap &types) { |
| 168 | + if (!append) |
| 169 | + types.Clear(); |
| 170 | + return types.GetSize(); |
| 171 | +} |
| 172 | + |
| 173 | +void 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(); |
| 184 | + for (llvm::StringRef line : lines(*m_obj_file, ConstString("PUBLIC"))) { |
| 185 | + // PUBLIC [m] address param_size name |
| 186 | + // skip PUBLIC keyword |
| 187 | + line = getToken(line).second; |
| 188 | + llvm::StringRef token; |
| 189 | + std::tie(token, line) = getToken(line); |
| 190 | + if (token == "m") |
| 191 | + std::tie(token, line) = getToken(line); |
| 192 | + |
| 193 | + addr_t address; |
| 194 | + if (!to_integer(token, address, 16)) |
| 195 | + continue; |
| 196 | + address += base; |
| 197 | + |
| 198 | + // skip param_size |
| 199 | + line = getToken(line).second; |
| 200 | + |
| 201 | + llvm::StringRef name = line.trim(); |
| 202 | + |
| 203 | + SectionSP section_sp = list.FindSectionContainingFileAddress(address); |
| 204 | + if (!section_sp) { |
| 205 | + LLDB_LOG(log, |
| 206 | + "Ignoring symbol {0}, whose address ({1}) is outside of the " |
| 207 | + "object file. Mismatched symbol file?", |
| 208 | + name, address); |
| 209 | + continue; |
| 210 | + } |
| 211 | + |
| 212 | + symtab.AddSymbol(Symbol( |
| 213 | + /*symID*/ 0, Mangled(name, /*is_mangled*/ false), eSymbolTypeCode, |
| 214 | + /*is_global*/ true, /*is_debug*/ false, /*is_trampoline*/ false, |
| 215 | + /*is_artificial*/ false, |
| 216 | + AddressRange(section_sp, address - section_sp->GetFileAddress(), 0), |
| 217 | + /*size_is_valid*/ 0, /*contains_linker_annotations*/ false, |
| 218 | + /*flags*/ 0)); |
| 219 | + } |
| 220 | + |
| 221 | + // TODO: Process FUNC records as well. |
| 222 | + |
| 223 | + symtab.CalculateSymbolSizes(); |
| 224 | +} |
0 commit comments