Example #1
0
        private void LoadSourceCodeForTypes(IEnumerable <TypeDefinition> types, ISymbolReader reader)
        {
            foreach (var typeDefinition in types)
            {
                foreach (var method in typeDefinition.Methods.Where(m => m.HasBody))
                {
                    MethodDefinition capturedMethod = method;
                    reader.Read(capturedMethod.Body,
                                o => capturedMethod.Body.Instructions.FirstOrDefault(i => i.Offset >= o));

                    var sourceFiles = method.Body.Instructions.Where(i => i.SequencePoint != null)
                                      .Select(i => i.SequencePoint.Document.Url)
                                      .Distinct();
                    foreach (var sourceFile in sourceFiles)
                    {
                        if (!SourceFiles.ContainsKey(sourceFile) && File.Exists(sourceFile))
                        {
                            SourceFiles.Add(sourceFile, File.ReadAllLines(sourceFile));
                        }
                    }
                }
                if (typeDefinition.NestedTypes != null)
                {
                    LoadSourceCodeForTypes(typeDefinition.NestedTypes, reader);
                }
            }
        }
Example #2
0
        private void ReadMethodBody()
        {
            byte flags = ReadByte();

            switch (flags & 0x3)
            {
            case 0x2:     // tiny
                body.code_size    = flags >> 2;
                body.MaxStackSize = 8;
                ReadCode();
                break;

            case 0x3:     // fat
                Advance(-1);
                ReadFatMethod();
                break;

            default:
                throw new InvalidOperationException();
            }

            ISymbolReader symbol_reader = _reader.module.symbol_reader;

            if (symbol_reader is not null && _method.debug_info is null)
            {
                _method.debug_info = symbol_reader.Read(_method);
            }

            if (_method.debug_info is not null)
            {
                ReadDebugInfo();
            }
        }
Example #3
0
        private void ReadMethodBody()
        {
            byte b = ReadByte();

            switch (b & 3)
            {
            case 2:
                body.code_size    = b >> 2;
                body.MaxStackSize = 8;
                ReadCode();
                break;

            case 3:
                base.Advance(-1);
                ReadFatMethod();
                break;

            default:
                throw new InvalidOperationException();
            }
            ISymbolReader symbol_reader = reader.module.symbol_reader;

            if (symbol_reader != null && method.debug_info == null)
            {
                method.debug_info = symbol_reader.Read(method);
            }
            if (method.debug_info != null)
            {
                ReadDebugInfo();
            }
        }
Example #4
0
        private void ReadMethodBody()
        {
            this.MoveTo(this.method.RVA);
            byte b = base.ReadByte();

            switch (b & 3)
            {
            case 2:
                this.body.code_size    = b >> 2;
                this.body.MaxStackSize = 8;
                this.ReadCode();
                break;

            case 3:
                this.position--;
                this.ReadFatMethod();
                break;

            default:
                throw new InvalidOperationException();
            }
            ISymbolReader symbol_reader = this.reader.module.symbol_reader;

            if (symbol_reader != null)
            {
                Collection <Instruction> instructions = this.body.Instructions;
                symbol_reader.Read(this.body, (int offset) => CodeReader.GetInstruction(instructions, offset));
            }
        }
Example #5
0
        private void ReadMethodBody()
        {
            this.MoveTo(this.method.RVA);
            byte num  = base.ReadByte();
            int  num1 = num & 3;

            if (num1 == 2)
            {
                this.body.code_size    = num >> 2;
                this.body.MaxStackSize = 8;
                this.ReadCode();
            }
            else
            {
                if (num1 != 3)
                {
                    throw new InvalidOperationException();
                }
                this.position--;
                this.ReadFatMethod();
            }
            ISymbolReader symbolReader = this.reader.module.symbol_reader;

            if (symbolReader != null)
            {
                Collection <Instruction> instructions = this.body.Instructions;
                symbolReader.Read(this.body, (int offset) => CodeReader.GetInstruction(instructions, offset));
            }
        }
Example #6
0
        private void ReadMethodsSymbols(TypeDefinition type, ISymbolReader symbol_reader)
        {
            Collection <MethodDefinition> methods = type.Methods;

            for (int i = 0; i < methods.Count; i++)
            {
                MethodDefinition methodDefinition = methods[i];
                if (methodDefinition.HasBody && methodDefinition.token.RID != 0 && methodDefinition.debug_info == null)
                {
                    methodDefinition.debug_info = symbol_reader.Read(methodDefinition);
                }
            }
        }
