public void Break(ScriptProgramNode program)
        {
            Log.Debug("Break");
            var iid = new Guid(BreakEvent.IID);

            _callback.Event(_engine, null, program, program, new BreakEvent(), ref iid, BreakEvent.Attributes);
        }
        /// <summary>
        ///     Attaches to the specified program nodes. This is the main entry point to debugging.
        /// </summary>
        /// <remarks>
        ///     This method is responsible for firing the correct Visual Studio events to begin debugging
        ///     and then to start the actual PowerShell execution.
        /// </remarks>
        /// <param name="rgpPrograms">The programs.</param>
        /// <param name="rgpProgramNodes">The program nodes.</param>
        /// <param name="celtPrograms">The celt programs.</param>
        /// <param name="pCallback">The callback.</param>
        /// <param name="dwReason">The reason.</param>
        /// <returns></returns>
        public int Attach(IDebugProgram2[] rgpPrograms, IDebugProgramNode2[] rgpProgramNodes, uint celtPrograms,
                          IDebugEventCallback2 pCallback, enum_ATTACH_REASON dwReason)
        {
            Log.Debug("Attaching the debug engine.");

            Guid id;

            rgpPrograms[0].GetProgramId(out id);

            if (_node == null)
            {
                _node = rgpProgramNodes[0] as ScriptProgramNode;
                _node.IsAttachedProgram = dwReason == enum_ATTACH_REASON.ATTACH_REASON_USER;
            }

            _node.Id = id;

            var publisher = (IDebugProgramPublisher2) new DebugProgramPublisher();

            publisher.PublishProgramNode(_node);

            _events = new EngineEvents(this, pCallback);
            _events.RunspaceRequest();
            _events.EngineCreated();
            _events.ProgramCreated(_node);
            _events.EngineLoaded();

            _events.DebugEntryPoint();

            Task.Factory.StartNew(Execute);

            return(VSConstants.S_OK);
        }
        public int LaunchSuspended(string pszServer, IDebugPort2 pPort, string pszExe, string pszArgs, string pszDir,
                                   string bstrEnv, string pszOptions, enum_LAUNCH_FLAGS dwLaunchFlags, uint hStdInput,
                                   uint hStdOutput, uint hStdError, IDebugEventCallback2 pCallback,
                                   out IDebugProcess2 ppProcess)
        {
            _runspaceSet.Reset();

            if (dwLaunchFlags.HasFlag(enum_LAUNCH_FLAGS.LAUNCH_DEBUG))
            {
                ppProcess = new ScriptDebugProcess(pPort);
            }
            else
            {
                ppProcess = new ScriptDebugProcess(pPort);
            }

            _node = (ppProcess as ScriptDebugProcess).Node;

            if (pszExe == "Selection")
            {
                _node.IsFile   = false;
                _node.FileName = pszOptions;
            }
            else
            {
                _node.IsFile    = true;
                _node.FileName  = pszExe;
                _node.Arguments = pszArgs;
            }


            _events = new EngineEvents(this, pCallback);

            return(VSConstants.S_OK);
        }
        public int GetProviderProcessData(enum_PROVIDER_FLAGS Flags, IDebugDefaultPort2 pPort, AD_PROCESS_ID ProcessId, CONST_GUID_ARRAY EngineFilter, PROVIDER_PROCESS_DATA[] pProcess)
        {
            Log.Debug("ProgramProvider: GetProviderProcessData");

            if (Flags.HasFlag(enum_PROVIDER_FLAGS.PFLAG_GET_PROGRAM_NODES))
            {
                var process = Process.GetProcessById((int) ProcessId.dwProcessId);
                foreach (ProcessModule module in process.Modules)
                {
                    if (module.ModuleName.StartsWith("System.Management.Automation", StringComparison.OrdinalIgnoreCase))
                    {
                        var node = new ScriptProgramNode(new ScriptDebugProcess(pPort, ProcessId.dwProcessId));

                        var programNodes = new[] { Marshal.GetComInterfaceForObject(node, typeof(IDebugProgramNode2)) };

                        var destinationArray = Marshal.AllocCoTaskMem(IntPtr.Size * programNodes.Length);
                        Marshal.Copy(programNodes, 0, destinationArray, programNodes.Length);

                        pProcess[0].Fields = enum_PROVIDER_FIELDS.PFIELD_PROGRAM_NODES;
                        pProcess[0].ProgramNodes.Members = destinationArray;
                        pProcess[0].ProgramNodes.dwCount = (uint)programNodes.Length;

                        return VSConstants.S_OK;
                    }
                }
            }

            return VSConstants.S_FALSE;
        }
