コード例 #1
0
ファイル: PortablePdbWriter.cs プロジェクト: haagenson/ILSpy
        public static void WritePdb(PEFile file, CSharpDecompiler decompiler, DecompilerSettings settings, Stream targetStream)
        {
            MetadataBuilder metadata         = new MetadataBuilder();
            MetadataReader  reader           = file.Metadata;
            var             entrypointHandle = MetadataTokens.MethodDefinitionHandle(file.Reader.PEHeaders.CorHeader.EntryPointTokenOrRelativeVirtualAddress);

            var hasher             = SHA256.Create();
            var sequencePointBlobs = new Dictionary <MethodDefinitionHandle, (DocumentHandle Document, BlobHandle SequencePoints)>();
            var importScopeBlobs   = new Dictionary <MethodDefinitionHandle, (DocumentHandle Document, BlobHandle ImportScope)>();
            var emptyList          = new List <SequencePoint>();

            foreach (var handle in reader.GetTopLevelTypeDefinitions())
            {
                var type = reader.GetTypeDefinition(handle);
                var name = metadata.GetOrAddDocumentName("ILSpy_Generated_" + type.GetFullTypeName(reader) + "_" + Guid.NewGuid() + ".cs");
                var ast  = decompiler.DecompileTypes(new[] { handle });
                ast.InsertChildAfter(null, new Comment(" PDB and source generated by ICSharpCode.Decompiler " + decompilerVersion.FileVersion), Roles.Comment);
                var sourceText     = SyntaxTreeToString(ast, settings);
                var sequencePoints = decompiler.CreateSequencePoints(ast).ToDictionary(sp => (MethodDefinitionHandle)sp.Key.Method.MetadataToken, sp => sp.Value);
                var sourceCheckSum = hasher.ComputeHash(Encoding.UTF8.GetBytes(sourceText));
                var sourceBlob     = WriteSourceToBlob(metadata, sourceText);

                var document = metadata.AddDocument(name,
                                                    hashAlgorithm: metadata.GetOrAddGuid(HashAlgorithmSHA256),
                                                    hash: metadata.GetOrAddBlob(sourceCheckSum),
                                                    language: metadata.GetOrAddGuid(CSharpLanguageGuid));

                metadata.AddCustomDebugInformation(document, metadata.GetOrAddGuid(DebugInfoEmbeddedSource), sourceBlob);

                foreach (var method in type.GetMethods())
                {
                    var methodDef = reader.GetMethodDefinition(method);
                    if (!sequencePoints.TryGetValue(method, out var points))
                    {
                        points = emptyList;
                    }
                    int             localSignatureRowId;
                    MethodBodyBlock methodBody;
                    if (methodDef.RelativeVirtualAddress != 0)
                    {
                        methodBody          = file.Reader.GetMethodBody(methodDef.RelativeVirtualAddress);
                        localSignatureRowId = methodBody.LocalSignature.IsNil ? 0 : MetadataTokens.GetRowNumber(methodBody.LocalSignature);
                    }
                    else
                    {
                        methodBody          = null;
                        localSignatureRowId = 0;
                    }
                    if (points.Count == 0)
                    {
                        sequencePointBlobs.Add(method, (default, default));
コード例 #2
0
 public override void Disassemble(PEFile module, MethodDefinitionHandle handle)
 {
     try {
         var csharpOutput            = new StringWriter();
         CSharpDecompiler decompiler = CreateDecompiler(module, options);
         var st = decompiler.Decompile(handle);
         WriteCode(csharpOutput, options.DecompilerSettings, st, decompiler.TypeSystem);
         var mapping = decompiler.CreateSequencePoints(st).FirstOrDefault(kvp => (kvp.Key.MoveNextMethod ?? kvp.Key.Method).MetadataToken == handle);
         this.sequencePoints = mapping.Value ?? (IList <SequencePoint>)EmptyList <SequencePoint> .Instance;
         this.codeLines      = csharpOutput.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
         base.Disassemble(module, handle);
     } finally {
         this.sequencePoints = null;
         this.codeLines      = null;
     }
 }
コード例 #3
0
            public override void Disassemble(MethodBody body)
            {
                var method = body.Method;

                try {
                    var csharpOutput            = new StringWriter();
                    CSharpDecompiler decompiler = CreateDecompiler(method.Module, options);
                    var st = decompiler.Decompile(method);
                    WriteCode(csharpOutput, options.DecompilerSettings, st, decompiler.TypeSystem);
                    var mapping = decompiler.CreateSequencePoints(st).FirstOrDefault(kvp => kvp.Key.CecilMethod == method);
                    this.sequencePoints = mapping.Value ?? (IList <IL.SequencePoint>)EmptyList <IL.SequencePoint> .Instance;
                    this.codeLines      = csharpOutput.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
                    base.Disassemble(body);
                } finally {
                    this.sequencePoints = null;
                    this.codeLines      = null;
                }
            }