コード例 #1
0
        internal override void OnConnected()
        {
            // Finish initialization now that the socket connection has been established
            var          threads      = _process.GetThreads();
            PythonThread activeThread = null;

            var dte = _serviceProvider.GetDTE();

            if (dte != null)
            {
                // If we are broken into the debugger, let's set the debug REPL active thread
                // to be the one that is active in the debugger
                var dteDebugger = dte.Debugger;
                if (dteDebugger.CurrentMode == EnvDTE.dbgDebugMode.dbgBreakMode &&
                    dteDebugger.CurrentProcess != null &&
                    dteDebugger.CurrentThread != null)
                {
                    if (_process.Id == dteDebugger.CurrentProcess.ProcessID)
                    {
                        var activeThreadId = _threadIdMapper.GetPythonThreadId((uint)dteDebugger.CurrentThread.ID);
                        activeThread = threads.SingleOrDefault(t => t.Id == activeThreadId);
                    }
                }
            }

            if (activeThread == null)
            {
                activeThread = threads.Count > 0 ? threads[0] : null;
            }

            if (activeThread != null)
            {
                SwitchThread(activeThread, false);
            }
        }
コード例 #2
0
ファイル: DebugReplEvaluatorTests.cs プロジェクト: xNUTs/PTVS
        public void ChangeThread()
        {
            Attach("DebugReplTest3.py", 39);

            var          threads = _processes[0].GetThreads();
            PythonThread main    = threads.SingleOrDefault(t => t.Frames[0].FunctionName == "threadmain");
            PythonThread worker1 = threads.SingleOrDefault(t => t.Frames[0].FunctionName == "thread1");
            PythonThread worker2 = threads.SingleOrDefault(t => t.Frames[0].FunctionName == "thread2");

            // We are broken in the the main thread
            string text;

            text = ExecuteCommand(new DebugReplThreadsCommand(), "");
            Assert.IsTrue(text.Contains(String.Format("=> Thread id={0}, name=", main.Id)));
            Assert.IsTrue(text.Contains(String.Format("   Thread id={0}, name=", worker1.Id)));
            Assert.IsTrue(text.Contains(String.Format("   Thread id={0}, name=", worker2.Id)));
            Assert.AreEqual(main.Id.ToString(), ExecuteCommand(new DebugReplThreadCommand(), ""));
            Assert.AreEqual("False", ExecuteText("t1_done"));
            Assert.AreEqual("False", ExecuteText("t2_done"));

            // Switch to worker thread 1
            Assert.AreEqual(String.Format("Current thread changed to {0}, frame 0", worker1.Id), ExecuteCommand(new DebugReplThreadCommand(), worker1.Id.ToString()));
            text = ExecuteCommand(new DebugReplThreadsCommand(), "");
            Assert.IsTrue(text.Contains(String.Format("   Thread id={0}, name=", main.Id)));
            Assert.IsTrue(text.Contains(String.Format("=> Thread id={0}, name=", worker1.Id)));
            Assert.IsTrue(text.Contains(String.Format("   Thread id={0}, name=", worker2.Id)));
            Assert.AreEqual(worker1.Id.ToString(), ExecuteCommand(new DebugReplThreadCommand(), ""));
            Assert.AreEqual("'thread1'", ExecuteText("t1_val"));
        }
コード例 #3
0
        public void TestBreakpointFailed()
        {
            var debugger = new PythonDebugger();

            PythonThread     thread     = null;
            PythonBreakpoint breakPoint = null;
            var process = DebugProcess(debugger, DebuggerTestPath + "BreakpointTest.py", (newproc, newthread) => {
                breakPoint = newproc.AddBreakPoint("doesnotexist.py", 1);
                breakPoint.Add();
                thread = newthread;
            });

            bool bindFailed = false;

            process.BreakpointBindFailed += (sender, args) => {
                bindFailed = true;
                Assert.AreEqual(args.Breakpoint, breakPoint);
            };
            process.ExceptionRaised += (sender, args) => {
                args.Thread.Resume();
            };

            process.Start();

            process.WaitForExit();

            Assert.AreEqual(bindFailed, true);
        }
コード例 #4
0
        internal void SwitchThread(PythonThread thread, bool verbose)
        {
            var frame = thread.Frames.FirstOrDefault();

            if (frame == null)
            {
                WriteError(Strings.DebugReplCannotChangeCurrentThreadNoFrame.FormatUI(thread.Id));
                return;
            }

            _threadId             = thread.Id;
            _frameId              = frame.FrameId;
            _currentScopeName     = CurrentFrameScopeFixedName;
            _currentScopeFileName = null;
            if (_currentFrameFilename != frame.FileName)
            {
                _currentFrameFilename = frame.FileName;
                _analyzer             = null;
            }
            _currentFrameLocals = frame.Locals
                                  .Where(r => !string.IsNullOrEmpty(r.Expression))
                                  .Select(r => new CompletionResult(r.Expression, Interpreter.PythonMemberType.Field))
                                  .ToArray();
            if (verbose)
            {
                WriteOutput(Strings.DebugReplThreadChanged.FormatUI(_threadId, _frameId));
            }
        }
コード例 #5
0
        internal void SwitchThread(PythonThread thread, bool verbose)
        {
            var frame = thread.Frames.FirstOrDefault();

            if (frame == null)
            {
                WriteError(Strings.DebugReplCannotChangeCurrentThreadNoFrame.FormatUI(thread.Id));
                return;
            }

            _threadId             = thread.Id;
            _frameId              = frame.FrameId;
            _currentScopeName     = CurrentFrameScopeFixedName;
            _currentScopeFileName = null;
            if (_currentFrameFilename != frame.FileName)
            {
                _currentFrameFilename = frame.FileName;
                _analyzer             = null;
            }
            UpdateFrameLocals(frame);
            if (verbose)
            {
                WriteOutput(Strings.DebugReplThreadChanged.FormatUI(_threadId, _frameId));
            }
        }