Example #5
0
        public int GetProviderProcessData(enum_PROVIDER_FLAGS Flags, IDebugDefaultPort2 pPort, AD_PROCESS_ID ProcessId, CONST_GUID_ARRAY EngineFilter, PROVIDER_PROCESS_DATA[] pProcess)
        {
            Log.Debug("ProgramProvider: GetProviderProcessData");

            if (Flags.HasFlag(enum_PROVIDER_FLAGS.PFLAG_GET_PROGRAM_NODES))
            {
                var process = Process.GetProcessById((int)ProcessId.dwProcessId);
                foreach (ProcessModule module in process.Modules)
                {
                    if (module.ModuleName.StartsWith("System.Management.Automation", StringComparison.OrdinalIgnoreCase))
                    {
                        var node = new ScriptProgramNode(new ScriptDebugProcess(pPort, ProcessId.dwProcessId));

                        var programNodes = new[] { Marshal.GetComInterfaceForObject(node, typeof(IDebugProgramNode2)) };

                        var destinationArray = Marshal.AllocCoTaskMem(IntPtr.Size * programNodes.Length);
                        Marshal.Copy(programNodes, 0, destinationArray, programNodes.Length);

                        pProcess[0].Fields = enum_PROVIDER_FIELDS.PFIELD_PROGRAM_NODES;
                        pProcess[0].ProgramNodes.Members = destinationArray;
                        pProcess[0].ProgramNodes.dwCount = (uint)programNodes.Length;

                        return(VSConstants.S_OK);
                    }
                }
            }

            return(VSConstants.S_FALSE);
        }
        public int GetProviderProcessData(enum_PROVIDER_FLAGS Flags, IDebugDefaultPort2 pPort, AD_PROCESS_ID ProcessId, CONST_GUID_ARRAY EngineFilter, PROVIDER_PROCESS_DATA[] pProcess)
        {
            Log.Debug("ProgramProvider: GetProviderProcessData");

            try
            {
                if (Flags.HasFlag(enum_PROVIDER_FLAGS.PFLAG_GET_PROGRAM_NODES))
                {
                    if (PowerShellToolsPackage.DebuggingService.IsAttachable(ProcessId.dwProcessId))
                    {
                        var node             = new ScriptProgramNode(new ScriptDebugProcess(pPort, ProcessId.dwProcessId));
                        var programNodes     = new[] { Marshal.GetComInterfaceForObject(node, typeof(IDebugProgramNode2)) };
                        var destinationArray = Marshal.AllocCoTaskMem(IntPtr.Size * programNodes.Length);

                        Marshal.Copy(programNodes, 0, destinationArray, programNodes.Length);
                        pProcess[0].Fields = enum_PROVIDER_FIELDS.PFIELD_PROGRAM_NODES;
                        pProcess[0].ProgramNodes.Members = destinationArray;
                        pProcess[0].ProgramNodes.dwCount = (uint)programNodes.Length;

                        return(VSConstants.S_OK);
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Debug("Exception while examining local running process: " + ex.Message);
            }

            return(VSConstants.S_FALSE);
        }
Example #7
0
 public ScriptDebugProcess(IDebugPort2 debugPort)
 {
     Log.Debug("Process: Constructor");
     Id    = Guid.NewGuid();
     _port = debugPort;
     Node  = new ScriptProgramNode(this);
 }
 public ScriptDebugProcess(IDebugPort2 debugPort)
 {
     Log.Debug("Process: Constructor");
     Id = Guid.NewGuid();
     _port = debugPort;
     Node = new ScriptProgramNode(this);
 }
        public void BreakpointHit(ScriptBreakpoint breakpoint, ScriptProgramNode node)
        {
            Log.Debug("BreakpointHit");
            var iid = new Guid(BreakPointHitEvent.IID);

            _callback.Event(_engine, null, node, node, new BreakPointHitEvent(breakpoint), ref iid, BreakPointHitEvent.Attributes);
        }
        public void Exception(ScriptProgramNode program, Exception ex)
        {
            Log.Debug("Exception");
            var iid = new Guid(ExceptionEvent.IID);

            _callback.Event(_engine, null, program, program, new ExceptionEvent(program, ex), ref iid, ExceptionEvent.Attributes);
        }
Example #11
0
        public ScriptBreakpoint(ScriptProgramNode node, string file, int line, int column, IEngineEvents callback)
        {
            Log.InfoFormat("ScriptBreakPoint: {0} {1} {2}", file, line, column);

            _node = node;
            _callback = callback;
            Line = line;
            Column = column;
            File = file;
        }
        public ScriptBreakpoint(ScriptProgramNode node, string file, int line, int column, IEngineEvents callback)
        {
            Log.InfoFormat("ScriptBreakPoint: {0} {1} {2}", file, line, column);

            _node     = node;
            _callback = callback;
            Line      = line;
            Column    = column;
            File      = file;
        }
        public ScriptStackFrameCollection(IEnumerable <ScriptStackFrame> frames, ScriptProgramNode node)
        {
            foreach (var frame in frames)
            {
                Add(frame);
            }

            _iterationLocation = 0;
            _node = node;
        }
Example #14
0
        /// <summary>
        /// Execute the current program node.
        /// </summary>
        /// <remarks>
        /// The node will either be a script file or script content; depending on the node
        /// passed to this function.
        /// </remarks>
        /// <param name="node"></param>
        public void Execute(ScriptProgramNode node)
        {
            CurrentExecutingNode = node;

            if (node.IsAttachedProgram)
            {
                string result = string.Empty;
                if (!node.IsRemoteProgram)
                {
                    result = DebuggingService.AttachToRunspace(node.Process.ProcessId);
                }
                else
                {
                    result = DebuggingService.AttachToRemoteRunspace(node.Process.ProcessId, node.Process.HostName);
                }

                if (!string.IsNullOrEmpty(result))
                {
                    // if either of the attaches returns an error, let the user know
                    MessageBox.Show(result, Resources.AttachErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    // see what state we are in post error, if not in a local state, we need to try and get there
                    DebugScenario postCleanupScenario = DebuggingService.GetDebugScenario();

                    // try as hard as we can to detach/cleanup the mess for the length of CleanupRetryTimeout
                    TimeSpan  retryTimeSpan = TimeSpan.FromMilliseconds(DebugEngineConstants.CleanupRetryTimeout);
                    Stopwatch timeElapsed   = Stopwatch.StartNew();
                    while (timeElapsed.Elapsed < retryTimeSpan && postCleanupScenario != DebugScenario.Local)
                    {
                        postCleanupScenario = DebuggingService.CleanupAttach();
                    }

                    // if our efforts to cleanup the mess were unsuccessful, inform the user
                    if (postCleanupScenario != DebugScenario.Local)
                    {
                        MessageBox.Show(Resources.CleanupErrorMessage, Resources.DetachErrorTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    RefreshPrompt();
                    DebuggerFinished();
                }
            }
            else
            {
                string commandLine = node.FileName;

                if (node.IsFile)
                {
                    commandLine = String.Format(DebugEngineConstants.ExecutionCommandFormat, node.FileName, node.Arguments);
                    HostUi.VsOutputString(string.Format("{0}{1}{2}", GetPrompt(), node.FileName, Environment.NewLine));
                }
                Execute(commandLine);
            }
        }
 public ScriptStackFrame(ScriptProgramNode node, string scriptName, string frame, int startLine, int endLine, int startColumn, int endColumn)
 {
     if (node == null)
     {
         throw new ArgumentNullException("node");
     }
     _node     = node;
     _debugger = node.Debugger;
     // VS is zero based, PS is 1 based
     _docContext = new ScriptDocumentContext(scriptName, frame, startLine - 1, endLine - 1, startColumn - 1, endColumn - 1);
 }
Example #16
0
 public ScriptStackFrame(ScriptProgramNode node, CallStackFrame frame)
 {
     if (node == null)
     {
         throw new ArgumentNullException("node");
     }
     _node     = node;
     _debugger = node.Debugger;
     // VS is zero based, PS is 1 based
     _docContext = new ScriptDocumentContext(frame.ScriptName, frame.ScriptLineNumber - 1, 0, frame.ToString());
     _frame      = frame;
 }
        /// <summary>
        /// Execute the current program node.
        /// </summary>
        /// <remarks>
        /// The node will either be a script file or script content; depending on the node
        /// passed to this function.
        /// </remarks>
        /// <param name="node"></param>
        public void Execute(ScriptProgramNode node)
        {
            CurrentExecutingNode = node;

            if (node.IsAttachedProgram)
            {
                Execute(String.Format("Enter-PSHostProcess -Id {0};", node.Process.ProcessId));
                Execute("Debug-Runspace 1");
            }
            else
            {
                string commandLine = node.FileName;

                if (node.IsFile)
                {
                    commandLine = String.Format(". '{0}' {1}", node.FileName, node.Arguments);
                }
                Execute(commandLine);
            }
        }
        public void ShouldExecuteSnippet()
        {
            var sbps = new List<ScriptBreakpoint>();
            _debugger = new ScriptDebugger(true, null);
            _debugger.SetRunspace(_runspace);
            _debugger.SetBreakpoints(sbps);

            var node = new ScriptProgramNode(null);
            node.FileName = "$Global:MyVariable = 'Test'";

            var mre = new ManualResetEvent(false);
            _debugger.DebuggingFinished += (sender, args) => mre.Set();

            _debugger.Execute(node);

            Assert.IsTrue(mre.WaitOne(5000));

            var myVariable = _runspace.SessionStateProxy.GetVariable("MyVariable");

            Assert.AreEqual("Test", myVariable);
        }
        public void ShouldSupportRemoteSession()
        {
            var sbps = new List<ScriptBreakpoint>();
            _debugger = new ScriptDebugger(true, null);

            _runspace.Dispose();
            _runspace = RunspaceFactory.CreateRunspace(_debugger);
            _runspace.Open();
            _debugger.SetRunspace(_runspace);
            _debugger.SetBreakpoints(sbps);

            var node = new ScriptProgramNode(null);
            node.FileName = "Enter-PSSession localhost";

            var mre = new ManualResetEvent(false);
            string outputString = null;
            _debugger.DebuggingFinished += (sender, args) => mre.Set();
            _debugger.OutputString += (sender, args) => outputString = args.Value;
            _debugger.Execute(node);

            Assert.IsTrue(mre.WaitOne(5000));

            mre.Reset();
            node = new ScriptProgramNode(null);
            node.FileName = "$host.Name";
            _debugger.Execute(node);
            Assert.IsTrue(mre.WaitOne(5000));

            Assert.AreEqual("ServerRemoteHost", outputString);
        }
        public void ShouldBreakOnBreakPoint()
        {
            var engineEvents = new Mock<IEngineEvents>();

            var fi = new FileInfo(".\\TestFile.ps1");

            var sbps = new List<ScriptBreakpoint>
                           {
                               new ScriptBreakpoint(null, fi.FullName, 3, 0, engineEvents.Object)
                           };

            _debugger = new ScriptDebugger(true, null);
            _debugger.SetRunspace(_runspace);
            _debugger.SetBreakpoints(sbps);

            var node = new ScriptProgramNode(null);
            node.IsFile = true;
            node.FileName = fi.FullName;

            var mre = new ManualResetEvent(false);

            bool breakpointHit = false;
            _debugger.BreakpointHit += (sender, args) => { breakpointHit = true; _debugger.Continue(); };
            _debugger.DebuggingFinished += (sender, args) => mre.Set();
            _debugger.Execute(node);

            Assert.IsTrue(mre.WaitOne(5000));
            Assert.IsTrue(breakpointHit);
        }
        public void ShouldAcceptArguments()
        {
            var fi = new FileInfo(".\\TestFile.ps1");

            var sbps = new List<ScriptBreakpoint>();
            _debugger = new ScriptDebugger(true, null);
            _debugger.SetRunspace(_runspace);
            _debugger.SetBreakpoints(sbps);

            var node = new ScriptProgramNode(null);
            node.FileName = fi.FullName;
            node.IsFile = true;
            node.Arguments = "'Arg1' 'Arg2'";

            var mre = new ManualResetEvent(false);
            _debugger.DebuggingFinished += (sender, args) => mre.Set();

            _debugger.Execute(node);

            Assert.IsTrue(mre.WaitOne(5000));

            var arg1 = _runspace.SessionStateProxy.GetVariable("Argument1");
            var arg2 = _runspace.SessionStateProxy.GetVariable("Argument2");

            Assert.AreEqual("Arg1", arg1);
            Assert.AreEqual("Arg2", arg2);
        }
Example #22
0
        public int LaunchSuspended(string pszServer, IDebugPort2 pPort, string pszExe, string pszArgs, string pszDir,
                                   string bstrEnv, string pszOptions, enum_LAUNCH_FLAGS dwLaunchFlags, uint hStdInput,
                                   uint hStdOutput, uint hStdError, IDebugEventCallback2 pCallback,
                                   out IDebugProcess2 ppProcess)
        {
            _runspaceSet.Reset();

            if (dwLaunchFlags.HasFlag(enum_LAUNCH_FLAGS.LAUNCH_DEBUG))
            {
                ppProcess = new ScriptDebugProcess(pPort);
            }
            else
            {
                ppProcess = new ScriptDebugProcess(pPort);
            }

            _node = (ppProcess as ScriptDebugProcess).Node;

            if (pszExe == "Selection")
            {
                _node.IsFile = false;
                _node.FileName = pszOptions;
            }
            else
            {
                _node.IsFile = true;
                _node.FileName = pszExe;
                _node.Arguments = pszArgs;
            }
            

            _events = new EngineEvents(this, pCallback);

            return VSConstants.S_OK;
        }
Example #23
0
        /// <summary>
        ///     Attaches to the specified program nodes. This is the main entry point to debugging.
        /// </summary>
        /// <remarks>
        ///     This method is responsible for firing the correct Visual Studio events to begin debugging
        ///     and then to start the actual PowerShell execution.
        /// </remarks>
        /// <param name="rgpPrograms">The programs.</param>
        /// <param name="rgpProgramNodes">The program nodes.</param>
        /// <param name="celtPrograms">The celt programs.</param>
        /// <param name="pCallback">The callback.</param>
        /// <param name="dwReason">The reason.</param>
        /// <returns></returns>
        public int Attach(IDebugProgram2[] rgpPrograms, IDebugProgramNode2[] rgpProgramNodes, uint celtPrograms,
                          IDebugEventCallback2 pCallback, enum_ATTACH_REASON dwReason)
        {
            Log.Debug("Attaching the debug engine.");

            Guid id;
            rgpPrograms[0].GetProgramId(out id);

            if (_node == null)
            {
                _node = rgpProgramNodes[0] as ScriptProgramNode;
                _node.IsAttachedProgram = dwReason == enum_ATTACH_REASON.ATTACH_REASON_USER;
            }

            _node.Id = id;

            var publisher = (IDebugProgramPublisher2) new DebugProgramPublisher();
            publisher.PublishProgramNode(_node);

            _events = new EngineEvents(this, pCallback);
            _events.RunspaceRequest();
            _events.EngineCreated();
            _events.ProgramCreated(_node);
            _events.EngineLoaded();

            _events.DebugEntryPoint();

            Task.Factory.StartNew(Execute);

            return VSConstants.S_OK;
        }
Example #24
0
 public void Break(ScriptProgramNode program)
 {
     Log.Debug("Break");
     var iid = new Guid(BreakEvent.IID);
     _callback.Event(_engine, null, program, program, new BreakEvent(), ref iid, BreakEvent.Attributes);
 }
Example #25
0
 public void Exception(ScriptProgramNode program, Exception ex)
 {
     Log.Debug("Exception");
     var iid = new Guid(ExceptionEvent.IID);
     _callback.Event(_engine, null, program, program, new ExceptionEvent(program, ex), ref iid, ExceptionEvent.Attributes);
 }
Example #26
0
 public void BreakpointHit(ScriptBreakpoint breakpoint, ScriptProgramNode node)
 {
     Log.Debug("BreakpointHit");
     var iid = new Guid(BreakPointHitEvent.IID);
     _callback.Event(_engine, null, node, node, new BreakPointHitEvent(breakpoint), ref iid, BreakPointHitEvent.Attributes);
 }