-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBreakpointManager.cs
More file actions
180 lines (163 loc) · 4.47 KB
/
Copy pathBreakpointManager.cs
File metadata and controls
180 lines (163 loc) · 4.47 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
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
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Microsoft.Diagnostics.Runtime.Interop;
namespace PSExt
{
[SuppressMessage("ReSharper", "SuspiciousTypeConversion.Global")]
public class BreakpointManager
{
private readonly IDebugControl5 _control5;
private readonly StringBuilder _builder = new StringBuilder(256);
private readonly Symbols _symbols;
public BreakpointManager(IDebugClient6 client)
{
_control5 = (IDebugControl5) client;
_symbols = new Symbols((IDebugSymbols3) client);
}
private uint GetBreakpointCount()
{
uint number;
var res = _control5.GetNumberBreakpoints(out number);
if (res != 0)
{
ErrorHelper.ThrowDebuggerException(res, "IDebugControl5::GetNumberBreakpoints");
}
return number;
}
public uint AddBreakpointOffset(ulong offset, BreakpointData data)
{
IDebugBreakpoint2 pBp;
var res = _control5.AddBreakpoint2(
(DEBUG_BREAKPOINT_TYPE) data.BreakType,
data.Id,
out pBp);
if (res != 0)
{
ErrorHelper.ThrowDebuggerException(res, "IDebugControl5.AddBreakpoint2");
}
pBp.SetOffset(offset);
{
pBp.SetCommandWide(data.Command);
}
if (data.PassCount != 0)
{
pBp.SetPassCount(data.PassCount);
}
if (data.MatchThread != 0)
{
pBp.SetMatchThreadId(data.MatchThread);
}
if (data.MatchThread != 0)
{
pBp.SetMatchThreadId(data.MatchThread);
}
if (data.BreakType == BreakType.Data)
{
pBp.SetDataParameters(data.DataSize, (DEBUG_BREAKPOINT_ACCESS_TYPE) data.DataBreakpointKind);
}
if (data.Flags != BreakpointFlags.None)
{
pBp.AddFlags((DEBUG_BREAKPOINT_FLAG) data.Flags);
}
uint newId;
res = pBp.GetId( out newId);
if (res != 0)
{
ErrorHelper.ThrowDebuggerException(res, "IDebugBreakpoint2.GetId");
}
return newId;
}
public IList<BreakpointData> GetBreakpoints()
{
const uint DEBUG_ANY_ID = 0xffffffff;
var count = GetBreakpointCount();
var retval = new List<BreakpointData>((int)count);
if (count != 0)
{
var bps = new DEBUG_BREAKPOINT_PARAMETERS[count];
var res = _control5.GetBreakpointParameters(count, null, 0, bps);
if (res != 0)
{
ErrorHelper.ThrowDebuggerException(res, "_control5.GetBreakpointParameters");
}
foreach (var bp in bps.Where(bp => bp.Id != DEBUG_ANY_ID))
{
IDebugBreakpoint2 bp2;
res = _control5.GetBreakpointById2(bp.Id, out bp2);
if (res != 0)
{
continue;
}
var command = GetCommand(bp2, bp.CommandSize);
var offsetExpression = GetOffsetExpression(bp2, bp.OffsetExpressionSize);
ulong offset;
bp2.GetOffset(out offset);
var p = bp;
var k = (BreakType)p.BreakType;
var flags = (BreakpointFlags)p.Flags;
var dk = (DataBreakpointAccessTypes)p.DataAccessType;
var bpd = new BreakpointData(p.Offset, k, flags, dk, p.DataSize, p.ProcType, p.MatchThread,
p.Id, p.PassCount, p.CurrentPassCount, command, offsetExpression);
retval.Add(bpd);
}
}
return retval;
}
string GetCommand(IDebugBreakpoint2 bp, uint commandSize)
{
_builder.EnsureCapacity((int) commandSize);
if (commandSize != 0)
{
uint size;
var res = bp.GetCommandWide(_builder, _builder.Capacity, out size);
if (res!=0)
{
ErrorHelper.ThrowDebuggerException(res, "IDebugBreakpoint2.GetCommandWide");
}
}
return _builder.ToString();
}
private string GetOffsetExpression(IDebugBreakpoint2 bp, uint expressionSize)
{
if (expressionSize != 0)
{
_builder.EnsureCapacity((int) expressionSize);
var res = bp.GetOffsetExpressionWide(_builder,_builder.Capacity, out expressionSize);
if (res != 0)
{
ErrorHelper.ThrowDebuggerException(res, "IDebugBreakpoint2.GetOffsetExpressionWide");
}
}
return _builder.ToString();
}
public IList<BreakpointData> AddBreakpoints(BreakpointData data)
{
var ids = new List<uint>();
uint bpid;
if (data.Expression != null)
{
var pattern = data.Expression;
IList<SymbolSearchResult> matches = _symbols.GetMatchingSymbols(pattern);
if (matches.Count == 0)
{
// do nothing
}
else {
foreach (var match in matches)
{
bpid = AddBreakpointOffset(match.Offset, data);
ids.Add(bpid);
}
}
}
else {
bpid = AddBreakpointOffset(data.Offset, data);
ids.Add(bpid);
}
var bps = GetBreakpoints();
return bps.Where(bp => ids.Contains(bp.Id)).ToList();
}
}
}