internal PythonThread(PythonProcess process, long identity, bool isWorkerThread) { _process = process; _identity = identity; _isWorkerThread = isWorkerThread; _name = ""; }
/// <summary> /// Computes the fully qualified function name, including name of the enclosing class for methods, /// and, recursively, names of any outer functions. /// </summary> /// <example> /// Given this code: /// <code> /// class A: /// def b(self): /// def c(): /// class D: /// def e(self): /// pass /// </code> /// And with the current statement being <c>pass</c>, the qualified name is "D.e in c in A.b". /// </example> public static string GetQualifiedFunctionName(PythonProcess process, string filename, int lineNo, string functionName) { var ast = process.GetAst(filename); if (ast == null) { return(functionName); } var walker = new QualifiedFunctionNameWalker(ast, lineNo, functionName); try { ast.Walk(walker); } catch (InvalidDataException) { // Walker ran into a mismatch between expected function name and AST, so we cannot // rely on AST to construct an accurate qualified name. Just return what we have. return(functionName); } string qualName = walker.Name; if (string.IsNullOrEmpty(qualName)) { return(functionName); } return(qualName); }
public static void RegisterProcess(Guid id, PythonProcess process) { lock (_targets) { EnsureListenerSocket(); _targets[id] = new WeakReference(process); } }
/// <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; }
private static void WriteErrorToOutputWindow(ConnErrorMessages result, PythonProcess targetProcess) { var outWin = (IVsOutputWindow)Package.GetGlobalService(typeof(IVsOutputWindow)); IVsOutputWindowPane pane; if (outWin != null && ErrorHandler.Succeeded(outWin.GetPane(VSConstants.GUID_OutWindowDebugPane, out pane))) { pane.Activate(); string moduleName; try { moduleName = Process.GetProcessById(targetProcess.Id).MainModule.ModuleName; } catch { // either the process is no longer around, or it's a 64-bit process // and we can't get the EXE name. moduleName = null; } if (moduleName != null) { pane.OutputString(Strings.DebugConnectionFailedToConnectToProcessWithModule.FormatUI( targetProcess.Id, moduleName, result.GetErrorMessage()) ); } else { pane.OutputString(Strings.DebugConnectionFailedToConnectToProcessWithoutModule.FormatUI( targetProcess.Id, result.GetErrorMessage()) ); } } }
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; }
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(filename)) { sb.Append(filename); if (!string.IsNullOrEmpty(lineNumber)) { sb.Append(":"); sb.Append(lineNumber); } sb.Append(" in "); } if (string.IsNullOrEmpty(functionName)) { sb.AppendLine("<unknown>"); } else { sb.AppendLine(functionName); } } while (sb.Length > 0 && char.IsWhiteSpace(sb[sb.Length - 1])) { sb.Length -= 1; } return(sb.ToString()); }
public static ConnErrorMessages TryAttach(int pid, out PythonProcess process) { try { process = new PythonProcess(pid); return(ConnErrorMessages.None); } catch (AttachException ex) { process = null; return(ex.Error); } }
/// <summary> /// Creates a PythonObject for an expression which successfully returned a value. /// </summary> public PythonEvaluationResult(PythonProcess process, string objRepr, string typeName, string expression, string childText, bool childIsIndex, PythonStackFrame frame, bool isExpandable) { _process = process; _expression = expression; _frame = frame; _objRepr = objRepr; _typeName = typeName; _isExpandable = isExpandable; _childText = childText; _childIsIndex = childIsIndex; }
/// <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; }
public static string GetQualifiedFunctionName(PythonProcess process, string filename, int lineNo, string functionName) { var ast = process.GetAst(filename); if (ast == null) { return(functionName); } return(QualifiedFunctionNameWalker.GetDisplayName( lineNo, functionName, ast, (a, n) => string.IsNullOrEmpty(a) ? n : Strings.DebugStackFrameNameInName.FormatUI(n, a) )); }
public void SetValue(PythonProcess process, string key, string value) { if (string.IsNullOrEmpty(key)) { Debug.Fail("Unexpected empty key"); return; } if (value == null) { Debug.Fail("Unexpected null value"); return; } switch (key.ToLowerInvariant()) { case "typename": TypeName = value; break; case "message": ExceptionMessage = value; break; case "trace": StackTrace = ReformatStackTrace(process, value); break; case "excvalue": ExceptionObjectExpression = value; break; case "breaktype": UserUnhandled = ("unhandled".Equals(value, StringComparison.OrdinalIgnoreCase)); break; default: Debug.WriteLine("Unexpected key in rich exception: " + key); break; } }
internal PythonThread(PythonProcess process, int identity) { _process = process; _identity = identity; }
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(filename)) { sb.Append(filename); if (!string.IsNullOrEmpty(lineNumber)) { sb.Append(":"); sb.Append(lineNumber); } sb.Append(" in "); } if (string.IsNullOrEmpty(functionName)) { sb.AppendLine("<unknown>"); } else { sb.AppendLine(functionName); } } while (sb.Length > 0 && char.IsWhiteSpace(sb[sb.Length - 1])) { sb.Length -= 1; } return sb.ToString(); }
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(); }
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(); }
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)); } } }
private static void WriteErrorToOutputWindow(ConnErrorMessages result, PythonProcess targetProcess) { var outWin = (IVsOutputWindow)Package.GetGlobalService(typeof(IVsOutputWindow)); IVsOutputWindowPane pane; if (outWin != null && ErrorHandler.Succeeded(outWin.GetPane(VSConstants.GUID_OutWindowDebugPane, out pane))) { pane.Activate(); string moduleName; try { moduleName = Process.GetProcessById(targetProcess.Id).MainModule.ModuleName; } catch { // either the process is no longer around, or it's a 64-bit process // and we can't get the EXE name. moduleName = null; } if (moduleName != null) { pane.OutputString(String.Format("Failed to connect to process {0} ({1}): {2}", targetProcess.Id, moduleName, result.GetErrorMessage()) ); } else { pane.OutputString(String.Format("Failed to connect to process {0}: {1}", targetProcess.Id, result.GetErrorMessage()) ); } } }
internal void AttachProcess(PythonProcess process, IThreadIdMapper threadIdMapper) { if (_evaluators.ContainsKey(process.Id)) { // Process is already attached, so just switch to it if needed SwitchProcess(process, false); return; } process.ProcessExited += new EventHandler<ProcessExitedEventArgs>(OnProcessExited); var evaluator = new PythonDebugProcessReplEvaluator(_serviceProvider, process, _pyService, threadIdMapper); evaluator.Window = _window; evaluator.InitializeAsync().WaitAndUnwrapExceptions(); evaluator.AvailableScopesChanged += new EventHandler<EventArgs>(evaluator_AvailableScopesChanged); evaluator.MultipleScopeSupportChanged += new EventHandler<EventArgs>(evaluator_MultipleScopeSupportChanged); _evaluators.Add(process.Id, evaluator); _activeEvaluator = evaluator; }
internal void DetachProcess(PythonProcess process) { int id = process.Id; PythonDebugProcessReplEvaluator evaluator; if (_evaluators.TryGetValue(id, out evaluator)) { evaluator.AvailableScopesChanged -= new EventHandler<EventArgs>(evaluator_AvailableScopesChanged); evaluator.MultipleScopeSupportChanged -= new EventHandler<EventArgs>(evaluator_MultipleScopeSupportChanged); process.DisconnectRepl(); _evaluators.Remove(id); if (_activeEvaluator == evaluator) { _activeEvaluator = null; } ActiveProcessChanged(); } }
/// <summary> /// Computes the fully qualified function name, including name of the enclosing class for methods, /// and, recursively, names of any outer functions. /// </summary> /// <example> /// Given this code: /// <code> /// class A: /// def b(self): /// def c(): /// class D: /// def e(self): /// pass /// </code> /// And with the current statement being <c>pass</c>, the qualified name is "D.e in c in A.b". /// </example> public static string GetQualifiedFunctionName(PythonProcess process, string filename, int lineNo, string functionName) { var ast = process.GetAst(filename); if (ast == null) { return functionName; } var walker = new QualifiedFunctionNameWalker(ast, lineNo, functionName); try { ast.Walk(walker); } catch (InvalidDataException) { // Walker ran into a mismatch between expected function name and AST, so we cannot // rely on AST to construct an accurate qualified name. Just return what we have. return functionName; } string qualName = walker.Name; if (string.IsNullOrEmpty(qualName)) { return functionName; } return qualName; }