forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOneScriptDebuggerClient.cs
More file actions
206 lines (171 loc) · 6.02 KB
/
OneScriptDebuggerClient.cs
File metadata and controls
206 lines (171 loc) · 6.02 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System.Runtime.CompilerServices;
using OneScript.DebugProtocol;
using OneScript.DebugProtocol.Abstractions;
using OneScript.DebugProtocol.TcpServer;
using Serilog;
using VSCode.DebugAdapter.Transport;
namespace VSCode.DebugAdapter
{
public class OneScriptDebuggerClient : IDebuggerService
{
private readonly IDebugEventListener _eventBackChannel;
private readonly IMessageChannel _commandsChannel;
private RpcProcessor _processor;
private readonly ILogger Log = Serilog.Log.ForContext<OneScriptDebuggerClient>();
public OneScriptDebuggerClient(
IMessageChannel commandsChannel,
IDebugEventListener eventBackChannel,
int protocolVersion)
{
_commandsChannel = commandsChannel;
_eventBackChannel = eventBackChannel;
ProtocolVersion = protocolVersion;
}
public int ProtocolVersion { get; }
public void Start()
{
RunEventsListener(_commandsChannel);
}
public void Stop()
{
_processor.Stop();
}
private void RunEventsListener(IMessageChannel channelToListen)
{
var server = new DefaultMessageServer<TcpProtocolDtoBase>(channelToListen);
server.ServerThreadName = "dbg-client-event-listener";
_processor = new RpcProcessor(server);
_processor.AddChannel(
nameof(IDebugEventListener),
typeof(IDebugEventListener),
_eventBackChannel);
_processor.AddChannel(
nameof(IDebuggerService),
typeof(IDebuggerService),
this);
_processor.Start();
Log.Debug("Debuggee event listener started");
}
private void WriteCommand<T>(T data, [CallerMemberName] string command = "")
{
Log.Verbose("Sending {Command} to debuggee, param {@Parameter}", command, data);
var dto = RpcCall.Create(nameof(IDebuggerService), command, data);
_commandsChannel.Write(dto);
Log.Verbose("Successfully written: {Command}", command);
}
private void WriteCommand(object[] data, [CallerMemberName] string command = "")
{
Log.Verbose("Sending {Command} to debuggee, params {@Parameters}", command, data);
var dto = RpcCall.Create(nameof(IDebuggerService), command, data);
_commandsChannel.Write(dto);
Log.Verbose("Successfully written: {Command}", command);
}
private T GetResponse<T>()
{
var rpcResult = _processor.GetResult();
Log.Verbose("Response received {Result} = {@Value}", rpcResult.Id, rpcResult.ReturnValue);
if (rpcResult.ReturnValue is RpcExceptionDto excDto)
{
Log.Verbose("RPC Exception received: {Description}", excDto.Description);
throw new RpcOperationException(excDto);
}
return (T) rpcResult.ReturnValue;
}
public void Execute(int threadId)
{
WriteCommand(threadId);
}
public void SetMachineExceptionBreakpoints((string Id, string Condition)[] filters)
{
WriteCommand(filters);
}
public void SetExceptionBreakpoints(ExceptionBreakpointFilter[] filters)
{
WriteCommand(filters);
}
public Breakpoint[] SetMachineBreakpoints(Breakpoint[] breaksToSet)
{
WriteCommand(breaksToSet);
return GetResponse<Breakpoint[]>();
}
public StackFrame[] GetStackFrames(int threadId)
{
WriteCommand(threadId);
return GetResponse<StackFrame[]>();
}
public Variable[] GetVariables(int threadId, int frameIndex, int[] path)
{
WriteCommand(new object[]
{
threadId,
frameIndex,
path
});
return GetResponse<Variable[]>();
}
public Variable[] GetModuleVariables(int threadId, int frameIndex, int[] path)
{
WriteCommand(new object[]
{
threadId,
frameIndex,
path
});
return GetResponse<Variable[]>();
}
public Variable[] GetEvaluatedVariables(string expression, int threadId, int frameIndex, int[] path)
{
WriteCommand(new object[]
{
expression,
threadId,
frameIndex,
path
});
return GetResponse<Variable[]>();
}
public Variable Evaluate(int threadId, int contextFrame, string expression)
{
WriteCommand(new object[]
{
threadId,
contextFrame,
expression
});
return GetResponse<Variable>();
}
public void Next(int threadId)
{
WriteCommand(threadId);
}
public void StepIn(int threadId)
{
WriteCommand(threadId);
}
public void StepOut(int threadId)
{
WriteCommand(threadId);
}
public void Disconnect(bool terminate)
{
WriteCommand(terminate);
Stop();
}
public int[] GetThreads()
{
WriteCommand(null);
return GetResponse<int[]>();
}
public int GetProcessId()
{
WriteCommand(null);
return GetResponse<int>();
}
}
}