Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1 | //===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "SymbolFilePDB.h" |
| 11 | |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 12 | #include "clang/Lex/Lexer.h" |
| 13 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/PluginManager.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/ClangASTContext.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/CompileUnit.h" |
| 18 | #include "lldb/Symbol/LineTable.h" |
| 19 | #include "lldb/Symbol/ObjectFile.h" |
| 20 | #include "lldb/Symbol/SymbolContext.h" |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 21 | #include "lldb/Symbol/SymbolVendor.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 22 | #include "lldb/Symbol/TypeMap.h" |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 23 | #include "lldb/Utility/RegularExpression.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 24 | |
Pavel Labath | b8d8c62 | 2016-05-09 11:07:43 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/PDB/GenericError.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 26 | #include "llvm/DebugInfo/PDB/IPDBDataStream.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" |
| 28 | #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" |
| 29 | #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 30 | #include "llvm/DebugInfo/PDB/IPDBTable.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 31 | #include "llvm/DebugInfo/PDB/PDBSymbol.h" |
| 32 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 33 | #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 34 | #include "llvm/DebugInfo/PDB/PDBSymbolData.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 35 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
| 36 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| 37 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" |
| 38 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 39 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" |
| 40 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" |
| 41 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
| 42 | |
| 43 | #include "Plugins/SymbolFile/PDB/PDBASTParser.h" |
| 44 | |
| 45 | #include <regex> |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 46 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 47 | using namespace lldb; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 48 | using namespace lldb_private; |
Zachary Turner | 54fd7ff | 2016-05-04 20:33:53 +0000 | [diff] [blame] | 49 | using namespace llvm::pdb; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 50 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 51 | namespace { |
| 52 | lldb::LanguageType TranslateLanguage(PDB_Lang lang) { |
| 53 | switch (lang) { |
| 54 | case PDB_Lang::Cpp: |
| 55 | return lldb::LanguageType::eLanguageTypeC_plus_plus; |
| 56 | case PDB_Lang::C: |
| 57 | return lldb::LanguageType::eLanguageTypeC; |
| 58 | default: |
| 59 | return lldb::LanguageType::eLanguageTypeUnknown; |
| 60 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 63 | bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line, |
| 64 | uint32_t addr_length) { |
| 65 | return ((requested_line == 0 || actual_line == requested_line) && |
| 66 | addr_length > 0); |
| 67 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 70 | void SymbolFilePDB::Initialize() { |
| 71 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 72 | GetPluginDescriptionStatic(), CreateInstance, |
| 73 | DebuggerInitialize); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 76 | void SymbolFilePDB::Terminate() { |
| 77 | PluginManager::UnregisterPlugin(CreateInstance); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 80 | void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {} |
| 81 | |
| 82 | lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() { |
| 83 | static ConstString g_name("pdb"); |
| 84 | return g_name; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 87 | const char *SymbolFilePDB::GetPluginDescriptionStatic() { |
| 88 | return "Microsoft PDB debug symbol file reader."; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | lldb_private::SymbolFile * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 92 | SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) { |
| 93 | return new SymbolFilePDB(obj_file); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 97 | : SymbolFile(object_file), m_session_up(), m_global_scope_up(), |
| 98 | m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {} |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 99 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 100 | SymbolFilePDB::~SymbolFilePDB() {} |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 101 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 102 | uint32_t SymbolFilePDB::CalculateAbilities() { |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 103 | uint32_t abilities = 0; |
| 104 | if (!m_obj_file) |
| 105 | return 0; |
| 106 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 107 | if (!m_session_up) { |
| 108 | // Lazily load and match the PDB file, but only do this once. |
| 109 | std::string exePath = m_obj_file->GetFileSpec().GetPath(); |
| 110 | auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath), |
| 111 | m_session_up); |
| 112 | if (error) { |
| 113 | llvm::consumeError(std::move(error)); |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 114 | auto module_sp = m_obj_file->GetModule(); |
| 115 | if (!module_sp) |
| 116 | return 0; |
| 117 | // See if any symbol file is specified through `--symfile` option. |
| 118 | FileSpec symfile = module_sp->GetSymbolFileFileSpec(); |
| 119 | if (!symfile) |
| 120 | return 0; |
| 121 | error = loadDataForPDB(PDB_ReaderType::DIA, |
| 122 | llvm::StringRef(symfile.GetPath()), |
| 123 | m_session_up); |
| 124 | if (error) { |
| 125 | llvm::consumeError(std::move(error)); |
| 126 | return 0; |
| 127 | } |
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 | } |
Aaron Smith | 1f8552a | 2017-12-22 00:04:36 +0000 | [diff] [blame] | 130 | if (!m_session_up.get()) |
| 131 | return 0; |
| 132 | |
| 133 | auto enum_tables_up = m_session_up->getEnumTables(); |
| 134 | if (!enum_tables_up) |
| 135 | return 0; |
| 136 | while (auto table_up = enum_tables_up->getNext()) { |
| 137 | if (table_up->getItemCount() == 0) |
| 138 | continue; |
| 139 | auto type = table_up->getTableType(); |
| 140 | switch (type) { |
| 141 | case PDB_TableType::Symbols: |
| 142 | // This table represents a store of symbols with types listed in |
| 143 | // PDBSym_Type |
| 144 | abilities |= (CompileUnits | Functions | Blocks | |
| 145 | GlobalVariables | LocalVariables | VariableTypes); |
| 146 | break; |
| 147 | case PDB_TableType::LineNumbers: |
| 148 | abilities |= LineTables; |
| 149 | break; |
| 150 | default: break; |
| 151 | } |
| 152 | } |
| 153 | return abilities; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 156 | void SymbolFilePDB::InitializeObject() { |
| 157 | lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 158 | lldbassert(obj_load_address && |
| 159 | obj_load_address != LLDB_INVALID_ADDRESS); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 160 | m_session_up->setLoadAddress(obj_load_address); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 161 | if (!m_global_scope_up) |
| 162 | m_global_scope_up = m_session_up->getGlobalScope(); |
| 163 | lldbassert(m_global_scope_up.get()); |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 164 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 165 | TypeSystem *type_system = |
| 166 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 167 | ClangASTContext *clang_type_system = |
| 168 | llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 169 | lldbassert(clang_type_system); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 170 | m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>( |
| 171 | type_system, clang_type_system->GetTranslationUnitDecl()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | uint32_t SymbolFilePDB::GetNumCompileUnits() { |
| 175 | if (m_cached_compile_unit_count == 0) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 176 | auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 177 | if (!compilands) |
| 178 | return 0; |
| 179 | |
| 180 | // The linker could link *.dll (compiland language = LINK), or import |
| 181 | // *.dll. For example, a compiland with name `Import:KERNEL32.dll` |
| 182 | // could be found as a child of the global scope (PDB executable). |
| 183 | // Usually, such compilands contain `thunk` symbols in which we are not |
| 184 | // interested for now. However we still count them in the compiland list. |
| 185 | // If we perform any compiland related activity, like finding symbols |
| 186 | // through llvm::pdb::IPDBSession methods, such compilands will all be |
| 187 | // searched automatically no matter whether we include them or not. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 188 | m_cached_compile_unit_count = compilands->getChildCount(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 189 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 190 | // The linker can inject an additional "dummy" compilation unit into the |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 191 | // PDB. Ignore this special compile unit for our purposes, if it is there. |
| 192 | // It is always the last one. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 193 | auto last_compiland_up = |
| 194 | compilands->getChildAtIndex(m_cached_compile_unit_count - 1); |
| 195 | lldbassert(last_compiland_up.get()); |
| 196 | std::string name = last_compiland_up->getName(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | if (name == "* Linker *") |
| 198 | --m_cached_compile_unit_count; |
| 199 | } |
| 200 | return m_cached_compile_unit_count; |
| 201 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 202 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 203 | void SymbolFilePDB::GetCompileUnitIndex( |
| 204 | const llvm::pdb::PDBSymbolCompiland *pdb_compiland, |
| 205 | uint32_t &index) { |
| 206 | if (!pdb_compiland) |
| 207 | return; |
| 208 | |
| 209 | auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 210 | if (!results_up) |
| 211 | return; |
| 212 | auto uid = pdb_compiland->getSymIndexId(); |
| 213 | for (int cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) { |
| 214 | auto compiland_up = results_up->getChildAtIndex(cu_idx); |
| 215 | if (!compiland_up) |
| 216 | continue; |
| 217 | if (compiland_up->getSymIndexId() == uid) { |
| 218 | index = cu_idx; |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | index = UINT32_MAX; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | std::unique_ptr<llvm::pdb::PDBSymbolCompiland> |
| 227 | SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) { |
| 228 | return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid); |
| 229 | } |
| 230 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 231 | lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 232 | if (index >= GetNumCompileUnits()) |
| 233 | return CompUnitSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 234 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 235 | // Assuming we always retrieve same compilands listed in same order through |
| 236 | // `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a |
| 237 | // compile unit makes no sense. |
| 238 | auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>(); |
| 239 | if (!results) |
| 240 | return CompUnitSP(); |
| 241 | auto compiland_up = results->getChildAtIndex(index); |
| 242 | if (!compiland_up) |
| 243 | return CompUnitSP(); |
| 244 | return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | lldb::LanguageType |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 248 | SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) { |
| 249 | // What fields should I expect to be filled out on the SymbolContext? Is it |
| 250 | // safe to assume that `sc.comp_unit` is valid? |
| 251 | if (!sc.comp_unit) |
| 252 | return lldb::eLanguageTypeUnknown; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 253 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 254 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 255 | if (!compiland_up) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 256 | return lldb::eLanguageTypeUnknown; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 257 | auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | if (!details) |
| 259 | return lldb::eLanguageTypeUnknown; |
| 260 | return TranslateLanguage(details->getLanguage()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 263 | size_t SymbolFilePDB::ParseCompileUnitFunctions( |
| 264 | const lldb_private::SymbolContext &sc) { |
| 265 | // TODO: Implement this |
| 266 | return size_t(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 267 | } |
| 268 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 269 | bool SymbolFilePDB::ParseCompileUnitLineTable( |
| 270 | const lldb_private::SymbolContext &sc) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 271 | lldbassert(sc.comp_unit); |
| 272 | if (sc.comp_unit->GetLineTable()) |
| 273 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 274 | return ParseCompileUnitLineTable(sc, 0); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | bool SymbolFilePDB::ParseCompileUnitDebugMacros( |
| 278 | const lldb_private::SymbolContext &sc) { |
| 279 | // PDB doesn't contain information about macros |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | bool SymbolFilePDB::ParseCompileUnitSupportFiles( |
| 284 | const lldb_private::SymbolContext &sc, |
| 285 | lldb_private::FileSpecList &support_files) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 286 | lldbassert(sc.comp_unit); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 287 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 288 | // 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] | 289 | // is easily (and quickly) accessible from DebugInfoPDB, so caching it a |
| 290 | // second time seems like a waste. Unfortunately, there's no good way around |
| 291 | // this short of a moderate refactor since SymbolVendor depends on being able |
| 292 | // to cache this list. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 293 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 294 | if (!compiland_up) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 295 | return false; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 296 | auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 297 | if (!files || files->getChildCount() == 0) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 298 | return false; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 299 | |
| 300 | while (auto file = files->getNext()) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 301 | FileSpec spec(file->getFileName(), false, FileSpec::ePathSyntaxWindows); |
| 302 | support_files.AppendIfUnique(spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 303 | } |
| 304 | return true; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 307 | bool SymbolFilePDB::ParseImportedModules( |
| 308 | const lldb_private::SymbolContext &sc, |
| 309 | std::vector<lldb_private::ConstString> &imported_modules) { |
| 310 | // PDB does not yet support module debug info |
| 311 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | size_t |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 315 | SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) { |
| 316 | // TODO: Implement this |
| 317 | return size_t(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 320 | size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) { |
| 321 | // TODO: Implement this |
| 322 | return size_t(); |
| 323 | } |
| 324 | |
| 325 | size_t |
| 326 | SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) { |
| 327 | // TODO: Implement this |
| 328 | return size_t(); |
| 329 | } |
| 330 | |
| 331 | lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) { |
| 332 | auto find_result = m_types.find(type_uid); |
| 333 | if (find_result != m_types.end()) |
| 334 | return find_result->second.get(); |
| 335 | |
| 336 | TypeSystem *type_system = |
| 337 | GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 338 | ClangASTContext *clang_type_system = |
| 339 | llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
| 340 | if (!clang_type_system) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 341 | return nullptr; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 342 | PDBASTParser *pdb = |
| 343 | llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser()); |
| 344 | if (!pdb) |
| 345 | return nullptr; |
| 346 | |
| 347 | auto pdb_type = m_session_up->getSymbolById(type_uid); |
| 348 | if (pdb_type == nullptr) |
| 349 | return nullptr; |
| 350 | |
| 351 | lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 352 | if (result.get()) |
| 353 | m_types.insert(std::make_pair(type_uid, result)); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 354 | return result.get(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 357 | bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) { |
| 358 | // TODO: Implement this |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) { |
| 363 | return lldb_private::CompilerDecl(); |
| 364 | } |
| 365 | |
| 366 | lldb_private::CompilerDeclContext |
| 367 | SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) { |
| 368 | // PDB always uses the translation unit decl context for everything. We can |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 369 | // improve this later but it's not easy because PDB doesn't provide a high |
| 370 | // enough level of type fidelity in this area. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 371 | return *m_tu_decl_ctx_up; |
| 372 | } |
| 373 | |
| 374 | lldb_private::CompilerDeclContext |
| 375 | SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { |
| 376 | return *m_tu_decl_ctx_up; |
| 377 | } |
| 378 | |
| 379 | void SymbolFilePDB::ParseDeclsForContext( |
| 380 | lldb_private::CompilerDeclContext decl_ctx) {} |
| 381 | |
| 382 | uint32_t |
| 383 | SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, |
| 384 | uint32_t resolve_scope, |
| 385 | lldb_private::SymbolContext &sc) { |
| 386 | return uint32_t(); |
| 387 | } |
| 388 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 389 | std::string SymbolFilePDB::GetSourceFileNameForPDBCompiland( |
| 390 | const PDBSymbolCompiland *pdb_compiland) { |
| 391 | if (!pdb_compiland) |
| 392 | return std::string(); |
| 393 | |
| 394 | std::string source_file_name; |
| 395 | // `getSourceFileName` returns the basename of the original source file |
| 396 | // used to generate this compiland. It does not return the full path. |
| 397 | // Currently the only way to get that is to do a basename lookup to get the |
| 398 | // IPDBSourceFile, but this is ambiguous in the case of two source files |
| 399 | // with the same name contributing to the same compiland. This is an edge |
| 400 | // case that we ignore for now, although we need to a long-term solution. |
| 401 | std::string file_name = pdb_compiland->getSourceFileName(); |
| 402 | if (!file_name.empty()) { |
| 403 | auto one_src_file_up = |
| 404 | m_session_up->findOneSourceFile(pdb_compiland, file_name, |
| 405 | PDB_NameSearchFlags::NS_CaseInsensitive); |
| 406 | if (one_src_file_up) |
| 407 | source_file_name = one_src_file_up->getFileName(); |
| 408 | } |
| 409 | // For some reason, source file name could be empty, so we will walk through |
| 410 | // all source files of this compiland, and determine the right source file |
| 411 | // if any that is used to generate this compiland based on language |
| 412 | // indicated in compilanddetails language field. |
| 413 | if (!source_file_name.empty()) |
| 414 | return source_file_name; |
| 415 | |
| 416 | auto details_up = pdb_compiland->findOneChild<PDBSymbolCompilandDetails>(); |
| 417 | PDB_Lang pdb_lang = details_up ? details_up->getLanguage() : PDB_Lang::Cpp; |
| 418 | auto src_files_up = |
| 419 | m_session_up->getSourceFilesForCompiland(*pdb_compiland); |
| 420 | if (src_files_up) { |
| 421 | while (auto file_up = src_files_up->getNext()) { |
| 422 | FileSpec file_spec(file_up->getFileName(), false, |
| 423 | FileSpec::ePathSyntaxWindows); |
| 424 | auto file_extension = file_spec.GetFileNameExtension(); |
| 425 | if (pdb_lang == PDB_Lang::Cpp || pdb_lang == PDB_Lang::C) { |
| 426 | static const char* exts[] = { "cpp", "c", "cc", "cxx" }; |
| 427 | if (llvm::is_contained(exts, file_extension.GetStringRef().lower())) |
| 428 | source_file_name = file_up->getFileName(); |
| 429 | break; |
| 430 | } else if (pdb_lang == PDB_Lang::Masm && |
| 431 | ConstString::Compare(file_extension, ConstString("ASM"), |
| 432 | false) == 0) { |
| 433 | source_file_name = file_up->getFileName(); |
| 434 | break; |
| 435 | } |
| 436 | } |
| 437 | } |
| 438 | return source_file_name; |
| 439 | } |
| 440 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 441 | uint32_t SymbolFilePDB::ResolveSymbolContext( |
| 442 | const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, |
| 443 | uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 444 | const size_t old_size = sc_list.GetSize(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 445 | if (resolve_scope & lldb::eSymbolContextCompUnit) { |
| 446 | // Locate all compilation units with line numbers referencing the specified |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 447 | // file. For example, if `file_spec` is <vector>, then this should return |
| 448 | // all source files and header files that reference <vector>, either |
| 449 | // directly or indirectly. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 450 | auto compilands = m_session_up->findCompilandsForSourceFile( |
| 451 | file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive); |
| 452 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 453 | if (!compilands) |
| 454 | return 0; |
| 455 | |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 456 | // For each one, either find its previously parsed data or parse it afresh |
| 457 | // and add it to the symbol context list. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 458 | while (auto compiland = compilands->getNext()) { |
| 459 | // If we're not checking inlines, then don't add line information for this |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 460 | // file unless the FileSpec matches. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 461 | if (!check_inlines) { |
| 462 | // `getSourceFileName` returns the basename of the original source file |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 463 | // used to generate this compiland. It does not return the full path. |
| 464 | // Currently the only way to get that is to do a basename lookup to get |
| 465 | // the IPDBSourceFile, but this is ambiguous in the case of two source |
| 466 | // files with the same name contributing to the same compiland. This is |
| 467 | // a moderately extreme edge case, so we consider this OK for now, |
| 468 | // although we need to find a long-term solution. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 469 | std::string source_file = |
| 470 | GetSourceFileNameForPDBCompiland(compiland.get()); |
| 471 | if (source_file.empty()) |
| 472 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 473 | FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 474 | bool need_full_match = !file_spec.GetDirectory().IsEmpty(); |
| 475 | if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 476 | continue; |
| 477 | } |
| 478 | |
| 479 | SymbolContext sc; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 480 | auto cu = ParseCompileUnitForUID(compiland->getSymIndexId()); |
| 481 | if (!cu.get()) |
| 482 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 483 | sc.comp_unit = cu.get(); |
| 484 | sc.module_sp = cu->GetModule(); |
| 485 | sc_list.Append(sc); |
| 486 | |
| 487 | // 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] | 488 | // table that match the requested line (or all lines if `line` == 0). |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 489 | if (resolve_scope & lldb::eSymbolContextLineEntry) |
| 490 | ParseCompileUnitLineTable(sc, line); |
| 491 | } |
| 492 | } |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 493 | return sc_list.GetSize() - old_size; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | uint32_t SymbolFilePDB::FindGlobalVariables( |
| 497 | const lldb_private::ConstString &name, |
| 498 | const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, |
| 499 | uint32_t max_matches, lldb_private::VariableList &variables) { |
| 500 | return uint32_t(); |
| 501 | } |
| 502 | |
| 503 | uint32_t |
| 504 | SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, |
| 505 | bool append, uint32_t max_matches, |
| 506 | lldb_private::VariableList &variables) { |
| 507 | return uint32_t(); |
| 508 | } |
| 509 | |
| 510 | uint32_t SymbolFilePDB::FindFunctions( |
| 511 | const lldb_private::ConstString &name, |
| 512 | const lldb_private::CompilerDeclContext *parent_decl_ctx, |
| 513 | uint32_t name_type_mask, bool include_inlines, bool append, |
| 514 | lldb_private::SymbolContextList &sc_list) { |
| 515 | return uint32_t(); |
| 516 | } |
| 517 | |
| 518 | uint32_t |
| 519 | SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, |
| 520 | bool include_inlines, bool append, |
| 521 | lldb_private::SymbolContextList &sc_list) { |
| 522 | return uint32_t(); |
| 523 | } |
| 524 | |
| 525 | void SymbolFilePDB::GetMangledNamesForFunction( |
| 526 | const std::string &scope_qualified_name, |
| 527 | std::vector<lldb_private::ConstString> &mangled_names) {} |
| 528 | |
| 529 | uint32_t SymbolFilePDB::FindTypes( |
| 530 | const lldb_private::SymbolContext &sc, |
| 531 | const lldb_private::ConstString &name, |
| 532 | const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, |
| 533 | uint32_t max_matches, |
| 534 | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
| 535 | lldb_private::TypeMap &types) { |
| 536 | if (!append) |
| 537 | types.Clear(); |
| 538 | if (!name) |
| 539 | return 0; |
| 540 | |
| 541 | searched_symbol_files.clear(); |
| 542 | searched_symbol_files.insert(this); |
| 543 | |
| 544 | std::string name_str = name.AsCString(); |
| 545 | |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 546 | // There is an assumption 'name' is not a regex |
| 547 | FindTypesByName(name_str, max_matches, types); |
| 548 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 549 | return types.GetSize(); |
| 550 | } |
| 551 | |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 552 | void |
| 553 | SymbolFilePDB::FindTypesByRegex(const lldb_private::RegularExpression ®ex, |
| 554 | uint32_t max_matches, |
| 555 | lldb_private::TypeMap &types) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 556 | // 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] | 557 | // space as much as possible since this searches EVERYTHING in the PDB, |
| 558 | // manually doing regex comparisons. PDB library isn't optimized for regex |
| 559 | // 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] | 560 | // 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] | 561 | // and do a regex comparison against each of them. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 562 | PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef, |
| 563 | PDB_SymType::UDT}; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 564 | std::unique_ptr<IPDBEnumSymbols> results; |
| 565 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 566 | uint32_t matches = 0; |
| 567 | |
| 568 | for (auto tag : tags_to_search) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 569 | results = m_global_scope_up->findAllChildren(tag); |
| 570 | if (!results) |
| 571 | continue; |
| 572 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 573 | while (auto result = results->getNext()) { |
| 574 | if (max_matches > 0 && matches >= max_matches) |
| 575 | break; |
| 576 | |
| 577 | std::string type_name; |
| 578 | if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get())) |
| 579 | type_name = enum_type->getName(); |
| 580 | else if (auto typedef_type = |
| 581 | llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get())) |
| 582 | type_name = typedef_type->getName(); |
| 583 | else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get())) |
| 584 | type_name = class_type->getName(); |
| 585 | else { |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 586 | // We're looking only for types that have names. Skip symbols, as well |
| 587 | // as unnamed types such as arrays, pointers, etc. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 588 | continue; |
| 589 | } |
| 590 | |
Aaron Smith | 86e9434 | 2017-12-22 05:26:50 +0000 | [diff] [blame] | 591 | if (!regex.Execute(type_name)) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 592 | continue; |
| 593 | |
| 594 | // This should cause the type to get cached and stored in the `m_types` |
| 595 | // lookup. |
| 596 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 597 | continue; |
| 598 | |
| 599 | auto iter = m_types.find(result->getSymIndexId()); |
| 600 | if (iter == m_types.end()) |
| 601 | continue; |
| 602 | types.Insert(iter->second); |
| 603 | ++matches; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | void SymbolFilePDB::FindTypesByName(const std::string &name, |
| 609 | uint32_t max_matches, |
| 610 | lldb_private::TypeMap &types) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 611 | std::unique_ptr<IPDBEnumSymbols> results; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 612 | results = m_global_scope_up->findChildren(PDB_SymType::None, name, |
| 613 | PDB_NameSearchFlags::NS_Default); |
| 614 | if (!results) |
| 615 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 616 | |
| 617 | uint32_t matches = 0; |
| 618 | |
| 619 | while (auto result = results->getNext()) { |
| 620 | if (max_matches > 0 && matches >= max_matches) |
| 621 | break; |
| 622 | switch (result->getSymTag()) { |
| 623 | case PDB_SymType::Enum: |
| 624 | case PDB_SymType::UDT: |
| 625 | case PDB_SymType::Typedef: |
| 626 | break; |
| 627 | default: |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 628 | // We're looking only for types that have names. Skip symbols, as well as |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 629 | // unnamed types such as arrays, pointers, etc. |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | // This should cause the type to get cached and stored in the `m_types` |
| 634 | // lookup. |
| 635 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 636 | continue; |
| 637 | |
| 638 | auto iter = m_types.find(result->getSymIndexId()); |
| 639 | if (iter == m_types.end()) |
| 640 | continue; |
| 641 | types.Insert(iter->second); |
| 642 | ++matches; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | size_t SymbolFilePDB::FindTypes( |
| 647 | const std::vector<lldb_private::CompilerContext> &contexts, bool append, |
| 648 | lldb_private::TypeMap &types) { |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | lldb_private::TypeList *SymbolFilePDB::GetTypeList() { return nullptr; } |
| 653 | |
| 654 | size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, |
| 655 | uint32_t type_mask, |
| 656 | lldb_private::TypeList &type_list) { |
| 657 | return size_t(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | lldb_private::TypeSystem * |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 661 | SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { |
| 662 | auto type_system = |
| 663 | m_obj_file->GetModule()->GetTypeSystemForLanguage(language); |
| 664 | if (type_system) |
| 665 | type_system->SetSymbolFile(this); |
| 666 | return type_system; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 667 | } |
| 668 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 669 | lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace( |
| 670 | const lldb_private::SymbolContext &sc, |
| 671 | const lldb_private::ConstString &name, |
| 672 | const lldb_private::CompilerDeclContext *parent_decl_ctx) { |
| 673 | return lldb_private::CompilerDeclContext(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 676 | lldb_private::ConstString SymbolFilePDB::GetPluginName() { |
| 677 | static ConstString g_name("pdb"); |
| 678 | return g_name; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 679 | } |
| 680 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 681 | uint32_t SymbolFilePDB::GetPluginVersion() { return 1; } |
| 682 | |
| 683 | IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; } |
| 684 | |
| 685 | const IPDBSession &SymbolFilePDB::GetPDBSession() const { |
| 686 | return *m_session_up; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 687 | } |
| 688 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 689 | lldb::CompUnitSP |
| 690 | SymbolFilePDB::ParseCompileUnitForUID(uint32_t id, uint32_t index) { |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 691 | auto found_cu = m_comp_units.find(id); |
| 692 | if (found_cu != m_comp_units.end()) |
| 693 | return found_cu->second; |
| 694 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 695 | auto compiland_up = GetPDBCompilandByUID(id); |
| 696 | if (!compiland_up) |
| 697 | return CompUnitSP(); |
| 698 | std::string path = GetSourceFileNameForPDBCompiland(compiland_up.get()); |
| 699 | if (path.empty()) |
| 700 | return CompUnitSP(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 701 | |
| 702 | lldb::LanguageType lang; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 703 | auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>(); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 704 | if (!details) |
| 705 | lang = lldb::eLanguageTypeC_plus_plus; |
| 706 | else |
| 707 | lang = TranslateLanguage(details->getLanguage()); |
| 708 | |
| 709 | // Don't support optimized code for now, DebugInfoPDB does not return this |
| 710 | // information. |
| 711 | LazyBool optimized = eLazyBoolNo; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 712 | auto cu_sp = std::make_shared<CompileUnit>( |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 713 | m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 714 | |
| 715 | if (!cu_sp) |
| 716 | return CompUnitSP(); |
| 717 | |
| 718 | m_comp_units.insert(std::make_pair(id, cu_sp)); |
| 719 | if (index == UINT32_MAX) |
| 720 | GetCompileUnitIndex(compiland_up.get(), index); |
| 721 | lldbassert(index != UINT32_MAX); |
| 722 | m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex( |
| 723 | index, cu_sp); |
| 724 | return cu_sp; |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 727 | bool SymbolFilePDB::ParseCompileUnitLineTable( |
| 728 | const lldb_private::SymbolContext &sc, uint32_t match_line) { |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 729 | lldbassert(sc.comp_unit); |
| 730 | |
| 731 | auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID()); |
| 732 | if (!compiland_up) |
| 733 | return false; |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 734 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 735 | // 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] | 736 | // returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us |
| 737 | // a globally unique idenfitifier in the namespace of the PDB. So, we have to |
| 738 | // do a mapping so that we can hand out indices. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 739 | llvm::DenseMap<uint32_t, uint32_t> index_map; |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 740 | BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 741 | auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 742 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 743 | // Find contributions to `compiland` from all source and header files. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 744 | std::string path = sc.comp_unit->GetPath(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 745 | auto files = m_session_up->getSourceFilesForCompiland(*compiland_up); |
| 746 | if (!files) |
| 747 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 748 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 749 | // For each source and header file, create a LineSequence for contributions to |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 750 | // the compiland from that file, and add the sequence. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 751 | while (auto file = files->getNext()) { |
| 752 | std::unique_ptr<LineSequence> sequence( |
| 753 | line_table->CreateLineSequenceContainer()); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 754 | auto lines = m_session_up->findLineNumbers(*compiland_up, *file); |
| 755 | if (!lines) |
| 756 | continue; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 757 | int entry_count = lines->getChildCount(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 758 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 759 | uint64_t prev_addr; |
| 760 | uint32_t prev_length; |
| 761 | uint32_t prev_line; |
| 762 | uint32_t prev_source_idx; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 763 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 764 | for (int i = 0; i < entry_count; ++i) { |
| 765 | auto line = lines->getChildAtIndex(i); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 766 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 767 | uint64_t lno = line->getLineNumber(); |
| 768 | uint64_t addr = line->getVirtualAddress(); |
| 769 | uint32_t length = line->getLength(); |
| 770 | uint32_t source_id = line->getSourceFileId(); |
| 771 | uint32_t col = line->getColumnNumber(); |
| 772 | uint32_t source_idx = index_map[source_id]; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 773 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 774 | // There was a gap between the current entry and the previous entry if the |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 775 | // addresses don't perfectly line up. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 776 | bool is_gap = (i > 0) && (prev_addr + prev_length < addr); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 777 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 778 | // Before inserting the current entry, insert a terminal entry at the end |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 779 | // of the previous entry's address range if the current entry resulted in |
| 780 | // a gap from the previous entry. |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 781 | if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) { |
| 782 | line_table->AppendLineEntryToSequence( |
| 783 | sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 784 | prev_source_idx, false, false, false, false, true); |
| 785 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 786 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 787 | if (ShouldAddLine(match_line, lno, length)) { |
| 788 | bool is_statement = line->isStatement(); |
| 789 | bool is_prologue = false; |
| 790 | bool is_epilogue = false; |
| 791 | auto func = |
| 792 | m_session_up->findSymbolByAddress(addr, PDB_SymType::Function); |
| 793 | if (func) { |
| 794 | auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 795 | if (prologue) |
| 796 | is_prologue = (addr == prologue->getVirtualAddress()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 797 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 798 | auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>(); |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 799 | if (epilogue) |
| 800 | is_epilogue = (addr == epilogue->getVirtualAddress()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 801 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 802 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 803 | line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, |
| 804 | source_idx, is_statement, false, |
| 805 | is_prologue, is_epilogue, false); |
| 806 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 807 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 808 | prev_addr = addr; |
| 809 | prev_length = length; |
| 810 | prev_line = lno; |
| 811 | prev_source_idx = source_idx; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 814 | if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) { |
| 815 | // The end is always a terminal entry, so insert it regardless. |
| 816 | line_table->AppendLineEntryToSequence( |
| 817 | sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 818 | prev_source_idx, false, false, false, false, true); |
| 819 | } |
| 820 | |
| 821 | line_table->InsertSequence(sequence.release()); |
| 822 | } |
| 823 | |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 824 | if (line_table->GetSize()) { |
| 825 | sc.comp_unit->SetLineTable(line_table.release()); |
| 826 | return true; |
| 827 | } |
| 828 | return false; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 831 | void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap( |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 832 | const PDBSymbolCompiland &compiland, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 833 | llvm::DenseMap<uint32_t, uint32_t> &index_map) const { |
| 834 | // This is a hack, but we need to convert the source id into an index into the |
Adrian McCarthy | 9d0eb996 | 2017-01-27 21:42:28 +0000 | [diff] [blame] | 835 | // support files array. We don't want to do path comparisons to avoid |
| 836 | // basename / full path issues that may or may not even be a problem, so we |
| 837 | // use the globally unique source file identifiers. Ideally we could use the |
| 838 | // global identifiers everywhere, but LineEntry currently assumes indices. |
Aaron Smith | 10a0257 | 2018-01-13 06:58:18 +0000 | [diff] [blame^] | 839 | auto source_files = m_session_up->getSourceFilesForCompiland(compiland); |
| 840 | if (!source_files) |
| 841 | return; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 842 | int index = 0; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 843 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 844 | while (auto file = source_files->getNext()) { |
| 845 | uint32_t source_id = file->getUniqueId(); |
| 846 | index_map[source_id] = index++; |
| 847 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 848 | } |