Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 1 | //===-- SymbolFileBreakpad.cpp ----------------------------------*- C++ -*-===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Plugins/SymbolFile/Breakpad/SymbolFileBreakpad.h" |
Pavel Labath | 2cf5486 | 2019-01-18 10:37:04 +0000 | [diff] [blame] | 10 | #include "Plugins/ObjectFile/Breakpad/BreakpadRecords.h" |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 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 |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 29 | LineIterator(ObjectFile &obj, Record::Kind section_type) |
| 30 | : m_obj(&obj), m_section_type(toString(section_type)), |
| 31 | m_next_section_idx(0) { |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 32 | ++*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 | |
| 54 | private: |
| 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 | |
| 63 | const 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 § = *list.GetSectionAtIndex(m_next_section_idx++); |
| 68 | if (sect.GetName() != m_section_type) |
| 69 | continue; |
| 70 | DataExtractor data; |
| 71 | m_obj->ReadSectionData(§, 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 | |
| 80 | static llvm::iterator_range<LineIterator> lines(ObjectFile &obj, |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 81 | Record::Kind section_type) { |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 82 | return llvm::make_range(LineIterator(obj, section_type), LineIterator(obj)); |
| 83 | } |
| 84 | |
| 85 | void SymbolFileBreakpad::Initialize() { |
| 86 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 87 | GetPluginDescriptionStatic(), CreateInstance, |
| 88 | DebuggerInitialize); |
| 89 | } |
| 90 | |
| 91 | void SymbolFileBreakpad::Terminate() { |
| 92 | PluginManager::UnregisterPlugin(CreateInstance); |
| 93 | } |
| 94 | |
| 95 | ConstString SymbolFileBreakpad::GetPluginNameStatic() { |
| 96 | static ConstString g_name("breakpad"); |
| 97 | return g_name; |
| 98 | } |
| 99 | |
| 100 | uint32_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 | |
| 109 | uint32_t SymbolFileBreakpad::GetNumCompileUnits() { |
| 110 | // TODO |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | CompUnitSP SymbolFileBreakpad::ParseCompileUnitAtIndex(uint32_t index) { |
| 115 | // TODO |
| 116 | return nullptr; |
| 117 | } |
| 118 | |
Zachary Turner | ce386e3 | 2019-01-11 18:35:58 +0000 | [diff] [blame] | 119 | size_t SymbolFileBreakpad::ParseFunctions(CompileUnit &comp_unit) { |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 120 | // TODO |
| 121 | return 0; |
| 122 | } |
| 123 | |
Zachary Turner | ce386e3 | 2019-01-11 18:35:58 +0000 | [diff] [blame] | 124 | bool SymbolFileBreakpad::ParseLineTable(CompileUnit &comp_unit) { |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 125 | // TODO |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | uint32_t |
| 130 | SymbolFileBreakpad::ResolveSymbolContext(const Address &so_addr, |
| 131 | SymbolContextItem resolve_scope, |
| 132 | SymbolContext &sc) { |
| 133 | // TODO |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | uint32_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 | |
| 147 | uint32_t SymbolFileBreakpad::FindFunctions(const RegularExpression ®ex, |
| 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 | |
| 156 | uint32_t SymbolFileBreakpad::FindTypes( |
Zachary Turner | 576495e | 2019-01-14 22:41:21 +0000 | [diff] [blame] | 157 | 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 Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 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(); |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 184 | 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 Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 189 | 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 Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 193 | name, address); |
| 194 | return; |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 195 | } |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 196 | 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 Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 204 | |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 205 | for (llvm::StringRef line : lines(*m_obj_file, Record::Func)) { |
| 206 | if (auto record = FuncRecord::parse(line)) |
Pavel Labath | 5b18ddb | 2019-01-24 04:17:59 +0000 | [diff] [blame] | 207 | add_symbol(record->Address, record->Size, record->Name); |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 210 | for (llvm::StringRef line : lines(*m_obj_file, Record::Public)) { |
| 211 | if (auto record = PublicRecord::parse(line)) |
Pavel Labath | 5b18ddb | 2019-01-24 04:17:59 +0000 | [diff] [blame] | 212 | add_symbol(record->Address, llvm::None, record->Name); |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 213 | else |
| 214 | LLDB_LOG(log, "Failed to parse: {0}. Skipping record.", line); |
| 215 | } |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 216 | |
Pavel Labath | 06bb373 | 2019-01-22 04:56:31 +0000 | [diff] [blame] | 217 | for (auto &KV : symbols) |
| 218 | symtab.AddSymbol(std::move(KV.second)); |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 219 | symtab.CalculateSymbolSizes(); |
| 220 | } |