Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1 | //===-- SymbolFilePDB.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 "SymbolFilePDB.h" |
| 11 | |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 12 | #include "clang/Lex/Lexer.h" |
| 13 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/PluginManager.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/ClangASTContext.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/CompileUnit.h" |
| 18 | #include "lldb/Symbol/LineTable.h" |
| 19 | #include "lldb/Symbol/ObjectFile.h" |
| 20 | #include "lldb/Symbol/SymbolContext.h" |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/SymbolVendor.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 22 | #include "lldb/Symbol/TypeMap.h" |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/TypeList.h" |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/RegularExpression.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 25 | |
Pavel Labath | b8d8c62 | 2016-05-09 11:07:43 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/PDB/GenericError.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/PDB/IPDBDataStream.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 28 | #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" |
| 29 | #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" |
| 30 | #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/PDB/IPDBTable.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/PDB/PDBSymbol.h" |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 35 | #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 37 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
| 38 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| 39 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" |
| 40 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 41 | #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" |
| 43 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" |
| 44 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
| 45 | |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 46 | #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 47 | #include "Plugins/SymbolFile/PDB/PDBASTParser.h" |
| 48 | |
| 49 | #include <regex> |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 50 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 51 | using namespace lldb; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 52 | using namespace lldb_private; |
Zachary Turner | 54fd7ff | 2016-05-04 20:33:53 +0000 | [diff] [blame] | 53 | using namespace llvm::pdb; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 54 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 55 | namespace { |
| 56 | lldb::LanguageType TranslateLanguage(PDB_Lang lang) { |
| 57 | switch (lang) { |
| 58 | case PDB_Lang::Cpp: |
| 59 | return lldb::LanguageType::eLanguageTypeC_plus_plus; |
| 60 | case PDB_Lang::C: |
| 61 | return lldb::LanguageType::eLanguageTypeC; |
| 62 | default: |
| 63 | return lldb::LanguageType::eLanguageTypeUnknown; |
| 64 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 67 | bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, |
| 68 | uint32_t addr_length) { |
| 69 | return ((requested_line == 0 || actual_line == requested_line) && |
| 70 | addr_length > 0); |
| 71 | } |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 72 | } // namespace |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 73 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 74 | void SymbolFilePDB::Initialize() { |
| 75 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 76 | GetPluginDescriptionStatic(), CreateInstance, |
| 77 | DebuggerInitialize); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | void SymbolFilePDB::Terminate() { |
| 81 | PluginManager::UnregisterPlugin(CreateInstance); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 84 | void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} |
| 85 | |
| 86 | lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { |
| 87 | static ConstString g_name("pdb"); |
| 88 | return g_name; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 91 | const char *SymbolFilePDB::GetPluginDescriptionStatic() { |
| 92 | return "Microsoft PDB debug symbol file reader."; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | lldb_private::SymbolFile * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 96 | SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { |
| 97 | return new SymbolFilePDB(obj_file); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 101 | : SymbolFile(object_file), m_session_up(), m_global_scope_up(), |
| 102 | m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {} |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 103 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 104 | SymbolFilePDB::~SymbolFilePDB() {} |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 105 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 106 | uint32_t SymbolFilePDB::CalculateAbilities() { |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 107 | uint32_t abilities = 0; |
| 108 | if (!m_obj_file) |
| 109 | return 0; |
| 110 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 111 | if (!m_session_up) { |
| 112 | // Lazily load and match the PDB file, but only do this once. |
| 113 | std::string exePath = m_obj_file->GetFileSpec().GetPath(); |
| 114 | auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), |
| 115 | m_session_up); |
| 116 | if (error) { |
| 117 | llvm::consumeError(std::move(error)); |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 118 | auto module_sp = m_obj_file->GetModule(); |
| 119 | if (!module_sp) |
| 120 | return 0; |
| 121 | // See if any symbol file is specified through `--symfile` option. |
| 122 | FileSpec symfile = module_sp->GetSymbolFileFileSpec(); |
| 123 | if (!symfile) |
| 124 | return 0; |
| 125 | error = loadDataForPDB(PDB_ReaderType::DIA, |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 126 | llvm::StringRef(symfile.GetPath()), m_session_up); |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 127 | if (error) { |
| 128 | llvm::consumeError(std::move(error)); |
| 129 | return 0; |
| 130 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 131 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 132 | } |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame^] | 133 | if (!m_session_up) |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 134 | return 0; |
| 135 | |
| 136 | auto enum_tables_up = m_session_up->getEnumTables(); |
| 137 | if (!enum_tables_up) |
| 138 | return 0; |
| 139 | while (auto table_up = enum_tables_up->getNext()) { |
| 140 | if (table_up->getItemCount() == 0) |
| 141 | continue; |
| 142 | auto type = table_up->getTableType(); |
| 143 | switch (type) { |
| 144 | case PDB_TableType::Symbols: |
| 145 | // This table represents a store of symbols with types listed in |
| 146 | // PDBSym_Type |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 147 | abilities |= (CompileUnits | Functions | Blocks | GlobalVariables | |
| 148 | LocalVariables | VariableTypes); |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 149 | break; |
| 150 | case PDB_TableType::LineNumbers: |
| 151 | abilities |= LineTables; |
| 152 | break; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 153 | default: |
| 154 | break; |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | return abilities; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | void SymbolFilePDB::InitializeObject() { |
| 161 | lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 162 | lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 163 | m_session_up->setLoadAddress(obj_load_address); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 164 | if (!m_global_scope_up) |
| 165 | m_global_scope_up = m_session_up->getGlobalScope(); |
| 166 | lldbassert(m_global_scope_up.get()); |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 167 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 168 | TypeSystem *type_system = |
| 169 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 170 | ClangASTContext *clang_type_system = |
| 171 | llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 172 | lldbassert(clang_type_system); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 173 | m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>( |
| 174 | type_system, clang_type_system->GetTranslationUnitDecl()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 177 | uint32_t SymbolFilePDB::GetNumCompileUnits() { |
| 178 | if (m_cached_compile_unit_count == 0) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 179 | auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 180 | if (!compilands) |
| 181 | return 0; |
| 182 | |
| 183 | // The linker could link *.dll (compiland language = LINK), or import |
| 184 | // *.dll. For example, a compiland with name `Import:KERNEL32.dll` |
| 185 | // could be found as a child of the global scope (PDB executable). |
| 186 | // Usually, such compilands contain `thunk` symbols in which we are not |
| 187 | // interested for now. However we still count them in the compiland list. |
| 188 | // If we perform any compiland related activity, like finding symbols |
| 189 | // through llvm::pdb::IPDBSession methods, such compilands will all be |
| 190 | // searched automatically no matter whether we include them or not. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | m_cached_compile_unit_count = compilands->getChildCount(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 192 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 193 | // The linker can inject an additional "dummy" compilation unit into the |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 194 | // PDB. Ignore this special compile unit for our purposes, if it is there. |
| 195 | // It is always the last one. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 196 | auto last_compiland_up = |
| 197 | compilands->getChildAtIndex(m_cached_compile_unit_count - 1); |
| 198 | lldbassert(last_compiland_up.get()); |
| 199 | std::string name = last_compiland_up->getName(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | if (name == "* Linker *") |
| 201 | --m_cached_compile_unit_count; |
| 202 | } |
| 203 | return m_cached_compile_unit_count; |
| 204 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 205 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 206 | void SymbolFilePDB::GetCompileUnitIndex( |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 207 | const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 208 | auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 209 | if (!results_up) |
| 210 | return; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 211 | auto uid = pdb_compiland.getSymIndexId(); |
Raphael Isemann | fbdf0b9 | 2018-01-22 06:56:09 +0000 | [diff] [blame] | 212 | for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 213 | auto compiland_up = results_up->getChildAtIndex(cu_idx); |
| 214 | if (!compiland_up) |
| 215 | continue; |
| 216 | if (compiland_up->getSymIndexId() == uid) { |
| 217 | index = cu_idx; |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | index = UINT32_MAX; |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | std::unique_ptr<llvm::pdb::PDBSymbolCompiland> |
| 226 | SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { |
| 227 | return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); |
| 228 | } |
| 229 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 230 | lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 231 | if (index >= GetNumCompileUnits()) |
| 232 | return CompUnitSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 233 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 234 | // Assuming we always retrieve same compilands listed in same order through |
| 235 | // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a |
| 236 | // compile unit makes no sense. |
| 237 | auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 238 | if (!results) |
| 239 | return CompUnitSP(); |
| 240 | auto compiland_up = results->getChildAtIndex(index); |
| 241 | if (!compiland_up) |
| 242 | return CompUnitSP(); |
| 243 | return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | lldb::LanguageType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 247 | SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) { |
| 248 | // What fields should I expect to be filled out on the SymbolContext? Is it |
| 249 | // safe to assume that `sc.comp_unit` is valid? |
| 250 | if (!sc.comp_unit) |
| 251 | return lldb::eLanguageTypeUnknown; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 252 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 253 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 254 | if (!compiland_up) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 255 | return lldb::eLanguageTypeUnknown; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 256 | auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 257 | if (!details) |
| 258 | return lldb::eLanguageTypeUnknown; |
| 259 | return TranslateLanguage(details->getLanguage()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 262 | lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc( |
| 263 | const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 264 | lldbassert(sc.comp_unit && sc.module_sp.get()); |
| 265 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 266 | auto file_vm_addr = pdb_func.getVirtualAddress(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 267 | if (file_vm_addr == LLDB_INVALID_ADDRESS) |
| 268 | return nullptr; |
| 269 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 270 | auto func_length = pdb_func.getLength(); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 271 | AddressRange func_range = |
| 272 | AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList()); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 273 | if (!func_range.GetBaseAddress().IsValid()) |
| 274 | return nullptr; |
| 275 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 276 | lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId()); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 277 | if (!func_type) |
| 278 | return nullptr; |
| 279 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 280 | user_id_t func_type_uid = pdb_func.getSignatureId(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 281 | |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 282 | Mangled mangled = GetMangledForPDBFunc(pdb_func); |
| 283 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 284 | FunctionSP func_sp = |
| 285 | std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(), |
| 286 | func_type_uid, mangled, func_type, func_range); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 287 | |
| 288 | sc.comp_unit->AddFunction(func_sp); |
| 289 | return func_sp.get(); |
| 290 | } |
| 291 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 292 | size_t SymbolFilePDB::ParseCompileUnitFunctions( |
| 293 | const lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 294 | lldbassert(sc.comp_unit); |
| 295 | size_t func_added = 0; |
| 296 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 297 | if (!compiland_up) |
| 298 | return 0; |
| 299 | auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); |
| 300 | if (!results_up) |
| 301 | return 0; |
| 302 | while (auto pdb_func_up = results_up->getNext()) { |
| 303 | auto func_sp = |
| 304 | sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId()); |
| 305 | if (!func_sp) { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 306 | if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc)) |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 307 | ++func_added; |
| 308 | } |
| 309 | } |
| 310 | return func_added; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 313 | bool SymbolFilePDB::ParseCompileUnitLineTable( |
| 314 | const lldb_private::SymbolContext &sc) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 315 | lldbassert(sc.comp_unit); |
| 316 | if (sc.comp_unit->GetLineTable()) |
| 317 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 318 | return ParseCompileUnitLineTable(sc, 0); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 321 | bool SymbolFilePDB::ParseCompileUnitDebugMacros( |
| 322 | const lldb_private::SymbolContext &sc) { |
| 323 | // PDB doesn't contain information about macros |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | bool SymbolFilePDB::ParseCompileUnitSupportFiles( |
| 328 | const lldb_private::SymbolContext &sc, |
| 329 | lldb_private::FileSpecList &support_files) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 330 | lldbassert(sc.comp_unit); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 331 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 332 | // In theory this is unnecessary work for us, because all of this information |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 333 | // is easily (and quickly) accessible from DebugInfoPDB, so caching it a |
| 334 | // second time seems like a waste. Unfortunately, there's no good way around |
| 335 | // this short of a moderate refactor since SymbolVendor depends on being able |
| 336 | // to cache this list. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 337 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 338 | if (!compiland_up) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 339 | return false; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 340 | auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | if (!files || files->getChildCount() == 0) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 342 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 343 | |
| 344 | while (auto file = files->getNext()) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 345 | FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows); |
| 346 | support_files.AppendIfUnique(spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 347 | } |
| 348 | return true; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 351 | bool SymbolFilePDB::ParseImportedModules( |
| 352 | const lldb_private::SymbolContext &sc, |
| 353 | std::vector<lldb_private::ConstString> &imported_modules) { |
| 354 | // PDB does not yet support module debug info |
| 355 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 358 | static size_t ParseFunctionBlocksForPDBSymbol( |
| 359 | const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr, |
| 360 | const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block, |
| 361 | bool is_top_parent) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 362 | assert(pdb_symbol && parent_block); |
| 363 | |
| 364 | size_t num_added = 0; |
| 365 | switch (pdb_symbol->getSymTag()) { |
| 366 | case PDB_SymType::Block: |
| 367 | case PDB_SymType::Function: { |
| 368 | Block *block = nullptr; |
| 369 | auto &raw_sym = pdb_symbol->getRawSymbol(); |
| 370 | if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) { |
| 371 | if (pdb_func->hasNoInlineAttribute()) |
| 372 | break; |
| 373 | if (is_top_parent) |
| 374 | block = parent_block; |
| 375 | else |
| 376 | break; |
| 377 | } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) { |
| 378 | auto uid = pdb_symbol->getSymIndexId(); |
| 379 | if (parent_block->FindBlockByID(uid)) |
| 380 | break; |
| 381 | if (raw_sym.getVirtualAddress() < func_file_vm_addr) |
| 382 | break; |
| 383 | |
| 384 | auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId()); |
| 385 | parent_block->AddChild(block_sp); |
| 386 | block = block_sp.get(); |
| 387 | } else |
| 388 | llvm_unreachable("Unexpected PDB symbol!"); |
| 389 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 390 | block->AddRange(Block::Range( |
| 391 | raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength())); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 392 | block->FinalizeRanges(); |
| 393 | ++num_added; |
| 394 | |
| 395 | auto results_up = pdb_symbol->findAllChildren(); |
| 396 | if (!results_up) |
| 397 | break; |
| 398 | while (auto symbol_up = results_up->getNext()) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 399 | num_added += ParseFunctionBlocksForPDBSymbol( |
| 400 | sc, func_file_vm_addr, symbol_up.get(), block, false); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 401 | } |
| 402 | } break; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 403 | default: |
| 404 | break; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 405 | } |
| 406 | return num_added; |
| 407 | } |
| 408 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 409 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 410 | SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 411 | lldbassert(sc.comp_unit && sc.function); |
| 412 | size_t num_added = 0; |
| 413 | auto uid = sc.function->GetID(); |
| 414 | auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); |
| 415 | if (!pdb_func_up) |
| 416 | return 0; |
| 417 | Block &parent_block = sc.function->GetBlock(false); |
| 418 | num_added = |
| 419 | ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(), |
| 420 | pdb_func_up.get(), &parent_block, true); |
| 421 | return num_added; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 422 | } |
| 423 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 424 | size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) { |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 425 | lldbassert(sc.module_sp.get()); |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 426 | if (!sc.comp_unit) |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 427 | return 0; |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 428 | |
| 429 | size_t num_added = 0; |
| 430 | auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 431 | if (!compiland) |
| 432 | return 0; |
| 433 | |
| 434 | auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { |
| 435 | std::unique_ptr<IPDBEnumSymbols> results; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 436 | PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, |
| 437 | PDB_SymType::UDT}; |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 438 | for (auto tag : tags_to_search) { |
| 439 | results = raw_sym.findAllChildren(tag); |
| 440 | if (!results || results->getChildCount() == 0) |
| 441 | continue; |
| 442 | while (auto symbol = results->getNext()) { |
| 443 | switch (symbol->getSymTag()) { |
| 444 | case PDB_SymType::Enum: |
| 445 | case PDB_SymType::UDT: |
| 446 | case PDB_SymType::Typedef: |
| 447 | break; |
| 448 | default: |
| 449 | continue; |
| 450 | } |
| 451 | |
| 452 | // This should cause the type to get cached and stored in the `m_types` |
| 453 | // lookup. |
| 454 | if (!ResolveTypeUID(symbol->getSymIndexId())) |
| 455 | continue; |
| 456 | |
| 457 | ++num_added; |
| 458 | } |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 459 | } |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 460 | }; |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 461 | |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 462 | if (sc.function) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 463 | auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( |
| 464 | sc.function->GetID()); |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 465 | if (!pdb_func) |
| 466 | return 0; |
| 467 | ParseTypesByTagFn(*pdb_func); |
| 468 | } else { |
| 469 | ParseTypesByTagFn(*compiland); |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 470 | |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 471 | // Also parse global types particularly coming from this compiland. |
| 472 | // Unfortunately, PDB has no compiland information for each global type. |
| 473 | // We have to parse them all. But ensure we only do this once. |
| 474 | static bool parse_all_global_types = false; |
| 475 | if (!parse_all_global_types) { |
| 476 | ParseTypesByTagFn(*m_global_scope_up); |
| 477 | parse_all_global_types = true; |
| 478 | } |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 479 | } |
| 480 | return num_added; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | size_t |
| 484 | SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { |
| 485 | // TODO: Implement this |
| 486 | return size_t(); |
| 487 | } |
| 488 | |
| 489 | lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { |
| 490 | auto find_result = m_types.find(type_uid); |
| 491 | if (find_result != m_types.end()) |
| 492 | return find_result->second.get(); |
| 493 | |
| 494 | TypeSystem *type_system = |
| 495 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 496 | ClangASTContext *clang_type_system = |
| 497 | llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
| 498 | if (!clang_type_system) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 499 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 500 | PDBASTParser *pdb = |
| 501 | llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser()); |
| 502 | if (!pdb) |
| 503 | return nullptr; |
| 504 | |
| 505 | auto pdb_type = m_session_up->getSymbolById(type_uid); |
| 506 | if (pdb_type == nullptr) |
| 507 | return nullptr; |
| 508 | |
| 509 | lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame^] | 510 | if (result) { |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 511 | m_types.insert(std::make_pair(type_uid, result)); |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 512 | auto type_list = GetTypeList(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 513 | if (type_list) |
| 514 | type_list->Insert(result); |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 515 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 516 | return result.get(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 519 | bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { |
| 520 | // TODO: Implement this |
| 521 | return false; |
| 522 | } |
| 523 | |
| 524 | lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { |
| 525 | return lldb_private::CompilerDecl(); |
| 526 | } |
| 527 | |
| 528 | lldb_private::CompilerDeclContext |
| 529 | SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { |
| 530 | // PDB always uses the translation unit decl context for everything. We can |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 531 | // improve this later but it's not easy because PDB doesn't provide a high |
| 532 | // enough level of type fidelity in this area. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 533 | return *m_tu_decl_ctx_up; |
| 534 | } |
| 535 | |
| 536 | lldb_private::CompilerDeclContext |
| 537 | SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { |
| 538 | return *m_tu_decl_ctx_up; |
| 539 | } |
| 540 | |
| 541 | void SymbolFilePDB::ParseDeclsForContext( |
| 542 | lldb_private::CompilerDeclContext decl_ctx) {} |
| 543 | |
| 544 | uint32_t |
| 545 | SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, |
| 546 | uint32_t resolve_scope, |
| 547 | lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 548 | uint32_t resolved_flags = 0; |
Pavel Labath | 4d4d63e | 2018-02-09 11:37:01 +0000 | [diff] [blame] | 549 | if (resolve_scope & eSymbolContextCompUnit || |
| 550 | resolve_scope & eSymbolContextVariable || |
| 551 | resolve_scope & eSymbolContextFunction || |
| 552 | resolve_scope & eSymbolContextBlock || |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 553 | resolve_scope & eSymbolContextLineEntry) { |
| 554 | addr_t file_vm_addr = so_addr.GetFileAddress(); |
| 555 | auto symbol_up = |
| 556 | m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::None); |
| 557 | if (!symbol_up) |
| 558 | return 0; |
| 559 | |
| 560 | auto cu_sp = GetCompileUnitContainsAddress(so_addr); |
| 561 | if (!cu_sp) { |
| 562 | if (resolved_flags | eSymbolContextVariable) { |
| 563 | // TODO: Resolve variables |
| 564 | } |
| 565 | return 0; |
| 566 | } |
| 567 | sc.comp_unit = cu_sp.get(); |
| 568 | resolved_flags |= eSymbolContextCompUnit; |
| 569 | lldbassert(sc.module_sp == cu_sp->GetModule()); |
| 570 | |
| 571 | switch (symbol_up->getSymTag()) { |
| 572 | case PDB_SymType::Function: |
| 573 | if (resolve_scope & eSymbolContextFunction) { |
| 574 | auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); |
| 575 | assert(pdb_func); |
| 576 | auto func_uid = pdb_func->getSymIndexId(); |
| 577 | sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); |
| 578 | if (sc.function == nullptr) |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 579 | sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 580 | if (sc.function) { |
| 581 | resolved_flags |= eSymbolContextFunction; |
| 582 | if (resolve_scope & eSymbolContextBlock) { |
| 583 | Block &block = sc.function->GetBlock(true); |
| 584 | sc.block = block.FindBlockByID(sc.function->GetID()); |
| 585 | if (sc.block) |
| 586 | resolved_flags |= eSymbolContextBlock; |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | break; |
| 591 | default: |
| 592 | break; |
| 593 | } |
| 594 | |
| 595 | if (resolve_scope & eSymbolContextLineEntry) { |
| 596 | if (auto *line_table = sc.comp_unit->GetLineTable()) { |
| 597 | Address addr(so_addr); |
| 598 | if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) |
| 599 | resolved_flags |= eSymbolContextLineEntry; |
| 600 | } |
| 601 | } |
| 602 | } |
| 603 | return resolved_flags; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | uint32_t SymbolFilePDB::ResolveSymbolContext( |
| 607 | const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, |
| 608 | uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 609 | const size_t old_size = sc_list.GetSize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 610 | if (resolve_scope & lldb::eSymbolContextCompUnit) { |
| 611 | // Locate all compilation units with line numbers referencing the specified |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 612 | // file. For example, if `file_spec` is <vector>, then this should return |
| 613 | // all source files and header files that reference <vector>, either |
| 614 | // directly or indirectly. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 615 | auto compilands = m_session_up->findCompilandsForSourceFile( |
| 616 | file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); |
| 617 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 618 | if (!compilands) |
| 619 | return 0; |
| 620 | |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 621 | // For each one, either find its previously parsed data or parse it afresh |
| 622 | // and add it to the symbol context list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 623 | while (auto compiland = compilands->getNext()) { |
| 624 | // If we're not checking inlines, then don't add line information for this |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 625 | // file unless the FileSpec matches. |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 626 | // For inline functions, we don't have to match the FileSpec since they |
| 627 | // could be defined in headers other than file specified in FileSpec. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 628 | if (!check_inlines) { |
Aaron Smith | 487b0c6 | 2018-03-20 00:18:22 +0000 | [diff] [blame] | 629 | std::string source_file = compiland->getSourceFileFullPath(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 630 | if (source_file.empty()) |
| 631 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 632 | FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 633 | bool need_full_match = !file_spec.GetDirectory().IsEmpty(); |
| 634 | if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 635 | continue; |
| 636 | } |
| 637 | |
| 638 | SymbolContext sc; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 639 | auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame^] | 640 | if (!cu) |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 641 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 642 | sc.comp_unit = cu.get(); |
| 643 | sc.module_sp = cu->GetModule(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 644 | |
| 645 | // If we were asked to resolve line entries, add all entries to the line |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 646 | // table that match the requested line (or all lines if `line` == 0). |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 647 | if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | |
| 648 | eSymbolContextLineEntry)) { |
| 649 | bool has_line_table = ParseCompileUnitLineTable(sc, line); |
| 650 | |
| 651 | if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { |
| 652 | // The query asks for line entries, but we can't get them for the |
| 653 | // compile unit. This is not normal for `line` = 0. So just assert it. |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 654 | assert(line && "Couldn't get all line entries!\n"); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 655 | |
| 656 | // Current compiland does not have the requested line. Search next. |
| 657 | continue; |
| 658 | } |
| 659 | |
| 660 | if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { |
| 661 | if (!has_line_table) |
| 662 | continue; |
| 663 | |
| 664 | auto *line_table = sc.comp_unit->GetLineTable(); |
| 665 | lldbassert(line_table); |
| 666 | |
| 667 | uint32_t num_line_entries = line_table->GetSize(); |
| 668 | // Skip the terminal line entry. |
| 669 | --num_line_entries; |
| 670 | |
| 671 | // If `line `!= 0, see if we can resolve function for each line |
| 672 | // entry in the line table. |
| 673 | for (uint32_t line_idx = 0; line && line_idx < num_line_entries; |
| 674 | ++line_idx) { |
| 675 | if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) |
| 676 | continue; |
| 677 | |
| 678 | auto file_vm_addr = |
| 679 | sc.line_entry.range.GetBaseAddress().GetFileAddress(); |
| 680 | if (file_vm_addr == LLDB_INVALID_ADDRESS) |
| 681 | continue; |
| 682 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 683 | auto symbol_up = m_session_up->findSymbolByAddress( |
| 684 | file_vm_addr, PDB_SymType::Function); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 685 | if (symbol_up) { |
| 686 | auto func_uid = symbol_up->getSymIndexId(); |
| 687 | sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); |
| 688 | if (sc.function == nullptr) { |
| 689 | auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); |
| 690 | assert(pdb_func); |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 691 | sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 692 | } |
| 693 | if (sc.function && (resolve_scope & eSymbolContextBlock)) { |
| 694 | Block &block = sc.function->GetBlock(true); |
| 695 | sc.block = block.FindBlockByID(sc.function->GetID()); |
| 696 | } |
| 697 | } |
| 698 | sc_list.Append(sc); |
| 699 | } |
| 700 | } else if (has_line_table) { |
| 701 | // We can parse line table for the compile unit. But no query to |
| 702 | // resolve function or block. We append `sc` to the list anyway. |
| 703 | sc_list.Append(sc); |
| 704 | } |
| 705 | } else { |
| 706 | // No query for line entry, function or block. But we have a valid |
| 707 | // compile unit, append `sc` to the list. |
| 708 | sc_list.Append(sc); |
| 709 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 712 | return sc_list.GetSize() - old_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | uint32_t SymbolFilePDB::FindGlobalVariables( |
| 716 | const lldb_private::ConstString &name, |
| 717 | const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, |
| 718 | uint32_t max_matches, lldb_private::VariableList &variables) { |
| 719 | return uint32_t(); |
| 720 | } |
| 721 | |
| 722 | uint32_t |
| 723 | SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, |
| 724 | bool append, uint32_t max_matches, |
| 725 | lldb_private::VariableList &variables) { |
| 726 | return uint32_t(); |
| 727 | } |
| 728 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 729 | bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 730 | bool include_inlines, |
| 731 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 732 | lldb_private::SymbolContext sc; |
Aaron Smith | a3a8cc8 | 2018-03-20 00:34:18 +0000 | [diff] [blame] | 733 | sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 734 | if (!sc.comp_unit) |
| 735 | return false; |
| 736 | sc.module_sp = sc.comp_unit->GetModule(); |
Aaron Smith | a3a8cc8 | 2018-03-20 00:34:18 +0000 | [diff] [blame] | 737 | sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 738 | if (!sc.function) |
| 739 | return false; |
| 740 | |
| 741 | sc_list.Append(sc); |
| 742 | return true; |
| 743 | } |
| 744 | |
| 745 | bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, |
| 746 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 747 | auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 748 | if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) |
| 749 | return false; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 750 | return ResolveFunction(*pdb_func_up, include_inlines, sc_list); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | void SymbolFilePDB::CacheFunctionNames() { |
| 754 | if (!m_func_full_names.IsEmpty()) |
| 755 | return; |
| 756 | |
| 757 | std::map<uint64_t, uint32_t> addr_ids; |
| 758 | |
| 759 | if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { |
| 760 | while (auto pdb_func_up = results_up->getNext()) { |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 761 | if (pdb_func_up->isCompilerGenerated()) |
| 762 | continue; |
| 763 | |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 764 | auto name = pdb_func_up->getName(); |
| 765 | auto demangled_name = pdb_func_up->getUndecoratedName(); |
| 766 | if (name.empty() && demangled_name.empty()) |
| 767 | continue; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 768 | |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 769 | auto uid = pdb_func_up->getSymIndexId(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 770 | if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) |
| 771 | addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); |
| 772 | |
| 773 | if (auto parent = pdb_func_up->getClassParent()) { |
| 774 | |
| 775 | // PDB have symbols for class/struct methods or static methods in Enum |
| 776 | // Class. We won't bother to check if the parent is UDT or Enum here. |
| 777 | m_func_method_names.Append(ConstString(name), uid); |
| 778 | |
| 779 | ConstString cstr_name(name); |
| 780 | |
| 781 | // To search a method name, like NS::Class:MemberFunc, LLDB searches its |
| 782 | // base name, i.e. MemberFunc by default. Since PDBSymbolFunc does not |
| 783 | // have inforamtion of this, we extract base names and cache them by our |
| 784 | // own effort. |
| 785 | llvm::StringRef basename; |
| 786 | CPlusPlusLanguage::MethodName cpp_method(cstr_name); |
| 787 | if (cpp_method.IsValid()) { |
| 788 | llvm::StringRef context; |
| 789 | basename = cpp_method.GetBasename(); |
| 790 | if (basename.empty()) |
| 791 | CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(), |
| 792 | context, basename); |
| 793 | } |
| 794 | |
| 795 | if (!basename.empty()) |
| 796 | m_func_base_names.Append(ConstString(basename), uid); |
| 797 | else { |
| 798 | m_func_base_names.Append(ConstString(name), uid); |
| 799 | } |
| 800 | |
| 801 | if (!demangled_name.empty()) |
| 802 | m_func_full_names.Append(ConstString(demangled_name), uid); |
| 803 | |
| 804 | } else { |
| 805 | // Handle not-method symbols. |
| 806 | |
| 807 | // The function name might contain namespace, or its lexical scope. It |
| 808 | // is not safe to get its base name by applying same scheme as we deal |
| 809 | // with the method names. |
| 810 | // FIXME: Remove namespace if function is static in a scope. |
| 811 | m_func_base_names.Append(ConstString(name), uid); |
| 812 | |
| 813 | if (name == "main") { |
| 814 | m_func_full_names.Append(ConstString(name), uid); |
| 815 | |
| 816 | if (!demangled_name.empty() && name != demangled_name) { |
| 817 | m_func_full_names.Append(ConstString(demangled_name), uid); |
| 818 | m_func_base_names.Append(ConstString(demangled_name), uid); |
| 819 | } |
| 820 | } else if (!demangled_name.empty()) { |
| 821 | m_func_full_names.Append(ConstString(demangled_name), uid); |
| 822 | } else { |
| 823 | m_func_full_names.Append(ConstString(name), uid); |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | if (auto results_up = |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 830 | m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 831 | while (auto pub_sym_up = results_up->getNext()) { |
| 832 | if (!pub_sym_up->isFunction()) |
| 833 | continue; |
| 834 | auto name = pub_sym_up->getName(); |
| 835 | if (name.empty()) |
| 836 | continue; |
| 837 | |
| 838 | if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 839 | auto vm_addr = pub_sym_up->getVirtualAddress(); |
| 840 | |
| 841 | // PDB public symbol has mangled name for its associated function. |
| 842 | if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) { |
| 843 | // Cache mangled name. |
| 844 | m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | } |
| 849 | // Sort them before value searching is working properly |
| 850 | m_func_full_names.Sort(); |
| 851 | m_func_full_names.SizeToFit(); |
| 852 | m_func_method_names.Sort(); |
| 853 | m_func_method_names.SizeToFit(); |
| 854 | m_func_base_names.Sort(); |
| 855 | m_func_base_names.SizeToFit(); |
| 856 | } |
| 857 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 858 | uint32_t SymbolFilePDB::FindFunctions( |
| 859 | const lldb_private::ConstString &name, |
| 860 | const lldb_private::CompilerDeclContext *parent_decl_ctx, |
| 861 | uint32_t name_type_mask, bool include_inlines, bool append, |
| 862 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 863 | if (!append) |
| 864 | sc_list.Clear(); |
| 865 | lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); |
| 866 | |
| 867 | if (name_type_mask == eFunctionNameTypeNone) |
| 868 | return 0; |
| 869 | if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) |
| 870 | return 0; |
| 871 | if (name.IsEmpty()) |
| 872 | return 0; |
| 873 | |
| 874 | auto old_size = sc_list.GetSize(); |
Pavel Labath | 4d4d63e | 2018-02-09 11:37:01 +0000 | [diff] [blame] | 875 | if (name_type_mask & eFunctionNameTypeFull || |
| 876 | name_type_mask & eFunctionNameTypeBase || |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 877 | name_type_mask & eFunctionNameTypeMethod) { |
| 878 | CacheFunctionNames(); |
| 879 | |
| 880 | std::set<uint32_t> resolved_ids; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 881 | auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids, |
| 882 | this](UniqueCStringMap<uint32_t> &Names) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 883 | std::vector<uint32_t> ids; |
| 884 | if (Names.GetValues(name, ids)) { |
| 885 | for (auto id : ids) { |
| 886 | if (resolved_ids.find(id) == resolved_ids.end()) { |
| 887 | if (ResolveFunction(id, include_inlines, sc_list)) |
| 888 | resolved_ids.insert(id); |
| 889 | } |
| 890 | } |
| 891 | } |
| 892 | }; |
| 893 | if (name_type_mask & eFunctionNameTypeFull) { |
| 894 | ResolveFn(m_func_full_names); |
| 895 | } |
| 896 | if (name_type_mask & eFunctionNameTypeBase) { |
| 897 | ResolveFn(m_func_base_names); |
| 898 | } |
| 899 | if (name_type_mask & eFunctionNameTypeMethod) { |
| 900 | ResolveFn(m_func_method_names); |
| 901 | } |
| 902 | } |
| 903 | return sc_list.GetSize() - old_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | uint32_t |
| 907 | SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, |
| 908 | bool include_inlines, bool append, |
| 909 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 910 | if (!append) |
| 911 | sc_list.Clear(); |
| 912 | if (!regex.IsValid()) |
| 913 | return 0; |
| 914 | |
| 915 | auto old_size = sc_list.GetSize(); |
| 916 | CacheFunctionNames(); |
| 917 | |
| 918 | std::set<uint32_t> resolved_ids; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 919 | auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, |
| 920 | this](UniqueCStringMap<uint32_t> &Names) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 921 | std::vector<uint32_t> ids; |
| 922 | if (Names.GetValues(regex, ids)) { |
| 923 | for (auto id : ids) { |
| 924 | if (resolved_ids.find(id) == resolved_ids.end()) |
| 925 | if (ResolveFunction(id, include_inlines, sc_list)) |
| 926 | resolved_ids.insert(id); |
| 927 | } |
| 928 | } |
| 929 | }; |
| 930 | ResolveFn(m_func_full_names); |
| 931 | ResolveFn(m_func_base_names); |
| 932 | |
| 933 | return sc_list.GetSize() - old_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | void SymbolFilePDB::GetMangledNamesForFunction( |
| 937 | const std::string &scope_qualified_name, |
| 938 | std::vector<lldb_private::ConstString> &mangled_names) {} |
| 939 | |
| 940 | uint32_t SymbolFilePDB::FindTypes( |
| 941 | const lldb_private::SymbolContext &sc, |
| 942 | const lldb_private::ConstString &name, |
| 943 | const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, |
| 944 | uint32_t max_matches, |
| 945 | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
| 946 | lldb_private::TypeMap &types) { |
| 947 | if (!append) |
| 948 | types.Clear(); |
| 949 | if (!name) |
| 950 | return 0; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 951 | if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) |
| 952 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 953 | |
| 954 | searched_symbol_files.clear(); |
| 955 | searched_symbol_files.insert(this); |
| 956 | |
| 957 | std::string name_str = name.AsCString(); |
| 958 | |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 959 | // There is an assumption 'name' is not a regex |
| 960 | FindTypesByName(name_str, max_matches, types); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 961 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 962 | return types.GetSize(); |
| 963 | } |
| 964 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 965 | void SymbolFilePDB::FindTypesByRegex( |
| 966 | const lldb_private::RegularExpression ®ex, uint32_t max_matches, |
| 967 | lldb_private::TypeMap &types) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 968 | // When searching by regex, we need to go out of our way to limit the search |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 969 | // space as much as possible since this searches EVERYTHING in the PDB, |
| 970 | // manually doing regex comparisons. PDB library isn't optimized for regex |
| 971 | // searches or searches across multiple symbol types at the same time, so the |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 972 | // best we can do is to search enums, then typedefs, then classes one by one, |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 973 | // and do a regex comparison against each of them. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 974 | PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, |
| 975 | PDB_SymType::UDT}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 976 | std::unique_ptr<IPDBEnumSymbols> results; |
| 977 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 978 | uint32_t matches = 0; |
| 979 | |
| 980 | for (auto tag : tags_to_search) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 981 | results = m_global_scope_up->findAllChildren(tag); |
| 982 | if (!results) |
| 983 | continue; |
| 984 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 985 | while (auto result = results->getNext()) { |
| 986 | if (max_matches > 0 && matches >= max_matches) |
| 987 | break; |
| 988 | |
| 989 | std::string type_name; |
| 990 | if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) |
| 991 | type_name = enum_type->getName(); |
| 992 | else if (auto typedef_type = |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 993 | llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 994 | type_name = typedef_type->getName(); |
| 995 | else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) |
| 996 | type_name = class_type->getName(); |
| 997 | else { |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 998 | // We're looking only for types that have names. Skip symbols, as well |
| 999 | // as unnamed types such as arrays, pointers, etc. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1000 | continue; |
| 1001 | } |
| 1002 | |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 1003 | if (!regex.Execute(type_name)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1004 | continue; |
| 1005 | |
| 1006 | // This should cause the type to get cached and stored in the `m_types` |
| 1007 | // lookup. |
| 1008 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 1009 | continue; |
| 1010 | |
| 1011 | auto iter = m_types.find(result->getSymIndexId()); |
| 1012 | if (iter == m_types.end()) |
| 1013 | continue; |
| 1014 | types.Insert(iter->second); |
| 1015 | ++matches; |
| 1016 | } |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | void SymbolFilePDB::FindTypesByName(const std::string &name, |
| 1021 | uint32_t max_matches, |
| 1022 | lldb_private::TypeMap &types) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1023 | std::unique_ptr<IPDBEnumSymbols> results; |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1024 | if (name.empty()) |
| 1025 | return; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1026 | results = m_global_scope_up->findChildren(PDB_SymType::None, name, |
| 1027 | PDB_NameSearchFlags::NS_Default); |
| 1028 | if (!results) |
| 1029 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1030 | |
| 1031 | uint32_t matches = 0; |
| 1032 | |
| 1033 | while (auto result = results->getNext()) { |
| 1034 | if (max_matches > 0 && matches >= max_matches) |
| 1035 | break; |
| 1036 | switch (result->getSymTag()) { |
| 1037 | case PDB_SymType::Enum: |
| 1038 | case PDB_SymType::UDT: |
| 1039 | case PDB_SymType::Typedef: |
| 1040 | break; |
| 1041 | default: |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1042 | // We're looking only for types that have names. Skip symbols, as well as |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1043 | // unnamed types such as arrays, pointers, etc. |
| 1044 | continue; |
| 1045 | } |
| 1046 | |
| 1047 | // This should cause the type to get cached and stored in the `m_types` |
| 1048 | // lookup. |
| 1049 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 1050 | continue; |
| 1051 | |
| 1052 | auto iter = m_types.find(result->getSymIndexId()); |
| 1053 | if (iter == m_types.end()) |
| 1054 | continue; |
| 1055 | types.Insert(iter->second); |
| 1056 | ++matches; |
| 1057 | } |
| 1058 | } |
| 1059 | |
| 1060 | size_t SymbolFilePDB::FindTypes( |
| 1061 | const std::vector<lldb_private::CompilerContext> &contexts, bool append, |
| 1062 | lldb_private::TypeMap &types) { |
| 1063 | return 0; |
| 1064 | } |
| 1065 | |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 1066 | lldb_private::TypeList *SymbolFilePDB::GetTypeList() { |
| 1067 | return m_obj_file->GetModule()->GetTypeList(); |
| 1068 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1069 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1070 | void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, |
| 1071 | uint32_t type_mask, |
| 1072 | TypeCollection &type_collection) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1073 | bool can_parse = false; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1074 | switch (pdb_symbol.getSymTag()) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1075 | case PDB_SymType::ArrayType: |
| 1076 | can_parse = ((type_mask & eTypeClassArray) != 0); |
| 1077 | break; |
| 1078 | case PDB_SymType::BuiltinType: |
| 1079 | can_parse = ((type_mask & eTypeClassBuiltin) != 0); |
| 1080 | break; |
| 1081 | case PDB_SymType::Enum: |
| 1082 | can_parse = ((type_mask & eTypeClassEnumeration) != 0); |
| 1083 | break; |
| 1084 | case PDB_SymType::Function: |
| 1085 | case PDB_SymType::FunctionSig: |
| 1086 | can_parse = ((type_mask & eTypeClassFunction) != 0); |
| 1087 | break; |
| 1088 | case PDB_SymType::PointerType: |
| 1089 | can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer | |
| 1090 | eTypeClassMemberPointer)) != 0); |
| 1091 | break; |
| 1092 | case PDB_SymType::Typedef: |
| 1093 | can_parse = ((type_mask & eTypeClassTypedef) != 0); |
| 1094 | break; |
| 1095 | case PDB_SymType::UDT: { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1096 | auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1097 | assert(udt); |
| 1098 | can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1099 | ((type_mask & (eTypeClassClass | eTypeClassStruct | |
| 1100 | eTypeClassUnion)) != 0)); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1101 | } break; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1102 | default: |
| 1103 | break; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | if (can_parse) { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1107 | if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1108 | auto result = |
| 1109 | std::find(type_collection.begin(), type_collection.end(), type); |
| 1110 | if (result == type_collection.end()) |
| 1111 | type_collection.push_back(type); |
| 1112 | } |
| 1113 | } |
| 1114 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1115 | auto results_up = pdb_symbol.findAllChildren(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1116 | while (auto symbol_up = results_up->getNext()) |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1117 | GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1118 | } |
| 1119 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1120 | size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, |
| 1121 | uint32_t type_mask, |
| 1122 | lldb_private::TypeList &type_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1123 | TypeCollection type_collection; |
| 1124 | uint32_t old_size = type_list.GetSize(); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1125 | CompileUnit *cu = |
| 1126 | sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1127 | if (cu) { |
| 1128 | auto compiland_up = GetPDBCompilandByUID(cu->GetID()); |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1129 | if (!compiland_up) |
| 1130 | return 0; |
| 1131 | GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1132 | } else { |
| 1133 | for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { |
| 1134 | auto cu_sp = ParseCompileUnitAtIndex(cu_idx); |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame^] | 1135 | if (cu_sp) { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1136 | if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) |
| 1137 | GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | for (auto type : type_collection) { |
| 1143 | type->GetForwardCompilerType(); |
| 1144 | type_list.Insert(type->shared_from_this()); |
| 1145 | } |
| 1146 | return type_list.GetSize() - old_size; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | lldb_private::TypeSystem * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1150 | SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { |
| 1151 | auto type_system = |
| 1152 | m_obj_file->GetModule()->GetTypeSystemForLanguage(language); |
| 1153 | if (type_system) |
| 1154 | type_system->SetSymbolFile(this); |
| 1155 | return type_system; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1158 | lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( |
| 1159 | const lldb_private::SymbolContext &sc, |
| 1160 | const lldb_private::ConstString &name, |
| 1161 | const lldb_private::CompilerDeclContext *parent_decl_ctx) { |
| 1162 | return lldb_private::CompilerDeclContext(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1165 | lldb_private::ConstString SymbolFilePDB::GetPluginName() { |
| 1166 | static ConstString g_name("pdb"); |
| 1167 | return g_name; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1168 | } |
| 1169 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1170 | uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } |
| 1171 | |
| 1172 | IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } |
| 1173 | |
| 1174 | const IPDBSession &SymbolFilePDB::GetPDBSession() const { |
| 1175 | return *m_session_up; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1178 | lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, |
| 1179 | uint32_t index) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1180 | auto found_cu = m_comp_units.find(id); |
| 1181 | if (found_cu != m_comp_units.end()) |
| 1182 | return found_cu->second; |
| 1183 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1184 | auto compiland_up = GetPDBCompilandByUID(id); |
| 1185 | if (!compiland_up) |
| 1186 | return CompUnitSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1187 | |
| 1188 | lldb::LanguageType lang; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1189 | auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1190 | if (!details) |
| 1191 | lang = lldb::eLanguageTypeC_plus_plus; |
| 1192 | else |
| 1193 | lang = TranslateLanguage(details->getLanguage()); |
| 1194 | |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1195 | if (lang == lldb::LanguageType::eLanguageTypeUnknown) |
| 1196 | return CompUnitSP(); |
| 1197 | |
Aaron Smith | 487b0c6 | 2018-03-20 00:18:22 +0000 | [diff] [blame] | 1198 | std::string path = compiland_up->getSourceFileFullPath(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1199 | if (path.empty()) |
| 1200 | return CompUnitSP(); |
| 1201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1202 | // Don't support optimized code for now, DebugInfoPDB does not return this |
| 1203 | // information. |
| 1204 | LazyBool optimized = eLazyBoolNo; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1205 | auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, |
| 1206 | path.c_str(), id, lang, optimized); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (!cu_sp) |
| 1209 | return CompUnitSP(); |
| 1210 | |
| 1211 | m_comp_units.insert(std::make_pair(id, cu_sp)); |
| 1212 | if (index == UINT32_MAX) |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1213 | GetCompileUnitIndex(*compiland_up, index); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1214 | lldbassert(index != UINT32_MAX); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1215 | m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index, |
| 1216 | cu_sp); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1217 | return cu_sp; |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1220 | bool SymbolFilePDB::ParseCompileUnitLineTable( |
| 1221 | const lldb_private::SymbolContext &sc, uint32_t match_line) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1222 | lldbassert(sc.comp_unit); |
| 1223 | |
| 1224 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 1225 | if (!compiland_up) |
| 1226 | return false; |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 1227 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1228 | // LineEntry needs the *index* of the file into the list of support files |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1229 | // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us |
| 1230 | // a globally unique idenfitifier in the namespace of the PDB. So, we have to |
| 1231 | // do a mapping so that we can hand out indices. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1232 | llvm::DenseMap<uint32_t, uint32_t> index_map; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1233 | BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1234 | auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1235 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1236 | // Find contributions to `compiland` from all source and header files. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1237 | std::string path = sc.comp_unit->GetPath(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1238 | auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); |
| 1239 | if (!files) |
| 1240 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1241 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1242 | // For each source and header file, create a LineSequence for contributions to |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1243 | // the compiland from that file, and add the sequence. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1244 | while (auto file = files->getNext()) { |
| 1245 | std::unique_ptr<LineSequence> sequence( |
| 1246 | line_table->CreateLineSequenceContainer()); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1247 | auto lines = m_session_up->findLineNumbers(*compiland_up, *file); |
| 1248 | if (!lines) |
| 1249 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1250 | int entry_count = lines->getChildCount(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1251 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1252 | uint64_t prev_addr; |
| 1253 | uint32_t prev_length; |
| 1254 | uint32_t prev_line; |
| 1255 | uint32_t prev_source_idx; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1256 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1257 | for (int i = 0; i < entry_count; ++i) { |
| 1258 | auto line = lines->getChildAtIndex(i); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1259 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1260 | uint64_t lno = line->getLineNumber(); |
| 1261 | uint64_t addr = line->getVirtualAddress(); |
| 1262 | uint32_t length = line->getLength(); |
| 1263 | uint32_t source_id = line->getSourceFileId(); |
| 1264 | uint32_t col = line->getColumnNumber(); |
| 1265 | uint32_t source_idx = index_map[source_id]; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1266 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1267 | // There was a gap between the current entry and the previous entry if the |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1268 | // addresses don't perfectly line up. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1269 | bool is_gap = (i > 0) && (prev_addr + prev_length < addr); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1270 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1271 | // Before inserting the current entry, insert a terminal entry at the end |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1272 | // of the previous entry's address range if the current entry resulted in |
| 1273 | // a gap from the previous entry. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1274 | if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { |
| 1275 | line_table->AppendLineEntryToSequence( |
| 1276 | sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 1277 | prev_source_idx, false, false, false, false, true); |
| 1278 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1279 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1280 | if (ShouldAddLine(match_line, lno, length)) { |
| 1281 | bool is_statement = line->isStatement(); |
| 1282 | bool is_prologue = false; |
| 1283 | bool is_epilogue = false; |
| 1284 | auto func = |
| 1285 | m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); |
| 1286 | if (func) { |
| 1287 | auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1288 | if (prologue) |
| 1289 | is_prologue = (addr == prologue->getVirtualAddress()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1290 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1291 | auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1292 | if (epilogue) |
| 1293 | is_epilogue = (addr == epilogue->getVirtualAddress()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1294 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 1295 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1296 | line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, |
| 1297 | source_idx, is_statement, false, |
| 1298 | is_prologue, is_epilogue, false); |
| 1299 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 1300 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1301 | prev_addr = addr; |
| 1302 | prev_length = length; |
| 1303 | prev_line = lno; |
| 1304 | prev_source_idx = source_idx; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1307 | if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { |
| 1308 | // The end is always a terminal entry, so insert it regardless. |
| 1309 | line_table->AppendLineEntryToSequence( |
| 1310 | sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 1311 | prev_source_idx, false, false, false, false, true); |
| 1312 | } |
| 1313 | |
| 1314 | line_table->InsertSequence(sequence.release()); |
| 1315 | } |
| 1316 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1317 | if (line_table->GetSize()) { |
| 1318 | sc.comp_unit->SetLineTable(line_table.release()); |
| 1319 | return true; |
| 1320 | } |
| 1321 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1324 | void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1325 | const PDBSymbolCompiland &compiland, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1326 | llvm::DenseMap<uint32_t, uint32_t> &index_map) const { |
| 1327 | // This is a hack, but we need to convert the source id into an index into the |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1328 | // support files array. We don't want to do path comparisons to avoid |
| 1329 | // basename / full path issues that may or may not even be a problem, so we |
| 1330 | // use the globally unique source file identifiers. Ideally we could use the |
| 1331 | // global identifiers everywhere, but LineEntry currently assumes indices. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1332 | auto source_files = m_session_up->getSourceFilesForCompiland(compiland); |
| 1333 | if (!source_files) |
| 1334 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1335 | int index = 0; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1336 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1337 | while (auto file = source_files->getNext()) { |
| 1338 | uint32_t source_id = file->getUniqueId(); |
| 1339 | index_map[source_id] = index++; |
| 1340 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1341 | } |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1342 | |
| 1343 | lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( |
| 1344 | const lldb_private::Address &so_addr) { |
| 1345 | lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); |
| 1346 | if (file_vm_addr == LLDB_INVALID_ADDRESS) |
| 1347 | return nullptr; |
| 1348 | |
| 1349 | auto lines_up = |
| 1350 | m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/200); |
| 1351 | if (!lines_up) |
| 1352 | return nullptr; |
| 1353 | |
| 1354 | auto first_line_up = lines_up->getNext(); |
| 1355 | if (!first_line_up) |
| 1356 | return nullptr; |
| 1357 | auto compiland_up = GetPDBCompilandByUID(first_line_up->getCompilandId()); |
| 1358 | if (compiland_up) { |
| 1359 | return ParseCompileUnitForUID(compiland_up->getSymIndexId()); |
| 1360 | } |
| 1361 | |
| 1362 | return nullptr; |
| 1363 | } |
| 1364 | |
| 1365 | Mangled |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1366 | SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1367 | Mangled mangled; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1368 | auto func_name = pdb_func.getName(); |
| 1369 | auto func_undecorated_name = pdb_func.getUndecoratedName(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1370 | std::string func_decorated_name; |
| 1371 | |
| 1372 | // Seek from public symbols for non-static function's decorated name if any. |
| 1373 | // For static functions, they don't have undecorated names and aren't exposed |
| 1374 | // in Public Symbols either. |
| 1375 | if (!func_undecorated_name.empty()) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1376 | auto result_up = m_global_scope_up->findChildren( |
| 1377 | PDB_SymType::PublicSymbol, func_undecorated_name, |
| 1378 | PDB_NameSearchFlags::NS_UndecoratedName); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1379 | if (result_up) { |
| 1380 | while (auto symbol_up = result_up->getNext()) { |
| 1381 | // For a public symbol, it is unique. |
| 1382 | lldbassert(result_up->getChildCount() == 1); |
| 1383 | if (auto *pdb_public_sym = |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1384 | llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>( |
| 1385 | symbol_up.get())) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1386 | if (pdb_public_sym->isFunction()) { |
| 1387 | func_decorated_name = pdb_public_sym->getName(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1388 | break; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1389 | } |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | if (!func_decorated_name.empty()) { |
| 1395 | mangled.SetMangledName(ConstString(func_decorated_name)); |
| 1396 | |
| 1397 | // For MSVC, format of C funciton's decorated name depends on calling |
| 1398 | // conventon. Unfortunately none of the format is recognized by current |
| 1399 | // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, |
| 1400 | // `__purecall` is retrieved as both its decorated and |
| 1401 | // undecorated name (using PDBSymbolFunc::getUndecoratedName method). |
| 1402 | // However `__purecall` string is not treated as mangled in LLDB |
| 1403 | // (neither `?` nor `_Z` prefix). Mangled::GetDemangledName method |
| 1404 | // will fail internally and caches an empty string as its undecorated |
| 1405 | // name. So we will face a contradition here for the same symbol: |
| 1406 | // non-empty undecorated name from PDB |
| 1407 | // empty undecorated name from LLDB |
| 1408 | if (!func_undecorated_name.empty() && |
| 1409 | mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty()) |
| 1410 | mangled.SetDemangledName(ConstString(func_undecorated_name)); |
| 1411 | |
| 1412 | // LLDB uses several flags to control how a C++ decorated name is |
| 1413 | // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. |
| 1414 | // So the yielded name could be different from what we retrieve from |
| 1415 | // PDB source unless we also apply same flags in getting undecorated |
| 1416 | // name through PDBSymbolFunc::getUndecoratedNameEx method. |
| 1417 | if (!func_undecorated_name.empty() && |
| 1418 | mangled.GetDemangledName(mangled.GuessLanguage()) != |
| 1419 | ConstString(func_undecorated_name)) |
| 1420 | mangled.SetDemangledName(ConstString(func_undecorated_name)); |
| 1421 | } else if (!func_undecorated_name.empty()) { |
| 1422 | mangled.SetDemangledName(ConstString(func_undecorated_name)); |
| 1423 | } else if (!func_name.empty()) |
| 1424 | mangled.SetValue(ConstString(func_name), false); |
| 1425 | |
| 1426 | return mangled; |
| 1427 | } |
| 1428 | |
| 1429 | bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( |
| 1430 | const lldb_private::CompilerDeclContext *decl_ctx) { |
| 1431 | if (decl_ctx == nullptr || !decl_ctx->IsValid()) |
| 1432 | return true; |
| 1433 | |
| 1434 | TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); |
| 1435 | if (!decl_ctx_type_system) |
| 1436 | return false; |
| 1437 | TypeSystem *type_system = GetTypeSystemForLanguage( |
| 1438 | decl_ctx_type_system->GetMinimumLanguage(nullptr)); |
| 1439 | if (decl_ctx_type_system == type_system) |
| 1440 | return true; // The type systems match, return true |
| 1441 | |
| 1442 | return false; |
| 1443 | } |