Code cleanup.

This commit is contained in:
José Pedro 2018-08-29 21:48:21 +01:00
parent 3bec068b7f
commit ec96889b15
No known key found for this signature in database
GPG key ID: B8247B9301707B83
5 changed files with 105 additions and 54 deletions

View file

@ -1,4 +1,6 @@
using System;
using System.Diagnostics.CodeAnalysis;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Debugger.Interop;
@ -16,9 +18,9 @@ namespace Cosmos.VS.DebugEngine.AD7.Impl
public AD7Expression(DebugLocalInfo pVar, AD7Process pProcess, AD7StackFrame pStackFrame)
{
this.m_var = pVar;
this.Process = pProcess;
this.StackFrame = pStackFrame;
m_var = pVar;
Process = pProcess;
StackFrame = pStackFrame;
}
// This method cancels asynchronous expression evaluation as started by a call to the IDebugExpression2::EvaluateAsync method.
@ -33,6 +35,7 @@ namespace Cosmos.VS.DebugEngine.AD7.Impl
// must be sent to the IDebugEventCallback2 event callback
//
// This is primarily used for the immediate window which this engine does not currently support.
[SuppressMessage("AsyncUsage.CSharp.Naming", "AvoidAsyncSuffix:Avoid Async suffix", Scope = "member")]
int IDebugExpression2.EvaluateAsync(enum_EVALFLAGS dwFlags, IDebugEventCallback2 pExprCallback)
{
throw new NotImplementedException();
@ -46,4 +49,4 @@ namespace Cosmos.VS.DebugEngine.AD7.Impl
}
}
}
}

View file

@ -2,6 +2,7 @@
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace Cosmos.VS.DebugEngine.Commands
@ -25,25 +26,31 @@ namespace Cosmos.VS.DebugEngine.Commands
string arguments;
hr = EnsureString(pvaIn, out arguments);
if (hr != VSConstants.S_OK)
return hr;
if (string.IsNullOrWhiteSpace(arguments))
if (hr != VSConstants.S_OK)
{
return hr;
}
if (String.IsNullOrWhiteSpace(arguments))
{
throw new ArgumentException("Expected a Cosmos command to execute (ex: Debug.CosmosDebugExec info sharedlibrary)");
}
DebugExec(arguments);
return VSConstants.S_OK;
}
private void DebugExec(string command)
private void DebugExec(string command)
{
ThreadHelper.ThrowIfNotOnUIThread();
var commandWindow = (IVsCommandWindow)serviceProvider.GetService(typeof(SVsCommandWindow));
bool atBreak = false;
var debugger = serviceProvider.GetService(typeof(SVsShellDebugger)) as IVsDebugger;
if (debugger != null)
var atBreak = false;
if (serviceProvider.GetService(typeof(SVsShellDebugger)) is IVsDebugger debugger)
{
DBGMODE[] mode = new DBGMODE[1];
var mode = new DBGMODE[1];
if (debugger.GetMode(mode) == VSConstants.S_OK)
{
atBreak = mode[0] == DBGMODE.DBGMODE_Break;
@ -85,4 +92,4 @@ namespace Cosmos.VS.DebugEngine.Commands
}
}
}
}
}

View file

