コード例 #1
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);
            }
        }
コード例 #2
0
ファイル: DebugReplEvaluatorTests.cs プロジェクト: xNUTs/PTVS
        public void ChangeProcess()
        {
            Attach("DebugReplTest4A.py", 3);
            Attach("DebugReplTest4B.py", 3);

            PythonProcess proc1 = _processes[0];
            PythonProcess proc2 = _processes[1];

            // We are broken in process 2 (the last one attached is the current one)
            string text;

            text = ExecuteCommand(new DebugReplProcessesCommand(), "");
            Assert.AreEqual(String.Format(@"   Process id={0}, Language version={2}
=> Process id={1}, Language version={2}", proc1.Id, proc2.Id, Version.Version), text);

            // Switch to process 1
            Assert.AreEqual(String.Format("Current process changed to {0}", proc1.Id), ExecuteCommand(new DebugReplProcessCommand(), proc1.Id.ToString()));
            Assert.AreEqual(String.Format("{0}", proc1.Id), ExecuteCommand(new DebugReplProcessCommand(), String.Empty));
            Assert.AreEqual("'hello'", ExecuteText("a1"));
            Assert.AreEqual("30", ExecuteText("b1"));

            // Switch to process 2
            Assert.AreEqual(String.Format("Current process changed to {0}", proc2.Id), ExecuteCommand(new DebugReplProcessCommand(), proc2.Id.ToString()));
            Assert.AreEqual(String.Format("{0}", proc2.Id), ExecuteCommand(new DebugReplProcessCommand(), String.Empty));
            Assert.AreEqual("'world'", ExecuteText("a2"));
            Assert.AreEqual("60", ExecuteText("b2"));
        }
コード例 #3
0
        public virtual async Task AttachSingleThreadedSleeper()
        {
            // http://pytools.codeplex.com/discussions/285741 1/12/2012 6:20 PM
            Process p = Process.Start(Version.InterpreterPath, "\"" + TestData.GetPath(@"TestData\DebuggerProject\AttachSingleThreadedSleeper.py") + "\"");

            try {
                Thread.Sleep(1000);

                var attached = new TaskCompletionSource <bool>();

                var proc = PythonProcess.Attach(p.Id);
                try {
                    proc.ProcessLoaded += (sender, args) => {
                        attached.SetResult(true);
                    };
                    await proc.StartListeningAsync();

                    using (var dumpWriter = new MiniDumpWriter(p)) {
                        await attached.Task.WithTimeout(10000, "Failed to attach within 10s");

                        await proc.ResumeAsync(TimeoutToken());

                        Debug.WriteLine("Waiting for exit");
                        dumpWriter.Cancel();
                    }
                } finally {
                    TerminateProcess(proc);
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #4
0
        public void DateXDecimalYTest()
        {
            var xyPair1 = new XyPair <DateTime, decimal>("Some Legend A");

            xyPair1.AddX(new DateTime(2018, 11, 20));
            xyPair1.AddX(new DateTime(2018, 11, 21));
            xyPair1.AddX(new DateTime(2018, 11, 22));
            xyPair1.AddY(0.18m);
            xyPair1.AddY(0.32m);
            xyPair1.AddY(0.21m);
            xyPair1.HasScatter = true;

            IPythonProcess pythonProcess = new PythonProcess(PythonResources.GetPythonPath());
            ITitle         title         = new Title("This is a title", 16);
            IPlotColor     colors        = new PlotColor();

            colors.OutsideColor = "#979899";
            colors.InsideColor  = "#d1d1d1";

            IDesign <DateTime, decimal>          design       = new Design <DateTime, decimal>(title, colors, true, 0.5m, "&", "cccc", "dddd");
            ITickComposer <DateTime, decimal>    tickComposer = new TickComposer <DateTime, decimal>(false, pythonProcess);
            IGeneralComposer <DateTime, decimal> composer     = new GeneralComposer <DateTime, decimal>(pythonProcess, tickComposer);
            IPlotV3 <DateTime, decimal>          plot         = new PlotV3 <DateTime, decimal>(pythonProcess, design, composer);

            plot.AddSource(xyPair1);
            plot.Show();
        }
コード例 #5
0
ファイル: AttachTests.cs プロジェクト: zhangjinxing/PTVS
        public virtual async Task AttachSingleThreadedSleeper()
        {
            // http://pytools.codeplex.com/discussions/285741 1/12/2012 6:20 PM
            Process p = Process.Start(Version.InterpreterPath, "\"" + TestData.GetPath(@"TestData\DebuggerProject\AttachSingleThreadedSleeper.py") + "\"");

            try {
                Thread.Sleep(1000);

                AutoResetEvent attached = new AutoResetEvent(false);

                var proc = PythonProcess.Attach(p.Id);
                try {
                    proc.ProcessLoaded += (sender, args) => {
                        attached.Set();
                    };
                    await proc.StartListeningAsync();

                    Assert.IsTrue(attached.WaitOne(10000), "Failed to attach within 10s");
                    await proc.ResumeAsync(TimeoutToken());

                    Debug.WriteLine("Waiting for exit");
                } finally {
                    TerminateProcess(proc);
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #6
0
        public void DateTimeXPlot()
        {
            var xyPair1 = new XyPair <DateTime, long>("AAAAA");

            xyPair1.AddX(new DateTime(2018, 11, 20, 2, 0, 0));
            xyPair1.AddX(new DateTime(2018, 11, 20, 4, 0, 0));
            xyPair1.AddX(new DateTime(2018, 11, 20, 8, 0, 0));
            xyPair1.AddY(18);
            xyPair1.AddY(32);
            xyPair1.AddY(21);
            xyPair1.HasScatter = true;

            IPythonProcess pythonProcess = new PythonProcess(PythonResources.GetPythonPath());
            ITitle         title         = new Title("BBBBB", 32);
            IPlotColor     colors        = new PlotColor();

            colors.OutsideColor = "#979899";
            colors.InsideColor  = "#d1d1d1";

            IDesign <DateTime, long>          design       = new Design <DateTime, long>(title, colors, true, 16m, "▬", "CCCCC", "DDDDD");
            ITickComposer <DateTime, long>    tickComposer = new TickComposer <DateTime, long>(true, pythonProcess);
            IGeneralComposer <DateTime, long> composer     = new GeneralComposer <DateTime, long>(pythonProcess, tickComposer);
            IPlotV3 <DateTime, long>          plot         = new PlotV3 <DateTime, long>(pythonProcess, design, composer);

            plot.AddSource(xyPair1);
            plot.Show();
        }
コード例 #7
0
ファイル: AttachTests.cs プロジェクト: zhangjinxing/PTVS
        public virtual async Task AttachReattachInfiniteThreads()
        {
            Process p = Process.Start(Version.InterpreterPath, "\"" + TestData.GetPath(@"TestData\DebuggerProject\InfiniteThreads.py") + "\"");

            try {
                Thread.Sleep(1000);

                AutoResetEvent attached = new AutoResetEvent(false);
                AutoResetEvent detached = new AutoResetEvent(false);
                for (int i = 0; i < 10; i++)
                {
                    Console.WriteLine(i);

                    var proc = PythonProcess.Attach(p.Id);

                    proc.ProcessLoaded += (sender, args) => {
                        attached.Set();
                    };
                    proc.ProcessExited += (sender, args) => {
                        detached.Set();
                    };
                    await proc.StartListeningAsync();

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

                    Assert.IsTrue(detached.WaitOne(30000), "Failed to detach within 30s");
                }
            } finally {
                DisposeProcess(p);
            }
        }
コード例 #8
0
        public void DateXDLongYTest()
        {
            var xyPair1 = new XyPair <DateTime, long>("Some Legend A");

            xyPair1.AddX(new DateTime(2018, 11, 20));
            xyPair1.AddX(new DateTime(2018, 11, 21));
            xyPair1.AddX(new DateTime(2018, 11, 22));
            xyPair1.AddY(18);
            xyPair1.AddY(32);
            xyPair1.AddY(21);
            xyPair1.HasScatter = true;

            IPythonProcess pythonProcess = new PythonProcess(PythonResources.GetPythonPath());
            ITitle         title         = new Title("This is a title", 32);
            IPlotColor     colors        = new PlotColor();

            colors.OutsideColor = "#979899";
            colors.InsideColor  = "#d1d1d1";

            IDesign <DateTime, long>          design       = new Design <DateTime, long>(title, colors, true, 16m, "▬", "aaa", "bbbb");
            ITickComposer <DateTime, long>    tickComposer = new TickComposer <DateTime, long>(false, pythonProcess);
            IGeneralComposer <DateTime, long> composer     = new GeneralComposer <DateTime, long>(pythonProcess, tickComposer);
            IPlotV3 <DateTime, long>          plot         = new PlotV3 <DateTime, long>(pythonProcess, design, composer);

            plot.AddSource(xyPair1);
            plot.Show();
        }
コード例 #9
0
 /// <summary>
 /// Creates a PythonObject for an expression which raised an exception instead of returning a value.
 /// </summary>
 public PythonEvaluationResult(PythonProcess process, string exceptionText, string expression, PythonStackFrame frame)
 {
     _process       = process;
     _expression    = expression;
     _frame         = frame;
     _exceptionText = exceptionText;
 }
コード例 #10
0
ファイル: PythonThread.cs プロジェクト: PeezoSlug/PTVS
 internal PythonThread(PythonProcess process, long identity, bool isWorkerThread)
 {
     _process        = process;
     _identity       = identity;
     _isWorkerThread = isWorkerThread;
     _name           = "";
 }
コード例 #11
0
ファイル: PythonBreakpoint.cs プロジェクト: PeezoSlug/PTVS
        public PythonBreakpoint(
            PythonProcess process,
            string filename,
            int lineNo,
            PythonBreakpointConditionKind conditionKind,
            string condition,
            PythonBreakpointPassCountKind passCountKind,
            int passCount,
            int breakpointId,
            bool isDjangoBreakpoint = false
            )
        {
            Debug.Assert(conditionKind != PythonBreakpointConditionKind.Always || string.IsNullOrEmpty(condition));
            Debug.Assert(passCountKind != PythonBreakpointPassCountKind.Always || passCount == 0);

            _process            = process;
            _filename           = filename;
            _lineNo             = lineNo;
            _breakpointId       = breakpointId;
            _conditionKind      = conditionKind;
            _condition          = condition;
            _passCountKind      = passCountKind;
            _passCount          = passCount;
            _isDjangoBreakpoint = isDjangoBreakpoint;
        }
コード例 #12
0
ファイル: Program.cs プロジェクト: OH1TR/RenderTrainer
        static void Main(string[] args)
        {
            string        workDir         = ConfigurationManager.AppSettings["WorkDir"];
            string        tensorBoardLogs = Path.Combine(workDir, "Logs");
            List <string> index           = new List <string>();

            Directory.SetCurrentDirectory("..\\..\\..\\PythonScripts");

            if (!Directory.Exists(tensorBoardLogs))
            {
                Directory.CreateDirectory(tensorBoardLogs);
            }

            PythonProcess blender = new PythonProcess("Blender");

            blender.EnsureCreated();
            blender.Connect();

            PythonProcess Tf = new PythonProcess("Tf");

            Tf.EnsureCreated();
            Tf.Connect();


            string dir = Directory.GetFiles(workDir, "Checkpoint_*").Where(i => !i.Contains("epoch")).OrderByDescending(i => i).FirstOrDefault();

            if (dir != null)
            {
                Tf.Call("load", dir);
                //Tf.Call("dump", "c:\\temp\\b.txt");
            }
            else
            {
                Tf.Call("build_model");
            }



            Tf.Call("enable_tensorboard", tensorBoardLogs);

            while (true)
            {
                for (int i = 0; i < 60; i++)
                {
                    string filePath = Path.Combine(workDir, "image" + i + ".jpg");
                    blender.Call("renderImage", filePath, i % 6);
                    index.Add((i % 6) + "|" + filePath);
                }

                string indexFile = Path.Combine(workDir, "index.txt");
                File.WriteAllLines(indexFile, index.ToArray());

                Tf.Call("loadDataset", indexFile);

                Tf.Call("fit");
                //Tf.Call("evaluate");
                Tf.Call("save", Path.Combine(workDir, "Checkpoint_" + DateTime.Now.ToString("yyyyMMdd_HHmmss")));
                //Tf.Call("dump", "c:\\temp\\a.txt");
            }
        }
コード例 #13
0
ファイル: AttachTests.cs プロジェクト: zhangjinxing/PTVS
        private async Task AttachTestTimeoutAsync(string hostCode)
        {
            var exe = CompileCode(hostCode);

            // start the test process w/ our handle
            var              eventName = Guid.NewGuid().ToString();
            EventWaitHandle  handle    = new EventWaitHandle(false, EventResetMode.AutoReset, eventName);
            ProcessStartInfo psi       = new ProcessStartInfo(exe, eventName);

            psi.UseShellExecute       = false;
            psi.RedirectStandardError = psi.RedirectStandardOutput = true;
            psi.CreateNoWindow        = true;
            // Add Python to PATH so that the host can locate the DLL in case it's not in \Windows\System32 (e.g. for EPD)
            psi.EnvironmentVariables["PATH"] = Environment.GetEnvironmentVariable("PATH") + ";" + Path.GetDirectoryName(Version.InterpreterPath);

            Process p       = Process.Start(psi);
            var     outRecv = new OutputReceiver();

            p.OutputDataReceived += outRecv.OutputDataReceived;
            p.ErrorDataReceived  += outRecv.OutputDataReceived;
            p.BeginErrorReadLine();
            p.BeginOutputReadLine();

            try {
                bool isAttached = false;

                // start the attach with the GIL held
                AutoResetEvent attachStarted = new AutoResetEvent(false);
                AutoResetEvent attachDone    = new AutoResetEvent(false);

                // We run the Attach and StartListeningAsync on a separate thread,
                // because StartListeningAsync waits until debuggee has connected
                // back (which it won't do until handle is set).
                var task = Task.Run(async() =>
                {
                    var proc = PythonProcess.Attach(p.Id);
                    try {
                        proc.ProcessLoaded += (sender, args) => {
                            attachDone.Set();
                            isAttached = false;
                        };

                        attachStarted.Set();
                        await proc.StartListeningAsync(10000);
                    } finally {
                        await DetachProcessAsync(proc);
                    }
                });

                Assert.IsTrue(attachStarted.WaitOne(10000), "Failed to start attaching within 10s");
                Assert.IsFalse(isAttached, "should not have attached yet"); // we should be blocked
                handle.Set();                                               // let the code start running

                Assert.IsTrue(attachDone.WaitOne(10000), "Failed to attach within 10s");
            } finally {
                Debug.WriteLine(String.Format("Process output: {0}", outRecv.Output.ToString()));
                DisposeProcess(p);
            }
        }
コード例 #14
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();
                    }
                }
            }
コード例 #15
0
 internal async Task DetachProcessAsync(PythonProcess p)
 {
     try {
         await p.DetachAsync(TimeoutToken());
     } catch (Exception ex) {
         Console.WriteLine("Failed to detach process");
         Console.WriteLine(ex);
     }
 }
コード例 #16
0
 public PythonBreakpoint(PythonProcess process, string filename, int lineNo, string condition, bool breakWhenChanged, int breakpointId)
 {
     _process          = process;
     _filename         = filename;
     _lineNo           = lineNo;
     _breakpointId     = breakpointId;
     _condition        = condition;
     _breakWhenChanged = breakWhenChanged;
 }
コード例 #17
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);
            }
        }
コード例 #18
0
 internal void TerminateProcess(PythonProcess p)
 {
     try {
         p.Terminate();
     } catch (Exception ex) {
         Console.WriteLine("Failed to detach process");
         Console.WriteLine(ex);
     }
 }
コード例 #19
0
        private async Task AttachAsync(string filename, int lineNo)
        {
            var           debugger = new PythonDebugger();
            PythonProcess process  = debugger.DebugProcess(Version, DebuggerTestPath + filename, null, async(newproc, newthread) => {
                var breakPoint = newproc.AddBreakpointByFileExtension(lineNo, filename);
                await breakPoint.AddAsync(TimeoutToken());
            });

            _processes.Add(process);

            long?threadAtBreakpoint = null;

            using (var brkHit = new AutoResetEvent(false))
                using (var procExited = new AutoResetEvent(false)) {
                    EventHandler <BreakpointHitEventArgs> breakpointHitHandler = (s, e) => {
                        threadAtBreakpoint = e.Thread.Id;
                        SafeSetEvent(brkHit);
                    };
                    EventHandler <ProcessExitedEventArgs> processExitedHandler = (s, e) => SafeSetEvent(procExited);
                    process.BreakpointHit += breakpointHitHandler;
                    process.ProcessExited += processExitedHandler;

                    try {
                        await process.StartAsync();
                    } catch (Win32Exception ex) {
                        _processes.Remove(process);
                        if (ex.HResult == -2147467259 /*0x80004005*/)
                        {
                            Assert.Inconclusive("Required Python interpreter is not installed");
                        }
                        else
                        {
                            Assert.Fail("Process start failed:\r\n" + ex.ToString());
                        }
                    }

                    var handles = new[] { brkHit, procExited };
                    if (WaitHandle.WaitAny(handles, 25000) != 0)
                    {
                        Assert.Fail("Failed to wait on event");
                    }

                    process.BreakpointHit -= breakpointHitHandler;
                    process.ProcessExited -= processExitedHandler;
                }

            await _evaluator.AttachProcessAsync(process, new MockThreadIdMapper());

            // AttachProcessAsync calls InitializeAsync which sets the active
            // thread by using the DTE (which is null in these tests), so we
            // adjust it to the correct thread where breakpoint was hit.
            if (threadAtBreakpoint != null)
            {
                _evaluator.ChangeActiveThread(threadAtBreakpoint.Value, false);
            }
        }
コード例 #20
0
 void Update()
 {
     if (pro != null)
     {
         if (pro.Update())
         {
             pro = null;
         }
     }
 }
コード例 #21
0
        public PythonDebugProcessReplEvaluator(IServiceProvider serviceProvider, PythonProcess process, PythonToolsService pyService, IThreadIdMapper threadIdMapper)
            : base(serviceProvider, pyService, GetOptions(serviceProvider, pyService))
        {
            _process         = process;
            _threadIdMapper  = threadIdMapper;
            _threadId        = process.GetThreads()[0].Id;
            _languageVersion = process.LanguageVersion;

            EnsureConnected();
        }
コード例 #22
0
 public PythonDebugProcessReplEvaluator(IServiceProvider serviceProvider, PythonProcess process, IThreadIdMapper threadIdMapper)
     : base(serviceProvider)
 {
     _process          = process;
     _threadIdMapper   = threadIdMapper;
     _threadId         = process.GetThreads()[0].Id;
     _languageVersion  = process.LanguageVersion;
     _currentScopeName = CurrentFrameScopeFixedName;
     DisplayName       = Strings.DebugReplDisplayName;
 }
コード例 #23
0
        internal static string ReformatStackTrace(PythonProcess process, string data)
        {
            var lines = new Stack <IEnumerable <string> >();
            var sb    = new StringBuilder();

            using (var reader = new StringReader(data))
            {
                for (var line = reader.ReadLine(); line != null; line = reader.ReadLine())
                {
                    lines.Push(SplitReprs(line).ToArray());
                }
            }

            foreach (var line in lines)
            {
                var filename     = line.ElementAtOrDefault(0);
                var lineNumber   = line.ElementAtOrDefault(1);
                var functionName = line.ElementAtOrDefault(2);
                //var text = stackLine.ElementAtOrDefault(3);

                int lineNo;
                if (process != null &&
                    !string.IsNullOrEmpty(filename) &&
                    !string.IsNullOrEmpty(lineNumber) &&
                    int.TryParse(lineNumber, out lineNo) &&
                    !string.IsNullOrEmpty(functionName))
                {
                    functionName = PythonStackFrame.GetQualifiedFunctionName(process, filename, lineNo, functionName);
                }

                if (string.IsNullOrEmpty(functionName))
                {
                    functionName = Strings.DebugUnknownFunctionName;
                }

                if (!string.IsNullOrEmpty(filename))
                {
                    sb.AppendLine(string.IsNullOrEmpty(lineNumber)
                                                ? Strings.DebugPythonExceptionStackTraceFileOnly.FormatUI(filename, functionName)
                                                : Strings.DebugPythonExceptionStackTraceFileAndLineNumber.FormatUI(filename, lineNumber, functionName)
                                  );
                }
                else
                {
                    sb.AppendLine(functionName);
                }
            }

            while (sb.Length > 0 && char.IsWhiteSpace(sb[sb.Length - 1]))
            {
                sb.Length -= 1;
            }

            return(sb.ToString());
        }
コード例 #24
0
        public PythonDebugProcessReplEvaluator(IServiceProvider serviceProvider, PythonProcess process, IThreadIdMapper threadIdMapper)
            : base(serviceProvider)
        {
            _process         = process;
            _threadIdMapper  = threadIdMapper;
            _threadId        = process.GetThreads()[0].Id;
            _languageVersion = process.LanguageVersion;
            DisplayName      = "Debug";

            EnsureConnectedOnCreate();
        }
コード例 #25
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);
            }
        }
