Skip to content
Navigation Menu
{{ message }}
forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOscriptDebugSession.cs
More file actions
524 lines (449 loc) · 18.9 KB
/
Copy pathOscriptDebugSession.cs
File metadata and controls
524 lines (449 loc) · 18.9 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*----------------------------------------------------------
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;
using System.Collections.Generic;
using System.IO;
using System.Runtime.CompilerServices;
using OneScript.DebugProtocol;
using Serilog;
using VSCode.DebugAdapter.Transport;
using VSCodeDebug;
namespace VSCode.DebugAdapter
{
internal class OscriptDebugSession : DebugSession
{
private DebugeeProcess _debuggee;
private bool _startupPerformed = false;
private ThreadStateContainer _threadState = new ThreadStateContainer();
private readonly ILogger Log = Serilog.Log.ForContext<OscriptDebugSession>();
public OscriptDebugSession() : base(true, false)
{
}
private string AdapterID { get; set; }
public override void Initialize(Response response, dynamic args)
{
LogCommandReceived();
AdapterID = (string) args.adapterID;
_debuggee = DebugeeFactory.CreateProcess(AdapterID, PathStrategy);
SendResponse(response, new Capabilities
{
supportsConditionalBreakpoints = true,
supportsFunctionBreakpoints = false,
supportsConfigurationDoneRequest = true,
supportsExceptionFilterOptions = true,
exceptionBreakpointFilters = new dynamic[]
{
new
{
filter = "uncaught",
label = "Необработанные исключения",
description = "Остановка при возникновении необработанного исключения",
supportsCondition = true,
conditionDescription = "Искомая подстрока текста исключения"
},
new
{
filter = "all",
label = "Все исключения",
description = "Остановка при возникновении любого исключения",
supportsCondition = true,
conditionDescription = "Искомая подстрока текста исключения"
}
},
supportsEvaluateForHovers = true,
supportTerminateDebuggee = true
});
SendEvent(new InitializedEvent());
}
public override void Launch(Response response, dynamic args)
{
LogCommandReceived();
try
{
Log.Debug("Initializing process settings");
_debuggee.Init(args);
}
catch (InvalidDebugeeOptionsException e)
{
Log.Error(e, "Wrong options received {ErrorCode}: {Message}", e.ErrorCode, e.Message);
SendErrorResponse(response, e.ErrorCode, e.Message);
return;
}
SubscribeForDebuggeeProcessEvents();
try
{
Log.Verbose("Starting debuggee");
_debuggee.Start();
Log.Information("Debuggee started");
}
catch (Exception e)
{
Log.Error(e, "Can't launch debuggee");
SendErrorResponse(response, 3012, "Can't launch debugee ({reason}).", new { reason = e.Message });
return;
}
DebugClientFactory debugClientFactory;
try
{
debugClientFactory = ConnectDebugServer();
}
catch (Exception e)
{
_debuggee.Kill();
SendEvent(new TerminatedEvent());
Log.Error(e, "Can't connect to debug server");
SendErrorResponse(response, 4550, "Can't connect: " + e.ToString());
return;
}
_debuggee.SetClient(debugClientFactory.CreateDebugClient());
SendResponse(response);
}
private void SubscribeForDebuggeeProcessEvents()
{
_debuggee.OutputReceived += (s, e) =>
{
Log.Debug("Output received {Output}", e.Content);
SendOutput(e.Category, e.Content);
};
_debuggee.ProcessExited += (s, e) =>
{
Log.Information("Debuggee has exited");
SendEvent(new TerminatedEvent());
};
}
public override void Attach(Response response, dynamic arguments)
{
LogCommandReceived();
SubscribeForDebuggeeProcessEvents();
_debuggee.DebugPort = GetFromContainer(arguments, "debugPort", 2801);
_debuggee.InitPathsMapper(arguments);
DebugClientFactory debugClientFactory;
try
{
debugClientFactory = ConnectDebugServer();
}
catch (Exception e)
{
Log.Error(e, "Can't connect debuggee");
SendErrorResponse(response, 4550, "Can't connect: " + e.ToString());
return;
}
_debuggee.SetClient(debugClientFactory.CreateDebugClient());
try
{
_debuggee.InitAttached();
}
catch (Exception e)
{
Log.Error(e, "Attach failed");
SendErrorResponse(response, 4550, "Attach failed: " + e.ToString());
return;
}
SendResponse(response);
}
private DebugClientFactory ConnectDebugServer()
{
var tcpConnection = ConnectionFactory.Connect(_debuggee.DebugPort);
var listener = new OscriptDebugEventsListener(this, _threadState);
return new DebugClientFactory(tcpConnection, listener);
}
public override void Disconnect(Response response, dynamic arguments)
{
LogCommandReceived(new { Terminate = arguments.terminateDebuggee });
bool terminateDebuggee = arguments.terminateDebuggee == true;
_debuggee.HandleDisconnect(terminateDebuggee);
SendResponse(response);
}
public override void SetExceptionBreakpoints(Response response, dynamic arguments)
{
LogCommandReceived();
var acceptedFilters = new List<VSCodeDebug.Breakpoint>();
var filters = new List<(string Id, string Condition)>();
foreach(var filter in arguments.filters)
{
filters.Add((filter, ""));
acceptedFilters.Add(new VSCodeDebug.Breakpoint(true));
}
foreach (var filterOption in arguments.filterOptions)
{
filters.Add((filterOption.filterId, filterOption.condition ?? ""));
acceptedFilters.Add(new VSCodeDebug.Breakpoint(true));
}
_debuggee.SetExceptionsBreakpoints(filters.ToArray());
SendResponse(response, new SetExceptionBreakpointsResponseBody(acceptedFilters));
}
public override void SetBreakpoints(Response response, dynamic arguments)
{
LogCommandReceived();
if ((bool)arguments.sourceModified)
{
if (_startupPerformed)
{
SendErrorResponse(response, 1102, "Нельзя установить точку останова на модифицированный файл.");
return;
}
SendResponse(response, new SetBreakpointsResponseBody());
return;
}
var path = (string) arguments.source.path;
path = ConvertClientPathToDebugger(path);
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
// vscode иногда передает путь, где диск - маленькая буква
path = NormalizeDriveLetter(path);
}
var breaks = new List<OneScript.DebugProtocol.Breakpoint>();
var useConditions = _debuggee.ProtocolVersion >= ProtocolVersions.Version2;
foreach (var srcBreakpoint in arguments.breakpoints)
{
var bpt = new OneScript.DebugProtocol.Breakpoint
{
Line = (int)srcBreakpoint.line,
Source = path,
Condition = useConditions ? srcBreakpoint.condition ?? string.Empty : string.Empty
};
breaks.Add(bpt);
}
if(breaks.Count == 0) // в целях сохранения интерфейса WCF придется сделать костыль на перех. период
{
var bpt = new OneScript.DebugProtocol.Breakpoint
{
Line = 0,
Source = path
};
breaks.Add(bpt);
}
var confirmedBreaks = _debuggee.SetBreakpoints(breaks);
var confirmedBreaksVSCode = new List<VSCodeDebug.Breakpoint>(confirmedBreaks.Length);
for (int i = 0; i < confirmedBreaks.Length; i++)
{
confirmedBreaksVSCode.Add(new VSCodeDebug.Breakpoint(true, confirmedBreaks[i].Line));
}
SendResponse(response, new SetBreakpointsResponseBody(confirmedBreaksVSCode));
}
private string NormalizeDriveLetter(string path)
{
if (Path.IsPathRooted(path))
return path[0].ToString().ToUpperInvariant() + path.Substring(1);
else
return path;
}
public override void ConfigurationDone(Response response, dynamic args)
{
if (_debuggee == null)
{
Log.Debug("Config Done. Process is not started");
SendResponse(response);
return;
}
Log.Debug("Config Done. Process is started, sending Execute");
_debuggee.BeginExecution(-1);
_startupPerformed = true;
SendResponse(response);
}
public override void Continue(Response response, dynamic arguments)
{
LogCommandReceived();
SendResponse(response);
_debuggee.BeginExecution(-1);
}
public override void Next(Response response, dynamic arguments)
{
LogCommandReceived();
SendResponse(response);
lock (_debuggee)
{
if (!_debuggee.HasExited)
{
_debuggee.Next((int)arguments.threadId);
}
}
}
public override void StepIn(Response response, dynamic arguments)
{
LogCommandReceived();
SendResponse(response);
lock (_debuggee)
{
if (!_debuggee.HasExited)
{
_debuggee.StepIn((int)arguments.threadId);
}
}
}
public override void StepOut(Response response, dynamic arguments)
{
LogCommandReceived();
SendResponse(response);
lock (_debuggee)
{
if (!_debuggee.HasExited)
{
_debuggee.StepOut((int)arguments.threadId);
}
}
}
public override void Pause(Response response, dynamic arguments)
{
LogCommandReceived();
throw new NotImplementedException();
}
public override void StackTrace(Response response, dynamic arguments)
{
LogCommandReceived();
var firstFrameIdx = (int?)arguments.startFrame ?? 0;
var limit = (int?) arguments.levels ?? 0;
var threadId = (int) arguments.threadId;
var processFrames = _debuggee.GetStackTrace(threadId, firstFrameIdx, limit);
var frames = new VSCodeDebug.StackFrame[processFrames.Length];
for (int i = 0; i < processFrames.Length; i++)
{
frames[i] = new VSCodeDebug.StackFrame(
_threadState.RegisterFrame(processFrames[i]),
processFrames[i].MethodName,
processFrames[i].GetSource(),
processFrames[i].LineNumber, 0);
}
SendResponse(response, new StackTraceResponseBody(frames));
}
public override void Scopes(Response response, dynamic arguments)
{
LogCommandReceived();
int frameId = GetFromContainer(arguments, "frameId", 0);
var frame = _threadState.GetFrameById(frameId);
if (frame == null)
{
SendErrorResponse(response, 10001, "No active stackframe");
return;
}
var scopes = new List<Scope>();
// Scope 1: Локальные переменные
var localProvider = new LocalScopeProvider(frame.ThreadId, frame.Index);
var localHandle = _threadState.RegisterVariablesProvider(localProvider);
scopes.Add(new Scope("Локальные переменные", localHandle));
// Scope 2: Переменные модуля (начиная с протокола версии 4)
if (_debuggee.ProtocolVersion >= ProtocolVersions.Version4)
{
var moduleProvider = new ModuleScopeProvider(frame.ThreadId, frame.Index);
var moduleHandle = _threadState.RegisterVariablesProvider(moduleProvider);
scopes.Add(new Scope("Переменные модуля", moduleHandle));
}
SendResponse(response, new ScopesResponseBody(scopes.ToArray()));
}
public override void Variables(Response response, dynamic arguments)
{
LogCommandReceived();
int varsHandle = GetFromContainer(arguments, "variablesReference", 0);
var provider = _threadState.GetVariablesProviderById(varsHandle);
if (provider == null)
{
SendErrorResponse(response, 10001, "Invalid variables reference");
return;
}
// Получаем переменные через провайдер
var variables = _debuggee.FetchVariables(provider);
var responseArray = new VSCodeDebug.Variable[variables.Length];
for (int i = 0; i < responseArray.Length; i++)
{
var variable = variables[i];
int childHandle = 0;
if (variable.IsStructured)
{
var childProvider = provider.CreateChildProvider(i);
childHandle = _threadState.RegisterVariablesProvider(childProvider);
}
responseArray[i] = new VSCodeDebug.Variable(
variable.Name,
variable.Presentation,
variable.TypeName,
childHandle);
}
SendResponse(response, new VariablesResponseBody(responseArray));
}
public override void Threads(Response response, dynamic arguments)
{
LogCommandReceived();
var threads = new List<VSCodeDebug.Thread>();
var processThreads = _debuggee.GetThreads();
for (int i = 0; i < processThreads.Length; i++)
{
threads.Add(new VSCodeDebug.Thread(processThreads[i], $"Thread {processThreads[i]}"));
}
SendResponse(response, new ThreadsResponseBody(threads));
}
public override void Evaluate(Response response, dynamic arguments)
{
LogCommandReceived();
// expression, frameId, context
int frameId = GetFromContainer(arguments, "frameId", 0);
var frame = _threadState.GetFrameById(frameId);
if (frame == null)
{
SendErrorResponse(response, 10001, "No active stackframe");
return;
}
var expression = (string) arguments.expression;
var context = (string) arguments.context;
Log.Debug("Evaluate {Expression} in {Context}", expression, context);
int id = 0;
OneScript.DebugProtocol.Variable evalResult;
try
{
evalResult = _debuggee.Evaluate(frame, expression);
if (evalResult.IsStructured)
{
var provider = new EvaluatedExpressionProvider(expression, frame.ThreadId, frame.Index);
id = _threadState.RegisterVariablesProvider(provider);
}
}
catch (Exception e)
{
evalResult = new OneScript.DebugProtocol.Variable() { Presentation = e.Message, Name = "$evalFault" };
}
if (evalResult.Name.Equals("$evalFault") && context.Equals("hover"))
{
evalResult.Presentation = $"err: {expression}";
}
var protResult = new EvaluateResponseBody(evalResult.Presentation, id) {type = evalResult.TypeName};
SendResponse(response, protResult);
}
private void SendOutput(string category, string data)
{
if (!String.IsNullOrEmpty(data))
{
if (data[data.Length - 1] != '\n')
{
data += '\n';
}
SendEvent(new OutputEvent(category, data));
}
}
private static T GetFromContainer<T>(dynamic container, string propertyName, T defaultValue = default)
{
try
{
return (T)container[propertyName];
}
catch (Exception)
{
// ignore and return default value
}
return defaultValue;
}
protected override void OnRequestError(Exception e)
{
Log.Error(e, "Unhandled request processing error");
}
private void LogCommandReceived(dynamic args = null, [CallerMemberName] string commandName = "")
{
if (args == null)
Log.Debug("Command received {Command}", commandName);
else
Log.Debug("Command received {Command}: {@Args}", commandName, args);
}
}
}
You can’t perform that action at this time.
