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