forked from msinilo/crunchersharp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CruncherSharp.cs
484 lines (430 loc) · 17.9 KB
/
CruncherSharp.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
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Dia2Lib;
namespace CruncherSharp
{
public partial class CruncherSharp : Form
{
public CruncherSharp()
{
InitializeComponent();
m_table = CreateDataTable();
bindingSourceSymbols.DataSource = m_table;
dataGridSymbols.DataSource = bindingSourceSymbols;
dataGridSymbols.Columns[0].Width = 271;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void loadPDBToolStripMenuItem_Click(object sender, EventArgs e)
{
if (openPdbDialog.ShowDialog() == DialogResult.OK)
{
m_symbols.Clear();
m_source = new DiaSourceClass();
try
{
Cursor.Current = Cursors.WaitCursor;
m_source.loadDataFromPdb(openPdbDialog.FileName);
m_source.openSession(out m_session);
Cursor.Current = Cursors.Default;
}
catch (System.Runtime.InteropServices.COMException exc)
{
MessageBox.Show(this, exc.ToString());
return;
}
Cursor.Current = Cursors.WaitCursor;
IDiaEnumSymbols allSymbols;
m_session.findChildren(m_session.globalScope, SymTagEnum.SymTagUDT, null, 0, out allSymbols);
// Temporarily clear the filter so, if current filter is invalid, we don't generate a ton of exceptions while populating the table
var preExistingFilter = textBoxFilter.Text;
textBoxFilter.Text = "";
{
PopulateDataTable(m_table, allSymbols);
Cursor.Current = Cursors.Default;
// Sort by name by default (ascending)
dataGridSymbols.Sort(dataGridSymbols.Columns[0], ListSortDirection.Ascending);
bindingSourceSymbols.Filter = null;// "Symbol LIKE '*rde*'";
}
// Restore the filter now that the table is populated
textBoxFilter.Text = preExistingFilter;
ShowSelectedSymbolInfo();
}
}
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)
{
ulong cacheLineSize = GetCacheLineSize();
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();
}
bool HasSymbol(string name)
{
return m_symbols.ContainsKey(name);
}
ulong GetCacheLineSize()
{
return Convert.ToUInt32(textBoxCache.Text);
}
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;
};
SymbolInfo FindSelectedSymbolInfo()
{
if (dataGridSymbols.SelectedRows.Count == 0)
return null;
DataGridViewRow selectedRow = dataGridSymbols.SelectedRows[0];
string symbolName = selectedRow.Cells[0].Value.ToString();
SymbolInfo info = FindSymbolInfo(symbolName);
return info;
}
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>();
DataTable m_table = null;
long m_prefetchStartOffset = 0;
private void dataGridSymbols_SelectionChanged(object sender, EventArgs e)
{
m_prefetchStartOffset = 0;
ShowSelectedSymbolInfo();
}
void ShowSelectedSymbolInfo()
{
dataGridViewSymbolInfo.Rows.Clear();
SymbolInfo info = FindSelectedSymbolInfo();
if (info != null)
ShowSymbolInfo(info);
}
void ShowSymbolInfo(SymbolInfo info)
{
dataGridViewSymbolInfo.Rows.Clear();
if (!info.HasChildren())
return;
long cacheLineSize = (long)GetCacheLineSize();
long prevCacheBoundaryOffset = m_prefetchStartOffset;
if (prevCacheBoundaryOffset > (long)info.m_size)
prevCacheBoundaryOffset = (long)info.m_size;
long numCacheLines = 0;
foreach (SymbolInfo child in info.m_children)
{
if (child.m_padding > 0)
{
long paddingOffset = child.m_offset - child.m_padding;
string[] paddingRow = { "Padding", paddingOffset.ToString(), child.m_padding.ToString() };
dataGridViewSymbolInfo.Rows.Add(paddingRow);
}
// long childEndOffset = child.m_offset + (long)child.m_size;
while (child.m_offset - prevCacheBoundaryOffset >= cacheLineSize)
{
numCacheLines = numCacheLines + 1;
long cacheLineOffset = numCacheLines * cacheLineSize + m_prefetchStartOffset;
string[] boundaryRow = { "Cacheline boundary", cacheLineOffset.ToString(), "" };
dataGridViewSymbolInfo.Rows.Add(boundaryRow);
prevCacheBoundaryOffset = cacheLineOffset;
}
string[] row = { child.m_name, child.m_offset.ToString(), child.m_size.ToString() };
dataGridViewSymbolInfo.Rows.Add(row);
dataGridViewSymbolInfo.Rows[dataGridViewSymbolInfo.Rows.Count - 1].Tag = child;
if (child.m_typeName != null && child.m_typeName.Length != 0)
{
dataGridViewSymbolInfo.Rows[dataGridViewSymbolInfo.Rows.Count - 1].Cells[0].ToolTipText = child.m_typeName;
}
}
// Final structure padding.
if (info.m_padding > 0)
{
long paddingOffset = (long)info.m_size - info.m_padding;
string[] paddingRow = { "Padding", paddingOffset.ToString(), info.m_padding.ToString() };
dataGridViewSymbolInfo.Rows.Add(paddingRow);
}
}
private void dataGridSymbols_SortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
if (e.Column.Index > 0)
{
e.Handled = true;
int val1;
int val2;
Int32.TryParse(e.CellValue1.ToString(), out val1);
Int32.TryParse(e.CellValue2.ToString(), out val2);
e.SortResult = val1 - val2;
}
}
private void dataGridViewSymbolInfo_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
foreach (DataGridViewRow row in dataGridViewSymbolInfo.Rows)
{
DataGridViewCell cell = row.Cells[0];
if (cell.Value.ToString() == "Padding")
{
cell.Style.BackColor = Color.LightPink;
row.Cells[1].Style.BackColor = Color.LightPink;
row.Cells[2].Style.BackColor = Color.LightPink;
}
else if (cell.Value.ToString().IndexOf("Base: ") == 0)
{
cell.Style.BackColor = Color.LightGreen;
row.Cells[1].Style.BackColor = Color.LightGreen;
row.Cells[2].Style.BackColor = Color.LightGreen;
}
else if (cell.Value.ToString() == "Cacheline boundary")
{
cell.Style.BackColor = Color.LightGray;
row.Cells[1].Style.BackColor = Color.LightGray;
row.Cells[2].Style.BackColor = Color.LightGray;
}
}
}
private void textBoxFilter_TextChanged(object sender, EventArgs e)
{
try
{
if (textBoxFilter.Text.Length == 0)
bindingSourceSymbols.Filter = null;
else
bindingSourceSymbols.Filter = "Symbol LIKE '" + textBoxFilter.Text + "'";
textBoxFilter.BackColor = Color.Empty;
textBoxFilter.ForeColor = Color.Empty;
filterFeedbackLabel.Text = "";
}
catch (System.Data.EvaluateException)
{
textBoxFilter.BackColor = Color.Red;
textBoxFilter.ForeColor = Color.White;
filterFeedbackLabel.Text = "Invalid filter";
}
}
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));
}
}
private void copyTypeLayoutToClipboardToolStripMenuItem_Click(object sender, EventArgs e)
{
SymbolInfo info = FindSelectedSymbolInfo();
if (info != null)
{
System.IO.StringWriter tw = new System.IO.StringWriter();
dumpSymbolInfo(tw, info);
Clipboard.SetText(tw.ToString());
}
}
private void textBoxCache_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Escape)
ShowSelectedSymbolInfo();
base.OnKeyPress(e);
}
private void textBoxCache_Leave(object sender, EventArgs e)
{
ShowSelectedSymbolInfo();
}
private void setPrefetchStartOffsetToolStripMenuItem_Click(object sender, EventArgs e)
{
if (dataGridViewSymbolInfo.SelectedRows.Count != 0)
{
DataGridViewRow selectedRow = dataGridViewSymbolInfo.SelectedRows[0];
long symbolOffset = Convert.ToUInt32(selectedRow.Cells[1].Value);
m_prefetchStartOffset = symbolOffset;
ShowSelectedSymbolInfo();
}
}
private void dataGridViewSymbolInfo_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (dataGridViewSymbolInfo.SelectedRows.Count != 0)
{
DataGridViewRow selectedRow = dataGridViewSymbolInfo.SelectedRows[0];
SymbolInfo info = selectedRow.Tag as SymbolInfo;
if (info != null)
{
SelectSymbol(info.m_typeName);
}
}
}
void SelectSymbol(string name)
{
foreach (DataGridViewRow dr in dataGridSymbols.Rows)
{
DataGridViewCell dc = dr.Cells[0]; // name
if (dc.Value.ToString() == name)
{
dataGridSymbols.CurrentCell = dc;
break;
}
}
}
}
}