@ -3,6 +3,7 @@ using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace Cosmos.VS.DebugEngine.Commands
@ -19,11 +20,13 @@ namespace Cosmos.VS.DebugEngine.Commands
public DebugLaunchCommand(IServiceProvider serviceProvider)
: base(serviceProvider)
{
{
}
public override int Execute(uint nCmdExecOpt, IntPtr pvaIn, IntPtr pvaOut)
{
ThreadHelper.ThrowIfNotOnUIThread();
int hr;
if (IsQueryParameterList(pvaIn, pvaOut, nCmdExecOpt))
@ -32,53 +35,66 @@ namespace Cosmos.VS.DebugEngine.Commands
return VSConstants.S_OK;
}
string arguments;
hr = EnsureString(pvaIn, out arguments);
if (hr != VSConstants.S_OK)
return hr;
hr = EnsureString(pvaIn, out var arguments);
IVsParseCommandLine parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine));
hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1);
if (ErrorHandler.Failed(hr))
if (hr != VSConstants.S_OK)
{
return hr;
}
var parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine));
hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1);
if (ErrorHandler.Failed(hr))
{
return hr;
}
hr = parseCommandLine.HasParams();
if (ErrorHandler.Failed(hr))
{
return hr;
}
if (hr == VSConstants.S_OK || parseCommandLine.HasSwitches() != VSConstants.S_OK)
{
string message = string.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n",
var message = String.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n",
"Debug.CosmosDebugLaunch /Executable:<path_or_logical_name> /OptionsFile:<path>");
throw new ApplicationException(message);
}
hr = parseCommandLine.EvaluateSwitches(DebugLaunchCommandSyntax);
if (ErrorHandler.Failed(hr))
{
return hr;
}
string executable;
if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.Executable, out executable) != VSConstants.S_OK ||
string.IsNullOrWhiteSpace(executable))
if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.Executable, out var executable) != VSConstants.S_OK ||
String.IsNullOrWhiteSpace(executable))
{
throw new ArgumentException("Executable must be specified");
}
bool checkExecutableExists = false;
string options = string.Empty;
var checkExecutableExists = false;
var options = String.Empty;
string optionsFilePath;
if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.OptionsFile, out optionsFilePath) == 0)
if (parseCommandLine.GetSwitchValue((int)DebugLaunchCommandSwitchEnum.OptionsFile, out var optionsFilePath) == 0)
{
// When using the options file, we want to allow the executable to be just a logical name, but if
// one enters a real path, we should make sure it isn't mistyped. If the path contains a slash, we assume it
// is meant to be a real path so enforce that it exists
checkExecutableExists = (executable.IndexOf('\\') >= 0);
if (string.IsNullOrWhiteSpace(optionsFilePath))
if (String.IsNullOrWhiteSpace(optionsFilePath))
{
throw new ArgumentException("Value expected for '/OptionsFile' option");
}
if (!File.Exists(optionsFilePath))
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Options file '{0}' does not exist", optionsFilePath));
{
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Options file '{0}' does not exist", optionsFilePath));
}
options = File.ReadAllText(optionsFilePath);
}
@ -87,7 +103,7 @@ namespace Cosmos.VS.DebugEngine.Commands
{
if (!File.Exists(executable))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Executable '{0}' does not exist", executable));
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Executable '{0}' does not exist", executable));
}
executable = Path.GetFullPath(executable);
@ -100,13 +116,15 @@ namespace Cosmos.VS.DebugEngine.Commands
private void LaunchDebugTarget(string filePath, string options)
{
IVsDebugger4 debugger = (IVsDebugger4)serviceProvider.GetService(typeof(IVsDebugger));
VsDebugTargetInfo4[] debugTargets = new VsDebugTargetInfo4[1];
ThreadHelper.ThrowIfNotOnUIThread();
var debugger = (IVsDebugger4)serviceProvider.GetService(typeof(IVsDebugger));
var debugTargets = new VsDebugTargetInfo4[1];
debugTargets[0].dlo = (uint)DEBUG_LAUNCH_OPERATION.DLO_CreateProcess;
debugTargets[0].bstrExe = filePath;
debugTargets[0].bstrOptions = options;
debugTargets[0].guidLaunchDebugEngine = Guids.DebugEngineGuid;
VsDebugTargetProcessInfo[] processInfo = new VsDebugTargetProcessInfo[debugTargets.Length];
var processInfo = new VsDebugTargetProcessInfo[debugTargets.Length];
debugger.LaunchDebugTargets4(1, debugTargets, processInfo);
}

View file

