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 | |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 12 | #include "PDBASTParser.h" |
| 13 | #include "PDBLocationToDWARFExpression.h" |
| 14 | |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 15 | #include "clang/Lex/Lexer.h" |
| 16 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 17 | #include "lldb/Core/Module.h" |
| 18 | #include "lldb/Core/PluginManager.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 19 | #include "lldb/Symbol/ClangASTContext.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 20 | #include "lldb/Symbol/CompileUnit.h" |
| 21 | #include "lldb/Symbol/LineTable.h" |
| 22 | #include "lldb/Symbol/ObjectFile.h" |
| 23 | #include "lldb/Symbol/SymbolContext.h" |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 24 | #include "lldb/Symbol/SymbolVendor.h" |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 25 | #include "lldb/Symbol/TypeList.h" |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/TypeMap.h" |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/Variable.h" |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 28 | #include "lldb/Utility/RegularExpression.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 29 | |
Pavel Labath | b8d8c62 | 2016-05-09 11:07:43 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/PDB/GenericError.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/PDB/IPDBDataStream.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 32 | #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" |
| 33 | #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/PDB/IPDBSectionContrib.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/PDB/IPDBTable.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 37 | #include "llvm/DebugInfo/PDB/PDBSymbol.h" |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 38 | #include "llvm/DebugInfo/PDB/PDBSymbolBlock.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 39 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 40 | #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 41 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
| 43 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| 44 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" |
| 45 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 46 | #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 47 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" |
| 48 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" |
| 49 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
| 50 | |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 51 | #include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" // For IsCPPMangledName |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 52 | #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 53 | #include "Plugins/SymbolFile/NativePDB/SymbolFileNativePDB.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 54 | |
| 55 | #include <regex> |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 56 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 57 | using namespace lldb; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 58 | using namespace lldb_private; |
Zachary Turner | 54fd7ff | 2016-05-04 20:33:53 +0000 | [diff] [blame] | 59 | using namespace llvm::pdb; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 60 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 61 | namespace { |
| 62 | lldb::LanguageType TranslateLanguage(PDB_Lang lang) { |
| 63 | switch (lang) { |
| 64 | case PDB_Lang::Cpp: |
| 65 | return lldb::LanguageType::eLanguageTypeC_plus_plus; |
| 66 | case PDB_Lang::C: |
| 67 | return lldb::LanguageType::eLanguageTypeC; |
| 68 | default: |
| 69 | return lldb::LanguageType::eLanguageTypeUnknown; |
| 70 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 73 | bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, |
| 74 | uint32_t addr_length) { |
| 75 | return ((requested_line == 0 || actual_line == requested_line) && |
| 76 | addr_length > 0); |
| 77 | } |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 78 | } // namespace |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 79 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 80 | static bool ShouldUseNativeReader() { |
| 81 | #if !defined(_WIN32) |
| 82 | return true; |
| 83 | #endif |
| 84 | llvm::StringRef use_native = ::getenv("LLDB_USE_NATIVE_PDB_READER"); |
| 85 | return use_native.equals_lower("on") || use_native.equals_lower("yes") || |
| 86 | use_native.equals_lower("1") || use_native.equals_lower("true"); |
| 87 | } |
| 88 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 89 | void SymbolFilePDB::Initialize() { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 90 | if (ShouldUseNativeReader()) { |
| 91 | npdb::SymbolFileNativePDB::Initialize(); |
| 92 | } else { |
| 93 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 94 | GetPluginDescriptionStatic(), CreateInstance, |
| 95 | DebuggerInitialize); |
| 96 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 99 | void SymbolFilePDB::Terminate() { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 100 | if (ShouldUseNativeReader()) { |
| 101 | npdb::SymbolFileNativePDB::Terminate(); |
| 102 | } else { |
| 103 | PluginManager::UnregisterPlugin(CreateInstance); |
| 104 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} |
| 108 | |
| 109 | lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { |
| 110 | static ConstString g_name("pdb"); |
| 111 | return g_name; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 114 | const char *SymbolFilePDB::GetPluginDescriptionStatic() { |
| 115 | return "Microsoft PDB debug symbol file reader."; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | lldb_private::SymbolFile * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 119 | SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { |
| 120 | return new SymbolFilePDB(obj_file); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 124 | : SymbolFile(object_file), m_session_up(), m_global_scope_up(), |
| 125 | m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {} |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 126 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 127 | SymbolFilePDB::~SymbolFilePDB() {} |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 128 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | uint32_t SymbolFilePDB::CalculateAbilities() { |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 130 | uint32_t abilities = 0; |
| 131 | if (!m_obj_file) |
| 132 | return 0; |
| 133 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | if (!m_session_up) { |
| 135 | // Lazily load and match the PDB file, but only do this once. |
| 136 | std::string exePath = m_obj_file->GetFileSpec().GetPath(); |
| 137 | auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), |
| 138 | m_session_up); |
| 139 | if (error) { |
| 140 | llvm::consumeError(std::move(error)); |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 141 | auto module_sp = m_obj_file->GetModule(); |
| 142 | if (!module_sp) |
| 143 | return 0; |
| 144 | // See if any symbol file is specified through `--symfile` option. |
| 145 | FileSpec symfile = module_sp->GetSymbolFileFileSpec(); |
| 146 | if (!symfile) |
| 147 | return 0; |
| 148 | error = loadDataForPDB(PDB_ReaderType::DIA, |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 149 | llvm::StringRef(symfile.GetPath()), m_session_up); |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 150 | if (error) { |
| 151 | llvm::consumeError(std::move(error)); |
| 152 | return 0; |
| 153 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 154 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | } |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame] | 156 | if (!m_session_up) |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 157 | return 0; |
| 158 | |
| 159 | auto enum_tables_up = m_session_up->getEnumTables(); |
| 160 | if (!enum_tables_up) |
| 161 | return 0; |
| 162 | while (auto table_up = enum_tables_up->getNext()) { |
| 163 | if (table_up->getItemCount() == 0) |
| 164 | continue; |
| 165 | auto type = table_up->getTableType(); |
| 166 | switch (type) { |
| 167 | case PDB_TableType::Symbols: |
| 168 | // This table represents a store of symbols with types listed in |
| 169 | // PDBSym_Type |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 170 | abilities |= (CompileUnits | Functions | Blocks | GlobalVariables | |
| 171 | LocalVariables | VariableTypes); |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 172 | break; |
| 173 | case PDB_TableType::LineNumbers: |
| 174 | abilities |= LineTables; |
| 175 | break; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 176 | default: |
| 177 | break; |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | return abilities; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | void SymbolFilePDB::InitializeObject() { |
| 184 | lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 185 | lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 186 | m_session_up->setLoadAddress(obj_load_address); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 187 | if (!m_global_scope_up) |
| 188 | m_global_scope_up = m_session_up->getGlobalScope(); |
| 189 | lldbassert(m_global_scope_up.get()); |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 190 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 191 | TypeSystem *type_system = |
| 192 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 193 | ClangASTContext *clang_type_system = |
| 194 | llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 195 | lldbassert(clang_type_system); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 196 | m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>( |
| 197 | type_system, clang_type_system->GetTranslationUnitDecl()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | uint32_t SymbolFilePDB::GetNumCompileUnits() { |
| 201 | if (m_cached_compile_unit_count == 0) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 202 | auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 203 | if (!compilands) |
| 204 | return 0; |
| 205 | |
| 206 | // The linker could link *.dll (compiland language = LINK), or import |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 207 | // *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be |
| 208 | // found as a child of the global scope (PDB executable). Usually, such |
| 209 | // compilands contain `thunk` symbols in which we are not interested for |
| 210 | // now. However we still count them in the compiland list. If we perform |
| 211 | // any compiland related activity, like finding symbols through |
| 212 | // llvm::pdb::IPDBSession methods, such compilands will all be searched |
| 213 | // automatically no matter whether we include them or not. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | m_cached_compile_unit_count = compilands->getChildCount(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 215 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 216 | // The linker can inject an additional "dummy" compilation unit into the |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 217 | // PDB. Ignore this special compile unit for our purposes, if it is there. |
| 218 | // It is always the last one. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 219 | auto last_compiland_up = |
| 220 | compilands->getChildAtIndex(m_cached_compile_unit_count - 1); |
| 221 | lldbassert(last_compiland_up.get()); |
| 222 | std::string name = last_compiland_up->getName(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 223 | if (name == "* Linker *") |
| 224 | --m_cached_compile_unit_count; |
| 225 | } |
| 226 | return m_cached_compile_unit_count; |
| 227 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 228 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 229 | void SymbolFilePDB::GetCompileUnitIndex( |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 230 | const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 231 | auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 232 | if (!results_up) |
| 233 | return; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 234 | auto uid = pdb_compiland.getSymIndexId(); |
Raphael Isemann | fbdf0b9 | 2018-01-22 06:56:09 +0000 | [diff] [blame] | 235 | for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 236 | auto compiland_up = results_up->getChildAtIndex(cu_idx); |
| 237 | if (!compiland_up) |
| 238 | continue; |
| 239 | if (compiland_up->getSymIndexId() == uid) { |
| 240 | index = cu_idx; |
| 241 | return; |
| 242 | } |
| 243 | } |
| 244 | index = UINT32_MAX; |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | std::unique_ptr<llvm::pdb::PDBSymbolCompiland> |
| 249 | SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { |
| 250 | return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); |
| 251 | } |
| 252 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 253 | lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 254 | if (index >= GetNumCompileUnits()) |
| 255 | return CompUnitSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 257 | // Assuming we always retrieve same compilands listed in same order through |
| 258 | // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a |
| 259 | // compile unit makes no sense. |
| 260 | auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 261 | if (!results) |
| 262 | return CompUnitSP(); |
| 263 | auto compiland_up = results->getChildAtIndex(index); |
| 264 | if (!compiland_up) |
| 265 | return CompUnitSP(); |
| 266 | return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | lldb::LanguageType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 270 | SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) { |
| 271 | // What fields should I expect to be filled out on the SymbolContext? Is it |
| 272 | // safe to assume that `sc.comp_unit` is valid? |
| 273 | if (!sc.comp_unit) |
| 274 | return lldb::eLanguageTypeUnknown; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 275 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 276 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 277 | if (!compiland_up) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 278 | return lldb::eLanguageTypeUnknown; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 279 | auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 280 | if (!details) |
| 281 | return lldb::eLanguageTypeUnknown; |
| 282 | return TranslateLanguage(details->getLanguage()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 285 | lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc( |
| 286 | const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 287 | lldbassert(sc.comp_unit && sc.module_sp.get()); |
| 288 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 289 | auto file_vm_addr = pdb_func.getVirtualAddress(); |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 290 | if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 291 | return nullptr; |
| 292 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 293 | auto func_length = pdb_func.getLength(); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 294 | AddressRange func_range = |
| 295 | AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList()); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 296 | if (!func_range.GetBaseAddress().IsValid()) |
| 297 | return nullptr; |
| 298 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 299 | lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId()); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 300 | if (!func_type) |
| 301 | return nullptr; |
| 302 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 303 | user_id_t func_type_uid = pdb_func.getSignatureId(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 304 | |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 305 | Mangled mangled = GetMangledForPDBFunc(pdb_func); |
| 306 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 307 | FunctionSP func_sp = |
| 308 | std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(), |
| 309 | func_type_uid, mangled, func_type, func_range); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 310 | |
| 311 | sc.comp_unit->AddFunction(func_sp); |
| 312 | return func_sp.get(); |
| 313 | } |
| 314 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 315 | size_t SymbolFilePDB::ParseCompileUnitFunctions( |
| 316 | const lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 317 | lldbassert(sc.comp_unit); |
| 318 | size_t func_added = 0; |
| 319 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 320 | if (!compiland_up) |
| 321 | return 0; |
| 322 | auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>(); |
| 323 | if (!results_up) |
| 324 | return 0; |
| 325 | while (auto pdb_func_up = results_up->getNext()) { |
| 326 | auto func_sp = |
| 327 | sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId()); |
| 328 | if (!func_sp) { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 329 | if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc)) |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 330 | ++func_added; |
| 331 | } |
| 332 | } |
| 333 | return func_added; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 336 | bool SymbolFilePDB::ParseCompileUnitLineTable( |
| 337 | const lldb_private::SymbolContext &sc) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 338 | lldbassert(sc.comp_unit); |
| 339 | if (sc.comp_unit->GetLineTable()) |
| 340 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 341 | return ParseCompileUnitLineTable(sc, 0); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 344 | bool SymbolFilePDB::ParseCompileUnitDebugMacros( |
| 345 | const lldb_private::SymbolContext &sc) { |
| 346 | // PDB doesn't contain information about macros |
| 347 | return false; |
| 348 | } |
| 349 | |
| 350 | bool SymbolFilePDB::ParseCompileUnitSupportFiles( |
| 351 | const lldb_private::SymbolContext &sc, |
| 352 | lldb_private::FileSpecList &support_files) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 353 | lldbassert(sc.comp_unit); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 354 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 355 | // 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] | 356 | // is easily (and quickly) accessible from DebugInfoPDB, so caching it a |
| 357 | // second time seems like a waste. Unfortunately, there's no good way around |
| 358 | // this short of a moderate refactor since SymbolVendor depends on being able |
| 359 | // to cache this list. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 360 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 361 | if (!compiland_up) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 362 | return false; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 363 | auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 364 | if (!files || files->getChildCount() == 0) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 365 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 366 | |
| 367 | while (auto file = files->getNext()) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 368 | FileSpec spec(file->getFileName(), FileSpec::Style::windows); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 369 | support_files.AppendIfUnique(spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 370 | } |
Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 371 | |
| 372 | // LLDB uses the DWARF-like file numeration (one based), |
| 373 | // the zeroth file is the compile unit itself |
| 374 | support_files.Insert(0, *sc.comp_unit); |
| 375 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 376 | return true; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 379 | bool SymbolFilePDB::ParseImportedModules( |
| 380 | const lldb_private::SymbolContext &sc, |
| 381 | std::vector<lldb_private::ConstString> &imported_modules) { |
| 382 | // PDB does not yet support module debug info |
| 383 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 386 | static size_t ParseFunctionBlocksForPDBSymbol( |
| 387 | const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr, |
| 388 | const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block, |
| 389 | bool is_top_parent) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 390 | assert(pdb_symbol && parent_block); |
| 391 | |
| 392 | size_t num_added = 0; |
| 393 | switch (pdb_symbol->getSymTag()) { |
| 394 | case PDB_SymType::Block: |
| 395 | case PDB_SymType::Function: { |
| 396 | Block *block = nullptr; |
| 397 | auto &raw_sym = pdb_symbol->getRawSymbol(); |
| 398 | if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) { |
| 399 | if (pdb_func->hasNoInlineAttribute()) |
| 400 | break; |
| 401 | if (is_top_parent) |
| 402 | block = parent_block; |
| 403 | else |
| 404 | break; |
| 405 | } else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) { |
| 406 | auto uid = pdb_symbol->getSymIndexId(); |
| 407 | if (parent_block->FindBlockByID(uid)) |
| 408 | break; |
| 409 | if (raw_sym.getVirtualAddress() < func_file_vm_addr) |
| 410 | break; |
| 411 | |
| 412 | auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId()); |
| 413 | parent_block->AddChild(block_sp); |
| 414 | block = block_sp.get(); |
| 415 | } else |
| 416 | llvm_unreachable("Unexpected PDB symbol!"); |
| 417 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 418 | block->AddRange(Block::Range( |
| 419 | raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength())); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 420 | block->FinalizeRanges(); |
| 421 | ++num_added; |
| 422 | |
| 423 | auto results_up = pdb_symbol->findAllChildren(); |
| 424 | if (!results_up) |
| 425 | break; |
| 426 | while (auto symbol_up = results_up->getNext()) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 427 | num_added += ParseFunctionBlocksForPDBSymbol( |
| 428 | sc, func_file_vm_addr, symbol_up.get(), block, false); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 429 | } |
| 430 | } break; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 431 | default: |
| 432 | break; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 433 | } |
| 434 | return num_added; |
| 435 | } |
| 436 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 437 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 438 | SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 439 | lldbassert(sc.comp_unit && sc.function); |
| 440 | size_t num_added = 0; |
| 441 | auto uid = sc.function->GetID(); |
| 442 | auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); |
| 443 | if (!pdb_func_up) |
| 444 | return 0; |
| 445 | Block &parent_block = sc.function->GetBlock(false); |
| 446 | num_added = |
| 447 | ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(), |
| 448 | pdb_func_up.get(), &parent_block, true); |
| 449 | return num_added; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 452 | size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) { |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 453 | lldbassert(sc.module_sp.get()); |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 454 | if (!sc.comp_unit) |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 455 | return 0; |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 456 | |
| 457 | size_t num_added = 0; |
| 458 | auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 459 | if (!compiland) |
| 460 | return 0; |
| 461 | |
| 462 | auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) { |
| 463 | std::unique_ptr<IPDBEnumSymbols> results; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 464 | PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, |
| 465 | PDB_SymType::UDT}; |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 466 | for (auto tag : tags_to_search) { |
| 467 | results = raw_sym.findAllChildren(tag); |
| 468 | if (!results || results->getChildCount() == 0) |
| 469 | continue; |
| 470 | while (auto symbol = results->getNext()) { |
| 471 | switch (symbol->getSymTag()) { |
| 472 | case PDB_SymType::Enum: |
| 473 | case PDB_SymType::UDT: |
| 474 | case PDB_SymType::Typedef: |
| 475 | break; |
| 476 | default: |
| 477 | continue; |
| 478 | } |
| 479 | |
| 480 | // This should cause the type to get cached and stored in the `m_types` |
| 481 | // lookup. |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 482 | if (auto type = ResolveTypeUID(symbol->getSymIndexId())) { |
| 483 | // Resolve the type completely to avoid a completion |
| 484 | // (and so a list change, which causes an iterators invalidation) |
| 485 | // during a TypeList dumping |
| 486 | type->GetFullCompilerType(); |
| 487 | ++num_added; |
| 488 | } |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 489 | } |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 490 | } |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 491 | }; |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 492 | |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 493 | if (sc.function) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 494 | auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( |
| 495 | sc.function->GetID()); |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 496 | if (!pdb_func) |
| 497 | return 0; |
| 498 | ParseTypesByTagFn(*pdb_func); |
| 499 | } else { |
| 500 | ParseTypesByTagFn(*compiland); |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 501 | |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 502 | // Also parse global types particularly coming from this compiland. |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 503 | // Unfortunately, PDB has no compiland information for each global type. We |
| 504 | // have to parse them all. But ensure we only do this once. |
Aaron Smith | 66b8407 | 2018-03-14 04:05:27 +0000 | [diff] [blame] | 505 | static bool parse_all_global_types = false; |
| 506 | if (!parse_all_global_types) { |
| 507 | ParseTypesByTagFn(*m_global_scope_up); |
| 508 | parse_all_global_types = true; |
| 509 | } |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 510 | } |
| 511 | return num_added; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | size_t |
| 515 | SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 516 | if (!sc.comp_unit) |
| 517 | return 0; |
| 518 | |
| 519 | size_t num_added = 0; |
| 520 | if (sc.function) { |
| 521 | auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>( |
| 522 | sc.function->GetID()); |
| 523 | if (!pdb_func) |
| 524 | return 0; |
| 525 | |
| 526 | num_added += ParseVariables(sc, *pdb_func); |
| 527 | sc.function->GetBlock(false).SetDidParseVariables(true, true); |
| 528 | } else if (sc.comp_unit) { |
| 529 | auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 530 | if (!compiland) |
| 531 | return 0; |
| 532 | |
| 533 | if (sc.comp_unit->GetVariableList(false)) |
| 534 | return 0; |
| 535 | |
| 536 | auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); |
| 537 | if (results && results->getChildCount()) { |
| 538 | while (auto result = results->getNext()) { |
Aleksandr Urakov | 356aa4a | 2018-10-23 08:29:17 +0000 | [diff] [blame] | 539 | auto cu_id = GetCompilandId(*result); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 540 | // FIXME: We are not able to determine variable's compile unit. |
| 541 | if (cu_id == 0) |
| 542 | continue; |
| 543 | |
| 544 | if (cu_id == sc.comp_unit->GetID()) |
| 545 | num_added += ParseVariables(sc, *result); |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | // FIXME: A `file static` or `global constant` variable appears both in |
| 550 | // compiland's children and global scope's children with unexpectedly |
| 551 | // different symbol's Id making it ambiguous. |
| 552 | |
| 553 | // FIXME: 'local constant', for example, const char var[] = "abc", declared |
| 554 | // in a function scope, can't be found in PDB. |
| 555 | |
| 556 | // Parse variables in this compiland. |
| 557 | num_added += ParseVariables(sc, *compiland); |
| 558 | } |
| 559 | |
| 560 | return num_added; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { |
| 564 | auto find_result = m_types.find(type_uid); |
| 565 | if (find_result != m_types.end()) |
| 566 | return find_result->second.get(); |
| 567 | |
| 568 | TypeSystem *type_system = |
| 569 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 570 | ClangASTContext *clang_type_system = |
| 571 | llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
| 572 | if (!clang_type_system) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 573 | return nullptr; |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 574 | PDBASTParser *pdb = clang_type_system->GetPDBParser(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 575 | if (!pdb) |
| 576 | return nullptr; |
| 577 | |
| 578 | auto pdb_type = m_session_up->getSymbolById(type_uid); |
| 579 | if (pdb_type == nullptr) |
| 580 | return nullptr; |
| 581 | |
| 582 | lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame] | 583 | if (result) { |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 584 | m_types.insert(std::make_pair(type_uid, result)); |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 585 | auto type_list = GetTypeList(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 586 | if (type_list) |
| 587 | type_list->Insert(result); |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 588 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 589 | return result.get(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 592 | llvm::Optional<SymbolFile::ArrayInfo> SymbolFilePDB::GetDynamicArrayInfoForUID( |
| 593 | lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { |
| 594 | return llvm::None; |
| 595 | } |
| 596 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 597 | bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 598 | std::lock_guard<std::recursive_mutex> guard( |
| 599 | GetObjectFile()->GetModule()->GetMutex()); |
| 600 | |
| 601 | ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( |
| 602 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); |
| 603 | if (!clang_ast_ctx) |
| 604 | return false; |
| 605 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 606 | PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); |
Aleksandr Urakov | 7d2a74f | 2018-08-14 07:57:44 +0000 | [diff] [blame] | 607 | if (!pdb) |
| 608 | return false; |
| 609 | |
| 610 | return pdb->CompleteTypeFromPDB(compiler_type); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 614 | ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( |
| 615 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); |
| 616 | if (!clang_ast_ctx) |
| 617 | return CompilerDecl(); |
| 618 | |
| 619 | PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); |
| 620 | if (!pdb) |
| 621 | return CompilerDecl(); |
| 622 | |
| 623 | auto symbol = m_session_up->getSymbolById(uid); |
| 624 | if (!symbol) |
| 625 | return CompilerDecl(); |
| 626 | |
| 627 | auto decl = pdb->GetDeclForSymbol(*symbol); |
| 628 | if (!decl) |
| 629 | return CompilerDecl(); |
| 630 | |
| 631 | return CompilerDecl(clang_ast_ctx, decl); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | lldb_private::CompilerDeclContext |
| 635 | SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 636 | ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( |
| 637 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); |
| 638 | if (!clang_ast_ctx) |
| 639 | return CompilerDeclContext(); |
| 640 | |
| 641 | PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); |
| 642 | if (!pdb) |
| 643 | return CompilerDeclContext(); |
| 644 | |
| 645 | auto symbol = m_session_up->getSymbolById(uid); |
| 646 | if (!symbol) |
| 647 | return CompilerDeclContext(); |
| 648 | |
| 649 | auto decl_context = pdb->GetDeclContextForSymbol(*symbol); |
| 650 | if (!decl_context) |
| 651 | return GetDeclContextContainingUID(uid); |
| 652 | |
| 653 | return CompilerDeclContext(clang_ast_ctx, decl_context); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | lldb_private::CompilerDeclContext |
| 657 | SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 658 | ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( |
| 659 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); |
| 660 | if (!clang_ast_ctx) |
| 661 | return CompilerDeclContext(); |
| 662 | |
| 663 | PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); |
| 664 | if (!pdb) |
| 665 | return CompilerDeclContext(); |
| 666 | |
| 667 | auto symbol = m_session_up->getSymbolById(uid); |
| 668 | if (!symbol) |
| 669 | return CompilerDeclContext(); |
| 670 | |
| 671 | auto decl_context = pdb->GetDeclContextContainingSymbol(*symbol); |
| 672 | assert(decl_context); |
| 673 | |
| 674 | return CompilerDeclContext(clang_ast_ctx, decl_context); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 675 | } |
| 676 | |
| 677 | void SymbolFilePDB::ParseDeclsForContext( |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 678 | lldb_private::CompilerDeclContext decl_ctx) { |
| 679 | ClangASTContext *clang_ast_ctx = llvm::dyn_cast_or_null<ClangASTContext>( |
| 680 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus)); |
| 681 | if (!clang_ast_ctx) |
| 682 | return; |
| 683 | |
| 684 | PDBASTParser *pdb = clang_ast_ctx->GetPDBParser(); |
| 685 | if (!pdb) |
| 686 | return; |
| 687 | |
| 688 | pdb->ParseDeclsForDeclContext( |
| 689 | static_cast<clang::DeclContext *>(decl_ctx.GetOpaqueDeclContext())); |
| 690 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 691 | |
| 692 | uint32_t |
| 693 | SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 +0000 | [diff] [blame] | 694 | SymbolContextItem resolve_scope, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 695 | lldb_private::SymbolContext &sc) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 696 | uint32_t resolved_flags = 0; |
Pavel Labath | 4d4d63e | 2018-02-09 11:37:01 +0000 | [diff] [blame] | 697 | if (resolve_scope & eSymbolContextCompUnit || |
| 698 | resolve_scope & eSymbolContextVariable || |
| 699 | resolve_scope & eSymbolContextFunction || |
| 700 | resolve_scope & eSymbolContextBlock || |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 701 | resolve_scope & eSymbolContextLineEntry) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 702 | auto cu_sp = GetCompileUnitContainsAddress(so_addr); |
| 703 | if (!cu_sp) { |
| 704 | if (resolved_flags | eSymbolContextVariable) { |
| 705 | // TODO: Resolve variables |
| 706 | } |
| 707 | return 0; |
| 708 | } |
| 709 | sc.comp_unit = cu_sp.get(); |
| 710 | resolved_flags |= eSymbolContextCompUnit; |
| 711 | lldbassert(sc.module_sp == cu_sp->GetModule()); |
Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 712 | } |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 713 | |
Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 714 | if (resolve_scope & eSymbolContextFunction || |
| 715 | resolve_scope & eSymbolContextBlock) { |
Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 716 | addr_t file_vm_addr = so_addr.GetFileAddress(); |
| 717 | auto symbol_up = |
| 718 | m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function); |
| 719 | if (symbol_up) { |
| 720 | auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); |
| 721 | assert(pdb_func); |
| 722 | auto func_uid = pdb_func->getSymIndexId(); |
| 723 | sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); |
| 724 | if (sc.function == nullptr) |
| 725 | sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); |
| 726 | if (sc.function) { |
| 727 | resolved_flags |= eSymbolContextFunction; |
| 728 | if (resolve_scope & eSymbolContextBlock) { |
Aleksandr Urakov | 398f81b | 2018-08-29 07:26:11 +0000 | [diff] [blame] | 729 | auto block_symbol = m_session_up->findSymbolByAddress( |
| 730 | file_vm_addr, PDB_SymType::Block); |
| 731 | auto block_id = block_symbol ? block_symbol->getSymIndexId() |
| 732 | : sc.function->GetID(); |
| 733 | sc.block = sc.function->GetBlock(true).FindBlockByID(block_id); |
Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 734 | if (sc.block) |
| 735 | resolved_flags |= eSymbolContextBlock; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 736 | } |
| 737 | } |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 738 | } |
| 739 | } |
Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 740 | |
| 741 | if (resolve_scope & eSymbolContextLineEntry) { |
| 742 | if (auto *line_table = sc.comp_unit->GetLineTable()) { |
| 743 | Address addr(so_addr); |
| 744 | if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) |
| 745 | resolved_flags |= eSymbolContextLineEntry; |
| 746 | } |
| 747 | } |
| 748 | |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 749 | return resolved_flags; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | uint32_t SymbolFilePDB::ResolveSymbolContext( |
| 753 | const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 +0000 | [diff] [blame] | 754 | SymbolContextItem resolve_scope, lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 755 | const size_t old_size = sc_list.GetSize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 756 | if (resolve_scope & lldb::eSymbolContextCompUnit) { |
| 757 | // Locate all compilation units with line numbers referencing the specified |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 758 | // file. For example, if `file_spec` is <vector>, then this should return |
| 759 | // all source files and header files that reference <vector>, either |
| 760 | // directly or indirectly. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 761 | auto compilands = m_session_up->findCompilandsForSourceFile( |
| 762 | file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); |
| 763 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 764 | if (!compilands) |
| 765 | return 0; |
| 766 | |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 767 | // For each one, either find its previously parsed data or parse it afresh |
| 768 | // and add it to the symbol context list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 769 | while (auto compiland = compilands->getNext()) { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 770 | // If we're not checking inlines, then don't add line information for |
| 771 | // this file unless the FileSpec matches. For inline functions, we don't |
| 772 | // have to match the FileSpec since they could be defined in headers |
| 773 | // other than file specified in FileSpec. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 774 | if (!check_inlines) { |
Aaron Smith | 487b0c6 | 2018-03-20 00:18:22 +0000 | [diff] [blame] | 775 | std::string source_file = compiland->getSourceFileFullPath(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 776 | if (source_file.empty()) |
| 777 | continue; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 778 | FileSpec this_spec(source_file, FileSpec::Style::windows); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 779 | bool need_full_match = !file_spec.GetDirectory().IsEmpty(); |
| 780 | if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 781 | continue; |
| 782 | } |
| 783 | |
| 784 | SymbolContext sc; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 785 | auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame] | 786 | if (!cu) |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 787 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 788 | sc.comp_unit = cu.get(); |
| 789 | sc.module_sp = cu->GetModule(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 790 | |
| 791 | // 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] | 792 | // table that match the requested line (or all lines if `line` == 0). |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 793 | if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock | |
| 794 | eSymbolContextLineEntry)) { |
| 795 | bool has_line_table = ParseCompileUnitLineTable(sc, line); |
| 796 | |
| 797 | if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) { |
| 798 | // The query asks for line entries, but we can't get them for the |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 799 | // compile unit. This is not normal for `line` = 0. So just assert |
| 800 | // it. |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 801 | assert(line && "Couldn't get all line entries!\n"); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 802 | |
| 803 | // Current compiland does not have the requested line. Search next. |
| 804 | continue; |
| 805 | } |
| 806 | |
| 807 | if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) { |
| 808 | if (!has_line_table) |
| 809 | continue; |
| 810 | |
| 811 | auto *line_table = sc.comp_unit->GetLineTable(); |
| 812 | lldbassert(line_table); |
| 813 | |
| 814 | uint32_t num_line_entries = line_table->GetSize(); |
| 815 | // Skip the terminal line entry. |
| 816 | --num_line_entries; |
| 817 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 818 | // If `line `!= 0, see if we can resolve function for each line entry |
| 819 | // in the line table. |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 820 | for (uint32_t line_idx = 0; line && line_idx < num_line_entries; |
| 821 | ++line_idx) { |
| 822 | if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry)) |
| 823 | continue; |
| 824 | |
| 825 | auto file_vm_addr = |
| 826 | sc.line_entry.range.GetBaseAddress().GetFileAddress(); |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 827 | if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 828 | continue; |
| 829 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 830 | auto symbol_up = m_session_up->findSymbolByAddress( |
| 831 | file_vm_addr, PDB_SymType::Function); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 832 | if (symbol_up) { |
| 833 | auto func_uid = symbol_up->getSymIndexId(); |
| 834 | sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get(); |
| 835 | if (sc.function == nullptr) { |
| 836 | auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get()); |
| 837 | assert(pdb_func); |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 838 | sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 839 | } |
| 840 | if (sc.function && (resolve_scope & eSymbolContextBlock)) { |
| 841 | Block &block = sc.function->GetBlock(true); |
| 842 | sc.block = block.FindBlockByID(sc.function->GetID()); |
| 843 | } |
| 844 | } |
| 845 | sc_list.Append(sc); |
| 846 | } |
| 847 | } else if (has_line_table) { |
| 848 | // We can parse line table for the compile unit. But no query to |
| 849 | // resolve function or block. We append `sc` to the list anyway. |
| 850 | sc_list.Append(sc); |
| 851 | } |
| 852 | } else { |
| 853 | // No query for line entry, function or block. But we have a valid |
| 854 | // compile unit, append `sc` to the list. |
| 855 | sc_list.Append(sc); |
| 856 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 859 | return sc_list.GetSize() - old_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 862 | std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) { |
Aleksandr Urakov | 356aa4a | 2018-10-23 08:29:17 +0000 | [diff] [blame] | 863 | // Cache public names at first |
| 864 | if (m_public_names.empty()) |
| 865 | if (auto result_up = |
| 866 | m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol)) |
| 867 | while (auto symbol_up = result_up->getNext()) |
| 868 | if (auto addr = symbol_up->getRawSymbol().getVirtualAddress()) |
| 869 | m_public_names[addr] = symbol_up->getRawSymbol().getName(); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 870 | |
Aleksandr Urakov | 356aa4a | 2018-10-23 08:29:17 +0000 | [diff] [blame] | 871 | // Look up the name in the cache |
| 872 | return m_public_names.lookup(pdb_data.getVirtualAddress()); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | VariableSP SymbolFilePDB::ParseVariableForPDBData( |
| 876 | const lldb_private::SymbolContext &sc, |
| 877 | const llvm::pdb::PDBSymbolData &pdb_data) { |
| 878 | VariableSP var_sp; |
| 879 | uint32_t var_uid = pdb_data.getSymIndexId(); |
| 880 | auto result = m_variables.find(var_uid); |
| 881 | if (result != m_variables.end()) |
| 882 | return result->second; |
| 883 | |
| 884 | ValueType scope = eValueTypeInvalid; |
| 885 | bool is_static_member = false; |
| 886 | bool is_external = false; |
| 887 | bool is_artificial = false; |
| 888 | |
| 889 | switch (pdb_data.getDataKind()) { |
| 890 | case PDB_DataKind::Global: |
| 891 | scope = eValueTypeVariableGlobal; |
| 892 | is_external = true; |
| 893 | break; |
| 894 | case PDB_DataKind::Local: |
| 895 | scope = eValueTypeVariableLocal; |
| 896 | break; |
| 897 | case PDB_DataKind::FileStatic: |
| 898 | scope = eValueTypeVariableStatic; |
| 899 | break; |
| 900 | case PDB_DataKind::StaticMember: |
| 901 | is_static_member = true; |
| 902 | scope = eValueTypeVariableStatic; |
| 903 | break; |
| 904 | case PDB_DataKind::Member: |
| 905 | scope = eValueTypeVariableStatic; |
| 906 | break; |
| 907 | case PDB_DataKind::Param: |
| 908 | scope = eValueTypeVariableArgument; |
| 909 | break; |
| 910 | case PDB_DataKind::Constant: |
| 911 | scope = eValueTypeConstResult; |
| 912 | break; |
| 913 | default: |
| 914 | break; |
| 915 | } |
| 916 | |
| 917 | switch (pdb_data.getLocationType()) { |
| 918 | case PDB_LocType::TLS: |
| 919 | scope = eValueTypeVariableThreadLocal; |
| 920 | break; |
| 921 | case PDB_LocType::RegRel: { |
| 922 | // It is a `this` pointer. |
| 923 | if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) { |
| 924 | scope = eValueTypeVariableArgument; |
| 925 | is_artificial = true; |
| 926 | } |
| 927 | } break; |
| 928 | default: |
| 929 | break; |
| 930 | } |
| 931 | |
| 932 | Declaration decl; |
| 933 | if (!is_artificial && !pdb_data.isCompilerGenerated()) { |
| 934 | if (auto lines = pdb_data.getLineNumbers()) { |
| 935 | if (auto first_line = lines->getNext()) { |
| 936 | uint32_t src_file_id = first_line->getSourceFileId(); |
| 937 | auto src_file = m_session_up->getSourceFileById(src_file_id); |
| 938 | if (src_file) { |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 939 | FileSpec spec(src_file->getFileName()); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 940 | decl.SetFile(spec); |
| 941 | decl.SetColumn(first_line->getColumnNumber()); |
| 942 | decl.SetLine(first_line->getLineNumber()); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | } |
| 947 | |
| 948 | Variable::RangeList ranges; |
| 949 | SymbolContextScope *context_scope = sc.comp_unit; |
| 950 | if (scope == eValueTypeVariableLocal) { |
| 951 | if (sc.function) { |
| 952 | context_scope = sc.function->GetBlock(true).FindBlockByID( |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 953 | pdb_data.getLexicalParentId()); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 954 | if (context_scope == nullptr) |
| 955 | context_scope = sc.function; |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | SymbolFileTypeSP type_sp = |
| 960 | std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId()); |
| 961 | |
| 962 | auto var_name = pdb_data.getName(); |
| 963 | auto mangled = GetMangledForPDBData(pdb_data); |
| 964 | auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str(); |
| 965 | |
Jonas Devlieghere | 924d560 | 2018-07-13 10:29:27 +0000 | [diff] [blame] | 966 | bool is_constant; |
| 967 | DWARFExpression location = ConvertPDBLocationToDWARFExpression( |
| 968 | GetObjectFile()->GetModule(), pdb_data, is_constant); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 969 | |
| 970 | var_sp = std::make_shared<Variable>( |
| 971 | var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope, |
| 972 | ranges, &decl, location, is_external, is_artificial, is_static_member); |
Jonas Devlieghere | 924d560 | 2018-07-13 10:29:27 +0000 | [diff] [blame] | 973 | var_sp->SetLocationIsConstantValueData(is_constant); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 974 | |
| 975 | m_variables.insert(std::make_pair(var_uid, var_sp)); |
| 976 | return var_sp; |
| 977 | } |
| 978 | |
| 979 | size_t |
| 980 | SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc, |
| 981 | const llvm::pdb::PDBSymbol &pdb_symbol, |
| 982 | lldb_private::VariableList *variable_list) { |
| 983 | size_t num_added = 0; |
| 984 | |
| 985 | if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) { |
| 986 | VariableListSP local_variable_list_sp; |
| 987 | |
| 988 | auto result = m_variables.find(pdb_data->getSymIndexId()); |
| 989 | if (result != m_variables.end()) { |
| 990 | if (variable_list) |
| 991 | variable_list->AddVariableIfUnique(result->second); |
| 992 | } else { |
| 993 | // Prepare right VariableList for this variable. |
| 994 | if (auto lexical_parent = pdb_data->getLexicalParent()) { |
| 995 | switch (lexical_parent->getSymTag()) { |
| 996 | case PDB_SymType::Exe: |
| 997 | assert(sc.comp_unit); |
| 998 | LLVM_FALLTHROUGH; |
| 999 | case PDB_SymType::Compiland: { |
| 1000 | if (sc.comp_unit) { |
| 1001 | local_variable_list_sp = sc.comp_unit->GetVariableList(false); |
| 1002 | if (!local_variable_list_sp) { |
| 1003 | local_variable_list_sp = std::make_shared<VariableList>(); |
| 1004 | sc.comp_unit->SetVariableList(local_variable_list_sp); |
| 1005 | } |
| 1006 | } |
| 1007 | } break; |
| 1008 | case PDB_SymType::Block: |
| 1009 | case PDB_SymType::Function: { |
| 1010 | if (sc.function) { |
| 1011 | Block *block = sc.function->GetBlock(true).FindBlockByID( |
| 1012 | lexical_parent->getSymIndexId()); |
| 1013 | if (block) { |
| 1014 | local_variable_list_sp = block->GetBlockVariableList(false); |
| 1015 | if (!local_variable_list_sp) { |
| 1016 | local_variable_list_sp = std::make_shared<VariableList>(); |
| 1017 | block->SetVariableList(local_variable_list_sp); |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | } break; |
| 1022 | default: |
| 1023 | break; |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | if (local_variable_list_sp) { |
| 1028 | if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) { |
| 1029 | local_variable_list_sp->AddVariableIfUnique(var_sp); |
| 1030 | if (variable_list) |
| 1031 | variable_list->AddVariableIfUnique(var_sp); |
| 1032 | ++num_added; |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | if (auto results = pdb_symbol.findAllChildren()) { |
| 1039 | while (auto result = results->getNext()) |
| 1040 | num_added += ParseVariables(sc, *result, variable_list); |
| 1041 | } |
| 1042 | |
| 1043 | return num_added; |
| 1044 | } |
| 1045 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1046 | uint32_t SymbolFilePDB::FindGlobalVariables( |
| 1047 | const lldb_private::ConstString &name, |
Pavel Labath | 34cda14 | 2018-05-31 09:46:26 +0000 | [diff] [blame] | 1048 | const lldb_private::CompilerDeclContext *parent_decl_ctx, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1049 | uint32_t max_matches, lldb_private::VariableList &variables) { |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1050 | if (!parent_decl_ctx) |
| 1051 | parent_decl_ctx = m_tu_decl_ctx_up.get(); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 1052 | if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) |
| 1053 | return 0; |
| 1054 | if (name.IsEmpty()) |
| 1055 | return 0; |
| 1056 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1057 | auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 1058 | if (!results) |
| 1059 | return 0; |
| 1060 | |
| 1061 | uint32_t matches = 0; |
| 1062 | size_t old_size = variables.GetSize(); |
| 1063 | while (auto result = results->getNext()) { |
| 1064 | auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get()); |
| 1065 | if (max_matches > 0 && matches >= max_matches) |
| 1066 | break; |
| 1067 | |
| 1068 | SymbolContext sc; |
| 1069 | sc.module_sp = m_obj_file->GetModule(); |
| 1070 | lldbassert(sc.module_sp.get()); |
| 1071 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1072 | if (!name.GetStringRef().equals( |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 1073 | MSVCUndecoratedNameParser::DropScope(pdb_data->getName()))) |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1074 | continue; |
| 1075 | |
Aleksandr Urakov | 356aa4a | 2018-10-23 08:29:17 +0000 | [diff] [blame] | 1076 | sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); |
| 1077 | // FIXME: We are not able to determine the compile unit. |
| 1078 | if (sc.comp_unit == nullptr) |
| 1079 | continue; |
| 1080 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1081 | auto actual_parent_decl_ctx = |
| 1082 | GetDeclContextContainingUID(result->getSymIndexId()); |
| 1083 | if (actual_parent_decl_ctx != *parent_decl_ctx) |
| 1084 | continue; |
| 1085 | |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 1086 | ParseVariables(sc, *pdb_data, &variables); |
| 1087 | matches = variables.GetSize() - old_size; |
| 1088 | } |
| 1089 | |
| 1090 | return matches; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | uint32_t |
| 1094 | SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, |
Pavel Labath | 34cda14 | 2018-05-31 09:46:26 +0000 | [diff] [blame] | 1095 | uint32_t max_matches, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1096 | lldb_private::VariableList &variables) { |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 1097 | if (!regex.IsValid()) |
| 1098 | return 0; |
| 1099 | auto results = m_global_scope_up->findAllChildren<PDBSymbolData>(); |
| 1100 | if (!results) |
| 1101 | return 0; |
| 1102 | |
| 1103 | uint32_t matches = 0; |
| 1104 | size_t old_size = variables.GetSize(); |
| 1105 | while (auto pdb_data = results->getNext()) { |
| 1106 | if (max_matches > 0 && matches >= max_matches) |
| 1107 | break; |
| 1108 | |
| 1109 | auto var_name = pdb_data->getName(); |
| 1110 | if (var_name.empty()) |
| 1111 | continue; |
| 1112 | if (!regex.Execute(var_name)) |
| 1113 | continue; |
| 1114 | SymbolContext sc; |
| 1115 | sc.module_sp = m_obj_file->GetModule(); |
| 1116 | lldbassert(sc.module_sp.get()); |
| 1117 | |
Aleksandr Urakov | 356aa4a | 2018-10-23 08:29:17 +0000 | [diff] [blame] | 1118 | sc.comp_unit = ParseCompileUnitForUID(GetCompilandId(*pdb_data)).get(); |
Aaron Smith | cab0d23 | 2018-05-23 01:52:42 +0000 | [diff] [blame] | 1119 | // FIXME: We are not able to determine the compile unit. |
| 1120 | if (sc.comp_unit == nullptr) |
| 1121 | continue; |
| 1122 | |
| 1123 | ParseVariables(sc, *pdb_data, &variables); |
| 1124 | matches = variables.GetSize() - old_size; |
| 1125 | } |
| 1126 | |
| 1127 | return matches; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1128 | } |
| 1129 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1130 | bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func, |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1131 | bool include_inlines, |
| 1132 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1133 | lldb_private::SymbolContext sc; |
Aaron Smith | a3a8cc8 | 2018-03-20 00:34:18 +0000 | [diff] [blame] | 1134 | sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1135 | if (!sc.comp_unit) |
| 1136 | return false; |
| 1137 | sc.module_sp = sc.comp_unit->GetModule(); |
Aaron Smith | a3a8cc8 | 2018-03-20 00:34:18 +0000 | [diff] [blame] | 1138 | sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1139 | if (!sc.function) |
| 1140 | return false; |
| 1141 | |
| 1142 | sc_list.Append(sc); |
| 1143 | return true; |
| 1144 | } |
| 1145 | |
| 1146 | bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines, |
| 1147 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1148 | auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1149 | if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute())) |
| 1150 | return false; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1151 | return ResolveFunction(*pdb_func_up, include_inlines, sc_list); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
| 1154 | void SymbolFilePDB::CacheFunctionNames() { |
| 1155 | if (!m_func_full_names.IsEmpty()) |
| 1156 | return; |
| 1157 | |
| 1158 | std::map<uint64_t, uint32_t> addr_ids; |
| 1159 | |
| 1160 | if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) { |
| 1161 | while (auto pdb_func_up = results_up->getNext()) { |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1162 | if (pdb_func_up->isCompilerGenerated()) |
| 1163 | continue; |
| 1164 | |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1165 | auto name = pdb_func_up->getName(); |
| 1166 | auto demangled_name = pdb_func_up->getUndecoratedName(); |
| 1167 | if (name.empty() && demangled_name.empty()) |
| 1168 | continue; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1169 | |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1170 | auto uid = pdb_func_up->getSymIndexId(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1171 | if (!demangled_name.empty() && pdb_func_up->getVirtualAddress()) |
| 1172 | addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid)); |
| 1173 | |
| 1174 | if (auto parent = pdb_func_up->getClassParent()) { |
| 1175 | |
| 1176 | // PDB have symbols for class/struct methods or static methods in Enum |
| 1177 | // Class. We won't bother to check if the parent is UDT or Enum here. |
| 1178 | m_func_method_names.Append(ConstString(name), uid); |
| 1179 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1180 | // To search a method name, like NS::Class:MemberFunc, LLDB searches |
| 1181 | // its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does |
| 1182 | // not have inforamtion of this, we extract base names and cache them |
| 1183 | // by our own effort. |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 1184 | llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1185 | if (!basename.empty()) |
| 1186 | m_func_base_names.Append(ConstString(basename), uid); |
| 1187 | else { |
| 1188 | m_func_base_names.Append(ConstString(name), uid); |
| 1189 | } |
| 1190 | |
| 1191 | if (!demangled_name.empty()) |
| 1192 | m_func_full_names.Append(ConstString(demangled_name), uid); |
| 1193 | |
| 1194 | } else { |
| 1195 | // Handle not-method symbols. |
| 1196 | |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 1197 | // The function name might contain namespace, or its lexical scope. |
| 1198 | llvm::StringRef basename = MSVCUndecoratedNameParser::DropScope(name); |
| 1199 | if (!basename.empty()) |
| 1200 | m_func_base_names.Append(ConstString(basename), uid); |
| 1201 | else |
| 1202 | m_func_base_names.Append(ConstString(name), uid); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1203 | |
| 1204 | if (name == "main") { |
| 1205 | m_func_full_names.Append(ConstString(name), uid); |
| 1206 | |
| 1207 | if (!demangled_name.empty() && name != demangled_name) { |
| 1208 | m_func_full_names.Append(ConstString(demangled_name), uid); |
| 1209 | m_func_base_names.Append(ConstString(demangled_name), uid); |
| 1210 | } |
| 1211 | } else if (!demangled_name.empty()) { |
| 1212 | m_func_full_names.Append(ConstString(demangled_name), uid); |
| 1213 | } else { |
| 1214 | m_func_full_names.Append(ConstString(name), uid); |
| 1215 | } |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | if (auto results_up = |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1221 | m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1222 | while (auto pub_sym_up = results_up->getNext()) { |
| 1223 | if (!pub_sym_up->isFunction()) |
| 1224 | continue; |
| 1225 | auto name = pub_sym_up->getName(); |
| 1226 | if (name.empty()) |
| 1227 | continue; |
| 1228 | |
| 1229 | if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1230 | auto vm_addr = pub_sym_up->getVirtualAddress(); |
| 1231 | |
| 1232 | // PDB public symbol has mangled name for its associated function. |
| 1233 | if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) { |
| 1234 | // Cache mangled name. |
| 1235 | m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]); |
| 1236 | } |
| 1237 | } |
| 1238 | } |
| 1239 | } |
| 1240 | // Sort them before value searching is working properly |
| 1241 | m_func_full_names.Sort(); |
| 1242 | m_func_full_names.SizeToFit(); |
| 1243 | m_func_method_names.Sort(); |
| 1244 | m_func_method_names.SizeToFit(); |
| 1245 | m_func_base_names.Sort(); |
| 1246 | m_func_base_names.SizeToFit(); |
| 1247 | } |
| 1248 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1249 | uint32_t SymbolFilePDB::FindFunctions( |
| 1250 | const lldb_private::ConstString &name, |
| 1251 | const lldb_private::CompilerDeclContext *parent_decl_ctx, |
Zachary Turner | 117b1fa | 2018-10-25 20:45:40 +0000 | [diff] [blame] | 1252 | FunctionNameType name_type_mask, bool include_inlines, bool append, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1253 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1254 | if (!append) |
| 1255 | sc_list.Clear(); |
| 1256 | lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0); |
| 1257 | |
| 1258 | if (name_type_mask == eFunctionNameTypeNone) |
| 1259 | return 0; |
| 1260 | if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) |
| 1261 | return 0; |
| 1262 | if (name.IsEmpty()) |
| 1263 | return 0; |
| 1264 | |
| 1265 | auto old_size = sc_list.GetSize(); |
Pavel Labath | 4d4d63e | 2018-02-09 11:37:01 +0000 | [diff] [blame] | 1266 | if (name_type_mask & eFunctionNameTypeFull || |
| 1267 | name_type_mask & eFunctionNameTypeBase || |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1268 | name_type_mask & eFunctionNameTypeMethod) { |
| 1269 | CacheFunctionNames(); |
| 1270 | |
| 1271 | std::set<uint32_t> resolved_ids; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1272 | auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids, |
| 1273 | this](UniqueCStringMap<uint32_t> &Names) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1274 | std::vector<uint32_t> ids; |
| 1275 | if (Names.GetValues(name, ids)) { |
| 1276 | for (auto id : ids) { |
| 1277 | if (resolved_ids.find(id) == resolved_ids.end()) { |
| 1278 | if (ResolveFunction(id, include_inlines, sc_list)) |
| 1279 | resolved_ids.insert(id); |
| 1280 | } |
| 1281 | } |
| 1282 | } |
| 1283 | }; |
| 1284 | if (name_type_mask & eFunctionNameTypeFull) { |
| 1285 | ResolveFn(m_func_full_names); |
| 1286 | } |
| 1287 | if (name_type_mask & eFunctionNameTypeBase) { |
| 1288 | ResolveFn(m_func_base_names); |
| 1289 | } |
| 1290 | if (name_type_mask & eFunctionNameTypeMethod) { |
| 1291 | ResolveFn(m_func_method_names); |
| 1292 | } |
| 1293 | } |
| 1294 | return sc_list.GetSize() - old_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | uint32_t |
| 1298 | SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, |
| 1299 | bool include_inlines, bool append, |
| 1300 | lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1301 | if (!append) |
| 1302 | sc_list.Clear(); |
| 1303 | if (!regex.IsValid()) |
| 1304 | return 0; |
| 1305 | |
| 1306 | auto old_size = sc_list.GetSize(); |
| 1307 | CacheFunctionNames(); |
| 1308 | |
| 1309 | std::set<uint32_t> resolved_ids; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1310 | auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids, |
| 1311 | this](UniqueCStringMap<uint32_t> &Names) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1312 | std::vector<uint32_t> ids; |
| 1313 | if (Names.GetValues(regex, ids)) { |
| 1314 | for (auto id : ids) { |
| 1315 | if (resolved_ids.find(id) == resolved_ids.end()) |
| 1316 | if (ResolveFunction(id, include_inlines, sc_list)) |
| 1317 | resolved_ids.insert(id); |
| 1318 | } |
| 1319 | } |
| 1320 | }; |
| 1321 | ResolveFn(m_func_full_names); |
| 1322 | ResolveFn(m_func_base_names); |
| 1323 | |
| 1324 | return sc_list.GetSize() - old_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | void SymbolFilePDB::GetMangledNamesForFunction( |
| 1328 | const std::string &scope_qualified_name, |
| 1329 | std::vector<lldb_private::ConstString> &mangled_names) {} |
| 1330 | |
| 1331 | uint32_t SymbolFilePDB::FindTypes( |
| 1332 | const lldb_private::SymbolContext &sc, |
| 1333 | const lldb_private::ConstString &name, |
| 1334 | const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, |
| 1335 | uint32_t max_matches, |
| 1336 | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
| 1337 | lldb_private::TypeMap &types) { |
| 1338 | if (!append) |
| 1339 | types.Clear(); |
| 1340 | if (!name) |
| 1341 | return 0; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1342 | if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx)) |
| 1343 | return 0; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1344 | |
| 1345 | searched_symbol_files.clear(); |
| 1346 | searched_symbol_files.insert(this); |
| 1347 | |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 1348 | // There is an assumption 'name' is not a regex |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 1349 | FindTypesByName(name.GetStringRef(), parent_decl_ctx, max_matches, types); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1350 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1351 | return types.GetSize(); |
| 1352 | } |
| 1353 | |
Zachary Turner | 4911023 | 2018-11-05 17:40:28 +0000 | [diff] [blame] | 1354 | void SymbolFilePDB::DumpClangAST(Stream &s) { |
| 1355 | auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 1356 | auto clang = llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
| 1357 | if (!clang) |
| 1358 | return; |
| 1359 | clang->Dump(s); |
| 1360 | } |
| 1361 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1362 | void SymbolFilePDB::FindTypesByRegex( |
| 1363 | const lldb_private::RegularExpression ®ex, uint32_t max_matches, |
| 1364 | lldb_private::TypeMap &types) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1365 | // 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] | 1366 | // space as much as possible since this searches EVERYTHING in the PDB, |
| 1367 | // manually doing regex comparisons. PDB library isn't optimized for regex |
| 1368 | // 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] | 1369 | // 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] | 1370 | // and do a regex comparison against each of them. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1371 | PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, |
| 1372 | PDB_SymType::UDT}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1373 | std::unique_ptr<IPDBEnumSymbols> results; |
| 1374 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1375 | uint32_t matches = 0; |
| 1376 | |
| 1377 | for (auto tag : tags_to_search) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1378 | results = m_global_scope_up->findAllChildren(tag); |
| 1379 | if (!results) |
| 1380 | continue; |
| 1381 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1382 | while (auto result = results->getNext()) { |
| 1383 | if (max_matches > 0 && matches >= max_matches) |
| 1384 | break; |
| 1385 | |
| 1386 | std::string type_name; |
| 1387 | if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) |
| 1388 | type_name = enum_type->getName(); |
| 1389 | else if (auto typedef_type = |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1390 | llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1391 | type_name = typedef_type->getName(); |
| 1392 | else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) |
| 1393 | type_name = class_type->getName(); |
| 1394 | else { |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1395 | // We're looking only for types that have names. Skip symbols, as well |
| 1396 | // as unnamed types such as arrays, pointers, etc. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1397 | continue; |
| 1398 | } |
| 1399 | |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 1400 | if (!regex.Execute(type_name)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1401 | continue; |
| 1402 | |
| 1403 | // This should cause the type to get cached and stored in the `m_types` |
| 1404 | // lookup. |
| 1405 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 1406 | continue; |
| 1407 | |
| 1408 | auto iter = m_types.find(result->getSymIndexId()); |
| 1409 | if (iter == m_types.end()) |
| 1410 | continue; |
| 1411 | types.Insert(iter->second); |
| 1412 | ++matches; |
| 1413 | } |
| 1414 | } |
| 1415 | } |
| 1416 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1417 | void SymbolFilePDB::FindTypesByName( |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 1418 | llvm::StringRef name, |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1419 | const lldb_private::CompilerDeclContext *parent_decl_ctx, |
| 1420 | uint32_t max_matches, lldb_private::TypeMap &types) { |
| 1421 | if (!parent_decl_ctx) |
| 1422 | parent_decl_ctx = m_tu_decl_ctx_up.get(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1423 | std::unique_ptr<IPDBEnumSymbols> results; |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1424 | if (name.empty()) |
| 1425 | return; |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1426 | results = m_global_scope_up->findAllChildren(PDB_SymType::None); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1427 | if (!results) |
| 1428 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1429 | |
| 1430 | uint32_t matches = 0; |
| 1431 | |
| 1432 | while (auto result = results->getNext()) { |
| 1433 | if (max_matches > 0 && matches >= max_matches) |
| 1434 | break; |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1435 | |
Aleksandr Urakov | c1e530e | 2018-11-06 08:02:55 +0000 | [diff] [blame^] | 1436 | if (MSVCUndecoratedNameParser::DropScope( |
| 1437 | result->getRawSymbol().getName()) != name) |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1438 | continue; |
| 1439 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1440 | switch (result->getSymTag()) { |
| 1441 | case PDB_SymType::Enum: |
| 1442 | case PDB_SymType::UDT: |
| 1443 | case PDB_SymType::Typedef: |
| 1444 | break; |
| 1445 | default: |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1446 | // We're looking only for types that have names. Skip symbols, as well |
| 1447 | // as unnamed types such as arrays, pointers, etc. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1448 | continue; |
| 1449 | } |
| 1450 | |
| 1451 | // This should cause the type to get cached and stored in the `m_types` |
| 1452 | // lookup. |
| 1453 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 1454 | continue; |
| 1455 | |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1456 | auto actual_parent_decl_ctx = |
| 1457 | GetDeclContextContainingUID(result->getSymIndexId()); |
| 1458 | if (actual_parent_decl_ctx != *parent_decl_ctx) |
| 1459 | continue; |
| 1460 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1461 | auto iter = m_types.find(result->getSymIndexId()); |
| 1462 | if (iter == m_types.end()) |
| 1463 | continue; |
| 1464 | types.Insert(iter->second); |
| 1465 | ++matches; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | size_t SymbolFilePDB::FindTypes( |
| 1470 | const std::vector<lldb_private::CompilerContext> &contexts, bool append, |
| 1471 | lldb_private::TypeMap &types) { |
| 1472 | return 0; |
| 1473 | } |
| 1474 | |
Aaron Smith | ec40f81 | 2018-01-23 20:35:19 +0000 | [diff] [blame] | 1475 | lldb_private::TypeList *SymbolFilePDB::GetTypeList() { |
| 1476 | return m_obj_file->GetModule()->GetTypeList(); |
| 1477 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1478 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1479 | void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol, |
| 1480 | uint32_t type_mask, |
| 1481 | TypeCollection &type_collection) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1482 | bool can_parse = false; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1483 | switch (pdb_symbol.getSymTag()) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1484 | case PDB_SymType::ArrayType: |
| 1485 | can_parse = ((type_mask & eTypeClassArray) != 0); |
| 1486 | break; |
| 1487 | case PDB_SymType::BuiltinType: |
| 1488 | can_parse = ((type_mask & eTypeClassBuiltin) != 0); |
| 1489 | break; |
| 1490 | case PDB_SymType::Enum: |
| 1491 | can_parse = ((type_mask & eTypeClassEnumeration) != 0); |
| 1492 | break; |
| 1493 | case PDB_SymType::Function: |
| 1494 | case PDB_SymType::FunctionSig: |
| 1495 | can_parse = ((type_mask & eTypeClassFunction) != 0); |
| 1496 | break; |
| 1497 | case PDB_SymType::PointerType: |
| 1498 | can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer | |
| 1499 | eTypeClassMemberPointer)) != 0); |
| 1500 | break; |
| 1501 | case PDB_SymType::Typedef: |
| 1502 | can_parse = ((type_mask & eTypeClassTypedef) != 0); |
| 1503 | break; |
| 1504 | case PDB_SymType::UDT: { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1505 | auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1506 | assert(udt); |
| 1507 | can_parse = (udt->getUdtKind() != PDB_UdtType::Interface && |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1508 | ((type_mask & (eTypeClassClass | eTypeClassStruct | |
| 1509 | eTypeClassUnion)) != 0)); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1510 | } break; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1511 | default: |
| 1512 | break; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | if (can_parse) { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1516 | if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1517 | auto result = |
| 1518 | std::find(type_collection.begin(), type_collection.end(), type); |
| 1519 | if (result == type_collection.end()) |
| 1520 | type_collection.push_back(type); |
| 1521 | } |
| 1522 | } |
| 1523 | |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1524 | auto results_up = pdb_symbol.findAllChildren(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1525 | while (auto symbol_up = results_up->getNext()) |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1526 | GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1529 | size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, |
Zachary Turner | 117b1fa | 2018-10-25 20:45:40 +0000 | [diff] [blame] | 1530 | TypeClass type_mask, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1531 | lldb_private::TypeList &type_list) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1532 | TypeCollection type_collection; |
| 1533 | uint32_t old_size = type_list.GetSize(); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1534 | CompileUnit *cu = |
| 1535 | sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1536 | if (cu) { |
| 1537 | auto compiland_up = GetPDBCompilandByUID(cu->GetID()); |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1538 | if (!compiland_up) |
| 1539 | return 0; |
| 1540 | GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1541 | } else { |
| 1542 | for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { |
| 1543 | auto cu_sp = ParseCompileUnitAtIndex(cu_idx); |
Aaron Smith | d5a925f | 2018-03-22 19:21:34 +0000 | [diff] [blame] | 1544 | if (cu_sp) { |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1545 | if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID())) |
| 1546 | GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | |
| 1551 | for (auto type : type_collection) { |
| 1552 | type->GetForwardCompilerType(); |
| 1553 | type_list.Insert(type->shared_from_this()); |
| 1554 | } |
| 1555 | return type_list.GetSize() - old_size; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1556 | } |
| 1557 | |
| 1558 | lldb_private::TypeSystem * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1559 | SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { |
| 1560 | auto type_system = |
| 1561 | m_obj_file->GetModule()->GetTypeSystemForLanguage(language); |
| 1562 | if (type_system) |
| 1563 | type_system->SetSymbolFile(this); |
| 1564 | return type_system; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1567 | lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( |
| 1568 | const lldb_private::SymbolContext &sc, |
| 1569 | const lldb_private::ConstString &name, |
| 1570 | const lldb_private::CompilerDeclContext *parent_decl_ctx) { |
Aleksandr Urakov | 709426b | 2018-09-10 08:08:43 +0000 | [diff] [blame] | 1571 | auto type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 1572 | auto clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
| 1573 | if (!clang_type_system) |
| 1574 | return CompilerDeclContext(); |
| 1575 | |
| 1576 | PDBASTParser *pdb = clang_type_system->GetPDBParser(); |
| 1577 | if (!pdb) |
| 1578 | return CompilerDeclContext(); |
| 1579 | |
| 1580 | clang::DeclContext *decl_context = nullptr; |
| 1581 | if (parent_decl_ctx) |
| 1582 | decl_context = static_cast<clang::DeclContext *>( |
| 1583 | parent_decl_ctx->GetOpaqueDeclContext()); |
| 1584 | |
| 1585 | auto namespace_decl = |
| 1586 | pdb->FindNamespaceDecl(decl_context, name.GetStringRef()); |
| 1587 | if (!namespace_decl) |
| 1588 | return CompilerDeclContext(); |
| 1589 | |
| 1590 | return CompilerDeclContext(type_system, |
| 1591 | static_cast<clang::DeclContext *>(namespace_decl)); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1594 | lldb_private::ConstString SymbolFilePDB::GetPluginName() { |
| 1595 | static ConstString g_name("pdb"); |
| 1596 | return g_name; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1597 | } |
| 1598 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1599 | uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } |
| 1600 | |
| 1601 | IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } |
| 1602 | |
| 1603 | const IPDBSession &SymbolFilePDB::GetPDBSession() const { |
| 1604 | return *m_session_up; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1607 | lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, |
| 1608 | uint32_t index) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1609 | auto found_cu = m_comp_units.find(id); |
| 1610 | if (found_cu != m_comp_units.end()) |
| 1611 | return found_cu->second; |
| 1612 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1613 | auto compiland_up = GetPDBCompilandByUID(id); |
| 1614 | if (!compiland_up) |
| 1615 | return CompUnitSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1616 | |
| 1617 | lldb::LanguageType lang; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1618 | auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1619 | if (!details) |
| 1620 | lang = lldb::eLanguageTypeC_plus_plus; |
| 1621 | else |
| 1622 | lang = TranslateLanguage(details->getLanguage()); |
| 1623 | |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1624 | if (lang == lldb::LanguageType::eLanguageTypeUnknown) |
| 1625 | return CompUnitSP(); |
| 1626 | |
Aaron Smith | 487b0c6 | 2018-03-20 00:18:22 +0000 | [diff] [blame] | 1627 | std::string path = compiland_up->getSourceFileFullPath(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1628 | if (path.empty()) |
| 1629 | return CompUnitSP(); |
| 1630 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1631 | // Don't support optimized code for now, DebugInfoPDB does not return this |
| 1632 | // information. |
| 1633 | LazyBool optimized = eLazyBoolNo; |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1634 | auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, |
| 1635 | path.c_str(), id, lang, optimized); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1636 | |
| 1637 | if (!cu_sp) |
| 1638 | return CompUnitSP(); |
| 1639 | |
| 1640 | m_comp_units.insert(std::make_pair(id, cu_sp)); |
| 1641 | if (index == UINT32_MAX) |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1642 | GetCompileUnitIndex(*compiland_up, index); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1643 | lldbassert(index != UINT32_MAX); |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1644 | m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index, |
| 1645 | cu_sp); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1646 | return cu_sp; |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 1647 | } |
| 1648 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1649 | bool SymbolFilePDB::ParseCompileUnitLineTable( |
| 1650 | const lldb_private::SymbolContext &sc, uint32_t match_line) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1651 | lldbassert(sc.comp_unit); |
| 1652 | |
| 1653 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 1654 | if (!compiland_up) |
| 1655 | return false; |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 1656 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1657 | // 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] | 1658 | // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1659 | // a globally unique idenfitifier in the namespace of the PDB. So, we have |
| 1660 | // to do a mapping so that we can hand out indices. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1661 | llvm::DenseMap<uint32_t, uint32_t> index_map; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1662 | BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1663 | auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1664 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1665 | // Find contributions to `compiland` from all source and header files. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1666 | std::string path = sc.comp_unit->GetPath(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1667 | auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); |
| 1668 | if (!files) |
| 1669 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1670 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1671 | // For each source and header file, create a LineSequence for contributions |
| 1672 | // to the compiland from that file, and add the sequence. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1673 | while (auto file = files->getNext()) { |
| 1674 | std::unique_ptr<LineSequence> sequence( |
| 1675 | line_table->CreateLineSequenceContainer()); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1676 | auto lines = m_session_up->findLineNumbers(*compiland_up, *file); |
| 1677 | if (!lines) |
| 1678 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1679 | int entry_count = lines->getChildCount(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1680 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1681 | uint64_t prev_addr; |
| 1682 | uint32_t prev_length; |
| 1683 | uint32_t prev_line; |
| 1684 | uint32_t prev_source_idx; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1685 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1686 | for (int i = 0; i < entry_count; ++i) { |
| 1687 | auto line = lines->getChildAtIndex(i); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1688 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1689 | uint64_t lno = line->getLineNumber(); |
| 1690 | uint64_t addr = line->getVirtualAddress(); |
| 1691 | uint32_t length = line->getLength(); |
| 1692 | uint32_t source_id = line->getSourceFileId(); |
| 1693 | uint32_t col = line->getColumnNumber(); |
| 1694 | uint32_t source_idx = index_map[source_id]; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1695 | |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1696 | // There was a gap between the current entry and the previous entry if |
| 1697 | // the addresses don't perfectly line up. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1698 | bool is_gap = (i > 0) && (prev_addr + prev_length < addr); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1699 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1700 | // Before inserting the current entry, insert a terminal entry at the end |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1701 | // of the previous entry's address range if the current entry resulted in |
| 1702 | // a gap from the previous entry. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1703 | if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { |
| 1704 | line_table->AppendLineEntryToSequence( |
| 1705 | sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 1706 | prev_source_idx, false, false, false, false, true); |
Aaron Smith | 010edd3 | 2018-06-08 02:45:25 +0000 | [diff] [blame] | 1707 | |
| 1708 | line_table->InsertSequence(sequence.release()); |
| 1709 | sequence.reset(line_table->CreateLineSequenceContainer()); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1710 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1711 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1712 | if (ShouldAddLine(match_line, lno, length)) { |
| 1713 | bool is_statement = line->isStatement(); |
| 1714 | bool is_prologue = false; |
| 1715 | bool is_epilogue = false; |
| 1716 | auto func = |
| 1717 | m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); |
| 1718 | if (func) { |
| 1719 | auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1720 | if (prologue) |
| 1721 | is_prologue = (addr == prologue->getVirtualAddress()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1722 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1723 | auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1724 | if (epilogue) |
| 1725 | is_epilogue = (addr == epilogue->getVirtualAddress()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1726 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 1727 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1728 | line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, |
| 1729 | source_idx, is_statement, false, |
| 1730 | is_prologue, is_epilogue, false); |
| 1731 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 1732 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1733 | prev_addr = addr; |
| 1734 | prev_length = length; |
| 1735 | prev_line = lno; |
| 1736 | prev_source_idx = source_idx; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1739 | if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { |
| 1740 | // The end is always a terminal entry, so insert it regardless. |
| 1741 | line_table->AppendLineEntryToSequence( |
| 1742 | sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 1743 | prev_source_idx, false, false, false, false, true); |
| 1744 | } |
| 1745 | |
| 1746 | line_table->InsertSequence(sequence.release()); |
| 1747 | } |
| 1748 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1749 | if (line_table->GetSize()) { |
| 1750 | sc.comp_unit->SetLineTable(line_table.release()); |
| 1751 | return true; |
| 1752 | } |
| 1753 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1754 | } |
| 1755 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1756 | void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1757 | const PDBSymbolCompiland &compiland, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1758 | llvm::DenseMap<uint32_t, uint32_t> &index_map) const { |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1759 | // This is a hack, but we need to convert the source id into an index into |
| 1760 | // the support files array. We don't want to do path comparisons to avoid |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 1761 | // basename / full path issues that may or may not even be a problem, so we |
| 1762 | // use the globally unique source file identifiers. Ideally we could use the |
| 1763 | // global identifiers everywhere, but LineEntry currently assumes indices. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame] | 1764 | auto source_files = m_session_up->getSourceFilesForCompiland(compiland); |
| 1765 | if (!source_files) |
| 1766 | return; |
Pavel Labath | 9ea80d2 | 2018-06-28 10:03:42 +0000 | [diff] [blame] | 1767 | |
| 1768 | // LLDB uses the DWARF-like file numeration (one based) |
| 1769 | int index = 1; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1770 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 1771 | while (auto file = source_files->getNext()) { |
| 1772 | uint32_t source_id = file->getUniqueId(); |
| 1773 | index_map[source_id] = index++; |
| 1774 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1775 | } |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1776 | |
| 1777 | lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress( |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 1778 | const lldb_private::Address &so_addr) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1779 | lldb::addr_t file_vm_addr = so_addr.GetFileAddress(); |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 1780 | if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1781 | return nullptr; |
| 1782 | |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 1783 | // If it is a PDB function's vm addr, this is the first sure bet. |
| 1784 | if (auto lines = |
| 1785 | m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) { |
| 1786 | if (auto first_line = lines->getNext()) |
| 1787 | return ParseCompileUnitForUID(first_line->getCompilandId()); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1788 | } |
| 1789 | |
Aaron Smith | 308e39c | 2018-03-22 19:26:33 +0000 | [diff] [blame] | 1790 | // Otherwise we resort to section contributions. |
| 1791 | if (auto sec_contribs = m_session_up->getSectionContribs()) { |
| 1792 | while (auto section = sec_contribs->getNext()) { |
| 1793 | auto va = section->getVirtualAddress(); |
| 1794 | if (file_vm_addr >= va && file_vm_addr < va + section->getLength()) |
| 1795 | return ParseCompileUnitForUID(section->getCompilandId()); |
| 1796 | } |
| 1797 | } |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1798 | return nullptr; |
| 1799 | } |
| 1800 | |
| 1801 | Mangled |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1802 | SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1803 | Mangled mangled; |
Aaron Smith | e664b5d | 2018-03-19 21:14:19 +0000 | [diff] [blame] | 1804 | auto func_name = pdb_func.getName(); |
| 1805 | auto func_undecorated_name = pdb_func.getUndecoratedName(); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1806 | std::string func_decorated_name; |
| 1807 | |
| 1808 | // Seek from public symbols for non-static function's decorated name if any. |
| 1809 | // For static functions, they don't have undecorated names and aren't exposed |
| 1810 | // in Public Symbols either. |
| 1811 | if (!func_undecorated_name.empty()) { |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1812 | auto result_up = m_global_scope_up->findChildren( |
| 1813 | PDB_SymType::PublicSymbol, func_undecorated_name, |
| 1814 | PDB_NameSearchFlags::NS_UndecoratedName); |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1815 | if (result_up) { |
| 1816 | while (auto symbol_up = result_up->getNext()) { |
| 1817 | // For a public symbol, it is unique. |
| 1818 | lldbassert(result_up->getChildCount() == 1); |
| 1819 | if (auto *pdb_public_sym = |
Aaron Smith | c8316ed | 2018-03-22 03:44:51 +0000 | [diff] [blame] | 1820 | llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>( |
| 1821 | symbol_up.get())) { |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1822 | if (pdb_public_sym->isFunction()) { |
| 1823 | func_decorated_name = pdb_public_sym->getName(); |
Aaron Smith | f76fe68 | 2018-03-07 03:16:50 +0000 | [diff] [blame] | 1824 | break; |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1825 | } |
| 1826 | } |
| 1827 | } |
| 1828 | } |
| 1829 | } |
| 1830 | if (!func_decorated_name.empty()) { |
| 1831 | mangled.SetMangledName(ConstString(func_decorated_name)); |
| 1832 | |
| 1833 | // For MSVC, format of C funciton's decorated name depends on calling |
| 1834 | // conventon. Unfortunately none of the format is recognized by current |
| 1835 | // LLDB. For example, `_purecall` is a __cdecl C function. From PDB, |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1836 | // `__purecall` is retrieved as both its decorated and undecorated name |
| 1837 | // (using PDBSymbolFunc::getUndecoratedName method). However `__purecall` |
| 1838 | // string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix). |
| 1839 | // Mangled::GetDemangledName method will fail internally and caches an |
| 1840 | // empty string as its undecorated name. So we will face a contradition |
| 1841 | // here for the same symbol: |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1842 | // non-empty undecorated name from PDB |
| 1843 | // empty undecorated name from LLDB |
| 1844 | if (!func_undecorated_name.empty() && |
| 1845 | mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty()) |
| 1846 | mangled.SetDemangledName(ConstString(func_undecorated_name)); |
| 1847 | |
| 1848 | // LLDB uses several flags to control how a C++ decorated name is |
Adrian Prantl | 0509724 | 2018-04-30 16:49:04 +0000 | [diff] [blame] | 1849 | // undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the |
| 1850 | // yielded name could be different from what we retrieve from |
Aaron Smith | 7ac1c78 | 2018-02-09 05:31:28 +0000 | [diff] [blame] | 1851 | // PDB source unless we also apply same flags in getting undecorated |
| 1852 | // name through PDBSymbolFunc::getUndecoratedNameEx method. |
| 1853 | if (!func_undecorated_name.empty() && |
| 1854 | mangled.GetDemangledName(mangled.GuessLanguage()) != |
| 1855 | ConstString(func_undecorated_name)) |
| 1856 | mangled.SetDemangledName(ConstString(func_undecorated_name)); |
| 1857 | } else if (!func_undecorated_name.empty()) { |
| 1858 | mangled.SetDemangledName(ConstString(func_undecorated_name)); |
| 1859 | } else if (!func_name.empty()) |
| 1860 | mangled.SetValue(ConstString(func_name), false); |
| 1861 | |
| 1862 | return mangled; |
| 1863 | } |
| 1864 | |
| 1865 | bool SymbolFilePDB::DeclContextMatchesThisSymbolFile( |
| 1866 | const lldb_private::CompilerDeclContext *decl_ctx) { |
| 1867 | if (decl_ctx == nullptr || !decl_ctx->IsValid()) |
| 1868 | return true; |
| 1869 | |
| 1870 | TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem(); |
| 1871 | if (!decl_ctx_type_system) |
| 1872 | return false; |
| 1873 | TypeSystem *type_system = GetTypeSystemForLanguage( |
| 1874 | decl_ctx_type_system->GetMinimumLanguage(nullptr)); |
| 1875 | if (decl_ctx_type_system == type_system) |
| 1876 | return true; // The type systems match, return true |
| 1877 | |
| 1878 | return false; |
| 1879 | } |
Aleksandr Urakov | 356aa4a | 2018-10-23 08:29:17 +0000 | [diff] [blame] | 1880 | |
| 1881 | uint32_t SymbolFilePDB::GetCompilandId(const llvm::pdb::PDBSymbolData &data) { |
| 1882 | static const auto pred_upper = [](uint32_t lhs, SecContribInfo rhs) { |
| 1883 | return lhs < rhs.Offset; |
| 1884 | }; |
| 1885 | |
| 1886 | // Cache section contributions |
| 1887 | if (m_sec_contribs.empty()) { |
| 1888 | if (auto SecContribs = m_session_up->getSectionContribs()) { |
| 1889 | while (auto SectionContrib = SecContribs->getNext()) { |
| 1890 | auto comp_id = SectionContrib->getCompilandId(); |
| 1891 | if (!comp_id) |
| 1892 | continue; |
| 1893 | |
| 1894 | auto sec = SectionContrib->getAddressSection(); |
| 1895 | auto &sec_cs = m_sec_contribs[sec]; |
| 1896 | |
| 1897 | auto offset = SectionContrib->getAddressOffset(); |
| 1898 | auto it = |
| 1899 | std::upper_bound(sec_cs.begin(), sec_cs.end(), offset, pred_upper); |
| 1900 | |
| 1901 | auto size = SectionContrib->getLength(); |
| 1902 | sec_cs.insert(it, {offset, size, comp_id}); |
| 1903 | } |
| 1904 | } |
| 1905 | } |
| 1906 | |
| 1907 | // Check by line number |
| 1908 | if (auto Lines = data.getLineNumbers()) { |
| 1909 | if (auto FirstLine = Lines->getNext()) |
| 1910 | return FirstLine->getCompilandId(); |
| 1911 | } |
| 1912 | |
| 1913 | // Retrieve section + offset |
| 1914 | uint32_t DataSection = data.getAddressSection(); |
| 1915 | uint32_t DataOffset = data.getAddressOffset(); |
| 1916 | if (DataSection == 0) { |
| 1917 | if (auto RVA = data.getRelativeVirtualAddress()) |
| 1918 | m_session_up->addressForRVA(RVA, DataSection, DataOffset); |
| 1919 | } |
| 1920 | |
| 1921 | if (DataSection) { |
| 1922 | // Search by section contributions |
| 1923 | auto &sec_cs = m_sec_contribs[DataSection]; |
| 1924 | auto it = |
| 1925 | std::upper_bound(sec_cs.begin(), sec_cs.end(), DataOffset, pred_upper); |
| 1926 | if (it != sec_cs.begin()) { |
| 1927 | --it; |
| 1928 | if (DataOffset < it->Offset + it->Size) |
| 1929 | return it->CompilandId; |
| 1930 | } |
| 1931 | } else { |
| 1932 | // Search in lexical tree |
| 1933 | auto LexParentId = data.getLexicalParentId(); |
| 1934 | while (auto LexParent = m_session_up->getSymbolById(LexParentId)) { |
| 1935 | if (LexParent->getSymTag() == PDB_SymType::Exe) |
| 1936 | break; |
| 1937 | if (LexParent->getSymTag() == PDB_SymType::Compiland) |
| 1938 | return LexParentId; |
| 1939 | LexParentId = LexParent->getRawSymbol().getLexicalParentId(); |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | return 0; |
| 1944 | } |