Example #1
0
 internal override void RemoveSelf(Debugger debugger)
 {
     Func<LineBreakpoint, bool> predicate = null;
     if (this.ScriptBlock != null)
     {
         List<LineBreakpoint> boundBreakpoints = debugger.GetBoundBreakpoints(this.ScriptBlock);
         if (boundBreakpoints != null)
         {
             boundBreakpoints.Remove(this);
             if (predicate == null)
             {
                 predicate = breakpoint => breakpoint.SequencePointIndex != this.SequencePointIndex;
             }
             if (boundBreakpoints.All<LineBreakpoint>(predicate))
             {
                 this.BreakpointBitArray.Set(this.SequencePointIndex, false);
             }
         }
     }
     debugger.RemoveLineBreakpoint(this);
 }
Example #2
0
        /// <summary>
        /// End processing.  Do work.
        /// </summary>
        protected override void EndProcessing()
        {
            switch (ParameterSetName)
            {
                case DebugJobCommand.JobParameterSet:
                    _job = Job;
                    break;

                case DebugJobCommand.JobNameParameterSet:
                    _job = GetJobByName(Name);
                    break;

                case DebugJobCommand.JobIdParameterSet:
                    _job = GetJobById(Id);
                    break;

                case DebugJobCommand.JobInstanceIdParameterSet:
                    _job = GetJobByInstanceId(InstanceId);
                    break;
            }

            if (!ShouldProcess(_job.Name, VerbsDiagnostic.Debug))
            {
                return;
            }

            Runspace runspace = LocalRunspace.DefaultRunspace;
            if (runspace == null || runspace.Debugger == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(RemotingErrorIdStrings.CannotDebugJobNoHostDebugger),
                        "DebugJobNoHostDebugger",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if ((runspace.Debugger.DebugMode == DebugModes.Default) || (runspace.Debugger.DebugMode == DebugModes.None))
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(RemotingErrorIdStrings.CannotDebugJobInvalidDebuggerMode),
                        "DebugJobWrongDebugMode",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (this.Host == null || this.Host.UI == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(RemotingErrorIdStrings.CannotDebugJobNoHostUI),
                        "DebugJobNoHostAvailable",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (!CheckForDebuggableJob())
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(DebuggerStrings.NoDebuggableJobsFound),
                        "DebugJobNoDebuggableJobsFound",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            // Set up host script debugger to debug the job.
            _debugger = runspace.Debugger;
            _debugger.DebugJob(_job);

            // Blocking call.  Send job output to host UI while debugging and wait for Job completion.
            WaitAndReceiveJobOutput();
        }
Example #3
0
 internal override void RemoveSelf(Debugger debugger)
 {
     debugger.RemoveVariableBreakpoint(this);
 }
Example #4
0
        /// <summary>
        /// End processing.  Do work.
        /// </summary>
        protected override void EndProcessing()
        {
            if (ParameterSetName == DebugRunspaceCommand.RunspaceParameterSet)
            {
                _runspace = Runspace;
            }
            else
            {
                IReadOnlyList <Runspace> runspaces = null;

                switch (ParameterSetName)
                {
                case DebugRunspaceCommand.NameParameterSet:
                    runspaces = GetRunspaceUtils.GetRunspacesByName(new string[] { Name });
                    break;

                case DebugRunspaceCommand.IdParameterSet:
                    runspaces = GetRunspaceUtils.GetRunspacesById(new int[] { Id });
                    break;

                case DebugRunspaceCommand.InstanceIdParameterSet:
                    runspaces = GetRunspaceUtils.GetRunspacesByInstanceId(new Guid[] { InstanceId });
                    break;
                }

                if (runspaces.Count > 1)
                {
                    ThrowTerminatingError(
                        new ErrorRecord(
                            new PSArgumentException(Debugger.RunspaceDebuggingTooManyRunspacesFound),
                            "DebugRunspaceTooManyRunspaceFound",
                            ErrorCategory.InvalidOperation,
                            this)
                        );
                }

                if (runspaces.Count == 1)
                {
                    _runspace = runspaces[0];
                }
            }

            if (_runspace == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSArgumentNullException(Debugger.RunspaceDebuggingNoRunspaceFound),
                        "DebugRunspaceNoRunspaceFound",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            Runspace defaultRunspace = LocalRunspace.DefaultRunspace;

            if (defaultRunspace == null || defaultRunspace.Debugger == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(Debugger.RunspaceDebuggingNoHostRunspaceOrDebugger),
                        "DebugRunspaceNoHostDebugger",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (_runspace == defaultRunspace)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(Debugger.RunspaceDebuggingCannotDebugDefaultRunspace),
                        "DebugRunspaceCannotDebugHostRunspace",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (this.Host == null || this.Host.UI == null)
            {
                ThrowTerminatingError(
                    new ErrorRecord(
                        new PSInvalidOperationException(Debugger.RunspaceDebuggingNoHost),
                        "DebugRunspaceNoHostAvailable",
                        ErrorCategory.InvalidOperation,
                        this)
                    );
            }

            if (!ShouldProcess(_runspace.Name, VerbsDiagnostic.Debug))
            {
                return;
            }

            _debugger = defaultRunspace.Debugger;

            try
            {
                PrepareRunspace(_runspace);

                // Blocking call.  Send runspace/command output to host UI while debugging and wait for runspace/command completion.
                WaitAndReceiveRunspaceOutput();
            }
            finally
            {
                RestoreRunspace(_runspace);
            }
        }
        /// <summary>
        /// Process Record.
        /// </summary>
        protected override void ProcessRecord()
        {
            if (this.ParameterSetName.Equals(CommonRunspaceCommandBase.ProcessNameParameterSet))
            {
                SetDebugPreferenceHelper(ProcessName, AppDomainName, true, "EnableRunspaceDebugCommandPersistDebugPreferenceFailure");
                return;
            }

            IReadOnlyList <Runspace> 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);
                    }
                }

                // If any breakpoints were provided, set those in the debugger.
                if (Breakpoint?.Length > 0)
                {
                    debugger.SetBreakpoints(Breakpoint);
                }
            }
        }
Example #6
0
 internal override void RemoveSelf(Debugger debugger)
 {
     debugger.RemoveCommandBreakpoint(this);
 }
Example #7
0
 internal virtual void RemoveSelf(Debugger debugger)
 {
 }