Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1 | //===-- SymbolFileNativePDB.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 "SymbolFileNativePDB.h" |
| 11 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 12 | #include "clang/AST/Attr.h" |
| 13 | #include "clang/AST/CharUnits.h" |
| 14 | #include "clang/AST/Decl.h" |
| 15 | #include "clang/AST/DeclCXX.h" |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 16 | #include "clang/AST/Type.h" |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 17 | |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 18 | #include "Plugins/Language/CPlusPlus/MSVCUndecoratedNameParser.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Module.h" |
| 20 | #include "lldb/Core/PluginManager.h" |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 21 | #include "lldb/Core/StreamBuffer.h" |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 22 | #include "lldb/Core/StreamFile.h" |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 23 | #include "lldb/Symbol/ClangASTContext.h" |
| 24 | #include "lldb/Symbol/ClangASTImporter.h" |
| 25 | #include "lldb/Symbol/ClangExternalASTSourceCommon.h" |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/ClangUtil.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/CompileUnit.h" |
| 28 | #include "lldb/Symbol/LineTable.h" |
| 29 | #include "lldb/Symbol/ObjectFile.h" |
| 30 | #include "lldb/Symbol/SymbolContext.h" |
| 31 | #include "lldb/Symbol/SymbolVendor.h" |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 32 | #include "lldb/Symbol/Variable.h" |
| 33 | #include "lldb/Symbol/VariableList.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 34 | |
| 35 | #include "llvm/DebugInfo/CodeView/CVRecord.h" |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 36 | #include "llvm/DebugInfo/CodeView/CVTypeVisitor.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 37 | #include "llvm/DebugInfo/CodeView/DebugLinesSubsection.h" |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 38 | #include "llvm/DebugInfo/CodeView/LazyRandomTypeCollection.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 39 | #include "llvm/DebugInfo/CodeView/RecordName.h" |
| 40 | #include "llvm/DebugInfo/CodeView/SymbolDeserializer.h" |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 41 | #include "llvm/DebugInfo/CodeView/SymbolRecordHelpers.h" |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 42 | #include "llvm/DebugInfo/CodeView/TypeDeserializer.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 43 | #include "llvm/DebugInfo/PDB/Native/DbiStream.h" |
| 44 | #include "llvm/DebugInfo/PDB/Native/GlobalsStream.h" |
| 45 | #include "llvm/DebugInfo/PDB/Native/InfoStream.h" |
| 46 | #include "llvm/DebugInfo/PDB/Native/ModuleDebugStream.h" |
| 47 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 48 | #include "llvm/DebugInfo/PDB/Native/SymbolStream.h" |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 49 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 50 | #include "llvm/DebugInfo/PDB/PDBTypes.h" |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 51 | #include "llvm/Demangle/MicrosoftDemangle.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 52 | #include "llvm/Object/COFF.h" |
| 53 | #include "llvm/Support/Allocator.h" |
| 54 | #include "llvm/Support/BinaryStreamReader.h" |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 55 | #include "llvm/Support/Error.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 56 | #include "llvm/Support/ErrorOr.h" |
| 57 | #include "llvm/Support/MemoryBuffer.h" |
| 58 | |
Zachary Turner | a93458b | 2018-12-06 17:49:15 +0000 | [diff] [blame] | 59 | #include "DWARFLocationExpression.h" |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 60 | #include "PdbAstBuilder.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 61 | #include "PdbSymUid.h" |
| 62 | #include "PdbUtil.h" |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 63 | #include "UdtRecordCompleter.h" |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 64 | |
| 65 | using namespace lldb; |
| 66 | using namespace lldb_private; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 67 | using namespace npdb; |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 68 | using namespace llvm::codeview; |
| 69 | using namespace llvm::pdb; |
| 70 | |
| 71 | static lldb::LanguageType TranslateLanguage(PDB_Lang lang) { |
| 72 | switch (lang) { |
| 73 | case PDB_Lang::Cpp: |
| 74 | return lldb::LanguageType::eLanguageTypeC_plus_plus; |
| 75 | case PDB_Lang::C: |
| 76 | return lldb::LanguageType::eLanguageTypeC; |
| 77 | default: |
| 78 | return lldb::LanguageType::eLanguageTypeUnknown; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static std::unique_ptr<PDBFile> loadPDBFile(std::string PdbPath, |
| 83 | llvm::BumpPtrAllocator &Allocator) { |
| 84 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer = |
| 85 | llvm::MemoryBuffer::getFile(PdbPath, /*FileSize=*/-1, |
| 86 | /*RequiresNullTerminator=*/false); |
| 87 | if (!ErrorOrBuffer) |
| 88 | return nullptr; |
| 89 | std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer); |
| 90 | |
| 91 | llvm::StringRef Path = Buffer->getBufferIdentifier(); |
| 92 | auto Stream = llvm::make_unique<llvm::MemoryBufferByteStream>( |
| 93 | std::move(Buffer), llvm::support::little); |
| 94 | |
| 95 | auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), Allocator); |
Zachary Turner | 8040eea | 2018-10-12 22:57:40 +0000 | [diff] [blame] | 96 | if (auto EC = File->parseFileHeaders()) { |
| 97 | llvm::consumeError(std::move(EC)); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 98 | return nullptr; |
Zachary Turner | 8040eea | 2018-10-12 22:57:40 +0000 | [diff] [blame] | 99 | } |
| 100 | if (auto EC = File->parseStreamData()) { |
| 101 | llvm::consumeError(std::move(EC)); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 102 | return nullptr; |
Zachary Turner | 8040eea | 2018-10-12 22:57:40 +0000 | [diff] [blame] | 103 | } |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 104 | |
| 105 | return File; |
| 106 | } |
| 107 | |
| 108 | static std::unique_ptr<PDBFile> |
| 109 | loadMatchingPDBFile(std::string exe_path, llvm::BumpPtrAllocator &allocator) { |
| 110 | // Try to find a matching PDB for an EXE. |
| 111 | using namespace llvm::object; |
| 112 | auto expected_binary = createBinary(exe_path); |
| 113 | |
| 114 | // If the file isn't a PE/COFF executable, fail. |
| 115 | if (!expected_binary) { |
| 116 | llvm::consumeError(expected_binary.takeError()); |
| 117 | return nullptr; |
| 118 | } |
| 119 | OwningBinary<Binary> binary = std::move(*expected_binary); |
| 120 | |
| 121 | auto *obj = llvm::dyn_cast<llvm::object::COFFObjectFile>(binary.getBinary()); |
| 122 | if (!obj) |
| 123 | return nullptr; |
| 124 | const llvm::codeview::DebugInfo *pdb_info = nullptr; |
| 125 | |
| 126 | // If it doesn't have a debug directory, fail. |
| 127 | llvm::StringRef pdb_file; |
| 128 | auto ec = obj->getDebugPDBInfo(pdb_info, pdb_file); |
| 129 | if (ec) |
| 130 | return nullptr; |
| 131 | |
| 132 | // if the file doesn't exist, is not a pdb, or doesn't have a matching guid, |
| 133 | // fail. |
| 134 | llvm::file_magic magic; |
| 135 | ec = llvm::identify_magic(pdb_file, magic); |
| 136 | if (ec || magic != llvm::file_magic::pdb) |
| 137 | return nullptr; |
| 138 | std::unique_ptr<PDBFile> pdb = loadPDBFile(pdb_file, allocator); |
Zachary Turner | 8040eea | 2018-10-12 22:57:40 +0000 | [diff] [blame] | 139 | if (!pdb) |
| 140 | return nullptr; |
| 141 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 142 | auto expected_info = pdb->getPDBInfoStream(); |
| 143 | if (!expected_info) { |
| 144 | llvm::consumeError(expected_info.takeError()); |
| 145 | return nullptr; |
| 146 | } |
| 147 | llvm::codeview::GUID guid; |
| 148 | memcpy(&guid, pdb_info->PDB70.Signature, 16); |
| 149 | |
| 150 | if (expected_info->getGuid() != guid) |
| 151 | return nullptr; |
| 152 | return pdb; |
| 153 | } |
| 154 | |
| 155 | static bool IsFunctionPrologue(const CompilandIndexItem &cci, |
| 156 | lldb::addr_t addr) { |
| 157 | // FIXME: Implement this. |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | static bool IsFunctionEpilogue(const CompilandIndexItem &cci, |
| 162 | lldb::addr_t addr) { |
| 163 | // FIXME: Implement this. |
| 164 | return false; |
| 165 | } |
| 166 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 167 | static llvm::StringRef GetSimpleTypeName(SimpleTypeKind kind) { |
| 168 | switch (kind) { |
| 169 | case SimpleTypeKind::Boolean128: |
| 170 | case SimpleTypeKind::Boolean16: |
| 171 | case SimpleTypeKind::Boolean32: |
| 172 | case SimpleTypeKind::Boolean64: |
| 173 | case SimpleTypeKind::Boolean8: |
| 174 | return "bool"; |
| 175 | case SimpleTypeKind::Byte: |
| 176 | case SimpleTypeKind::UnsignedCharacter: |
| 177 | return "unsigned char"; |
| 178 | case SimpleTypeKind::NarrowCharacter: |
| 179 | return "char"; |
| 180 | case SimpleTypeKind::SignedCharacter: |
| 181 | case SimpleTypeKind::SByte: |
Zachary Turner | 71ebb72 | 2018-10-23 22:15:05 +0000 | [diff] [blame] | 182 | return "signed char"; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 183 | case SimpleTypeKind::Character16: |
| 184 | return "char16_t"; |
| 185 | case SimpleTypeKind::Character32: |
| 186 | return "char32_t"; |
| 187 | case SimpleTypeKind::Complex80: |
| 188 | case SimpleTypeKind::Complex64: |
| 189 | case SimpleTypeKind::Complex32: |
| 190 | return "complex"; |
| 191 | case SimpleTypeKind::Float128: |
| 192 | case SimpleTypeKind::Float80: |
| 193 | return "long double"; |
| 194 | case SimpleTypeKind::Float64: |
| 195 | return "double"; |
| 196 | case SimpleTypeKind::Float32: |
| 197 | return "float"; |
| 198 | case SimpleTypeKind::Float16: |
| 199 | return "single"; |
| 200 | case SimpleTypeKind::Int128: |
| 201 | return "__int128"; |
| 202 | case SimpleTypeKind::Int64: |
| 203 | case SimpleTypeKind::Int64Quad: |
Zachary Turner | 71ebb72 | 2018-10-23 22:15:05 +0000 | [diff] [blame] | 204 | return "int64_t"; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 205 | case SimpleTypeKind::Int32: |
| 206 | return "int"; |
| 207 | case SimpleTypeKind::Int16: |
| 208 | return "short"; |
| 209 | case SimpleTypeKind::UInt128: |
| 210 | return "unsigned __int128"; |
| 211 | case SimpleTypeKind::UInt64: |
| 212 | case SimpleTypeKind::UInt64Quad: |
Zachary Turner | 71ebb72 | 2018-10-23 22:15:05 +0000 | [diff] [blame] | 213 | return "uint64_t"; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 214 | case SimpleTypeKind::HResult: |
| 215 | return "HRESULT"; |
| 216 | case SimpleTypeKind::UInt32: |
| 217 | return "unsigned"; |
| 218 | case SimpleTypeKind::UInt16: |
| 219 | case SimpleTypeKind::UInt16Short: |
| 220 | return "unsigned short"; |
| 221 | case SimpleTypeKind::Int32Long: |
| 222 | return "long"; |
| 223 | case SimpleTypeKind::UInt32Long: |
| 224 | return "unsigned long"; |
| 225 | case SimpleTypeKind::Void: |
| 226 | return "void"; |
| 227 | case SimpleTypeKind::WideCharacter: |
| 228 | return "wchar_t"; |
| 229 | default: |
| 230 | return ""; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | static bool IsClassRecord(TypeLeafKind kind) { |
| 235 | switch (kind) { |
| 236 | case LF_STRUCTURE: |
| 237 | case LF_CLASS: |
| 238 | case LF_INTERFACE: |
| 239 | return true; |
| 240 | default: |
| 241 | return false; |
| 242 | } |
| 243 | } |
| 244 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 245 | void SymbolFileNativePDB::Initialize() { |
| 246 | PluginManager::RegisterPlugin(GetPluginNameStatic(), |
| 247 | GetPluginDescriptionStatic(), CreateInstance, |
| 248 | DebuggerInitialize); |
| 249 | } |
| 250 | |
| 251 | void SymbolFileNativePDB::Terminate() { |
| 252 | PluginManager::UnregisterPlugin(CreateInstance); |
| 253 | } |
| 254 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 255 | void SymbolFileNativePDB::DebuggerInitialize(Debugger &debugger) {} |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 256 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 257 | ConstString SymbolFileNativePDB::GetPluginNameStatic() { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 258 | static ConstString g_name("native-pdb"); |
| 259 | return g_name; |
| 260 | } |
| 261 | |
| 262 | const char *SymbolFileNativePDB::GetPluginDescriptionStatic() { |
| 263 | return "Microsoft PDB debug symbol cross-platform file reader."; |
| 264 | } |
| 265 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 266 | SymbolFile *SymbolFileNativePDB::CreateInstance(ObjectFile *obj_file) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 267 | return new SymbolFileNativePDB(obj_file); |
| 268 | } |
| 269 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 270 | SymbolFileNativePDB::SymbolFileNativePDB(ObjectFile *object_file) |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 271 | : SymbolFile(object_file) {} |
| 272 | |
| 273 | SymbolFileNativePDB::~SymbolFileNativePDB() {} |
| 274 | |
| 275 | uint32_t SymbolFileNativePDB::CalculateAbilities() { |
| 276 | uint32_t abilities = 0; |
| 277 | if (!m_obj_file) |
| 278 | return 0; |
| 279 | |
| 280 | if (!m_index) { |
| 281 | // Lazily load and match the PDB file, but only do this once. |
| 282 | std::unique_ptr<PDBFile> file_up = |
| 283 | loadMatchingPDBFile(m_obj_file->GetFileSpec().GetPath(), m_allocator); |
| 284 | |
| 285 | if (!file_up) { |
| 286 | auto module_sp = m_obj_file->GetModule(); |
| 287 | if (!module_sp) |
| 288 | return 0; |
| 289 | // See if any symbol file is specified through `--symfile` option. |
| 290 | FileSpec symfile = module_sp->GetSymbolFileFileSpec(); |
| 291 | if (!symfile) |
| 292 | return 0; |
| 293 | file_up = loadPDBFile(symfile.GetPath(), m_allocator); |
| 294 | } |
| 295 | |
| 296 | if (!file_up) |
| 297 | return 0; |
| 298 | |
| 299 | auto expected_index = PdbIndex::create(std::move(file_up)); |
| 300 | if (!expected_index) { |
| 301 | llvm::consumeError(expected_index.takeError()); |
| 302 | return 0; |
| 303 | } |
| 304 | m_index = std::move(*expected_index); |
| 305 | } |
| 306 | if (!m_index) |
| 307 | return 0; |
| 308 | |
| 309 | // We don't especially have to be precise here. We only distinguish between |
| 310 | // stripped and not stripped. |
| 311 | abilities = kAllAbilities; |
| 312 | |
| 313 | if (m_index->dbi().isStripped()) |
| 314 | abilities &= ~(Blocks | LocalVariables); |
| 315 | return abilities; |
| 316 | } |
| 317 | |
| 318 | void SymbolFileNativePDB::InitializeObject() { |
| 319 | m_obj_load_address = m_obj_file->GetFileOffset(); |
| 320 | m_index->SetLoadAddress(m_obj_load_address); |
| 321 | m_index->ParseSectionContribs(); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 322 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 323 | TypeSystem *ts = m_obj_file->GetModule()->GetTypeSystemForLanguage( |
| 324 | lldb::eLanguageTypeC_plus_plus); |
| 325 | if (ts) |
| 326 | ts->SetSymbolFile(this); |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 327 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 328 | m_ast = llvm::make_unique<PdbAstBuilder>(*m_obj_file, *m_index); |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 331 | uint32_t SymbolFileNativePDB::GetNumCompileUnits() { |
| 332 | const DbiModuleList &modules = m_index->dbi().modules(); |
| 333 | uint32_t count = modules.getModuleCount(); |
| 334 | if (count == 0) |
| 335 | return count; |
| 336 | |
| 337 | // The linker can inject an additional "dummy" compilation unit into the |
| 338 | // PDB. Ignore this special compile unit for our purposes, if it is there. |
| 339 | // It is always the last one. |
| 340 | DbiModuleDescriptor last = modules.getModuleDescriptor(count - 1); |
| 341 | if (last.getModuleName() == "* Linker *") |
| 342 | --count; |
| 343 | return count; |
| 344 | } |
| 345 | |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 346 | Block &SymbolFileNativePDB::CreateBlock(PdbCompilandSymId block_id) { |
| 347 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi); |
| 348 | CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 349 | |
| 350 | if (sym.kind() == S_GPROC32 || sym.kind() == S_LPROC32) { |
| 351 | // This is a function. It must be global. Creating the Function entry for |
| 352 | // it automatically creates a block for it. |
| 353 | CompUnitSP comp_unit = GetOrCreateCompileUnit(*cii); |
| 354 | return GetOrCreateFunction(block_id, *comp_unit)->GetBlock(false); |
| 355 | } |
| 356 | |
| 357 | lldbassert(sym.kind() == S_BLOCK32); |
| 358 | |
| 359 | // This is a block. Its parent is either a function or another block. In |
| 360 | // either case, its parent can be viewed as a block (e.g. a function contains |
| 361 | // 1 big block. So just get the parent block and add this block to it. |
| 362 | BlockSym block(static_cast<SymbolRecordKind>(sym.kind())); |
| 363 | cantFail(SymbolDeserializer::deserializeAs<BlockSym>(sym, block)); |
| 364 | lldbassert(block.Parent != 0); |
| 365 | PdbCompilandSymId parent_id(block_id.modi, block.Parent); |
| 366 | Block &parent_block = GetOrCreateBlock(parent_id); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 367 | lldb::user_id_t opaque_block_uid = toOpaqueUid(block_id); |
| 368 | BlockSP child_block = std::make_shared<Block>(opaque_block_uid); |
| 369 | parent_block.AddChild(child_block); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 370 | |
| 371 | m_ast->GetOrCreateBlockDecl(block_id); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 372 | |
| 373 | m_blocks.insert({opaque_block_uid, child_block}); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 374 | return *child_block; |
| 375 | } |
| 376 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 377 | lldb::FunctionSP SymbolFileNativePDB::CreateFunction(PdbCompilandSymId func_id, |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 378 | CompileUnit &comp_unit) { |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 379 | const CompilandIndexItem *cci = |
| 380 | m_index->compilands().GetCompiland(func_id.modi); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 381 | lldbassert(cci); |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 382 | CVSymbol sym_record = cci->m_debug_stream.readSymbolAtOffset(func_id.offset); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 383 | |
| 384 | lldbassert(sym_record.kind() == S_LPROC32 || sym_record.kind() == S_GPROC32); |
| 385 | SegmentOffsetLength sol = GetSegmentOffsetAndLength(sym_record); |
| 386 | |
| 387 | auto file_vm_addr = m_index->MakeVirtualAddress(sol.so); |
| 388 | if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0) |
| 389 | return nullptr; |
| 390 | |
| 391 | AddressRange func_range(file_vm_addr, sol.length, |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 392 | comp_unit.GetModule()->GetSectionList()); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 393 | if (!func_range.GetBaseAddress().IsValid()) |
| 394 | return nullptr; |
| 395 | |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 396 | ProcSym proc(static_cast<SymbolRecordKind>(sym_record.kind())); |
| 397 | cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym_record, proc)); |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 398 | if (proc.FunctionType == TypeIndex::None()) |
| 399 | return nullptr; |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 400 | TypeSP func_type = GetOrCreateType(proc.FunctionType); |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 401 | if (!func_type) |
| 402 | return nullptr; |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 403 | |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 404 | PdbTypeSymId sig_id(proc.FunctionType, false); |
| 405 | Mangled mangled(proc.Name); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 406 | FunctionSP func_sp = std::make_shared<Function>( |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 407 | &comp_unit, toOpaqueUid(func_id), toOpaqueUid(sig_id), mangled, |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 408 | func_type.get(), func_range); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 409 | |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 410 | comp_unit.AddFunction(func_sp); |
| 411 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 412 | m_ast->GetOrCreateFunctionDecl(func_id); |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 413 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 414 | return func_sp; |
| 415 | } |
| 416 | |
| 417 | CompUnitSP |
| 418 | SymbolFileNativePDB::CreateCompileUnit(const CompilandIndexItem &cci) { |
| 419 | lldb::LanguageType lang = |
| 420 | cci.m_compile_opts ? TranslateLanguage(cci.m_compile_opts->getLanguage()) |
| 421 | : lldb::eLanguageTypeUnknown; |
| 422 | |
| 423 | LazyBool optimized = eLazyBoolNo; |
| 424 | if (cci.m_compile_opts && cci.m_compile_opts->hasOptimizations()) |
| 425 | optimized = eLazyBoolYes; |
| 426 | |
Zachary Turner | 51f88af | 2018-12-19 19:45:30 +0000 | [diff] [blame] | 427 | llvm::SmallString<64> source_file_name = |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 428 | m_index->compilands().GetMainSourceFile(cci); |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 429 | FileSpec fs(source_file_name); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 430 | |
| 431 | CompUnitSP cu_sp = |
| 432 | std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, fs, |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 433 | toOpaqueUid(cci.m_id), lang, optimized); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 434 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 435 | m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex( |
| 436 | cci.m_id.modi, cu_sp); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 437 | return cu_sp; |
| 438 | } |
| 439 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 440 | lldb::TypeSP SymbolFileNativePDB::CreateModifierType(PdbTypeSymId type_id, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 441 | const ModifierRecord &mr, |
| 442 | CompilerType ct) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 443 | TpiStream &stream = m_index->tpi(); |
| 444 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 445 | std::string name; |
| 446 | if (mr.ModifiedType.isSimple()) |
| 447 | name = GetSimpleTypeName(mr.ModifiedType.getSimpleKind()); |
| 448 | else |
| 449 | name = computeTypeName(stream.typeCollection(), mr.ModifiedType); |
| 450 | Declaration decl; |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 451 | lldb::TypeSP modified_type = GetOrCreateType(mr.ModifiedType); |
| 452 | |
| 453 | return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(name), |
| 454 | modified_type->GetByteSize(), nullptr, |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 455 | LLDB_INVALID_UID, Type::eEncodingIsUID, decl, |
| 456 | ct, Type::eResolveStateFull); |
| 457 | } |
| 458 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 459 | lldb::TypeSP |
| 460 | SymbolFileNativePDB::CreatePointerType(PdbTypeSymId type_id, |
| 461 | const llvm::codeview::PointerRecord &pr, |
| 462 | CompilerType ct) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 463 | TypeSP pointee = GetOrCreateType(pr.ReferentType); |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 464 | if (!pointee) |
| 465 | return nullptr; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 466 | |
| 467 | if (pr.isPointerToMember()) { |
| 468 | MemberPointerInfo mpi = pr.getMemberInfo(); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 469 | GetOrCreateType(mpi.ContainingType); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 472 | Declaration decl; |
| 473 | return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(), |
| 474 | pr.getSize(), nullptr, LLDB_INVALID_UID, |
| 475 | Type::eEncodingIsUID, decl, ct, |
| 476 | Type::eResolveStateFull); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 477 | } |
| 478 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 479 | lldb::TypeSP SymbolFileNativePDB::CreateSimpleType(TypeIndex ti, |
| 480 | CompilerType ct) { |
Zachary Turner | 9fbf935 | 2018-11-16 03:16:27 +0000 | [diff] [blame] | 481 | uint64_t uid = toOpaqueUid(PdbTypeSymId(ti, false)); |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 482 | if (ti == TypeIndex::NullptrT()) { |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 483 | Declaration decl; |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 484 | return std::make_shared<Type>( |
| 485 | uid, this, ConstString("std::nullptr_t"), 0, nullptr, LLDB_INVALID_UID, |
| 486 | Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull); |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 489 | if (ti.getSimpleMode() != SimpleTypeMode::Direct) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 490 | TypeSP direct_sp = GetOrCreateType(ti.makeDirect()); |
Zachary Turner | 71ebb72 | 2018-10-23 22:15:05 +0000 | [diff] [blame] | 491 | uint32_t pointer_size = 0; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 492 | switch (ti.getSimpleMode()) { |
| 493 | case SimpleTypeMode::FarPointer32: |
| 494 | case SimpleTypeMode::NearPointer32: |
| 495 | pointer_size = 4; |
| 496 | break; |
| 497 | case SimpleTypeMode::NearPointer64: |
| 498 | pointer_size = 8; |
| 499 | break; |
| 500 | default: |
| 501 | // 128-bit and 16-bit pointers unsupported. |
| 502 | return nullptr; |
| 503 | } |
| 504 | Declaration decl; |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 505 | return std::make_shared<Type>( |
| 506 | uid, this, ConstString(), pointer_size, nullptr, LLDB_INVALID_UID, |
| 507 | Type::eEncodingIsUID, decl, ct, Type::eResolveStateFull); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 510 | if (ti.getSimpleKind() == SimpleTypeKind::NotTranslated) |
| 511 | return nullptr; |
| 512 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 513 | size_t size = GetTypeSizeForSimpleKind(ti.getSimpleKind()); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 514 | llvm::StringRef type_name = GetSimpleTypeName(ti.getSimpleKind()); |
| 515 | |
| 516 | Declaration decl; |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 517 | return std::make_shared<Type>(uid, this, ConstString(type_name), size, |
| 518 | nullptr, LLDB_INVALID_UID, Type::eEncodingIsUID, |
| 519 | decl, ct, Type::eResolveStateFull); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 522 | static std::string GetUnqualifiedTypeName(const TagRecord &record) { |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 523 | if (!record.hasUniqueName()) { |
| 524 | MSVCUndecoratedNameParser parser(record.Name); |
| 525 | llvm::ArrayRef<MSVCUndecoratedNameSpecifier> specs = parser.GetSpecifiers(); |
| 526 | |
| 527 | return specs.back().GetBaseName(); |
| 528 | } |
| 529 | |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 530 | llvm::ms_demangle::Demangler demangler; |
| 531 | StringView sv(record.UniqueName.begin(), record.UniqueName.size()); |
| 532 | llvm::ms_demangle::TagTypeNode *ttn = demangler.parseTagUniqueName(sv); |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 533 | if (demangler.Error) |
| 534 | return record.Name; |
| 535 | |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 536 | llvm::ms_demangle::IdentifierNode *idn = |
| 537 | ttn->QualifiedName->getUnqualifiedIdentifier(); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 538 | return idn->toString(); |
Zachary Turner | 056e4ab | 2018-11-08 18:50:11 +0000 | [diff] [blame] | 539 | } |
| 540 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 541 | lldb::TypeSP |
| 542 | SymbolFileNativePDB::CreateClassStructUnion(PdbTypeSymId type_id, |
| 543 | const TagRecord &record, |
| 544 | size_t size, CompilerType ct) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 545 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 546 | std::string uname = GetUnqualifiedTypeName(record); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 547 | |
| 548 | // FIXME: Search IPI stream for LF_UDT_MOD_SRC_LINE. |
| 549 | Declaration decl; |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 550 | return std::make_shared<Type>(toOpaqueUid(type_id), this, ConstString(uname), |
| 551 | size, nullptr, LLDB_INVALID_UID, |
| 552 | Type::eEncodingIsUID, decl, ct, |
| 553 | Type::eResolveStateForward); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 556 | lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 557 | const ClassRecord &cr, |
| 558 | CompilerType ct) { |
| 559 | return CreateClassStructUnion(type_id, cr, cr.getSize(), ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 562 | lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 563 | const UnionRecord &ur, |
| 564 | CompilerType ct) { |
| 565 | return CreateClassStructUnion(type_id, ur, ur.getSize(), ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 568 | lldb::TypeSP SymbolFileNativePDB::CreateTagType(PdbTypeSymId type_id, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 569 | const EnumRecord &er, |
| 570 | CompilerType ct) { |
| 571 | std::string uname = GetUnqualifiedTypeName(er); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 572 | |
| 573 | Declaration decl; |
| 574 | TypeSP underlying_type = GetOrCreateType(er.UnderlyingType); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 575 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 576 | return std::make_shared<lldb_private::Type>( |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 577 | toOpaqueUid(type_id), this, ConstString(uname), |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 578 | underlying_type->GetByteSize(), nullptr, LLDB_INVALID_UID, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 579 | lldb_private::Type::eEncodingIsUID, decl, ct, |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 580 | lldb_private::Type::eResolveStateForward); |
| 581 | } |
| 582 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 583 | TypeSP SymbolFileNativePDB::CreateArrayType(PdbTypeSymId type_id, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 584 | const ArrayRecord &ar, |
| 585 | CompilerType ct) { |
Zachary Turner | 511bff2 | 2018-10-30 18:57:08 +0000 | [diff] [blame] | 586 | TypeSP element_type = GetOrCreateType(ar.ElementType); |
Zachary Turner | 511bff2 | 2018-10-30 18:57:08 +0000 | [diff] [blame] | 587 | |
| 588 | Declaration decl; |
| 589 | TypeSP array_sp = std::make_shared<lldb_private::Type>( |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 590 | toOpaqueUid(type_id), this, ConstString(), ar.Size, nullptr, |
| 591 | LLDB_INVALID_UID, lldb_private::Type::eEncodingIsUID, decl, ct, |
| 592 | lldb_private::Type::eResolveStateFull); |
Zachary Turner | 511bff2 | 2018-10-30 18:57:08 +0000 | [diff] [blame] | 593 | array_sp->SetEncodingType(element_type.get()); |
| 594 | return array_sp; |
| 595 | } |
| 596 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 597 | TypeSP SymbolFileNativePDB::CreateProcedureType(PdbTypeSymId type_id, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 598 | const ProcedureRecord &pr, |
| 599 | CompilerType ct) { |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 600 | Declaration decl; |
| 601 | return std::make_shared<lldb_private::Type>( |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 602 | toOpaqueUid(type_id), this, ConstString(), 0, nullptr, LLDB_INVALID_UID, |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 603 | lldb_private::Type::eEncodingIsUID, decl, ct, |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 604 | lldb_private::Type::eResolveStateFull); |
| 605 | } |
| 606 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 607 | TypeSP SymbolFileNativePDB::CreateType(PdbTypeSymId type_id, CompilerType ct) { |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 608 | if (type_id.index.isSimple()) |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 609 | return CreateSimpleType(type_id.index, ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 610 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 611 | TpiStream &stream = type_id.is_ipi ? m_index->ipi() : m_index->tpi(); |
| 612 | CVType cvt = stream.getType(type_id.index); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 613 | |
| 614 | if (cvt.kind() == LF_MODIFIER) { |
| 615 | ModifierRecord modifier; |
| 616 | llvm::cantFail( |
| 617 | TypeDeserializer::deserializeAs<ModifierRecord>(cvt, modifier)); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 618 | return CreateModifierType(type_id, modifier, ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | if (cvt.kind() == LF_POINTER) { |
| 622 | PointerRecord pointer; |
| 623 | llvm::cantFail( |
| 624 | TypeDeserializer::deserializeAs<PointerRecord>(cvt, pointer)); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 625 | return CreatePointerType(type_id, pointer, ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (IsClassRecord(cvt.kind())) { |
| 629 | ClassRecord cr; |
| 630 | llvm::cantFail(TypeDeserializer::deserializeAs<ClassRecord>(cvt, cr)); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 631 | return CreateTagType(type_id, cr, ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | if (cvt.kind() == LF_ENUM) { |
| 635 | EnumRecord er; |
| 636 | llvm::cantFail(TypeDeserializer::deserializeAs<EnumRecord>(cvt, er)); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 637 | return CreateTagType(type_id, er, ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | if (cvt.kind() == LF_UNION) { |
| 641 | UnionRecord ur; |
| 642 | llvm::cantFail(TypeDeserializer::deserializeAs<UnionRecord>(cvt, ur)); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 643 | return CreateTagType(type_id, ur, ct); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 644 | } |
| 645 | |
Zachary Turner | 511bff2 | 2018-10-30 18:57:08 +0000 | [diff] [blame] | 646 | if (cvt.kind() == LF_ARRAY) { |
| 647 | ArrayRecord ar; |
| 648 | llvm::cantFail(TypeDeserializer::deserializeAs<ArrayRecord>(cvt, ar)); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 649 | return CreateArrayType(type_id, ar, ct); |
Zachary Turner | 511bff2 | 2018-10-30 18:57:08 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 652 | if (cvt.kind() == LF_PROCEDURE) { |
| 653 | ProcedureRecord pr; |
| 654 | llvm::cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(cvt, pr)); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 655 | return CreateProcedureType(type_id, pr, ct); |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 658 | return nullptr; |
| 659 | } |
| 660 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 661 | TypeSP SymbolFileNativePDB::CreateAndCacheType(PdbTypeSymId type_id) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 662 | // If they search for a UDT which is a forward ref, try and resolve the full |
| 663 | // decl and just map the forward ref uid to the full decl record. |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 664 | llvm::Optional<PdbTypeSymId> full_decl_uid; |
| 665 | if (IsForwardRefUdt(type_id, m_index->tpi())) { |
| 666 | auto expected_full_ti = |
| 667 | m_index->tpi().findFullDeclForForwardRef(type_id.index); |
| 668 | if (!expected_full_ti) |
| 669 | llvm::consumeError(expected_full_ti.takeError()); |
| 670 | else if (*expected_full_ti != type_id.index) { |
Zachary Turner | 9fbf935 | 2018-11-16 03:16:27 +0000 | [diff] [blame] | 671 | full_decl_uid = PdbTypeSymId(*expected_full_ti, false); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 672 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 673 | // It's possible that a lookup would occur for the full decl causing it |
| 674 | // to be cached, then a second lookup would occur for the forward decl. |
| 675 | // We don't want to create a second full decl, so make sure the full |
| 676 | // decl hasn't already been cached. |
| 677 | auto full_iter = m_types.find(toOpaqueUid(*full_decl_uid)); |
| 678 | if (full_iter != m_types.end()) { |
| 679 | TypeSP result = full_iter->second; |
| 680 | // Map the forward decl to the TypeSP for the full decl so we can take |
| 681 | // the fast path next time. |
| 682 | m_types[toOpaqueUid(type_id)] = result; |
| 683 | return result; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 688 | PdbTypeSymId best_decl_id = full_decl_uid ? *full_decl_uid : type_id; |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 689 | |
| 690 | clang::QualType qt = m_ast->GetOrCreateType(best_decl_id); |
| 691 | |
| 692 | TypeSP result = CreateType(best_decl_id, m_ast->ToCompilerType(qt)); |
Zachary Turner | 544a66d8 | 2018-11-01 16:37:29 +0000 | [diff] [blame] | 693 | if (!result) |
| 694 | return nullptr; |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 695 | |
| 696 | uint64_t best_uid = toOpaqueUid(best_decl_id); |
| 697 | m_types[best_uid] = result; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 698 | // If we had both a forward decl and a full decl, make both point to the new |
| 699 | // type. |
| 700 | if (full_decl_uid) |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 701 | m_types[toOpaqueUid(type_id)] = result; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 702 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 703 | return result; |
| 704 | } |
| 705 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 706 | TypeSP SymbolFileNativePDB::GetOrCreateType(PdbTypeSymId type_id) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 707 | // We can't use try_emplace / overwrite here because the process of creating |
| 708 | // a type could create nested types, which could invalidate iterators. So |
| 709 | // we have to do a 2-phase lookup / insert. |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 710 | auto iter = m_types.find(toOpaqueUid(type_id)); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 711 | if (iter != m_types.end()) |
| 712 | return iter->second; |
| 713 | |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 714 | TypeSP type = CreateAndCacheType(type_id); |
| 715 | if (type) |
| 716 | m_obj_file->GetModule()->GetTypeList()->Insert(type); |
| 717 | return type; |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 720 | VariableSP SymbolFileNativePDB::CreateGlobalVariable(PdbGlobalSymId var_id) { |
| 721 | CVSymbol sym = m_index->symrecords().readRecord(var_id.offset); |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 722 | if (sym.kind() == S_CONSTANT) |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 723 | return CreateConstantSymbol(var_id, sym); |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 724 | |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 725 | lldb::ValueType scope = eValueTypeInvalid; |
| 726 | TypeIndex ti; |
| 727 | llvm::StringRef name; |
| 728 | lldb::addr_t addr = 0; |
| 729 | uint16_t section = 0; |
| 730 | uint32_t offset = 0; |
| 731 | bool is_external = false; |
| 732 | switch (sym.kind()) { |
| 733 | case S_GDATA32: |
| 734 | is_external = true; |
| 735 | LLVM_FALLTHROUGH; |
| 736 | case S_LDATA32: { |
| 737 | DataSym ds(sym.kind()); |
| 738 | llvm::cantFail(SymbolDeserializer::deserializeAs<DataSym>(sym, ds)); |
| 739 | ti = ds.Type; |
| 740 | scope = (sym.kind() == S_GDATA32) ? eValueTypeVariableGlobal |
| 741 | : eValueTypeVariableStatic; |
| 742 | name = ds.Name; |
| 743 | section = ds.Segment; |
| 744 | offset = ds.DataOffset; |
| 745 | addr = m_index->MakeVirtualAddress(ds.Segment, ds.DataOffset); |
| 746 | break; |
| 747 | } |
| 748 | case S_GTHREAD32: |
| 749 | is_external = true; |
| 750 | LLVM_FALLTHROUGH; |
| 751 | case S_LTHREAD32: { |
| 752 | ThreadLocalDataSym tlds(sym.kind()); |
| 753 | llvm::cantFail( |
| 754 | SymbolDeserializer::deserializeAs<ThreadLocalDataSym>(sym, tlds)); |
| 755 | ti = tlds.Type; |
| 756 | name = tlds.Name; |
| 757 | section = tlds.Segment; |
| 758 | offset = tlds.DataOffset; |
| 759 | addr = m_index->MakeVirtualAddress(tlds.Segment, tlds.DataOffset); |
| 760 | scope = eValueTypeVariableThreadLocal; |
| 761 | break; |
| 762 | } |
| 763 | default: |
| 764 | llvm_unreachable("unreachable!"); |
| 765 | } |
| 766 | |
| 767 | CompUnitSP comp_unit; |
| 768 | llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(addr); |
| 769 | if (modi) { |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 770 | CompilandIndexItem &cci = m_index->compilands().GetOrCreateCompiland(*modi); |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 771 | comp_unit = GetOrCreateCompileUnit(cci); |
| 772 | } |
| 773 | |
| 774 | Declaration decl; |
Zachary Turner | 9fbf935 | 2018-11-16 03:16:27 +0000 | [diff] [blame] | 775 | PdbTypeSymId tid(ti, false); |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 776 | SymbolFileTypeSP type_sp = |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 777 | std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid)); |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 778 | Variable::RangeList ranges; |
| 779 | |
Zachary Turner | 2256633 | 2019-01-02 18:33:54 +0000 | [diff] [blame] | 780 | m_ast->GetOrCreateVariableDecl(var_id); |
Zachary Turner | 3790029 | 2018-12-20 23:32:37 +0000 | [diff] [blame] | 781 | |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 782 | DWARFExpression location = MakeGlobalLocationExpression( |
| 783 | section, offset, GetObjectFile()->GetModule()); |
| 784 | |
| 785 | std::string global_name("::"); |
| 786 | global_name += name; |
| 787 | VariableSP var_sp = std::make_shared<Variable>( |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 788 | toOpaqueUid(var_id), name.str().c_str(), global_name.c_str(), type_sp, |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 789 | scope, comp_unit.get(), ranges, &decl, location, is_external, false, |
| 790 | false); |
| 791 | var_sp->SetLocationIsConstantValueData(false); |
| 792 | |
| 793 | return var_sp; |
| 794 | } |
| 795 | |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 796 | lldb::VariableSP |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 797 | SymbolFileNativePDB::CreateConstantSymbol(PdbGlobalSymId var_id, |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 798 | const CVSymbol &cvs) { |
| 799 | TpiStream &tpi = m_index->tpi(); |
| 800 | ConstantSym constant(cvs.kind()); |
| 801 | |
| 802 | llvm::cantFail(SymbolDeserializer::deserializeAs<ConstantSym>(cvs, constant)); |
| 803 | std::string global_name("::"); |
| 804 | global_name += constant.Name; |
Zachary Turner | 9fbf935 | 2018-11-16 03:16:27 +0000 | [diff] [blame] | 805 | PdbTypeSymId tid(constant.Type, false); |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 806 | SymbolFileTypeSP type_sp = |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 807 | std::make_shared<SymbolFileType>(*this, toOpaqueUid(tid)); |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 808 | |
| 809 | Declaration decl; |
| 810 | Variable::RangeList ranges; |
| 811 | ModuleSP module = GetObjectFile()->GetModule(); |
Zachary Turner | a93458b | 2018-12-06 17:49:15 +0000 | [diff] [blame] | 812 | DWARFExpression location = MakeConstantLocationExpression( |
| 813 | constant.Type, tpi, constant.Value, module); |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 814 | |
| 815 | VariableSP var_sp = std::make_shared<Variable>( |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 816 | toOpaqueUid(var_id), constant.Name.str().c_str(), global_name.c_str(), |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 817 | type_sp, eValueTypeVariableGlobal, module.get(), ranges, &decl, location, |
| 818 | false, false, false); |
| 819 | var_sp->SetLocationIsConstantValueData(true); |
| 820 | return var_sp; |
| 821 | } |
| 822 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 823 | VariableSP |
| 824 | SymbolFileNativePDB::GetOrCreateGlobalVariable(PdbGlobalSymId var_id) { |
| 825 | auto emplace_result = m_global_vars.try_emplace(toOpaqueUid(var_id), nullptr); |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 826 | if (emplace_result.second) |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 827 | emplace_result.first->second = CreateGlobalVariable(var_id); |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 828 | |
| 829 | return emplace_result.first->second; |
| 830 | } |
| 831 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 832 | lldb::TypeSP SymbolFileNativePDB::GetOrCreateType(TypeIndex ti) { |
Zachary Turner | 9fbf935 | 2018-11-16 03:16:27 +0000 | [diff] [blame] | 833 | return GetOrCreateType(PdbTypeSymId(ti, false)); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 836 | FunctionSP SymbolFileNativePDB::GetOrCreateFunction(PdbCompilandSymId func_id, |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 837 | CompileUnit &comp_unit) { |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 838 | auto emplace_result = m_functions.try_emplace(toOpaqueUid(func_id), nullptr); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 839 | if (emplace_result.second) |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 840 | emplace_result.first->second = CreateFunction(func_id, comp_unit); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 841 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 842 | return emplace_result.first->second; |
| 843 | } |
| 844 | |
| 845 | CompUnitSP |
| 846 | SymbolFileNativePDB::GetOrCreateCompileUnit(const CompilandIndexItem &cci) { |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 847 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 848 | auto emplace_result = |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 849 | m_compilands.try_emplace(toOpaqueUid(cci.m_id), nullptr); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 850 | if (emplace_result.second) |
| 851 | emplace_result.first->second = CreateCompileUnit(cci); |
| 852 | |
| 853 | lldbassert(emplace_result.first->second); |
| 854 | return emplace_result.first->second; |
| 855 | } |
| 856 | |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 857 | Block &SymbolFileNativePDB::GetOrCreateBlock(PdbCompilandSymId block_id) { |
| 858 | auto iter = m_blocks.find(toOpaqueUid(block_id)); |
| 859 | if (iter != m_blocks.end()) |
| 860 | return *iter->second; |
| 861 | |
| 862 | return CreateBlock(block_id); |
| 863 | } |
| 864 | |
Zachary Turner | 2256633 | 2019-01-02 18:33:54 +0000 | [diff] [blame] | 865 | void SymbolFileNativePDB::ParseDeclsForContext( |
| 866 | lldb_private::CompilerDeclContext decl_ctx) { |
| 867 | clang::DeclContext *context = m_ast->FromCompilerDeclContext(decl_ctx); |
| 868 | if (!context) |
| 869 | return; |
| 870 | m_ast->ParseDeclsForContext(*context); |
| 871 | } |
| 872 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 873 | lldb::CompUnitSP SymbolFileNativePDB::ParseCompileUnitAtIndex(uint32_t index) { |
| 874 | if (index >= GetNumCompileUnits()) |
| 875 | return CompUnitSP(); |
| 876 | lldbassert(index < UINT16_MAX); |
| 877 | if (index >= UINT16_MAX) |
| 878 | return nullptr; |
| 879 | |
| 880 | CompilandIndexItem &item = m_index->compilands().GetOrCreateCompiland(index); |
| 881 | |
| 882 | return GetOrCreateCompileUnit(item); |
| 883 | } |
| 884 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 885 | lldb::LanguageType SymbolFileNativePDB::ParseLanguage(CompileUnit &comp_unit) { |
| 886 | PdbSymUid uid(comp_unit.GetID()); |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 887 | lldbassert(uid.kind() == PdbSymUidKind::Compiland); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 888 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 889 | CompilandIndexItem *item = |
| 890 | m_index->compilands().GetCompiland(uid.asCompiland().modi); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 891 | lldbassert(item); |
| 892 | if (!item->m_compile_opts) |
| 893 | return lldb::eLanguageTypeUnknown; |
| 894 | |
| 895 | return TranslateLanguage(item->m_compile_opts->getLanguage()); |
| 896 | } |
| 897 | |
Zachary Turner | b3130b4 | 2019-01-02 18:32:50 +0000 | [diff] [blame] | 898 | void SymbolFileNativePDB::AddSymbols(Symtab &symtab) { return; } |
| 899 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 900 | size_t SymbolFileNativePDB::ParseFunctions(CompileUnit &comp_unit) { |
| 901 | PdbSymUid uid{comp_unit.GetID()}; |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 902 | lldbassert(uid.kind() == PdbSymUidKind::Compiland); |
| 903 | uint16_t modi = uid.asCompiland().modi; |
| 904 | CompilandIndexItem &cii = m_index->compilands().GetOrCreateCompiland(modi); |
| 905 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 906 | size_t count = comp_unit.GetNumFunctions(); |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 907 | const CVSymbolArray &syms = cii.m_debug_stream.getSymbolArray(); |
| 908 | for (auto iter = syms.begin(); iter != syms.end(); ++iter) { |
| 909 | if (iter->kind() != S_LPROC32 && iter->kind() != S_GPROC32) |
| 910 | continue; |
| 911 | |
| 912 | PdbCompilandSymId sym_id{modi, iter.offset()}; |
| 913 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 914 | FunctionSP func = GetOrCreateFunction(sym_id, comp_unit); |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 917 | size_t new_count = comp_unit.GetNumFunctions(); |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 918 | lldbassert(new_count >= count); |
| 919 | return new_count - count; |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | static bool NeedsResolvedCompileUnit(uint32_t resolve_scope) { |
| 923 | // If any of these flags are set, we need to resolve the compile unit. |
| 924 | uint32_t flags = eSymbolContextCompUnit; |
| 925 | flags |= eSymbolContextVariable; |
| 926 | flags |= eSymbolContextFunction; |
| 927 | flags |= eSymbolContextBlock; |
| 928 | flags |= eSymbolContextLineEntry; |
| 929 | return (resolve_scope & flags) != 0; |
| 930 | } |
| 931 | |
Zachary Turner | 991e445 | 2018-10-25 20:45:19 +0000 | [diff] [blame] | 932 | uint32_t SymbolFileNativePDB::ResolveSymbolContext( |
| 933 | const Address &addr, SymbolContextItem resolve_scope, SymbolContext &sc) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 934 | uint32_t resolved_flags = 0; |
| 935 | lldb::addr_t file_addr = addr.GetFileAddress(); |
| 936 | |
| 937 | if (NeedsResolvedCompileUnit(resolve_scope)) { |
| 938 | llvm::Optional<uint16_t> modi = m_index->GetModuleIndexForVa(file_addr); |
| 939 | if (!modi) |
| 940 | return 0; |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 941 | CompilandIndexItem *cci = m_index->compilands().GetCompiland(*modi); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 942 | if (!cci) |
| 943 | return 0; |
| 944 | |
| 945 | sc.comp_unit = GetOrCreateCompileUnit(*cci).get(); |
| 946 | resolved_flags |= eSymbolContextCompUnit; |
| 947 | } |
| 948 | |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 949 | if (resolve_scope & eSymbolContextFunction || |
| 950 | resolve_scope & eSymbolContextBlock) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 951 | lldbassert(sc.comp_unit); |
| 952 | std::vector<SymbolAndUid> matches = m_index->FindSymbolsByVa(file_addr); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 953 | // Search the matches in reverse. This way if there are multiple matches |
| 954 | // (for example we are 3 levels deep in a nested scope) it will find the |
| 955 | // innermost one first. |
| 956 | for (const auto &match : llvm::reverse(matches)) { |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 957 | if (match.uid.kind() != PdbSymUidKind::CompilandSym) |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 958 | continue; |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 959 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 960 | PdbCompilandSymId csid = match.uid.asCompilandSym(); |
| 961 | CVSymbol cvs = m_index->ReadSymbolRecord(csid); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 962 | PDB_SymType type = CVSymToPDBSym(cvs.kind()); |
| 963 | if (type != PDB_SymType::Function && type != PDB_SymType::Block) |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 964 | continue; |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 965 | if (type == PDB_SymType::Function) { |
| 966 | sc.function = GetOrCreateFunction(csid, *sc.comp_unit).get(); |
| 967 | sc.block = sc.GetFunctionBlock(); |
| 968 | } |
| 969 | |
| 970 | if (type == PDB_SymType::Block) { |
| 971 | sc.block = &GetOrCreateBlock(csid); |
| 972 | sc.function = sc.block->CalculateSymbolContextFunction(); |
| 973 | } |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 974 | resolved_flags |= eSymbolContextFunction; |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 975 | resolved_flags |= eSymbolContextBlock; |
| 976 | break; |
| 977 | } |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | if (resolve_scope & eSymbolContextLineEntry) { |
| 981 | lldbassert(sc.comp_unit); |
| 982 | if (auto *line_table = sc.comp_unit->GetLineTable()) { |
| 983 | if (line_table->FindLineEntryByAddress(addr, sc.line_entry)) |
| 984 | resolved_flags |= eSymbolContextLineEntry; |
| 985 | } |
| 986 | } |
| 987 | |
| 988 | return resolved_flags; |
| 989 | } |
| 990 | |
Zachary Turner | b3130b4 | 2019-01-02 18:32:50 +0000 | [diff] [blame] | 991 | uint32_t SymbolFileNativePDB::ResolveSymbolContext( |
| 992 | const FileSpec &file_spec, uint32_t line, bool check_inlines, |
| 993 | lldb::SymbolContextItem resolve_scope, SymbolContextList &sc_list) { |
| 994 | return 0; |
| 995 | } |
| 996 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 997 | static void AppendLineEntryToSequence(LineTable &table, LineSequence &sequence, |
| 998 | const CompilandIndexItem &cci, |
| 999 | lldb::addr_t base_addr, |
| 1000 | uint32_t file_number, |
| 1001 | const LineFragmentHeader &block, |
| 1002 | const LineNumberEntry &cur) { |
| 1003 | LineInfo cur_info(cur.Flags); |
| 1004 | |
| 1005 | if (cur_info.isAlwaysStepInto() || cur_info.isNeverStepInto()) |
| 1006 | return; |
| 1007 | |
| 1008 | uint64_t addr = base_addr + cur.Offset; |
| 1009 | |
| 1010 | bool is_statement = cur_info.isStatement(); |
| 1011 | bool is_prologue = IsFunctionPrologue(cci, addr); |
| 1012 | bool is_epilogue = IsFunctionEpilogue(cci, addr); |
| 1013 | |
| 1014 | uint32_t lno = cur_info.getStartLine(); |
| 1015 | |
| 1016 | table.AppendLineEntryToSequence(&sequence, addr, lno, 0, file_number, |
| 1017 | is_statement, false, is_prologue, is_epilogue, |
| 1018 | false); |
| 1019 | } |
| 1020 | |
| 1021 | static void TerminateLineSequence(LineTable &table, |
| 1022 | const LineFragmentHeader &block, |
| 1023 | lldb::addr_t base_addr, uint32_t file_number, |
| 1024 | uint32_t last_line, |
| 1025 | std::unique_ptr<LineSequence> seq) { |
| 1026 | // The end is always a terminal entry, so insert it regardless. |
| 1027 | table.AppendLineEntryToSequence(seq.get(), base_addr + block.CodeSize, |
| 1028 | last_line, 0, file_number, false, false, |
| 1029 | false, false, true); |
| 1030 | table.InsertSequence(seq.release()); |
| 1031 | } |
| 1032 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 1033 | bool SymbolFileNativePDB::ParseLineTable(CompileUnit &comp_unit) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1034 | // Unfortunately LLDB is set up to parse the entire compile unit line table |
| 1035 | // all at once, even if all it really needs is line info for a specific |
| 1036 | // function. In the future it would be nice if it could set the sc.m_function |
| 1037 | // member, and we could only get the line info for the function in question. |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 1038 | PdbSymUid cu_id(comp_unit.GetID()); |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 1039 | lldbassert(cu_id.kind() == PdbSymUidKind::Compiland); |
| 1040 | CompilandIndexItem *cci = |
| 1041 | m_index->compilands().GetCompiland(cu_id.asCompiland().modi); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1042 | lldbassert(cci); |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 1043 | auto line_table = llvm::make_unique<LineTable>(&comp_unit); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1044 | |
| 1045 | // This is basically a copy of the .debug$S subsections from all original COFF |
| 1046 | // object files merged together with address relocations applied. We are |
| 1047 | // looking for all DEBUG_S_LINES subsections. |
| 1048 | for (const DebugSubsectionRecord &dssr : |
| 1049 | cci->m_debug_stream.getSubsectionsArray()) { |
| 1050 | if (dssr.kind() != DebugSubsectionKind::Lines) |
| 1051 | continue; |
| 1052 | |
| 1053 | DebugLinesSubsectionRef lines; |
| 1054 | llvm::BinaryStreamReader reader(dssr.getRecordData()); |
| 1055 | if (auto EC = lines.initialize(reader)) { |
| 1056 | llvm::consumeError(std::move(EC)); |
| 1057 | return false; |
| 1058 | } |
| 1059 | |
| 1060 | const LineFragmentHeader *lfh = lines.header(); |
| 1061 | uint64_t virtual_addr = |
| 1062 | m_index->MakeVirtualAddress(lfh->RelocSegment, lfh->RelocOffset); |
| 1063 | |
| 1064 | const auto &checksums = cci->m_strings.checksums().getArray(); |
| 1065 | const auto &strings = cci->m_strings.strings(); |
| 1066 | for (const LineColumnEntry &group : lines) { |
| 1067 | // Indices in this structure are actually offsets of records in the |
| 1068 | // DEBUG_S_FILECHECKSUMS subsection. Those entries then have an index |
| 1069 | // into the global PDB string table. |
| 1070 | auto iter = checksums.at(group.NameIndex); |
| 1071 | if (iter == checksums.end()) |
| 1072 | continue; |
| 1073 | |
| 1074 | llvm::Expected<llvm::StringRef> efn = |
| 1075 | strings.getString(iter->FileNameOffset); |
| 1076 | if (!efn) { |
| 1077 | llvm::consumeError(efn.takeError()); |
| 1078 | continue; |
| 1079 | } |
| 1080 | |
| 1081 | // LLDB wants the index of the file in the list of support files. |
| 1082 | auto fn_iter = llvm::find(cci->m_file_list, *efn); |
| 1083 | lldbassert(fn_iter != cci->m_file_list.end()); |
Zachary Turner | b3130b4 | 2019-01-02 18:32:50 +0000 | [diff] [blame] | 1084 | // LLDB support file indices are 1-based. |
| 1085 | uint32_t file_index = |
| 1086 | 1 + std::distance(cci->m_file_list.begin(), fn_iter); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1087 | |
| 1088 | std::unique_ptr<LineSequence> sequence( |
| 1089 | line_table->CreateLineSequenceContainer()); |
| 1090 | lldbassert(!group.LineNumbers.empty()); |
| 1091 | |
| 1092 | for (const LineNumberEntry &entry : group.LineNumbers) { |
| 1093 | AppendLineEntryToSequence(*line_table, *sequence, *cci, virtual_addr, |
| 1094 | file_index, *lfh, entry); |
| 1095 | } |
| 1096 | LineInfo last_line(group.LineNumbers.back().Flags); |
| 1097 | TerminateLineSequence(*line_table, *lfh, virtual_addr, file_index, |
| 1098 | last_line.getEndLine(), std::move(sequence)); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | if (line_table->GetSize() == 0) |
| 1103 | return false; |
| 1104 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 1105 | comp_unit.SetLineTable(line_table.release()); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1106 | return true; |
| 1107 | } |
| 1108 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 1109 | bool SymbolFileNativePDB::ParseDebugMacros(CompileUnit &comp_unit) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1110 | // PDB doesn't contain information about macros |
| 1111 | return false; |
| 1112 | } |
| 1113 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 1114 | bool SymbolFileNativePDB::ParseSupportFiles(CompileUnit &comp_unit, |
| 1115 | FileSpecList &support_files) { |
| 1116 | PdbSymUid cu_id(comp_unit.GetID()); |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 1117 | lldbassert(cu_id.kind() == PdbSymUidKind::Compiland); |
| 1118 | CompilandIndexItem *cci = |
| 1119 | m_index->compilands().GetCompiland(cu_id.asCompiland().modi); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1120 | lldbassert(cci); |
| 1121 | |
| 1122 | for (llvm::StringRef f : cci->m_file_list) { |
| 1123 | FileSpec::Style style = |
| 1124 | f.startswith("/") ? FileSpec::Style::posix : FileSpec::Style::windows; |
Jonas Devlieghere | 8f3be7a | 2018-11-01 21:05:36 +0000 | [diff] [blame] | 1125 | FileSpec spec(f, style); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1126 | support_files.Append(spec); |
| 1127 | } |
| 1128 | |
Zachary Turner | b3130b4 | 2019-01-02 18:32:50 +0000 | [diff] [blame] | 1129 | llvm::SmallString<64> main_source_file = |
| 1130 | m_index->compilands().GetMainSourceFile(*cci); |
| 1131 | FileSpec::Style style = main_source_file.startswith("/") |
| 1132 | ? FileSpec::Style::posix |
| 1133 | : FileSpec::Style::windows; |
| 1134 | FileSpec spec(main_source_file, style); |
| 1135 | support_files.Insert(0, spec); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1136 | return true; |
| 1137 | } |
| 1138 | |
| 1139 | bool SymbolFileNativePDB::ParseImportedModules( |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1140 | const SymbolContext &sc, std::vector<ConstString> &imported_modules) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1141 | // PDB does not yet support module debug info |
| 1142 | return false; |
| 1143 | } |
| 1144 | |
Zachary Turner | ffc1b8f | 2019-01-14 22:40:41 +0000 | [diff] [blame^] | 1145 | size_t SymbolFileNativePDB::ParseBlocksRecursive(Function &func) { |
| 1146 | GetOrCreateBlock(PdbSymUid(func.GetID()).asCompilandSym()); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1147 | // FIXME: Parse child blocks |
| 1148 | return 1; |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1151 | void SymbolFileNativePDB::DumpClangAST(Stream &s) { m_ast->Dump(s); } |
Zachary Turner | 4911023 | 2018-11-05 17:40:28 +0000 | [diff] [blame] | 1152 | |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 1153 | uint32_t SymbolFileNativePDB::FindGlobalVariables( |
| 1154 | const ConstString &name, const CompilerDeclContext *parent_decl_ctx, |
| 1155 | uint32_t max_matches, VariableList &variables) { |
| 1156 | using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>; |
| 1157 | |
| 1158 | std::vector<SymbolAndOffset> results = m_index->globals().findRecordsByName( |
| 1159 | name.GetStringRef(), m_index->symrecords()); |
| 1160 | for (const SymbolAndOffset &result : results) { |
| 1161 | VariableSP var; |
| 1162 | switch (result.second.kind()) { |
| 1163 | case SymbolKind::S_GDATA32: |
| 1164 | case SymbolKind::S_LDATA32: |
| 1165 | case SymbolKind::S_GTHREAD32: |
Zachary Turner | 2af3416 | 2018-11-13 20:07:57 +0000 | [diff] [blame] | 1166 | case SymbolKind::S_LTHREAD32: |
| 1167 | case SymbolKind::S_CONSTANT: { |
Zachary Turner | 9fbf935 | 2018-11-16 03:16:27 +0000 | [diff] [blame] | 1168 | PdbGlobalSymId global(result.first, false); |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 1169 | var = GetOrCreateGlobalVariable(global); |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 1170 | variables.AddVariable(var); |
| 1171 | break; |
| 1172 | } |
| 1173 | default: |
| 1174 | continue; |
| 1175 | } |
| 1176 | } |
| 1177 | return variables.GetSize(); |
| 1178 | } |
| 1179 | |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1180 | uint32_t SymbolFileNativePDB::FindFunctions( |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1181 | const ConstString &name, const CompilerDeclContext *parent_decl_ctx, |
Zachary Turner | 117b1fa | 2018-10-25 20:45:40 +0000 | [diff] [blame] | 1182 | FunctionNameType name_type_mask, bool include_inlines, bool append, |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1183 | SymbolContextList &sc_list) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1184 | // For now we only support lookup by method name. |
| 1185 | if (!(name_type_mask & eFunctionNameTypeMethod)) |
| 1186 | return 0; |
| 1187 | |
| 1188 | using SymbolAndOffset = std::pair<uint32_t, llvm::codeview::CVSymbol>; |
| 1189 | |
| 1190 | std::vector<SymbolAndOffset> matches = m_index->globals().findRecordsByName( |
| 1191 | name.GetStringRef(), m_index->symrecords()); |
| 1192 | for (const SymbolAndOffset &match : matches) { |
| 1193 | if (match.second.kind() != S_PROCREF && match.second.kind() != S_LPROCREF) |
| 1194 | continue; |
| 1195 | ProcRefSym proc(match.second.kind()); |
| 1196 | cantFail(SymbolDeserializer::deserializeAs<ProcRefSym>(match.second, proc)); |
| 1197 | |
| 1198 | if (!IsValidRecord(proc)) |
| 1199 | continue; |
| 1200 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 1201 | CompilandIndexItem &cci = |
| 1202 | m_index->compilands().GetOrCreateCompiland(proc.modi()); |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1203 | SymbolContext sc; |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1204 | |
| 1205 | sc.comp_unit = GetOrCreateCompileUnit(cci).get(); |
Zachary Turner | 9fbf935 | 2018-11-16 03:16:27 +0000 | [diff] [blame] | 1206 | PdbCompilandSymId func_id(proc.modi(), proc.SymOffset); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1207 | sc.function = GetOrCreateFunction(func_id, *sc.comp_unit).get(); |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1208 | |
| 1209 | sc_list.Append(sc); |
| 1210 | } |
| 1211 | |
| 1212 | return sc_list.GetSize(); |
| 1213 | } |
| 1214 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1215 | uint32_t SymbolFileNativePDB::FindFunctions(const RegularExpression ®ex, |
| 1216 | bool include_inlines, bool append, |
| 1217 | SymbolContextList &sc_list) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1218 | return 0; |
| 1219 | } |
| 1220 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1221 | uint32_t SymbolFileNativePDB::FindTypes( |
| 1222 | const SymbolContext &sc, const ConstString &name, |
| 1223 | const CompilerDeclContext *parent_decl_ctx, bool append, |
| 1224 | uint32_t max_matches, llvm::DenseSet<SymbolFile *> &searched_symbol_files, |
| 1225 | TypeMap &types) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 1226 | if (!append) |
| 1227 | types.Clear(); |
| 1228 | if (!name) |
| 1229 | return 0; |
| 1230 | |
| 1231 | searched_symbol_files.clear(); |
| 1232 | searched_symbol_files.insert(this); |
| 1233 | |
| 1234 | // There is an assumption 'name' is not a regex |
| 1235 | size_t match_count = FindTypesByName(name.GetStringRef(), max_matches, types); |
| 1236 | |
| 1237 | return match_count; |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
| 1240 | size_t |
| 1241 | SymbolFileNativePDB::FindTypes(const std::vector<CompilerContext> &context, |
| 1242 | bool append, TypeMap &types) { |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 1246 | size_t SymbolFileNativePDB::FindTypesByName(llvm::StringRef name, |
| 1247 | uint32_t max_matches, |
| 1248 | TypeMap &types) { |
| 1249 | |
| 1250 | size_t match_count = 0; |
| 1251 | std::vector<TypeIndex> matches = m_index->tpi().findRecordsByName(name); |
| 1252 | if (max_matches > 0 && max_matches < matches.size()) |
| 1253 | matches.resize(max_matches); |
| 1254 | |
| 1255 | for (TypeIndex ti : matches) { |
| 1256 | TypeSP type = GetOrCreateType(ti); |
| 1257 | if (!type) |
| 1258 | continue; |
| 1259 | |
| 1260 | types.Insert(type); |
| 1261 | ++match_count; |
| 1262 | } |
| 1263 | return match_count; |
| 1264 | } |
| 1265 | |
Zachary Turner | 863f8c1 | 2019-01-11 18:03:20 +0000 | [diff] [blame] | 1266 | size_t SymbolFileNativePDB::ParseTypes(CompileUnit &comp_unit) { |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 1267 | // Only do the full type scan the first time. |
| 1268 | if (m_done_full_type_scan) |
| 1269 | return 0; |
| 1270 | |
| 1271 | size_t old_count = m_obj_file->GetModule()->GetTypeList()->GetSize(); |
| 1272 | LazyRandomTypeCollection &types = m_index->tpi().typeCollection(); |
| 1273 | |
| 1274 | // First process the entire TPI stream. |
| 1275 | for (auto ti = types.getFirst(); ti; ti = types.getNext(*ti)) { |
| 1276 | TypeSP type = GetOrCreateType(*ti); |
| 1277 | if (type) |
| 1278 | (void)type->GetFullCompilerType(); |
| 1279 | } |
| 1280 | |
| 1281 | // Next look for S_UDT records in the globals stream. |
| 1282 | for (const uint32_t gid : m_index->globals().getGlobalsTable()) { |
| 1283 | PdbGlobalSymId global{gid, false}; |
| 1284 | CVSymbol sym = m_index->ReadSymbolRecord(global); |
| 1285 | if (sym.kind() != S_UDT) |
| 1286 | continue; |
| 1287 | |
| 1288 | UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym)); |
| 1289 | bool is_typedef = true; |
| 1290 | if (IsTagRecord(PdbTypeSymId{udt.Type, false}, m_index->tpi())) { |
| 1291 | CVType cvt = m_index->tpi().getType(udt.Type); |
| 1292 | llvm::StringRef name = CVTagRecord::create(cvt).name(); |
| 1293 | if (name == udt.Name) |
| 1294 | is_typedef = false; |
| 1295 | } |
| 1296 | |
| 1297 | if (is_typedef) |
| 1298 | GetOrCreateTypedef(global); |
| 1299 | } |
| 1300 | |
| 1301 | size_t new_count = m_obj_file->GetModule()->GetTypeList()->GetSize(); |
| 1302 | |
| 1303 | m_done_full_type_scan = true; |
| 1304 | |
| 1305 | return new_count - old_count; |
| 1306 | } |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1307 | |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1308 | size_t |
| 1309 | SymbolFileNativePDB::ParseVariablesForCompileUnit(CompileUnit &comp_unit, |
| 1310 | VariableList &variables) { |
| 1311 | PdbSymUid sym_uid(comp_unit.GetID()); |
| 1312 | lldbassert(sym_uid.kind() == PdbSymUidKind::Compiland); |
| 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | VariableSP SymbolFileNativePDB::CreateLocalVariable(PdbCompilandSymId scope_id, |
| 1317 | PdbCompilandSymId var_id, |
| 1318 | bool is_param) { |
| 1319 | ModuleSP module = GetObjectFile()->GetModule(); |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1320 | VariableInfo var_info = GetVariableLocationInfo(*m_index, var_id, module); |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 1321 | if (!var_info.location || !var_info.ranges) |
| 1322 | return nullptr; |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1323 | |
| 1324 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(var_id.modi); |
| 1325 | CompUnitSP comp_unit_sp = GetOrCreateCompileUnit(*cii); |
| 1326 | TypeSP type_sp = GetOrCreateType(var_info.type); |
| 1327 | std::string name = var_info.name.str(); |
| 1328 | Declaration decl; |
| 1329 | SymbolFileTypeSP sftype = |
| 1330 | std::make_shared<SymbolFileType>(*this, type_sp->GetID()); |
| 1331 | |
| 1332 | ValueType var_scope = |
| 1333 | is_param ? eValueTypeVariableArgument : eValueTypeVariableLocal; |
| 1334 | VariableSP var_sp = std::make_shared<Variable>( |
| 1335 | toOpaqueUid(var_id), name.c_str(), name.c_str(), sftype, var_scope, |
| 1336 | comp_unit_sp.get(), *var_info.ranges, &decl, *var_info.location, false, |
| 1337 | false, false); |
| 1338 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1339 | if (!is_param) |
Zachary Turner | 2256633 | 2019-01-02 18:33:54 +0000 | [diff] [blame] | 1340 | m_ast->GetOrCreateVariableDecl(scope_id, var_id); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1341 | |
| 1342 | m_local_variables[toOpaqueUid(var_id)] = var_sp; |
| 1343 | return var_sp; |
| 1344 | } |
| 1345 | |
| 1346 | VariableSP SymbolFileNativePDB::GetOrCreateLocalVariable( |
| 1347 | PdbCompilandSymId scope_id, PdbCompilandSymId var_id, bool is_param) { |
| 1348 | auto iter = m_local_variables.find(toOpaqueUid(var_id)); |
| 1349 | if (iter != m_local_variables.end()) |
| 1350 | return iter->second; |
| 1351 | |
| 1352 | return CreateLocalVariable(scope_id, var_id, is_param); |
| 1353 | } |
| 1354 | |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 1355 | TypeSP SymbolFileNativePDB::CreateTypedef(PdbGlobalSymId id) { |
| 1356 | CVSymbol sym = m_index->ReadSymbolRecord(id); |
| 1357 | lldbassert(sym.kind() == SymbolKind::S_UDT); |
| 1358 | |
| 1359 | UDTSym udt = llvm::cantFail(SymbolDeserializer::deserializeAs<UDTSym>(sym)); |
| 1360 | |
| 1361 | TypeSP target_type = GetOrCreateType(udt.Type); |
| 1362 | |
| 1363 | (void)m_ast->GetOrCreateTypedefDecl(id); |
| 1364 | |
| 1365 | Declaration decl; |
| 1366 | return std::make_shared<lldb_private::Type>( |
| 1367 | toOpaqueUid(id), this, ConstString(udt.Name), target_type->GetByteSize(), |
| 1368 | nullptr, target_type->GetID(), lldb_private::Type::eEncodingIsTypedefUID, |
| 1369 | decl, target_type->GetForwardCompilerType(), |
| 1370 | lldb_private::Type::eResolveStateForward); |
| 1371 | } |
| 1372 | |
| 1373 | TypeSP SymbolFileNativePDB::GetOrCreateTypedef(PdbGlobalSymId id) { |
| 1374 | auto iter = m_types.find(toOpaqueUid(id)); |
| 1375 | if (iter != m_types.end()) |
| 1376 | return iter->second; |
| 1377 | |
| 1378 | return CreateTypedef(id); |
| 1379 | } |
| 1380 | |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1381 | size_t SymbolFileNativePDB::ParseVariablesForBlock(PdbCompilandSymId block_id) { |
| 1382 | Block &block = GetOrCreateBlock(block_id); |
| 1383 | |
| 1384 | size_t count = 0; |
| 1385 | |
| 1386 | CompilandIndexItem *cii = m_index->compilands().GetCompiland(block_id.modi); |
| 1387 | CVSymbol sym = cii->m_debug_stream.readSymbolAtOffset(block_id.offset); |
| 1388 | uint32_t params_remaining = 0; |
| 1389 | switch (sym.kind()) { |
| 1390 | case S_GPROC32: |
| 1391 | case S_LPROC32: { |
| 1392 | ProcSym proc(static_cast<SymbolRecordKind>(sym.kind())); |
| 1393 | cantFail(SymbolDeserializer::deserializeAs<ProcSym>(sym, proc)); |
| 1394 | CVType signature = m_index->tpi().getType(proc.FunctionType); |
| 1395 | ProcedureRecord sig; |
| 1396 | cantFail(TypeDeserializer::deserializeAs<ProcedureRecord>(signature, sig)); |
| 1397 | params_remaining = sig.getParameterCount(); |
| 1398 | break; |
| 1399 | } |
| 1400 | case S_BLOCK32: |
| 1401 | break; |
| 1402 | default: |
| 1403 | lldbassert(false && "Symbol is not a block!"); |
| 1404 | return 0; |
| 1405 | } |
| 1406 | |
| 1407 | VariableListSP variables = block.GetBlockVariableList(false); |
| 1408 | if (!variables) { |
| 1409 | variables = std::make_shared<VariableList>(); |
| 1410 | block.SetVariableList(variables); |
| 1411 | } |
| 1412 | |
| 1413 | CVSymbolArray syms = limitSymbolArrayToScope( |
| 1414 | cii->m_debug_stream.getSymbolArray(), block_id.offset); |
| 1415 | |
| 1416 | // Skip the first record since it's a PROC32 or BLOCK32, and there's |
| 1417 | // no point examining it since we know it's not a local variable. |
| 1418 | syms.drop_front(); |
| 1419 | auto iter = syms.begin(); |
| 1420 | auto end = syms.end(); |
| 1421 | |
| 1422 | while (iter != end) { |
| 1423 | uint32_t record_offset = iter.offset(); |
| 1424 | CVSymbol variable_cvs = *iter; |
| 1425 | PdbCompilandSymId child_sym_id(block_id.modi, record_offset); |
| 1426 | ++iter; |
| 1427 | |
| 1428 | // If this is a block, recurse into its children and then skip it. |
| 1429 | if (variable_cvs.kind() == S_BLOCK32) { |
| 1430 | uint32_t block_end = getScopeEndOffset(variable_cvs); |
| 1431 | count += ParseVariablesForBlock(child_sym_id); |
| 1432 | iter = syms.at(block_end); |
| 1433 | continue; |
| 1434 | } |
| 1435 | |
| 1436 | bool is_param = params_remaining > 0; |
| 1437 | VariableSP variable; |
| 1438 | switch (variable_cvs.kind()) { |
| 1439 | case S_REGREL32: |
| 1440 | case S_REGISTER: |
| 1441 | case S_LOCAL: |
| 1442 | variable = GetOrCreateLocalVariable(block_id, child_sym_id, is_param); |
| 1443 | if (is_param) |
| 1444 | --params_remaining; |
Zachary Turner | 44f1951 | 2019-01-10 20:57:32 +0000 | [diff] [blame] | 1445 | if (variable) |
| 1446 | variables->AddVariableIfUnique(variable); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1447 | break; |
| 1448 | default: |
| 1449 | break; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | // Pass false for set_children, since we call this recursively so that the |
| 1454 | // children will call this for themselves. |
| 1455 | block.SetDidParseVariables(true, false); |
| 1456 | |
| 1457 | return count; |
| 1458 | } |
| 1459 | |
| 1460 | size_t SymbolFileNativePDB::ParseVariablesForContext(const SymbolContext &sc) { |
| 1461 | lldbassert(sc.function || sc.comp_unit); |
| 1462 | |
| 1463 | VariableListSP variables; |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1464 | if (sc.block) { |
| 1465 | PdbSymUid block_id(sc.block->GetID()); |
| 1466 | |
| 1467 | size_t count = ParseVariablesForBlock(block_id.asCompilandSym()); |
| 1468 | return count; |
| 1469 | } |
| 1470 | |
| 1471 | if (sc.function) { |
| 1472 | PdbSymUid block_id(sc.function->GetID()); |
| 1473 | |
| 1474 | size_t count = ParseVariablesForBlock(block_id.asCompilandSym()); |
| 1475 | return count; |
| 1476 | } |
| 1477 | |
| 1478 | if (sc.comp_unit) { |
| 1479 | variables = sc.comp_unit->GetVariableList(false); |
| 1480 | if (!variables) { |
| 1481 | variables = std::make_shared<VariableList>(); |
| 1482 | sc.comp_unit->SetVariableList(variables); |
| 1483 | } |
| 1484 | return ParseVariablesForCompileUnit(*sc.comp_unit, *variables); |
| 1485 | } |
| 1486 | |
| 1487 | llvm_unreachable("Unreachable!"); |
| 1488 | } |
| 1489 | |
| 1490 | CompilerDecl SymbolFileNativePDB::GetDeclForUID(lldb::user_id_t uid) { |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1491 | clang::Decl *decl = m_ast->GetOrCreateDeclForUid(PdbSymUid(uid)); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1492 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1493 | return m_ast->ToCompilerDecl(*decl); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
| 1496 | CompilerDeclContext |
| 1497 | SymbolFileNativePDB::GetDeclContextForUID(lldb::user_id_t uid) { |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1498 | clang::DeclContext *context = |
| 1499 | m_ast->GetOrCreateDeclContextForUid(PdbSymUid(uid)); |
| 1500 | if (!context) |
| 1501 | return {}; |
| 1502 | |
| 1503 | return m_ast->ToCompilerDeclContext(*context); |
Zachary Turner | d3d2b9b | 2018-12-13 18:17:51 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 1506 | CompilerDeclContext |
| 1507 | SymbolFileNativePDB::GetDeclContextContainingUID(lldb::user_id_t uid) { |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1508 | clang::DeclContext *context = m_ast->GetParentDeclContext(PdbSymUid(uid)); |
| 1509 | return m_ast->ToCompilerDeclContext(*context); |
Zachary Turner | a42bbe3 | 2018-12-07 19:34:02 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1512 | Type *SymbolFileNativePDB::ResolveTypeUID(lldb::user_id_t type_uid) { |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 1513 | auto iter = m_types.find(type_uid); |
| 1514 | // lldb should not be passing us non-sensical type uids. the only way it |
| 1515 | // could have a type uid in the first place is if we handed it out, in which |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 1516 | // case we should know about the type. However, that doesn't mean we've |
| 1517 | // instantiated it yet. We can vend out a UID for a future type. So if the |
| 1518 | // type doesn't exist, let's instantiate it now. |
| 1519 | if (iter != m_types.end()) |
| 1520 | return &*iter->second; |
| 1521 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 1522 | PdbSymUid uid(type_uid); |
| 1523 | lldbassert(uid.kind() == PdbSymUidKind::Type); |
| 1524 | PdbTypeSymId type_id = uid.asTypeSym(); |
| 1525 | if (type_id.index.isNoneType()) |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 1526 | return nullptr; |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 1527 | |
Zachary Turner | 6284aee | 2018-11-16 02:42:32 +0000 | [diff] [blame] | 1528 | TypeSP type_sp = CreateAndCacheType(type_id); |
Zachary Turner | 9f72795 | 2018-10-26 09:06:38 +0000 | [diff] [blame] | 1529 | return &*type_sp; |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Adrian Prantl | eca07c5 | 2018-11-05 20:49:07 +0000 | [diff] [blame] | 1532 | llvm::Optional<SymbolFile::ArrayInfo> |
| 1533 | SymbolFileNativePDB::GetDynamicArrayInfoForUID( |
| 1534 | lldb::user_id_t type_uid, const lldb_private::ExecutionContext *exe_ctx) { |
| 1535 | return llvm::None; |
| 1536 | } |
| 1537 | |
| 1538 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1539 | bool SymbolFileNativePDB::CompleteType(CompilerType &compiler_type) { |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1540 | clang::QualType qt = |
| 1541 | clang::QualType::getFromOpaquePtr(compiler_type.GetOpaqueQualType()); |
Zachary Turner | 2f7efbc | 2018-10-23 16:37:53 +0000 | [diff] [blame] | 1542 | |
Zachary Turner | 594c85e | 2018-12-17 19:43:33 +0000 | [diff] [blame] | 1543 | return m_ast->CompleteType(qt); |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | size_t SymbolFileNativePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, |
Zachary Turner | 117b1fa | 2018-10-25 20:45:40 +0000 | [diff] [blame] | 1547 | TypeClass type_mask, |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1548 | lldb_private::TypeList &type_list) { |
| 1549 | return 0; |
| 1550 | } |
| 1551 | |
| 1552 | CompilerDeclContext |
| 1553 | SymbolFileNativePDB::FindNamespace(const SymbolContext &sc, |
| 1554 | const ConstString &name, |
| 1555 | const CompilerDeclContext *parent_decl_ctx) { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1556 | return {}; |
| 1557 | } |
| 1558 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1559 | TypeSystem * |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1560 | SymbolFileNativePDB::GetTypeSystemForLanguage(lldb::LanguageType language) { |
| 1561 | auto type_system = |
| 1562 | m_obj_file->GetModule()->GetTypeSystemForLanguage(language); |
| 1563 | if (type_system) |
| 1564 | type_system->SetSymbolFile(this); |
| 1565 | return type_system; |
| 1566 | } |
| 1567 | |
Zachary Turner | b96181c | 2018-10-22 16:19:07 +0000 | [diff] [blame] | 1568 | ConstString SymbolFileNativePDB::GetPluginName() { |
Zachary Turner | 307f5ae | 2018-10-12 19:47:13 +0000 | [diff] [blame] | 1569 | static ConstString g_name("pdb"); |
| 1570 | return g_name; |
| 1571 | } |
| 1572 | |
| 1573 | uint32_t SymbolFileNativePDB::GetPluginVersion() { return 1; } |