-
Notifications
You must be signed in to change notification settings - Fork 140
/
Copy pathPdbScanner.cpp
1340 lines (1234 loc) · 34.5 KB
/
PdbScanner.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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2015, 2020 IBM Corp. and others
*
* This program and the accompanying materials are made available under
* the terms of the Eclipse Public License 2.0 which accompanies this
* distribution and is available at http://eclipse.org/legal/epl-2.0
* or the Apache License, Version 2.0 which accompanies this distribution
* and is available at https://www.apache.org/licenses/LICENSE-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License, v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception [1] and GNU General Public
* License, version 2 with the OpenJDK Assembly Exception [2].
*
* [1] https://www.gnu.org/software/classpath/license.html
* [2] http://openjdk.java.net/legal/assembly-exception.html
*
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 OR LicenseRef-GPL-2.0 WITH Assembly-exception
*******************************************************************************/
#include "ddr/scanner/pdb/PdbScanner.hpp"
#include <algorithm>
#include <assert.h>
#include <comdef.h>
#if !defined(OMR_OS_WINDOWS)
#include <inttypes.h>
#endif /* !defined(OMR_OS_WINDOWS) */
#include <stdio.h>
#include "ddr/std/sstream.hpp"
#include "ddr/config.hpp"
#include "ddr/ir/EnumMember.hpp"
#include "ddr/ir/ClassUDT.hpp"
#include "ddr/ir/EnumUDT.hpp"
#include "ddr/ir/Field.hpp"
#include "ddr/ir/Symbol_IR.hpp"
#include "ddr/ir/Type.hpp"
#include "ddr/ir/TypedefUDT.hpp"
using std::ios;
using std::stringstream;
static const char * const baseTypeArray[] = {
"<NoType>",
"void",
"I8", /* This could also be char. */
"wchar_t",
"I8",
"U8",
"I32",
"U32",
"float",
"<BCD>",
"bool",
"short",
"unsigned short",
"I32", /* This should be just a long */
"U32", /* This should be unsigned long */
"I8",
"I16",
"I32",
"I64",
"__int128",
"U8",
"U16",
"U32",
"U64",
"U128",
"unsigned __int128",
"<currency>",
"<date>",
"VARIANT",
"<complex>",
"<bit>",
"BSTR",
"HRESULT",
"double"
};
void
PdbScanner::addType(UDT *type, NamespaceUDT *outerNamespace)
{
/* Add the type to the map of found types. Subtypes should
* not be added to the IR. Types are mapped by their size and
* name to be referenced when used as a field.
*/
string fullName = type->getFullName();
if (!fullName.empty() && _typeMap.end() == _typeMap.find(fullName)) {
_typeMap[fullName] = type;
}
if (NULL != outerNamespace) {
outerNamespace->_subUDTs.push_back(type);
} else {
_ir->_types.push_back(type);
}
}
void
PdbScanner::initBaseTypeList()
{
/* Add the base types to the typemap and to the IR. */
size_t length = sizeof(baseTypeArray) / sizeof(*baseTypeArray);
for (size_t i = 0; i < length; ++i) {
Type *type = new Type(0);
type->_name = baseTypeArray[i];
_typeMap[type->_name] = type;
_ir->_types.push_back(type);
}
}
DDR_RC
PdbScanner::startScan(OMRPortLibrary *portLibrary, Symbol_IR *ir, vector<string> *debugFiles, const char *excludesFilePath)
{
DDR_RC rc = DDR_RC_OK;
IDiaDataSource *diaDataSource = NULL;
IDiaSession *diaSession = NULL;
IDiaSymbol *diaSymbol = NULL;
_ir = ir;
initBaseTypeList();
if (FAILED(CoInitialize(NULL))) {
rc = DDR_RC_ERROR;
}
if (DDR_RC_OK == rc) {
rc = loadExcludesFile(portLibrary, excludesFilePath);
}
if (DDR_RC_OK == rc) {
/* For each input Pdb file, load the file, then add the UDTs and Enums.
* If findChildren(SymTagNull, ...) were to be used instead of finding
* the UDTs and enums separately, duplicate types are returned with
* undecorated names. The IR would contain inner classes twice, once as
* an inner class, and once with no parent link and an undecorated name.
* Finding UDT and enum children separately seems to work around this
* quirk in the PDB API.
*/
for (vector<string>::iterator it = debugFiles->begin(); it != debugFiles->end(); ++it) {
const string &file = *it;
const size_t len = file.length();
wchar_t *filename = new wchar_t[len + 1];
mbstowcs(filename, file.c_str(), len + 1);
rc = loadDataFromBinary(filename, &diaDataSource, &diaSession, &diaSymbol);
if (DDR_RC_OK == rc) {
rc = addChildrenSymbols(diaSymbol, SymTagUDT, NULL);
}
if (DDR_RC_OK == rc) {
rc = addChildrenSymbols(diaSymbol, SymTagEnum, NULL);
}
if (DDR_RC_OK == rc) {
rc = addChildrenSymbols(diaSymbol, SymTagTypedef, NULL);
}
if (NULL != diaDataSource) {
diaDataSource->Release();
diaDataSource = NULL;
}
if (NULL != diaSession) {
diaSession->Release();
diaSession = NULL;
}
if (NULL != diaSymbol) {
diaSymbol->Release();
diaSymbol = NULL;
}
delete[] filename;
if (DDR_RC_OK != rc) {
break;
}
}
}
/* Field and superclass types which are needed before the type is found
* are added to a postponed list. After all types are found, process the
* postponed list to add these missing references.
*/
if (DDR_RC_OK == rc) {
rc = updatePostponedFieldNames();
}
CoUninitialize();
return rc;
}
DDR_RC
PdbScanner::loadDataFromBinary(const wchar_t *filename, IDiaDataSource **dataSource, IDiaSession **session, IDiaSymbol **symbol)
{
DDR_RC rc = DDR_RC_OK;
bool isPDBFile = false;
/* get location of the file extension */
const wchar_t *fileExtension = wcsrchr(filename, L'.');
/* check if file extension is '.pdb' */
if ((NULL!= fileExtension) && (0 == wcscmp(fileExtension, L".pdb"))){
isPDBFile = true;
}
/* Attemt to co-create the DiaSource instance. On failure to find the required
* dll, instead attempt to find and load the dll first.
*/
HRESULT hr = CoCreateInstance(__uuidof(DiaSource), NULL, CLSCTX_INPROC_SERVER, __uuidof(IDiaDataSource), (void **)dataSource);
if (FAILED(hr)) {
ERRMSG("CoCreateInstance failed with HRESULT = %08lX", hr);
static const char * const libraries[] = { "MSDIA140", "MSDIA120", "MSDIA100", "MSDIA80", "MSDIA70", "MSDIA60" };
rc = DDR_RC_ERROR;
for (size_t i = 0; i < sizeof(libraries) / sizeof(*libraries); ++i) {
HMODULE hmodule = LoadLibrary(libraries[i]);
if (NULL == hmodule) {
ERRMSG("LoadLibrary failed for %s.dll", libraries[i]);
continue;
}
BOOL (WINAPI *DllGetClassObject)(REFCLSID, REFIID, LPVOID) =
(BOOL (WINAPI *)(REFCLSID, REFIID, LPVOID))
GetProcAddress(hmodule, "DllGetClassObject");
if (NULL == DllGetClassObject) {
ERRMSG("Could not find DllGetClassObject in %s.dll", libraries[i]);
continue;
}
IClassFactory *classFactory = NULL;
hr = DllGetClassObject(__uuidof(DiaSource), IID_IClassFactory, &classFactory);
if (FAILED(hr)) {
ERRMSG("DllGetClassObject failed with HRESULT = %08lX", hr);
continue;
}
hr = classFactory->CreateInstance(NULL, __uuidof(IDiaDataSource), (void **)dataSource);
if (FAILED(hr)) {
ERRMSG("CreateInstance failed with HRESULT = %08lX", hr);
} else {
ERRMSG("Instance of IDiaDataSource created using %s.dll", libraries[i]);
rc = DDR_RC_OK;
break;
}
}
}
if (DDR_RC_OK == rc) {
if(isPDBFile){
hr = (*dataSource)->loadDataFromPdb(filename);
} else {
hr = (*dataSource)->loadDataForExe(filename, NULL, NULL);
}
if (FAILED(hr)) {
ERRMSG("loadDataFromPdb() failed for file with HRESULT = %08lX.\nFile: %ls", hr, filename);
rc = DDR_RC_ERROR;
}
}
if (DDR_RC_OK == rc) {
hr = (*dataSource)->openSession(session);
if (FAILED(hr)) {
ERRMSG("openSession() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
}
if (DDR_RC_OK == rc) {
hr = (*session)->get_globalScope(symbol);
if (FAILED(hr)) {
ERRMSG("get_globalScope() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
}
return rc;
}
DDR_RC
PdbScanner::updatePostponedFieldNames()
{
DDR_RC rc = DDR_RC_OK;
/* Update field type references for fields which were
* processed before their type was added to the IR.
*/
for (vector<PostponedType>::iterator it = _postponedFields.begin(); it != _postponedFields.end(); ++it) {
Type **type = it->type;
const string &fullName = it->name;
unordered_map<string, Type *>::const_iterator map_it = _typeMap.find(fullName);
if (_typeMap.end() != map_it) {
*type = map_it->second;
} else {
*type = new ClassUDT(0);
(*type)->_name = getSimpleName(fullName);
(*type)->_excluded = checkExcludedType((*type)->_name);
}
}
_postponedFields.clear();
return rc;
}
DDR_RC
PdbScanner::addChildrenSymbols(IDiaSymbol *symbol, enum SymTagEnum symTag, NamespaceUDT *outerNamespace)
{
DDR_RC rc = DDR_RC_OK;
IDiaEnumSymbols *classSymbols = NULL;
/* Find children symbols. SymTagUDT covers struct, union, and class symbols. */
HRESULT hr = symbol->findChildren(symTag, NULL, nsNone, &classSymbols);
if (FAILED(hr)) {
ERRMSG("findChildren() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
/* Allocate space for the children symbols. */
LONG count = 0;
if (DDR_RC_OK == rc) {
hr = classSymbols->get_Count(&count);
if (FAILED(hr)) {
ERRMSG("Failed to get count of children symbols with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
}
IDiaSymbol **childSymbols = NULL;
ULONG celt = 0;
if ((DDR_RC_OK == rc) && (0 != count)) {
childSymbols = new IDiaSymbol*[count];
hr = classSymbols->Next(count, childSymbols, &celt);
if (FAILED(hr)) {
ERRMSG("Failed to get children symbols with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
}
/* Iterate the children symbols, adding them to the IR.
* Inner symbols are first found with a decorated name and no
* parent reference. Ignore these for now and add the outer
* types first.
*/
vector<ULONG> innerTypeSymbols;
if (DDR_RC_OK == rc) {
bool alreadyHadFields = false;
bool alreadyCheckedFields = false;
bool alreadyHadSubtypes = false;
if (NULL != outerNamespace) {
alreadyHadSubtypes = !outerNamespace->_subUDTs.empty();
}
for (ULONG index = 0; index < celt; ++index) {
string udtName = "";
DWORD childTag = 0;
hr = childSymbols[index]->get_symTag(&childTag);
if (FAILED(hr)) {
ERRMSG("Failed to get child symbol SymTag with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
if ((DDR_RC_OK == rc) && ((SymTagEnum == childTag) || (SymTagUDT == childTag))) {
rc = getName(childSymbols[index], &udtName);
}
if (DDR_RC_OK == rc) {
if ((NULL == outerNamespace) || (string::npos == udtName.find("::"))) {
/* When adding fields/subtypes to a type, only add fields to a type with no fields
* and only add subtypes to a type with no subtypes. This avoids adding duplicates
* fields/subtypes when scanning multiple files. Children symbols must be processed
* for already existing symbols because fields and subtypes can appear separately.
*/
if (!alreadyCheckedFields && (SymTagData == childTag)) {
alreadyCheckedFields = true;
alreadyHadFields = !((ClassType *)outerNamespace)->_fieldMembers.empty();
}
if (!((SymTagData == childTag) ? alreadyHadFields : alreadyHadSubtypes)) {
rc = addSymbol(childSymbols[index], outerNamespace);
}
childSymbols[index]->Release();
} else {
innerTypeSymbols.push_back(index);
}
}
if (DDR_RC_OK != rc) {
break;
}
}
}
/* Iterate the inner types. Only namespaces will be added to the IR
* here, since they have no associated symbol.
*/
if (DDR_RC_OK == rc) {
for (vector<ULONG>::iterator it = innerTypeSymbols.begin(); it != innerTypeSymbols.end() && DDR_RC_OK == rc; ++it) {
rc = addSymbol(childSymbols[*it], outerNamespace);
childSymbols[*it]->Release();
}
}
if (NULL != childSymbols) {
delete childSymbols;
}
if (NULL != classSymbols) {
classSymbols->Release();
}
return rc;
}
DDR_RC
PdbScanner::createTypedef(IDiaSymbol *symbol, NamespaceUDT *outerNamespace)
{
/* Get the typedef name and check if it should be excluded. */
string symbolName = "";
DDR_RC rc = getName(symbol, &symbolName);
if (DDR_RC_OK == rc) {
string typedefName = getSimpleName(symbolName);
TypedefUDT *newTypedef = new TypedefUDT;
newTypedef->_name = typedefName;
newTypedef->_excluded = checkExcludedType(typedefName);
newTypedef->_outerNamespace = outerNamespace;
/* Get the base type. */
rc = setType(symbol, &newTypedef->_aliasedType, &newTypedef->_modifiers, NULL);
if (DDR_RC_OK == rc) {
Type *aliasedType = newTypedef->_aliasedType;
if (NULL != aliasedType) {
newTypedef->_sizeOf = aliasedType->_sizeOf;
}
addType(newTypedef, outerNamespace);
} else {
delete newTypedef;
}
}
return rc;
}
DDR_RC
PdbScanner::addEnumMembers(IDiaSymbol *symbol, EnumUDT *enumUDT)
{
DDR_RC rc = DDR_RC_OK;
/* All children symbols of a symbol for an Enum type should be
* enum members.
*/
IDiaEnumSymbols *enumSymbols = NULL;
HRESULT hr = symbol->findChildren(SymTagNull, NULL, nsNone, &enumSymbols);
if (FAILED(hr)) {
ERRMSG("findChildren() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
if (DDR_RC_OK == rc) {
vector<EnumMember *> *members = NULL;
NamespaceUDT *outerUDT = enumUDT->_outerNamespace;
/* literals of a nested anonymous enum are collected in the enclosing namespace */
if ((NULL != outerUDT) && enumUDT->isAnonymousType()) {
members = &outerUDT->_enumMembers;
} else {
members = &enumUDT->_enumMembers;
}
set<string> memberNames;
for (vector<EnumMember *>::iterator m = members->begin(); m != members->end(); ++m) {
memberNames.insert((*m)->_name);
}
IDiaSymbol *childSymbol = NULL;
ULONG celt = 0;
LONG count = 0;
enumSymbols->get_Count(&count);
members->reserve(count);
while (SUCCEEDED(enumSymbols->Next(1, &childSymbol, &celt))
&& (1 == celt)
&& (DDR_RC_OK == rc)
) {
string name = "";
int value = 0;
rc = getName(childSymbol, &name);
if (DDR_RC_OK == rc) {
VARIANT variantValue;
variantValue.vt = VT_EMPTY;
hr = childSymbol->get_value(&variantValue);
if (FAILED(hr)) {
ERRMSG("get_value() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else {
switch (variantValue.vt) {
case VT_I1:
value = variantValue.cVal;
break;
case VT_I2:
value = variantValue.iVal;
break;
case VT_I4:
value = (int)variantValue.lVal;
break;
case VT_UI1:
value = variantValue.bVal;
break;
case VT_UI2:
value = variantValue.uiVal;
break;
case VT_UI4:
value = (int)variantValue.ulVal;
break;
case VT_INT:
value = variantValue.intVal;
break;
case VT_UINT:
value = (int)variantValue.uintVal;
break;
default:
ERRMSG("get_value() unexpected variant: 0x%02X", variantValue.vt);
rc = DDR_RC_ERROR;
break;
}
}
}
if (DDR_RC_OK == rc) {
if (memberNames.end() == memberNames.find(name)) {
EnumMember *enumMember = new EnumMember;
enumMember->_name = name;
enumMember->_value = value;
members->push_back(enumMember);
memberNames.insert(name);
}
}
childSymbol->Release();
}
enumSymbols->Release();
}
return rc;
}
DDR_RC
PdbScanner::createEnumUDT(IDiaSymbol *symbol, NamespaceUDT *outerNamespace)
{
DDR_RC rc = DDR_RC_OK;
/* Verify the given symbol is for an enum. */
DWORD symTag = 0;
HRESULT hr = symbol->get_symTag(&symTag);
if (FAILED(hr)) {
ERRMSG("get_symTag() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else if (SymTagEnum != symTag) {
ERRMSG("symTag is not Enum");
rc = DDR_RC_ERROR;
}
string symbolName = "";
if (DDR_RC_OK == rc) {
rc = getName(symbol, &symbolName);
}
if (DDR_RC_OK == rc) {
/* Sub enums are added by their undecorated name when found as a
* child symbol of another UDT symbol. They are also found with
* a decorated "Parent::SubUDT" name again while iterating all Enums.
*/
ULONGLONG size = 0ULL;
rc = getSize(symbol, &size);
if (DDR_RC_OK == rc) {
getNamespaceFromName(symbolName, &outerNamespace);
string name = getSimpleName(symbolName);
string fullName = "";
if (NULL == outerNamespace) {
fullName = name;
} else {
fullName = outerNamespace->getFullName() + "::" + name;
}
unordered_map<string, Type *>::const_iterator it = _typeMap.find(fullName);
if (_typeMap.end() == it) {
/* If this is a new enum, get its members and add it to the IR. */
EnumUDT *enumUDT = new EnumUDT;
enumUDT->_name = name;
enumUDT->_excluded = checkExcludedType(name);
enumUDT->_outerNamespace = outerNamespace;
rc = addEnumMembers(symbol, enumUDT);
/* If successful, add the new enum to the IR. */
if (DDR_RC_OK == rc) {
addType(enumUDT, outerNamespace);
} else {
delete enumUDT;
}
} else {
Type *existingType = it->second;
if ("enum" == existingType->getSymbolKindName()) {
EnumUDT *enumUDT = (EnumUDT *)existingType;
if ((NULL != outerNamespace) && (NULL == enumUDT->_outerNamespace)) {
enumUDT->_outerNamespace = outerNamespace;
outerNamespace->_subUDTs.push_back(enumUDT);
}
if (enumUDT->_enumMembers.empty()) {
rc = addEnumMembers(symbol, enumUDT);
}
}
}
}
}
return rc;
}
DDR_RC
PdbScanner::setMemberOffset(IDiaSymbol *symbol, Field *newField)
{
DDR_RC rc = DDR_RC_OK;
size_t offset = 0;
DWORD locType = 0;
HRESULT hr = symbol->get_locationType(&locType);
if (FAILED(hr)) {
ERRMSG("Symbol in optimized code");
rc = DDR_RC_ERROR;
}
if (DDR_RC_OK == rc) {
switch (locType) {
case LocIsThisRel:
{
LONG loffset = 0;
hr = symbol->get_offset(&loffset);
if (FAILED(hr)) {
ERRMSG("get_offset() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else {
offset = (size_t)loffset;
}
break;
}
case LocIsStatic:
case LocIsConstant:
{
/* Get offset of static class members. */
newField->_isStatic = true;
LONG loffset = 0;
hr = symbol->get_offset(&loffset);
if (SUCCEEDED(hr)) {
offset = (size_t)loffset;
}
break;
}
case LocIsBitField:
{
LONG loffset = 0;
hr = symbol->get_offset(&loffset);
if (FAILED(hr)) {
ERRMSG("get_offset() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else {
offset = (size_t)loffset;
}
if (DDR_RC_OK == rc) {
ULONGLONG bitwidth = 0;
/* For bit-fields, the 'length' is measured in bits. */
hr = symbol->get_length(&bitwidth);
if (FAILED(hr)) {
ERRMSG("get_length() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else {
newField->_bitField = (size_t)bitwidth;
}
}
break;
}
default:
ERRMSG("Unknown offset type: %lu, name: %s", locType, newField->_name.c_str());
rc = DDR_RC_ERROR;
break;
}
}
if (DDR_RC_OK == rc) {
newField->_offset = offset;
}
return rc;
}
DDR_RC
PdbScanner::setTypeModifier(IDiaSymbol *symbol, Modifiers *modifiers)
{
DDR_RC rc = DDR_RC_OK;
/* Get const, volatile, and unaligned type modifiers for a field. */
BOOL bSet = TRUE;
HRESULT hr = symbol->get_constType(&bSet);
if (FAILED(hr)) {
ERRMSG("get_constType() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else if (bSet) {
modifiers->_modifierFlags |= Modifiers::CONST_TYPE;
}
if (DDR_RC_OK == rc) {
hr = symbol->get_volatileType(&bSet);
if (FAILED(hr)) {
ERRMSG("get_volatileType() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else if (bSet) {
modifiers->_modifierFlags |= Modifiers::VOLATILE_TYPE;
}
}
if (DDR_RC_OK == rc) {
hr = symbol->get_unalignedType(&bSet);
if (FAILED(hr)) {
ERRMSG("get_unalignedType() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else if (bSet) {
modifiers->_modifierFlags |= Modifiers::UNALIGNED_TYPE;
}
}
return rc;
}
DDR_RC
PdbScanner::setTypeUDT(IDiaSymbol *typeSymbol, Type **type, NamespaceUDT *outerUDT)
{
DDR_RC rc = DDR_RC_OK;
/* Get the type for a field. */
DWORD udtKind = 0;
HRESULT hr = typeSymbol->get_udtKind(&udtKind);
if (FAILED(hr)) {
ERRMSG("get_udtKind() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
string symbolName = "";
if (DDR_RC_OK == rc) {
rc = getName(typeSymbol, &symbolName);
}
if (DDR_RC_OK == rc) {
getNamespaceFromName(symbolName, &outerUDT);
string name = getSimpleName(symbolName);
string fullName = "";
if (NULL == outerUDT) {
fullName = name;
} else {
fullName = outerUDT->getFullName() + "::" + name;
}
unordered_map<string, Type *>::const_iterator map_it = _typeMap.find(fullName);
if (!fullName.empty() && _typeMap.end() != map_it) {
*type = map_it->second;
} else if (fullName.empty() && (NULL != outerUDT)) {
/* Anonymous inner union UDTs are missing the parent
* relationship and cannot be added later.
*/
ClassUDT *newClass = NULL;
rc = createClassUDT(typeSymbol, &newClass, outerUDT);
if ((DDR_RC_OK == rc) && (NULL != newClass)) {
newClass->_name = "";
*type = newClass;
}
} else if (!name.empty()) {
PostponedType p = { type, fullName };
_postponedFields.push_back(p);
}
}
return rc;
}
DDR_RC
PdbScanner::setPointerType(IDiaSymbol *symbol, Modifiers *modifiers)
{
DDR_RC rc = DDR_RC_OK;
/* Get the pointer and reference count for a field. */
BOOL bSet = TRUE;
HRESULT hr = symbol->get_reference(&bSet);
if (FAILED(hr)) {
ERRMSG("get_reference failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
} else if (bSet) {
modifiers->_referenceCount += 1;
} else {
modifiers->_pointerCount += 1;
}
return rc;
}
Type *
PdbScanner::getType(const string &name)
{
/* Get a type in the map by name. This used only for base types. */
Type *type = NULL;
unordered_map<string, Type *>::const_iterator map_it = _typeMap.find(name);
if (!name.empty() && _typeMap.end() != map_it) {
type = map_it->second;
}
return type;
}
DDR_RC
PdbScanner::setBaseTypeInt(ULONGLONG ulLen, Type **type)
{
DDR_RC rc = DDR_RC_OK;
/* Set a field type to an integer base type depending on its size. */
switch (ulLen) {
case 1:
*type = getType("I8"); /* This could also be a signed char. */
break;
case 2:
*type = getType("I16"); /* This also could be a short int. */
break;
case 4:
*type = getType("I32");
break;
case 8:
*type = getType("I64");
break;
default:
ERRMSG("Unknown int length: %llu", ulLen);
rc = DDR_RC_ERROR;
break;
}
return rc;
}
DDR_RC
PdbScanner::setBaseTypeFloat(ULONGLONG ulLen, Type **type)
{
DDR_RC rc = DDR_RC_OK;
/* Set a field type to a float/double base type depending on its size. */
switch (ulLen) {
case 4:
*type = getType("float"); /* This could also be an unsigned char */
break;
case 8:
*type = getType("double");
break;
default:
ERRMSG("Unknown float length: %llu", ulLen);
rc = DDR_RC_ERROR;
break;
}
return rc;
}
DDR_RC
PdbScanner::setBaseTypeUInt(ULONGLONG ulLen, Type **type)
{
DDR_RC rc = DDR_RC_OK;
/* Set a field type to an unsigned integer base type depending on its size. */
switch (ulLen) {
case 1:
*type = getType("U8"); /* This could also be an unsigned char. */
break;
case 2:
*type = getType("U16");
break;
case 4:
*type = getType("U32");
break;
case 8:
*type = getType("U64");
break;
case 16:
*type = getType("U128");
break;
default:
ERRMSG("Unknown int length: %llu", ulLen);
rc = DDR_RC_ERROR;
break;
}
return rc;
}
DDR_RC
PdbScanner::setBaseType(IDiaSymbol *typeSymbol, Type **type)
{
/* Select a base type from the map for a field based on its
* Pdb base type and size.
*/
DDR_RC rc = DDR_RC_OK;
DWORD baseType = 0;
HRESULT hr = typeSymbol->get_baseType(&baseType);
if (FAILED(hr)) {
ERRMSG("get_baseType() failed with HRESULT = %08lX", hr);
rc = DDR_RC_ERROR;
}
ULONGLONG ulLen = 0ULL;
if (DDR_RC_OK == rc) {
rc = getSize(typeSymbol, &ulLen);
}
if (DDR_RC_OK == rc) {
switch (baseType) {
case btChar:
case btWChar:
case btUInt:
rc = setBaseTypeUInt(ulLen, type);
break;
case btInt:
rc = setBaseTypeInt(ulLen, type);
break;
case btFloat:
rc = setBaseTypeFloat(ulLen, type);
break;
default:
*type = getType(string(baseTypeArray[baseType])); /* This could also be an unsigned char */
break;
}
}
return rc;
}
static string
symTagToString(DWORD value)
{
switch (value) {
case SymTagNull:
return "SymTagNull";
case SymTagExe:
return "SymTagExe";
case SymTagCompiland:
return "SymTagCompiland";
case SymTagCompilandDetails:
return "SymTagCompilandDetails";
case SymTagCompilandEnv:
return "SymTagCompilandEnv";
case SymTagFunction:
return "SymTagFunction";
case SymTagBlock:
return "SymTagBlock";
case SymTagData:
return "SymTagData";
case SymTagAnnotation:
return "SymTagAnnotation";
case SymTagLabel:
return "SymTagLabel";
case SymTagPublicSymbol:
return "SymTagPublicSymbol";
case SymTagUDT:
return "SymTagUDT";
case SymTagEnum:
return "SymTagEnum";
case SymTagFunctionType:
return "SymTagFunctionType";
case SymTagPointerType:
return "SymTagPointerType";
case SymTagArrayType:
return "SymTagArrayType";
case SymTagBaseType:
return "SymTagBaseType";
case SymTagTypedef:
return "SymTagTypedef";
case SymTagBaseClass:
return "SymTagBaseClass";
case SymTagFriend:
return "SymTagFriend";
case SymTagFunctionArgType:
return "SymTagFunctionArgType";
case SymTagFuncDebugStart:
return "SymTagFuncDebugStart";
case SymTagFuncDebugEnd:
return "SymTagFuncDebugEnd";
case SymTagUsingNamespace:
return "SymTagUsingNamespace";
case SymTagVTableShape:
return "SymTagVTableShape";
case SymTagVTable:
return "SymTagVTable";
case SymTagCustom:
return "SymTagCustom";
case SymTagThunk:
return "SymTagThunk";
case SymTagCustomType:
return "SymTagCustomType";
case SymTagManagedType:
return "SymTagManagedType";
case SymTagDimension:
return "SymTagDimension";
#if 0 /* Note: The following are not present in all versions. */
case SymTagCallSite:
return "SymTagCallSite";
case SymTagInlineSite:
return "SymTagInlineSite";
case SymTagBaseInterface:
return "SymTagBaseInterface";
case SymTagVectorType:
return "SymTagVectorType";
case SymTagMatrixType:
return "SymTagMatrixType";
case SymTagHLSLType:
return "SymTagHLSLType";
#endif
}
stringstream result;
result << "value " << value;
return result.str();
}
DDR_RC
PdbScanner::setType(IDiaSymbol *symbol, Type **type, Modifiers *modifiers, NamespaceUDT *outerUDT)
{
DDR_RC rc = DDR_RC_OK;
/* Get all type information, such as type and modifiers, for abort