コード例 #26
0
 /// <summary>
 /// Creates a PythonObject for an expression which successfully returned a value.
 /// </summary>
 public PythonEvaluationResult(PythonProcess process, string objRepr, string hexRepr, string typeName, long length, string expression, string childName, PythonStackFrame frame, PythonEvaluationResultFlags flags)
 {
     _process    = process;
     _objRepr    = objRepr;
     _hexRepr    = hexRepr;
     _typeName   = typeName;
     _length     = length;
     _expression = expression;
     _childName  = childName;
     _frame      = frame;
     _flags      = flags;
 }
コード例 #27
0
        public static void Init()
        {
            string pythonPath = $"{Entity.Type.Resource.Instance.Directory}/{PythonProgram}";
            string kerasPath  = $"{Entity.Type.Resource.Instance.Directory}/{KerasScript}";

            if (!IsProcessRunning && File.Exists(kerasPath))
            {
                IsProcessRunning = true;

                Server = new TcpListener(IPAddress.Loopback, 0);
                Server.Start();

                int port = ((IPEndPoint)Server.LocalEndpoint).Port;

                Debug.WriteLine($"Running server on port {port}");

                PythonProcess.StartInfo.FileName  = pythonPath;
                PythonProcess.StartInfo.Arguments = $"\"{kerasPath}\" -p {port}";
                PythonProcess.Start();

                DateTime startTime = DateTime.Now;

                while ((DateTime.Now - startTime).Seconds < 10)
                {
                    if (Server.Pending())
                    {
                        Client = Server.AcceptTcpClient();
                        break;
                    }
                    Thread.Sleep(100);
                }

                if (Client == null)
                {
                    throw new SocketException((int)SocketError.TimedOut);
                }

                Input = new StreamWriter(Client.GetStream())
                {
                    AutoFlush = false
                };
                Output = new StreamReader(Client.GetStream());

                ProcessThread = Task
                                .Run(() =>
                {
                    PythonProcess.WaitForExit();
                    Server.Stop();
                })
                                .ContinueWith(t => IsProcessRunning = false);
            }
        }
