Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 1 | //===- NativeSession.cpp - Native implementation of IPDBSession -*- 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 "llvm/DebugInfo/PDB/Native/NativeSession.h" |
| 11 | |
| 12 | #include "llvm/ADT/STLExtras.h" |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 13 | #include "llvm/DebugInfo/CodeView/TypeIndex.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 14 | #include "llvm/DebugInfo/PDB/GenericError.h" |
| 15 | #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" |
| 16 | #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 17 | #include "llvm/DebugInfo/PDB/Native/NativeBuiltinSymbol.h" |
Adrian McCarthy | bf0afc3 | 2017-06-28 22:47:40 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/PDB/Native/NativeCompilandSymbol.h" |
Adrian McCarthy | b41f03e | 2017-08-04 22:37:58 +0000 | [diff] [blame] | 19 | #include "llvm/DebugInfo/PDB/Native/NativeEnumSymbol.h" |
| 20 | #include "llvm/DebugInfo/PDB/Native/NativeEnumTypes.h" |
Adrian McCarthy | 4d93d66 | 2017-03-29 19:27:08 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/PDB/Native/NativeExeSymbol.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 22 | #include "llvm/DebugInfo/PDB/Native/PDBFile.h" |
| 23 | #include "llvm/DebugInfo/PDB/Native/RawError.h" |
Adrian McCarthy | b41f03e | 2017-08-04 22:37:58 +0000 | [diff] [blame] | 24 | #include "llvm/DebugInfo/PDB/Native/TpiStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 25 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 26 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
Adrian McCarthy | b41f03e | 2017-08-04 22:37:58 +0000 | [diff] [blame] | 27 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 28 | #include "llvm/Support/Allocator.h" |
Zachary Turner | d9dc282 | 2017-03-02 20:52:51 +0000 | [diff] [blame] | 29 | #include "llvm/Support/BinaryByteStream.h" |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Error.h" |
| 31 | #include "llvm/Support/ErrorOr.h" |
| 32 | #include "llvm/Support/MemoryBuffer.h" |
Adrian McCarthy | bf0afc3 | 2017-06-28 22:47:40 +0000 | [diff] [blame] | 33 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 34 | #include <algorithm> |
Adrian McCarthy | b41f03e | 2017-08-04 22:37:58 +0000 | [diff] [blame] | 35 | #include <cassert> |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 36 | #include <memory> |
Adrian McCarthy | bf0afc3 | 2017-06-28 22:47:40 +0000 | [diff] [blame] | 37 | #include <utility> |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 38 | |
| 39 | using namespace llvm; |
| 40 | using namespace llvm::msf; |
| 41 | using namespace llvm::pdb; |
| 42 | |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 43 | namespace { |
| 44 | // Maps codeview::SimpleTypeKind of a built-in type to the parameters necessary |
| 45 | // to instantiate a NativeBuiltinSymbol for that type. |
Reid Kleckner | 0962cb2 | 2017-07-12 19:46:35 +0000 | [diff] [blame] | 46 | static const struct BuiltinTypeEntry { |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 47 | codeview::SimpleTypeKind Kind; |
| 48 | PDB_BuiltinType Type; |
| 49 | uint32_t Size; |
| 50 | } BuiltinTypes[] = { |
| 51 | {codeview::SimpleTypeKind::Int32, PDB_BuiltinType::Int, 4}, |
| 52 | {codeview::SimpleTypeKind::UInt32, PDB_BuiltinType::UInt, 4}, |
| 53 | {codeview::SimpleTypeKind::UInt32Long, PDB_BuiltinType::UInt, 4}, |
| 54 | {codeview::SimpleTypeKind::UInt64Quad, PDB_BuiltinType::UInt, 8}, |
| 55 | {codeview::SimpleTypeKind::NarrowCharacter, PDB_BuiltinType::Char, 1}, |
| 56 | {codeview::SimpleTypeKind::SignedCharacter, PDB_BuiltinType::Char, 1}, |
| 57 | {codeview::SimpleTypeKind::UnsignedCharacter, PDB_BuiltinType::UInt, 1}, |
| 58 | {codeview::SimpleTypeKind::UInt16Short, PDB_BuiltinType::UInt, 2}, |
| 59 | {codeview::SimpleTypeKind::Boolean8, PDB_BuiltinType::Bool, 1} |
| 60 | // This table can be grown as necessary, but these are the only types we've |
| 61 | // needed so far. |
| 62 | }; |
| 63 | } // namespace |
| 64 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 65 | NativeSession::NativeSession(std::unique_ptr<PDBFile> PdbFile, |
| 66 | std::unique_ptr<BumpPtrAllocator> Allocator) |
| 67 | : Pdb(std::move(PdbFile)), Allocator(std::move(Allocator)) {} |
| 68 | |
| 69 | NativeSession::~NativeSession() = default; |
| 70 | |
Peter Collingbourne | 75257bc | 2017-10-20 19:48:26 +0000 | [diff] [blame] | 71 | Error NativeSession::createFromPdb(std::unique_ptr<MemoryBuffer> Buffer, |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 72 | std::unique_ptr<IPDBSession> &Session) { |
Peter Collingbourne | 75257bc | 2017-10-20 19:48:26 +0000 | [diff] [blame] | 73 | StringRef Path = Buffer->getBufferIdentifier(); |
Zachary Turner | 695ed56 | 2017-02-28 00:04:07 +0000 | [diff] [blame] | 74 | auto Stream = llvm::make_unique<MemoryBufferByteStream>( |
| 75 | std::move(Buffer), llvm::support::little); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 76 | |
| 77 | auto Allocator = llvm::make_unique<BumpPtrAllocator>(); |
Zachary Turner | 7b327d0 | 2017-02-16 23:35:45 +0000 | [diff] [blame] | 78 | auto File = llvm::make_unique<PDBFile>(Path, std::move(Stream), *Allocator); |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 79 | if (auto EC = File->parseFileHeaders()) |
| 80 | return EC; |
| 81 | if (auto EC = File->parseStreamData()) |
| 82 | return EC; |
| 83 | |
| 84 | Session = |
| 85 | llvm::make_unique<NativeSession>(std::move(File), std::move(Allocator)); |
| 86 | |
| 87 | return Error::success(); |
| 88 | } |
| 89 | |
| 90 | Error NativeSession::createFromExe(StringRef Path, |
| 91 | std::unique_ptr<IPDBSession> &Session) { |
| 92 | return make_error<RawError>(raw_error_code::feature_unsupported); |
| 93 | } |
| 94 | |
Adrian McCarthy | bf0afc3 | 2017-06-28 22:47:40 +0000 | [diff] [blame] | 95 | std::unique_ptr<PDBSymbolCompiland> |
| 96 | NativeSession::createCompilandSymbol(DbiModuleDescriptor MI) { |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 97 | const auto Id = static_cast<SymIndexId>(SymbolCache.size()); |
Adrian McCarthy | bf0afc3 | 2017-06-28 22:47:40 +0000 | [diff] [blame] | 98 | SymbolCache.push_back( |
| 99 | llvm::make_unique<NativeCompilandSymbol>(*this, Id, MI)); |
| 100 | return llvm::make_unique<PDBSymbolCompiland>( |
| 101 | *this, std::unique_ptr<IPDBRawSymbol>(SymbolCache[Id]->clone())); |
| 102 | } |
| 103 | |
Adrian McCarthy | b41f03e | 2017-08-04 22:37:58 +0000 | [diff] [blame] | 104 | std::unique_ptr<PDBSymbolTypeEnum> |
| 105 | NativeSession::createEnumSymbol(codeview::TypeIndex Index) { |
| 106 | const auto Id = findSymbolByTypeIndex(Index); |
| 107 | return llvm::make_unique<PDBSymbolTypeEnum>( |
| 108 | *this, std::unique_ptr<IPDBRawSymbol>(SymbolCache[Id]->clone())); |
| 109 | } |
| 110 | |
| 111 | std::unique_ptr<IPDBEnumSymbols> |
| 112 | NativeSession::createTypeEnumerator(codeview::TypeLeafKind Kind) { |
| 113 | auto Tpi = Pdb->getPDBTpiStream(); |
| 114 | if (!Tpi) { |
| 115 | consumeError(Tpi.takeError()); |
| 116 | return nullptr; |
| 117 | } |
| 118 | auto &Types = Tpi->typeCollection(); |
| 119 | return std::unique_ptr<IPDBEnumSymbols>( |
| 120 | new NativeEnumTypes(*this, Types, codeview::LF_ENUM)); |
| 121 | } |
| 122 | |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 123 | SymIndexId NativeSession::findSymbolByTypeIndex(codeview::TypeIndex Index) { |
| 124 | // First see if it's already in our cache. |
| 125 | const auto Entry = TypeIndexToSymbolId.find(Index); |
| 126 | if (Entry != TypeIndexToSymbolId.end()) |
| 127 | return Entry->second; |
| 128 | |
| 129 | // Symbols for built-in types are created on the fly. |
| 130 | if (Index.isSimple()) { |
| 131 | // FIXME: We will eventually need to handle pointers to other simple types, |
| 132 | // which are still simple types in the world of CodeView TypeIndexes. |
| 133 | if (Index.getSimpleMode() != codeview::SimpleTypeMode::Direct) |
| 134 | return 0; |
| 135 | const auto Kind = Index.getSimpleKind(); |
Reid Kleckner | 0962cb2 | 2017-07-12 19:46:35 +0000 | [diff] [blame] | 136 | const auto It = |
| 137 | std::find_if(std::begin(BuiltinTypes), std::end(BuiltinTypes), |
| 138 | [Kind](const BuiltinTypeEntry &Builtin) { |
| 139 | return Builtin.Kind == Kind; |
| 140 | }); |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 141 | if (It == std::end(BuiltinTypes)) |
| 142 | return 0; |
| 143 | SymIndexId Id = SymbolCache.size(); |
| 144 | SymbolCache.emplace_back( |
Reid Kleckner | 0962cb2 | 2017-07-12 19:46:35 +0000 | [diff] [blame] | 145 | llvm::make_unique<NativeBuiltinSymbol>(*this, Id, It->Type, It->Size)); |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 146 | TypeIndexToSymbolId[Index] = Id; |
| 147 | return Id; |
| 148 | } |
| 149 | |
Adrian McCarthy | b41f03e | 2017-08-04 22:37:58 +0000 | [diff] [blame] | 150 | // We need to instantiate and cache the desired type symbol. |
| 151 | auto Tpi = Pdb->getPDBTpiStream(); |
| 152 | if (!Tpi) { |
| 153 | consumeError(Tpi.takeError()); |
| 154 | return 0; |
| 155 | } |
| 156 | auto &Types = Tpi->typeCollection(); |
| 157 | const auto &I = Types.getType(Index); |
| 158 | const auto Id = static_cast<SymIndexId>(SymbolCache.size()); |
| 159 | // TODO(amccarth): Make this handle all types, not just LF_ENUMs. |
| 160 | assert(I.kind() == codeview::LF_ENUM); |
| 161 | SymbolCache.emplace_back(llvm::make_unique<NativeEnumSymbol>(*this, Id, I)); |
| 162 | TypeIndexToSymbolId[Index] = Id; |
| 163 | return Id; |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 166 | uint64_t NativeSession::getLoadAddress() const { return 0; } |
| 167 | |
Aaron Smith | 89a19ac | 2018-02-23 00:02:27 +0000 | [diff] [blame] | 168 | bool NativeSession::setLoadAddress(uint64_t Address) { return false; } |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 169 | |
Adrian McCarthy | 6a4b080 | 2017-06-22 18:42:23 +0000 | [diff] [blame] | 170 | std::unique_ptr<PDBSymbolExe> NativeSession::getGlobalScope() { |
Adrian McCarthy | 8d090fc | 2017-07-12 19:38:11 +0000 | [diff] [blame] | 171 | const auto Id = static_cast<SymIndexId>(SymbolCache.size()); |
Adrian McCarthy | bf0afc3 | 2017-06-28 22:47:40 +0000 | [diff] [blame] | 172 | SymbolCache.push_back(llvm::make_unique<NativeExeSymbol>(*this, Id)); |
| 173 | auto RawSymbol = SymbolCache[Id]->clone(); |
Adrian McCarthy | 649b8e0 | 2017-02-24 00:10:47 +0000 | [diff] [blame] | 174 | auto PdbSymbol(PDBSymbol::create(*this, std::move(RawSymbol))); |
| 175 | std::unique_ptr<PDBSymbolExe> ExeSymbol( |
Adrian McCarthy | 6a4b080 | 2017-06-22 18:42:23 +0000 | [diff] [blame] | 176 | static_cast<PDBSymbolExe *>(PdbSymbol.release())); |
Adrian McCarthy | 649b8e0 | 2017-02-24 00:10:47 +0000 | [diff] [blame] | 177 | return ExeSymbol; |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | std::unique_ptr<PDBSymbol> |
| 181 | NativeSession::getSymbolById(uint32_t SymbolId) const { |
Adrian McCarthy | bf0afc3 | 2017-06-28 22:47:40 +0000 | [diff] [blame] | 182 | // If the caller has a SymbolId, it'd better be in our SymbolCache. |
| 183 | return SymbolId < SymbolCache.size() |
| 184 | ? PDBSymbol::create(*this, SymbolCache[SymbolId]->clone()) |
| 185 | : nullptr; |
Adrian McCarthy | 6b6b8c4 | 2017-01-25 22:38:55 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | std::unique_ptr<PDBSymbol> |
| 189 | NativeSession::findSymbolByAddress(uint64_t Address, PDB_SymType Type) const { |
| 190 | return nullptr; |
| 191 | } |
| 192 | |
| 193 | std::unique_ptr<IPDBEnumLineNumbers> |
| 194 | NativeSession::findLineNumbers(const PDBSymbolCompiland &Compiland, |
| 195 | const IPDBSourceFile &File) const { |
| 196 | return nullptr; |
| 197 | } |
| 198 | |
| 199 | std::unique_ptr<IPDBEnumLineNumbers> |
| 200 | NativeSession::findLineNumbersByAddress(uint64_t Address, |
| 201 | uint32_t Length) const { |
| 202 | return nullptr; |
| 203 | } |
| 204 | |
| 205 | std::unique_ptr<IPDBEnumSourceFiles> |
| 206 | NativeSession::findSourceFiles(const PDBSymbolCompiland *Compiland, |
| 207 | StringRef Pattern, |
| 208 | PDB_NameSearchFlags Flags) const { |
| 209 | return nullptr; |
| 210 | } |
| 211 | |
| 212 | std::unique_ptr<IPDBSourceFile> |
| 213 | NativeSession::findOneSourceFile(const PDBSymbolCompiland *Compiland, |
| 214 | StringRef Pattern, |
| 215 | PDB_NameSearchFlags Flags) const { |
| 216 | return nullptr; |
| 217 | } |
| 218 | |
| 219 | std::unique_ptr<IPDBEnumChildren<PDBSymbolCompiland>> |
| 220 | NativeSession::findCompilandsForSourceFile(StringRef Pattern, |
| 221 | PDB_NameSearchFlags Flags) const { |
| 222 | return nullptr; |
| 223 | } |
| 224 | |
| 225 | std::unique_ptr<PDBSymbolCompiland> |
| 226 | NativeSession::findOneCompilandForSourceFile(StringRef Pattern, |
| 227 | PDB_NameSearchFlags Flags) const { |
| 228 | return nullptr; |
| 229 | } |
| 230 | |
| 231 | std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getAllSourceFiles() const { |
| 232 | return nullptr; |
| 233 | } |
| 234 | |
| 235 | std::unique_ptr<IPDBEnumSourceFiles> NativeSession::getSourceFilesForCompiland( |
| 236 | const PDBSymbolCompiland &Compiland) const { |
| 237 | return nullptr; |
| 238 | } |
| 239 | |
| 240 | std::unique_ptr<IPDBSourceFile> |
| 241 | NativeSession::getSourceFileById(uint32_t FileId) const { |
| 242 | return nullptr; |
| 243 | } |
| 244 | |
| 245 | std::unique_ptr<IPDBEnumDataStreams> NativeSession::getDebugStreams() const { |
| 246 | return nullptr; |
| 247 | } |
Aaron Smith | 89bca9e | 2017-11-16 14:33:09 +0000 | [diff] [blame] | 248 | |
| 249 | std::unique_ptr<IPDBEnumTables> NativeSession::getEnumTables() const { |
| 250 | return nullptr; |
| 251 | } |
Zachary Turner | 679aead | 2018-03-13 17:46:06 +0000 | [diff] [blame] | 252 | |
| 253 | std::unique_ptr<IPDBEnumInjectedSources> |
| 254 | NativeSession::getInjectedSources() const { |
| 255 | return nullptr; |
| 256 | } |