Skip to content

Commit

Permalink
fixup for vs2017
Browse files Browse the repository at this point in the history
  • Loading branch information
nokotan committed Jul 19, 2024
1 parent db6047a commit 0e7b5c5
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 44 deletions.
91 changes: 68 additions & 23 deletions Emscripten.Debugger.VSCodeDwarfDebug/AdapterLauncher.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,78 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Task = System.Threading.Tasks.Task;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Runtime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Emscripten.DebuggerLauncher;
using Microsoft.VisualStudio.Debugger.DebugAdapterHost.Interfaces;
using Microsoft.VisualStudio.ProjectSystem.Debug;
using Microsoft.VisualStudio.ProjectSystem.VS.Debug;
using StreamJsonRpc;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Threading;
using Newtonsoft.Json;
using static System.Windows.Forms.Design.AxImporter;

namespace Kamenokosoft.Emscripten.Debugger.VSCodeDwarfDebug
{
internal sealed class DebugEngineLauncher
public class AdapterLauncher : IAdapterLauncher, IDebugAdapterHostComponent
{
static public async Task LaunchAsync(DebugLaunchSettings setting)
private IDebugAdapterHostContext context;

private Process debugServerProcess;

public void Initialize(IDebugAdapterHostContext context)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
// Save the context object provided by the Debug Adapter Host so we can use it to access services later
this.context = context;

context.Events.DebuggingEnded += OnDebugSessionEnded;
}

public void UpdateLaunchOptions(IAdapterLaunchInfo launchInfo)
{
if (launchInfo.LaunchJson.Contains("__pendingTarget"))
{
// child target. ignore
}
else
{
LaunchDebugServer(launchInfo);
}
}

var obj = Package.GetGlobalService(typeof(SVsShellDebugger)) as IVsDebugger4;
var vsDebugTargetInfo = new VsDebugTargetInfo4
private void OnDebugSessionEnded(object sender, EventArgs e)
{
if (debugServerProcess != null && !debugServerProcess.HasExited)
{
LaunchFlags = (uint)setting.LaunchOptions,
dlo = (uint)setting.LaunchOperation,
guidLaunchDebugEngine = setting.LaunchDebugEngineGuid,
bstrExe = setting.Executable,
bstrOptions = setting.Options
};
var _unused = new VsDebugTargetProcessInfo[1];

obj.LaunchDebugTargets4(1, new VsDebugTargetInfo4[1] { vsDebugTargetInfo }, _unused);
debugServerProcess.Kill();
}
}

private void LaunchDebugServer(IAdapterLaunchInfo launchInfo)
{
var AdapterExecutable = launchInfo.GetMetricString("Adapter");
var AdapterArguments = launchInfo.GetMetricString("AdapterArgs");

var AdapterServerProcess = new Process();

AdapterServerProcess.StartInfo.FileName = AdapterExecutable;
AdapterServerProcess.StartInfo.Arguments = AdapterArguments;
AdapterServerProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// AdapterServerProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

AdapterServerProcess.Start();

debugServerProcess = AdapterServerProcess;
}

public ITargetHostProcess LaunchAdapter(IAdapterLaunchInfo launchInfo, ITargetHostInterop targetInterop)
{
// nop
return null;
}
}
}
}
19 changes: 12 additions & 7 deletions Emscripten.Debugger.VSCodeDwarfDebug/CustomMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