コード例 #28
0
        internal void WaitForExit(PythonProcess process, bool assert = true)
        {
            bool exited = process.WaitForExit(DefaultWaitForExitTimeout);

            if (!exited)
            {
                process.Terminate();
                if (assert)
                {
                    Assert.Fail("Timeout while waiting for Python process to exit.");
                }
            }
        }
コード例 #29
0
        internal void SwitchProcess(PythonProcess process, bool verbose)
        {
            var newEvaluator = _evaluators[process.Id];

            if (newEvaluator != _activeEvaluator)
            {
                _activeEvaluator = newEvaluator;
                ActiveProcessChanged();
                if (verbose)
                {
                    CurrentWindow.WriteLine(Strings.DebugReplSwitchProcessOutput.FormatUI(process.Id));
                }
            }
        }
コード例 #30
0
        internal void SwitchProcess(PythonProcess process, bool verbose)
        {
            var newEvaluator = _evaluators[process.Id];

            if (newEvaluator != _activeEvaluator)
            {
                _activeEvaluator = newEvaluator;
                ActiveProcessChanged();
                if (verbose)
                {
                    _window.WriteLine(String.Format("Current process changed to {0}", process.Id));
                }
            }
        }
コード例 #31
0
        // Launches a process by means of the debug engine.
        // Normally, Visual Studio launches a program using the IDebugPortEx2::LaunchSuspended method and then attaches the debugger
        // to the suspended program. However, there are circumstances in which the debug engine may need to launch a program
        // (for example, if the debug engine is part of an interpreter and the program being debugged is an interpreted language),
        // in which case Visual Studio uses the IDebugEngineLaunch2::LaunchSuspended method
        // The IDebugEngineLaunch2::ResumeProcess method is called to start the process after the process has been successfully launched in a suspended state.
        int IDebugEngineLaunch2.LaunchSuspended(string pszServer, IDebugPort2 port, string exe, string args, string dir, string env, string options, enum_LAUNCH_FLAGS launchFlags, uint hStdInput, uint hStdOutput, uint hStdError, IDebugEventCallback2 ad7Callback, out IDebugProcess2 process)
        {
            process = null;
            if (_mixedMode) {
                return VSConstants.E_NOTIMPL;
            }

            Debug.WriteLine("--------------------------------------------------------------------------------");
            Debug.WriteLine("PythonEngine LaunchSuspended Begin " + launchFlags + " " + GetHashCode());
            AssertMainThread();
            Debug.Assert(_events == null);
            Debug.Assert(_process == null);
            Debug.Assert(_ad7ProgramId == Guid.Empty);

            _events = ad7Callback;
            _engineCreated = _programCreated = false;
            _loadComplete.Reset();

            if (options != null) {
                ParseOptions(options);
            }

            Send(new AD7CustomEvent(VsPackageMessage.SetDebugOptions, this), AD7CustomEvent.IID, null, null);

            // If this is a windowed application, there's no console to wait on, so disable those flags if they were set.
            if (_debugOptions.HasFlag(PythonDebugOptions.IsWindowsApplication)) {
                _debugOptions &= ~(PythonDebugOptions.WaitOnNormalExit | PythonDebugOptions.WaitOnAbnormalExit);
            }

            Guid processId;
            if (_debugOptions.HasFlag(PythonDebugOptions.AttachRunning)) {
                if (!Guid.TryParse(exe, out processId)) {
                    Debug.Fail("When PythonDebugOptions.AttachRunning is used, the 'exe' parameter must be a debug session GUID.");
                    return VSConstants.E_INVALIDARG;
                }

                _process = DebugConnectionListener.GetProcess(processId);
                _attached = true;
                _pseudoAttach = true;
            } else {
                _process = new PythonProcess(_languageVersion, exe, args, dir, env, _interpreterOptions, _debugOptions, _dirMapping);
            }

            if (!_debugOptions.HasFlag(PythonDebugOptions.AttachRunning)) {
                _process.Start(false);
            }

            AttachEvents(_process);

            AD_PROCESS_ID adProcessId = new AD_PROCESS_ID();
            adProcessId.ProcessIdType = (uint)enum_AD_PROCESS_ID.AD_PROCESS_ID_SYSTEM;
            adProcessId.dwProcessId = (uint)_process.Id;

            EngineUtils.RequireOk(port.GetProcess(adProcessId, out process));
            Debug.WriteLine("PythonEngine LaunchSuspended returning S_OK");
            Debug.Assert(process != null);
            Debug.Assert(!_process.HasExited);

            return VSConstants.S_OK;
        }
