aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/raw_pdb/src/Examples
diff options
context:
space:
mode:
Diffstat (limited to 'thirdparty/raw_pdb/src/Examples')
-rw-r--r--thirdparty/raw_pdb/src/Examples/CMakeLists.txt39
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleContributions.cpp96
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleFunctionSymbols.cpp262
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleFunctionVariables.cpp382
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleIPI.cpp198
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleLines.cpp268
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleMain.cpp200
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.cpp100
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.h29
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExamplePDBSize.cpp124
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleSymbols.cpp238
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleTimedScope.cpp54
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleTimedScope.h22
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleTypeTable.cpp41
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleTypeTable.h49
-rw-r--r--thirdparty/raw_pdb/src/Examples/ExampleTypes.cpp1418
-rw-r--r--thirdparty/raw_pdb/src/Examples/Examples_PCH.cpp4
-rw-r--r--thirdparty/raw_pdb/src/Examples/Examples_PCH.h53
18 files changed, 3577 insertions, 0 deletions
diff --git a/thirdparty/raw_pdb/src/Examples/CMakeLists.txt b/thirdparty/raw_pdb/src/Examples/CMakeLists.txt
new file mode 100644
index 000000000..6e59c1a9d
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/CMakeLists.txt
@@ -0,0 +1,39 @@
+project(Examples)
+
+set(SOURCES
+ ExampleContributions.cpp
+ ExampleFunctionSymbols.cpp
+ ExampleFunctionVariables.cpp
+ ExampleIPI.cpp
+ ExampleLines.cpp
+ ExampleMain.cpp
+ ExampleMemoryMappedFile.cpp
+ ExampleMemoryMappedFile.h
+ ExamplePDBSize.cpp
+ Examples_PCH.cpp
+ Examples_PCH.h
+ ExampleSymbols.cpp
+ ExampleTimedScope.cpp
+ ExampleTimedScope.h
+ ExampleTypes.cpp
+ ExampleTypeTable.cpp
+ ExampleTypeTable.h
+)
+
+source_group(src FILES
+ ${SOURCES}
+)
+
+add_executable(Examples
+ ${SOURCES}
+)
+
+target_link_libraries(Examples
+ PUBLIC
+ raw_pdb
+)
+
+target_precompile_headers(Examples
+ PUBLIC
+ Examples_PCH.h
+) \ No newline at end of file
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleContributions.cpp b/thirdparty/raw_pdb/src/Examples/ExampleContributions.cpp
new file mode 100644
index 000000000..93c509117
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleContributions.cpp
@@ -0,0 +1,96 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "PDB_RawFile.h"
+#include "PDB_DBIStream.h"
+
+
+namespace
+{
+ // we don't have to store std::string in the contributions, since all the data is memory-mapped anyway.
+ // we do it in this example to ensure that we don't "cheat" when reading the PDB file. memory-mapped data will only
+ // be faulted into the process once it's touched, so actually copying the string data makes us touch the needed data,
+ // giving us a real performance measurement.
+ struct Contribution
+ {
+ std::string objectFile;
+ uint32_t rva;
+ uint32_t size;
+ };
+}
+
+
+void ExampleContributions(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
+void ExampleContributions(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
+{
+ TimedScope total("\nRunning example \"Contributions\"");
+
+ // in order to keep the example easy to understand, we load the PDB data serially.
+ // note that this can be improved a lot by reading streams concurrently.
+
+ // prepare the image section stream first. it is needed for converting section + offset into an RVA
+ TimedScope sectionScope("Reading image section stream");
+ const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
+ sectionScope.Done();
+
+
+ // prepare the module info stream for matching contributions against files
+ TimedScope moduleScope("Reading module info stream");
+ const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
+ moduleScope.Done();
+
+
+ // read contribution stream
+ TimedScope contributionScope("Reading section contribution stream");
+ const PDB::SectionContributionStream sectionContributionStream = dbiStream.CreateSectionContributionStream(rawPdbFile);
+ contributionScope.Done();
+
+ std::vector<Contribution> contributions;
+ {
+ TimedScope scope("Storing contributions");
+
+ const PDB::ArrayView<PDB::DBI::SectionContribution> sectionContributions = sectionContributionStream.GetContributions();
+ const size_t count = sectionContributions.GetLength();
+
+ contributions.reserve(count);
+
+ for (const PDB::DBI::SectionContribution& contribution : sectionContributions)
+ {
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(contribution.section, contribution.offset);
+ if (rva == 0u)
+ {
+ printf("Contribution has invalid RVA\n");
+ continue;
+ }
+
+ const PDB::ModuleInfoStream::Module& module = moduleInfoStream.GetModule(contribution.moduleIndex);
+
+ contributions.push_back(Contribution { module.GetName().Decay(), rva, contribution.size });
+ }
+
+ scope.Done(count);
+ }
+
+ TimedScope sortScope("std::sort contributions");
+ std::sort(contributions.begin(), contributions.end(), [](const Contribution& lhs, const Contribution& rhs)
+ {
+ return lhs.size > rhs.size;
+ });
+ sortScope.Done();
+
+ total.Done();
+
+ // log the 20 largest contributions
+ {
+ printf("20 largest contributions:\n");
+
+ const size_t countToShow = std::min<size_t>(20ul, contributions.size());
+ for (size_t i = 0u; i < countToShow; ++i)
+ {
+ const Contribution& contribution = contributions[i];
+ printf("%zu: %u bytes from %s\n", i + 1u, contribution.size, contribution.objectFile.c_str());
+ }
+ }
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleFunctionSymbols.cpp b/thirdparty/raw_pdb/src/Examples/ExampleFunctionSymbols.cpp
new file mode 100644
index 000000000..fee212e2b
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleFunctionSymbols.cpp
@@ -0,0 +1,262 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "PDB_RawFile.h"
+#include "PDB_DBIStream.h"
+
+namespace
+{
+ // in this example, we are only interested in function symbols: function name, RVA, and size.
+ // this is what most profilers need, they aren't interested in any other data.
+ struct FunctionSymbol
+ {
+ std::string name;
+ uint32_t rva;
+ uint32_t size;
+ const PDB::CodeView::DBI::Record* frameProc;
+ };
+}
+
+
+void ExampleFunctionSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
+void ExampleFunctionSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
+{
+ TimedScope total("\nRunning example \"Function symbols\"");
+
+ // in order to keep the example easy to understand, we load the PDB data serially.
+ // note that this can be improved a lot by reading streams concurrently.
+
+ // prepare the image section stream first. it is needed for converting section + offset into an RVA
+ TimedScope sectionScope("Reading image section stream");
+ const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
+ sectionScope.Done();
+
+
+ // prepare the module info stream for grabbing function symbols from modules
+ TimedScope moduleScope("Reading module info stream");
+ const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
+ moduleScope.Done();
+
+
+ // prepare symbol record stream needed by the public stream
+ TimedScope symbolStreamScope("Reading symbol record stream");
+ const PDB::CoalescedMSFStream symbolRecordStream = dbiStream.CreateSymbolRecordStream(rawPdbFile);
+ symbolStreamScope.Done();
+
+
+ // note that we only use unordered_set in order to keep the example code easy to understand.
+ // using other hash set implementations like e.g. abseil's Swiss Tables (https://abseil.io/about/design/swisstables) is *much* faster.
+ std::vector<FunctionSymbol> functionSymbols;
+ std::unordered_set<uint32_t> seenFunctionRVAs;
+
+ // start by reading the module stream, grabbing every function symbol we can find.
+ // in most cases, this gives us ~90% of all function symbols already, along with their size.
+ {
+ TimedScope scope("Storing function symbols from modules");
+
+ const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
+
+ for (const PDB::ModuleInfoStream::Module& module : modules)
+ {
+ if (!module.HasSymbolStream())
+ {
+ continue;
+ }
+
+ const PDB::ModuleSymbolStream moduleSymbolStream = module.CreateSymbolStream(rawPdbFile);
+ moduleSymbolStream.ForEachSymbol([&functionSymbols, &seenFunctionRVAs, &imageSectionStream](const PDB::CodeView::DBI::Record* record)
+ {
+ // only grab function symbols from the module streams
+ const char* name = nullptr;
+ uint32_t rva = 0u;
+ uint32_t size = 0u;
+ if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_FRAMEPROC)
+ {
+ functionSymbols[functionSymbols.size() - 1].frameProc = record;
+ return;
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_THUNK32)
+ {
+ if (record->data.S_THUNK32.thunk == PDB::CodeView::DBI::ThunkOrdinal::TrampolineIncremental)
+ {
+ // we have never seen incremental linking thunks stored inside a S_THUNK32 symbol, but better safe than sorry
+ name = "ILT";
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_THUNK32.section, record->data.S_THUNK32.offset);
+ size = 5u;
+ }
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_TRAMPOLINE)
+ {
+ // incremental linking thunks are stored in the linker module
+ name = "ILT";
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_TRAMPOLINE.thunkSection, record->data.S_TRAMPOLINE.thunkOffset);
+ size = 5u;
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32)
+ {
+ name = record->data.S_LPROC32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32.section, record->data.S_LPROC32.offset);
+ size = record->data.S_LPROC32.codeSize;
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32)
+ {
+ name = record->data.S_GPROC32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32.section, record->data.S_GPROC32.offset);
+ size = record->data.S_GPROC32.codeSize;
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32_ID)
+ {
+ name = record->data.S_LPROC32_ID.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32_ID.section, record->data.S_LPROC32_ID.offset);
+ size = record->data.S_LPROC32_ID.codeSize;
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32_ID)
+ {
+ name = record->data.S_GPROC32_ID.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32_ID.section, record->data.S_GPROC32_ID.offset);
+ size = record->data.S_GPROC32_ID.codeSize;
+ }
+
+ if (rva == 0u)
+ {
+ return;
+ }
+
+ functionSymbols.push_back(FunctionSymbol { name, rva, size, nullptr });
+ seenFunctionRVAs.emplace(rva);
+ });
+ }
+
+ scope.Done(modules.GetLength());
+ }
+
+ // we don't need to touch global symbols in this case.
+ // most of the data we need can be obtained from the module symbol streams, and the global symbol stream only offers data symbols on top of that, which we are not interested in.
+ // however, there can still be public function symbols we haven't seen yet in any of the modules, especially for PDBs that don't provide module-specific information.
+
+ // read public symbols
+ TimedScope publicScope("Reading public symbol stream");
+ const PDB::PublicSymbolStream publicSymbolStream = dbiStream.CreatePublicSymbolStream(rawPdbFile);
+ publicScope.Done();
+ {
+ TimedScope scope("Storing public function symbols");
+
+ const PDB::ArrayView<PDB::HashRecord> hashRecords = publicSymbolStream.GetRecords();
+ const size_t count = hashRecords.GetLength();
+
+ for (const PDB::HashRecord& hashRecord : hashRecords)
+ {
+ const PDB::CodeView::DBI::Record* record = publicSymbolStream.GetRecord(symbolRecordStream, hashRecord);
+ if (record->header.kind != PDB::CodeView::DBI::SymbolRecordKind::S_PUB32)
+ {
+ // normally, a PDB only contains S_PUB32 symbols in the public symbol stream, but we have seen PDBs that also store S_CONSTANT as public symbols.
+ // ignore these.
+ continue;
+ }
+
+ if ((PDB_AS_UNDERLYING(record->data.S_PUB32.flags) & PDB_AS_UNDERLYING(PDB::CodeView::DBI::PublicSymbolFlags::Function)) == 0u)
+ {
+ // ignore everything that is not a function
+ continue;
+ }
+
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_PUB32.section, record->data.S_PUB32.offset);
+ if (rva == 0u)
+ {
+ // certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
+ continue;
+ }
+
+ // check whether we already know this symbol from one of the module streams
+ const auto it = seenFunctionRVAs.find(rva);
+ if (it != seenFunctionRVAs.end())
+ {
+ // we know this symbol already, ignore it
+ continue;
+ }
+
+ // this is a new function symbol, so store it.
+ // note that we don't know its size yet.
+ functionSymbols.push_back(FunctionSymbol { record->data.S_PUB32.name, rva, 0u, nullptr });
+ }
+
+ scope.Done(count);
+ }
+
+
+ // we still need to find the size of the public function symbols.
+ // this can be deduced by sorting the symbols by their RVA, and then computing the distance between the current and the next symbol.
+ // this works since functions are always mapped to executable pages, so they aren't interleaved by any data symbols.
+ TimedScope sortScope("std::sort function symbols");
+ std::sort(functionSymbols.begin(), functionSymbols.end(), [](const FunctionSymbol& lhs, const FunctionSymbol& rhs)
+ {
+ return lhs.rva < rhs.rva;
+ });
+ sortScope.Done();
+
+ const size_t symbolCount = functionSymbols.size();
+ if (symbolCount != 0u)
+ {
+ TimedScope computeScope("Computing function symbol sizes");
+
+ size_t foundCount = 0u;
+
+ // we have at least 1 symbol.
+ // compute missing symbol sizes by computing the distance from this symbol to the next.
+ // note that this includes "int 3" padding after the end of a function. if you don't want that, but the actual number of bytes of
+ // the function's code, your best bet is to use a disassembler instead.
+ for (size_t i = 0u; i < symbolCount - 1u; ++i)
+ {
+ FunctionSymbol& currentSymbol = functionSymbols[i];
+ if (currentSymbol.size != 0u)
+ {
+ // the symbol's size is already known
+ continue;
+ }
+
+ const FunctionSymbol& nextSymbol = functionSymbols[i + 1u];
+ const size_t size = nextSymbol.rva - currentSymbol.rva;
+ (void)size; // unused
+ ++foundCount;
+ }
+
+ // we know have the sizes of all symbols, except the last.
+ // this can be found by going through the contributions, if needed.
+ FunctionSymbol& lastSymbol = functionSymbols[symbolCount - 1u];
+ if (lastSymbol.size == 0u)
+ {
+ // bad luck, we can't deduce the last symbol's size, so have to consult the contributions instead.
+ // we do a linear search in this case to keep the code simple.
+ const PDB::SectionContributionStream sectionContributionStream = dbiStream.CreateSectionContributionStream(rawPdbFile);
+ const PDB::ArrayView<PDB::DBI::SectionContribution> sectionContributions = sectionContributionStream.GetContributions();
+ for (const PDB::DBI::SectionContribution& contribution : sectionContributions)
+ {
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(contribution.section, contribution.offset);
+ if (rva == 0u)
+ {
+ printf("Contribution has invalid RVA\n");
+ continue;
+ }
+
+ if (rva == lastSymbol.rva)
+ {
+ lastSymbol.size = contribution.size;
+ break;
+ }
+
+ if (rva > lastSymbol.rva)
+ {
+ // should have found the contribution by now
+ printf("Unknown contribution for symbol %s at RVA 0x%X", lastSymbol.name.c_str(), lastSymbol.rva);
+ break;
+ }
+ }
+ }
+
+ computeScope.Done(foundCount);
+ }
+
+ total.Done(functionSymbols.size());
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleFunctionVariables.cpp b/thirdparty/raw_pdb/src/Examples/ExampleFunctionVariables.cpp
new file mode 100644
index 000000000..85b561026
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleFunctionVariables.cpp
@@ -0,0 +1,382 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "ExampleTypeTable.h"
+#include "PDB_RawFile.h"
+#include "PDB_DBIStream.h"
+#include "PDB_TPIStream.h"
+
+using SymbolRecordKind = PDB::CodeView::DBI::SymbolRecordKind;
+
+static std::string GetVariableTypeName(const TypeTable& typeTable, uint32_t typeIndex)
+{
+ // Defined in ExampleTypes.cpp
+ extern std::string GetTypeName(const TypeTable & typeTable, uint32_t typeIndex);
+
+ std::string typeName = GetTypeName(typeTable, typeIndex);
+
+ // Remove any '%s' substring used to insert a variable/field name.
+ const uint64_t markerPos = typeName.find("%s");
+ if (markerPos != typeName.npos)
+ {
+ typeName.erase(markerPos, 2);
+ }
+
+ return typeName;
+}
+
+static void Printf(uint32_t indent, const char* format, ...)
+{
+ va_list args;
+ va_start(args, format);
+
+ printf("%*s", indent * 4, "");
+ vprintf(format, args);
+
+ va_end(args);
+}
+
+void ExampleFunctionVariables(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::TPIStream& tpiStream);
+void ExampleFunctionVariables(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::TPIStream& tpiStream)
+{
+ TimedScope total("\nRunning example \"Function variables\"");
+
+ TimedScope typeTableScope("Create TypeTable");
+ TypeTable typeTable(tpiStream);
+ typeTableScope.Done();
+
+ // in order to keep the example easy to understand, we load the PDB data serially.
+ // note that this can be improved a lot by reading streams concurrently.
+
+ // prepare the image section stream first. it is needed for converting section + offset into an RVA
+ TimedScope sectionScope("Reading image section stream");
+ const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
+ sectionScope.Done();
+
+ // prepare the module info stream for grabbing function symbols from modules
+ TimedScope moduleScope("Reading module info stream");
+ const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
+ moduleScope.Done();
+
+ // prepare symbol record stream needed by the public stream
+ TimedScope symbolStreamScope("Reading symbol record stream");
+ const PDB::CoalescedMSFStream symbolRecordStream = dbiStream.CreateSymbolRecordStream(rawPdbFile);
+ symbolStreamScope.Done();
+
+ {
+ TimedScope scope("Printing function variable records from modules\n");
+
+ const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
+
+ uint32_t blockLevel = 0;
+ uint32_t recordCount = 0;
+
+ for (const PDB::ModuleInfoStream::Module& module : modules)
+ {
+ if (!module.HasSymbolStream())
+ {
+ continue;
+ }
+
+ const PDB::ModuleSymbolStream moduleSymbolStream = module.CreateSymbolStream(rawPdbFile);
+ moduleSymbolStream.ForEachSymbol([&typeTable, &imageSectionStream, &blockLevel, &recordCount](const PDB::CodeView::DBI::Record* record)
+ {
+ const SymbolRecordKind kind = record->header.kind;
+ const PDB::CodeView::DBI::Record::Data& data = record->data;
+
+ if (kind == SymbolRecordKind::S_END)
+ {
+ PDB_ASSERT(blockLevel > 0, "Block level for S_END is 0");
+ blockLevel--;
+ Printf(blockLevel, "S_END\n");
+
+ if (blockLevel == 0)
+ {
+ Printf(0, "\n");
+ }
+ }
+ else if(kind == SymbolRecordKind::S_SKIP)
+ {
+ Printf(blockLevel, "S_SKIP\n");
+ }
+ else if (kind == SymbolRecordKind::S_BLOCK32)
+ {
+ const uint32_t offset = imageSectionStream.ConvertSectionOffsetToRVA(data.S_BLOCK32.section, data.S_BLOCK32.offset);
+
+ Printf(blockLevel, "S_BLOCK32: '%s' | Code Offset 0x%X\n", data.S_BLOCK32.name, offset);
+ blockLevel++;
+ }
+ else if (kind == SymbolRecordKind::S_LABEL32)
+ {
+ Printf(blockLevel, "S_LABEL32: '%s' | Offset 0x%X\n", data.S_LABEL32.name, data.S_LABEL32.offset);
+ }
+ else if(kind == SymbolRecordKind::S_CONSTANT)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_CONSTANT.typeIndex);
+
+ Printf(blockLevel, "S_CONSTANT: '%s' -> '%s' | Value 0x%X\n", typeName.c_str(), data.S_CONSTANT.name, data.S_CONSTANT.value);
+ }
+ else if(kind == SymbolRecordKind::S_LOCAL)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_LOCAL.typeIndex);
+ Printf(blockLevel, "S_LOCAL: '%s' -> '%s' | Param: %s | Optimized Out: %s\n", typeName.c_str(), data.S_LOCAL.name, data.S_LOCAL.flags.fIsParam ? "True" : "False", data.S_LOCAL.flags.fIsOptimizedOut ? "True" : "False");
+ }
+ else if (kind == SymbolRecordKind::S_DEFRANGE_REGISTER)
+ {
+ Printf(blockLevel, "S_DEFRANGE_REGISTER: Register 0x%X\n", data.S_DEFRANGE_REGISTER.reg);
+ }
+ else if(kind == SymbolRecordKind::S_DEFRANGE_FRAMEPOINTER_REL)
+ {
+ Printf(blockLevel, "S_DEFRANGE_FRAMEPOINTER_REL: Frame Pointer Offset 0x%X | Range Start 0x%X | Range Section Start 0x%X | Range Length %u\n",
+ data.S_DEFRANGE_FRAMEPOINTER_REL.offsetFramePointer,
+ data.S_DEFRANGE_FRAMEPOINTER_REL.range.offsetStart,
+ data.S_DEFRANGE_FRAMEPOINTER_REL.range.isectionStart,
+ data.S_DEFRANGE_FRAMEPOINTER_REL.range.length);
+ }
+ else if(kind == SymbolRecordKind::S_DEFRANGE_SUBFIELD_REGISTER)
+ {
+ Printf(blockLevel, "S_DEFRANGE_SUBFIELD_REGISTER: Register %u | Parent offset 0x%X | Range Start 0x%X | Range Section Start 0x%X | Range Length %u\n",
+ data.S_DEFRANGE_SUBFIELD_REGISTER.reg,
+ data.S_DEFRANGE_SUBFIELD_REGISTER.offsetParent,
+ data.S_DEFRANGE_SUBFIELD_REGISTER.range.offsetStart,
+ data.S_DEFRANGE_SUBFIELD_REGISTER.range.isectionStart,
+ data.S_DEFRANGE_SUBFIELD_REGISTER.range.length);
+ }
+ else if (kind == SymbolRecordKind::S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE)
+ {
+ Printf(blockLevel, "S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE: Offset 0x%X\n", data.S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE.offsetFramePointer);
+ }
+ else if (kind == SymbolRecordKind::S_DEFRANGE_REGISTER_REL)
+ {
+ Printf(blockLevel, "S_DEFRANGE_REGISTER_REL: Base Register %u | Parent offset 0x%X | Base Register Offset 0x%X | Range Start 0x%X | Range Section Start 0x%X | Range Length %u\n",
+ data.S_DEFRANGE_REGISTER_REL.baseRegister,
+ data.S_DEFRANGE_REGISTER_REL.offsetParent,
+ data.S_DEFRANGE_REGISTER_REL.offsetBasePointer,
+ data.S_DEFRANGE_REGISTER_REL.offsetParent,
+ data.S_DEFRANGE_REGISTER_REL.range.offsetStart,
+ data.S_DEFRANGE_REGISTER_REL.range.isectionStart,
+ data.S_DEFRANGE_REGISTER_REL.range.length);
+ }
+ else if(kind == SymbolRecordKind::S_FILESTATIC)
+ {
+ Printf(blockLevel, "S_FILESTATIC: '%s'\n", data.S_FILESTATIC.name);
+ }
+ else if (kind == SymbolRecordKind::S_INLINESITE)
+ {
+ Printf(blockLevel, "S_INLINESITE: Parent 0x%X\n", data.S_INLINESITE.parent);
+ blockLevel++;
+ }
+ else if (kind == SymbolRecordKind::S_INLINESITE_END)
+ {
+ PDB_ASSERT(blockLevel > 0, "Block level for S_INLINESITE_END is 0");
+ blockLevel--;
+ Printf(blockLevel, "S_INLINESITE_END:\n");
+ }
+ else if (kind == SymbolRecordKind::S_CALLEES)
+ {
+ Printf(blockLevel, "S_CALLEES: Count %u\n", data.S_CALLEES.count);
+ }
+ else if (kind == SymbolRecordKind::S_CALLERS)
+ {
+ Printf(blockLevel, "S_CALLERS: Count %u\n", data.S_CALLERS.count);
+ }
+ else if (kind == SymbolRecordKind::S_INLINEES)
+ {
+ Printf(blockLevel, "S_INLINEES: Count %u\n", data.S_INLINEES.count);
+ }
+ else if (kind == SymbolRecordKind::S_LDATA32)
+ {
+ if (blockLevel > 0)
+ {
+ // Not sure why some type index 0 (T_NO_TYPE) are included in some PDBs.
+ if (data.S_LDATA32.typeIndex != 0) // PDB::CodeView::TPI::TypeIndexKind::T_NOTYPE)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_LDATA32.typeIndex);
+ Printf(blockLevel, "S_LDATA32: '%s' -> '%s'\n", data.S_LDATA32.name, typeName.c_str());
+ }
+ }
+ }
+ else if (kind == SymbolRecordKind::S_LTHREAD32)
+ {
+ if (blockLevel > 0)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_LTHREAD32.typeIndex);
+ Printf(blockLevel, "S_LTHREAD32: '%s' -> '%s'\n", data.S_LTHREAD32.name, typeName.c_str());
+ }
+ }
+ else if (kind == SymbolRecordKind::S_UDT)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_UDT.typeIndex);
+
+ Printf(blockLevel, "S_UDT: '%s' -> '%s'\n", data.S_UDT.name, typeName.c_str());
+ }
+ else if (kind == PDB::CodeView::DBI::SymbolRecordKind::S_REGISTER)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_REGSYM.typeIndex);
+
+ Printf(blockLevel, "S_REGSYM: '%s' -> '%s' | Register %i\n",
+ data.S_REGSYM.name, typeName.c_str(),
+ data.S_REGSYM.reg);
+ }
+ else if (kind == PDB::CodeView::DBI::SymbolRecordKind::S_BPREL32)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_BPRELSYM32.typeIndex);
+
+ Printf(blockLevel, "S_BPRELSYM32: '%s' -> '%s' | BP register Offset 0x%X\n",
+ data.S_BPRELSYM32.name, typeName.c_str(),
+ data.S_BPRELSYM32.offset);
+ }
+ else if (kind == PDB::CodeView::DBI::SymbolRecordKind::S_REGREL32)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_REGREL32.typeIndex);
+
+ Printf(blockLevel, "S_REGREL32: '%s' -> '%s' | Register %i | Register Offset 0x%X\n",
+ data.S_REGREL32.name, typeName.c_str(),
+ data.S_REGREL32.reg,
+ data.S_REGREL32.offset);
+ }
+ else if(kind == SymbolRecordKind::S_FRAMECOOKIE)
+ {
+ Printf(blockLevel, "S_FRAMECOOKIE: Offset 0x%X | Register %u | Type %u\n",
+ data.S_FRAMECOOKIE.offset,
+ data.S_FRAMECOOKIE.reg,
+ data.S_FRAMECOOKIE.cookietype);
+ }
+ else if(kind == SymbolRecordKind::S_CALLSITEINFO)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_CALLSITEINFO.typeIndex);
+ Printf(blockLevel, "S_CALLSITEINFO: '%s' | Offset 0x%X | Section %u\n", typeName.c_str(), data.S_CALLSITEINFO.offset, data.S_CALLSITEINFO.section);
+ }
+ else if(kind == SymbolRecordKind::S_HEAPALLOCSITE)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_HEAPALLOCSITE.typeIndex);
+ Printf(blockLevel, "S_HEAPALLOCSITE: '%s' | Offset 0x%X | Section %u | Instruction Length %u\n", typeName.c_str(),
+ data.S_HEAPALLOCSITE.offset,
+ data.S_HEAPALLOCSITE.section,
+ data.S_HEAPALLOCSITE.instructionLength);
+ }
+ else if (kind == SymbolRecordKind::S_FRAMEPROC)
+ {
+ Printf(blockLevel, "S_FRAMEPROC: Size %u | Padding %u | Padding Offset 0x%X | Callee Registers Size %u\n",
+ data.S_FRAMEPROC.cbFrame,
+ data.S_FRAMEPROC.cbPad,
+ data.S_FRAMEPROC.offPad,
+ data.S_FRAMEPROC.cbSaveRegs);
+ }
+ else if (kind == SymbolRecordKind::S_ANNOTATION)
+ {
+ Printf(blockLevel, "S_ANNOTATION: Offset 0x%X | Count %u\n", data.S_ANNOTATIONSYM.offset, data.S_ANNOTATIONSYM.annotationsCount);
+ // print N null-terminated annotation strings, skipping their null-terminators to get to the next string
+ const char* annotation = data.S_ANNOTATIONSYM.annotations;
+ for (int i = 0; i < data.S_ANNOTATIONSYM.annotationsCount; ++i, annotation += strlen(annotation) + 1)
+ Printf(blockLevel + 1, "S_ANNOTATION.%u: %s\n", i, annotation);
+ PDB_ASSERT(annotation <= (const char*)record + record->header.size + sizeof(record->header.size),
+ "Annotation strings end beyond the record size %X; annotaions count: %u", record->header.size, data.S_ANNOTATIONSYM.annotationsCount);
+ }
+ else if (kind == SymbolRecordKind::S_THUNK32)
+ {
+ PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
+
+ if (data.S_THUNK32.thunk == PDB::CodeView::DBI::ThunkOrdinal::TrampolineIncremental)
+ {
+ // we have never seen incremental linking thunks stored inside a S_THUNK32 symbol, but better safe than sorry
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_THUNK32.section, data.S_THUNK32.offset);
+ Printf(blockLevel, "Function: 'ILT/Thunk' | RVA 0x%X\n", rva);
+ }
+ else
+ {
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_THUNK32.section, data.S_THUNK32.offset);
+ Printf(blockLevel, "S_THUNK32 Function '%s' | RVA 0x%X\n", data.S_THUNK32.name, rva);
+ blockLevel++;
+ }
+ }
+ else if (kind == SymbolRecordKind::S_TRAMPOLINE)
+ {
+ PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
+ // incremental linking thunks are stored in the linker module
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_TRAMPOLINE.thunkSection, data.S_TRAMPOLINE.thunkOffset);
+ Printf(blockLevel, "Function 'ILT/Trampoline' | RVA 0x%X\n", rva);
+ }
+ else if (kind == SymbolRecordKind::S_LPROC32)
+ {
+ PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_LPROC32.section, data.S_LPROC32.offset);
+ Printf(blockLevel, "S_LPROC32 Function '%s' | RVA 0x%X\n", data.S_LPROC32.name, rva);
+ blockLevel++;
+ }
+ else if (kind == SymbolRecordKind::S_GPROC32)
+ {
+ PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_GPROC32.section, data.S_GPROC32.offset);
+ Printf(blockLevel, "S_GPROC32 Function '%s' | RVA 0x%X\n", data.S_GPROC32.name, rva);
+ blockLevel++;
+ }
+ else if (kind == SymbolRecordKind::S_LPROC32_ID)
+ {
+ PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_LPROC32_ID.section, data.S_LPROC32_ID.offset);
+ Printf(blockLevel, "S_LPROC32_ID Function '%s' | RVA 0x%X\n", data.S_LPROC32_ID.name, rva);
+ blockLevel++;
+ }
+ else if (kind == SymbolRecordKind::S_GPROC32_ID)
+ {
+ PDB_ASSERT(blockLevel == 0, "BlockLevel %u != 0", blockLevel);
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(data.S_GPROC32_ID.section, data.S_GPROC32_ID.offset);
+ Printf(blockLevel, "S_GPROC32_ID Function '%s' | RVA 0x%X\n", data.S_GPROC32_ID.name, rva);
+ blockLevel++;
+ }
+ else if (kind == SymbolRecordKind::S_REGREL32_INDIR)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_REGREL32_INDIR.typeIndex);
+
+ Printf(blockLevel, "S_REGREL32_INDIR: '%s' -> '%s' | Register %i | Unknown1 0x%X | Unknown2 0x%X\n",
+ data.S_REGREL32_INDIR.name, typeName.c_str(),
+ data.S_REGREL32_INDIR.unknown1,
+ data.S_REGREL32_INDIR.unknown1);
+ }
+ else if (kind == SymbolRecordKind::S_REGREL32_ENCTMP)
+ {
+ const std::string typeName = GetVariableTypeName(typeTable, data.S_REGREL32.typeIndex);
+
+ Printf(blockLevel, "S_REGREL32_ENCTMP: '%s' -> '%s' | Register %i | Register Offset 0x%X\n",
+ data.S_REGREL32.name, typeName.c_str(),
+ data.S_REGREL32.reg,
+ data.S_REGREL32.offset);
+ }
+ else if (kind == SymbolRecordKind::S_UNAMESPACE)
+ {
+ Printf(blockLevel, "S_UNAMESPACE: '%s'\n", data.S_UNAMESPACE.name);
+ }
+ else if (kind == SymbolRecordKind::S_ARMSWITCHTABLE)
+ {
+ Printf(blockLevel, "S_ARMSWITCHTABLE: "
+ "Switch Type: %u | Num Entries: %u | Base Section: %u | Base Offset: 0x%X | "
+ "Branch Section: %u | Branch Offset: 0x%X | Table Section: %u | Table Offset: 0x%X\n",
+ data.S_ARMSWITCHTABLE.switchType,
+ data.S_ARMSWITCHTABLE.numEntries,
+ data.S_ARMSWITCHTABLE.sectionBase,
+ data.S_ARMSWITCHTABLE.offsetBase,
+ data.S_ARMSWITCHTABLE.sectionBranch,
+ data.S_ARMSWITCHTABLE.offsetBranch,
+ data.S_ARMSWITCHTABLE.sectionTable,
+ data.S_ARMSWITCHTABLE.offsetTable);
+ }
+ else
+ {
+ // We only care about records inside functions.
+ if (blockLevel > 0)
+ {
+ PDB_ASSERT(false, "Unhandled record kind 0x%X with block level %u\n", static_cast<uint16_t>(kind), blockLevel);
+ }
+ }
+
+ recordCount++;
+
+ });
+ }
+
+ scope.Done(recordCount);
+ }
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleIPI.cpp b/thirdparty/raw_pdb/src/Examples/ExampleIPI.cpp
new file mode 100644
index 000000000..5286689e9
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleIPI.cpp
@@ -0,0 +1,198 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "ExampleTypeTable.h"
+#include "PDB_RawFile.h"
+#include "PDB_InfoStream.h"
+#include "PDB_IPIStream.h"
+#include "PDB_TPIStream.h"
+
+static std::string GetTypeNameIPI(const TypeTable& typeTable, uint32_t typeIndex)
+{
+ // Defined in ExampleTypes.cpp
+ extern std::string GetTypeName(const TypeTable & typeTable, uint32_t typeIndex);
+
+ std::string typeName = GetTypeName(typeTable, typeIndex);
+
+ // Remove any '%s' substring used to insert a variable/field name.
+ const uint64_t markerPos = typeName.find("%s");
+ if (markerPos != typeName.npos)
+ {
+ typeName.erase(markerPos, 2);
+ }
+
+ return typeName;
+}
+
+void ExampleIPI(const PDB::RawFile& rawPdbFile, const PDB::InfoStream& infoStream, const PDB::TPIStream& tpiStream, const PDB::IPIStream& ipiStream);
+
+void ExampleIPI(const PDB::RawFile& rawPdbFile, const PDB::InfoStream& infoStream, const PDB::TPIStream& tpiStream, const PDB::IPIStream& ipiStream)
+{
+ if (!infoStream.HasIPIStream())
+ {
+ return;
+ }
+
+ TimedScope total("\nRunning example \"IPI\"");
+
+ TimedScope typeTableScope("Create TypeTable");
+ TypeTable typeTable(tpiStream);
+ typeTableScope.Done();
+
+ // prepare names stream for grabbing file paths from lines
+ TimedScope namesScope("Reading names stream");
+ const PDB::NamesStream namesStream = infoStream.CreateNamesStream(rawPdbFile);
+ namesScope.Done();
+
+ const uint32_t firstTypeIndex = ipiStream.GetFirstTypeIndex();
+
+ PDB::ArrayView<const PDB::CodeView::IPI::Record*> records = ipiStream.GetTypeRecords();
+
+ std::vector<const char*> strings;
+
+ strings.resize(records.GetLength(), nullptr);
+
+ size_t index = 0;
+
+ for (const PDB::CodeView::IPI::Record* record : records)
+ {
+ const PDB::CodeView::IPI::RecordHeader& header = record->header;
+
+ if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_STRING_ID)
+ {
+ strings[index] = record->data.LF_STRING_ID.name;
+ }
+
+ index++;
+ }
+
+ uint32_t identifier = firstTypeIndex;
+
+ std::string typeName, parentTypeName;
+
+ printf("\n --- IPI Records ---\n\n");
+
+ for(const PDB::CodeView::IPI::Record* record : records)
+ {
+ const PDB::CodeView::IPI::RecordHeader& header = record->header;
+
+ if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_FUNC_ID)
+ {
+ typeName = GetTypeNameIPI(typeTable, record->data.LF_FUNC_ID.typeIndex);
+
+ printf("Kind: 'LF_FUNC_ID' Size: %i ID: %u\n", header.size, identifier);
+ printf(" Scope ID: %u\n Type: '%s'\n Name: '%s'\n\n",
+ record->data.LF_FUNC_ID.scopeId,
+ typeName.c_str(),
+ record->data.LF_FUNC_ID.name);
+
+ }
+ else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_MFUNC_ID)
+ {
+ typeName = GetTypeNameIPI(typeTable, record->data.LF_MFUNC_ID.typeIndex);
+ parentTypeName = GetTypeNameIPI(typeTable, record->data.LF_MFUNC_ID.parentTypeIndex);
+
+ printf("Kind: 'LF_MFUNC_ID' Size: %i ID: %u\n", header.size, identifier);
+ printf(" Parent Type: '%s'\n Type: '%s'\n Name: '%s'\n\n",
+ parentTypeName.c_str(),
+ typeName.c_str(),
+ record->data.LF_MFUNC_ID.name);
+
+ }
+ else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_BUILDINFO)
+ {
+ printf("Kind: 'LF_BUILDINFO' Size: %u ID: %u\n", header.size, identifier);
+
+ if (record->data.LF_BUILDINFO.count == 0)
+ {
+ continue;
+ }
+
+ printf("Strings: '%s'", strings[record->data.LF_BUILDINFO.typeIndices[0] - firstTypeIndex]);
+
+ for (uint32_t i = 1, size = record->data.LF_BUILDINFO.count; i < size; ++i)
+ {
+ const uint32_t stringIndex = record->data.LF_BUILDINFO.typeIndices[i];
+
+ if (stringIndex == 0)
+ {
+ printf(", ''");
+ }
+ else
+ {
+ printf(", '%s'", strings[stringIndex - firstTypeIndex]);
+ }
+ }
+
+ printf("\n\n");
+ }
+ else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_SUBSTR_LIST)
+ {
+ printf("Kind: 'LF_SUBSTR_LIST' Size: %u ID: %u\n", header.size, identifier);
+
+ if (record->data.LF_SUBSTR_LIST.count == 0)
+ {
+ continue;
+ }
+
+ printf(" Strings: '%s'", strings[record->data.LF_SUBSTR_LIST.typeIndices[0] - firstTypeIndex]);
+
+ for (uint32_t i = 1, size = record->data.LF_SUBSTR_LIST.count; i < size; ++i)
+ {
+ const uint32_t stringIndex = record->data.LF_SUBSTR_LIST.typeIndices[i];
+
+ if (stringIndex == 0)
+ {
+ printf(", ''");
+ }
+ else
+ {
+ printf(", '%s'", strings[stringIndex - firstTypeIndex]);
+ }
+ }
+
+ printf("\n\n");
+ }
+ else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_STRING_ID)
+ {
+ printf("Kind: 'LF_STRING_ID' Size: %u ID: %u\n", header.size, identifier);
+
+ printf(" Substring ID: %u\n Name: '%s'\n\n", record->data.LF_STRING_ID.id, record->data.LF_STRING_ID.name);
+ }
+ else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_UDT_SRC_LINE)
+ {
+ typeName = GetTypeNameIPI(typeTable, record->data.LF_UDT_SRC_LINE.typeIndex);
+
+ const uint32_t stringIndex = record->data.LF_UDT_SRC_LINE.stringIndex;
+
+ printf("Kind: 'LF_UDT_SRC_LINE' Size: %u ID: %u\n", header.size, identifier);
+
+ printf(" Type: '%s'\n Source Path: %s\n Line: %u\n\n",
+ typeName.c_str(),
+ strings[stringIndex - firstTypeIndex],
+ record->data.LF_UDT_SRC_LINE.line);
+ }
+ else if (header.kind == PDB::CodeView::IPI::TypeRecordKind::LF_UDT_MOD_SRC_LINE)
+ {
+ typeName = GetTypeNameIPI(typeTable, record->data.LF_UDT_MOD_SRC_LINE.typeIndex);
+
+ const char* string = namesStream.GetFilename(record->data.LF_UDT_MOD_SRC_LINE.stringIndex);
+
+ printf("Kind: 'LF_UDT_SRC_LINE' Size: %u ID: %u\n", header.size, identifier);
+
+ printf(" Type: '%s'\n Source Path: %s\n Line: %u\n Module Index: %u\n\n",
+ typeName.c_str(),
+ string,
+ record->data.LF_UDT_MOD_SRC_LINE.line,
+ record->data.LF_UDT_MOD_SRC_LINE.moduleIndex);
+ }
+ else
+ {
+ printf("Kind: 0x%X Size: %u ID: %u\n\n", static_cast<uint32_t>(header.kind), header.size, identifier);
+ }
+
+ identifier++;
+ }
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleLines.cpp b/thirdparty/raw_pdb/src/Examples/ExampleLines.cpp
new file mode 100644
index 000000000..f055b98c5
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleLines.cpp
@@ -0,0 +1,268 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "Foundation/PDB_PointerUtil.h"
+#include "PDB_RawFile.h"
+#include "PDB_DBIStream.h"
+#include "PDB_InfoStream.h"
+
+#include <cstring>
+
+namespace
+{
+ struct Section
+ {
+ uint16_t index;
+ uint32_t offset;
+ size_t lineIndex;
+ };
+
+ struct Filename
+ {
+ uint32_t fileChecksumOffset;
+ uint32_t namesFilenameOffset;
+ PDB::CodeView::DBI::ChecksumKind checksumKind;
+ uint8_t checksumSize;
+ uint8_t checksum[32];
+ };
+
+ struct Line
+ {
+ uint32_t lineNumber;
+ uint32_t codeSize;
+ size_t filenameIndex;
+ };
+}
+
+void ExampleLines(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::InfoStream& infoStream);
+void ExampleLines(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::InfoStream& infoStream)
+{
+ if (!infoStream.HasNamesStream())
+ {
+ printf("PDB has no '/names' stream for looking up filenames for lines, skipping \"Lines\" example.");
+ return;
+ }
+
+ TimedScope total("\nRunning example \"Lines\"");
+
+ // prepare the image section stream first. it is needed for converting section + offset into an RVA
+ TimedScope sectionScope("Reading image section stream");
+ const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
+ sectionScope.Done();
+
+ // prepare the module info stream for grabbing function symbols from modules
+ TimedScope moduleScope("Reading module info stream");
+ const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
+ moduleScope.Done();
+
+ // prepare names stream for grabbing file paths from lines
+ TimedScope namesScope("Reading names stream");
+ const PDB::NamesStream namesStream = infoStream.CreateNamesStream(rawPdbFile);
+ namesScope.Done();
+
+ // keeping sections and lines separate, as sorting the smaller Section struct is 2x faster in release builds
+ // than having all the fields in one big Line struct and sorting those.
+ std::vector<Section> sections;
+ std::vector<Filename> filenames;
+ std::vector<Line> lines;
+
+ {
+ TimedScope scope("Storing lines from modules");
+
+ const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
+
+ for (const PDB::ModuleInfoStream::Module& module : modules)
+ {
+ if (!module.HasLineStream())
+ {
+ continue;
+ }
+
+ const PDB::ModuleLineStream moduleLineStream = module.CreateLineStream(rawPdbFile);
+
+ const size_t moduleFilenamesStartIndex = filenames.size();
+ const PDB::CodeView::DBI::FileChecksumHeader* moduleFileChecksumHeader = nullptr;
+
+ moduleLineStream.ForEachSection([&moduleLineStream, &namesStream, &moduleFileChecksumHeader, &sections, &filenames, &lines](const PDB::CodeView::DBI::LineSection* lineSection)
+ {
+ if (lineSection->header.kind == PDB::CodeView::DBI::DebugSubsectionKind::S_LINES)
+ {
+ moduleLineStream.ForEachLinesBlock(lineSection,
+ [&lineSection, &sections, &filenames, &lines](const PDB::CodeView::DBI::LinesFileBlockHeader* linesBlockHeader, const PDB::CodeView::DBI::Line* blocklines, const PDB::CodeView::DBI::Column* blockColumns)
+ {
+ if (linesBlockHeader->numLines == 0)
+ {
+ return;
+ }
+
+ const PDB::CodeView::DBI::Line& firstLine = blocklines[0];
+
+ const uint16_t sectionIndex = lineSection->linesHeader.sectionIndex;
+ const uint32_t sectionOffset = lineSection->linesHeader.sectionOffset;
+ const uint32_t fileChecksumOffset = linesBlockHeader->fileChecksumOffset;
+
+ const size_t filenameIndex = filenames.size();
+
+ // there will be duplicate filenames for any real world pdb.
+ // ideally the filenames would be stored in a map with the filename or checksum as the key.
+ // but that would complicate the logic in this example and therefore just use a vector to make it easier to understand.
+ filenames.push_back({ fileChecksumOffset, 0, PDB::CodeView::DBI::ChecksumKind::None, 0, {0} });
+
+ sections.push_back({ sectionIndex, sectionOffset, lines.size() });
+
+ // initially set code size of first line to 0, will be updated in loop below.
+ lines.push_back({ firstLine.linenumStart, 0, filenameIndex });
+
+ for(uint32_t i = 1, size = linesBlockHeader->numLines; i < size; ++i)
+ {
+ const PDB::CodeView::DBI::Line& line = blocklines[i];
+
+ // calculate code size of previous line by using the current line offset.
+ lines.back().codeSize = line.offset - blocklines[i-1].offset;
+
+ sections.push_back({ sectionIndex, sectionOffset + line.offset, lines.size() });
+ lines.push_back({ line.linenumStart, 0, filenameIndex });
+ }
+
+ // calc code size of last line
+ lines.back().codeSize = lineSection->linesHeader.codeSize - blocklines[linesBlockHeader->numLines-1].offset;
+
+ // columns are optional
+ if (blockColumns == nullptr)
+ {
+ return;
+ }
+
+ for (uint32_t i = 0, size = linesBlockHeader->numLines; i < size; ++i)
+ {
+ const PDB::CodeView::DBI::Column& column = blockColumns[i];
+ (void)column;
+ }
+ });
+ }
+ else if (lineSection->header.kind == PDB::CodeView::DBI::DebugSubsectionKind::S_FILECHECKSUMS)
+ {
+ // how to read checksums and their filenames from the Names Stream
+ moduleLineStream.ForEachFileChecksum(lineSection, [&namesStream](const PDB::CodeView::DBI::FileChecksumHeader* fileChecksumHeader)
+ {
+ const char* filename = namesStream.GetFilename(fileChecksumHeader->filenameOffset);
+ (void)filename;
+ });
+
+ // store the checksum header for the module, as there might be more lines after the checksums.
+ // so lines will get their checksum header values assigned after processing all line sections in the module.
+ PDB_ASSERT(moduleFileChecksumHeader == nullptr, "Module File Checksum Header already set");
+ moduleFileChecksumHeader = &lineSection->checksumHeader;
+ }
+ else if (lineSection->header.kind == PDB::CodeView::DBI::DebugSubsectionKind::S_INLINEELINES)
+ {
+ if (lineSection->inlineeHeader.kind == PDB::CodeView::DBI::InlineeSourceLineKind::Signature)
+ {
+ moduleLineStream.ForEachInlineeSourceLine(lineSection, [](const PDB::CodeView::DBI::InlineeSourceLine* inlineeSourceLine)
+ {
+ (void)inlineeSourceLine;
+
+ });
+ }
+ else
+ {
+ moduleLineStream.ForEachInlineeSourceLineEx(lineSection, [](const PDB::CodeView::DBI::InlineeSourceLineEx* inlineeSourceLineEx)
+ {
+ for (uint32_t i = 0; i < inlineeSourceLineEx->extraLines; ++i)
+ {
+ const uint32_t checksumOffset = inlineeSourceLineEx->extrafileChecksumOffsets[i];
+ (void)checksumOffset;
+ }
+ });
+ }
+ }
+ else
+ {
+ PDB_ASSERT(false, "Line Section kind 0x%X not handled", static_cast<uint32_t>(lineSection->header.kind));
+ }
+ });
+
+ // assign checksum values for each filename added in this module
+ for (size_t i = moduleFilenamesStartIndex, size = filenames.size(); i < size; ++i)
+ {
+ Filename& filename = filenames[i];
+
+ // look up the filename's checksum header in the module's checksums section
+ const PDB::CodeView::DBI::FileChecksumHeader* checksumHeader = PDB::Pointer::Offset<const PDB::CodeView::DBI::FileChecksumHeader*>(moduleFileChecksumHeader, filename.fileChecksumOffset);
+
+ PDB_ASSERT(checksumHeader->checksumKind >= PDB::CodeView::DBI::ChecksumKind::None &&
+ checksumHeader->checksumKind <= PDB::CodeView::DBI::ChecksumKind::SHA256,
+ "Invalid checksum kind %u", static_cast<uint16_t>(checksumHeader->checksumKind));
+
+ // store checksum values in filname struct
+ filename.namesFilenameOffset = checksumHeader->filenameOffset;
+ filename.checksumKind = checksumHeader->checksumKind;
+ filename.checksumSize = checksumHeader->checksumSize;
+ std::memcpy(filename.checksum, checksumHeader->checksum, checksumHeader->checksumSize);
+ }
+ }
+
+ scope.Done(modules.GetLength());
+
+ TimedScope sortScope("std::sort sections");
+
+ // sort sections, so we can iterate over lines by address order.
+ std::sort(sections.begin(), sections.end(), [](const Section& lhs, const Section& rhs)
+ {
+ if (lhs.index == rhs.index)
+ {
+ return lhs.offset < rhs.offset;
+ }
+
+ return lhs.index < rhs.index;
+ });
+
+ sortScope.Done(sections.size());
+
+// Disabled by default, as it will print a lot of lines for large PDBs :-)
+#if 0
+ // DIA2Dump style lines output
+ static const char hexChars[17] = "0123456789ABCDEF";
+ char checksumString[128];
+
+ printf("*** LINES RAW PDB\n");
+
+ const char* prevFilename = nullptr;
+
+ for (const Section& section : sections)
+ {
+ const Line& line = lines[section.lineIndex];
+ const Filename& lineFilename = filenames[line.filenameIndex];
+
+ const char* filename = namesStream.GetFilename(lineFilename.namesFilenameOffset);
+
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(section.index, section.offset);
+
+ // only print filename for a line if it is different from the previous one.
+ if (filename != prevFilename)
+ {
+ for (size_t i = 0, j = 0; i < lineFilename.checksumSize; i++, j+=2)
+ {
+ checksumString[j] = hexChars[lineFilename.checksum[i] >> 4];
+ checksumString[j+1] = hexChars[lineFilename.checksum[i] & 0xF];
+ }
+
+ checksumString[lineFilename.checksumSize * 2] = '\0';
+
+ printf(" line %u at [0x%08X][0x%04X:0x%08X], len = 0x%X %s (0x%02X: %s)\n",
+ line.lineNumber, rva, section.index, section.offset, line.codeSize,
+ filename, static_cast<uint32_t>(lineFilename.checksumKind), checksumString);
+
+ prevFilename = filename;
+ }
+ else
+ {
+ printf(" line %u at [0x%08X][0x%04X:0x%08X], len = 0x%X\n",
+ line.lineNumber, rva, section.index, section.offset, line.codeSize);
+ }
+ }
+#endif
+ }
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleMain.cpp b/thirdparty/raw_pdb/src/Examples/ExampleMain.cpp
new file mode 100644
index 000000000..b4249422f
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleMain.cpp
@@ -0,0 +1,200 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleMemoryMappedFile.h"
+#include "PDB.h"
+#include "PDB_RawFile.h"
+#include "PDB_InfoStream.h"
+#include "PDB_DBIStream.h"
+#include "PDB_TPIStream.h"
+#include "PDB_IPIStream.h"
+#include "PDB_NamesStream.h"
+
+namespace
+{
+ PDB_NO_DISCARD static bool IsError(PDB::ErrorCode errorCode)
+ {
+ switch (errorCode)
+ {
+ case PDB::ErrorCode::Success:
+ return false;
+
+ case PDB::ErrorCode::InvalidSuperBlock:
+ printf("Invalid Superblock\n");
+ return true;
+
+ case PDB::ErrorCode::InvalidFreeBlockMap:
+ printf("Invalid free block map\n");
+ return true;
+
+ case PDB::ErrorCode::InvalidStream:
+ printf("Invalid stream\n");
+ return true;
+
+ case PDB::ErrorCode::InvalidSignature:
+ printf("Invalid stream signature\n");
+ return true;
+
+ case PDB::ErrorCode::InvalidStreamIndex:
+ printf("Invalid stream index\n");
+ return true;
+
+ case PDB::ErrorCode::InvalidDataSize:
+ printf("Invalid data size\n");
+ return true;
+
+ case PDB::ErrorCode::UnknownVersion:
+ printf("Unknown version\n");
+ return true;
+ }
+
+ // only ErrorCode::Success means there wasn't an error, so all other paths have to assume there was an error
+ return true;
+ }
+
+ PDB_NO_DISCARD static bool HasValidDBIStreams(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
+ {
+ // check whether the DBI stream offers all sub-streams we need
+ if (IsError(dbiStream.HasValidSymbolRecordStream(rawPdbFile)))
+ {
+ return false;
+ }
+
+ if (IsError(dbiStream.HasValidPublicSymbolStream(rawPdbFile)))
+ {
+ return false;
+ }
+
+ if (IsError(dbiStream.HasValidGlobalSymbolStream(rawPdbFile)))
+ {
+ return false;
+ }
+
+ if (IsError(dbiStream.HasValidSectionContributionStream(rawPdbFile)))
+ {
+ return false;
+ }
+
+ if (IsError(dbiStream.HasValidImageSectionStream(rawPdbFile)))
+ {
+ return false;
+ }
+
+ return true;
+ }
+}
+
+
+// declare all examples
+extern void ExamplePDBSize(const PDB::RawFile&, const PDB::DBIStream&);
+extern void ExampleTPISize(const PDB::TPIStream& tpiStream, const char* outPath);
+extern void ExampleContributions(const PDB::RawFile&, const PDB::DBIStream&);
+extern void ExampleSymbols(const PDB::RawFile&, const PDB::DBIStream&);
+extern void ExampleFunctionSymbols(const PDB::RawFile&, const PDB::DBIStream&);
+extern void ExampleFunctionVariables(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::TPIStream&);
+extern void ExampleLines(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream, const PDB::InfoStream& infoStream);
+extern void ExampleTypes(const PDB::TPIStream&);
+extern void ExampleIPI(const PDB::RawFile& rawPdbFile, const PDB::InfoStream& infoStream, const PDB::TPIStream& tpiStream, const PDB::IPIStream& ipiStream);
+
+int main(int argc, char** argv)
+{
+ if (argc != 2)
+ {
+ printf("Usage: Examples <PDB path>\nError: Incorrect usage\n");
+
+ return 1;
+ }
+
+ printf("Opening PDB file %s\n", argv[1]);
+
+ // try to open the PDB file and check whether all the data we need is available
+ MemoryMappedFile::Handle pdbFile = MemoryMappedFile::Open(argv[1]);
+ if (!pdbFile.baseAddress)
+ {
+ printf("Cannot memory-map file %s\n", argv[1]);
+
+ return 1;
+ }
+
+ if (IsError(PDB::ValidateFile(pdbFile.baseAddress, pdbFile.len)))
+ {
+ MemoryMappedFile::Close(pdbFile);
+
+ return 2;
+ }
+
+ const PDB::RawFile rawPdbFile = PDB::CreateRawFile(pdbFile.baseAddress);
+ if (IsError(PDB::HasValidDBIStream(rawPdbFile)))
+ {
+ MemoryMappedFile::Close(pdbFile);
+
+ return 3;
+ }
+
+ const PDB::InfoStream infoStream(rawPdbFile);
+ if (infoStream.UsesDebugFastLink())
+ {
+ printf("PDB was linked using unsupported option /DEBUG:FASTLINK\n");
+
+ MemoryMappedFile::Close(pdbFile);
+
+ return 4;
+ }
+
+ const auto h = infoStream.GetHeader();
+ printf("Version %u, signature %u, age %u, GUID %08x-%04x-%04x-%02x%02x%02x%02x%02x%02x%02x%02x\n",
+ static_cast<uint32_t>(h->version), h->signature, h->age,
+ h->guid.Data1, h->guid.Data2, h->guid.Data3,
+ h->guid.Data4[0], h->guid.Data4[1], h->guid.Data4[2], h->guid.Data4[3], h->guid.Data4[4], h->guid.Data4[5], h->guid.Data4[6], h->guid.Data4[7]);
+
+ const PDB::DBIStream dbiStream = PDB::CreateDBIStream(rawPdbFile);
+ if (!HasValidDBIStreams(rawPdbFile, dbiStream))
+ {
+ MemoryMappedFile::Close(pdbFile);
+
+ return 5;
+ }
+
+ if (IsError(PDB::HasValidTPIStream(rawPdbFile)))
+ {
+ MemoryMappedFile::Close(pdbFile);
+
+ return 5;
+ }
+ const PDB::TPIStream tpiStream = PDB::CreateTPIStream(rawPdbFile);
+
+ PDB::IPIStream ipiStream;
+
+ // It's perfectly possible that an old PDB does not have an IPI stream.
+ if(infoStream.HasIPIStream())
+ {
+ PDB::ErrorCode error = PDB::HasValidIPIStream(rawPdbFile);
+
+ if (error != PDB::ErrorCode::InvalidStream && IsError(error))
+ {
+ MemoryMappedFile::Close(pdbFile);
+
+ return 5;
+ }
+
+ ipiStream = PDB::CreateIPIStream(rawPdbFile);
+ }
+
+
+ // run all examples
+ ExamplePDBSize(rawPdbFile, dbiStream);
+ ExampleContributions(rawPdbFile, dbiStream);
+ ExampleSymbols(rawPdbFile, dbiStream);
+ ExampleFunctionSymbols(rawPdbFile, dbiStream);
+ ExampleFunctionVariables(rawPdbFile, dbiStream, tpiStream);
+ ExampleLines(rawPdbFile, dbiStream, infoStream);
+ ExampleTypes(tpiStream);
+ ExampleIPI(rawPdbFile, infoStream, tpiStream, ipiStream);
+ // uncomment to dump type sizes to a CSV
+ // ExampleTPISize(tpiStream, "output.csv");
+
+ MemoryMappedFile::Close(pdbFile);
+
+ return 0;
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.cpp b/thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.cpp
new file mode 100644
index 000000000..4b46b4bab
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.cpp
@@ -0,0 +1,100 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleMemoryMappedFile.h"
+
+
+MemoryMappedFile::Handle MemoryMappedFile::Open(const char* path)
+{
+#ifdef _WIN32
+ void* file = CreateFileA(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, nullptr);
+
+ if (file == INVALID_HANDLE_VALUE)
+ {
+ return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
+ }
+
+ void* fileMapping = CreateFileMappingW(file, nullptr, PAGE_READONLY, 0, 0, nullptr);
+
+ if (fileMapping == nullptr)
+ {
+ CloseHandle(file);
+
+ return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
+ }
+
+ void* baseAddress = MapViewOfFile(fileMapping, FILE_MAP_READ, 0, 0, 0);
+
+ if (baseAddress == nullptr)
+ {
+ CloseHandle(fileMapping);
+ CloseHandle(file);
+
+ return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
+ }
+
+ BY_HANDLE_FILE_INFORMATION fileInformation;
+ const bool getInformationResult = GetFileInformationByHandle(file, &fileInformation);
+ if (!getInformationResult)
+ {
+ UnmapViewOfFile(baseAddress);
+ CloseHandle(fileMapping);
+ CloseHandle(file);
+
+ return Handle { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, nullptr, 0 };
+ }
+
+ const size_t fileSizeHighBytes = static_cast<size_t>(fileInformation.nFileSizeHigh) << 32;
+ const size_t fileSizeLowBytes = fileInformation.nFileSizeLow;
+ const size_t fileSize = fileSizeHighBytes | fileSizeLowBytes;
+ return Handle { file, fileMapping, baseAddress, fileSize };
+#else
+ struct stat fileSb;
+
+ int file = open(path, O_RDONLY);
+
+ if (file == INVALID_HANDLE_VALUE)
+ {
+ return Handle { INVALID_HANDLE_VALUE, nullptr, 0 };
+ }
+
+ if (fstat(file, &fileSb) == -1)
+ {
+ close(file);
+
+ return Handle { INVALID_HANDLE_VALUE, nullptr, 0 };
+ }
+
+ void* baseAddress = mmap(nullptr, fileSb.st_size, PROT_READ, MAP_PRIVATE, file, 0);
+
+ if (baseAddress == MAP_FAILED)
+ {
+ close(file);
+
+ return Handle { INVALID_HANDLE_VALUE, nullptr, 0 };
+ }
+
+ return Handle { file, baseAddress, static_cast<size_t>(fileSb.st_size) };
+#endif
+}
+
+
+void MemoryMappedFile::Close(Handle& handle)
+{
+#ifdef _WIN32
+ UnmapViewOfFile(handle.baseAddress);
+ CloseHandle(handle.fileMapping);
+ CloseHandle(handle.file);
+
+ handle.file = nullptr;
+ handle.fileMapping = nullptr;
+#else
+ munmap(handle.baseAddress, handle.len);
+ close(handle.file);
+
+ handle.file = 0;
+#endif
+
+ handle.baseAddress = nullptr;
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.h b/thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.h
new file mode 100644
index 000000000..c14575336
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleMemoryMappedFile.h
@@ -0,0 +1,29 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#ifndef _WIN32
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#define INVALID_HANDLE_VALUE ((long)-1)
+#endif
+
+namespace MemoryMappedFile
+{
+ struct Handle
+ {
+#ifdef _WIN32
+ void* file;
+ void* fileMapping;
+#else
+ int file;
+#endif
+ void* baseAddress;
+ size_t len;
+ };
+
+ Handle Open(const char* path);
+ void Close(Handle& handle);
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExamplePDBSize.cpp b/thirdparty/raw_pdb/src/Examples/ExamplePDBSize.cpp
new file mode 100644
index 000000000..c0a4dc6a4
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExamplePDBSize.cpp
@@ -0,0 +1,124 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "PDB_RawFile.h"
+#include "PDB_DBIStream.h"
+
+
+namespace
+{
+ struct Stream
+ {
+ std::string name;
+ uint32_t size;
+ };
+}
+
+
+void ExamplePDBSize(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
+void ExamplePDBSize(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
+{
+ TimedScope total("\nRunning example \"PDBSize\"");
+
+ std::vector<Stream> streams;
+
+ // print show general statistics
+ printf("General\n");
+ printf("-------\n");
+ {
+ const PDB::SuperBlock* superBlock = rawPdbFile.GetSuperBlock();
+ printf("PDB page size (block size): %u\n", superBlock->blockSize);
+ printf("PDB block count: %u\n", superBlock->blockCount);
+
+ const size_t rawSize = static_cast<size_t>(superBlock->blockSize) * static_cast<size_t>(superBlock->blockCount);
+ printf("PDB raw size: %zu MiB (%zu GiB)\n", rawSize >> 20u, rawSize >> 30u);
+ }
+
+ // print the sizes of all known streams
+ printf("\n");
+ printf("Sizes of known streams\n");
+ printf("----------------------\n");
+ {
+ const uint32_t streamCount = rawPdbFile.GetStreamCount();
+ const uint32_t tpiStreamSize = (streamCount > 2u) ? rawPdbFile.GetStreamSize(2u) : 0u;
+ const uint32_t dbiStreamSize = (streamCount > 3u) ? rawPdbFile.GetStreamSize(3u) : 0u;
+ const uint32_t ipiStreamSize = (streamCount > 4u) ? rawPdbFile.GetStreamSize(4u) : 0u;
+
+ printf("TPI stream size: %u KiB (%u MiB)\n", tpiStreamSize >> 10u, tpiStreamSize >> 20u);
+ printf("DBI stream size: %u KiB (%u MiB)\n", dbiStreamSize >> 10u, dbiStreamSize >> 20u);
+ printf("IPI stream size: %u KiB (%u MiB)\n", ipiStreamSize >> 10u, ipiStreamSize >> 20u);
+
+ streams.push_back(Stream { "TPI", tpiStreamSize });
+ streams.push_back(Stream { "DBI", dbiStreamSize });
+ streams.push_back(Stream { "IPI", ipiStreamSize });
+
+ const uint32_t globalSymbolStreamSize = rawPdbFile.GetStreamSize(dbiStream.GetHeader().globalStreamIndex);
+ const uint32_t publicSymbolStreamSize = rawPdbFile.GetStreamSize(dbiStream.GetHeader().publicStreamIndex);
+ const uint32_t symbolRecordStreamSize = rawPdbFile.GetStreamSize(dbiStream.GetHeader().symbolRecordStreamIndex);
+
+ printf("Global symbol stream size: %u KiB (%u MiB)\n", globalSymbolStreamSize >> 10u, globalSymbolStreamSize >> 20u);
+ printf("Public symbol stream size: %u KiB (%u MiB)\n", publicSymbolStreamSize >> 10u, publicSymbolStreamSize >> 20u);
+ printf("Symbol record stream size: %u KiB (%u MiB)\n", symbolRecordStreamSize >> 10u, symbolRecordStreamSize >> 20u);
+
+ streams.emplace_back(Stream { "Global", globalSymbolStreamSize });
+ streams.emplace_back(Stream { "Public", publicSymbolStreamSize });
+ streams.emplace_back(Stream { "Symbol", symbolRecordStreamSize });
+ }
+
+ // print the sizes of all module streams
+ printf("\n");
+ printf("Sizes of module streams\n");
+ printf("-----------------------\n");
+ {
+ const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
+ const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
+
+ for (const PDB::ModuleInfoStream::Module& module : modules)
+ {
+ const PDB::DBI::ModuleInfo* moduleInfo = module.GetInfo();
+ const char* name = module.GetName().Decay();
+ const char* objectName = module.GetObjectName().Decay();
+
+ const uint16_t streamIndex = module.HasSymbolStream() ? moduleInfo->moduleSymbolStreamIndex : 0u;
+ const uint32_t moduleStreamSize = (streamIndex != 0u) ? rawPdbFile.GetStreamSize(streamIndex) : 0u;
+
+ printf("Module %s (%s) stream size: %u KiB (%u MiB)\n", name, objectName, moduleStreamSize >> 10u, moduleStreamSize >> 20u);
+
+ streams.push_back(Stream { name, moduleStreamSize });
+ }
+ }
+
+ // sort the streams by their size
+ std::sort(streams.begin(), streams.end(), [](const Stream& lhs, const Stream& rhs)
+ {
+ return lhs.size > rhs.size;
+ });
+
+ // log the 20 largest stream
+ {
+ printf("\n");
+ printf("Sizes of 20 largest streams:\n");
+
+ const size_t countToShow = std::min<size_t>(20ul, streams.size());
+ for (size_t i = 0u; i < countToShow; ++i)
+ {
+ const Stream& stream = streams[i];
+ printf("%zu: %u KiB (%u MiB) from stream %s\n", i + 1u, stream.size >> 10u, stream.size >> 20u, stream.name.c_str());
+ }
+ }
+
+ // print the raw stream sizes
+ printf("\n");
+ printf("Raw sizes of all streams\n");
+ printf("------------------------\n");
+ {
+ const uint32_t streamCount = rawPdbFile.GetStreamCount();
+ for (uint32_t i = 0u; i < streamCount; ++i)
+ {
+ const uint32_t streamSize = rawPdbFile.GetStreamSize(i);
+ printf("Stream %u size: %u KiB (%u MiB)\n", i, streamSize >> 10u, streamSize >> 20u);
+ }
+ }
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleSymbols.cpp b/thirdparty/raw_pdb/src/Examples/ExampleSymbols.cpp
new file mode 100644
index 000000000..c1b2ef8ac
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleSymbols.cpp
@@ -0,0 +1,238 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "PDB_RawFile.h"
+#include "PDB_DBIStream.h"
+
+
+namespace
+{
+ // we don't have to store std::string in the symbols, since all the data is memory-mapped anyway.
+ // we do it in this example to ensure that we don't "cheat" when reading the PDB file. memory-mapped data will only
+ // be faulted into the process once it's touched, so actually copying the string data makes us touch the needed data,
+ // giving us a real performance measurement.
+ struct Symbol
+ {
+ std::string name;
+ uint32_t rva;
+ };
+}
+
+
+void ExampleSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream);
+void ExampleSymbols(const PDB::RawFile& rawPdbFile, const PDB::DBIStream& dbiStream)
+{
+ TimedScope total("\nRunning example \"Symbols\"");
+
+ // in order to keep the example easy to understand, we load the PDB data serially.
+ // note that this can be improved a lot by reading streams concurrently.
+
+ // prepare the image section stream first. it is needed for converting section + offset into an RVA
+ TimedScope sectionScope("Reading image section stream");
+ const PDB::ImageSectionStream imageSectionStream = dbiStream.CreateImageSectionStream(rawPdbFile);
+ sectionScope.Done();
+
+
+ // prepare the module info stream for matching contributions against files
+ TimedScope moduleScope("Reading module info stream");
+ const PDB::ModuleInfoStream moduleInfoStream = dbiStream.CreateModuleInfoStream(rawPdbFile);
+ moduleScope.Done();
+
+
+ // prepare symbol record stream needed by both public and global streams
+ TimedScope symbolStreamScope("Reading symbol record stream");
+ const PDB::CoalescedMSFStream symbolRecordStream = dbiStream.CreateSymbolRecordStream(rawPdbFile);
+ symbolStreamScope.Done();
+
+ std::vector<Symbol> symbols;
+
+ // read public symbols
+ TimedScope publicScope("Reading public symbol stream");
+ const PDB::PublicSymbolStream publicSymbolStream = dbiStream.CreatePublicSymbolStream(rawPdbFile);
+ publicScope.Done();
+ {
+ TimedScope scope("Storing public symbols");
+
+ const PDB::ArrayView<PDB::HashRecord> hashRecords = publicSymbolStream.GetRecords();
+ const size_t count = hashRecords.GetLength();
+
+ symbols.reserve(count);
+
+ for (const PDB::HashRecord& hashRecord : hashRecords)
+ {
+ const PDB::CodeView::DBI::Record* record = publicSymbolStream.GetRecord(symbolRecordStream, hashRecord);
+ if (record->header.kind != PDB::CodeView::DBI::SymbolRecordKind::S_PUB32)
+ {
+ // normally, a PDB only contains S_PUB32 symbols in the public symbol stream, but we have seen PDBs that also store S_CONSTANT as public symbols.
+ // ignore these.
+ continue;
+ }
+
+ const uint32_t rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_PUB32.section, record->data.S_PUB32.offset);
+ if (rva == 0u)
+ {
+ // certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
+ continue;
+ }
+
+ symbols.push_back(Symbol { record->data.S_PUB32.name, rva });
+ }
+
+ scope.Done(count);
+ }
+
+
+ // read global symbols
+ TimedScope globalScope("Reading global symbol stream");
+ const PDB::GlobalSymbolStream globalSymbolStream = dbiStream.CreateGlobalSymbolStream(rawPdbFile);
+ globalScope.Done();
+ {
+ TimedScope scope("Storing global symbols");
+
+ const PDB::ArrayView<PDB::HashRecord> hashRecords = globalSymbolStream.GetRecords();
+ const size_t count = hashRecords.GetLength();
+
+ symbols.reserve(symbols.size() + count);
+
+ for (const PDB::HashRecord& hashRecord : hashRecords)
+ {
+ const PDB::CodeView::DBI::Record* record = globalSymbolStream.GetRecord(symbolRecordStream, hashRecord);
+
+ const char* name = nullptr;
+ uint32_t rva = 0u;
+ if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GDATA32)
+ {
+ name = record->data.S_GDATA32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GDATA32.section, record->data.S_GDATA32.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GTHREAD32)
+ {
+ name = record->data.S_GTHREAD32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GTHREAD32.section, record->data.S_GTHREAD32.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LDATA32)
+ {
+ name = record->data.S_LDATA32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LDATA32.section, record->data.S_LDATA32.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LTHREAD32)
+ {
+ name = record->data.S_LTHREAD32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LTHREAD32.section, record->data.S_LTHREAD32.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_UDT)
+ {
+ name = record->data.S_UDT.name;
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_UDT_ST)
+ {
+ name = record->data.S_UDT_ST.name;
+ }
+
+ if (rva == 0u)
+ {
+ // certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
+ continue;
+ }
+
+ symbols.push_back(Symbol { name, rva });
+ }
+
+ scope.Done(count);
+ }
+
+
+ // read module symbols
+ {
+ TimedScope scope("Storing symbols from modules");
+
+ const PDB::ArrayView<PDB::ModuleInfoStream::Module> modules = moduleInfoStream.GetModules();
+
+ for (const PDB::ModuleInfoStream::Module& module : modules)
+ {
+ if (!module.HasSymbolStream())
+ {
+ continue;
+ }
+
+ const PDB::ModuleSymbolStream moduleSymbolStream = module.CreateSymbolStream(rawPdbFile);
+ moduleSymbolStream.ForEachSymbol([&symbols, &imageSectionStream](const PDB::CodeView::DBI::Record* record)
+ {
+ const char* name = nullptr;
+ uint32_t rva = 0u;
+ if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_THUNK32)
+ {
+ if (record->data.S_THUNK32.thunk == PDB::CodeView::DBI::ThunkOrdinal::TrampolineIncremental)
+ {
+ // we have never seen incremental linking thunks stored inside a S_THUNK32 symbol, but better be safe than sorry
+ name = "ILT";
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_THUNK32.section, record->data.S_THUNK32.offset);
+ }
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_TRAMPOLINE)
+ {
+ // incremental linking thunks are stored in the linker module
+ name = "ILT";
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_TRAMPOLINE.thunkSection, record->data.S_TRAMPOLINE.thunkOffset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_BLOCK32)
+ {
+ // blocks never store a name and are only stored for indicating whether other symbols are children of this block
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LABEL32)
+ {
+ // labels don't have a name
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32)
+ {
+ name = record->data.S_LPROC32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32.section, record->data.S_LPROC32.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32)
+ {
+ name = record->data.S_GPROC32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32.section, record->data.S_GPROC32.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LPROC32_ID)
+ {
+ name = record->data.S_LPROC32_ID.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LPROC32_ID.section, record->data.S_LPROC32_ID.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_GPROC32_ID)
+ {
+ name = record->data.S_GPROC32_ID.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_GPROC32_ID.section, record->data.S_GPROC32_ID.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_REGREL32)
+ {
+ name = record->data.S_REGREL32.name;
+ // You can only get the address while running the program by checking the register value and adding the offset
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LDATA32)
+ {
+ name = record->data.S_LDATA32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LDATA32.section, record->data.S_LDATA32.offset);
+ }
+ else if (record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_LTHREAD32)
+ {
+ name = record->data.S_LTHREAD32.name;
+ rva = imageSectionStream.ConvertSectionOffsetToRVA(record->data.S_LTHREAD32.section, record->data.S_LTHREAD32.offset);
+ }
+
+ if (rva == 0u)
+ {
+ // certain symbols (e.g. control-flow guard symbols) don't have a valid RVA, ignore those
+ return;
+ }
+
+ symbols.push_back(Symbol { name, rva });
+ });
+ }
+
+ scope.Done(modules.GetLength());
+ }
+
+ total.Done(symbols.size());
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleTimedScope.cpp b/thirdparty/raw_pdb/src/Examples/ExampleTimedScope.cpp
new file mode 100644
index 000000000..74b3fb04e
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleTimedScope.cpp
@@ -0,0 +1,54 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+
+namespace
+{
+ static unsigned int g_indent = 0u;
+
+ static void PrintIndent(void)
+ {
+ printf("%.*s", g_indent * 2u, "| | | | | | | | ");
+ }
+}
+
+
+TimedScope::TimedScope(const char* message)
+ : m_begin(std::chrono::high_resolution_clock::now())
+{
+ PrintIndent();
+ ++g_indent;
+
+ printf("%s\n", message);
+}
+
+
+void TimedScope::Done(void) const
+{
+ --g_indent;
+ PrintIndent();
+
+ const double milliSeconds = ReadMilliseconds();
+ printf("---> done in %.3fms\n", milliSeconds);
+}
+
+
+void TimedScope::Done(size_t count) const
+{
+ --g_indent;
+ PrintIndent();
+
+ const double milliSeconds = ReadMilliseconds();
+ printf("---> done in %.3fms (%zu elements)\n", milliSeconds, count);
+}
+
+
+double TimedScope::ReadMilliseconds(void) const
+{
+ const std::chrono::high_resolution_clock::time_point now = std::chrono::high_resolution_clock::now();
+ const std::chrono::duration<double> seconds = now - m_begin;
+
+ return seconds.count() * 1000.0;
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleTimedScope.h b/thirdparty/raw_pdb/src/Examples/ExampleTimedScope.h
new file mode 100644
index 000000000..0488dbbb1
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleTimedScope.h
@@ -0,0 +1,22 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Foundation/PDB_Macros.h"
+#include <chrono>
+
+
+class TimedScope
+{
+public:
+ explicit TimedScope(const char* message);
+
+ void Done(void) const;
+ void Done(size_t count) const;
+
+private:
+ double ReadMilliseconds(void) const;
+
+ const std::chrono::high_resolution_clock::time_point m_begin;
+
+ PDB_DISABLE_COPY_MOVE(TimedScope);
+};
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleTypeTable.cpp b/thirdparty/raw_pdb/src/Examples/ExampleTypeTable.cpp
new file mode 100644
index 000000000..260b4d2ca
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleTypeTable.cpp
@@ -0,0 +1,41 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
+#include "ExampleTypeTable.h"
+#include "Foundation/PDB_Memory.h"
+
+TypeTable::TypeTable(const PDB::TPIStream& tpiStream) PDB_NO_EXCEPT
+ : typeIndexBegin(tpiStream.GetFirstTypeIndex()), typeIndexEnd(tpiStream.GetLastTypeIndex()),
+ m_recordCount(tpiStream.GetTypeRecordCount())
+{
+ // Create coalesced stream from TPI stream, so the records can be referenced directly using pointers.
+ const PDB::DirectMSFStream& directStream = tpiStream.GetDirectMSFStream();
+ m_stream = PDB::CoalescedMSFStream(directStream, directStream.GetSize(), 0);
+
+ // types in the TPI stream are accessed by their index from other streams.
+ // however, the index is not stored with types in the TPI stream directly, but has to be built while walking the stream.
+ // similarly, because types are variable-length records, there are no direct offsets to access individual types.
+ // we therefore walk the TPI stream once, and store pointers to the records for trivial O(1) array lookup by index later.
+ m_records = PDB_NEW_ARRAY(const PDB::CodeView::TPI::Record*, m_recordCount);
+
+ // parse the CodeView records
+ uint32_t typeIndex = 0u;
+
+ tpiStream.ForEachTypeRecordHeaderAndOffset([this, &typeIndex](const PDB::CodeView::TPI::RecordHeader& header, size_t offset)
+ {
+ // The header includes the record kind and size, which can be stored along with offset
+ // to allow for lazy loading of the types on-demand directly from the TPIStream::GetDirectMSFStream()
+ // using DirectMSFStream::ReadAtOffset(...). Thus not needing a CoalescedMSFStream to look up the types.
+ (void)header;
+
+ const PDB::CodeView::TPI::Record* record = m_stream.GetDataAtOffset<const PDB::CodeView::TPI::Record>(offset);
+ m_records[typeIndex] = record;
+ ++typeIndex;
+ });
+}
+
+TypeTable::~TypeTable() PDB_NO_EXCEPT
+{
+ PDB_DELETE_ARRAY(m_records);
+}
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleTypeTable.h b/thirdparty/raw_pdb/src/Examples/ExampleTypeTable.h
new file mode 100644
index 000000000..7448952f3
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleTypeTable.h
@@ -0,0 +1,49 @@
+#pragma once
+
+#include "PDB_TPIStream.h"
+#include "PDB_CoalescedMSFStream.h"
+
+class TypeTable
+{
+public:
+ explicit TypeTable(const PDB::TPIStream& tpiStream) PDB_NO_EXCEPT;
+ ~TypeTable() PDB_NO_EXCEPT;
+
+ // Returns the index of the first type, which is not necessarily zero.
+ PDB_NO_DISCARD inline uint32_t GetFirstTypeIndex(void) const PDB_NO_EXCEPT
+ {
+ return typeIndexBegin;
+ }
+
+ // Returns the index of the last type.
+ PDB_NO_DISCARD inline uint32_t GetLastTypeIndex(void) const PDB_NO_EXCEPT
+ {
+ return typeIndexEnd;
+ }
+
+ PDB_NO_DISCARD inline const PDB::CodeView::TPI::Record* GetTypeRecord(uint32_t typeIndex) const PDB_NO_EXCEPT
+ {
+ if (typeIndex < typeIndexBegin || typeIndex > typeIndexEnd)
+ return nullptr;
+
+ return m_records[typeIndex - typeIndexBegin];
+ }
+
+ // Returns a view of all type records.
+ // Records identified by a type index can be accessed via "allRecords[typeIndex - firstTypeIndex]".
+ PDB_NO_DISCARD inline PDB::ArrayView<const PDB::CodeView::TPI::Record*> GetTypeRecords(void) const PDB_NO_EXCEPT
+ {
+ return PDB::ArrayView<const PDB::CodeView::TPI::Record*>(m_records, m_recordCount);
+ }
+
+private:
+ uint32_t typeIndexBegin;
+ uint32_t typeIndexEnd;
+
+ size_t m_recordCount;
+ const PDB::CodeView::TPI::Record **m_records;
+
+ PDB::CoalescedMSFStream m_stream;
+
+ PDB_DISABLE_COPY(TypeTable);
+};
diff --git a/thirdparty/raw_pdb/src/Examples/ExampleTypes.cpp b/thirdparty/raw_pdb/src/Examples/ExampleTypes.cpp
new file mode 100644
index 000000000..cd30b22a1
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/ExampleTypes.cpp
@@ -0,0 +1,1418 @@
+
+#include "Examples_PCH.h"
+#include "ExampleTimedScope.h"
+#include "ExampleTypeTable.h"
+#include "PDB_RawFile.h"
+#include "PDB_DBIStream.h"
+#include "PDB_TPIStream.h"
+#include <cstring>
+#include <cinttypes>
+
+// not all enumeration values are handled explicitly by some of the switch statements
+PDB_DISABLE_WARNING_MSVC(4061)
+PDB_DISABLE_WARNING_CLANG("-Wswitch-enum")
+
+// some format strings are not string literals
+PDB_DISABLE_WARNING_MSVC(4774)
+PDB_DISABLE_WARNING_CLANG("-Wformat-nonliteral")
+
+std::string GetTypeName(const TypeTable& typeTable, uint32_t typeIndex);
+
+static uint8_t GetLeafSize(PDB::CodeView::TPI::TypeRecordKind kind)
+{
+ if (kind < PDB::CodeView::TPI::TypeRecordKind::LF_NUMERIC)
+ {
+ // No leaf can have an index less than LF_NUMERIC (0x8000)
+ // so word is the value...
+ return sizeof(PDB::CodeView::TPI::TypeRecordKind);
+ }
+
+ switch (kind)
+ {
+ case PDB::CodeView::TPI::TypeRecordKind::LF_CHAR:
+ return sizeof(PDB::CodeView::TPI::TypeRecordKind) + sizeof(uint8_t);
+
+ case PDB::CodeView::TPI::TypeRecordKind::LF_USHORT:
+ case PDB::CodeView::TPI::TypeRecordKind::LF_SHORT:
+ return sizeof(PDB::CodeView::TPI::TypeRecordKind) + sizeof(uint16_t);
+
+ case PDB::CodeView::TPI::TypeRecordKind::LF_LONG:
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ULONG:
+ return sizeof(PDB::CodeView::TPI::TypeRecordKind) + sizeof(uint32_t);
+
+ case PDB::CodeView::TPI::TypeRecordKind::LF_QUADWORD:
+ case PDB::CodeView::TPI::TypeRecordKind::LF_UQUADWORD:
+ return sizeof(PDB::CodeView::TPI::TypeRecordKind) + sizeof(uint64_t);
+
+ default:
+ printf("Error! 0x%04x bogus type encountered, aborting...\n", PDB_AS_UNDERLYING(kind));
+ }
+ return 0;
+}
+
+
+static const char* GetLeafName(const char* data, PDB::CodeView::TPI::TypeRecordKind kind)
+{
+ return &data[GetLeafSize(kind)];
+}
+
+
+static const char* GetTypeName(const TypeTable& typeTable, uint32_t typeIndex, uint8_t& pointerLevel, const PDB::CodeView::TPI::Record** referencedType, const PDB::CodeView::TPI::Record** modifierRecord)
+{
+ const char* typeName = nullptr;
+ const PDB::CodeView::TPI::Record* underlyingType = nullptr;
+
+ if (referencedType)
+ *referencedType = nullptr;
+
+ if (modifierRecord)
+ *modifierRecord = nullptr;
+
+ auto typeIndexBegin = typeTable.GetFirstTypeIndex();
+ if (typeIndex < typeIndexBegin)
+ {
+ auto type = static_cast<PDB::CodeView::TPI::TypeIndexKind>(typeIndex);
+ switch (type)
+ {
+ case PDB::CodeView::TPI::TypeIndexKind::T_NOTYPE:
+ return "<NO TYPE>";
+ case PDB::CodeView::TPI::TypeIndexKind::T_HRESULT:
+ return "HRESULT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PHRESULT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PHRESULT:
+ return "PHRESULT";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_UNKNOWN_0600:
+ return "UNKNOWN_0x0600";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_VOID:
+ return "void";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PVOID:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PVOID:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PVOID:
+ return "PVOID";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PBOOL08:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PBOOL16:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PBOOL32:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PBOOL64:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PBOOL08:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PBOOL16:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PBOOL32:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PBOOL64:
+ return "PBOOL";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_BOOL08:
+ case PDB::CodeView::TPI::TypeIndexKind::T_BOOL16:
+ case PDB::CodeView::TPI::TypeIndexKind::T_BOOL32:
+ return "BOOL";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_RCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_CHAR:
+ return "CHAR";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PRCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PRCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PRCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PCHAR:
+ return "PCHAR";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_UCHAR:
+ return "UCHAR";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PUCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PUCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PUCHAR:
+ return "PUCHAR";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_WCHAR:
+ return "WCHAR";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PWCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PWCHAR:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PWCHAR:
+ return "PWCHAR";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_CHAR8:
+ return "CHAR8";
+ case PDB::CodeView::TPI::TypeIndexKind::T_PCHAR8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PFCHAR8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PHCHAR8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PCHAR8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PFCHAR8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PCHAR8:
+ return "PCHAR8";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_CHAR16:
+ return "CHAR16";
+ case PDB::CodeView::TPI::TypeIndexKind::T_PCHAR16:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PCHAR16:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PCHAR16:
+ return "PCHAR16";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_CHAR32:
+ return "CHAR32";
+ case PDB::CodeView::TPI::TypeIndexKind::T_PCHAR32:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PCHAR32:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PCHAR32:
+ return "PCHAR32";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_SHORT:
+ return "SHORT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PSHORT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PSHORT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PSHORT:
+ return "PSHORT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_USHORT:
+ return "USHORT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PUSHORT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PUSHORT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PUSHORT:
+ return "PUSHORT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_LONG:
+ return "LONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PLONG:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PLONG:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PLONG:
+ return "PLONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_ULONG:
+ return "ULONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PULONG:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PULONG:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PULONG:
+ return "PULONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_REAL32:
+ return "FLOAT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PREAL32:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PREAL32:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PREAL32:
+ return "PFLOAT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_REAL64:
+ return "DOUBLE";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PREAL64:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PREAL64:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PREAL64:
+ return "PDOUBLE";
+ case PDB::CodeView::TPI::TypeIndexKind::T_REAL80:
+ return "REAL80";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PREAL80:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PREAL80:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PREAL80:
+ return "PREAL80";
+ case PDB::CodeView::TPI::TypeIndexKind::T_QUAD:
+ return "LONGLONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PQUAD:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PQUAD:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PQUAD:
+ return "PLONGLONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_UQUAD:
+ return "ULONGLONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PUQUAD:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PUQUAD:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PUQUAD:
+ return "PULONGLONG";
+ case PDB::CodeView::TPI::TypeIndexKind::T_INT4:
+ return "INT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PINT4:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PINT4:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PINT4:
+ return "PINT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_UINT4:
+ return "UINT";
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PUINT4:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PUINT4:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PUINT4:
+ return "PUINT";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_UINT8:
+ return "UINT8";
+ case PDB::CodeView::TPI::TypeIndexKind::T_PUINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PFUINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PHUINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PUINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PFUINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PUINT8:
+ return "PUINT8";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_INT8:
+ return "INT8";
+ case PDB::CodeView::TPI::TypeIndexKind::T_PINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PFINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PHINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PFINT8:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PINT8:
+ return "PINT8";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_OCT:
+ return "OCTAL";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_POCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PFOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PHOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32POCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PFOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64POCT:
+ return "POCTAL";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_UOCT:
+ return "UOCTAL";
+
+ case PDB::CodeView::TPI::TypeIndexKind::T_PUOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PFUOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_PHUOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PUOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_32PFUOCT:
+ case PDB::CodeView::TPI::TypeIndexKind::T_64PUOCT:
+ return "PUOCTAL";
+
+ default:
+ PDB_ASSERT(false, "Unhandled special type 0x%X", typeIndex);
+ return "unhandled_special_type";
+ }
+ }
+ else
+ {
+ auto typeRecord = typeTable.GetTypeRecord(typeIndex);
+ if (!typeRecord)
+ return nullptr;
+
+ switch (typeRecord->header.kind)
+ {
+ case PDB::CodeView::TPI::TypeRecordKind::LF_MODIFIER:
+ if(modifierRecord)
+ *modifierRecord = typeRecord;
+ return GetTypeName(typeTable, typeRecord->data.LF_MODIFIER.type, pointerLevel, referencedType, nullptr);
+ case PDB::CodeView::TPI::TypeRecordKind::LF_POINTER:
+ ++pointerLevel;
+ if(referencedType)
+ *referencedType = typeRecord;
+ if (typeRecord->data.LF_POINTER.utype >= typeIndexBegin)
+ {
+ underlyingType = typeTable.GetTypeRecord(typeRecord->data.LF_POINTER.utype);
+ if (!underlyingType)
+ return nullptr;
+
+ if(underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_POINTER)
+ return GetTypeName(typeTable, typeRecord->data.LF_POINTER.utype, pointerLevel, referencedType, modifierRecord);
+
+ // Type record order can be LF_POINTER -> LF_MODIFIER -> LF_POINTER -> ...
+ if (underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_MODIFIER)
+ {
+ if (modifierRecord)
+ *modifierRecord = underlyingType;
+
+ return GetTypeName(typeTable, underlyingType->data.LF_MODIFIER.type, pointerLevel, referencedType, nullptr);
+ }
+ }
+
+ return GetTypeName(typeTable, typeRecord->data.LF_POINTER.utype, pointerLevel, &typeRecord, modifierRecord);
+ case PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE:
+ if (referencedType)
+ *referencedType = typeRecord;
+ return nullptr;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_BITFIELD:
+ if (typeRecord->data.LF_BITFIELD.type < typeIndexBegin)
+ {
+ typeName = GetTypeName(typeTable, typeRecord->data.LF_BITFIELD.type, pointerLevel, nullptr, modifierRecord);
+ if (referencedType)
+ *referencedType = typeRecord;
+ return typeName;
+ }
+ else
+ {
+ if (referencedType)
+ *referencedType = typeRecord;
+ return nullptr;
+ }
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ARRAY:
+ if (referencedType)
+ *referencedType = typeRecord;
+ return GetTypeName(typeTable, typeRecord->data.LF_ARRAY.elemtype, pointerLevel, &typeRecord, modifierRecord);
+ case PDB::CodeView::TPI::TypeRecordKind::LF_CLASS:
+ case PDB::CodeView::TPI::TypeRecordKind::LF_STRUCTURE:
+ return GetLeafName(typeRecord->data.LF_CLASS.data, typeRecord->header.kind);
+
+ case PDB::CodeView::TPI::TypeRecordKind::LF_CLASS2:
+ case PDB::CodeView::TPI::TypeRecordKind::LF_STRUCTURE2:
+ return GetLeafName(typeRecord->data.LF_CLASS2.data, typeRecord->header.kind);
+
+ case PDB::CodeView::TPI::TypeRecordKind::LF_UNION:
+ return GetLeafName(typeRecord->data.LF_UNION.data, typeRecord->header.kind);
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ENUM:
+ return &typeRecord->data.LF_ENUM.name[0];
+ case PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION:
+ if (referencedType)
+ *referencedType = typeRecord;
+ return nullptr;
+
+ default:
+ PDB_ASSERT(false, "Unhandled TypeRecordKind 0x%X", static_cast<uint16_t>(typeRecord->header.kind));
+ break;
+ }
+
+ }
+
+ return "unknown_type";
+}
+
+static const char* GetModifierName(const PDB::CodeView::TPI::Record* modifierRecord)
+{
+ if (modifierRecord->data.LF_MODIFIER.attr.MOD_const)
+ return "const";
+ else if (modifierRecord->data.LF_MODIFIER.attr.MOD_volatile)
+ return "volatile";
+ else if (modifierRecord->data.LF_MODIFIER.attr.MOD_unaligned)
+ return "unaligned";
+
+ return "";
+}
+
+static bool GetMethodPrototype(const TypeTable& typeTable, const PDB::CodeView::TPI::Record* methodRecord, std::string& methodPrototype);
+
+static bool GetFunctionPrototype(const TypeTable& typeTable, const PDB::CodeView::TPI::Record* functionRecord, std::string& functionPrototype)
+{
+ PDB_ASSERT(functionRecord->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE, "TPI Record kind is 0x%X, expected 0x%X (LF_PROCEDURE)",
+ (uint32_t)functionRecord->header.kind, (uint32_t)PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE);
+
+ std::string underlyingTypePrototype;
+
+ size_t markerPos = 0;
+ uint8_t pointerLevel = 0;
+ const PDB::CodeView::TPI::Record* referencedType = nullptr;
+ const PDB::CodeView::TPI::Record* underlyingType = nullptr;
+ const PDB::CodeView::TPI::Record* modifierRecord = nullptr;
+
+ functionPrototype.clear();
+
+ auto typeName = GetTypeName(typeTable, functionRecord->data.LF_PROCEDURE.rvtype, pointerLevel, &referencedType, &modifierRecord);
+ if (typeName)
+ {
+ if (modifierRecord)
+ {
+ functionPrototype += GetModifierName(modifierRecord);
+ functionPrototype += ' ';
+ }
+
+ functionPrototype += typeName;
+
+ for (size_t i = 0; i < pointerLevel; i++)
+ functionPrototype += '*';
+ }
+ else
+ {
+ PDB_ASSERT(referencedType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_POINTER, "Referenced type kind 0x%X != LF_POINTER (0x%X)", (uint32_t)referencedType->header.kind, (uint32_t)PDB::CodeView::TPI::TypeRecordKind::LF_POINTER);
+
+ underlyingType = typeTable.GetTypeRecord(referencedType->data.LF_POINTER.utype);
+ if (!underlyingType)
+ return false;
+
+ if (underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE)
+ {
+ if (!GetFunctionPrototype(typeTable, underlyingType, underlyingTypePrototype))
+ return false;
+ }
+ else if (underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION)
+ {
+ if (!GetMethodPrototype(typeTable, underlyingType, underlyingTypePrototype))
+ return false;
+ }
+ else
+ {
+ PDB_ASSERT(false, "Unhandled underlyingType kind 0x%X", (uint32_t)underlyingType->header.kind);
+ }
+
+ markerPos = underlyingTypePrototype.find("%s");
+ underlyingTypePrototype.erase(markerPos, 2);
+ functionPrototype = underlyingTypePrototype;
+ }
+
+ functionPrototype += " (*%s)(";
+
+ if (functionRecord->data.LF_PROCEDURE.parmcount)
+ {
+ auto argList = typeTable.GetTypeRecord(functionRecord->data.LF_PROCEDURE.arglist);
+ if (!argList)
+ return false;
+
+ for (size_t i = 0; i < argList->data.LF_ARGLIST.count; i++)
+ {
+ pointerLevel = 0;
+ typeName = GetTypeName(typeTable, argList->data.LF_ARGLIST.arg[i], pointerLevel, &referencedType, &modifierRecord);
+ if (referencedType)
+ {
+ if (referencedType->data.LF_POINTER.utype >= typeTable.GetFirstTypeIndex())
+ {
+ underlyingType = typeTable.GetTypeRecord(referencedType->data.LF_POINTER.utype);
+ if (!underlyingType)
+ return false;
+ }
+
+ if (!underlyingType || (underlyingType->header.kind != PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE && underlyingType->header.kind != PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION))
+ {
+ if (modifierRecord)
+ {
+ functionPrototype += GetModifierName(modifierRecord);
+ functionPrototype += ' ';
+ }
+
+ functionPrototype += typeName;
+ functionPrototype += '*';
+
+ if (referencedType->data.LF_POINTER.attr.isvolatile)
+ functionPrototype += "volatile";
+ else if (referencedType->data.LF_POINTER.attr.isconst)
+ functionPrototype += "const";
+ }
+ else if(underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE)
+ {
+ if (!GetFunctionPrototype(typeTable, underlyingType, underlyingTypePrototype))
+ return false;
+
+ markerPos = underlyingTypePrototype.find("%s");
+ underlyingTypePrototype.erase(markerPos, 2);
+
+ for (size_t j = 1; j < pointerLevel; j++)
+ underlyingTypePrototype.insert(markerPos, 1, '*');
+
+ functionPrototype += underlyingTypePrototype;
+ }
+ else if(underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION)
+ {
+ functionPrototype += GetTypeName(typeTable, argList->data.LF_ARGLIST.arg[i]);
+ }
+ }
+ else
+ {
+ functionPrototype += typeName;
+ }
+
+ if (i < (argList->data.LF_ARGLIST.count - 1))
+ functionPrototype += ", ";
+ }
+ }
+
+ functionPrototype += ')';
+
+ return true;
+}
+
+
+static bool GetMethodPrototype(const TypeTable& typeTable, const PDB::CodeView::TPI::Record* methodRecord, std::string& methodPrototype)
+{
+ PDB_ASSERT(methodRecord->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION, "TPI Record kind is 0x%X, expected 0x%X (LF_MFUNCTION)",
+ (uint32_t)methodRecord->header.kind, (uint32_t)PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION);
+
+ std::string underlyingTypePrototype;
+
+ size_t markerPos = 0;
+ uint8_t pointerLevel = 0;
+ const PDB::CodeView::TPI::Record* referencedType = nullptr;
+ const PDB::CodeView::TPI::Record* underlyingType = nullptr;
+ const PDB::CodeView::TPI::Record* modifierRecord = nullptr;
+
+ methodPrototype.clear();
+
+ auto typeName = GetTypeName(typeTable, methodRecord->data.LF_MFUNCTION.rvtype, pointerLevel, &referencedType, &modifierRecord);
+ if (typeName)
+ {
+ if (modifierRecord)
+ {
+ methodPrototype += GetModifierName(modifierRecord);
+ methodPrototype += ' ';
+ }
+
+ methodPrototype += typeName;
+
+ for (size_t i = 0; i < pointerLevel; i++)
+ methodPrototype += '*';
+ }
+ else
+ {
+ underlyingType = typeTable.GetTypeRecord(referencedType->data.LF_POINTER.utype);
+ if (!underlyingType)
+ return false;
+
+ if (underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE)
+ {
+ if (!GetFunctionPrototype(typeTable, underlyingType, underlyingTypePrototype))
+ return false;
+ }
+ else if(underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION)
+ {
+ if (!GetMethodPrototype(typeTable, underlyingType, underlyingTypePrototype))
+ return false;
+ }
+ else
+ {
+ PDB_ASSERT(false, "Unhandled underlyingType kind 0x%X", (uint32_t)underlyingType->header.kind);
+ }
+
+ markerPos = underlyingTypePrototype.find("%s");
+ underlyingTypePrototype.erase(markerPos, 2);
+ methodPrototype = underlyingTypePrototype;
+ }
+
+ methodPrototype += " %s(";
+
+ if (methodRecord->data.LF_MFUNCTION.parmcount)
+ {
+ auto argList = typeTable.GetTypeRecord(methodRecord->data.LF_MFUNCTION.arglist);
+ if (!argList)
+ return false;
+
+ for (size_t i = 0; i < argList->data.LF_ARGLIST.count; i++)
+ {
+ pointerLevel = 0;
+ typeName = GetTypeName(typeTable, argList->data.LF_ARGLIST.arg[i], pointerLevel, &referencedType, &modifierRecord);
+ if (referencedType)
+ {
+ if (referencedType->data.LF_POINTER.utype >= typeTable.GetFirstTypeIndex())
+ {
+ underlyingType = typeTable.GetTypeRecord(referencedType->data.LF_POINTER.utype);
+ if (!underlyingType)
+ return false;
+ }
+
+ if (!underlyingType || (underlyingType->header.kind != PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE && underlyingType->header.kind != PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION))
+ {
+ if (modifierRecord)
+ {
+ methodPrototype += GetModifierName(modifierRecord);
+ methodPrototype += ' ';
+ }
+
+ if(typeName)
+ methodPrototype += typeName;
+
+ methodPrototype += '*';
+
+ if (referencedType->data.LF_POINTER.attr.isvolatile)
+ methodPrototype += "volatile";
+ else if (referencedType->data.LF_POINTER.attr.isconst)
+ methodPrototype += "const";
+ }
+ else if (underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE)
+ {
+ if (!GetFunctionPrototype(typeTable, underlyingType, underlyingTypePrototype))
+ return false;
+
+ markerPos = underlyingTypePrototype.find("%s");
+ underlyingTypePrototype.erase(markerPos, 2);
+
+ for (size_t j = 1; j < pointerLevel; j++)
+ underlyingTypePrototype.insert(markerPos, 1, '*');
+
+ methodPrototype += underlyingTypePrototype;
+ }
+ else if (underlyingType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION)
+ {
+ methodPrototype += GetTypeName(typeTable, argList->data.LF_ARGLIST.arg[i]);
+ }
+ }
+ else
+ {
+ methodPrototype += typeName;
+ }
+
+ if (i < (argList->data.LF_ARGLIST.count - 1))
+ methodPrototype += ", ";
+ }
+ }
+
+ methodPrototype += ')';
+
+ return true;
+}
+
+
+static const char* GetMethodName(const PDB::CodeView::TPI::FieldList* fieldRecord)
+{
+ auto methodAttributes = static_cast<PDB::CodeView::TPI::MethodProperty>(fieldRecord->data.LF_ONEMETHOD.attributes.mprop);
+ switch (methodAttributes)
+ {
+ case PDB::CodeView::TPI::MethodProperty::Intro:
+ case PDB::CodeView::TPI::MethodProperty::PureIntro:
+ return &reinterpret_cast<const char*>(fieldRecord->data.LF_ONEMETHOD.vbaseoff)[sizeof(uint32_t)];
+ default:
+ break;
+ }
+
+ return &reinterpret_cast<const char*>(fieldRecord->data.LF_ONEMETHOD.vbaseoff)[0];
+}
+
+
+static void DisplayFields(const TypeTable& typeTable, const PDB::CodeView::TPI::Record* record)
+{
+ const PDB::CodeView::TPI::Record* referencedType = nullptr;
+ const PDB::CodeView::TPI::Record* underlyingType = nullptr;
+ const PDB::CodeView::TPI::Record* modifierRecord = nullptr;
+ const char* leafName = nullptr;
+ const char* typeName = nullptr;
+ std::string functionPrototype;
+ uint16_t offset = 0;
+
+ auto maximumSize = record->header.size - sizeof(uint16_t);
+
+ for (size_t i = 0; i < maximumSize;)
+ {
+ uint8_t pointerLevel = 0;
+ auto fieldRecord = reinterpret_cast<const PDB::CodeView::TPI::FieldList*>(reinterpret_cast<const uint8_t*>(&record->data.LF_FIELD.list) + i);
+
+ // Other kinds of records are not implemented
+ PDB_ASSERT(
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_BCLASS ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VBCLASS ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_IVBCLASS ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_INDEX ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VFUNCTAB ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_NESTTYPE ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ENUM ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_MEMBER ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_STMEMBER ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_METHOD ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ONEMETHOD,
+ "Unknown record kind %X",
+ static_cast<unsigned int>(fieldRecord->kind));
+
+ if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_MEMBER)
+ {
+ if (fieldRecord->data.LF_MEMBER.lfEasy.kind < PDB::CodeView::TPI::TypeRecordKind::LF_NUMERIC)
+ offset = *reinterpret_cast<const uint16_t*>(&fieldRecord->data.LF_MEMBER.offset[0]);
+ else
+ offset = *reinterpret_cast<const uint16_t*>(&fieldRecord->data.LF_MEMBER.offset[sizeof(PDB::CodeView::TPI::TypeRecordKind)]);
+
+ leafName = GetLeafName(fieldRecord->data.LF_MEMBER.offset, fieldRecord->data.LF_MEMBER.lfEasy.kind);
+
+ typeName = GetTypeName(typeTable, fieldRecord->data.LF_MEMBER.index, pointerLevel, &referencedType, &modifierRecord);
+ if (referencedType)
+ {
+ switch (referencedType->header.kind)
+ {
+ case PDB::CodeView::TPI::TypeRecordKind::LF_POINTER:
+ if (referencedType->data.LF_POINTER.utype >= typeTable.GetFirstTypeIndex())
+ {
+ underlyingType = typeTable.GetTypeRecord(referencedType->data.LF_POINTER.utype);
+ if (!underlyingType)
+ break;
+
+ if (underlyingType->header.kind != PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE)
+ {
+ if (modifierRecord)
+ printf("[0x%X]%s %s", offset, GetModifierName(modifierRecord), typeName);
+ else
+ printf("[0x%X]%s", offset, typeName);
+
+ for (size_t j = 0; j < pointerLevel; j++)
+ printf("*");
+
+ printf(" %s\n", leafName);
+ }
+ else
+ {
+ if (!GetFunctionPrototype(typeTable, underlyingType, functionPrototype))
+ break;
+
+ printf("[0x%X]", offset);
+ printf(functionPrototype.c_str(), leafName);
+ printf("\n");
+ }
+ }
+ else
+ {
+ printf("[0x%X]%s", offset, typeName);
+
+ for (size_t j = 0; j < pointerLevel; j++)
+ printf("*");
+
+ if (referencedType->data.LF_POINTER.attr.isvolatile)
+ printf(" volatile");
+ else if (referencedType->data.LF_POINTER.attr.isconst)
+ printf(" const");
+
+ printf(" %s\n", leafName);
+ }
+ break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_BITFIELD:
+ if (typeName)
+ {
+ printf("[0x%X]%s %s : %d\n",
+ offset,
+ typeName,
+ leafName,
+ referencedType->data.LF_BITFIELD.length);
+ }
+ else
+ {
+ modifierRecord = typeTable.GetTypeRecord(referencedType->data.LF_BITFIELD.type);
+ if (!modifierRecord)
+ break;
+
+ printf("[0x%X]%s %s %s : %d\n",
+ offset,
+ GetModifierName(modifierRecord),
+ GetTypeName(typeTable, modifierRecord->data.LF_MODIFIER.type, pointerLevel, nullptr, nullptr),
+ leafName,
+ referencedType->data.LF_BITFIELD.length);
+ }
+ break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ARRAY:
+ if (!modifierRecord)
+ {
+ printf("[0x%X]%s %s[] /*0x%X*/\n",
+ offset,
+ typeName,
+ leafName,
+ *reinterpret_cast<const uint16_t*>(referencedType->data.LF_ARRAY.data));
+ }
+ else
+ {
+ printf("[0x%X]%s %s %s[] /*0x%X*/\n",
+ offset,
+ GetModifierName(modifierRecord),
+ GetTypeName(typeTable, modifierRecord->data.LF_MODIFIER.type, pointerLevel, nullptr, nullptr),
+ leafName,
+ *reinterpret_cast<const uint16_t*>(referencedType->data.LF_ARRAY.data));
+ }
+ break;
+ default:
+ break;
+ }
+ }
+ else
+ {
+ if (modifierRecord)
+ printf("[0x%X]%s %s %s\n", offset, GetModifierName(modifierRecord), typeName, leafName);
+ else
+ printf("[0x%X]%s %s\n", offset, typeName, leafName);
+ }
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_NESTTYPE)
+ {
+ leafName = &fieldRecord->data.LF_NESTTYPE.name[0];
+ typeName = GetTypeName(typeTable, fieldRecord->data.LF_NESTTYPE.index, pointerLevel, &referencedType, &modifierRecord);
+
+ printf("%s %s\n", typeName, leafName);
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_STMEMBER)
+ {
+ leafName = &fieldRecord->data.LF_STMEMBER.name[0];
+ typeName = GetTypeName(typeTable, fieldRecord->data.LF_STMEMBER.index, pointerLevel, &referencedType, &modifierRecord);
+
+ if (!modifierRecord)
+ printf("%s %s\n", typeName, leafName);
+ else
+ printf("%s %s %s\n", GetModifierName(modifierRecord), typeName, leafName);
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_METHOD)
+ {
+ leafName = fieldRecord->data.LF_METHOD.name;
+
+ auto methodList = typeTable.GetTypeRecord(fieldRecord->data.LF_METHOD.mList);
+ if (!methodList)
+ break;
+
+ // https://github.com/microsoft/microsoft-pdb/blob/master/PDB/include/symtypeutils.h#L220
+ size_t offsetInMethodList = 0;
+ for (size_t j = 0; j < fieldRecord->data.LF_METHOD.count; j++)
+ {
+ size_t entrySize = 2 * sizeof(uint32_t);
+ const PDB::CodeView::TPI::MethodListEntry* entry = (const PDB::CodeView::TPI::MethodListEntry*)(methodList->data.LF_METHODLIST.mList + offsetInMethodList);
+ if (!GetMethodPrototype(typeTable, typeTable.GetTypeRecord(entry->index), functionPrototype))
+ break;
+ printf(functionPrototype.c_str(), leafName);
+ printf("\n");
+ PDB::CodeView::TPI::MethodProperty methodProp = (PDB::CodeView::TPI::MethodProperty)entry->attributes.mprop;
+ if (methodProp == PDB::CodeView::TPI::MethodProperty::Intro || methodProp == PDB::CodeView::TPI::MethodProperty::PureIntro)
+ entrySize += sizeof(uint32_t);
+ offsetInMethodList += entrySize;
+ }
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ONEMETHOD)
+ {
+ leafName = GetMethodName(fieldRecord);
+
+ referencedType = typeTable.GetTypeRecord(fieldRecord->data.LF_ONEMETHOD.index);
+ if (!referencedType)
+ break;
+
+ if (!GetMethodPrototype(typeTable, referencedType, functionPrototype))
+ break;
+
+ printf(functionPrototype.c_str(), leafName);
+ printf("\n");
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_BCLASS)
+ {
+ leafName = GetLeafName(fieldRecord->data.LF_BCLASS.offset, fieldRecord->data.LF_BCLASS.lfEasy.kind);
+
+ i += static_cast<size_t>(leafName - reinterpret_cast<const char*>(fieldRecord));
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VBCLASS || fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_IVBCLASS)
+ {
+ // virtual base pointer offset from address point
+ // followed by virtual base offset from vbtable
+
+ const PDB::CodeView::TPI::TypeRecordKind vbpOffsetAddressPointKind = *(const PDB::CodeView::TPI::TypeRecordKind*)(fieldRecord->data.LF_IVBCLASS.vbpOffset);
+ const uint8_t vbpOffsetAddressPointSize = GetLeafSize(vbpOffsetAddressPointKind);
+
+ const PDB::CodeView::TPI::TypeRecordKind vbpOffsetVBTableKind = *(const PDB::CodeView::TPI::TypeRecordKind*)(fieldRecord->data.LF_IVBCLASS.vbpOffset + vbpOffsetAddressPointSize);
+ const uint8_t vbpOffsetVBTableSize = GetLeafSize(vbpOffsetVBTableKind);
+
+ i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_VBCLASS);
+ i += vbpOffsetAddressPointSize + vbpOffsetVBTableSize;
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_INDEX)
+ {
+ i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_INDEX);
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VFUNCTAB)
+ {
+ i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_VFUNCTAB);
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else
+ {
+ break;
+ }
+
+ i += static_cast<size_t>(leafName - reinterpret_cast<const char*>(fieldRecord));
+ i += strnlen(leafName, maximumSize - i - 1) + 1;
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ }
+}
+
+// Used in ExamplesFunctionVariables
+std::string GetTypeName(const TypeTable& typeTable, uint32_t typeIndex)
+{
+ uint8_t pointerLevel = 0;
+ const PDB::CodeView::TPI::Record* referencedType = nullptr;
+ const PDB::CodeView::TPI::Record* modifierRecord = nullptr;
+
+ const char* typeName = GetTypeName(typeTable, typeIndex, pointerLevel, &referencedType, &modifierRecord);
+
+ if (typeName == nullptr)
+ {
+ if (referencedType == nullptr && (typeIndex & 0x80000000) != 0)
+ {
+ // d3d12.pdb\1DEAE23C86E6462A86018FB180EB8E4A1, S_CALLSITE for `dynamic initializer for 'g_Telemetry'': typeIndex == 0x80900001
+ char typeIndexBuf[0x0C];
+ sprintf_s(typeIndexBuf, sizeof(typeIndexBuf), "%08X", typeIndex);
+ return std::string("<BAD_TYPE_INDEX:0x") + typeIndexBuf + ">";
+ }
+ PDB_ASSERT(referencedType != nullptr, "Neither typeName nor referencedType are set.");
+
+ if (referencedType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_POINTER)
+ {
+ std::string pointerType = GetTypeName(typeTable, referencedType->data.LF_POINTER.utype);
+
+ for (size_t i = 0; i < pointerLevel; i++)
+ pointerType += '*';
+
+ return pointerType;
+ }
+ else if (referencedType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_ARRAY)
+ {
+ const std::string elementType = GetTypeName(typeTable, referencedType->data.LF_ARRAY.elemtype);
+ const std::string indexType = GetTypeName(typeTable, referencedType->data.LF_ARRAY.idxtype);
+
+ return elementType + "[" + indexType + "]";
+ }
+ else if (referencedType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE)
+ {
+ std::string functionPrototype;
+
+ if (!GetFunctionPrototype(typeTable, referencedType, functionPrototype))
+ {
+ PDB_ASSERT(false, "Resolving function prototype failed");
+ return "resolving function type failed";
+ }
+
+ return functionPrototype;
+ }
+ else if (referencedType->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION)
+ {
+ std::string methodPrototype;
+
+ if (!GetMethodPrototype(typeTable, referencedType, methodPrototype))
+ {
+ PDB_ASSERT(false, "Resolving method prototype failed");
+ return "resolving method type failed";
+ }
+
+ std::string classTypeName = GetTypeName(typeTable, referencedType->data.LF_MFUNCTION.classtype);
+ classTypeName += "::*";
+
+ const int stringLength = std::snprintf(nullptr, 0, methodPrototype.c_str(), classTypeName.c_str());
+ PDB_ASSERT(stringLength > 0, "String length %i <= 0", stringLength);
+
+ std::vector<char> resultString(static_cast<size_t>(stringLength) + 1u);
+
+ std::snprintf(&resultString[0], resultString.size(), methodPrototype.c_str(), classTypeName.c_str());
+
+ return std::string(resultString.data());
+ }
+ else
+ {
+ PDB_ASSERT(false, "Unhandled referencedType kind 0x%X", static_cast<uint16_t>(referencedType->header.kind));
+ return "not found";
+ }
+ }
+
+ return typeName;
+}
+
+static void DisplayEnumerates(const PDB::CodeView::TPI::Record* record, uint8_t underlyingTypeSize)
+{
+ const char* leafName = nullptr;
+ uint64_t value = 0;
+ const char* valuePtr = nullptr;
+
+ auto maximumSize = record->header.size - sizeof(uint16_t);
+
+ for (size_t i = 0; i < maximumSize;)
+ {
+ auto fieldRecord = reinterpret_cast<const PDB::CodeView::TPI::FieldList*>(reinterpret_cast<const uint8_t*>(&record->data.LF_FIELD.list) + i);
+
+ leafName = GetLeafName(fieldRecord->data.LF_ENUMERATE.value, fieldRecord->data.LF_ENUMERATE.lfEasy.kind);
+
+ if (fieldRecord->data.LF_ENUMERATE.lfEasy.kind < PDB::CodeView::TPI::TypeRecordKind::LF_NUMERIC)
+ valuePtr = &fieldRecord->data.LF_ENUMERATE.value[0];
+ else
+ valuePtr = &fieldRecord->data.LF_ENUMERATE.value[sizeof(PDB::CodeView::TPI::TypeRecordKind)];
+
+ switch (underlyingTypeSize)
+ {
+ case 1:
+ value = *reinterpret_cast<const uint8_t*>(&fieldRecord->data.LF_ENUMERATE.value[0]);
+ break;
+ case 2:
+ value = *reinterpret_cast<const uint16_t*>(&fieldRecord->data.LF_ENUMERATE.value[0]);
+ break;
+ case 4:
+ value = *reinterpret_cast<const uint32_t*>(&fieldRecord->data.LF_ENUMERATE.value[0]);
+ break;
+ case 8:
+ value = *reinterpret_cast<const uint64_t*>(&fieldRecord->data.LF_ENUMERATE.value[0]);
+ break;
+ default:
+ break;
+ }
+
+ printf("%s = %" PRIu64 "\n", leafName, value);
+
+ i += static_cast<size_t>(leafName - reinterpret_cast<const char*>(fieldRecord));
+ i += strnlen(leafName, maximumSize - i - 1) + 1;
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+
+ (void)valuePtr;
+ }
+}
+
+
+void ExampleTypes(const PDB::TPIStream& tpiStream);
+void ExampleTypes(const PDB::TPIStream& tpiStream)
+{
+ TimedScope total("\nRunning example \"Function types\"");
+
+ TimedScope typeTableScope("Create TypeTable");
+ TypeTable typeTable(tpiStream);
+ typeTableScope.Done();
+
+ for (const auto& record : typeTable.GetTypeRecords())
+ {
+ if ((record->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_CLASS) || (record->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_STRUCTURE))
+ {
+ if (record->data.LF_CLASS.property.fwdref)
+ continue;
+
+ auto typeRecord = typeTable.GetTypeRecord(record->data.LF_CLASS.field);
+ if (!typeRecord)
+ continue;
+
+ auto leafName = GetLeafName(record->data.LF_CLASS.data, record->data.LF_CLASS.lfEasy.kind);
+
+ printf("struct %s\n{\n", leafName);
+
+ DisplayFields(typeTable, typeRecord);
+
+ printf("}\n");
+ }
+ else if (record->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_UNION)
+ {
+ if (record->data.LF_UNION.property.fwdref)
+ continue;
+
+ auto typeRecord = typeTable.GetTypeRecord(record->data.LF_UNION.field);
+ if (!typeRecord)
+ continue;
+
+ auto leafName = GetLeafName(record->data.LF_UNION.data, static_cast<PDB::CodeView::TPI::TypeRecordKind>(0));
+
+ printf("union %s\n{\n", leafName);
+
+ DisplayFields(typeTable, typeRecord);
+
+ printf("}\n");
+ }
+ else if (record->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_ENUM)
+ {
+ if (record->data.LF_ENUM.property.fwdref)
+ continue;
+
+ auto typeRecord = typeTable.GetTypeRecord(record->data.LF_ENUM.field);
+ if (!typeRecord)
+ continue;
+
+ printf("enum %s\n{\n", record->data.LF_ENUM.name);
+
+ DisplayEnumerates(typeRecord, GetLeafSize(static_cast<PDB::CodeView::TPI::TypeRecordKind>(record->data.LF_ENUM.utype)));
+
+ printf("}\n");
+ }
+ }
+
+ total.Done(tpiStream.GetTypeRecordCount());
+}
+
+template<typename T>
+static void TagRecursively(const TypeTable& typeTable, uint32_t typeIndex, T setName);
+
+#define TAG_AND_CHECK(typeIndex) if (setName(typeIndex)) TagRecursively(typeTable, typeIndex, setName)
+
+template<typename T>
+static void TagChildren(const TypeTable& typeTable, const PDB::CodeView::TPI::Record* record, T setName)
+{
+ const char* leafName = nullptr;
+
+ auto maximumSize = record->header.size - sizeof(uint16_t);
+
+ for (size_t i = 0; i < maximumSize;)
+ {
+ auto fieldRecord = reinterpret_cast<const PDB::CodeView::TPI::FieldList*>(reinterpret_cast<const uint8_t*>(&record->data.LF_FIELD.list) + i);
+
+ // these are all the record kinds I have observed
+ PDB_ASSERT(
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_BCLASS ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VBCLASS ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_IVBCLASS ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_INDEX ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VFUNCTAB ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_NESTTYPE ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ENUM ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_MEMBER ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_STMEMBER ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_METHOD ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ONEMETHOD ||
+ fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ENUMERATE,
+ "Unknown record kind %X",
+ static_cast<unsigned int>(fieldRecord->kind));
+
+ if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_MEMBER)
+ {
+ leafName = GetLeafName(fieldRecord->data.LF_MEMBER.offset, fieldRecord->data.LF_MEMBER.lfEasy.kind);
+ TAG_AND_CHECK(fieldRecord->data.LF_MEMBER.index);
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_NESTTYPE)
+ {
+ leafName = &fieldRecord->data.LF_NESTTYPE.name[0];
+ TAG_AND_CHECK(fieldRecord->data.LF_NESTTYPE.index);
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_STMEMBER)
+ {
+ leafName = &fieldRecord->data.LF_STMEMBER.name[0];
+ TAG_AND_CHECK(fieldRecord->data.LF_STMEMBER.index);
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_METHOD)
+ {
+ leafName = fieldRecord->data.LF_METHOD.name;
+ setName(fieldRecord->data.LF_METHOD.mList);
+
+ auto methodList = typeTable.GetTypeRecord(fieldRecord->data.LF_METHOD.mList);
+ if (!methodList)
+ break;
+
+ // https://github.com/microsoft/microsoft-pdb/blob/master/PDB/include/symtypeutils.h#L220
+ size_t offsetInMethodList = 0;
+ for (size_t j = 0; j < fieldRecord->data.LF_METHOD.count; j++)
+ {
+ size_t entrySize = sizeof(PDB::CodeView::TPI::MethodListEntry);
+ const PDB::CodeView::TPI::MethodListEntry* entry = (const PDB::CodeView::TPI::MethodListEntry*)(methodList->data.LF_METHODLIST.mList + offsetInMethodList);
+ TAG_AND_CHECK(entry->index);
+ PDB::CodeView::TPI::MethodProperty methodProp = (PDB::CodeView::TPI::MethodProperty)entry->attributes.mprop;
+ if (methodProp == PDB::CodeView::TPI::MethodProperty::Intro || methodProp == PDB::CodeView::TPI::MethodProperty::PureIntro)
+ entrySize += sizeof(uint32_t);
+ offsetInMethodList += entrySize;
+ }
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ONEMETHOD)
+ {
+ leafName = GetMethodName(fieldRecord);
+ TAG_AND_CHECK(fieldRecord->data.LF_ONEMETHOD.index);
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_BCLASS)
+ {
+ leafName = GetLeafName(fieldRecord->data.LF_BCLASS.offset, fieldRecord->data.LF_BCLASS.lfEasy.kind);
+
+ i += static_cast<size_t>(leafName - reinterpret_cast<const char*>(fieldRecord));
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VBCLASS || fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_IVBCLASS)
+ {
+ // virtual base pointer offset from address point
+ // followed by virtual base offset from vbtable
+
+ const PDB::CodeView::TPI::TypeRecordKind vbpOffsetAddressPointKind = *(const PDB::CodeView::TPI::TypeRecordKind*)(fieldRecord->data.LF_IVBCLASS.vbpOffset);
+ const uint8_t vbpOffsetAddressPointSize = GetLeafSize(vbpOffsetAddressPointKind);
+
+ const PDB::CodeView::TPI::TypeRecordKind vbpOffsetVBTableKind = *(const PDB::CodeView::TPI::TypeRecordKind*)(fieldRecord->data.LF_IVBCLASS.vbpOffset + vbpOffsetAddressPointSize);
+ const uint8_t vbpOffsetVBTableSize = GetLeafSize(vbpOffsetVBTableKind);
+
+ TAG_AND_CHECK(fieldRecord->data.LF_VBCLASS.vbpIndex);
+
+ i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_VBCLASS);
+ i += vbpOffsetAddressPointSize + vbpOffsetVBTableSize;
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_INDEX)
+ {
+ // this is continued elsewhere
+ setName(fieldRecord->data.LF_INDEX.type);
+ auto continued = typeTable.GetTypeRecord(fieldRecord->data.LF_INDEX.type);
+ if (continued)
+ TagChildren(typeTable, continued, setName);
+
+ i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_INDEX);
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_VFUNCTAB)
+ {
+ TAG_AND_CHECK(fieldRecord->data.LF_VFUNCTAB.type);
+ i += sizeof(PDB::CodeView::TPI::FieldList::Data::LF_VFUNCTAB);
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ continue;
+ }
+ else if (fieldRecord->kind == PDB::CodeView::TPI::TypeRecordKind::LF_ENUMERATE)
+ {
+ leafName = GetLeafName(fieldRecord->data.LF_ENUMERATE.value, fieldRecord->data.LF_ENUMERATE.lfEasy.kind);
+ }
+ else
+ {
+ break;
+ }
+
+ i += static_cast<size_t>(leafName - reinterpret_cast<const char*>(fieldRecord));
+ i += strnlen(leafName, maximumSize - i - 1) + 1;
+ i = (i + (sizeof(uint32_t) - 1)) & (0 - sizeof(uint32_t));
+ }
+}
+
+template<typename T>
+static void TagRecursively(const TypeTable& typeTable, uint32_t typeIndex, T setName)
+{
+ const PDB::CodeView::TPI::Record* record = typeTable.GetTypeRecord(typeIndex);
+ if (!record)
+ return;
+ switch (record->header.kind)
+ {
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ARRAY:
+ TAG_AND_CHECK(record->data.LF_ARRAY.elemtype);
+ TAG_AND_CHECK(record->data.LF_ARRAY.idxtype);
+ break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_POINTER:
+ TAG_AND_CHECK(record->data.LF_POINTER.utype);
+ break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_MODIFIER:
+ TAG_AND_CHECK(record->data.LF_MODIFIER.type);
+ break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE:
+ TAG_AND_CHECK(record->data.LF_PROCEDURE.rvtype);
+ TAG_AND_CHECK(record->data.LF_PROCEDURE.arglist);
+ break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ARGLIST:
+ {
+ size_t count = record->data.LF_ARGLIST.count;
+ for (size_t i = 0; i < count; i++)
+ {
+ uint32_t type = record->data.LF_ARGLIST.arg[i];
+ TAG_AND_CHECK(type);
+ }
+ break;
+ }
+ case PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION:
+ TAG_AND_CHECK(record->data.LF_MFUNCTION.rvtype);
+ TAG_AND_CHECK(record->data.LF_MFUNCTION.arglist);
+ TAG_AND_CHECK(record->data.LF_MFUNCTION.thistype);
+ break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_FIELDLIST:
+ TagChildren(typeTable, record, setName);
+ break;
+ default:
+ break;
+ }
+}
+
+// This example takes a PDB's TPI stream and prints out a CSV file that contains all records in the TPI stream.
+// You can use it to figure out what's taking up space in the stream.
+//
+// The format of the CSV is Size; Kind; Name. "Size" is the size of the record in bytes, "Kind" is the kind of
+// the entry, and "Name" is a name associated with this entry.Type - definitions, member functions, and member
+// lists use their type as the name. The idea is that you can bucket by "Name" to get actionable information
+//and insight.
+//
+// The Name is set to "???" if no name was found, and it is set to "!!!" if multiple names reference the entry.
+void ExampleTPISize(const PDB::TPIStream& tpiStream, const char* outPath);
+void ExampleTPISize(const PDB::TPIStream& tpiStream, const char* outPath)
+{
+ TimedScope total("\nRunning example \"TPI Size\"");
+
+ FILE* f;
+#ifndef __unix
+ fopen_s(&f, outPath, "w");
+#else
+ f = fopen(outPath, "w");
+#endif
+ PDB_ASSERT(f, "Failed to open %s for writing", outPath);
+
+ fprintf(f, "Size;Kind;Name\n");
+
+ TimedScope typeTableScope("Create TypeTable");
+ TypeTable typeTable(tpiStream);
+ typeTableScope.Done();
+
+ std::vector<const char*> names;
+ names.resize(typeTable.GetTypeRecords().GetLength());
+
+ const size_t minIndex = typeTable.GetFirstTypeIndex();
+ // sets the name of an entry and returns whether the name changed (because it wasn't set, or because we've found
+ // conflicting information).
+ auto setNameGlobal = [&names, minIndex](uint32_t typeIndex, const char* name) -> bool {
+ if (!name || typeIndex < minIndex)
+ return false;
+ size_t idx = typeIndex - minIndex;
+ const char* prev = names[idx];
+ if (names[idx] == nullptr)
+ {
+ names[idx] = name;
+ return true;
+ }
+ else
+ {
+ names[idx] = "!!!"; // multiple references
+ return names[idx] != prev;
+ }
+ };
+
+ // collect base types and propagate their name
+ auto typeRecords = typeTable.GetTypeRecords();
+ for (size_t i = 0, n = typeRecords.GetLength(); i < n; i++)
+ {
+ const PDB::CodeView::TPI::Record* record = typeRecords[i];
+ PDB::CodeView::TPI::TypeRecordKind kind = record->header.kind;
+ if (kind == PDB::CodeView::TPI::TypeRecordKind::LF_STRUCTURE)
+ {
+ names[i] = GetLeafName(record->data.LF_CLASS.data, record->data.LF_CLASS.lfEasy.kind);
+ auto setName = [&setNameGlobal, names, i](uint32_t typeIndex) -> bool {
+ return setNameGlobal(typeIndex, names[i]);
+ };
+ TAG_AND_CHECK(record->data.LF_CLASS.field);
+ }
+ else if (kind == PDB::CodeView::TPI::TypeRecordKind::LF_CLASS)
+ {
+ names[i] = GetLeafName(record->data.LF_CLASS.data, record->data.LF_CLASS.lfEasy.kind);
+ auto setName = [&setNameGlobal, names, i](uint32_t typeIndex) -> bool {
+ return setNameGlobal(typeIndex, names[i]);
+ };
+ TAG_AND_CHECK(record->data.LF_CLASS.field);
+ }
+ else if (kind == PDB::CodeView::TPI::TypeRecordKind::LF_UNION)
+ {
+ names[i] = GetLeafName(record->data.LF_UNION.data, static_cast<PDB::CodeView::TPI::TypeRecordKind>(0));
+ auto setName = [&setNameGlobal, names, i](uint32_t typeIndex) -> bool {
+ return setNameGlobal(typeIndex, names[i]);
+ };
+ TAG_AND_CHECK(record->data.LF_UNION.field);
+ }
+ else if (kind == PDB::CodeView::TPI::TypeRecordKind::LF_ENUM)
+ {
+ names[i] = record->data.LF_ENUM.name;
+ auto setName = [&setNameGlobal, names, i](uint32_t typeIndex) -> bool {
+ return setNameGlobal(typeIndex, names[i]);
+ };
+ TAG_AND_CHECK(record->data.LF_ENUM.field);
+ }
+ else if (kind == PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION)
+ {
+ const char* name = names[i];
+ if (!name)
+ {
+ const PDB::CodeView::TPI::Record* containingRecord = typeTable.GetTypeRecord((record->data.LF_MFUNCTION.classtype));
+ if (containingRecord) {
+ if (containingRecord->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_CLASS ||
+ containingRecord->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_STRUCTURE)
+ name = GetLeafName(containingRecord->data.LF_CLASS.data, containingRecord->data.LF_CLASS.lfEasy.kind);
+ else if (containingRecord->header.kind == PDB::CodeView::TPI::TypeRecordKind::LF_UNION)
+ name = GetLeafName(record->data.LF_UNION.data, static_cast<PDB::CodeView::TPI::TypeRecordKind>(0));
+ else
+ PDB_ASSERT(false, "unsupported");
+ }
+ }
+ auto setName = [&setNameGlobal, name](uint32_t typeIndex) -> bool {
+ return setNameGlobal(typeIndex, name);
+ };
+ uint32_t typeIndex = (uint32_t)(minIndex + i);
+ TAG_AND_CHECK(typeIndex);
+ }
+ }
+
+ for (size_t i = 0, n = typeRecords.GetLength(); i < n; i++)
+ {
+ const PDB::CodeView::TPI::Record* record = typeRecords[i];
+ const char* kindName = nullptr;
+ const char* typeName = i < names.size() ? names[i] : nullptr;
+ switch (record->header.kind)
+ {
+ case PDB::CodeView::TPI::TypeRecordKind::LF_VTSHAPE: kindName = "LF_VTSHAPE;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_POINTER: kindName = "LF_POINTER;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_MODIFIER: kindName = "LF_MODIFIER;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_PROCEDURE: kindName = "LF_PROCEDURE;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_FIELDLIST: kindName = "LF_FIELDLIST;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_LABEL: kindName = "LF_LABEL;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ARGLIST: kindName = "LF_ARGLIST;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_BITFIELD: kindName = "LF_BITFIELD;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_METHODLIST: kindName = "LF_METHODLIST;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ARRAY: kindName = "LF_ARRAY;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_PRECOMP: kindName = "LF_PRECOMP;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_MFUNCTION: kindName = "LF_MFUNCTION;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_STRUCTURE: kindName = "LF_STRUCTURE;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_CLASS: kindName = "LF_CLASS;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_UNION: kindName = "LF_UNION;"; break;
+ case PDB::CodeView::TPI::TypeRecordKind::LF_ENUM: kindName = "LF_ENUM;"; break;
+ default: break;
+ }
+
+ fprintf(f, "%hu;", 2 + record->header.size);
+ if (kindName)
+ fprintf(f, "%s;", kindName);
+ else
+ fprintf(f, "0x%04X;", static_cast<uint16_t>(record->header.kind));
+
+ if (typeName)
+ fprintf(f, "%s\n", typeName);
+ else
+ fprintf(f, "???\n");
+ }
+
+ fclose(f);
+ total.Done(tpiStream.GetTypeRecordCount());
+}
+#undef TAG_AND_CHECK
diff --git a/thirdparty/raw_pdb/src/Examples/Examples_PCH.cpp b/thirdparty/raw_pdb/src/Examples/Examples_PCH.cpp
new file mode 100644
index 000000000..993ae7de7
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/Examples_PCH.cpp
@@ -0,0 +1,4 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#include "Examples_PCH.h"
diff --git a/thirdparty/raw_pdb/src/Examples/Examples_PCH.h b/thirdparty/raw_pdb/src/Examples/Examples_PCH.h
new file mode 100644
index 000000000..0a7f2e2ca
--- /dev/null
+++ b/thirdparty/raw_pdb/src/Examples/Examples_PCH.h
@@ -0,0 +1,53 @@
+// Copyright 2011-2022, Molecular Matters GmbH <[email protected]>
+// See LICENSE.txt for licensing details (2-clause BSD License: https://opensource.org/licenses/BSD-2-Clause)
+
+#pragma once
+
+#include "Foundation/PDB_Warnings.h"
+
+// The following clang warnings must be disabled for the examples to build with 0 warnings
+#if PDB_COMPILER_CLANG
+# pragma clang diagnostic ignored "-Wformat-nonliteral" // format string is not a string literal
+# pragma clang diagnostic ignored "-Wswitch-default" // switch' missing 'default' label
+# pragma clang diagnostic ignored "-Wcast-align" // increases required alignment from X to Y
+# pragma clang diagnostic ignored "-Wold-style-cast" // use of old-style cast
+#endif
+
+#if PDB_COMPILER_MSVC
+# pragma warning(push, 0)
+#elif PDB_COMPILER_CLANG
+# pragma clang diagnostic push
+#endif
+
+#if PDB_COMPILER_MSVC
+ // we compile without exceptions
+# define _ALLOW_RTCc_IN_STL
+
+ // triggered by Windows.h
+# pragma warning (disable : 4668)
+
+ // triggered by xlocale in VS 2017
+# pragma warning (disable : 4625) // copy constructor was implicitly defined as deleted
+# pragma warning (disable : 4626) // assignment operator was implicitly defined as deleted
+# pragma warning (disable : 5026) // move constructor was implicitly defined as deleted
+# pragma warning (disable : 5027) // move assignment operator was implicitly defined as deleted
+# pragma warning (disable : 4774) // format string expected in argument 1 is not a string literal
+#endif
+
+#ifdef _WIN32
+# define NOMINMAX
+# include <Windows.h>
+# undef cdecl
+#endif
+# include <vector>
+# include <unordered_set>
+# include <chrono>
+# include <string>
+# include <algorithm>
+# include <cstdarg>
+
+#if PDB_COMPILER_MSVC
+# pragma warning(pop)
+#elif PDB_COMPILER_CLANG
+# pragma clang diagnostic pop
+#endif