@@ -9,9 +9,9 @@ namespace PmipMyCallStack
9
9
{
10
10
public class PmipCallStackFilter : IDkmCallStackFilter
11
11
{
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 ( ) ;
15
15
16
16
public DkmStackWalkFrame [ ] FilterNextFrame ( DkmStackContext stackContext , DkmStackWalkFrame input )
17
17
{
@@ -30,105 +30,67 @@ public DkmStackWalkFrame[] FilterNextFrame(DkmStackContext stackContext, DkmStac
30
30
31
31
return new [ ] { PmipStackFrame ( stackContext , input ) } ;
32
32
}
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
- }
57
33
58
- public static bool TryGetDescriptionForIP ( ulong ip , out string name )
34
+ public static DkmStackWalkFrame PmipStackFrame ( DkmStackContext stackContext , DkmStackWalkFrame frame )
59
35
{
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 ) ;
85
49
86
- name = IPs [ linearIndex ] . Name ;
87
- return true ;
50
+ return frame ;
88
51
}
89
52
90
53
public static void RefreshStackData ( string fileName )
91
54
{
92
55
try
93
56
{
94
57
if ( ! File . Exists ( fileName ) )
95
- {
96
- File . WriteAllText ( fileName , string . Empty ) ;
97
58
return ;
98
- }
99
59
100
60
var fileInfo = new FileInfo ( fileName ) ;
101
- if ( fileInfo . Length == previousFileLength )
61
+ if ( fileInfo . Length == _previousFileLength )
102
62
return ;
103
63
104
- var list = new List < Range > ( 1000 ) ;
64
+ var list = new List < Range > ( 10000 ) ;
105
65
using ( var inStream = new FileStream ( fileName , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
106
- using ( var file = new StreamReader ( inStream ) )
107
66
{
108
- string line ;
109
- while ( ( line = file . ReadLine ( ) ) != null )
67
+ using ( var file = new StreamReader ( inStream ) )
110
68
{
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 ) ;
113
74
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 ;
117
78
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 ] ;
121
82
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 ) ;
124
85
125
- list . Add ( new Range ( ) { Name = description , Start = startipint , End = endipint } ) ;
86
+ list . Add ( new Range ( ) { Name = description , Start = startiplong , End = endipint } ) ;
87
+ }
126
88
}
127
89
}
128
90
129
91
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 ;
132
94
}
133
95
catch ( Exception ex )
134
96
{
@@ -137,23 +99,22 @@ public static void RefreshStackData(string fileName)
137
99
138
100
}
139
101
140
- public static DkmStackWalkFrame PmipStackFrame ( DkmStackContext stackContext , DkmStackWalkFrame frame )
102
+ public static bool TryGetDescriptionForIp ( ulong ip , out string name )
141
103
{
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 ;
155
105
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 ;
157
118
}
158
119
}
159
120
}
0 commit comments