-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollector.h
219 lines (189 loc) · 5.13 KB
/
Collector.h
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
#pragma once
#include <functional>
#include <unordered_set>
#include <comutil.h>
#include "SymWrap.h"
#pragma comment(lib, "comsuppw.lib")
class CPdbCollector;
typedef BOOL (*EnumProc)(IDiaSymbol* curSym, PVOID param);
typedef struct tagPDBSYMBOL
{
std::string sKey;
DWORD dwSymId;
DWORD dwAddrSect;
DWORD dwAddrOffset;
DWORD dwSymTag;
DWORD dwUniqueId;
} PDBSYMBOL;
BOOL _ProcessUDT(IDiaSymbol* sym, LPVOID param);
BOOL _ProcessEnum(IDiaSymbol* sym, LPVOID param);
BOOL _ProcessTypedef(IDiaSymbol* sym, LPVOID param);
class CPdbCollector : public CThreadImpl<CPdbCollector>
{
friend BOOL _ProcessUDT(IDiaSymbol* sym, LPVOID param);
friend BOOL _ProcessEnum(IDiaSymbol* sym, LPVOID param);
friend BOOL _ProcessTypedef(IDiaSymbol* sym, LPVOID param);
public:
HWND m_hWnd;
CString m_sFilePath;
CString m_sLastError;
CComAutoCriticalSection m_lock;
IDiaDataSource* m_spDataSource = NULL;
IDiaSymbol* m_global_sym = NULL;
IDiaSession* m_session = NULL;
CAtlArray<PDBSYMBOL> m_aSymbols;
int m_cnt_udt;
int m_cnt_typedef;
int m_cnt_enum;
volatile bool m_bDone;
std::unordered_set<std::wstring> m_dupSet;
CPdbCollector() : m_hWnd(NULL), m_bDone(false)
{
m_cnt_udt = 0;
m_cnt_typedef = 0;
m_cnt_enum = 0;
}
void Init(HWND hWnd, LPCTSTR pstrFilename)
{
m_aSymbols.RemoveAll();
m_sFilePath = pstrFilename;
m_sLastError.Empty();
m_hWnd = hWnd;
m_bDone = false;
m_dupSet.clear();
m_cnt_enum = 0;
m_cnt_typedef = 0;
m_cnt_udt = 0;
}
DWORD Run()
{
::PostMessage(m_hWnd, WM_USER_LOAD_START, 0, 0L);
m_bDone = false;
_ATLTRY
{
if (m_spDataSource) m_spDataSource->Release();
HRESULT hr = CoInitialize(NULL);
hr = CoCreateInstance(__uuidof(DiaSource),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IDiaDataSource),
(void**)&m_spDataSource);
if (FAILED(hr))
{
return 1;
}
wchar_t wszExt[MAX_PATH];
_wsplitpath_s(m_sFilePath, NULL, 0, NULL, 0, NULL, 0, wszExt, MAX_PATH);
if (!_wcsicmp(wszExt, L".pdb"))
{
hr = m_spDataSource->loadDataFromPdb(m_sFilePath);
if (FAILED(hr))
{
return 2;
}
}
hr = m_spDataSource->openSession(&m_session);
if (FAILED(hr))
{
return 3;
}
if (IsAborted()) _Abort();
_ProcessTables(m_session);
}
_ATLCATCHALL()
{
::PostMessage(m_hWnd, WM_USER_LOAD_END, 0, 0L);
}
m_spDataSource->Release();
m_bDone = true;
::CoUninitialize();
return 0;
}
void _ProcessTables(IDiaSession* spSession) throw(...)
{
//IDiaSymbol* global_symbol = NULL;
if (FAILED(spSession->get_globalScope(&m_global_sym)))
{
return;
}
CSym::Enum(m_global_sym, SymTagUDT, _ProcessUDT, this);
CSym::Enum(m_global_sym, SymTagEnum, _ProcessEnum, this);
CSym::Enum(m_global_sym, SymTagTypedef, _ProcessTypedef, this);
//global_symbol->Release();
}
void _Fail()
{
CComBSTR bstrError;
m_spDataSource->get_lastError(&bstrError);
m_sLastError = bstrError;
m_spDataSource->Release();
}
};
BOOL _ProcessUDT(IDiaSymbol* sym, LPVOID param)
{
auto This = (CPdbCollector*)param;
UdtKind enKind;
sym->get_udtKind((LPDWORD)&enKind);
CComBSTR bstrName;
sym->get_name(&bstrName);
if(0 != lstrcmpW(bstrName, L"__unnamed") && This->m_dupSet.count(bstrName.m_str) == 0)
{
DWORD id, dwAddrSect, dwAddrOffset;
sym->get_symIndexId(&id);
sym->get_addressSection(&dwAddrSect);
sym->get_addressOffset(&dwAddrOffset);
_bstr_t tmp(bstrName);
std::string sKey((char*)tmp);
CComCritSecLock<CComCriticalSection> lock(This->m_lock);
PDBSYMBOL Info = { sKey, id, dwAddrSect, dwAddrOffset, SymTagUDT, 0 };
This->m_aSymbols.Add(Info);
This->m_dupSet.insert(bstrName.m_str);
This->m_cnt_udt++;
if (This->IsAborted()) throw 1;
}
return FALSE;
}
BOOL _ProcessEnum(IDiaSymbol* sym, LPVOID param)
{
auto This = (CPdbCollector*)param;
CComBSTR bstrName;
sym->get_name(&bstrName);
if(0 != lstrcmpW(bstrName, L"__unnamed") && This->m_dupSet.count(bstrName.m_str) == 0)
{
DWORD id, dwAddrSect, dwAddrOffset;
sym->get_symIndexId(&id);
sym->get_addressSection(&dwAddrSect);
sym->get_addressOffset(&dwAddrOffset);
_bstr_t tmp(bstrName);
std::string sKey((char*)tmp);
CComCritSecLock<CComCriticalSection> lock(This->m_lock);
PDBSYMBOL Info = { sKey, id, dwAddrSect, dwAddrOffset, SymTagEnum, 0 };
This->m_aSymbols.Add(Info);
This->m_dupSet.insert(bstrName.m_str);
This->m_cnt_enum++;
if (This->IsAborted()) throw 1;
}
return FALSE;
}
BOOL _ProcessTypedef(IDiaSymbol* sym, LPVOID param)
{
auto This = (CPdbCollector*)param;
CComBSTR bstrName;
sym->get_name(&bstrName);
if(0 != lstrcmpW(bstrName, L"__unnamed") && This->m_dupSet.count(bstrName.m_str) == 0)
{
DWORD id, dwAddrSect, dwAddrOffset;
sym->get_symIndexId(&id);
sym->get_addressSection(&dwAddrSect);
sym->get_addressOffset(&dwAddrOffset);
_bstr_t tmp(bstrName);
std::string sKey((char*)tmp);
CComCritSecLock<CComCriticalSection> lock(This->m_lock);
PDBSYMBOL Info = { sKey, id, dwAddrSect, dwAddrOffset, SymTagTypedef, 0 };
This->m_aSymbols.Add(Info);
This->m_dupSet.insert(bstrName.m_str);
This->m_cnt_typedef++;
if (This->IsAborted()) throw 1;
}
return FALSE;
}