aboutsummaryrefslogtreecommitdiff
path: root/thirdparty/raw_pdb/src/Examples/ExampleIPI.cpp
blob: 5286689e9cfd431b6d8b3cd133f39f60d4aa49d4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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++;
	}
}