Beispiel #1
0
 public static void ContinueDebugEvent(uint dwProcessId, uint dwThreadId, ContinueStatus dwContinueStatus)
 {
     if (!__ContinueDebugEvent(dwProcessId, dwThreadId, dwContinueStatus))
     {
         throw new Win32Exception();
     }
 }
Beispiel #2
0
        public void Continue(ContinueStatus status)
        {
            if (State != TargetState.Paused)
                throw new InvalidOperationException("Continue requires a Paused target");

            if (!Kernel32.ContinueDebugEvent(lastEvent.ProcessId, lastEvent.ThreadId, status))
                throw new InvalidOperationException("ContinueDebugEvent failed");

            State = (lastEvent.EventCode == DebugEventCode.RipEvent | lastEvent.EventCode == DebugEventCode.ExitProcess)
                ? TargetState.Exited    /* todo: handle this properly */
                : TargetState.Running;
        }
Beispiel #3
0
        private static string ProcessExceptionDebugEvent(DEBUG_EVENT evt)
        {
            if (evt.Exception.dwFirstChance != decimal.Zero && evt.Exception.ExceptionRecord.ExceptionCode != decimal.Zero)
            {
                DBG_CONTINUE_NEXT_STATUS = ContinueStatus.DBG_EXCEPTION_NOT_HANDLED;
            }
            else
            {
                DBG_CONTINUE_NEXT_STATUS = ContinueStatus.DBG_CONTINUE;
            }

            return($"[EXCEPTION DEBUG] ExceptionCode: {evt.Exception.ExceptionRecord.ExceptionCode.ToHex()}");
        }