コード例 #32
0
        // Called by the SDM to indicate that a synchronous debug event, previously sent by the DE to the SDM,
        // was received and processed.
        int IDebugEngine2.ContinueFromSynchronousEvent(IDebugEvent2 eventObject)
        {
            if (_mixedMode) {
                return VSConstants.E_NOTIMPL;
            }

            AssertMainThread();

            if (eventObject is AD7ProgramDestroyEvent) {
                var debuggedProcess = _process;

                _events = null;
                _process = null;
                _ad7ProgramId = Guid.Empty;
                foreach (var thread in _threads.Values) {
                    thread.Dispose();
                }
                _threads.Clear();
                _modules.Clear();

                debuggedProcess.Dispose();
            } else if (eventObject is AD7CustomEvent) {
            } else {
                Debug.Fail("Unknown synchronous event");
            }

            return VSConstants.S_OK;
        }
コード例 #33
0
        // Attach the debug engine to a program.
        int IDebugEngine2.Attach(IDebugProgram2[] rgpPrograms, IDebugProgramNode2[] rgpProgramNodes, uint celtPrograms, IDebugEventCallback2 ad7Callback, enum_ATTACH_REASON dwReason)
        {
            Debug.WriteLine("PythonEngine Attach Begin " + GetHashCode());

            AssertMainThread();
            Debug.Assert(_ad7ProgramId == Guid.Empty);

            if (celtPrograms != 1) {
                Debug.Fail("Python debugging only supports one program in a process");
                throw new ArgumentException();
            }

            var program = rgpPrograms[0];
            int processId = EngineUtils.GetProcessId(program);
            if (processId == 0) {
                // engine only supports system processes
                Debug.WriteLine("PythonEngine failed to get process id during attach");
                return VSConstants.E_NOTIMPL;
            }

            EngineUtils.RequireOk(program.GetProgramId(out _ad7ProgramId));

            // Attach can either be called to attach to a new process, or to complete an attach
            // to a launched process
            if (_process == null) {
                _events = ad7Callback;

                Send(new AD7CustomEvent(VsPackageMessage.SetDebugOptions, this), AD7CustomEvent.IID, null);

                // Check whether we're debugging Python alongside something else. If so, let Concord handle everything.
                if (!IsDebuggingPythonOnly(program)) {
                    _attached = true;
                    _mixedMode = true;
                    AD7EngineCreateEvent.Send(this);
                    AD7ProgramCreateEvent.Send(this);
                    AD7LoadCompleteEvent.Send(this);
                    Debug.WriteLine("PythonEngine Attach bailing out early - mixed-mode debugging");
                    return VSConstants.S_OK;
                }

                // Check if we're attaching remotely using the Python remote debugging transport
                var remoteProgram = program as PythonRemoteDebugProgram;
                try {
                    if (remoteProgram != null) {
                        var remotePort = remoteProgram.DebugProcess.DebugPort;

                        var uriBuilder = new UriBuilder(remotePort.Uri);
                        string query = uriBuilder.Query ?? "";
                        if (query.Length > 0) {
                            // Strip leading "?" - UriBuilder.Query getter returns it as part of the string, but the setter
                            // will automatically prepend it even if it was already there, producing a malformed query.
                            query = query.Substring(1);
                        }
                        query += "&" + DebugOptionsKey + "=" + _debugOptions;
                        uriBuilder.Query = query;

                        _process = PythonRemoteProcess.Attach(uriBuilder.Uri, true);
                    } else {
                        _process = PythonProcess.Attach(processId, _debugOptions);
                    }
                } catch (ConnectionException ex) {
                    MessageBox.Show("Failed to attach debugger:\r\n" + ex.Message, null, MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return VSConstants.E_FAIL;
                }

                AttachEvents(_process);
                _attached = true;
            } else {
                if (processId != _process.Id) {
                    Debug.Fail("Asked to attach to a process while we are debugging");
                    return VSConstants.E_FAIL;
                }
            }

            AD7EngineCreateEvent.Send(this);

            lock (_syncLock) {
                _engineCreated = true;
                if (_isProgramCreateDelayed) {
                    _isProgramCreateDelayed = false;
                    SendProgramCreate();
                }
            }

            Debug.WriteLine("PythonEngine Attach returning S_OK");
            return VSConstants.S_OK;
        }
コード例 #34
0
        private void AttachEvents(PythonProcess process)
        {
            process.Connected += OnConnected;
            process.ProcessLoaded += OnProcessLoaded;
            process.ModuleLoaded += OnModuleLoaded;
            process.ThreadCreated += OnThreadCreated;

            process.BreakpointBindFailed += OnBreakpointBindFailed;
            process.BreakpointBindSucceeded += OnBreakpointBindSucceeded;

            process.BreakpointHit += OnBreakpointHit;
            process.EntryPointHit += OnEntryPointHit;
            process.AsyncBreakComplete += OnAsyncBreakComplete;
            process.ExceptionRaised += OnExceptionRaised;
            process.ProcessExited += OnProcessExited;
            process.StepComplete += OnStepComplete;
            process.ThreadExited += OnThreadExited;
            process.DebuggerOutput += OnDebuggerOutput;

            process.StartListening();
        }