/// <summary>
 /// Creates an instance of the StackFrameDetails class from a
 /// CallStackFrame instance provided by the PowerShell engine.
 /// </summary>
 /// <param name="callStackFrame">
 /// The original CallStackFrame instance from which details will be obtained.
 /// </param>
 /// <param name="autoVariables">
 /// A variable container with all the filtered, auto variables for this stack frame.
 /// </param>
 /// <param name="localVariables">
 /// A variable container with all the local variables for this stack frame.
 /// </param>
 /// <returns>A new instance of the StackFrameDetails class.</returns>
 static internal StackFrameDetails Create(
     CallStackFrame callStackFrame,
     VariableContainerDetails autoVariables,
     VariableContainerDetails localVariables)
 {
     return new StackFrameDetails
     {
         ScriptPath = callStackFrame.ScriptName,
         FunctionName = callStackFrame.FunctionName,
         LineNumber = callStackFrame.Position.StartLineNumber,
         ColumnNumber = callStackFrame.Position.StartColumnNumber,
         AutoVariables = autoVariables,
         LocalVariables = localVariables
     };
 }
Exemple #2
0
        public static LogEntryInfo FromMessage(string message, CallStackFrame callStackFrame = null)
        {
            string messageWithoutLineInfo;
            string file;
            int lineNumber;
            int columnNumber;

            if (TryGetLineInfoFromMessages(message, out messageWithoutLineInfo, out file, out lineNumber, out columnNumber))
            {
                return new LogEntryInfo(
                    message: messageWithoutLineInfo,
                    file: file,
                    lineNumber: lineNumber,
                    columnNumber: columnNumber);
            }

            if (callStackFrame != null)
            {
                return new LogEntryInfo(
                    message: message,
                    file: callStackFrame.ScriptName,
                    lineNumber: string.IsNullOrEmpty(callStackFrame.ScriptName) ? 0 : callStackFrame.ScriptLineNumber,
                    columnNumber: 0);
            }

            return new LogEntryInfo(
                message: message,
                file: null,
                lineNumber: 0,
                columnNumber: 0);
        }
Exemple #3
0
        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;
        }