Beispiel #4
0
        private void HandleNextAction(DebuggerAction nextAction, DEBUG_EVENT nextEvent)
        {
            switch (nextAction)
            {
            case DebuggerAction.Continue:
                _nextContinueStatus = ContinueStatus.DBG_CONTINUE;
                break;

            case DebuggerAction.ContinueWithException:
                _nextContinueStatus = ContinueStatus.DBG_EXCEPTION_NOT_HANDLED;
                break;

            case DebuggerAction.Stop:
                var process = GetProcessById((int)nextEvent.dwProcessId);
                var thread  = process.GetThreadById((int)nextEvent.dwThreadId)
                              ?? new DebuggeeThread(process, IntPtr.Zero, (int)nextEvent.dwThreadId, IntPtr.Zero);

                var eventArgs = new DebuggeeThreadEventArgs(thread);
                eventArgs.NextAction = DebuggerAction.Stop;
                OnPaused(eventArgs);

                switch (eventArgs.NextAction)
                {
                case DebuggerAction.Continue:
                    _nextContinueStatus = ContinueStatus.DBG_CONTINUE;
                    break;

                case DebuggerAction.ContinueWithException:
                    _nextContinueStatus = ContinueStatus.DBG_EXCEPTION_NOT_HANDLED;
                    break;

                case DebuggerAction.Stop:
                    _continueEvent.WaitOne();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                break;
            }
        }
Beispiel #5
0
 internal void SignalDebuggerLoop(DebuggerAction nextAction)
 {
     _nextContinueStatus = nextAction.ToContinueStatus();
     _continueEvent.Set();
 }
		public static extern bool ContinueDebugEvent
			(
			uint dwProcessId,
			uint dwThreadId,
			ContinueStatus dwContinueStatus
			);
Beispiel #7
0
        private void Loop(Process debuggee)
        {
            do
            {
                ERROR = NativeMethods.WaitForDebugEvent(out EVENT, WAITFOR_MS);
                DBG_CONTINUE_NEXT_STATUS = ContinueStatus.DBG_CONTINUE;

                switch (EVENT.dwDebugEventCode)
                {
                case DebugEventType.NONE:
                    break;

                case DebugEventType.RIP_EVENT:
                    break;

                case DebugEventType.OUTPUT_DEBUG_STRING_EVENT:
                    byte[] buffer = EVENT.DebugString.nDebugStringLength != decimal.Zero
                            ? new byte[EVENT.DebugString.nDebugStringLength - 1]
                            : new byte[DLLPATH_BUFFER_SIZE];

                    IntPtr bytesReadPtr = IntPtr.Zero;

                    NativeMethods.ReadProcessMemory(
                        debuggee.Handle,
                        EVENT.DebugString.lpDebugStringData,
                        buffer,
                        EVENT.DebugString.nDebugStringLength - 1,
                        out bytesReadPtr);

                    break;

                case DebugEventType.UNLOAD_DLL_DEBUG_EVENT:
                    UnloadDLL(EVENT.UnloadDll.lpBaseOfDll);
                    break;

                case DebugEventType.LOAD_DLL_DEBUG_EVENT:

                    NativeMethods.GetFinalPathNameByHandle(EVENT.LoadDll.hFile, sb, DLLPATH_BUFFER_SIZE, FinalPathFlags.FILE_NAME_NORMALIZED);

                    string dllName = sb.ToString();

                    LoadedDlls[EVENT.LoadDll.lpBaseOfDll] = dllName;

                    LoadDLL(EVENT.LoadDll.lpBaseOfDll);

                    break;

                case DebugEventType.EXIT_PROCESS_DEBUG_EVENT:
                    ExitProcess($"[PROCESS EXIT] Exit Code:{EVENT.ExitProcess.dwExitCode.Address()}");
                    break;

                case DebugEventType.EXIT_THREAD_DEBUG_EVENT:
                    ExitThread($"[THREAD EXIT] ExitCode: {EVENT.ExitThread.dwExitCode.Address()}");
                    break;

                case DebugEventType.CREATE_PROCESS_DEBUG_EVENT:

                    NativeMethods.GetFinalPathNameByHandle(EVENT.CreateProcessInfo.hFile, sb, DLLPATH_BUFFER_SIZE, FinalPathFlags.FILE_NAME_NORMALIZED);
                    CreateProcess($"[PROCESS CREATED] {EVENT.CreateProcessInfo.lpBaseOfImage.Address()} {sb.ToString()}");
                    break;

                case DebugEventType.CREATE_THREAD_DEBUG_EVENT:
                    CreateThread($"[THREAD CREATED] {EVENT.CreateThread.hThread.Address()}");
                    break;

                case DebugEventType.EXCEPTION_DEBUG_EVENT:
                    Exception($"[EXCEPTION] First: {EVENT.Exception.dwFirstChance} @ {EVENT.Exception.ExceptionRecord.ExceptionAddress.Address()} Code: {EVENT.Exception.ExceptionRecord.ExceptionCode.Address()}");
                    DBG_CONTINUE_NEXT_STATUS = ContinueStatus.DBG_EXCEPTION_NOT_HANDLED;
                    break;

                default:
                    break;
                }

                OK         = NativeMethods.ContinueDebugEvent(EVENT.dwProcessId, EVENT.dwThreadId, DBG_CONTINUE_NEXT_STATUS);
                ERROR_CODE = Marshal.GetLastWin32Error();
            } while (true);
        }
Beispiel #8
0
 internal void WriteProgress(long sourceId, ProgressRecord progressRecord, bool overrideInquire)
 {
     if (progressRecord == null)
     {
         throw PSTraceSource.NewArgumentNullException("progressRecord");
     }
     if ((this.Host == null) || (this.Host.UI == null))
     {
         throw PSTraceSource.NewInvalidOperationException();
     }
     InternalHostUserInterface uI = this.Host.UI as InternalHostUserInterface;
     ActionPreference progressPreference = this.ProgressPreference;
     if (overrideInquire && (progressPreference == ActionPreference.Inquire))
     {
         progressPreference = ActionPreference.Continue;
     }
     if (this.WriteHelper_ShouldWrite(progressPreference, this.lastProgressContinueStatus))
     {
         uI.WriteProgress(sourceId, progressRecord);
     }
     this.lastProgressContinueStatus = this.WriteHelper(null, null, progressPreference, this.lastProgressContinueStatus, "ProgressPreference", progressRecord.Activity);
 }
Beispiel #9
0
 private static extern bool __ContinueDebugEvent(uint dwProcessId, uint dwThreadId, ContinueStatus dwContinueStatus);
Beispiel #10
0
        internal bool WriteHelper_ShouldWrite(ActionPreference preference, ContinueStatus lastContinueStatus)
        {
            this.ThrowIfStopping();
            this.ThrowIfWriteNotPermitted(false);
            switch (lastContinueStatus)
            {
                case ContinueStatus.YesToAll:
                    return true;

                case ContinueStatus.NoToAll:
                    return false;
            }
            switch (preference)
            {
                case ActionPreference.SilentlyContinue:
                case ActionPreference.Ignore:
                    return false;

                case ActionPreference.Stop:
                case ActionPreference.Continue:
                case ActionPreference.Inquire:
                    return true;
            }
            return true;
        }
Beispiel #11
0
        internal ContinueStatus WriteHelper(string inquireCaption, string inquireMessage, ActionPreference preference, ContinueStatus lastContinueStatus, string preferenceVariableName, string message)
        {
            switch (lastContinueStatus)
            {
                case ContinueStatus.YesToAll:
                    return ContinueStatus.YesToAll;

                case ContinueStatus.NoToAll:
                    return ContinueStatus.NoToAll;
            }
            switch (preference)
            {
                case ActionPreference.SilentlyContinue:
                case ActionPreference.Continue:
                case ActionPreference.Ignore:
                    return ContinueStatus.Yes;

                case ActionPreference.Stop:
                {
                    ActionPreferenceStopException exception = new ActionPreferenceStopException(this.MyInvocation, this.CBResourcesBaseName, "ErrorPreferenceStop", new object[] { preferenceVariableName, message });
                    throw this.ManageException(exception);
                }
                case ActionPreference.Inquire:
                    return this.InquireHelper(inquireMessage, inquireCaption, true, false, true);
            }
            ActionPreferenceStopException e = new ActionPreferenceStopException(this.MyInvocation, this.CBResourcesBaseName, "PreferenceInvalid", new object[] { preferenceVariableName, preference });
            throw this.ManageException(e);
        }
Beispiel #12
0
 internal void WriteDebug(DebugRecord record, bool overrideInquire = false)
 {
     ActionPreference debugPreference = this.DebugPreference;
     if (overrideInquire && (debugPreference == ActionPreference.Inquire))
     {
         debugPreference = ActionPreference.Continue;
     }
     if (this.WriteHelper_ShouldWrite(debugPreference, this.lastDebugContinueStatus))
     {
         if (record.InvocationInfo == null)
         {
             record.SetInvocationInfo(this.MyInvocation);
         }
         if (this.DebugOutputPipe != null)
         {
             if (((this.CBhost != null) && (this.CBhost.InternalUI != null)) && this.DebugOutputPipe.NullPipe)
             {
                 this.CBhost.InternalUI.WriteDebugInfoBuffers(record);
             }
             PSObject obj2 = PSObject.AsPSObject(record);
             if (obj2.Members["WriteDebugStream"] == null)
             {
                 obj2.Properties.Add(new PSNoteProperty("WriteDebugStream", true));
             }
             this.DebugOutputPipe.Add(obj2);
         }
         else
         {
             if ((this.Host == null) || (this.Host.UI == null))
             {
                 throw PSTraceSource.NewInvalidOperationException();
             }
             this.CBhost.InternalUI.WriteDebugRecord(record);
         }
     }
     this.lastDebugContinueStatus = this.WriteHelper(null, null, debugPreference, this.lastDebugContinueStatus, "DebugPreference", record.Message);
 }
Beispiel #13
0
        private bool DoShouldProcess(string verboseDescription, string verboseWarning, string caption, out ShouldProcessReason shouldProcessReason)
        {
            this.ThrowIfStopping();
            shouldProcessReason = ShouldProcessReason.None;
            switch (this.lastShouldProcessContinueStatus)
            {
                case ContinueStatus.YesToAll:
                    return true;

                case ContinueStatus.NoToAll:
                    return false;

                default:
                    if (this.WhatIf != 0)
                    {
                        this.ThrowIfWriteNotPermitted(false);
                        shouldProcessReason = ShouldProcessReason.WhatIf;
                        string str = StringUtil.Format(CommandBaseStrings.ShouldProcessWhatIfMessage, verboseDescription);
                        this.CBhost.UI.WriteLine(str);
                        return false;
                    }
                    if (this.CanShouldProcessAutoConfirm())
                    {
                        if (this.Verbose)
                        {
                            this.ThrowIfWriteNotPermitted(false);
                            this.WriteVerbose(verboseDescription);
                        }
                        return true;
                    }
                    if (string.IsNullOrEmpty(verboseWarning))
                    {
                        verboseWarning = StringUtil.Format(CommandBaseStrings.ShouldProcessWarningFallback, verboseDescription);
                    }
                    this.ThrowIfWriteNotPermitted(false);
                    this.lastShouldProcessContinueStatus = this.InquireHelper(verboseWarning, caption, true, true, false);
                    switch (this.lastShouldProcessContinueStatus)
                    {
                        case ContinueStatus.No:
                        case ContinueStatus.NoToAll:
                            return false;
                    }
                    break;
            }
            return true;
        }
Beispiel #14
0
        } // WriteHelper_ShouldWrite

        /// <summary>
        /// Complete implementation of WriteDebug/WriteVerbose/WriteProgress
        /// </summary>
        /// <param name="inquireCaption"></param>
        /// <param name="inquireMessage"></param>
        /// <param name="preference"></param>
        /// <param name="lastContinueStatus"></param>
        /// <param name="preferenceVariableName"></param>
        /// <param name="message"></param>
        /// <returns>Did Inquire return YesToAll?</returns>
        /// <exception cref="System.Management.Automation.PipelineStoppedException">
        /// The pipeline has already been terminated, or was terminated
        /// during the execution of this method.
        /// The Cmdlet should generally just allow PipelineStoppedException
        /// to percolate up to the caller of ProcessRecord etc.
        /// </exception>
        /// <remarks>
        /// If the pipeline is terminated due to ActionPreference.Stop
        /// or ActionPreference.Inquire, this method will throw
        /// <see cref="System.Management.Automation.PipelineStoppedException"/>,
        /// but the command failure will ultimately be
        /// <see cref="System.Management.Automation.ActionPreferenceStopException"/>,
        /// </remarks>
        internal ContinueStatus WriteHelper(
            string inquireCaption,
            string inquireMessage,
            ActionPreference preference,
            ContinueStatus lastContinueStatus,
            string preferenceVariableName,
            string message)
        {
            switch (lastContinueStatus)
            {
                case ContinueStatus.NoToAll:  // previously answered NoToAll
                    return ContinueStatus.NoToAll;
                case ContinueStatus.YesToAll: // previously answered YesToAll
                    return ContinueStatus.YesToAll;
            }

            switch (preference)
            {
                case ActionPreference.Ignore: // YesToAll
                case ActionPreference.SilentlyContinue:
                case ActionPreference.Continue:
                    return ContinueStatus.Yes;

                case ActionPreference.Stop:
                    ActionPreferenceStopException e =
                        new ActionPreferenceStopException(
                            MyInvocation,
                            StringUtil.Format(CommandBaseStrings.ErrorPreferenceStop, preferenceVariableName, message));
                    throw ManageException(e);

                case ActionPreference.Inquire:
                    break;

                default:
                    Dbg.Assert(false, "Bad preference value" + preference);
                    ActionPreferenceStopException apse =
                        new ActionPreferenceStopException(
                            MyInvocation,
                            StringUtil.Format(CommandBaseStrings.PreferenceInvalid, preferenceVariableName, preference));
                    throw ManageException(apse);
            }

            return InquireHelper(
                inquireMessage,
                inquireCaption,
                true,  // allowYesToAll
                false, // allowNoToAll
                true,  // replaceNoWithHalt
                false  // hasSecurityImpact
            );
        } // WriteHelper
