LLDB mainline
ObjectFilePDB.cpp
Go to the documentation of this file.
1//===-- ObjectFilePDB.cpp -------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "ObjectFilePDB.h"
10#include "lldb/Core/Module.h"
13#include "lldb/Core/Section.h"
15#include "llvm/BinaryFormat/Magic.h"
16#include "llvm/DebugInfo/PDB/Native/DbiStream.h"
17#include "llvm/DebugInfo/PDB/Native/InfoStream.h"
18#include "llvm/DebugInfo/PDB/Native/NativeSession.h"
19#include "llvm/DebugInfo/PDB/Native/PDBFile.h"
20#include "llvm/DebugInfo/PDB/PDB.h"
21#include "llvm/Support/BinaryByteStream.h"
22
23using namespace lldb;
24using namespace lldb_private;
25using namespace llvm::pdb;
26using namespace llvm::codeview;
27
29
30static UUID GetPDBUUID(InfoStream &IS) {
31 UUID::CvRecordPdb70 debug_info;
32 memcpy(&debug_info.Uuid, IS.getGuid().Guid, sizeof(debug_info.Uuid));
33 debug_info.Age = IS.getAge();
34 return UUID(debug_info);
35}
36
38
43}
44
47}
48
50 auto dbi_stream = m_file_up->getPDBDbiStream();
51 if (!dbi_stream) {
52 llvm::consumeError(dbi_stream.takeError());
53 return ArchSpec();
54 }
55
56 PDB_Machine machine = dbi_stream->getMachineType();
57 switch (machine) {
58 default:
59 break;
60 case PDB_Machine::Amd64:
61 case PDB_Machine::x86:
62 case PDB_Machine::PowerPC:
63 case PDB_Machine::PowerPCFP:
64 case PDB_Machine::Arm:
65 case PDB_Machine::ArmNT:
66 case PDB_Machine::Thumb:
67 case PDB_Machine::Arm64:
68 ArchSpec arch;
69 arch.SetArchitecture(eArchTypeCOFF, static_cast<int>(machine),
71 return arch;
72 }
73 return ArchSpec();
74}
75
78 if (!m_file_up)
79 return false;
80 auto info_stream = m_file_up->getPDBInfoStream();
81 if (!info_stream) {
82 llvm::consumeError(info_stream.takeError());
83 return false;
84 }
85 m_uuid = GetPDBUUID(*info_stream);
86 return true;
87}
88
90ObjectFilePDB::CreateInstance(const ModuleSP &module_sp, DataBufferSP data_sp,
91 offset_t data_offset, const FileSpec *file,
92 offset_t file_offset, offset_t length) {
93 auto objfile_up = std::make_unique<ObjectFilePDB>(
94 module_sp, data_sp, data_offset, file, file_offset, length);
95 if (!objfile_up->initPDBFile())
96 return nullptr;
97 return objfile_up.release();
98}
99
101 WritableDataBufferSP data_sp,
102 const ProcessSP &process_sp,
103 addr_t header_addr) {
104 return nullptr;
105}
106
108 const FileSpec &file, DataBufferSP &data_sp, offset_t data_offset,
109 offset_t file_offset, offset_t length, ModuleSpecList &specs) {
110 const size_t initial_count = specs.GetSize();
111 ModuleSpec module_spec(file);
112 llvm::BumpPtrAllocator allocator;
113 std::unique_ptr<PDBFile> pdb_file = loadPDBFile(file.GetPath(), allocator);
114 if (!pdb_file)
115 return initial_count;
116
117 auto info_stream = pdb_file->getPDBInfoStream();
118 if (!info_stream) {
119 llvm::consumeError(info_stream.takeError());
120 return initial_count;
121 }
122 auto dbi_stream = pdb_file->getPDBDbiStream();
123 if (!dbi_stream) {
124 llvm::consumeError(dbi_stream.takeError());
125 return initial_count;
126 }
127
128 lldb_private::UUID &uuid = module_spec.GetUUID();
129 uuid = GetPDBUUID(*info_stream);
130
131 ArchSpec &module_arch = module_spec.GetArchitecture();
132 switch (dbi_stream->getMachineType()) {
133 case PDB_Machine::Amd64:
134 module_arch.SetTriple("x86_64-pc-windows");
135 specs.Append(module_spec);
136 break;
137 case PDB_Machine::x86:
138 module_arch.SetTriple("i386-pc-windows");
139 specs.Append(module_spec);
140 break;
141 case PDB_Machine::ArmNT:
142 module_arch.SetTriple("armv7-pc-windows");
143 specs.Append(module_spec);
144 break;
145 case PDB_Machine::Arm64:
146 module_arch.SetTriple("aarch64-pc-windows");
147 specs.Append(module_spec);
148 break;
149 default:
150 break;
151 }
152
153 return specs.GetSize() - initial_count;
154}
155
156ObjectFilePDB::ObjectFilePDB(const ModuleSP &module_sp, DataBufferSP &data_sp,
157 offset_t data_offset, const FileSpec *file,
158 offset_t offset, offset_t length)
159 : ObjectFile(module_sp, file, offset, length, data_sp, data_offset) {}
160
161std::unique_ptr<PDBFile>
162ObjectFilePDB::loadPDBFile(std::string PdbPath,
163 llvm::BumpPtrAllocator &Allocator) {
164 llvm::file_magic magic;
165 auto ec = llvm::identify_magic(PdbPath, magic);
166 if (ec || magic != llvm::file_magic::pdb)
167 return nullptr;
168 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> ErrorOrBuffer =
169 llvm::MemoryBuffer::getFile(PdbPath, /*IsText=*/false,
170 /*RequiresNullTerminator=*/false);
171 if (!ErrorOrBuffer)
172 return nullptr;
173 std::unique_ptr<llvm::MemoryBuffer> Buffer = std::move(*ErrorOrBuffer);
174
175 llvm::StringRef Path = Buffer->getBufferIdentifier();
176 auto Stream = std::make_unique<llvm::MemoryBufferByteStream>(
177 std::move(Buffer), llvm::support::little);
178
179 auto File = std::make_unique<PDBFile>(Path, std::move(Stream), Allocator);
180 if (auto EC = File->parseFileHeaders()) {
181 llvm::consumeError(std::move(EC));
182 return nullptr;
183 }
184 if (auto EC = File->parseStreamData()) {
185 llvm::consumeError(std::move(EC));
186 return nullptr;
187 }
188
189 return File;
190}
static UUID GetPDBUUID(InfoStream &IS)
#define LLDB_PLUGIN_DEFINE(PluginName)
Definition: PluginManager.h:31
An architecture specification class.
Definition: ArchSpec.h:32
bool SetTriple(const llvm::Triple &triple)
Architecture triple setter.
Definition: ArchSpec.cpp:750
bool SetArchitecture(ArchitectureType arch_type, uint32_t cpu, uint32_t sub, uint32_t os=0)
Change the architecture object type, CPU type and OS type.
Definition: ArchSpec.cpp:854
A file utility class.
Definition: FileSpec.h:56
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition: FileSpec.cpp:366
An abstract base class for files.
Definition: File.h:36
void Append(const ModuleSpec &spec)
Definition: ModuleSpec.h:308
ArchSpec & GetArchitecture()
Definition: ModuleSpec.h:89
static const char * GetPluginDescriptionStatic()
Definition: ObjectFilePDB.h:26
static std::unique_ptr< llvm::pdb::PDBFile > loadPDBFile(std::string PdbPath, llvm::BumpPtrAllocator &Allocator)
static size_t GetModuleSpecifications(const FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, ModuleSpecList &specs)
std::unique_ptr< llvm::pdb::PDBFile > m_file_up
Definition: ObjectFilePDB.h:99
ObjectFilePDB(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
static ObjectFile * CreateMemoryInstance(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t header_addr)
llvm::BumpPtrAllocator m_allocator
Definition: ObjectFilePDB.h:98
static llvm::StringRef GetPluginNameStatic()
Definition: ObjectFilePDB.h:25
ArchSpec GetArchitecture() override
Get the ArchSpec for this object file.
static ObjectFile * CreateInstance(const lldb::ModuleSP &module_sp, lldb::DataBufferSP data_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
A plug-in interface definition class for object file parsers.
Definition: ObjectFile.h:62
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
#define LLDB_INVALID_CPUTYPE
Definition: lldb-defines.h:95
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
uint64_t offset_t
Definition: lldb-types.h:83
uint64_t addr_t
Definition: lldb-types.h:79
struct lldb_private::UUID::CvRecordPdb70::@43 Uuid
llvm::support::ulittle32_t Age
Definition: UUID.h:46