コード例 #6
0
        public virtual async Task AttachWithOutputRedirection()
        {
            var expectedOutput = new[] { "stdout", "stderr" };

            string script = TestData.GetPath(@"TestData\DebuggerProject\AttachOutput.py");
            var    p      = Process.Start(Version.InterpreterPath, "\"" + script + "\"");

            try {
                using (var dumpWriter = new MiniDumpWriter(p)) {
                    Thread.Sleep(1000);
                    var proc = PythonProcess.Attach(p.Id, PythonDebugOptions.RedirectOutput);
                    try {
                        var attached = new TaskCompletionSource <bool>();
                        proc.ProcessLoaded += (sender, args) => {
                            Console.WriteLine("Process loaded");
                            attached.SetResult(true);
                        };
                        await proc.StartListeningAsync();

                        await attached.Task.WithTimeout(20000, "Failed to attach within 20s");

                        await proc.ResumeAsync(TimeoutToken());

                        var          bpHit  = new TaskCompletionSource <bool>();
                        PythonThread thread = null;
                        proc.BreakpointHit += (sender, args) => {
                            thread = args.Thread;
                            bpHit.SetResult(true);
                        };
                        var bp = proc.AddBreakpoint(script, 5);
                        await bp.AddAsync(TimeoutToken());

                        await bpHit.Task.WithTimeout(20000, "Failed to hit breakpoint within 20s");

                        Assert.IsNotNull(thread);

                        var actualOutput = new List <string>();
                        proc.DebuggerOutput += (sender, e) => {
                            Console.WriteLine("Debugger output: '{0}'", e.Output);
                            actualOutput.Add(e.Output);
                        };

                        var frame = thread.Frames[0];
                        Assert.AreEqual("False", (await frame.ExecuteTextAsync("attached", ct: CancellationTokens.After15s)).StringRepr);
                        await frame.ExecuteTextAsync("attached = True", ct : TimeoutToken());

                        await proc.ResumeAsync(TimeoutToken());

                        WaitForExit(proc);
                        AssertUtil.ArrayEquals(expectedOutput, actualOutput);
                    } finally {
                        await DetachProcessAsync(proc);
                    }

                    dumpWriter.Cancel();
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #7
0
            public async Task RunAsync()
            {
                PythonThread thread = await _tests.RunAndBreakAsync(FileName, LineNo, breakFilename : BreakFileName, arguments : Arguments, processLoaded : ProcessLoaded, debugOptions : DebugOptions);

                PythonProcess process = thread.Process;

                try {
                    var frames             = thread.Frames;
                    var localNamesExpected = Locals.Select(v => v.Expression).ToSet();
                    var paramNamesExpected = Params.Select(v => v.Expression).ToSet();

                    string fileNameExpected;
                    if (BreakFileName == null)
                    {
                        fileNameExpected = Path.GetFullPath(_tests.DebuggerTestPath + FileName);
                    }
                    else if (Path.IsPathRooted(BreakFileName))
                    {
                        fileNameExpected = BreakFileName;
                    }
                    else
                    {
                        fileNameExpected = Path.GetFullPath(_tests.DebuggerTestPath + BreakFileName);
                    }

                    Assert.AreEqual(frames[0].FileName, fileNameExpected, true);

                    if (!IgnoreExtra)
                    {
                        AssertUtil.ContainsExactly(frames[0].Locals.Select(x => x.Expression), localNamesExpected);
                        AssertUtil.ContainsExactly(frames[0].Parameters.Select(x => x.Expression), paramNamesExpected);
                    }

                    foreach (var expectedLocal in Locals)
                    {
                        var actualLocal = frames[0].Locals.First(v => v.Expression == expectedLocal.Expression);
                        expectedLocal.Validate(actualLocal);
                    }

                    foreach (var expectedParam in Params)
                    {
                        var actualParam = frames[0].Parameters.First(v => v.Expression == expectedParam.Expression);
                        expectedParam.Validate(actualParam);
                    }

                    await process.ResumeAsync(TimeoutToken());

                    if (WaitForExit)
                    {
                        _tests.WaitForExit(process);
                    }
                } finally {
                    if (!process.HasExited)
                    {
                        process.Terminate();
                    }
                }
            }
コード例 #8
0
    private IEnumerator StartGenCoroutine(string fileToGen)
    {
        yield return(new WaitForSeconds(1.5f));

        LoadScene.Instance.ChangeScene();
        UpdateTrainingCommand();
        PythonThread.ExecuteCommand(_commands[0]);
        MapReader.GM.InitMap(fileToGen);
    }
コード例 #9
0
        public virtual async Task AttachAndStepWithBlankSysPrefix()
        {
            string script = TestData.GetPath(@"TestData\DebuggerProject\InfiniteRunBlankPrefix.py");
            var    p      = Process.Start(Version.InterpreterPath, "\"" + script + "\"");

            try {
                using (var dumpWriter = new MiniDumpWriter(p)) {
                    Thread.Sleep(1000);
                    var proc = PythonProcess.Attach(p.Id);
                    try {
                        var attached = new TaskCompletionSource <bool>();
                        proc.ProcessLoaded += async(sender, args) => {
                            Console.WriteLine("Process loaded");
                            await proc.ResumeAsync(TimeoutToken());

                            attached.SetResult(true);
                        };
                        await proc.StartListeningAsync();

                        await attached.Task.WithTimeout(20000, "Failed to attach within 20s");

                        var              bpHit    = new TaskCompletionSource <bool>();
                        PythonThread     thread   = null;
                        PythonStackFrame oldFrame = null;
                        proc.BreakpointHit += (sender, args) => {
                            Console.WriteLine("Breakpoint hit");
                            thread   = args.Thread;
                            oldFrame = args.Thread.Frames[0];
                            bpHit.SetResult(true);
                        };
                        var bp = proc.AddBreakpoint(script, 6);
                        await bp.AddAsync(TimeoutToken());

                        await bpHit.Task.WithTimeout(20000, "Failed to hit breakpoint within 20s");

                        var stepComplete          = new TaskCompletionSource <bool>();
                        PythonStackFrame newFrame = null;
                        proc.StepComplete += (sender, args) => {
                            newFrame = args.Thread.Frames[0];
                            stepComplete.SetResult(true);
                        };
                        await thread.StepOverAsync(TimeoutToken());

                        await stepComplete.Task.WithTimeout(20000, "Failed to complete the step within 20s");

                        Assert.AreEqual(oldFrame.FileName, newFrame.FileName);
                        Assert.IsTrue(oldFrame.LineNo + 1 == newFrame.LineNo);
                    } finally {
                        await DetachProcessAsync(proc);
                    }

                    dumpWriter.Cancel();
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #10
0
ファイル: AttachTests.cs プロジェクト: zuokaihuang/PTVS
        public virtual void AttachWithOutputRedirection()
        {
            var expectedOutput = new[] { "stdout", "stderr" };

            string script = TestData.GetPath(@"TestData\DebuggerProject\AttachOutput.py");
            var    p      = Process.Start(Version.InterpreterPath, "\"" + script + "\"");

            try {
                Thread.Sleep(1000);
                var proc = PythonProcess.Attach(p.Id, PythonDebugOptions.RedirectOutput);
                try {
                    var attached = new AutoResetEvent(false);
                    proc.ProcessLoaded += (sender, args) => {
                        Console.WriteLine("Process loaded");
                        attached.Set();
                    };
                    proc.StartListening();

                    Assert.IsTrue(attached.WaitOne(20000), "Failed to attach within 20s");
                    proc.Resume();

                    var          bpHit  = new AutoResetEvent(false);
                    PythonThread thread = null;
                    proc.BreakpointHit += (sender, args) => {
                        thread = args.Thread;
                        bpHit.Set();
                    };
                    var bp = proc.AddBreakPoint(script, 5);
                    bp.Add();

                    Assert.IsTrue(bpHit.WaitOne(20000), "Failed to hit breakpoint within 20s");
                    Assert.IsNotNull(thread);

                    var actualOutput = new List <string>();
                    proc.DebuggerOutput += (sender, e) => {
                        Console.WriteLine("Debugger output: '{0}'", e.Output);
                        actualOutput.Add(e.Output);
                    };

                    var frame = thread.Frames[0];
                    Assert.AreEqual("False", frame.ExecuteTextAsync("attached").Result.StringRepr);
                    Assert.IsTrue(frame.ExecuteTextAsync("attached = True").Wait(20000), "Failed to complete evaluation within 20s");

                    proc.Resume();
                    WaitForExit(proc);
                    AssertUtil.ArrayEquals(expectedOutput, actualOutput);
                } finally {
                    DetachProcess(proc);
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #11
0
        private void EvalTest(string filename, int lineNo, string frameName, int frameIndex, EvalResult eval)
        {
            var          debugger = new PythonDebugger();
            PythonThread thread   = null;
            var          process  = DebugProcess(debugger, DebuggerTestPath + filename, (newproc, newthread) => {
                var breakPoint = newproc.AddBreakPoint(filename, lineNo);
                breakPoint.Add();
                thread = newthread;
            });

            AutoResetEvent brkHit = new AutoResetEvent(false);

            process.BreakpointHit += (sender, args) => {
                brkHit.Set();
            };
            process.ExceptionRaised += (sender, args) => {
                args.Thread.Resume();
            };

            process.Start();

            brkHit.WaitOne();

            var frames = thread.GetFrames();

            PythonEvaluationResult obj = null;
            string errorMsg;

            if (eval.IsError)
            {
                Assert.IsTrue(!frames[frameIndex].TryParseText(eval.Expression, out errorMsg));
                Assert.AreEqual(errorMsg, eval.ExceptionText);
            }
            else
            {
                Assert.IsTrue(frames[frameIndex].TryParseText(eval.Expression, out errorMsg));
                Assert.AreEqual(errorMsg, null);

                AutoResetEvent textExecuted = new AutoResetEvent(false);
                Assert.AreEqual(frameName, frames[frameIndex].FunctionName);
                frames[frameIndex].ExecuteText(eval.Expression, (completion) => {
                    obj = completion;
                    textExecuted.Set();
                }
                                               );
                textExecuted.WaitOne();
                eval.Validate(obj);
            }

            process.Continue();

            process.WaitForExit();
        }
コード例 #12
0
ファイル: AttachTests.cs プロジェクト: zuokaihuang/PTVS
        public virtual void AttachAndStepWithBlankSysPrefix()
        {
            string script = TestData.GetPath(@"TestData\DebuggerProject\InfiniteRunBlankPrefix.py");
            var    p      = Process.Start(Version.InterpreterPath, "\"" + script + "\"");

            try {
                Thread.Sleep(1000);
                var proc = PythonProcess.Attach(p.Id);
                try {
                    var attached = new AutoResetEvent(false);
                    proc.ProcessLoaded += (sender, args) => {
                        Console.WriteLine("Process loaded");
                        proc.Resume();
                        attached.Set();
                    };
                    proc.StartListening();
                    Assert.IsTrue(attached.WaitOne(20000), "Failed to attach within 20s");

                    var              bpHit    = new AutoResetEvent(false);
                    PythonThread     thread   = null;
                    PythonStackFrame oldFrame = null;
                    proc.BreakpointHit += (sender, args) => {
                        Console.WriteLine("Breakpoint hit");
                        thread   = args.Thread;
                        oldFrame = args.Thread.Frames[0];
                        bpHit.Set();
                    };
                    var bp = proc.AddBreakPoint(script, 6);
                    bp.Add();
                    Assert.IsTrue(bpHit.WaitOne(20000), "Failed to hit breakpoint within 20s");

                    var stepComplete          = new AutoResetEvent(false);
                    PythonStackFrame newFrame = null;
                    proc.StepComplete += (sender, args) => {
                        newFrame = args.Thread.Frames[0];
                        stepComplete.Set();
                    };
                    thread.StepOver();
                    Assert.IsTrue(stepComplete.WaitOne(20000), "Failed to complete the step within 20s");

                    Assert.AreEqual(oldFrame.FileName, newFrame.FileName);
                    Assert.IsTrue(oldFrame.LineNo + 1 == newFrame.LineNo);
                } finally {
                    DetachProcess(proc);
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #13
0
        internal void SwitchThread(PythonThread thread, bool verbose)
        {
            var frame = thread.Frames.FirstOrDefault();

            if (frame == null)
            {
                WriteError(Strings.DebugReplCannotChangeCurrentThreadNoFrame.FormatUI(thread.Id));
                return;
            }

            _threadId = thread.Id;
            _frameId  = frame.FrameId;
            _thread?.SetThreadAndFrameCommand(thread.Id, _frameId, frame.Kind);
            if (verbose)
            {
                WriteOutput(Strings.DebugReplThreadChanged.FormatUI(_threadId, _frameId));
            }
        }
コード例 #14
0
        internal void SwitchThread(PythonThread thread, bool verbose)
        {
            var frame = thread.Frames.FirstOrDefault();

            if (frame == null)
            {
                Window.WriteError(string.Format("Cannot change current thread to {0}, because it does not have any visible frames.", thread.Id));
                return;
            }

            _threadId = thread.Id;
            _frameId  = frame.FrameId;
            SetThreadAndFrameCommand(thread.Id, _frameId, frame.Kind);
            if (verbose)
            {
                Window.WriteLine(String.Format("Current thread changed to {0}, frame {1}", _threadId, _frameId));
            }
        }
コード例 #15
0
        public void TestBreakAllThreads()
        {
            var debugger = new PythonDebugger();

            PythonThread   thread  = null;
            AutoResetEvent loaded  = new AutoResetEvent(false);
            var            process = DebugProcess(debugger, DebuggerTestPath + "InfiniteThreads.py", (newproc, newthread) => {
                loaded.Set();
                thread = newthread;
            });

            process.ExceptionRaised += (sender, args) => {
                args.Thread.Resume();
            };

            process.Start();
            if (!loaded.WaitOne(10000))
            {
                Assert.Fail("Failed to load");
            }

            AutoResetEvent breakComplete = new AutoResetEvent(false);

            process.AsyncBreakComplete += (sender, args) => {
                breakComplete.Set();
            };

            // let loop run
            for (int i = 0; i < 100; i++)
            {
                Thread.Sleep(50);

                Debug.WriteLine(String.Format("Breaking {0}", i));
                process.Break();
                if (!breakComplete.WaitOne(10000))
                {
                    Console.WriteLine("Failed to break");
                }
                process.Resume();
                Debug.WriteLine(String.Format("Resumed {0}", i));
            }

            process.Terminate();
        }
コード例 #16
0
        public void TestBreakAll()
        {
            var debugger = new PythonDebugger();

            PythonThread   thread  = null;
            AutoResetEvent loaded  = new AutoResetEvent(false);
            var            process = DebugProcess(debugger, DebuggerTestPath + "BreakAllTest.py", (newproc, newthread) => {
                loaded.Set();
                thread = newthread;
            });

            process.ExceptionRaised += (sender, args) => {
                args.Thread.Resume();
            };

            process.Start();
            if (!loaded.WaitOne(10000))
            {
                Assert.Fail("Failed to load");
            }

            // let loop run
            Thread.Sleep(500);
            AutoResetEvent breakComplete = new AutoResetEvent(false);
            PythonThread   breakThread   = null;

            process.AsyncBreakComplete += (sender, args) => {
                breakThread = args.Thread;
                breakComplete.Set();
            };

            process.Break();
            if (!breakComplete.WaitOne(10000))
            {
                Assert.Fail("failed to break");
            }

            Assert.AreEqual(breakThread, thread);

            process.Resume();

            process.Terminate();
        }
コード例 #17
0
 internal void ChangeActiveThread(long id, bool verbose)
 {
     if (_activeEvaluator != null)
     {
         PythonThread thread = _activeEvaluator.GetThreads().SingleOrDefault(target => target.Id == id);
         if (thread != null)
         {
             _activeEvaluator.SwitchThread(thread, verbose);
         }
         else
         {
             CurrentWindow.WriteError(Strings.DebugReplInvalidThreadId.FormatUI(id));
         }
     }
     else
     {
         NoProcessError();
     }
 }
コード例 #18
0
 internal void ChangeActiveThread(long id, bool verbose)
 {
     if (_activeEvaluator != null)
     {
         PythonThread thread = _activeEvaluator.GetThreads().SingleOrDefault(target => target.Id == id);
         if (thread != null)
         {
             _activeEvaluator.SwitchThread(thread, verbose);
         }
         else
         {
             _window.WriteError(String.Format("Invalid thread id '{0}'.", id));
         }
     }
     else
     {
         NoProcessError();
     }
 }
コード例 #19
0
 internal void ChangeActiveThread(long id, bool verbose)
 {
     if (_activeEvaluator != null)
     {
         PythonThread thread = _activeEvaluator.GetThreads().SingleOrDefault(target => target.Id == id);
         if (thread != null)
         {
             _activeEvaluator.SwitchThread(thread, verbose);
         }
         else
         {
             CurrentWindow.WriteErrorLine(Strings.DebugReplInvalidThreadId.FormatUI(id));
         }
     }
     else if (CustomDebugAdapterProtocolExtension.CanUseExperimental())
     {
         NotSupported();
     }
     else
     {
         NoProcessError();
     }
 }
コード例 #20
0
        private void LocalsTest(string filename, int lineNo, string[] paramNames, string[] localsNames)
        {
            var          debugger = new PythonDebugger();
            PythonThread thread   = null;
            var          process  = DebugProcess(debugger, DebuggerTestPath + filename, (newproc, newthread) => {
                var breakPoint = newproc.AddBreakPoint(filename, lineNo);
                breakPoint.Add();
                thread = newthread;
            });

            AutoResetEvent brkHit = new AutoResetEvent(false);

            process.BreakpointHit += (sender, args) => {
                brkHit.Set();
            };
            process.ExceptionRaised += (sender, args) => {
                // some versions of Python raise exceptions
                args.Thread.Resume();
            };

            process.Start();

            brkHit.WaitOne();

            var frames         = thread.GetFrames();
            var localsExpected = new HashSet <string>(localsNames);
            var paramsExpected = new HashSet <string>(paramNames);

            Assert.IsTrue(localsExpected.ContainsExactly(frames[0].Locals.Select(x => x.Expression)));
            Assert.IsTrue(paramsExpected.ContainsExactly(frames[0].Parameters.Select(x => x.Expression)));
            Assert.AreEqual(frames[0].FileName, Path.GetFullPath(DebuggerTestPath + filename));

            process.Continue();

            process.WaitForExit();
        }
コード例 #21
0
ファイル: AttachTests.cs プロジェクト: zhangjinxing/PTVS
        public virtual async Task AttachThreadingStartNewThread()
        {
            // http://pytools.codeplex.com/workitem/638
            // http://pytools.codeplex.com/discussions/285741#post724014
            var psi = new ProcessStartInfo(Version.InterpreterPath, "\"" + TestData.GetPath(@"TestData\DebuggerProject\ThreadingStartNewThread.py") + "\"");

            psi.WorkingDirectory = TestData.GetPath(@"TestData\DebuggerProject");
            psi.EnvironmentVariables["PYTHONPATH"] = @"..\..";
            psi.UseShellExecute        = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;
            Process p = Process.Start(psi);

            try {
                if (p.WaitForExit(1000))
                {
                    Assert.Fail("Process exited");
                }

                var proc = PythonProcess.Attach(p.Id);
                try {
                    using (var attached = new AutoResetEvent(false))
                        using (var readyToContinue = new AutoResetEvent(false))
                            using (var threadBreakpointHit = new AutoResetEvent(false)) {
                                proc.ProcessLoaded += async(sender, args) => {
                                    attached.Set();
                                    var bp = proc.AddBreakpoint("ThreadingStartNewThread.py", 9);
                                    await bp.AddAsync(TimeoutToken());

                                    bp = proc.AddBreakpoint("ThreadingStartNewThread.py", 5);
                                    await bp.AddAsync(TimeoutToken());

                                    await proc.ResumeAsync(TimeoutToken());
                                };
                                PythonThread mainThread = null;
                                PythonThread bpThread   = null;
                                bool         wrongLine  = false;
                                proc.BreakpointHit += async(sender, args) => {
                                    if (args.Breakpoint.LineNo == 9)
                                    {
                                        // stop running the infinite loop
                                        Debug.WriteLine(String.Format("First BP hit {0}", args.Thread.Id));
                                        mainThread = args.Thread;
                                        await args.Thread.Frames[0].ExecuteTextAsync("x = False", (x) => { readyToContinue.Set(); }, TimeoutToken());
                                    }
                                    else if (args.Breakpoint.LineNo == 5)
                                    {
                                        // we hit the breakpoint on the new thread
                                        Debug.WriteLine(String.Format("Second BP hit {0}", args.Thread.Id));
                                        bpThread = args.Thread;
                                        threadBreakpointHit.Set();
                                        await proc.ResumeAsync(TimeoutToken());
                                    }
                                    else
                                    {
                                        Debug.WriteLine(String.Format("Hit breakpoint on wrong line number: {0}", args.Breakpoint.LineNo));
                                        wrongLine = true;
                                        attached.Set();
                                        threadBreakpointHit.Set();
                                        await proc.ResumeAsync(TimeoutToken());
                                    }
                                };

                                await proc.StartListeningAsync();

                                Assert.IsTrue(attached.WaitOne(30000), "Failed to attach within 30s");

                                Assert.IsTrue(readyToContinue.WaitOne(30000), "Failed to hit the main thread breakpoint within 30s");
                                await proc.ResumeAsync(TimeoutToken());

                                Assert.IsTrue(threadBreakpointHit.WaitOne(30000), "Failed to hit the background thread breakpoint within 30s");
                                Assert.IsFalse(wrongLine, "Breakpoint broke on the wrong line");

                                Assert.AreNotEqual(mainThread, bpThread);
                            }
                } finally {
                    await DetachProcessAsync(proc);
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #22
0
 public AD7Thread(AD7Engine engine, PythonThread debuggedThread)
 {
     _engine         = engine;
     _debuggedThread = debuggedThread;
     _vsTid          = engine.RegisterThreadId(debuggedThread.Id);
 }
コード例 #23
0
        public override async Task <ExecutionResult> InitializeAsync()
        {
            var result = await base.InitializeAsync();

            if (!result.IsSuccessful)
            {
                return(result);
            }

            result = await _serviceProvider.GetUIThread().InvokeTask(async() => {
                try {
                    UpdatePropertiesFromProjectMoniker();
                } catch (NoInterpretersException ex) {
                    WriteError(ex.ToString());
                } catch (MissingInterpreterException ex) {
                    WriteError(ex.ToString());
                } catch (DirectoryNotFoundException ex) {
                    WriteError(ex.ToString());
                } catch (Exception ex) when(!ex.IsCriticalException())
                {
                    WriteError(ex.ToUnhandledExceptionMessage(GetType()));
                }

                var remoteProcess = _process as PythonRemoteProcess;

                try {
                    _serviceProvider.GetPythonToolsService().Logger.LogEvent(Logging.PythonLogEvent.DebugRepl, new Logging.DebugReplInfo {
                        RemoteProcess = remoteProcess != null,
                        Version       = _process.LanguageVersion.ToVersion().ToString()
                    });
                } catch (Exception ex) {
                    Debug.Fail(ex.ToUnhandledExceptionMessage(GetType()));
                }

                _process.ModulesChanged += OnModulesChanged;

                var threads = _process.GetThreads();
                PythonThread activeThread = null;

                var dte = _serviceProvider.GetDTE();
                if (dte != null)
                {
                    // If we are broken into the debugger, let's set the debug REPL active thread
                    // to be the one that is active in the debugger
                    var dteDebugger = dte.Debugger;
                    if (dteDebugger.CurrentMode == EnvDTE.dbgDebugMode.dbgBreakMode &&
                        dteDebugger.CurrentProcess != null &&
                        dteDebugger.CurrentThread != null)
                    {
                        if (_process.Id == dteDebugger.CurrentProcess.ProcessID)
                        {
                            var activeThreadId = _threadIdMapper.GetPythonThreadId((uint)dteDebugger.CurrentThread.ID);
                            activeThread       = threads.SingleOrDefault(t => t.Id == activeThreadId);
                        }
                    }
                }

                if (activeThread == null)
                {
                    activeThread = threads.Count > 0 ? threads[0] : null;
                }

                if (activeThread != null)
                {
                    SwitchThread(activeThread, false);
                }

                return(ExecutionResult.Success);
            });

            return(result);
        }
コード例 #24
0
        internal async Task StepTestAsync(string filename, string breakFile, string arguments, int[] breakLines, Action <PythonProcess>[] breakAction, Action processLoaded, PythonDebugOptions options = PythonDebugOptions.RedirectOutput, bool waitForExit = true, params ExpectedStep[] kinds)
        {
            Console.WriteLine("--- Begin Step Test ---");
            var debugger = new PythonDebugger();

            if (breakFile == null)
            {
                breakFile = filename;
            }

            string fullPath = Path.GetFullPath(filename);
            string dir      = Path.GetDirectoryName(filename);
            var    process  = debugger.CreateProcess(Version.Version, Version.InterpreterPath, "\"" + fullPath + "\" " + (arguments ?? ""), dir, "", null, options, DebugLog);

            try {
                PythonThread thread = null;
                process.ThreadCreated += (sender, args) => {
                    thread = args.Thread;
                };


                AutoResetEvent processEvent = new AutoResetEvent(false);

                bool processLoad = false, stepComplete = false;
                process.ProcessLoaded += async(sender, args) => {
                    foreach (var breakLine in breakLines)
                    {
                        var bp = process.AddBreakpointByFileExtension(breakLine, breakFile);
                        await bp.AddAsync(TimeoutToken());
                    }

                    processLoad = true;
                    processEvent.Set();
                    processLoaded?.Invoke();
                };

                process.StepComplete += (sender, args) => {
                    stepComplete = true;
                    processEvent.Set();
                };

                int breakHits             = 0;
                ExceptionDispatchInfo edi = null;
                process.BreakpointHit += (sender, args) => {
                    try {
                        Console.WriteLine("Breakpoint hit");
                        if (breakAction != null)
                        {
                            if (breakHits >= breakAction.Length)
                            {
                                Assert.Fail("Unexpected breakpoint hit at {0}:{1}", args.Breakpoint.Filename, args.Breakpoint.LineNo);
                            }
                            breakAction[breakHits++](process);
                        }
                        stepComplete = true;
                        processEvent.Set();
                    } catch (Exception ex) {
                        edi = ExceptionDispatchInfo.Capture(ex);
                        try {
                            processEvent.Set();
                        } catch { }
                    }
                };

                await process.StartAsync();

                for (int curStep = 0; curStep < kinds.Length; curStep++)
                {
                    Console.WriteLine("Step {0} {1}", curStep, kinds[curStep].Kind);
                    // process the stepping events as they occur, we cannot callback during the
                    // event because the notificaiton happens on the debugger thread and we
                    // need to callback to get the frames.
                    AssertWaited(processEvent);
                    edi?.Throw();

                    // first time through we hit process load, each additional time we should hit step complete.
                    Debug.Assert((processLoad == true && stepComplete == false && curStep == 0) ||
                                 (stepComplete == true && processLoad == false && curStep != 0));

                    processLoad = stepComplete = false;

                    var frames   = thread.Frames;
                    var stepInfo = kinds[curStep];
                    Assert.AreEqual(stepInfo.StartLine, frames[0].LineNo, String.Format("{0} != {1} on {2} step", stepInfo.StartLine, frames[0].LineNo, curStep));

                    switch (stepInfo.Kind)
                    {
                    case StepKind.Into:
                        await thread.StepIntoAsync(TimeoutToken());

                        break;

                    case StepKind.Out:
                        await thread.StepOutAsync(TimeoutToken());

                        break;

                    case StepKind.Over:
                        await thread.StepOverAsync(TimeoutToken());

                        break;

                    case StepKind.Resume:
                        await process.ResumeAsync(TimeoutToken());

                        break;
                    }
                }
                if (waitForExit)
                {
                    WaitForExit(process);
                }
            } finally {
                process.Terminate();
            }
        }
コード例 #25
0
    // Update is called once per frame
    void Update()
    {
        if (_start)
        {
            if (_moveImage.GetComponent <MoveImage>().HasFinishedMovement())
            {
                if (_change)
                {
                    if (!_resetText)
                    {
                        this.transform.GetChild(0).GetChild(1).GetComponent <UnityEngine.UI.Text>().text = _text;
                        _resetText = true;
                    }
                    Color tmp2 = _internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color;
                    tmp2.a += 0.08f;
                    _internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color = tmp2;
                    if (_internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color.a > 1.5f)
                    {
                        _start = false;
                        if (!_coroutine && _fileToGen != "")
                        {
                            if (_commands.Count > 1)
                            {
                                PythonThread.ExecuteCommand(_commands[1]);
                                //PythonThread.ExecuteCommand("/C python GenerateNGrams.py ..\\NGramsTraining\\20200528_112114_Training_Ngrams.pkl 250 ..\\Maps\\25.csv");
                            }
                            StartCoroutine(StartGenCoroutine(_fileToGen));
                            _coroutine = false;
                        }
                    }

                    /*if (!_resetText && _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color.a > 0)
                     * {
                     *  Color tmp = _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color;
                     *  tmp.a -= 0.01f;
                     *  _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color = tmp;
                     *  Debug.Log("ALPHA: " + _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color.a);
                     * }
                     * if (!_resetText && _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color.a <= 0)
                     * {
                     *  _start = false;
                     *  Color tmp2 = _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color;
                     *  tmp2.a = 0f;
                     *  _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color = tmp2;
                     *  _resetText = true;
                     *  SetLoadText(_text);
                     * }
                     * else
                     * {
                     *  Color tmp2 = _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color;
                     *  tmp2.a += 0.01f;
                     *  _internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color = tmp2;
                     *  if (_internalImage.transform.GetChild(1).GetComponentInChildren<UnityEngine.UI.Text>().color.a > 1.5f && _fileToGen != "")
                     * }*/
                }
                else
                {
                    // Debug.Log("AA");
                    Color tmp = _internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color;
                    tmp.a -= 0.01f;
                    _internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color = tmp;
                    if (tmp.a <= 0)
                    {
                        _start = false;
                        _internalImage.gameObject.SetActive(false);
                    }
                }
            }
            else
            {
                Color tmp = _internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color;
                tmp.a -= 0.01f;
                _internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color = tmp;
                if (_internalImage.transform.GetChild(1).GetComponentInChildren <UnityEngine.UI.Text>().color.a <= 0)
                {
                }
            }
        }
        //Debug.Log("START: " + _start + "       CHANGESCENE: " + _canChangeScene);
        if (!_start && _canChangeScene && _moveImage.GetComponent <MoveImage>().CanStartLoading())
        {
            GameManager.GM.ChangeScene(GameManager.SceneFlow.NEXT);
            _canChangeScene = false;
            //GameCamera.Instance.GoToBlueScreen();
        }
    }
コード例 #26
0
            public async Task RunAsync()
            {
                string runFileName = RunFileName;

                if (!Path.IsPathRooted(runFileName))
                {
                    runFileName = _tests.DebuggerTestPath + runFileName;
                }

                string breakFileName = BreakFileName;

                if (breakFileName != null && !Path.IsPathRooted(breakFileName))
                {
                    breakFileName = _tests.DebuggerTestPath + breakFileName;
                }

                foreach (var bp in Breakpoints)
                {
                    var fileName = bp.FileName ?? breakFileName ?? runFileName;
                    if (fileName.EndsWith(".py"))
                    {
                        Assert.IsTrue(bp is Breakpoint);
                    }
                    else
                    {
                        Assert.IsTrue(bp is DjangoBreakpoint);
                    }
                }

                var bps                  = new Dictionary <PythonBreakpoint, BreakpointBase>();
                var unboundBps           = new HashSet <Breakpoint>();
                var breakpointsToBeBound = Breakpoints.Count;

                var          debugger = new PythonDebugger();
                PythonThread thread   = null;

                // Used to signal exceptions from debugger event handlers that run on a background thread.
                var backgroundException = new TaskCompletionSource <bool>();

                var processLoaded = new TaskCompletionSource <bool>();
                var process       = _tests.DebugProcess(
                    debugger,
                    runFileName,
                    cwd: WorkingDirectory,
                    arguments: Arguments,
                    resumeOnProcessLoaded: false,
                    onLoaded: async(newproc, newthread) => {
                    try {
                        foreach (var bp in Breakpoints)
                        {
                            var fileName = bp.FileName ?? breakFileName ?? runFileName;

                            PythonBreakpoint breakpoint;
                            var pyBP = bp as Breakpoint;
                            if (pyBP != null)
                            {
                                breakpoint = newproc.AddBreakpoint(fileName, pyBP.LineNumber, pyBP.ConditionKind, pyBP.Condition, pyBP.PassCountKind, pyBP.PassCount);
                                unboundBps.Add(pyBP);
                            }
                            else
                            {
                                var djangoBP = bp as DjangoBreakpoint;
                                if (djangoBP != null)
                                {
                                    breakpoint = newproc.AddDjangoBreakpoint(fileName, djangoBP.LineNumber);
                                    // Django breakpoints are never bound.
                                    --breakpointsToBeBound;
                                }
                                else
                                {
                                    Assert.Fail("Unknown breakpoint type.");
                                    return;
                                }
                            }

                            // Bind failed and succeeded events expect to find the breakpoint
                            // in the dictionary, so update it before sending the add request.
                            bps.Add(breakpoint, bp);
                            await breakpoint.AddAsync(TimeoutToken());
                        }

                        OnProcessLoaded?.Invoke(newproc);

                        thread = newthread;
                        processLoaded.SetResult(true);
                    } catch (Exception ex) {
                        backgroundException.TrySetException(ex);
                    }
                },
                    interpreterOptions: InterpreterOptions
                    );

                int breakpointsBound    = 0;
                int breakpointsNotBound = 0;
                int nextExpectedHit     = 0;

                var allBreakpointsHit        = new TaskCompletionSource <bool>();
                var allBreakpointBindResults = new TaskCompletionSource <bool>();

                if (breakpointsToBeBound == 0)
                {
                    allBreakpointBindResults.SetResult(true);
                }

                try {
                    process.BreakpointBindFailed += (sender, args) => {
                        try {
                            var bp = (Breakpoint)bps[args.Breakpoint];
                            if (bp != null && !(bp.IsBindFailureExpected ?? IsBindFailureExpected))
                            {
                                Assert.Fail("Breakpoint at {0}:{1} failed to bind.", bp.FileName ?? breakFileName ?? runFileName, bp.LineNumber);
                            }
                            ++breakpointsNotBound;
                            if (breakpointsBound + breakpointsNotBound == breakpointsToBeBound)
                            {
                                allBreakpointBindResults.SetResult(true);
                            }
                        } catch (Exception ex) {
                            backgroundException.TrySetException(ex);
                        }
                    };

                    process.BreakpointBindSucceeded += (sender, args) => {
                        try {
                            var bp = (Breakpoint)bps[args.Breakpoint];
                            Assert.AreEqual(bp.FileName ?? breakFileName ?? runFileName, args.Breakpoint.Filename);
                            Assert.IsTrue(unboundBps.Remove(bp));
                            ++breakpointsBound;
                            if (breakpointsBound + breakpointsNotBound == breakpointsToBeBound)
                            {
                                allBreakpointBindResults.SetResult(true);
                            }
                        } catch (Exception ex) {
                            backgroundException.TrySetException(ex);
                        }
                    };

                    process.BreakpointHit += async(sender, args) => {
                        try {
                            if (nextExpectedHit < ExpectedHits.Count)
                            {
                                var bp = Breakpoints[ExpectedHits[nextExpectedHit]];
                                Trace.TraceInformation("Hit {0}:{1}", args.Breakpoint.Filename, args.Breakpoint.LineNo);
                                Assert.AreSame(bp, bps[args.Breakpoint]);

                                if (bp.RemoveWhenHit)
                                {
                                    await args.Breakpoint.RemoveAsync(TimeoutToken());
                                }

                                if (bp.ExpectHitOnMainThread ?? ExpectHitOnMainThread)
                                {
                                    Assert.AreSame(thread, args.Thread);
                                }

                                bp.OnHit?.Invoke(args);

                                if (++nextExpectedHit == ExpectedHits.Count)
                                {
                                    allBreakpointsHit.SetResult(true);
                                }
                            }

                            try {
                                await process.ResumeAsync(TimeoutToken());
                            } catch (TaskCanceledException) {
                                // If we don't wait for exit, the Terminate() call
                                // will cause ResumeAsync to be canceled.
                                if (WaitForExit)
                                {
                                    throw;
                                }
                            }
                        } catch (Exception ex) {
                            backgroundException.TrySetException(ex);
                        }
                    };

                    await process.StartAsync();

                    Assert.IsTrue(WaitForAny(10000, processLoaded.Task, backgroundException.Task), "Timed out waiting for process load");

                    await process.AutoResumeThread(thread.Id, TimeoutToken());

                    if (breakpointsToBeBound > 0)
                    {
                        Assert.IsTrue(WaitForAny(10000, allBreakpointBindResults.Task, backgroundException.Task), "Timed out waiting for breakpoints to bind");
                    }
                } finally {
                    if (WaitForExit)
                    {
                        _tests.WaitForExit(process);
                    }
                    else
                    {
                        Assert.IsTrue(WaitForAny(20000, allBreakpointsHit.Task, backgroundException.Task), "Timed out waiting for breakpoints to hit");
                        process.Terminate();
                    }
                }

                if (backgroundException.Task.IsFaulted)
                {
                    backgroundException.Task.GetAwaiter().GetResult();
                }

                Assert.AreEqual(ExpectedHits.Count, nextExpectedHit);
                Assert.IsTrue(unboundBps.All(bp => bp.IsBindFailureExpected ?? IsBindFailureExpected));
            }
コード例 #27
0
    /// <summary>
    /// Button method to continue to the next step of the input.
    /// If load mode goes to the file selected.
    /// If generation mode, calls the python thread to execute the command with the
    /// proper variables
    /// </summary>
    public void OnClickContinue()
    {
        string text = " ";

        if (_generationMode)
        {
            string fileToGen = "";
            if (_mlMode)
            {
                fileToGen = _fileNameNGrams;
                text      = "GENERATING NGRAMS";
            }
            else
            {
                fileToGen = _fileNameRNN;
                text      = "GENERATING RNN";
            }
            if (arrFiles.Count > 0)
            {
                if (_gOp != GenerateOptions.TRAIN)
                {
                    LoadScene.Instance.ActiveLoadObject();
                    SendCommand();
                    LoadScene.Instance.GetCommands(_commands);
                    LoadScene.Instance.SetLoadText(text);
                    LoadScene.Instance.StartFadeIn("SampleScene", fileToGen);
                }
                else
                {
                    int    nFiles = GetFilesSelectedLength();
                    string concat = "";

                    for (int i = 0; i < nFiles; i++)
                    {
                        string file    = GetFilesToConcatInput()[i];
                        string sufix   = ".csv";
                        string concatS = "..\\Maps\\" + file + sufix;
                        concat = concat + " " + concatS;
                    }

                    string interpolation = "0";
                    if (_makeInterpolation)
                    {
                        interpolation = "1 " + _interpolationProbs;
                    }

                    string debug     = "";
                    bool   debugMode = GetCheckBoxActive();
                    if (debugMode)
                    {
                        debug = " -d";
                    }
                    if (_mlMode)
                    {
                        PythonThread.ExecuteCommand("/C python TrainNGrams.py " + nFiles.ToString() + concat + " " + _nGramsInput + " " + interpolation + " " + debug);
                    }
                    else
                    {
                        string concatLayers = "";
                        for (int i = 0; i < _layersArr.Count; i++)
                        {
                            string file    = _layersArr[i];
                            string concatS = file;
                            concatLayers = concatLayers + " " + concatS;
                        }
                        PythonThread.ExecuteCommand("/C python TrainNN_2.py " + nFiles.ToString() + concat /*ESTO ES PARA LO DE MEZCLA DE ARCHIVOS nFiles.ToString() + concat +*/ + " " + _seqLengthInput + " " + _bufferSizeInput + " " + _embedDimInput + " " + _nnUnitsInput + " " + _epochsInput + " "
                                                    + _layersArr.Count + " " + concatLayers + " " + _temperatureInput + " " + debug);
                    }
                    GameManager.GM.ChangeScene(GameManager.SceneFlow.CURRENT);
                }
            }
        }
        else
        {
            if (arrFiles.Count == 1)
            {
                text = "MAP: " + arrFiles[0];
                //PythonThread.ExecuteCommand();
                LoadScene.Instance.ActiveLoadObject();
                LoadScene.Instance.SetLoadText(text);
                LoadScene.Instance.StartFadeIn("SampleScene", "");
                MapReader.GM.InitMap(arrFiles[0], false);
                LoadScene.Instance.ChangeScene();
            }
        }
    }
コード例 #28
0
        internal IList <PythonStackFrame> GetFrames()
        {
            PythonThread activeThread = _process.GetThreads().SingleOrDefault(t => t.Id == _threadId);

            return(activeThread != null ? activeThread.Frames : new List <PythonStackFrame>());
        }
コード例 #29
0
ファイル: ThreadEventArgs.cs プロジェクト: PeezoSlug/PTVS
 public ThreadEventArgs(PythonThread thread)
 {
     _thread = thread;
 }
コード例 #30
0
 public BreakpointHitEventArgs(PythonBreakpoint breakpoint, PythonThread thread)
 {
     _breakpoint = breakpoint;
     _thread     = thread;
 }
コード例 #31
0
ファイル: AD7Thread.cs プロジェクト: wenh123/PTVS
 public AD7Thread(AD7Engine engine, PythonThread debuggedThread) {
     _engine = engine;
     _debuggedThread = debuggedThread;
     _vsTid = engine.RegisterThreadId(debuggedThread.Id);
 }