Beispiel #15
0
        /// <summary>
        /// Should the verbose/debug/progress message be printed?
        /// </summary>
        /// <param name="preference"></param>
        /// <param name="lastContinueStatus"></param>
        /// <returns></returns>
        /// <exception cref="System.Management.Automation.PipelineStoppedException"></exception>
        /// <exception cref="System.InvalidOperationException"></exception>
        internal bool WriteHelper_ShouldWrite(
            ActionPreference preference,
            ContinueStatus lastContinueStatus)
        {
            ThrowIfStopping();

            // 2005/05/24 908827
            // WriteDebug/WriteVerbose/WriteProgress/WriteWarning should only be callable from the main thread
            //
            // WriteError/WriteObject have a check that prevents them to be called from outside 
            // Begin/Process/End. This is done because the Pipeline needs to be ready before these 
            // functions can be called.
            //
            // WriteDebug/Warning/Verbose/Process used to do the same check, even though it is not 
            // strictly needed. If we ever implement pipelines for these objects we may need to 
            // enforce the check again.
            //
            // See bug 583774 in the Windows 7 database for more details.
            //
            ThrowIfWriteNotPermitted(false);

            switch (lastContinueStatus)
            {
                case ContinueStatus.NoToAll:  // previously answered NoToAll
                    return false;
                case ContinueStatus.YesToAll: // previously answered YesToAll
                    return true;
            }

            switch (preference)
            {
                case ActionPreference.Ignore:
                case ActionPreference.SilentlyContinue:
                    return false;

                case ActionPreference.Continue:
                case ActionPreference.Stop:
                case ActionPreference.Inquire:
                    return true;

                default:
                    Dbg.Assert(false, "Bad preference value" + preference);
                    return true;
            }
        } // WriteHelper_ShouldWrite
