-
Notifications
You must be signed in to change notification settings - Fork 0
/
useful.cpp
434 lines (380 loc) · 8.46 KB
/
useful.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//useful.cpp
#include "stdafx.h"
#include "useful.h"
#include "UDT.h"
std::wstring getName(CComPtr<IDiaSymbol> sym)
{
std::wstring ret;
if(!sym)
{
return ret;
}
CComBSTR bstr;
checkResult(sym->get_name(&bstr));
//The BSTR may be invalid. If so do not assign to the wstring.
if(!!bstr)
ret = bstr;
return ret;
}
std::wstring getKind(CComPtr<IDiaSymbol> sym)
{
std::wstring ret;
DWORD kind = 0;
checkResult(sym->get_udtKind(&kind));
switch(kind)
{
case UdtStruct: ret += L"struct"; break;
case UdtClass: ret += L"class"; break;
case UdtUnion: ret += L"union"; break;
}
return ret;
}
/*
enum BasicType
{
btNoType = 0,
btVoid = 1,
btChar = 2,
btWChar = 3,
btInt = 6,
btUInt = 7,
btFloat = 8,
btBCD = 9,
btBool = 10,
btLong = 13,
btULong = 14,
btCurrency = 25,
btDate = 26,
btVariant = 27,
btComplex = 28,
btBit = 29,
btBSTR = 30,
btHresult = 31
};
*/
static wchar_t* szBasicTypes[] = {
L"/* no type */",
L"void",
L"char",
L"wchar_t",
L"/* unknown type */",
L"/* unknown type */",
L"int",
L"unsigned int",
L"float",
L"/* BCD */",
L"bool",
L"/* unknown type */",
L"/* unknown type */",
L"long",
L"unsigned long",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* unknown type */",
L"/* Currency */",
L"/* Date */",
L"/* Variant */",
L"/* Complex */",
L"/* Bit */",
L"/* BSTR */",
L"HRESULT",
};
std::wstring getBasicTypeString(DWORD basicType)
{
return szBasicTypes[basicType];
};
std::wstring removeLeading(std::wstring str)
{
//this function is not safe for templates
if(std::wstring::npos != str.rfind(L"<"))
return str;
std::wstring::size_type pos = str.rfind(L"::");
if(std::wstring::npos != pos)
{
std::wstring ret(str,pos+2,std::wstring::npos);
return ret;
}
return str;
}
static wchar_t* szDataKinds[] = {
L"Unknown",
L"Local",
L"Static Local",
L"Parameter",
L"Object Pointer",
L"File Static",
L"Global",
L"Member",
L"Static Member",
L"Constant"
};
std::wstring dataKind(CComPtr<IDiaSymbol> sym)
{
DWORD kind;
checkResult(sym->get_dataKind(&kind));
return szDataKinds[kind];
}
static wchar_t* szLocationTypes[] = {
L"LocIsNull",
L"LocIsStatic",
L"LocIsTLS",
L"LocIsRegRel",
L"LocIsThisRel",
L"LocIsEnregistered",
L"LocIsBitField",
L"LocIsSlot",
L"LocIsIlRel",
L"LocInMetaData",
L"LocIsConstant",
};
std::wstring locationType(CComPtr<IDiaSymbol> sym)
{
DWORD loc;
checkResult(sym->get_locationType(&loc));
return szLocationTypes[loc];
}
std::wstring toString(int val)
{
std::wstring ret;
std::wstringstream converter;
converter << val;
converter >> ret;
return ret;
}
std::wstring expandTypeSpecial(CComPtr<IDiaSymbol> type)
{
std::wstring ret;
if(!type)
{
return ret;
}
DWORD tag;
checkResult(type->get_symTag(&tag));
if(SymTagPointerType == tag)
{
CComPtr<IDiaSymbol> pointee;
checkResult(type->get_type(&pointee));
ret += expandTypeSpecial(pointee);
}
else if(SymTagUDT == tag)
{
UDT udt(type);
ret += udt.getName();
}
return ret;
}
std::wstring expandType(CComPtr<IDiaSymbol> type, const std::wstring& name)
{
std::wstring ret;
if(!type)
{
return ret;
}
BOOL vol;
checkResult(type->get_volatileType(&vol));
if(vol)
{
ret += L"volatile ";
}
BOOL con;
checkResult(type->get_constType(&con));
if(con)
{
ret += L"const ";
}
DWORD tag;
checkResult(type->get_symTag(&tag));
if(SymTagBaseType == tag)
{
DWORD baseType;
checkResult(type->get_baseType(&baseType));
ret += getBasicTypeString(baseType);
return ret;
}
if(SymTagPointerType == tag)
{
CComPtr<IDiaSymbol> pointee;
checkResult(type->get_type(&pointee));
DWORD pointeeTag;
checkResult(pointee->get_symTag(&pointeeTag));
if(SymTagFunctionType == pointeeTag)//function pointer!
{
CComPtr<IDiaSymbol> returnType;
checkResult(pointee->get_type(&returnType));
ret += expandType(returnType);
ret += L" (*" + name + L")(";
CComPtr<IDiaEnumSymbols> pIDiaEnumSymbols;
checkResult(pointee->findChildren(SymTagFunctionArgType, NULL,
nsNone,&pIDiaEnumSymbols));
if(!!pIDiaEnumSymbols)
{
LONG count;
checkResult(pIDiaEnumSymbols->get_Count(&count));
for(int i = 0; i < count; ++i)
{
if(i)ret += L", ";
CComPtr<IDiaSymbol> pIDiaSymbol;
checkResult(pIDiaEnumSymbols->Item(i,&pIDiaSymbol));
CComPtr<IDiaSymbol> funcArg;
checkResult(pIDiaSymbol->get_type(&funcArg));
ret += expandType(funcArg);
}
}
ret += L")";
return ret;
}
ret += expandType(pointee);
BOOL ref;
checkResult(type->get_reference(&ref));
if(ref)
{
ret += L"&";
}
else
{
ret += L"*";
}
return ret;
}
else if(SymTagArrayType == tag)
{
CComPtr<IDiaSymbol> arrayBase;
checkResult(type->get_type(&arrayBase));
ULONGLONG total;
checkResult(type->get_length(&total));
ULONGLONG len;
checkResult(arrayBase->get_length(&len));
ret += expandType(arrayBase);
ret += L"[" + toString((int)(total/len)) + L"]";//sometimes seems to be inaccurate
return ret;
}
else if(SymTagUDT == tag)
{
UDT udt(type);
ret = udt.getKind() + L" " + udt.getName();
return ret;
}
else if(SymTagEnum == tag)
{
ret = L"enum " + getName(type);
return ret;
}
else
{
__asm int 3;//JC to do
}
return getName(type);
}
std::wstring expandBaseType(CComPtr<IDiaSymbol> type)
{
std::wstring ret;
if(!type)
{
return ret;
}
DWORD tag;
checkResult(type->get_symTag(&tag));
if(SymTagUDT == tag)
{
UDT udt(type);
ret = udt.getKind() + L" " + udt.getName();
}
else if(SymTagPointerType == tag)
{
CComPtr<IDiaSymbol> pointee;
checkResult(type->get_type(&pointee));
DWORD pointeeTag;
checkResult(pointee->get_symTag(&pointeeTag));
if(SymTagUDT == pointeeTag)
{
UDT udt(pointee);
ret = udt.getKind() + L" " + udt.getName();
}
}
else if(SymTagBaseType == tag)
{
DWORD baseType;
checkResult(type->get_baseType(&baseType));
ret += getBasicTypeString(baseType);
return ret;
}
else
{
std::wstringstream str;
str << tag;
str >> ret;
}
return ret;
}
void exploreSymbol3(CComPtr<IDiaSymbol> symbol)
{
std::wstring symbolName = ::getName(symbol);
CComPtr<IDiaEnumSymbols> pIDiaEnumSymbols;
checkResult(symbol->findChildren(SymTagNull,
NULL,nsNone,&pIDiaEnumSymbols));
if(!pIDiaEnumSymbols)
return;
LONG count;
checkResult(pIDiaEnumSymbols->get_Count(&count));
for(int i = 0; i < count; ++i)
{
CComPtr<IDiaSymbol> pIDiaSymbol;
checkResult(pIDiaEnumSymbols->Item(i,&pIDiaSymbol));//baseType
DWORD tagSym;
checkResult(pIDiaSymbol->get_symTag(&tagSym));
std::wstring name = ::getName(pIDiaSymbol);
if(SymTagFriend == tagSym)
__asm int 3;
}
}
void exploreSymbol2(CComPtr<IDiaSymbol> symbol)
{
std::wstring symbolName = ::getName(symbol);
CComPtr<IDiaEnumSymbols> pIDiaEnumSymbols;
checkResult(symbol->findChildren(SymTagNull,
NULL,nsNone,&pIDiaEnumSymbols));
if(!pIDiaEnumSymbols)
return;
LONG count;
checkResult(pIDiaEnumSymbols->get_Count(&count));
for(int i = 0; i < count; ++i)
{
CComPtr<IDiaSymbol> pIDiaSymbol;
checkResult(pIDiaEnumSymbols->Item(i,&pIDiaSymbol));//baseType
DWORD tagSym;
checkResult(pIDiaSymbol->get_symTag(&tagSym));
std::wstring name = ::getName(pIDiaSymbol);
if(SymTagFriend == tagSym)
__asm int 3;
exploreSymbol3(pIDiaSymbol);
}
}
void exploreSymbol(CComPtr<IDiaSymbol> symbol)
{
std::wstring symbolName = ::getName(symbol);
CComPtr<IDiaEnumSymbols> pIDiaEnumSymbols;
checkResult(symbol->findChildren(SymTagNull,
NULL,nsNone,&pIDiaEnumSymbols));
if(!pIDiaEnumSymbols)
return;
LONG count;
checkResult(pIDiaEnumSymbols->get_Count(&count));
for(int i = 0; i < count; ++i)
{
CComPtr<IDiaSymbol> pIDiaSymbol;
checkResult(pIDiaEnumSymbols->Item(i,&pIDiaSymbol));//baseType
DWORD tagSym;
checkResult(pIDiaSymbol->get_symTag(&tagSym));
std::wstring name = ::getName(pIDiaSymbol);
if(SymTagFriend == tagSym)
__asm int 3;
exploreSymbol2(pIDiaSymbol);
}
}