-
Notifications
You must be signed in to change notification settings - Fork 3
/
Fairy.Debugger.Breakpoint.cs
201 lines (194 loc) · 10.7 KB
/
Fairy.Debugger.Breakpoint.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
using Neo.Json;
using Neo.SmartContract.Native;
using System.Collections.Concurrent;
namespace Neo.Plugins
{
public partial class Fairy
{
readonly ConcurrentDictionary<UInt160, HashSet<uint>> contractScriptHashToAssemblyBreakpoints = new();
readonly ConcurrentDictionary<UInt160, HashSet<SourceFilenameAndLineNum>> contractScriptHashToSourceCodeBreakpoints = new();
[RpcMethod]
protected virtual JToken SetAssemblyBreakpoints(JArray _params)
{
UInt160 scriptHash = UInt160.Parse(_params[0]!.AsString());
//if (!contractScriptHashToInstructionPointerToOpCode.ContainsKey(scriptHash))
//{
// string? contractName = NativeContract.ContractManagement.GetContract(system.StoreView, scriptHash)?.Manifest.Name;
// throw new ArgumentException($"Scripthash {scriptHash} {contractName} not registered for debugging. Call SetDebugInfo(scriptHash, nefDbgNfo, dumpNef) first");
//}
HashSet<uint>? assemblyBreakpoints;
if (!contractScriptHashToAssemblyBreakpoints.TryGetValue(scriptHash, out assemblyBreakpoints))
{
assemblyBreakpoints = new HashSet<uint>();
contractScriptHashToAssemblyBreakpoints[scriptHash] = assemblyBreakpoints;
}
JObject json = new();
for (int i = 1; i < _params.Count; i++)
{
string breakpointInstructionPointerStr = _params[i]!.AsString();
uint breakpointInstructionPointer = uint.Parse(breakpointInstructionPointerStr);
json[breakpointInstructionPointerStr] = assemblyBreakpoints.Add(breakpointInstructionPointer);
if (contractScriptHashToInstructionPointerToOpCode.TryGetValue(scriptHash, out Dictionary<uint, VM.OpCode>? instructionPointerToOpCode))
{// A contract that has registered debuginfo
if (!instructionPointerToOpCode.ContainsKey(breakpointInstructionPointer))
throw new ArgumentException($"No instruction at InstructionPointer={breakpointInstructionPointer}");
// TODO: we can check whether the addr is valid, without the debuginfo and instructionPointerToOpCode
}
// else this is a contract without debug info registration. Do nothing.
}
return json;
}
[RpcMethod]
protected virtual JToken ListAssemblyBreakpoints(JArray _params)
{
UInt160 scriptHash = UInt160.Parse(_params[0]!.AsString());
if (!contractScriptHashToInstructionPointerToOpCode.ContainsKey(scriptHash))
{
string? contractName = NativeContract.ContractManagement.GetContract(system.StoreView, scriptHash)?.Manifest.Name;
throw new ArgumentException($"Scripthash {scriptHash} {contractName} not registered for debugging. Call SetDebugInfo(scriptHash, nefDbgNfo, dumpNef) first");
}
List<uint> assemblyBreakpoints = contractScriptHashToAssemblyBreakpoints[scriptHash].ToList();
assemblyBreakpoints.Sort();
JArray breakpointList = [.. assemblyBreakpoints];
return breakpointList;
}
[RpcMethod]
protected virtual JToken DeleteAssemblyBreakpoints(JArray _params)
{
UInt160 scriptHash = UInt160.Parse(_params[0]!.AsString());
if (!contractScriptHashToInstructionPointerToOpCode.ContainsKey(scriptHash))
{
string? contractName = NativeContract.ContractManagement.GetContract(system.StoreView, scriptHash)?.Manifest.Name;
throw new ArgumentException($"Scripthash {scriptHash} {contractName} not registered for debugging. Call SetDebugInfo(scriptHash, nefDbgNfo, dumpNef) first");
}
JObject json = new();
if (!contractScriptHashToAssemblyBreakpoints.ContainsKey(scriptHash))
return json;
if (_params.Count == 1) // delete all breakpoints
{
List<uint> assemblyBreakpoints = contractScriptHashToAssemblyBreakpoints[scriptHash].ToList();
assemblyBreakpoints.Sort();
foreach (uint breakpointInstructionPointer in assemblyBreakpoints)
{
json[breakpointInstructionPointer.ToString()] = true;
}
contractScriptHashToAssemblyBreakpoints[scriptHash].Clear();
}
else
{
HashSet<uint> assemblyBreakpoints = contractScriptHashToAssemblyBreakpoints[scriptHash];
for (int i = 1; i < _params.Count; i++)
{
string breakpointInstructionPointerStr = _params[i]!.AsString();
uint breakpointInstructionPointer = uint.Parse(breakpointInstructionPointerStr);
json[breakpointInstructionPointerStr] = assemblyBreakpoints.Remove(breakpointInstructionPointer);
}
}
return json;
}
[RpcMethod]
protected virtual JToken SetSourceCodeBreakpoints(JArray _params)
{
UInt160 scriptHash = UInt160.Parse(_params[0]!.AsString());
if (!contractScriptHashToAllSourceLineNums.ContainsKey(scriptHash))
{
string? contractName = NativeContract.ContractManagement.GetContract(system.StoreView, scriptHash)?.Manifest.Name;
throw new ArgumentException($"Scripthash {scriptHash} {contractName} not registered for debugging. Call SetDebugInfo(scriptHash, nefDbgNfo, dumpNef) first");
}
HashSet<SourceFilenameAndLineNum>? sourceCodeBreakpoints;
if (!contractScriptHashToSourceCodeBreakpoints.TryGetValue(scriptHash, out sourceCodeBreakpoints))
{
sourceCodeBreakpoints = new HashSet<SourceFilenameAndLineNum>();
contractScriptHashToSourceCodeBreakpoints[scriptHash] = sourceCodeBreakpoints;
}
JArray breakpointList = new();
int i = 1;
while (_params.Count > i)
{
string sourceCodeFilename = _params[i]!.AsString();
i++;
uint sourceCodeBreakpointLineNum = uint.Parse(_params[i]!.AsString());
i++;
JObject json = new();
SourceFilenameAndLineNum breakpoint = new SourceFilenameAndLineNum { sourceFilename = sourceCodeFilename, lineNum = sourceCodeBreakpointLineNum };
if (contractScriptHashToAllSourceLineNums[scriptHash].Contains(breakpoint))
{
sourceCodeBreakpoints.Add(breakpoint);
json["filename"] = sourceCodeFilename;
json["line"] = sourceCodeBreakpointLineNum;
breakpointList.Add(json);
}
else
{
throw new ArgumentException($"No code at filename={sourceCodeFilename}, line={sourceCodeBreakpointLineNum}");
}
}
return breakpointList;
}
[RpcMethod]
protected virtual JToken ListSourceCodeBreakpoints(JArray _params)
{
UInt160 scriptHash = UInt160.Parse(_params[0]!.AsString());
if (!contractScriptHashToAllSourceLineNums.ContainsKey(scriptHash))
{
string? contractName = NativeContract.ContractManagement.GetContract(system.StoreView, scriptHash)?.Manifest.Name;
throw new ArgumentException($"Scripthash {scriptHash} {contractName} not registered for debugging. Call SetDebugInfo(scriptHash, nefDbgNfo, dumpNef) first");
}
List<SourceFilenameAndLineNum> sourceCodeBreakpoints = contractScriptHashToSourceCodeBreakpoints[scriptHash].OrderBy(p => p.sourceFilename).ThenBy(p => p.lineNum).ToList();
JArray breakpointList = new();
foreach (SourceFilenameAndLineNum sourceCodeBreakpointLineNum in sourceCodeBreakpoints)
{
JObject breakpoint = new JObject();
breakpoint["filename"] = sourceCodeBreakpointLineNum.sourceFilename;
breakpoint["line"] = sourceCodeBreakpointLineNum.lineNum;
breakpointList.Add(breakpoint);
}
return breakpointList;
}
[RpcMethod]
protected virtual JToken DeleteSourceCodeBreakpoints(JArray _params)
{
UInt160 scriptHash = UInt160.Parse(_params[0]!.AsString());
if (!contractScriptHashToAllSourceLineNums.ContainsKey(scriptHash))
{
string? contractName = NativeContract.ContractManagement.GetContract(system.StoreView, scriptHash)?.Manifest.Name;
throw new ArgumentException($"Scripthash {scriptHash} {contractName} not registered for debugging. Call SetDebugInfo(scriptHash, nefDbgNfo, dumpNef) first");
}
JArray breakpointList = new();
if (!contractScriptHashToSourceCodeBreakpoints.ContainsKey(scriptHash))
return breakpointList;
if (_params.Count == 1) // delete all breakpoints
{
List<SourceFilenameAndLineNum> sourceCodeBreakpoints = contractScriptHashToSourceCodeBreakpoints[scriptHash].OrderBy(p => p.sourceFilename).ThenBy(p => p.lineNum).ToList();
foreach (SourceFilenameAndLineNum sourceCodeBreakpointLineNum in sourceCodeBreakpoints)
{
JObject json = new();
json["filename"] = sourceCodeBreakpointLineNum.sourceFilename;
json["line"] = sourceCodeBreakpointLineNum.lineNum;
breakpointList.Add(json);
}
contractScriptHashToSourceCodeBreakpoints[scriptHash].Clear();
}
else
{
HashSet<SourceFilenameAndLineNum> sourceCodeBreakpoints = contractScriptHashToSourceCodeBreakpoints[scriptHash];
int i = 1;
while (_params.Count > i)
{
string sourceCodeBreakpointFilename = _params[i]!.AsString();
i++;
uint sourceCodeBreakpointLineNum = uint.Parse(_params[i]!.AsString());
i++;
if (sourceCodeBreakpoints.Remove(new SourceFilenameAndLineNum { sourceFilename = sourceCodeBreakpointFilename, lineNum = sourceCodeBreakpointLineNum }))
{
JObject json = new();
json["filename"] = sourceCodeBreakpointFilename;
json["line"] = sourceCodeBreakpointLineNum;
breakpointList.Add(json);
}
}
}
return breakpointList;
}
}
}