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