-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
90 lines (72 loc) · 2.92 KB
/
Copy pathProgram.cs
File metadata and controls
90 lines (72 loc) · 2.92 KB
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
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Diagnostics.Runtime;
namespace ClrMdExample
{
partial class Program
{
static void Main()
{
var exePath = CreateSampleApplication();
var target = DataTarget.AttachToProcess(Process.Start(exePath).Id, uint.MaxValue);
var clr = target.ClrVersions.First().CreateRuntime();
Console.WriteLine("*** Threads");
clr.Threads
.Select(thread => thread.StackTrace.Select(frame => frame.DisplayString))
.ToList()
.ForEach
(
frame =>
{
Console.WriteLine("*** Stack trace for thread");
frame.ToList().ForEach(Console.WriteLine);
}
);
Console.WriteLine("*** Strings in the heap");
var objects = clr.GetHeap()
.EnumerateObjectAddresses()
.Select(address => new { Type = clr.GetHeap().GetObjectType(address), Address = address })
.ToList();
objects.ForEach
(
o => o.Type
.Fields
.Where(f => f.Type.IsString)
.Select(field => field.GetValue(o.Address)?.ToString().Trim())
.Where(s => !string.IsNullOrEmpty(s))
.OrderByDescending(s => s.Length)
.ToList()
.ForEach(Console.WriteLine)
);
Console.ReadKey();
Console.WriteLine("*** Biggest 10 objects in the heap");
objects.OrderByDescending(o => o.Type.GetSize(o.Address))
.Take(10)
.ToList()
.ForEach
(
o =>
{
Console.WriteLine($"*** Type: {o.Type.Name}");
o.Type
.Fields
.ToList()
.ForEach
(
f =>
{
if (!f.HasSimpleValue)
return;
var value = f.GetValue(o.Address, false, true)?.ToString().Trim();
Console.WriteLine($"{value}");
}
);
}
);
Console.ReadKey();
File.Delete(exePath);
}
}
}