Beispiel #16
0
 public void ContinueDebugEvent(ContinueStatus status) => Kernel32.ContinueDebugEvent(dwProcessId, dwThreadId, status);
Beispiel #17
0
 public static extern bool ContinueDebugEvent(
     uint dwProcessId,
     uint dwThreadId,
     ContinueStatus dwContinueStatus);
Beispiel #18
0
 internal void _WriteErrorSkipAllowCheck(ErrorRecord errorRecord, ActionPreference? actionPreference = new ActionPreference?())
 {
     this.ThrowIfStopping();
     if ((errorRecord.ErrorDetails != null) && (errorRecord.ErrorDetails.TextLookupError != null))
     {
         Exception textLookupError = errorRecord.ErrorDetails.TextLookupError;
         errorRecord.ErrorDetails.TextLookupError = null;
         MshLog.LogCommandHealthEvent(this.context, textLookupError, Severity.Warning);
     }
     this.pipelineProcessor.ExecutionFailed = true;
     if (this.shouldLogPipelineExecutionDetail)
     {
         this.pipelineProcessor.LogExecutionError(this.thisCommand.MyInvocation, errorRecord);
     }
     ActionPreference errorAction = this.ErrorAction;
     if (actionPreference.HasValue)
     {
         errorAction = actionPreference.Value;
     }
     if (ActionPreference.Ignore != errorAction)
     {
         if (errorAction == ActionPreference.SilentlyContinue)
         {
             this.AppendErrorToVariables(errorRecord);
         }
         else
         {
             if (ContinueStatus.YesToAll == this.lastErrorContinueStatus)
             {
                 errorAction = ActionPreference.Continue;
             }
             switch (errorAction)
             {
                 case ActionPreference.Stop:
                 {
                     ActionPreferenceStopException e = new ActionPreferenceStopException(this.MyInvocation, errorRecord, this.CBResourcesBaseName, "ErrorPreferenceStop", new object[] { "ErrorActionPreference", errorRecord.ToString() });
                     throw this.ManageException(e);
                 }
                 case ActionPreference.Inquire:
                     this.lastErrorContinueStatus = this.InquireHelper(RuntimeException.RetrieveMessage(errorRecord), null, true, false, true);
                     break;
             }
             this.AppendErrorToVariables(errorRecord);
             PSObject obj2 = PSObject.AsPSObject(errorRecord);
             if (obj2.Members["writeErrorStream"] == null)
             {
                 PSNoteProperty member = new PSNoteProperty("writeErrorStream", true);
                 obj2.Properties.Add(member);
             }
             if (this.ErrorMergeTo != MergeDataStream.None)
             {
                 this.OutputPipe.AddWithoutAppendingOutVarList(obj2);
             }
             else
             {
                 this.ErrorOutputPipe.AddWithoutAppendingOutVarList(obj2);
             }
         }
     }
 }