static void Main(string[] args) { Options options = null; Parser.Default.ParseArguments <Options>(args) .WithParsed(o => options = o); if (options == null) { return; } DebuggerInitialization.OpenDump(options.DumpPath, options.SymbolPath); Console.WriteLine("Threads: {0}", Thread.All.Length); Console.WriteLine("Current thread: {0}", Thread.Current.Id); var frames = Thread.Current.StackTrace.Frames; Console.WriteLine("Call stack:"); foreach (var frame in frames) { try { Console.WriteLine(" {0,3:x} {1}+0x{2:x} ({3}:{4})", frame.FrameNumber, frame.FunctionName, frame.FunctionDisplacement, frame.SourceFileName, frame.SourceFileLine); } catch (Exception) { Console.WriteLine(" {0,3:x} {1}+0x{2:x}", frame.FrameNumber, frame.FunctionName, frame.FunctionDisplacement); } } // In order to use console output and error in scripts, we must set it to debug client DebugOutput captureFlags = DebugOutput.Normal | DebugOutput.Error | DebugOutput.Warning | DebugOutput.Verbose | DebugOutput.Prompt | DebugOutput.PromptRegisters | DebugOutput.ExtensionWarning | DebugOutput.Debuggee | DebugOutput.DebuggeePrompt | DebugOutput.Symbols | DebugOutput.Status; var callbacks = DebuggerOutputToTextWriter.Create(Console.Out, captureFlags); using (OutputCallbacksSwitcher switcher = OutputCallbacksSwitcher.Create(callbacks)) { Action action = () => { ScriptExecution.Execute(@"..\..\samples\script.csx", args); }; DbgEngDll dbgEngDll = Context.Debugger as DbgEngDll; if (dbgEngDll != null) { dbgEngDll.ExecuteAction(action); } else { action(); } } }
public void VerifyOutput(string code) { UiExecutionEntry[] executions = GetExecutions().Take(2).ToArray(); Assert.Equal(2, executions.Length); UiExecutionEntry currentEntry = executions[0]; UiExecutionEntry lastExecution = executions[1]; // Capture code output string expectedOutput; string expectedError; using (StringWriter outputWriter = new StringWriter()) using (StringWriter errorWriter = new StringWriter()) { TextWriter originalOutput = Console.Out; TextWriter originalError = Console.Error; try { Console.SetOut(outputWriter); Console.SetError(errorWriter); DebugOutput captureFlags = DebugOutput.Normal | DebugOutput.Error | DebugOutput.Warning | DebugOutput.Verbose | DebugOutput.Prompt | DebugOutput.PromptRegisters | DebugOutput.ExtensionWarning | DebugOutput.Debuggee | DebugOutput.DebuggeePrompt | DebugOutput.Symbols | DebugOutput.Status; var callbacks = DebuggerOutputToTextWriter.Create(outputWriter, captureFlags); using (OutputCallbacksSwitcher switcher = OutputCallbacksSwitcher.Create(callbacks)) { InteractiveExecution.Interpret(code); } expectedOutput = outputWriter.GetStringBuilder().ToString(); expectedError = errorWriter.GetStringBuilder().ToString(); } finally { Console.SetOut(originalOutput); Console.SetError(originalError); } } // Verify that output is the same if (currentEntry.Code == lastExecution.Code) { Assert.Equal(FixOutput(expectedError), FixOutput(lastExecution.ResultText)); } else { Assert.Equal(FixOutput(expectedOutput), FixOutput(lastExecution.ResultText)); } }
/// <summary> /// Executes the specified command and captures its output. /// </summary> /// <param name="captureFlags">The capture flags.</param> /// <param name="command">The command.</param> /// <param name="parameters">The parameters.</param> /// <returns>Captured text</returns> public static string ExecuteAndCapture(DebugOutput captureFlags, string command, params object[] parameters) { using (StringWriter writer = new StringWriter()) { var callbacks = DebuggerOutputToTextWriter.Create(writer, captureFlags); using (OutputCallbacksSwitcher switcher = OutputCallbacksSwitcher.Create(callbacks)) { Execute(command, parameters); writer.Flush(); return(writer.GetStringBuilder().ToString()); } } }
protected override void OnExecuteCSharpScript() { BackgroundExecute((string documentText, out string textOutput, out string errorOutput, out IEnumerable <object> result) => { // Setting results textOutput = ""; errorOutput = ""; // Execution code var oldOut = Console.Out; var oldError = Console.Error; try { using (StringWriter writer = new StringWriter()) { Console.SetOut(writer); Console.SetError(writer); DebugOutput captureFlags = DebugOutput.Normal | DebugOutput.Error | DebugOutput.Warning | DebugOutput.Verbose | DebugOutput.Prompt | DebugOutput.PromptRegisters | DebugOutput.ExtensionWarning | DebugOutput.Debuggee | DebugOutput.DebuggeePrompt | DebugOutput.Symbols | DebugOutput.Status; var callbacks = DebuggerOutputToTextWriter.Create(Console.Out, captureFlags); using (OutputCallbacksSwitcher switcher = OutputCallbacksSwitcher.Create(callbacks)) { interactiveExecution.UnsafeInterpret(documentText); writer.Flush(); textOutput = writer.GetStringBuilder().ToString(); } } UpdateScriptCode(); } catch (Microsoft.CodeAnalysis.Scripting.CompilationErrorException ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Compile errors:"); foreach (var error in ex.Diagnostics) { sb.AppendLine(error.ToString()); } errorOutput = sb.ToString(); } catch (ExitRequestedException) { throw; } catch (AggregateException ex) { if (ex.InnerException is ExitRequestedException) { throw ex.InnerException; } errorOutput = ex.ToString(); } catch (TargetInvocationException ex) { if (ex.InnerException is ExitRequestedException) { throw ex.InnerException; } errorOutput = ex.InnerException.ToString(); } catch (Exception ex) { errorOutput = ex.ToString(); } finally { Console.SetError(oldError); Console.SetOut(oldOut); result = results; results = new List <object>(); } }, true); }
protected override void OnExecuteCSharpScript() { BackgroundExecute((string _documentText, out string _textOutput, out string _errorOutput, out IEnumerable <object> _result) => { BackgroundExecuteDelegate scriptExecution = (string documentText, out string textOutput, out string errorOutput, out IEnumerable <object> result) => { // Setting results textOutput = ""; errorOutput = ""; // Execution code var oldOut = Console.Out; var oldError = Console.Error; var oldGraphics = Context.Graphics; try { Context.Graphics = new Graphics(Dispatcher); using (StringWriter writer = new StringWriter()) { Console.SetOut(writer); Console.SetError(writer); DebugOutput captureFlags = DebugOutput.Normal | DebugOutput.Error | DebugOutput.Warning | DebugOutput.Verbose | DebugOutput.Prompt | DebugOutput.PromptRegisters | DebugOutput.ExtensionWarning | DebugOutput.Debuggee | DebugOutput.DebuggeePrompt | DebugOutput.Symbols | DebugOutput.Status; var callbacks = DebuggerOutputToTextWriter.Create(Console.Out, captureFlags); InteractiveExecution.scriptBase._UiActionExecutor_ = (action) => Dispatcher.Invoke(action); using (OutputCallbacksSwitcher switcher = OutputCallbacksSwitcher.Create(callbacks)) { InteractiveExecution.UnsafeInterpret(documentText); writer.Flush(); textOutput = writer.GetStringBuilder().ToString(); } } UpdateScriptCode(); } catch (Microsoft.CodeAnalysis.Scripting.CompilationErrorException ex) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Compile errors:"); foreach (var error in ex.Diagnostics) { sb.AppendLine(error.ToString()); } errorOutput = sb.ToString(); } catch (ExitRequestedException) { throw; } catch (AggregateException ex) { if (ex.InnerException is ExitRequestedException) { throw ex.InnerException; } errorOutput = ex.ToString(); } catch (TargetInvocationException ex) { if (ex.InnerException is ExitRequestedException) { throw ex.InnerException; } errorOutput = ex.InnerException.ToString(); } catch (Exception ex) { errorOutput = ex.ToString(); } finally { Console.SetError(oldError); Console.SetOut(oldOut); Context.Graphics = oldGraphics; result = results; results = new List <object>(); InteractiveExecution.scriptBase._UiActionExecutor_ = null; } }; // Check if we should execute script code in STA thread if (InteractiveExecution.scriptBase.ForceStaExecution) { string tempTextOutput = null; string tempErrorOutput = null; IEnumerable <object> tempResult = null; InteractiveWindow.ExecuteInSTA(() => { scriptExecution(_documentText, out tempTextOutput, out tempErrorOutput, out tempResult); }); _textOutput = tempTextOutput; _errorOutput = tempErrorOutput; _result = tempResult; } else { scriptExecution(_documentText, out _textOutput, out _errorOutput, out _result); } }, true); }