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