// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics.CodeAnalysis; using System.Globalization; using System.Management.Automation; using System.Management.Automation.Runspaces; namespace Microsoft.PowerShell.Commands { #region PSRunspaceDebug class /// /// Runspace Debug Options class. /// public sealed class PSRunspaceDebug { #region Properties /// /// When true this property will cause any breakpoints set in a Runspace to stop /// the running command or script when the breakpoint is hit, regardless of whether a /// debugger is currently attached. The script or command will remain stopped until /// a debugger is attached to debug the breakpoint. /// public bool Enabled { get; } /// /// When true this property will cause any running command or script in the Runspace /// to stop in step mode, regardless of whether a debugger is currently attached. The /// script or command will remain stopped until a debugger is attached to debug the /// current stop point. /// public bool BreakAll { get; } /// /// Name of runspace for which the options apply. /// public string RunspaceName { get; } /// /// Local Id of runspace for which the options apply. /// public int RunspaceId { get; } #endregion #region Constructors /// /// Initializes a new instance of the class. /// /// Enable debugger option. /// BreakAll option. /// Runspace name. /// Runspace local Id. public PSRunspaceDebug(bool enabled, bool breakAll, string runspaceName, int runspaceId) { if (string.IsNullOrEmpty(runspaceName)) { throw new PSArgumentNullException(nameof(runspaceName)); } this.Enabled = enabled; this.BreakAll = breakAll; this.RunspaceName = runspaceName; this.RunspaceId = runspaceId; } #endregion } #endregion #region CommonRunspaceCommandBase class /// /// Abstract class that defines common Runspace Command parameters. /// public abstract class CommonRunspaceCommandBase : PSCmdlet { #region Strings /// /// RunspaceParameterSet. /// protected const string RunspaceParameterSet = "RunspaceParameterSet"; /// /// RunspaceNameParameterSet. /// protected const string RunspaceNameParameterSet = "RunspaceNameParameterSet"; /// /// RunspaceIdParameterSet. /// protected const string RunspaceIdParameterSet = "RunspaceIdParameterSet"; /// /// RunspaceInstanceIdParameterSet. /// protected const string RunspaceInstanceIdParameterSet = "RunspaceInstanceIdParameterSet"; /// /// ProcessNameParameterSet. /// protected const string ProcessNameParameterSet = "ProcessNameParameterSet"; #endregion #region Parameters /// /// Runspace Name. /// [Parameter(Position = 0, ParameterSetName = CommonRunspaceCommandBase.RunspaceNameParameterSet)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public string[] RunspaceName { get; set; } /// /// Runspace. /// [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true, ValueFromPipeline = true, ParameterSetName = CommonRunspaceCommandBase.RunspaceParameterSet)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public Runspace[] Runspace { get; set; } /// /// Runspace Id. /// [Parameter(Position = 0, Mandatory = true, ParameterSetName = CommonRunspaceCommandBase.RunspaceIdParameterSet)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public int[] RunspaceId { get; set; } /// /// RunspaceInstanceId. /// [Parameter(Position = 0, Mandatory = true, ParameterSetName = CommonRunspaceCommandBase.RunspaceInstanceIdParameterSet)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")] public System.Guid[] RunspaceInstanceId { get; set; } /// /// Gets or Sets the ProcessName for which runspace debugging has to be enabled or disabled. /// [Parameter(Position = 0, ParameterSetName = CommonRunspaceCommandBase.ProcessNameParameterSet)] [ValidateNotNullOrEmpty] public string ProcessName { get; set; } /// /// Gets or Sets the AppDomain Names for which runspace debugging has to be enabled or disabled. /// [Parameter(Position = 1, ParameterSetName = CommonRunspaceCommandBase.ProcessNameParameterSet)] [ValidateNotNullOrEmpty] [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", Target = "Microsoft.PowerShell.Commands.CommonRunspaceCommandBase.#AppDomainName")] public string[] AppDomainName { get; set; } #endregion #region Protected Methods /// /// Returns a list of valid runspaces based on current parameter set. /// /// IReadOnlyList. protected IReadOnlyList GetRunspaces() { IReadOnlyList results = null; if ((ParameterSetName == CommonRunspaceCommandBase.RunspaceNameParameterSet) && ((RunspaceName == null) || RunspaceName.Length == 0)) { results = GetRunspaceUtils.GetAllRunspaces(); } else { switch (ParameterSetName) { case CommonRunspaceCommandBase.RunspaceNameParameterSet: results = GetRunspaceUtils.GetRunspacesByName(RunspaceName); break; case CommonRunspaceCommandBase.RunspaceIdParameterSet: results = GetRunspaceUtils.GetRunspacesById(RunspaceId); break; case CommonRunspaceCommandBase.RunspaceParameterSet: results = new ReadOnlyCollection(new List(Runspace)); break; case CommonRunspaceCommandBase.RunspaceInstanceIdParameterSet: results = GetRunspaceUtils.GetRunspacesByInstanceId(RunspaceInstanceId); break; } } return results; } /// /// Returns Runspace Debugger. /// /// Runspace. /// Debugger. protected System.Management.Automation.Debugger GetDebuggerFromRunspace(Runspace runspace) { System.Management.Automation.Debugger debugger = null; try { debugger = runspace.Debugger; } catch (PSInvalidOperationException) { } if (debugger == null) { WriteError( new ErrorRecord( new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, Debugger.RunspaceOptionNoDebugger, runspace.Name)), "RunspaceDebugOptionNoDebugger", ErrorCategory.InvalidOperation, this) ); } return debugger; } /// /// SetDebugPreferenceHelper is a helper method used to enable/disable debug preference. /// /// Process Name. /// App Domain Name. /// Indicates if debug preference has to be enabled or disabled. /// FullyQualifiedErrorId to be used on error. protected void SetDebugPreferenceHelper(string processName, string[] appDomainName, bool enable, string fullyQualifiedErrorId) { List appDomainNames = null; if (appDomainName != null) { foreach (string currentAppDomainName in appDomainName) { if (!string.IsNullOrEmpty(currentAppDomainName)) { appDomainNames ??= new List(); appDomainNames.Add(currentAppDomainName.ToLowerInvariant()); } } } try { System.Management.Automation.Runspaces.LocalRunspace.SetDebugPreference(processName.ToLowerInvariant(), appDomainNames, enable); } catch (Exception ex) { ErrorRecord errorRecord = new( new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, Debugger.PersistDebugPreferenceFailure, processName), ex), fullyQualifiedErrorId, ErrorCategory.InvalidOperation, this); WriteError(errorRecord); } } #endregion } #endregion #region EnableRunspaceDebugCommand Cmdlet /// /// This cmdlet enables debugging for selected runspaces in the current or specified process. /// [Cmdlet(VerbsLifecycle.Enable, "RunspaceDebug", DefaultParameterSetName = CommonRunspaceCommandBase.RunspaceNameParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2096831")] public sealed class EnableRunspaceDebugCommand : CommonRunspaceCommandBase { #region Parameters /// /// When true this property will cause any running command or script in the Runspace /// to stop in step mode, regardless of whether a debugger is currently attached. The /// script or command will remain stopped until a debugger is attached to debug the /// current stop point. /// [Parameter(Position = 1, ParameterSetName = CommonRunspaceCommandBase.RunspaceParameterSet)] [Parameter(Position = 1, ParameterSetName = CommonRunspaceCommandBase.RunspaceNameParameterSet)] [Parameter(Position = 1, ParameterSetName = CommonRunspaceCommandBase.RunspaceIdParameterSet)] public SwitchParameter BreakAll { get; set; } #endregion #region Overrides /// /// Process Record. /// protected override void ProcessRecord() { if (this.ParameterSetName.Equals(CommonRunspaceCommandBase.ProcessNameParameterSet)) { SetDebugPreferenceHelper(ProcessName, AppDomainName, true, "EnableRunspaceDebugCommandPersistDebugPreferenceFailure"); return; } IReadOnlyList results = GetRunspaces(); foreach (var runspace in results) { if (runspace.RunspaceStateInfo.State != RunspaceState.Opened) { WriteError( new ErrorRecord(new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, Debugger.RunspaceOptionInvalidRunspaceState, runspace.Name)), "SetRunspaceDebugOptionCommandInvalidRunspaceState", ErrorCategory.InvalidOperation, this)); continue; } System.Management.Automation.Debugger debugger = GetDebuggerFromRunspace(runspace); if (debugger == null) { continue; } // Enable debugging by preserving debug stop events. debugger.UnhandledBreakpointMode = UnhandledBreakpointProcessingMode.Wait; if (this.MyInvocation.BoundParameters.ContainsKey(nameof(BreakAll))) { if (BreakAll) { try { debugger.SetDebuggerStepMode(true); } catch (PSInvalidOperationException e) { WriteError( new ErrorRecord( e, "SetRunspaceDebugOptionCommandCannotEnableDebuggerStepping", ErrorCategory.InvalidOperation, this)); } } else { debugger.SetDebuggerStepMode(false); } } } } #endregion } #endregion #region DisableRunspaceDebugCommand Cmdlet /// /// This cmdlet disables Runspace debugging in selected Runspaces. /// [Cmdlet(VerbsLifecycle.Disable, "RunspaceDebug", DefaultParameterSetName = CommonRunspaceCommandBase.RunspaceNameParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2096924")] public sealed class DisableRunspaceDebugCommand : CommonRunspaceCommandBase { #region Overrides /// /// Process Record. /// protected override void ProcessRecord() { if (this.ParameterSetName.Equals(CommonRunspaceCommandBase.ProcessNameParameterSet)) { SetDebugPreferenceHelper(ProcessName.ToLowerInvariant(), AppDomainName, false, "DisableRunspaceDebugCommandPersistDebugPreferenceFailure"); } else { IReadOnlyList results = GetRunspaces(); foreach (var runspace in results) { if (runspace.RunspaceStateInfo.State != RunspaceState.Opened) { WriteError( new ErrorRecord( new PSInvalidOperationException(string.Format(CultureInfo.InvariantCulture, Debugger.RunspaceOptionInvalidRunspaceState, runspace.Name)), "SetRunspaceDebugOptionCommandInvalidRunspaceState", ErrorCategory.InvalidOperation, this) ); continue; } System.Management.Automation.Debugger debugger = GetDebuggerFromRunspace(runspace); if (debugger == null) { continue; } debugger.SetDebuggerStepMode(false); debugger.UnhandledBreakpointMode = UnhandledBreakpointProcessingMode.Ignore; } } } #endregion } #endregion #region GetRunspaceDebugCommand Cmdlet /// /// This cmdlet returns a PSRunspaceDebug object for each found Runspace object. /// [Cmdlet(VerbsCommon.Get, "RunspaceDebug", DefaultParameterSetName = CommonRunspaceCommandBase.RunspaceNameParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097015")] [OutputType(typeof(PSRunspaceDebug))] public sealed class GetRunspaceDebugCommand : CommonRunspaceCommandBase { #region Overrides /// /// Process Record. /// protected override void ProcessRecord() { IReadOnlyList results = GetRunspaces(); foreach (var runspace in results) { System.Management.Automation.Debugger debugger = GetDebuggerFromRunspace(runspace); if (debugger != null) { WriteObject( new PSRunspaceDebug((debugger.UnhandledBreakpointMode == UnhandledBreakpointProcessingMode.Wait), debugger.IsDebuggerSteppingEnabled, runspace.Name, runspace.Id) ); } } } #endregion } #endregion #region WaitDebuggerCommand Cmdlet /// /// This cmdlet causes a running script or command to stop in the debugger at the next execution point. /// [Cmdlet(VerbsLifecycle.Wait, "Debugger", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2097035")] public sealed class WaitDebuggerCommand : PSCmdlet { #region Overrides /// /// EndProcessing. /// protected override void EndProcessing() { Runspace currentRunspace = this.Context.CurrentRunspace; if (currentRunspace != null && currentRunspace.Debugger != null) { WriteVerbose(string.Format(CultureInfo.InvariantCulture, Debugger.DebugBreakMessage, MyInvocation.ScriptLineNumber, MyInvocation.ScriptName)); currentRunspace.Debugger.Break(); } } #endregion } #endregion }