Skip to content

Commit 33e981e

Browse files
author
Michael DeRoy
committed
Cleanup Pmip
1 parent 859ae65 commit 33e981e

File tree

3 files changed

+82
-92
lines changed

3 files changed

+82
-92
lines changed

FuzzyRangeComparer.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.Collections.Generic;
2+
3+
namespace PmipMyCallStack
4+
{
5+
struct Range
6+
{
7+
public ulong Start;
8+
public ulong End;
9+
public string Name;
10+
}
11+
class FuzzyRangeComparer : IComparer<Range>
12+
{
13+
public int Compare(Range x, Range y)
14+
{
15+
if (x.Name == null && y.Start <= x.Start && y.End >= x.Start)
16+
{
17+
return 0;
18+
}
19+
20+
if (y.Name == null && x.Start <= y.Start && x.End >= y.Start)
21+
{
22+
return 0;
23+
}
24+
25+
return x.Start.CompareTo(y.Start);
26+
}
27+
}
28+
}

PmipCallStackFilter.cs

Lines changed: 52 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ namespace PmipMyCallStack
99
{
1010
public class PmipCallStackFilter : IDkmCallStackFilter
1111
{
12-
private static Range[] IPs;
13-
private static long previousFileLength;
14-
static FuzzyRangeComparer comparer = new FuzzyRangeComparer();
12+
private static Range[] _rangesSortedByIp;
13+
private static long _previousFileLength;
14+
private static FuzzyRangeComparer _comparer = new FuzzyRangeComparer();
1515

1616
public DkmStackWalkFrame[] FilterNextFrame(DkmStackContext stackContext, DkmStackWalkFrame input)
1717
{
@@ -30,105 +30,67 @@ public DkmStackWalkFrame[] FilterNextFrame(DkmStackContext stackContext, DkmStac
3030

3131
return new[] { PmipStackFrame(stackContext, input) };
3232
}
33-
struct Range
34-
{
35-
public ulong Start;
36-
public ulong End;
37-
public string Name;
38-
}
39-
40-
class FuzzyRangeComparer : IComparer<Range>
41-
{
42-
public int Compare(Range x, Range y)
43-
{
44-
if (x.Name == null && y.Start <= x.Start && y.End >= x.Start)
45-
{
46-
return 0;
47-
}
48-
49-
if (y.Name == null && x.Start <= y.Start && x.End >= y.Start)
50-
{
51-
return 0;
52-
}
53-
54-
return x.Start.CompareTo(y.Start);
55-
}
56-
}
5733

58-
public static bool TryGetDescriptionForIP(ulong ip, out string name)
34+
public static DkmStackWalkFrame PmipStackFrame(DkmStackContext stackContext, DkmStackWalkFrame frame)
5935
{
60-
name = string.Empty;
61-
if (IPs == null)
62-
return false;
63-
int index = Array.BinarySearch(IPs, new Range() {Start = ip}, comparer);
64-
int linearIndex = -1;
65-
for (var i =0; i < IPs.Length; i++)
66-
{
67-
var item = IPs[i];
68-
if (ip > item.Start && ip < item.End)
69-
{
70-
linearIndex = i;
71-
break;
72-
}
73-
}
74-
75-
76-
if (linearIndex == -1)
77-
{
78-
if (index >= 0)
79-
GC.KeepAlive(name);
80-
return false;
81-
}
82-
83-
if (linearIndex != index)
84-
GC.KeepAlive(name);
36+
var fileName = Path.Combine(Path.GetTempPath(), "pmip." + frame.Process.LivePart.Id);
37+
RefreshStackData(fileName);
38+
string name = null;
39+
if (TryGetDescriptionForIp(frame.InstructionAddress.CPUInstructionPart.InstructionPointer, out name))
40+
return DkmStackWalkFrame.Create(
41+
stackContext.Thread,
42+
frame.InstructionAddress,
43+
frame.FrameBase,
44+
frame.FrameSize,
45+
frame.Flags,
46+
name,
47+
frame.Registers,
48+
frame.Annotations);
8549

86-
name = IPs[linearIndex].Name;
87-
return true;
50+
return frame;
8851
}
8952

9053
public static void RefreshStackData(string fileName)
9154
{
9255
try
9356
{
9457
if (!File.Exists(fileName))
95-
{
96-
File.WriteAllText(fileName, string.Empty);
9758
return;
98-
}
9959

10060
var fileInfo = new FileInfo(fileName);
101-
if (fileInfo.Length == previousFileLength)
61+
if (fileInfo.Length == _previousFileLength)
10262
return;
10363

104-
var list = new List<Range>(1000);
64+
var list = new List<Range>(10000);
10565
using (var inStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
106-
using (var file = new StreamReader(inStream))
10766
{
108-
string line;
109-
while ((line = file.ReadLine()) != null)
67+
using (var file = new StreamReader(inStream))
11068
{
111-
const char delemiter = ';';
112-
string[] tokens = line.Split(delemiter);
69+
string line;
70+
while ((line = file.ReadLine()) != null)
71+
{
72+
const char delemiter = ';';
73+
var tokens = line.Split(delemiter);
11374

114-
//should never happen, but lets be safe and not get array out of bounds if it does
115-
if (tokens.Length != 3)
116-
continue;
75+
//should never happen, but lets be safe and not get array out of bounds if it does
76+
if (tokens.Length != 3)
77+
continue;
11778

118-
string startip = tokens[0];
119-
string endip = tokens[1];
120-
string description = tokens[2];
79+
var startip = tokens[0];
80+
var endip = tokens[1];
81+
var description = tokens[2];
12182

122-
var startipint = ulong.Parse(startip, NumberStyles.HexNumber);
123-
var endipint = ulong.Parse(endip, NumberStyles.HexNumber);
83+
var startiplong = ulong.Parse(startip, NumberStyles.HexNumber);
84+
var endipint = ulong.Parse(endip, NumberStyles.HexNumber);
12485

125-
list.Add(new Range() { Name = description, Start = startipint, End = endipint });
86+
list.Add(new Range() { Name = description, Start = startiplong, End = endipint });
87+
}
12688
}
12789
}
12890

12991
list.Sort((r1, r2) => r1.Start.CompareTo(r2.Start));
130-
IPs = list.ToArray();
131-
previousFileLength = fileInfo.Length;
92+
_rangesSortedByIp = list.ToArray();
93+
_previousFileLength = fileInfo.Length;
13294
}
13395
catch (Exception ex)
13496
{
@@ -137,23 +99,22 @@ public static void RefreshStackData(string fileName)
13799

138100
}
139101

140-
public static DkmStackWalkFrame PmipStackFrame(DkmStackContext stackContext, DkmStackWalkFrame frame)
102+
public static bool TryGetDescriptionForIp(ulong ip, out string name)
141103
{
142-
var fileName = Path.Combine(Path.GetTempPath(), "pmip." + frame.Process.LivePart.Id);
143-
RefreshStackData(fileName);
144-
string name = null;
145-
if (TryGetDescriptionForIP(frame.InstructionAddress.CPUInstructionPart.InstructionPointer, out name))
146-
return DkmStackWalkFrame.Create(
147-
stackContext.Thread,
148-
frame.InstructionAddress,
149-
frame.FrameBase,
150-
frame.FrameSize,
151-
frame.Flags,
152-
name,
153-
frame.Registers,
154-
frame.Annotations);
104+
name = string.Empty;
155105

156-
return frame;
106+
if (_rangesSortedByIp == null)
107+
return false;
108+
109+
var rangeToFindIp = new Range() { Start = ip };
110+
var index = Array.BinarySearch(_rangesSortedByIp, rangeToFindIp, _comparer);
111+
112+
if (index < 0)
113+
return false;
114+
115+
name = _rangesSortedByIp[index].Name;
116+
117+
return true;
157118
}
158119
}
159120
}

PmipMyCallStack.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<PropertyGroup>
44
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
@@ -58,6 +58,7 @@
5858
</Reference>
5959
</ItemGroup>
6060
<ItemGroup>
61+
<Compile Include="FuzzyRangeComparer.cs" />
6162
<Compile Include="PmipCallStackFilter.cs" />
6263
<Compile Include="Properties\AssemblyInfo.cs" />
6364
</ItemGroup>

0 commit comments

Comments
 (0)