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