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