Example #7
0
        /// <summary>
        /// Retrieves source code segment locations with corresponding offsets in compiled assembly
        /// </summary>
        /// <param name="methodDef">Method definition</param>
        /// <returns>Dictionary: Key is an instruction offset, Value - source code segment location</returns>
        public IDictionary <int, CodeSegment> GetSegmentsByMethod(MethodDefinition methodDef)
        {
            var symbols = new MethodSymbols(methodDef.MetadataToken);

            _reader.Read(symbols);

            return(symbols.Instructions.ToDictionary(
                       inst => inst.Offset,
                       inst => new CodeSegment(
                           inst.SequencePoint.StartColumn,
                           inst.SequencePoint.EndColumn,
                           inst.SequencePoint.StartLine,
                           inst.SequencePoint.EndLine,
                           inst.SequencePoint.Document.Url
                           )
                       ));
        }
Example #8
0
        public ByteBuffer PatchRawMethodBody(MethodDefinition method, CodeWriter writer, out MethodSymbols symbols)
        {
            ByteBuffer byteBuffer = new ByteBuffer();

            symbols             = new MethodSymbols(method.Name);
            this.method         = method;
            this.reader.context = method;
            this.MoveTo(method.RVA);
            byte          b = base.ReadByte();
            MetadataToken zero;

            switch (b & 3)
            {
            case 2:
                byteBuffer.WriteByte(b);
                zero = MetadataToken.Zero;
                symbols.code_size = b >> 2;
                this.PatchRawCode(byteBuffer, symbols.code_size, writer);
                break;

            case 3:
                this.position--;
                this.PatchRawFatMethod(byteBuffer, symbols, writer, out zero);
                break;

            default:
                throw new NotSupportedException();
            }
            ISymbolReader symbol_reader = this.reader.module.symbol_reader;

            if (symbol_reader != null && writer.metadata.write_symbols)
            {
                symbols.method_token    = CodeReader.GetOriginalToken(writer.metadata, method);
                symbols.local_var_token = zero;
                symbol_reader.Read(symbols);
            }
            return(byteBuffer);
        }
Example #9
0
        public ByteBuffer PatchRawMethodBody(MethodDefinition method, CodeWriter writer, out MethodSymbols symbols)
        {
            MetadataToken zero;
            ByteBuffer    byteBuffer = new ByteBuffer();

            symbols             = new MethodSymbols(method.Name);
            this.method         = method;
            this.reader.context = method;
            this.MoveTo(method.RVA);
            byte num  = base.ReadByte();
            int  num1 = num & 3;

            if (num1 == 2)
            {
                byteBuffer.WriteByte(num);
                zero = MetadataToken.Zero;
                symbols.code_size = num >> 2;
                this.PatchRawCode(byteBuffer, symbols.code_size, writer);
            }
            else
            {
                if (num1 != 3)
                {
                    throw new NotSupportedException();
                }
                this.position--;
                this.PatchRawFatMethod(byteBuffer, symbols, writer, out zero);
            }
            ISymbolReader symbolReader = this.reader.module.symbol_reader;

            if (symbolReader != null && writer.metadata.write_symbols)
            {
                symbols.method_token    = CodeReader.GetOriginalToken(writer.metadata, method);
                symbols.local_var_token = zero;
                symbolReader.Read(symbols);
            }
            return(byteBuffer);
        }
Example #10
0
        private void LoadSourceCodeForTypes(IEnumerable<TypeDefinition> types, ISymbolReader reader)
        {
            foreach (var typeDefinition in types)
            {
                foreach (var method in typeDefinition.Methods.Where(m => m.HasBody))
                {
                    MethodDefinition capturedMethod = method;
                    reader.Read(capturedMethod.Body,
                        o => capturedMethod.Body.Instructions.FirstOrDefault(i => i.Offset >= o));

                    var sourceFiles = method.Body.Instructions.Where(i => i.SequencePoint != null)
                        .Select(i => i.SequencePoint.Document.Url)
                        .Distinct();
                    foreach (var sourceFile in sourceFiles)
                    {
                        if (!SourceFiles.ContainsKey(sourceFile) && File.Exists(sourceFile))
                        {
                            SourceFiles.Add(sourceFile, File.ReadAllLines(sourceFile));
                        }
                    }
                }
                if (typeDefinition.NestedTypes != null)
                {
                    LoadSourceCodeForTypes(typeDefinition.NestedTypes, reader);
                }
            }
        }