Пример #1
0
        public void GetSourceLineInfoWithoutCasAssert(string assemblyPath, IntPtr loadedPeAddress, int loadedPeSize,
                                                      IntPtr inMemoryPdbAddress, int inMemoryPdbSize, int methodToken, int ilOffset,
                                                      out string sourceFile, out int sourceLine, out int sourceColumn)
        {
            sourceFile   = null;
            sourceLine   = 0;
            sourceColumn = 0;

            try
            {
                MetadataReader reader = TryGetReader(assemblyPath, loadedPeAddress, loadedPeSize, inMemoryPdbAddress, inMemoryPdbSize);
                if (reader == null)
                {
                    return;
                }

                Handle handle = MetadataTokens.Handle(methodToken);
                if (handle.Kind != HandleKind.MethodDefinition)
                {
                    return;
                }

                MethodDebugInformationHandle methodDebugHandle = ((MethodDefinitionHandle)handle).ToDebugInformationHandle();
                MethodDebugInformation       methodInfo        = reader.GetMethodDebugInformation(methodDebugHandle);

                if (!methodInfo.SequencePointsBlob.IsNil)
                {
                    SequencePointCollection sequencePoints = methodInfo.GetSequencePoints();

                    SequencePoint?bestPointSoFar = null;
                    foreach (SequencePoint point in sequencePoints)
                    {
                        if (point.Offset > ilOffset)
                        {
                            break;
                        }

                        if (point.StartLine != SequencePoint.HiddenLine)
                        {
                            bestPointSoFar = point;
                        }
                    }

                    if (bestPointSoFar.HasValue)
                    {
                        sourceLine   = bestPointSoFar.Value.StartLine;
                        sourceColumn = bestPointSoFar.Value.StartColumn;
                        sourceFile   = reader.GetString(reader.GetDocument(bestPointSoFar.Value.Document).Name);
                    }
                }
            }
            catch (BadImageFormatException)
            {
                // ignore
            }
            catch (IOException)
            {
                // ignore
            }
        }
Пример #2
0
 public InstructionSequence(IList <Instruction> instructions, InstructionSequence?previous, int count, SequencePoint?point)
 {
     _instructions = instructions;
     _previous     = previous;
     Count         = count;
     Point         = point;
 }
Пример #3
0
        public SourceLocation GetLocationByIl(int pos)
        {
            SequencePoint?prev = null;

            if (!DebugInformation.SequencePointsBlob.IsNil)
            {
                foreach (SequencePoint sp in DebugInformation.GetSequencePoints())
                {
                    if (sp.Offset > pos)
                    {
                        //get the earlier line number if the offset is in a hidden sequence point and has a earlier line number available
                        // if is doesn't continue and get the next line number that is not in a hidden sequence point
                        if (sp.IsHidden && prev == null)
                        {
                            continue;
                        }
                        break;
                    }

                    if (!sp.IsHidden)
                    {
                        prev = sp;
                    }
                }

                if (prev.HasValue)
                {
                    return(new SourceLocation(this, prev.Value));
                }
            }
            return(null);
        }
Пример #4
0
        public void MergeWithPreviousSequencePoint(SequencePoint?sequencePoint)
        {
            if (sequencePoint == null)
            {
                return;
            }

            var sequencePoints = _method.DebugInformation.SequencePoints;

            var index = sequencePoints.IndexOf(sequencePoint);

            if (index <= 0)
            {
                return;
            }

            var previousSequencePoint = sequencePoints[index - 1];

            if (previousSequencePoint.Document.Url != sequencePoint.Document.Url)
            {
                return;
            }

            previousSequencePoint.EndLine   = sequencePoint.EndLine;
            previousSequencePoint.EndColumn = sequencePoint.EndColumn;

            sequencePoints.RemoveAt(index);
        }
Пример #5
0
        public void Warning(string message, SequencePoint?sequencePoint)
        {
            if (_moduleWeaver.LogWarningPoint != null)
            {
                _moduleWeaver.LogWarningPoint.Invoke(message, sequencePoint);
                return;
            }

            _moduleWeaver.LogWarning?.Invoke(message);
        }
Пример #6
0
        public void Error(string message, SequencePoint?sequencePoint)
        {
            if (_moduleWeaver.LogErrorPoint != null)
            {
                _moduleWeaver.LogErrorPoint.Invoke(message, sequencePoint);
                return;
            }

            _moduleWeaver.LogError?.Invoke(message);
        }