namespace Kamenokosoft.Emscripten.Debugger.VSCodeDwarfDebug
{


public class StartDebuggingRequestHandler : ICustomProtocolExtension
{
private IDebugAdapterHostContext context;
Expand Down Expand Up @@ -46,8 +44,9 @@ private void OnStartDebuggingRequest(IRequestResponder<StartDebuggingArgs, Start
settings.LaunchOperation = DebugLaunchOperation.CreateProcess;
settings.LaunchDebugEngineGuid = new Guid("9849C080-ECCF-46EE-9758-9F6F9ED68693");
settings.Executable = "WebAssembly DWARF Debug";
responder.Arguments.Config.ConfigurationProperties["$debugServer"] = 8123;
settings.Options = JsonConvert.SerializeObject(responder.Arguments.Config);
var debugConfig = responder.Arguments.Configuration;
debugConfig.DebugServerPort = 8123;
settings.Options = JsonConvert.SerializeObject(debugConfig);

ThreadHelper.JoinableTaskFactory.RunAsync(async () =>
{
Expand All @@ -59,8 +58,14 @@ private void OnStartDebuggingRequest(IRequestResponder<StartDebuggingArgs, Start

internal class ConfigurationProperty
{
[JsonExtensionData(ReadData = true, WriteData = true)]
public JObject ConfigurationProperties { get; set; }
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("__pendingTargetId")]
public string __PendingTargetId { get; set; }
[JsonProperty("$debugServer")]
public int DebugServerPort { get; set; }
}

internal class StartDebuggingArgs
Expand All @@ -69,7 +74,7 @@ internal class StartDebuggingArgs
public string Request { get; set; }

[JsonProperty("configuration")]
public ConfigurationProperty Config { get; set; }
public ConfigurationProperty Configuration { get; set; }
}

internal class StartDebuggingResponse : ResponseBody
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions Emscripten.Debugger.VSCodeDwarfDebug/DebugEngineLauncher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.ProjectSystem.VS.Debug;
using Microsoft.VisualStudio.Debugger.DebugAdapterHost.Interfaces;
using System.IO;
using System.Diagnostics;

namespace Kamenokosoft.Emscripten.Debugger.VSCodeDwarfDebug
{
internal class LaunchedTarget : ITargetHostProcess, IDisposable
{
private Process target;

public IntPtr Handle => target.Handle;

public Stream StandardInput => target.StandardInput.BaseStream;

public Stream StandardOutput => target.StandardOutput.BaseStream;

public bool HasExited => target.HasExited;

public event EventHandler Exited;
public event DataReceivedEventHandler ErrorDataReceived;

public LaunchedTarget(Process process)
{
target = process;

target.Exited += OnExited;
target.ErrorDataReceived += OnErrorDataReceived;
}

public void Terminate()
{
throw new NotImplementedException();
}

public void Dispose()
{
target.Exited -= OnExited;
target.ErrorDataReceived -= OnErrorDataReceived;

((IDisposable)target).Dispose();
}

private void OnExited(object sender, EventArgs e)
{
Exited(sender, e);
}

private void OnErrorDataReceived(object sender, DataReceivedEventArgs e)
{
ErrorDataReceived(sender, e);
}
}

internal sealed class DebugEngineLauncher
{
static public async System.Threading.Tasks.Task<ITargetHostProcess> LaunchAsync(DebugLaunchSettings setting)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

var obj = Package.GetGlobalService(typeof(SVsShellDebugger)) as IVsDebugger4;
var vsDebugTargetInfo = new VsDebugTargetInfo4
{
LaunchFlags = (uint)setting.LaunchOptions,
dlo = (uint)setting.LaunchOperation,
guidLaunchDebugEngine = setting.LaunchDebugEngineGuid,
bstrExe = setting.Executable,
bstrOptions = setting.Options
};
var launchedProcess = new VsDebugTargetProcessInfo[1];

obj.LaunchDebugTargets4(1, new VsDebugTargetInfo4[1] { vsDebugTargetInfo }, launchedProcess);

return new LaunchedTarget(Process.GetProcessById((int)launchedProcess[0].dwProcessId));
}

static public ITargetHostProcess Launch(DebugLaunchSettings setting)
{
var obj = Package.GetGlobalService(typeof(SVsShellDebugger)) as IVsDebugger4;
var vsDebugTargetInfo = new VsDebugTargetInfo4
{
LaunchFlags = (uint)setting.LaunchOptions,
dlo = (uint)setting.LaunchOperation,
guidLaunchDebugEngine = setting.LaunchDebugEngineGuid,
bstrExe = setting.Executable,
bstrOptions = setting.Options
};
var launchedProcess = new VsDebugTargetProcessInfo[1];

obj.LaunchDebugTargets4(1, new VsDebugTargetInfo4[1] { vsDebugTargetInfo }, launchedProcess);

return new LaunchedTarget(Process.GetProcessById((int)launchedProcess[0].dwProcessId));
}
}
}
Loading

0 comments on commit 0e7b5c5

Please sign in to comment.