/// <summary> /// MisleadingBacktick: Checks that lines don't end with a backtick followed by a whitespace /// </summary> public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) { if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); string[] lines = NewlineRegex.Split(ast.Extent.Text); if((ast.Extent.EndLineNumber - ast.Extent.StartLineNumber + 1) != lines.Length) { // Did not match the number of lines that the extent indicated yield break; } foreach (int i in Enumerable.Range(0, lines.Length)) { string line = lines[i]; Match match = TrailingEscapedWhitespaceRegex.Match(line); if(match.Success) { int lineNumber = ast.Extent.StartLineNumber + i; ScriptPosition start = new ScriptPosition(fileName, lineNumber, match.Index, line); ScriptPosition end = new ScriptPosition(fileName, lineNumber, match.Index + match.Length, line); yield return new DiagnosticRecord( string.Format(CultureInfo.CurrentCulture, Strings.MisleadingBacktickError), new ScriptExtent(start, end), GetName(), DiagnosticSeverity.Warning, fileName); } } }
internal InvocationInfo(PSObject psObject) { this._historyId = -1L; this._pipelineIterationInfo = new int[0]; this._commandOrigin = (System.Management.Automation.CommandOrigin) SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_CommandOrigin"); this._expectingInput = (bool) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ExpectingInput"); this._invocationName = (string) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_InvocationName"); this._historyId = (long) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_HistoryId"); this._pipelineLength = (int) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelineLength"); this._pipelinePosition = (int) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelinePosition"); string propertyValue = (string) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptName"); int scriptLineNumber = (int) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptLineNumber"); int offsetInLine = (int) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_OffsetInLine"); string line = (string) SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_Line"); System.Management.Automation.Language.ScriptPosition startPosition = new System.Management.Automation.Language.ScriptPosition(propertyValue, scriptLineNumber, offsetInLine, line); this._scriptPosition = new ScriptExtent(startPosition, startPosition); this._commandInfo = RemoteCommandInfo.FromPSObjectForRemoting(psObject); ArrayList psObjectPropertyBaseObject = (ArrayList) SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_PipelineIterationInfo"); if (psObjectPropertyBaseObject != null) { this._pipelineIterationInfo = (int[]) psObjectPropertyBaseObject.ToArray(Type.GetType("System.Int32")); } else { this._pipelineIterationInfo = new int[0]; } Hashtable hashtable = (Hashtable) SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_BoundParameters"); Dictionary<string, object> dictionary = new Dictionary<string, object>(); if (hashtable != null) { foreach (DictionaryEntry entry in hashtable) { dictionary.Add((string) entry.Key, entry.Value); } } this._boundParameters = dictionary; ArrayList list2 = (ArrayList) SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_UnboundArguments"); this._unboundArguments = new List<object>(); if (list2 != null) { foreach (object obj2 in list2) { this._unboundArguments.Add(obj2); } } object obj3 = SerializationUtilities.GetPropertyValue(psObject, "SerializeExtent"); bool flag = false; if (obj3 != null) { flag = (bool) obj3; } if (flag) { this._displayScriptPosition = ScriptExtent.FromPSObjectForRemoting(psObject); } }
private void PopulateFromSerializedInfo(PSObject serializedScriptExtent) { string propertyValue = RemotingDecoder.GetPropertyValue<string>(serializedScriptExtent, "ScriptExtent_File"); int scriptLineNumber = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_StartLineNumber"); int offsetInLine = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_StartColumnNumber"); int num3 = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_EndLineNumber"); int num4 = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_EndColumnNumber"); ScriptPosition position = new ScriptPosition(propertyValue, scriptLineNumber, offsetInLine, null); ScriptPosition position2 = new ScriptPosition(propertyValue, num3, num4, null); this._startPosition = position; this._endPosition = position2; }
public void LoadFile(string fileName) { Token[] tokens; ParseError[] localErrors=null; try { var ast = Parser.ParseFile(fileName, out tokens, out localErrors); errors.AddRange(localErrors); } catch (ParseException ex) { var pos = new ScriptPosition(fileName,0,0,""); var err = new ParseError(new ScriptExtent(pos, pos), "FatalParserError", GetNestedMessage(ex)); errors.Add(err); } }
private void HandleWorkflowApplicationFaultedState(Exception e) { if (System.Threading.Interlocked.CompareExchange(ref terminalStateHandled, Handled, NotHandled) == Handled) return; Tracer.WriteMessage("Workflow Application is completed in Faulted state."); // there might be possible of race condition here in case of Winrm shutdown. if the activity is // executing on loop back winrm process so there might be possibility that the winrm shuts down the // activity process fist and causing the workflow to fail in the M3P process. // in order to avoid those situations we will ignore the remote exception if the shutdown is in progress if (WorkflowJobSourceAdapter.GetInstance().IsShutdownInProgress) { if (e.GetType() == typeof(RemoteException)) { Tracer.WriteMessage("PSWorkflowApplicationInstance", "HandleWorkflowApplicationFaultedState", id, "Since we are in shuting down mode so ignoring the remote exception"); Tracer.TraceException(e); // no need to proceed any further return; } } // before we start handling the faulted state we need to cancel // all running activities. Activities may be running if there is a // parallel statement involved StopAllAsyncExecutions(); State = JobState.Failed; Exception reason = null; if (!_suppressTerminateError) { reason = e; ErrorRecord failureErrorRecord = GetInnerErrorRecord(reason); if (failureErrorRecord != null) { reason = failureErrorRecord.Exception; if (PSWorkflowJob.SynchronousExecution && Streams.ErrorStream.IsOpen) { // If we're running synchronously from the commandline, we want to see the error // record from the workflow without the Receive-Job decoration. Receive-Job will // decorate the reason, so do not send it as the reason. // Note that if the error collection is closed, we do not want to lose this data, so // pass the exception despite the bad decoration. Streams.ErrorStream.Add(failureErrorRecord); reason = null; } } else { // No error record, a raw exception was thrown. See if we have location // information that we can wrap it in. HostSettingCommandMetadata commandMetadata = null; if (_paramDefaults != null) commandMetadata = _paramDefaults.HostCommandMetadata; if (commandMetadata != null) { ScriptPosition scriptStart = new ScriptPosition( commandMetadata.CommandName, commandMetadata.StartLineNumber, commandMetadata.StartColumnNumber, null); ScriptPosition scriptEnd = new ScriptPosition( commandMetadata.CommandName, commandMetadata.EndLineNumber, commandMetadata.EndColumnNumber, null); ScriptExtent extent = new ScriptExtent(scriptStart, scriptEnd); reason = new JobFailedException(reason, extent); } } } this.Error = reason; this.PerformTaskAtTerminalState(); // do all cleanups here to save memory PerformCleanupAtTerminalState(); if (this.OnFaulted != null) this.OnFaulted(reason, this); }
/// <summary> /// Creates an InformationalRecord from an instance serialized as a PSObject by ToPSObjectForRemoting. /// </summary> internal InvocationInfo(PSObject psObject) { CommandOrigin = (CommandOrigin)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_CommandOrigin"); ExpectingInput = (bool)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ExpectingInput"); _invocationName = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_InvocationName"); HistoryId = (long)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_HistoryId"); PipelineLength = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelineLength"); PipelinePosition = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelinePosition"); string scriptName = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptName"); int scriptLineNumber = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptLineNumber"); int offsetInLine = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_OffsetInLine"); string line = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_Line"); var scriptPosition = new ScriptPosition(scriptName, scriptLineNumber, offsetInLine, line); ScriptPosition scriptEndPosition; if (!string.IsNullOrEmpty(line)) { int endColumn = line.Length + 1; scriptEndPosition = new ScriptPosition(scriptName, scriptLineNumber, endColumn, line); } else { scriptEndPosition = scriptPosition; } _scriptPosition = new ScriptExtent(scriptPosition, scriptEndPosition); MyCommand = RemoteCommandInfo.FromPSObjectForRemoting(psObject); // // Arrays are de-serialized as ArrayList so we need to convert the deserialized // object into an int[] before assigning to pipelineIterationInfo. // var list = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_PipelineIterationInfo"); if (list != null) { PipelineIterationInfo = (int[])list.ToArray(typeof(Int32)); } else { PipelineIterationInfo = Utils.EmptyArray<int>(); } // // Dictionaries are de-serialized as Hashtables so we need to convert the deserialized object into a dictionary // before assigning to CommandLineParameters. // Hashtable hashtable = (Hashtable)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_BoundParameters"); Dictionary<string, object> dictionary = new Dictionary<string, object>(); if (hashtable != null) { foreach (DictionaryEntry entry in hashtable) { dictionary.Add((string)entry.Key, entry.Value); } } _boundParameters = dictionary; // // The unbound parameters are de-serialized as an ArrayList, which we need to convert to a List // var unboundArguments = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_UnboundArguments"); _unboundArguments = new List<object>(); if (unboundArguments != null) { foreach (object o in unboundArguments) { _unboundArguments.Add(o); } } object value = SerializationUtilities.GetPropertyValue(psObject, "SerializeExtent"); bool serializeExtent = false; if (value != null) serializeExtent = (bool)value; if (serializeExtent) DisplayScriptPosition = ScriptExtent.FromPSObjectForRemoting(psObject); }
/// <summary> /// Returns script extent of username and password parameters /// </summary> /// <param name="usernameAst"></param> /// <param name="passwordAst"></param> /// <returns>IScriptExtent</returns> private IScriptExtent GetExtent(ParameterAst usernameAst, ParameterAst passwordAst, Ast scriptAst) { var usrExt = usernameAst.Extent; var pwdExt = passwordAst.Extent; IScriptExtent startExt, endExt; var usrBeforePwd = (usrExt.StartLineNumber == pwdExt.StartLineNumber && usrExt.StartColumnNumber < pwdExt.StartColumnNumber) || usrExt.StartLineNumber < pwdExt.StartLineNumber; if (usrBeforePwd) { startExt = usrExt; endExt = pwdExt; } else { startExt = pwdExt; endExt = usrExt; } var startPos = new ScriptPosition( startExt.File, startExt.StartLineNumber, startExt.StartColumnNumber, startExt.StartScriptPosition.Line); var endPos = new ScriptPosition( endExt.File, endExt.EndLineNumber, endExt.EndColumnNumber, endExt.EndScriptPosition.Line); return new ScriptExtent(startPos, endPos); }
private void PopulateFromSerializedInfo(PSObject serializedScriptExtent) { string file = RemotingDecoder.GetPropertyValue<string>(serializedScriptExtent, "ScriptExtent_File"); int startLineNumber = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_StartLineNumber"); int startColumnNumber = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_StartColumnNumber"); int endLineNumber = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_EndLineNumber"); int endColumnNumber = RemotingDecoder.GetPropertyValue<int>(serializedScriptExtent, "ScriptExtent_EndColumnNumber"); ScriptPosition startPosition = new ScriptPosition(file, startLineNumber, startColumnNumber, null); ScriptPosition endPosition = new ScriptPosition(file, endLineNumber, endColumnNumber, null); _startPosition = startPosition; _endPosition = endPosition; }
/// <summary> /// Creates a new ScriptExtent class. /// </summary> public ScriptExtent(ScriptPosition startPosition, ScriptPosition endPosition) { _startPosition = startPosition; _endPosition = endPosition; }
private void HandleWorkflowApplicationFaultedState(Exception e) { if (Interlocked.CompareExchange(ref this.terminalStateHandled, 1, 0) != 1) { this.Tracer.WriteMessage("Workflow Application is completed in Faulted state."); if (!WorkflowJobSourceAdapter.GetInstance().IsShutdownInProgress || !(e.GetType() == typeof(RemoteException))) { this.StopAllAsyncExecutions(); this.State = JobState.Failed; Exception jobFailedException = e; ErrorRecord innerErrorRecord = this.GetInnerErrorRecord(jobFailedException); if (innerErrorRecord == null) { HostSettingCommandMetadata hostCommandMetadata = null; if (this._paramDefaults != null) { hostCommandMetadata = this._paramDefaults.HostCommandMetadata; } if (hostCommandMetadata != null) { ScriptPosition scriptPosition = new ScriptPosition(hostCommandMetadata.CommandName, hostCommandMetadata.StartLineNumber, hostCommandMetadata.StartColumnNumber, null); ScriptPosition scriptPosition1 = new ScriptPosition(hostCommandMetadata.CommandName, hostCommandMetadata.EndLineNumber, hostCommandMetadata.EndColumnNumber, null); ScriptExtent scriptExtent = new ScriptExtent(scriptPosition, scriptPosition1); jobFailedException = new JobFailedException(jobFailedException, scriptExtent); } } else { jobFailedException = innerErrorRecord.Exception; if (this.PSWorkflowJob.SynchronousExecution && this.Streams.ErrorStream.IsOpen) { this.Streams.ErrorStream.Add(innerErrorRecord); jobFailedException = null; } } this.Error = jobFailedException; this.PerformTaskAtTerminalState(); this.PerformCleanupAtTerminalState(); if (base.OnFaulted != null) { base.OnFaulted(jobFailedException, this); } return; } else { this.Tracer.WriteMessage("PSWorkflowApplicationInstance", "HandleWorkflowApplicationFaultedState", this.id, "Sicne we are in shuting down mode so ignoring the remote exception", new string[0]); this.Tracer.TraceException(e); return; } } else { return; } }
public ScriptExtent(ScriptPosition startPosition, ScriptPosition endPosition) { this._startPosition = startPosition; this._endPosition = endPosition; }
private static void PowerShellInvocation_ErrorAdding(object sender, DataAddingEventArgs e, HostSettingCommandMetadata commandMetadata, PSDataCollection<PSObject> output) { ErrorRecord itemAdded = e.ItemAdded as ErrorRecord; if (itemAdded != null) { if (commandMetadata != null) { ScriptPosition scriptPosition = new ScriptPosition(commandMetadata.CommandName, commandMetadata.StartLineNumber, commandMetadata.StartColumnNumber, null); ScriptPosition scriptPosition1 = new ScriptPosition(commandMetadata.CommandName, commandMetadata.EndLineNumber, commandMetadata.EndColumnNumber, null); ScriptExtent scriptExtent = new ScriptExtent(scriptPosition, scriptPosition1); if (itemAdded.InvocationInfo != null) { itemAdded.InvocationInfo.DisplayScriptPosition = scriptExtent; } } if (output != null) { output.Add(PSObject.AsPSObject(itemAdded)); } } }
private InvocationInfo CreateInvocationInfoFromParent( CallStackFrame parentStackFrame, int debugLineNumber, int debugStartColNumber, int debugEndColNumber) { if (parentStackFrame == null) { return null; } // Attempt to find parent script file create script block with Ast to // find correct line and offset adjustments. if ((_parentScriptBlockAst == null) && !string.IsNullOrEmpty(parentStackFrame.ScriptName) && System.IO.File.Exists(parentStackFrame.ScriptName)) { ParseError[] errors; Token[] tokens; _parentScriptBlockAst = Parser.ParseInput( System.IO.File.ReadAllText(parentStackFrame.ScriptName), out tokens, out errors); } if (_parentScriptBlockAst != null) { int callingLineNumber = parentStackFrame.ScriptLineNumber; StatementAst debugStatement = null; StatementAst callingStatement = _parentScriptBlockAst.Find( ast => { return ((ast is StatementAst) && (ast.Extent.StartLineNumber == callingLineNumber)); } , true) as StatementAst; if (callingStatement != null) { // Find first statement in calling statement. StatementAst firstStatement = callingStatement.Find(ast => { return ((ast is StatementAst) && ast.Extent.StartLineNumber > callingLineNumber); }, true) as StatementAst; if (firstStatement != null) { int adjustedLineNumber = firstStatement.Extent.StartLineNumber + debugLineNumber - 1; debugStatement = callingStatement.Find(ast => { return ((ast is StatementAst) && ast.Extent.StartLineNumber == adjustedLineNumber); }, true) as StatementAst; } } if (debugStatement != null) { int endColNum = debugStartColNumber + (debugEndColNumber - debugStartColNumber) - 2; string statementExtentText = FixUpStatementExtent(debugStatement.Extent.StartColumnNumber - 1, debugStatement.Extent.Text); ScriptPosition scriptStartPosition = new ScriptPosition( parentStackFrame.ScriptName, debugStatement.Extent.StartLineNumber, debugStartColNumber, statementExtentText); ScriptPosition scriptEndPosition = new ScriptPosition( parentStackFrame.ScriptName, debugStatement.Extent.EndLineNumber, endColNum, statementExtentText); return InvocationInfo.Create( parentStackFrame.InvocationInfo.MyCommand, new ScriptExtent( scriptStartPosition, scriptEndPosition) ); } } return null; }
/// <summary> /// Creates a source list based on root script debugger source information if available, with /// the current source line highlighted. This is used internally for nested runspace debugging /// where the runspace command is run in context of a parent script. /// </summary> /// <param name="lineNum">Current source line</param> /// <param name="output">Output collection</param> /// <returns>True if source listed successfully</returns> internal override bool InternalProcessListCommand(int lineNum, IList<PSObject> output) { if (!DebuggerStopped || (_currentInvocationInfo == null)) { return false; } // Create an Invocation object that has full source script from script debugger plus // line information provided from caller. string fullScript = _currentInvocationInfo.GetFullScript(); ScriptPosition startScriptPosition = new ScriptPosition( _currentInvocationInfo.ScriptName, lineNum, _currentInvocationInfo.ScriptPosition.StartScriptPosition.Offset, _currentInvocationInfo.Line, fullScript); ScriptPosition endScriptPosition = new ScriptPosition( _currentInvocationInfo.ScriptName, lineNum, _currentInvocationInfo.ScriptPosition.StartScriptPosition.Offset, _currentInvocationInfo.Line, fullScript); InvocationInfo tempInvocationInfo = InvocationInfo.Create( _currentInvocationInfo.MyCommand, new ScriptExtent( startScriptPosition, endScriptPosition) ); _commandProcessor.ProcessListCommand(tempInvocationInfo, output); return true; }
internal InvocationInfo(PSObject psObject) { this._historyId = -1L; this._pipelineIterationInfo = new int[0]; this._commandOrigin = (System.Management.Automation.CommandOrigin)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_CommandOrigin"); this._expectingInput = (bool)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ExpectingInput"); this._invocationName = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_InvocationName"); this._historyId = (long)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_HistoryId"); this._pipelineLength = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelineLength"); this._pipelinePosition = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_PipelinePosition"); string propertyValue = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptName"); int scriptLineNumber = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_ScriptLineNumber"); int offsetInLine = (int)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_OffsetInLine"); string line = (string)SerializationUtilities.GetPropertyValue(psObject, "InvocationInfo_Line"); System.Management.Automation.Language.ScriptPosition startPosition = new System.Management.Automation.Language.ScriptPosition(propertyValue, scriptLineNumber, offsetInLine, line); this._scriptPosition = new ScriptExtent(startPosition, startPosition); this._commandInfo = RemoteCommandInfo.FromPSObjectForRemoting(psObject); ArrayList psObjectPropertyBaseObject = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_PipelineIterationInfo"); if (psObjectPropertyBaseObject != null) { this._pipelineIterationInfo = (int[])psObjectPropertyBaseObject.ToArray(Type.GetType("System.Int32")); } else { this._pipelineIterationInfo = new int[0]; } Hashtable hashtable = (Hashtable)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_BoundParameters"); Dictionary <string, object> dictionary = new Dictionary <string, object>(); if (hashtable != null) { foreach (DictionaryEntry entry in hashtable) { dictionary.Add((string)entry.Key, entry.Value); } } this._boundParameters = dictionary; ArrayList list2 = (ArrayList)SerializationUtilities.GetPsObjectPropertyBaseObject(psObject, "InvocationInfo_UnboundArguments"); this._unboundArguments = new List <object>(); if (list2 != null) { foreach (object obj2 in list2) { this._unboundArguments.Add(obj2); } } object obj3 = SerializationUtilities.GetPropertyValue(psObject, "SerializeExtent"); bool flag = false; if (obj3 != null) { flag = (bool)obj3; } if (flag) { this._displayScriptPosition = ScriptExtent.FromPSObjectForRemoting(psObject); } }