Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 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 | |
Zachary Turner | ce386e3 | 2019-01-11 18:35:58 +0000 | [diff] [blame] | 118 | size_t SymbolFileBreakpad::ParseFunctions(CompileUnit &comp_unit) { |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 119 | // TODO |
| 120 | return 0; |
| 121 | } |
| 122 | |
Zachary Turner | ce386e3 | 2019-01-11 18:35:58 +0000 | [diff] [blame] | 123 | bool SymbolFileBreakpad::ParseLineTable(CompileUnit &comp_unit) { |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 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( |
Zachary Turner | 576495e | 2019-01-14 22:41:21 +0000 | [diff] [blame] | 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) { |
Pavel Labath | 1cf23e1 | 2019-01-11 11:17:51 +0000 | [diff] [blame] | 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 | // 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); |
| 189 | if (token == "m") |
| 190 | std::tie(token, line) = getToken(line); |
| 191 | |
| 192 | addr_t address; |
| 193 | if (!to_integer(token, address, 16)) |
| 194 | continue; |
| 195 | address += base; |
| 196 | |
| 197 | // skip param_size |
| 198 | line = getToken(line).second; |
| 199 | |
| 200 | llvm::StringRef name = line.trim(); |
| 201 | |
| 202 | SectionSP section_sp = list.FindSectionContainingFileAddress(address); |
| 203 | if (!section_sp) { |
| 204 | LLDB_LOG(log, |
| 205 | "Ignoring symbol {0}, whose address ({1}) is outside of the " |
| 206 | "object file. Mismatched symbol file?", |
| 207 | name, address); |
| 208 | continue; |
| 209 | } |
| 210 | |
| 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, |
| 217 | /*flags*/ 0)); |
| 218 | } |
| 219 | |
| 220 | // TODO: Process FUNC records as well. |
| 221 | |
| 222 | symtab.CalculateSymbolSizes(); |
| 223 | } |