Esempio n. 1
0
        internal static IEnumerable <Sharp> GetSource(ClrMethod method, ILToNativeMap map)
        {
            var sourceLocation = method.GetSourceLocation(map.ILOffset);

            if (sourceLocation == null)
            {
                yield break;
            }

            for (int line = sourceLocation.LineNumber; line <= sourceLocation.LineNumberEnd; ++line)
            {
                var sourceLine = ReadSourceLine(sourceLocation.FilePath, line);
                if (sourceLine == null)
                {
                    continue;
                }

                var text = sourceLine + Environment.NewLine
                           + GetSmartPointer(sourceLine,
                                             start: line == sourceLocation.LineNumber ? sourceLocation.ColStart - 1 : default(int?),
                                             end: line == sourceLocation.LineNumberEnd ? sourceLocation.ColEnd - 1 : default(int?));

                yield return(new Sharp
                {
                    Text = text,
                    InstructionPointer = map.StartAddress,
                    FilePath = sourceLocation.FilePath,
                    LineNumber = line
                });
            }
        }
Esempio n. 2
0
        static IEnumerable <Sharp> GetSource(ClrMethod method, ILToNativeMap map)
        {
            var sourceLocation = method.GetSourceLocation(map.ILOffset);

            if (sourceLocation == null)
            {
                yield break;
            }

            for (int line = sourceLocation.LineNumber; line <= sourceLocation.LineNumberEnd; ++line)
            {
                var sourceLine = ReadSourceLine(sourceLocation.FilePath, line);

                if (sourceLine != null)
                {
                    yield return(new Sharp
                    {
                        TextRepresentation = sourceLine + Environment.NewLine + new string(' ', sourceLocation.ColStart - 1) + new string('^', sourceLocation.ColEnd - sourceLocation.ColStart),
                        FilePath = sourceLocation.FilePath,
                        LineNumber = line
                    });
                }
            }
        }
Esempio n. 3
0
        private void DisassembleMethod(ClrMethod method)
        {
            var    module   = method.Type.Module;
            string fileName = module.FileName;

            AssemblyDefinition assembly = AssemblyDefinition.ReadAssembly(fileName);
            TypeDefinition     type     = assembly.MainModule.GetType(method.Type.Name);

            MethodDefinition methodDef = type.Methods.Single(
                m => m.MetadataToken.ToUInt32() == method.MetadataToken);

            _context.WriteLine("{0}", method.GetFullSignature());

            if (method.ILOffsetMap == null)
            {
                return;
            }

            var mapByOffset = (from map in method.ILOffsetMap
                               where map.ILOffset >= 0 // prolog is -2, epilog -3
                               where map.StartAddress <= map.EndAddress
                               orderby map.ILOffset
                               select map).ToArray();

            if (mapByOffset.Length == 0)
            {
                // The method doesn't have an offset map. Just print the whole thing.
                PrintInstructions(methodDef.Body.Instructions);
            }

            // This is the prologue, looks like it's always there, but it could
            // also be the only thing that's in the method
            var prologue = method.ILOffsetMap[0];

            if (prologue.ILOffset == -2) // -2 is a magic number for prologue
            {
                DisassembleNative(prologue);
            }

            for (int i = 0; i < mapByOffset.Length; ++i)
            {
                var map = mapByOffset[i];
                IEnumerable <Instruction> instructions;
                if (i == mapByOffset.Length - 1)
                {
                    instructions = methodDef.Body.Instructions.Where(
                        instr => instr.Offset >= map.ILOffset);
                }
                else
                {
                    instructions = methodDef.Body.Instructions.Where(
                        instr => instr.Offset >= map.ILOffset &&
                        instr.Offset < mapByOffset[i + 1].ILOffset);
                }

                var sourceLocation = method.GetSourceLocation(map.ILOffset);
                if (sourceLocation != null)
                {
                    _context.WriteLine("{0} {1}-{2}:{3}-{4}", sourceLocation.FilePath,
                                       sourceLocation.LineNumber, sourceLocation.LineNumberEnd,
                                       sourceLocation.ColStart, sourceLocation.ColEnd);
                    for (int line = sourceLocation.LineNumber; line <= sourceLocation.LineNumberEnd; ++line)
                    {
                        var sourceLine = ReadSourceLine(sourceLocation.FilePath, line);

                        if (sourceLine != null)
                        {
                            _context.WriteLine(sourceLine);
                            _context.WriteLine(new string(' ', sourceLocation.ColStart - 1) + new string('^', sourceLocation.ColEnd - sourceLocation.ColStart));
                        }
                    }
                }
                PrintInstructions(instructions);
                DisassembleNative(map);
            }

            var epilogue = method.ILOffsetMap[method.ILOffsetMap.Length - 1];

            if (epilogue.ILOffset == -3) // -3 is a magic number for epilog
            {
                DisassembleNative(epilogue);
            }
        }