Пример #7
0
        /// <summary>
        /// Helper method to return source line number and source file name for given IL offset and method token.
        /// </summary>
        /// <param name="openedReader">symbol reader returned by LoadSymbolsForModule</param>
        /// <param name="methodToken">method token</param>
        /// <param name="ilOffset">IL offset</param>
        /// <param name="lineNumber">source line number return</param>
        /// <param name="fileName">source file name return</param>
        /// <returns> true if information is available</returns>
        private bool GetSourceLineByILOffset(
            OpenedReader openedReader,
            int methodToken,
            long ilOffset,
            out int lineNumber,
            out string fileName)
        {
            lineNumber = 0;
            fileName   = null;
            MetadataReader reader = openedReader.Reader;

            try
            {
                Handle handle = MetadataTokens.Handle(methodToken);
                if (handle.Kind != HandleKind.MethodDefinition)
                {
                    return(false);
                }

                MethodDebugInformationHandle methodDebugHandle = ((MethodDefinitionHandle)handle).ToDebugInformationHandle();
                if (methodDebugHandle.IsNil)
                {
                    return(false);
                }

                MethodDebugInformation  methodDebugInfo = reader.GetMethodDebugInformation(methodDebugHandle);
                SequencePointCollection sequencePoints  = methodDebugInfo.GetSequencePoints();

                SequencePoint?nearestPoint = null;
                foreach (SequencePoint point in sequencePoints)
                {
                    if (point.Offset > ilOffset)
                    {
                        break;
                    }

                    if (point.StartLine != 0 && !point.IsHidden)
                    {
                        nearestPoint = point;
                    }
                }

                if (nearestPoint.HasValue)
                {
                    lineNumber = nearestPoint.Value.StartLine;
                    fileName   = reader.GetString(reader.GetDocument(nearestPoint.Value.Document).Name);
                    return(true);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError($"GetSourceLineByILOffset: {ex.Message}");
            }
            return(false);
        }
Пример #8
0
 void LogErrorPoint(string message, SequencePoint? point)
 {
     if (point == null)
     {
         Logger.LogError(message);
     }
     else
     {
         Logger.LogError(message, point.Document.Url, point.StartLine, point.StartColumn, point.EndLine, point.EndColumn);
     }
 }
Пример #9
0
        public Instruction MarkLabel(string labelName, SequencePoint?sequencePoint)
        {
            var labelInfo = _labels.GetOrAddNew(labelName);

            if (labelInfo.IsDefined)
            {
                throw new WeavingException($"Label '{labelName}' is already defined");
            }

            labelInfo.SequencePoint = sequencePoint;
            return(labelInfo.PlaceholderTarget);
        }
Пример #10
0
        public void PopulateStackFrame(bool isMethodDynamic, string location, int methodTokenId, int IlOffset, out string fileName, out int row, out int column)
        {
            fileName = "";
            row      = 0;
            column   = 0;

            if (isMethodDynamic)
            {
                return;
            }

            var metadataReader = GetMetadataReader(location);

            if (metadataReader == null)
            {
                return;
            }

            var methodToken = MetadataTokens.Handle(methodTokenId);

            Debug.Assert(methodToken.Kind == HandleKind.MethodDefinition);

            var handle = ((MethodDefinitionHandle)methodToken).ToDebugInformationHandle();

            if (!handle.IsNil)
            {
                var           methodDebugInfo = metadataReader.GetMethodDebugInformation(handle);
                var           sequencePoints  = methodDebugInfo.GetSequencePoints();
                SequencePoint?bestPointSoFar  = null;

                foreach (var point in sequencePoints)
                {
                    if (point.Offset > IlOffset)
                    {
                        break;
                    }

                    if (point.StartLine != SequencePoint.HiddenLine)
                    {
                        bestPointSoFar = point;
                    }
                }

                if (bestPointSoFar.HasValue)
                {
                    row      = bestPointSoFar.Value.StartLine;
                    column   = bestPointSoFar.Value.StartColumn;
                    fileName = metadataReader.GetString(metadataReader.GetDocument(bestPointSoFar.Value.Document).Name);
                }
            }
        }
Пример #11
0
        public override string?ToString()
        {
            int    sourceLine = SourceLine, sourceColumn = SourceColumn;
            string?fileName = FileName;

            if (Provider is MethodDefinition method &&
                method.DebugInformation.HasSequencePoints)
            {
                var           offset = ILOffset ?? 0;
                SequencePoint?correspondingSequencePoint = method.DebugInformation.SequencePoints
                                                           .Where(s => s.Offset <= offset)?.Last();

                // If the warning comes from hidden line (compiler generated code typically)
                // search for any sequence point with non-hidden line number and report that as a best effort.
                if (correspondingSequencePoint?.StartLine == HiddenLineNumber)
                {
                    correspondingSequencePoint = method.DebugInformation.SequencePoints
                                                 .Where(s => s.StartLine != HiddenLineNumber).FirstOrDefault();
                }

                if (correspondingSequencePoint != null)
                {
                    fileName     = correspondingSequencePoint.Document.Url;
                    sourceLine   = correspondingSequencePoint.StartLine;
                    sourceColumn = correspondingSequencePoint.StartColumn;
                }
            }

            if (fileName == null)
            {
                return(null);
            }

            StringBuilder sb = new StringBuilder(fileName);

            if (sourceLine != 0)
            {
                sb.Append("(").Append(sourceLine);
                if (sourceColumn != 0)
                {
                    sb.Append(",").Append(sourceColumn);
                }

                sb.Append(")");
            }

            return(sb.ToString());
        }
        /// <summary>
        /// Returns the source file and line number information for the method.
        /// </summary>
        /// <param name="assembly">managed assembly</param>
        /// <param name="assemblyPath">file path of the assembly or null</param>
        /// <param name="loadedPeAddress">loaded PE image address or zero</param>
        /// <param name="loadedPeSize">loaded PE image size</param>
        /// <param name="inMemoryPdbAddress">in memory PDB address or zero</param>
        /// <param name="inMemoryPdbSize">in memory PDB size</param>
        /// <param name="methodToken">method token</param>
        /// <param name="ilOffset">il offset of the stack frame</param>
        /// <param name="sourceFile">source file return</param>
        /// <param name="sourceLine">line number return</param>
        /// <param name="sourceColumn">column return</param>
        internal void GetSourceLineInfo(Assembly assembly, string assemblyPath, IntPtr loadedPeAddress, int loadedPeSize,
                                        IntPtr inMemoryPdbAddress, int inMemoryPdbSize, int methodToken, int ilOffset,
                                        out string?sourceFile, out int sourceLine, out int sourceColumn)
        {
            sourceFile   = null;
            sourceLine   = 0;
            sourceColumn = 0;

            MetadataReader?reader = TryGetReader(assembly, assemblyPath, loadedPeAddress, loadedPeSize, inMemoryPdbAddress, inMemoryPdbSize);

            if (reader != null)
            {
                Handle handle = MetadataTokens.Handle(methodToken);

                if (handle.Kind == HandleKind.MethodDefinition)
                {
                    MethodDebugInformationHandle methodDebugHandle = ((MethodDefinitionHandle)handle).ToDebugInformationHandle();
                    MethodDebugInformation       methodInfo        = reader.GetMethodDebugInformation(methodDebugHandle);

                    if (!methodInfo.SequencePointsBlob.IsNil)
                    {
                        SequencePointCollection sequencePoints = methodInfo.GetSequencePoints();

                        SequencePoint?bestPointSoFar = null;
                        foreach (SequencePoint point in sequencePoints)
                        {
                            if (point.Offset > ilOffset)
                            {
                                break;
                            }

                            if (point.StartLine != SequencePoint.HiddenLine)
                            {
                                bestPointSoFar = point;
                            }
                        }

                        if (bestPointSoFar.HasValue)
                        {
                            sourceLine   = bestPointSoFar.Value.StartLine;
                            sourceColumn = bestPointSoFar.Value.StartColumn;
                            sourceFile   = reader.GetString(reader.GetDocument(bestPointSoFar.Value.Document).Name);
                        }
                    }
                }
            }
        }
Пример #13
0
 public ILInstruction(
     int offset,
     ILInstructionType type,
     ILInstructionFlagsContext flagsContext,
     ushort popCount,
     ushort pushCount,
     object argument,
     SequencePoint?sequencePoint)
 {
     Offset          = offset;
     InstructionType = type;
     FlagsContext    = flagsContext;
     PopCount        = popCount;
     PushCount       = pushCount;
     Argument        = argument;
     SequencePoint   = sequencePoint;
 }
Пример #14
0
    public void Warning(string message, SequencePoint?sequencePoint)
    {
        switch (_config.Warnings)
        {
        case WeaverConfigOptions.WarningsBehavior.Ignore:
            _log.Debug($"Ignored warning: {message}");
            break;

        case WeaverConfigOptions.WarningsBehavior.Errors:
            _log.Error($"Warning as error: {message}", sequencePoint);
            break;

        default:
            _log.Warning(message, sequencePoint);
            break;
        }
    }
Пример #15
0
        public void PopulateStackFrame(StackFrameInfo frameInfo, MethodBase method, int IlOffset)
        {
            if (method.Module.Assembly.IsDynamic)
            {
                return;
            }

            var metadataReader = GetMetadataReader(method.Module.Assembly.Location);

            if (metadataReader == null)
            {
                return;
            }

            var methodToken = MetadataTokens.Handle(method.MetadataToken);

            Debug.Assert(methodToken.Kind == HandleKind.MethodDefinition);

            var handle = ((MethodDefinitionHandle)methodToken).ToDebugInformationHandle();

            if (!handle.IsNil)
            {
                var           methodDebugInfo = metadataReader.GetMethodDebugInformation(handle);
                var           sequencePoints  = methodDebugInfo.GetSequencePoints();
                SequencePoint?bestPointSoFar  = null;

                foreach (var point in sequencePoints)
                {
                    if (point.Offset > IlOffset)
                    {
                        break;
                    }

                    if (point.StartLine != SequencePoint.HiddenLine)
                    {
                        bestPointSoFar = point;
                    }
                }

                if (bestPointSoFar.HasValue)
                {
                    frameInfo.LineNumber = bestPointSoFar.Value.StartLine;
                    frameInfo.FilePath   = metadataReader.GetString(metadataReader.GetDocument(bestPointSoFar.Value.Document).Name);
                }
            }
        }
Пример #16
0
        /// <summary>
        /// Disassembles the current method and returns a list of
        /// disassembled instructions.
        /// </summary>
        /// <returns>The list of disassembled instructions.</returns>
        public DisassembledMethod Disassemble()
        {
            while (ilOffset < il.Length)
            {
                instructionOffset = ilOffset;
                var opCode = ReadOpCode();
                if (debugInformationEnumerator.MoveTo(instructionOffset))
                {
                    CurrentSequencePoint = debugInformationEnumerator.Current;
                }
                if (PrefixHandlers.TryGetValue(opCode, out PrefixHandler prefixHandler))
                {
                    prefixHandler(this);
                }
                else if (OpCodeHandlers.TryGetValue(opCode, out OpCodeHandler opCodeHandler))
                {
                    // Handle operation
                    opCodeHandler(this);

                    // Reset flags
                    flags         = ILInstructionFlags.None;
                    flagsArgument = null;
                }
                else
                {
                    if (NotSupportedILInstruction == null)
                    {
                        throw new NotSupportedException(string.Format(
                                                            ErrorMessages.NotSupportedILInstruction, MethodBase.Name, opCode));
                    }
                    else
                    {
                        NotSupportedILInstruction(this, opCode);
                    }
                }
            }

            return(new DisassembledMethod(MethodBase, instructions, MethodBody.MaxStackSize));
        }
        private SequencePoint?FindClosestSequencePoint(MethodDefinition method, Instruction call)
        {
            var debug = method.DebugInformation;

            if (debug == null || !debug.HasSequencePoints)
            {
                return(null);
            }

            var           points    = debug.SequencePoints;
            SequencePoint?candidate = null;

            foreach (var sequencePoint in debug.SequencePoints)
            {
                if (call.Offset < sequencePoint.Offset)
                {
                    break;
                }
                candidate = sequencePoint;
            }

            return(candidate);
        }
Пример #18
0
 void ILogger.LogError(string message, SequencePoint?sequencePoint)
 {
     WriteError(message, sequencePoint);
 }
Пример #19
0
 public InstructionOptions(Dictionary <object, object> ops, Instruction instr)
 {
     Code          = instr.OpCode.Code;
     Operand       = BodyUtils.ToOperandVM(ops, instr.Operand);
     SequencePoint = instr.SequencePoint;
 }
Пример #20
0
 protected virtual void AddError(string message, SequencePoint?sequencePoint)
 => _log.Error(message, sequencePoint);
Пример #21
0
 /// <summary>
 /// Write a warning to MSBuild and use <paramref name="sequencePoint"/> for the file and line information.
 /// </summary>
 public virtual void WriteWarning(string message, SequencePoint?sequencePoint)
 {
     Guard.AgainstNullAndEmpty(nameof(message), message);
     LogWarningPoint(message, sequencePoint);
 }
Пример #22
0
 public void Warning(string message, SequencePoint?sequencePoint)
 {
 }
Пример #23
0
 public SequencePointMessage(string text, SequencePoint?sequencePoint)
 {
     Text          = text;
     SequencePoint = sequencePoint;
 }
Пример #24
0
 public void Error(string message, SequencePoint?sequencePoint)
 => _log.Error(QualifyMessage(message), sequencePoint);
Пример #25
0
 public void Warning(string message, SequencePoint?sequencePoint)
 => _log.Warning(QualifyMessage(message), sequencePoint);
Пример #26
0
 void ILogger.LogWarning(string message, SequencePoint?sequencePoint)
 {
     WriteWarning(message, sequencePoint);
 }
Пример #27
0
 public void Error(string message, SequencePoint?sequencePoint)
 => _log.Error(message, sequencePoint);
Пример #28
0
 public WeavingException(string message, SequencePoint?sequencePoint = null)
     : base(message)
 {
     SequencePoint = sequencePoint;
 }
Пример #29
0
        internal void AddError(string text, SequencePoint?sequencePoint)
        {
            var message = new SequencePointMessage(text, sequencePoint);

            errors.Add(message);
        }
Пример #30
0
 public void Error(string message, SequencePoint?sequencePoint)
 {
 }