forked from farbrausch/fr_public
-
Notifications
You must be signed in to change notification settings - Fork 2
/
pdbfile.cpp
279 lines (224 loc) · 6.11 KB
/
pdbfile.cpp
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Written by Fabian "ryg" Giesen.
// I hereby place this code in the public domain.
#include "_types.hpp"
#include "debuginfo.hpp"
#include "pdbfile.hpp"
#include <malloc.h>
#include "windows.h"
#include "ole2.h"
#include <dia2.h>
struct PDBFileReader::SectionContrib
{
DWORD Section;
DWORD Offset;
DWORD Length;
DWORD Compiland;
BOOL CodeFlag;
};
sU32 PDBFileReader::CompilandFromSectionOffset(sU32 sec,sU32 offs,sBool &codeFlag)
{
sInt l,r,x;
l = 0;
r = nContribs;
while(l < r)
{
x = (l + r) / 2;
const SectionContrib &cur = Contribs[x];
if(sec < cur.Section || sec == cur.Section && offs < cur.Offset)
r = x;
else if(sec > cur.Section || sec == cur.Section && offs >= cur.Offset + cur.Length)
l = x+1;
else // we got a winner
{
codeFlag = cur.CodeFlag;
return cur.Compiland;
}
}
// normally, this shouldn't happen!
return 0;
}
// helpers
static sChar *BStrToString(BSTR str,sChar *defString="")
{
if(!str)
{
sInt len = sGetStringLen(defString);
sChar *buffer = new sChar[len+1];
sCopyString(buffer,defString,len+1);
return buffer;
}
else
{
sInt len = SysStringLen(str);
sChar *buffer = new sChar[len+1];
for(sInt i=0;i<len;i++)
buffer[i] = (str[i] >= 32 && str[i] < 128) ? str[i] : '?';
buffer[len] = 0;
return buffer;
}
}
static sInt GetBStr(BSTR str,sChar *defString,DebugInfo &to)
{
sChar *normalStr = BStrToString(str);
sInt result = to.MakeString(normalStr);
delete[] normalStr;
return result;
}
void PDBFileReader::ProcessSymbol(IDiaSymbol *symbol,DebugInfo &to)
{
DWORD section,offset,rva;
enum SymTagEnum tag;
ULONGLONG length = 0;
sU32 compilandId;
IDiaSymbol *compiland = 0;
BSTR name = 0, sourceName = 0;
sBool codeFlag;
symbol->get_symTag((DWORD *) &tag);
symbol->get_relativeVirtualAddress(&rva);
symbol->get_length(&length);
symbol->get_addressSection(§ion);
symbol->get_addressOffset(&offset);
// is the previous symbol desperately looking for a length? we can help!
if(DanglingLengthStart)
{
to.Symbols[to.Symbols.Count - 1].Size = rva - DanglingLengthStart;
DanglingLengthStart = 0;
}
// get length from type for data
if(tag == SymTagData)
{
IDiaSymbol *type;
if(symbol->get_type(&type) == S_OK)
{
type->get_length(&length);
type->Release();
}
// if length still zero, just guess and take number of bytes between
// this symbol and the next one.
if(!length)
DanglingLengthStart = rva;
}
compilandId = CompilandFromSectionOffset(section,offset,codeFlag);
Session->symbolById(compilandId,&compiland);
if(compiland)
compiland->get_name(&sourceName);
symbol->get_name(&name);
// fill out structure
sChar *nameStr = BStrToString(name,"<no name>");
sChar *sourceStr = BStrToString(sourceName,"<no source>");
if(tag == SymTagPublicSymbol)
{
length = 0;
DanglingLengthStart = rva;
}
DISymbol *outSym = to.Symbols.Add();
outSym->Name.Index = outSym->MangledName.Index = to.MakeString(nameStr);
outSym->FileNum = to.GetFileByName(sourceStr);
outSym->VA = rva;
outSym->Size = (sU32) length;
outSym->Class = codeFlag ? DIC_CODE : DIC_DATA;
outSym->NameSpNum = to.GetNameSpaceByName(nameStr);
// clean up
delete[] nameStr;
delete[] sourceStr;
if(compiland) compiland->Release();
if(sourceName) SysFreeString(sourceName);
if(name) SysFreeString(name);
}
void PDBFileReader::ReadEverything(DebugInfo &to)
{
ULONG celt;
Contribs = 0;
nContribs = 0;
DanglingLengthStart = 0;
// read section table
IDiaEnumTables *enumTables;
if(Session->getEnumTables(&enumTables) == S_OK)
{
VARIANT vIndex;
vIndex.vt = VT_BSTR;
vIndex.bstrVal = SysAllocString(L"Sections");
IDiaTable *secTable;
if(enumTables->Item(vIndex,&secTable) == S_OK)
{
LONG count;
secTable->get_Count(&count);
Contribs = new SectionContrib[count];
nContribs = 0;
IDiaSectionContrib *item;
while(SUCCEEDED(secTable->Next(1,(IUnknown **)&item,&celt)) && celt == 1)
{
SectionContrib &contrib = Contribs[nContribs++];
item->get_addressOffset(&contrib.Offset);
item->get_addressSection(&contrib.Section);
item->get_length(&contrib.Length);
item->get_compilandId(&contrib.Compiland);
item->get_execute(&contrib.CodeFlag);
item->Release();
}
secTable->Release();
}
SysFreeString(vIndex.bstrVal);
enumTables->Release();
}
// enumerate symbols by (virtual) address
IDiaEnumSymbolsByAddr *enumByAddr;
if(SUCCEEDED(Session->getSymbolsByAddr(&enumByAddr)))
{
IDiaSymbol *symbol;
// get first symbol to get first RVA (argh)
if(SUCCEEDED(enumByAddr->symbolByAddr(1,0,&symbol)))
{
DWORD rva;
if(symbol->get_relativeVirtualAddress(&rva) == S_OK)
{
symbol->Release();
// now, enumerate by rva.
if(SUCCEEDED(enumByAddr->symbolByRVA(rva,&symbol)))
{
do
{
ProcessSymbol(symbol,to);
symbol->Release();
if(FAILED(enumByAddr->Next(1,&symbol,&celt)))
break;
}
while(celt == 1);
}
}
else
symbol->Release();
}
enumByAddr->Release();
}
// clean up
delete[] Contribs;
}
/****************************************************************************/
sBool PDBFileReader::ReadDebugInfo(sChar *fileName,DebugInfo &to)
{
sBool readOk = sFALSE;
if(FAILED(CoInitialize(0)))
return sFALSE;
IDiaDataSource *source = 0;
CoCreateInstance(__uuidof(DiaSource),0,CLSCTX_INPROC_SERVER,
__uuidof(IDiaDataSource),(void**) &source);
if(source)
{
wchar_t wideFileName[260];
mbstowcs(wideFileName,fileName,260);
if(SUCCEEDED(source->loadDataForExe(wideFileName,0,0)))
{
if(SUCCEEDED(source->openSession(&Session)))
{
ReadEverything(to);
readOk = sTRUE;
Session->Release();
}
}
source->Release();
}
CoUninitialize();
return readOk;
}
/****************************************************************************/