From cec21758c062d5050466f2ca6e9b9b1374b30ca7 Mon Sep 17 00:00:00 2001 From: Matt Parker <61717342+MattParkerDev@users.noreply.github.com> Date: Tue, 19 Aug 2025 19:07:00 +1000 Subject: [PATCH] Rough debugging service v1 --- .../Features/Debugging/DebuggingService.cs | 111 ++++++++++++++++++ .../SharpIDE.Application.csproj | 1 + 2 files changed, 112 insertions(+) create mode 100644 src/SharpIDE.Application/Features/Debugging/DebuggingService.cs diff --git a/src/SharpIDE.Application/Features/Debugging/DebuggingService.cs b/src/SharpIDE.Application/Features/Debugging/DebuggingService.cs new file mode 100644 index 0000000..e61ba2e --- /dev/null +++ b/src/SharpIDE.Application/Features/Debugging/DebuggingService.cs @@ -0,0 +1,111 @@ +using System.Diagnostics; +using System.IO.Pipelines; +using OmniSharp.Extensions.DebugAdapter.Client; +using OmniSharp.Extensions.DebugAdapter.Protocol.Events; +using OmniSharp.Extensions.DebugAdapter.Protocol.Models; +using OmniSharp.Extensions.DebugAdapter.Protocol.Requests; +using OmniSharp.Extensions.DebugAdapter.Server; +using OmniSharp.Extensions.JsonRpc.Client; + +namespace SharpIDE.Application.Features.Debugging; + +public class DebuggingService +{ + + public async Task Test(CancellationToken cancellationToken = default) + { + await Task.CompletedTask.ConfigureAwait(ConfigureAwaitOptions.ForceYielding); + var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = @"C:\Users\Matthew\Downloads\netcoredbg-win64\netcoredbg\netcoredbg.exe", + Arguments = """ --interpreter=vscode -- "C:\Program Files\dotnet\dotnet.exe" "C:\Users\Matthew\Documents\Git\BlazorCodeBreaker\artifacts\bin\WebApi\debug\WebApi.dll" """, RedirectStandardInput = true, + RedirectStandardOutput = true, + UseShellExecute = false + } + }; + process.Start(); + + var stoppedTcs = new TaskCompletionSource(); + + var client = DebugAdapterClient.Create(options => + { + options.AdapterId = "coreclr"; + options.ClientId = "vscode"; + options.LinesStartAt1 = true; + options.ColumnsStartAt1 = true; + options.SupportsVariableType = true; + options.SupportsVariablePaging = true; + options.SupportsRunInTerminalRequest = true; + options.PathFormat = PathFormat.Path; + options + .WithInput(process.StandardOutput.BaseStream) + .WithOutput(process.StandardInput.BaseStream) + .OnStarted(async (adapterClient, token) => + { + Console.WriteLine("Started"); + }) + .OnCapabilities(async (capabilitiesEvent, token) => + { + Console.WriteLine("Capabilities"); + }) + .OnBreakpoint(async (breakpointEvent, token) => + { + Console.WriteLine($"Breakpoint hit: {breakpointEvent}"); + }) + .OnStopped(async (stoppedEvent, token) => + { + Console.WriteLine($"Notification received: {stoppedEvent}"); + stoppedTcs.SetResult(stoppedEvent); + }) + .OnTerminated(async (terminatedEvent, token) => + { + Console.WriteLine($"Terminated: {terminatedEvent}"); + }); + //.OnNotification(EventNames., ); + }); + + + await client.Initialize(cancellationToken); + var breakpointsResponse = await client.SetBreakpoints(new SetBreakpointsArguments + { + Source = new Source { Path = @"C:\Users\Matthew\Documents\Git\BlazorCodeBreaker\src\WebApi\Program.cs" }, + Breakpoints = new Container(new SourceBreakpoint { Line = 7 }) + }, cancellationToken); + var launchResponse = await client.Launch(new LaunchRequestArguments() + { + NoDebug = false, + ExtensionData = new Dictionary + { + //["program"] = @"""C:\Program Files\dotnet\dotnet.exe"" ""C:\Users\Matthew\Documents\Git\BlazorCodeBreaker\artifacts\bin\WebApi\debug\WebApi.dll""", + //["cwd"] = @"C:\Users\Matthew\Documents\Git\BlazorCodeBreaker\artifacts\bin\WebApi\debug", // working directory + //["stopAtEntry"] = true, + //["env"] = new Dictionary { { "ASPNETCORE_ENVIRONMENT", "Development" } } + } + }, cancellationToken); + + var configurationDoneResponse = await client.RequestConfigurationDone(new ConfigurationDoneArguments(), cancellationToken); + + var stoppedEvent = await stoppedTcs.Task; + var threads = await client.RequestThreads(new ThreadsArguments(), cancellationToken); + + var currentThread = threads.Threads!.Single(s => s.Id == stoppedEvent.ThreadId); + var stackTrace = await client.RequestStackTrace(new StackTraceArguments { ThreadId = currentThread.Id }, cancellationToken); + var frame = stackTrace.StackFrames!.First(); + var scopes = await client.RequestScopes(new ScopesArguments { FrameId = frame.Id }, cancellationToken); + var scope = scopes.Scopes.First(); + var variablesResponse = await client.RequestVariables(new VariablesArguments() {VariablesReference = scope.VariablesReference}, cancellationToken); + var variable = variablesResponse.Variables!.Skip(1).First(); + var variables2Response = await client.RequestVariables(new VariablesArguments() {VariablesReference = variable.VariablesReference}, cancellationToken); + var variable2 = variables2Response.Variables!.Single(); + var variables3Response = await client.RequestVariables(new VariablesArguments() {VariablesReference = variable2.VariablesReference}, cancellationToken); + //var continueResponse = await client.RequestContinue(new ContinueArguments(){ThreadId = 1}, cancellationToken); + //await Task.Delay(1000); + //var test = await client.RequestNext(new NextArguments(), cancellationToken: cancellationToken); + + //var result = await client.RequestStepIn(new StepInArguments(), cancellationToken: cancellationToken); + + await process.WaitForExitAsync(); + } +} diff --git a/src/SharpIDE.Application/SharpIDE.Application.csproj b/src/SharpIDE.Application/SharpIDE.Application.csproj index 9570cc3..2a1d36a 100644 --- a/src/SharpIDE.Application/SharpIDE.Application.csproj +++ b/src/SharpIDE.Application/SharpIDE.Application.csproj @@ -14,6 +14,7 @@ true +