forked from msinilo/crunchersharp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CruncherData.cs
271 lines (241 loc) · 7.27 KB
/
CruncherData.cs
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
using System;
using System.Collections.Generic;
using System.Data;
using Dia2Lib;
namespace CruncherSharp
{
public class CruncherData
{
public CruncherData()
{
m_table = CreateDataTable();
}
public string loadDataFromPdb(string FileName)
{
m_symbols.Clear();
m_source = new DiaSourceClass();
try
{
m_source.loadDataFromPdb(FileName);
m_source.openSession(out m_session);
}
catch (System.Runtime.InteropServices.COMException exc)
{
return exc.ToString();
}
IDiaEnumSymbols allSymbols;
m_session.findChildren(m_session.globalScope, SymTagEnum.SymTagUDT, null, 0, out allSymbols);
{
PopulateDataTable(m_table, allSymbols);
}
return null;
}
DataTable CreateDataTable()
{
DataTable table = new DataTable("Symbols");
DataColumn column = new DataColumn();
column.ColumnName = "Symbol";
column.ReadOnly = true;
table.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Size";
column.ReadOnly = true;
column.DataType = System.Type.GetType("System.Int32");
table.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Padding";
column.ReadOnly = true;
column.DataType = System.Type.GetType("System.Int32");
table.Columns.Add(column);
column = new DataColumn();
column.ColumnName = "Padding/Size";
column.ReadOnly = true;
column.DataType = System.Type.GetType("System.Double");
table.Columns.Add(column);
return table;
}
void PopulateDataTable(DataTable table, IDiaEnumSymbols symbols)
{
table.Rows.Clear();
table.BeginLoadData();
foreach (IDiaSymbol sym in symbols)
{
if (sym.length > 0 && !HasSymbol(sym.name))
{
SymbolInfo info = new SymbolInfo();
info.Set(sym.name, "", sym.length, 0);
info.ProcessChildren(sym);
long totalPadding = info.CalcTotalPadding();
DataRow row = table.NewRow();
string symbolName = sym.name;
row["Symbol"] = symbolName;
row["Size"] = info.m_size;
row["Padding"] = totalPadding;
row["Padding/Size"] = (double)totalPadding / info.m_size;
table.Rows.Add(row);
m_symbols.Add(info.m_name, info);
}
}
table.EndLoadData();
}
public bool HasSymbol(string name)
{
return m_symbols.ContainsKey(name);
}
public SymbolInfo FindSymbolInfo(string name)
{
SymbolInfo info;
m_symbols.TryGetValue(name, out info);
return info;
}
IDiaDataSource m_source;
IDiaSession m_session;
Dictionary<string, SymbolInfo> m_symbols = new Dictionary<string, SymbolInfo>();
public DataTable m_table = null;
public void dumpSymbolInfo(System.IO.TextWriter tw, SymbolInfo info)
{
tw.WriteLine("Symbol: " + info.m_name);
tw.WriteLine("Size: " + info.m_size.ToString());
tw.WriteLine("Total padding: " + info.CalcTotalPadding().ToString());
tw.WriteLine("Members");
tw.WriteLine("-------");
foreach (SymbolInfo child in info.m_children)
{
if (child.m_padding > 0)
{
long paddingOffset = child.m_offset - child.m_padding;
tw.WriteLine(String.Format("{0,-40} {1,5} {2,5}", "****Padding", paddingOffset, child.m_padding));
}
tw.WriteLine(String.Format("{0,-40} {1,5} {2,5}", child.m_name, child.m_offset, child.m_size));
}
// Final structure padding.
if (info.m_padding > 0)
{
long paddingOffset = (long)info.m_size - info.m_padding;
tw.WriteLine(String.Format("{0,-40} {1,5} {2,5}", "****Padding", paddingOffset, info.m_padding));
}
}
}
public class SymbolInfo
{
public void ProcessChildren(IDiaSymbol symbol)
{
IDiaEnumSymbols children;
symbol.findChildren(SymTagEnum.SymTagNull, null, 0, out children);
foreach (IDiaSymbol child in children)
{
SymbolInfo childInfo;
if (ProcessChild(child, out childInfo))
{
AddChild(childInfo);
}
}
// Sort children by offset, recalc padding.
// Sorting is not needed normally (for data fields), but sometimes base class order is wrong.
if (HasChildren())
{
m_children.Sort(CompareOffsets);
for (int i = 0; i < m_children.Count; ++i)
{
SymbolInfo child = m_children[i];
child.m_padding = CalcPadding(child.m_offset, i);
}
m_padding = CalcPadding((long)m_size, m_children.Count);
}
}
bool ProcessChild(IDiaSymbol symbol, out SymbolInfo info)
{
info = new SymbolInfo();
if (symbol.isStatic != 0 || (symbol.symTag != (uint)SymTagEnum.SymTagData && symbol.symTag != (uint)SymTagEnum.SymTagBaseClass))
{
return false;
}
// LocIsThisRel || LocIsNull || LocIsBitField
if (symbol.locationType != 4 && symbol.locationType != 0 && symbol.locationType != 6)
{
return false;
}
ulong len = symbol.length;
IDiaSymbol typeSymbol = symbol.type;
if (typeSymbol != null)
{
len = typeSymbol.length;
}
string symbolName = symbol.name;
if (symbol.symTag == (uint)SymTagEnum.SymTagBaseClass)
{
symbolName = "Base: " + symbolName;
}
info.Set(symbolName, (typeSymbol != null ? typeSymbol.name : ""), len, symbol.offset);
return true;
}
long CalcPadding(long offset, int index)
{
long padding = 0;
if (HasChildren() && index > 0)
{
SymbolInfo lastInfo = m_children[index - 1];
padding = offset - (lastInfo.m_offset + (long)lastInfo.m_size);
}
return padding > 0 ? padding : 0;
}
public void Set(string name, string typeName, ulong size, long offset)
{
m_name = name;
m_typeName = typeName;
m_size = size;
m_offset = offset;
}
public bool HasChildren() { return m_children != null; }
public long CalcTotalPadding()
{
long totalPadding = m_padding;
if (HasChildren())
{
foreach (SymbolInfo info in m_children)
{
totalPadding += info.m_padding;
}
}
return totalPadding;
}
void AddChild(SymbolInfo child)
{
if (m_children == null)
{
m_children = new List<SymbolInfo>();
}
m_children.Add(child);
}
public bool IsBase()
{
return m_name.IndexOf("Base: ") == 0;
}
private static int CompareOffsets(SymbolInfo x, SymbolInfo y)
{
// Base classes have to go first.
if (x.IsBase() && !y.IsBase())
{
return -1;
}
if (!x.IsBase() && y.IsBase())
{
return 1;
}
if (x.m_offset == y.m_offset)
{
return (x.m_size == y.m_size ? 0 : (x.m_size < y.m_size) ? -1 : 1);
}
else
{
return (x.m_offset < y.m_offset) ? -1 : 1;
}
}
public string m_name;
public string m_typeName;
public ulong m_size;
public long m_offset;
public long m_padding;
public List<SymbolInfo> m_children;
};
}