@ -2,6 +2,7 @@
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
namespace Cosmos.VS.DebugEngine.Commands
@ -24,6 +25,8 @@ namespace Cosmos.VS.DebugEngine.Commands
public override int Execute(uint nCmdLogOpt, IntPtr pvaIn, IntPtr pvaOut)
{
ThreadHelper.ThrowIfNotOnUIThread();
int hr;
if (IsQueryParameterList(pvaIn, pvaOut, nCmdLogOpt))
@ -32,47 +35,59 @@ namespace Cosmos.VS.DebugEngine.Commands
return VSConstants.S_OK;
}
string arguments;
hr = EnsureString(pvaIn, out arguments);
if (hr != VSConstants.S_OK)
return hr;
hr = EnsureString(pvaIn, out var arguments);
IVsParseCommandLine parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine));
hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1);
if (ErrorHandler.Failed(hr))
if (hr != VSConstants.S_OK)
{
return hr;
}
var parseCommandLine = (IVsParseCommandLine)serviceProvider.GetService(typeof(SVsParseCommandLine));
hr = parseCommandLine.ParseCommandTail(arguments, iMaxParams: -1);
if (ErrorHandler.Failed(hr))
{
return hr;
}
hr = parseCommandLine.HasParams();
if (ErrorHandler.Failed(hr))
{
return hr;
}
if (hr == VSConstants.S_OK || parseCommandLine.HasSwitches() != VSConstants.S_OK)
{
string message = string.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n",
string message = String.Concat("Unexpected syntax for CosmosDebugLaunch command. Expected:\n",
"Debug.CosmosDebugLog [/On:<optional_path> [/OutputWindow] | /Off]");
throw new ApplicationException(message);
}
hr = parseCommandLine.EvaluateSwitches(DebugLogCommandSyntax);
if (ErrorHandler.Failed(hr))
return hr;
string logPath = string.Empty;
bool logToOutput = false;
if (ErrorHandler.Failed(hr))
{
return hr;
}
var logPath = String.Empty;
var logToOutput = false;
if (parseCommandLine.GetSwitchValue((int)DebugLogCommandSwitchEnum.On, out logPath) == VSConstants.S_OK)
{
logToOutput = parseCommandLine.IsSwitchPresent((int)DebugLogCommandSwitchEnum.OutputWindow) == VSConstants.S_OK;
if (parseCommandLine.IsSwitchPresent((int)DebugLogCommandSwitchEnum.Off) == VSConstants.S_OK)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "/On and /Off cannot both appear on command line"));
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "/On and /Off cannot both appear on command line"));
}
if (!logToOutput && string.IsNullOrEmpty(logPath))
if (!logToOutput && String.IsNullOrEmpty(logPath))
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Must specify a log file (/On:<path>) or /OutputWindow"));
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "Must specify a log file (/On:<path>) or /OutputWindow"));
}
}
else if (parseCommandLine.IsSwitchPresent((int)DebugLogCommandSwitchEnum.Off) != VSConstants.S_OK)
{
throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "One of /On or /Off must be present on command line"));
throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, "One of /On or /Off must be present on command line"));
}
EnableLogging(logToOutput, logPath);
@ -82,6 +97,8 @@ namespace Cosmos.VS.DebugEngine.Commands
private void EnableLogging(bool sendToOutputWindow, string logFile)
{
ThreadHelper.ThrowIfNotOnUIThread();
try
{
//MIDebugCommandDispatcher.EnableLogging(sendToOutputWindow, logFile);
@ -89,8 +106,8 @@ namespace Cosmos.VS.DebugEngine.Commands
catch (Exception e)
{
var commandWindow = (IVsCommandWindow)serviceProvider.GetService(typeof(SVsCommandWindow));
commandWindow.Print(string.Format(CultureInfo.CurrentCulture, "Error: {0}\r\n", e.Message));
commandWindow.Print(String.Format(CultureInfo.CurrentCulture, "Error: {0}\r\n", e.Message));
}
}
}
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
namespace Cosmos.VS.DebugEngine.Engine.Impl
@ -24,7 +25,7 @@ namespace Cosmos.VS.DebugEngine.Engine.Impl
m_opComplete = new ManualResetEvent(true);
m_quitOperation = new Operation(delegate() { });
Thread thread = new Thread(new ThreadStart(ThreadFunc));
var thread = new Thread(new ThreadStart(ThreadFunc));
thread.Start();
}
@ -37,15 +38,20 @@ namespace Cosmos.VS.DebugEngine.Engine.Impl
public void RunOperation(Operation op)
{
if (op == null)
{
throw new ArgumentNullException();
}
SetOperationInternal(op, true);
}
[SuppressMessage("AsyncUsage.CSharp.Naming", "AvoidAsyncSuffix:Avoid Async suffix", Scope = "member")]
public void RunOperationAsync(Operation op)
{
if (op == null)
{
throw new ArgumentNullException();
}
SetOperationInternal(op, false);
}