Exemple #1
0
        public void TestSingleStream()
        {
            string filePath = Path.GetTempFileName();

            using (Stream strm = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite)) {
                MSFWriter wr = new MSFWriter(strm);
                wr.PageSize = 4096;

                byte[] sourceData = File.ReadAllBytes(Assembly
                                                      .GetExecutingAssembly()
                                                      .Location);

                wr.StreamTable.AddStream(sourceData);
                wr.Commit();

                strm.Position = 0;
                PDBFile rdr = new PDBFile(strm);
                Assert.AreEqual(rdr.Streams.Count(), 1);

                byte[] streamData = rdr.Streams.First();
                Assert.AreEqual(sourceData, streamData);
            }

            File.Delete(filePath);
        }
Exemple #2
0
        public void Write(PDBFile pdb, Stream stream)
        {
            TypeDataWriter w = CreateWriter(LeafType.LF_REAL32);

            w.WriteSingle(Value);
            w.WriteHeader();
        }
Exemple #3
0
        private static ProteinGraph TrimGraph(PDBFile pdbFile, ProteinGraph proteinGraph)
        {
            var trimmedGraph = new ProteinGraph();
            Dictionary <GWNode <ResidueNodeData, SimpleEdgeData, ProteinGraphData>, GWNode <ResidueNodeData, SimpleEdgeData, ProteinGraphData> > dict = new Dictionary <GWNode <ResidueNodeData, SimpleEdgeData, ProteinGraphData>, GWNode <ResidueNodeData, SimpleEdgeData, ProteinGraphData> >();

            foreach (var node in proteinGraph.Nodes)
            {
                if (!node.Data.IsCore)
                {
                    var newNode = trimmedGraph.CreateNode();
                    newNode.Data = node.Data;
                    dict.Add(node, newNode);
                }
                else
                {
                }
            }

            foreach (var edge in proteinGraph.Edges)
            {
                if (!edge.Head.Data.IsCore && !edge.Foot.Data.IsCore)
                {
                    var newEdge = trimmedGraph.CreateEdge(dict[edge.Head], dict[edge.Foot]);
                    newEdge.Data = edge.Data;
                }
            }

            return(trimmedGraph);
        }
        public string ComputeIndexKey(string path, Stream fileStream)
        {
            try
            {
                if (Path.GetExtension(path) != ".pdb")
                {
                    return(null);
                }
                PDBFile pdb = new PDBFile(new StreamAddressSpace(fileStream));
                if (!pdb.Header.IsMagicValid.Check())
                {
                    return(null);
                }

                string        filename = Path.GetFileName(path).ToLowerInvariant();
                StringBuilder key      = new StringBuilder();
                key.Append(filename);
                key.Append("/");
                key.Append(pdb.Signature.ToString().Replace("-", "").ToLowerInvariant());
                key.Append(pdb.Age.ToString("x").ToLowerInvariant());
                key.Append("/");
                key.Append(filename);
                return(key.ToString());
            }
            catch (InputParsingException)
            {
                return(null);
            }
        }
        public PDBFileKeyGenerator(ITracer tracer, SymbolStoreFile file)
            : base(tracer)
        {
            StreamAddressSpace dataSource = new StreamAddressSpace(file.Stream);

            _pdbFile = new PDBFile(dataSource);
            _path    = file.FileName;
        }
Exemple #6
0
        public void Write(PDBFile pdb, Stream stream)
        {
            TypeDataWriter w = CreateWriter(LeafType.LF_UNION);

            w.WriteUInt16(NumberOfElements);
            w.Write <TypeProperties>(Properties);
            w.WriteIndexedType(FieldType);
            w.WriteVaryingType(StructSize);
            w.WriteCString(Name);
        }
Exemple #7
0
 public void CheckIndexingInfo()
 {
     using (Stream s = File.OpenRead("TestBinaries/HelloWorld.pdb"))
     {
         StreamAddressSpace fileContent = new StreamAddressSpace(s);
         PDBFile            pdb         = new PDBFile(fileContent);
         Assert.True(pdb.Header.IsMagicValid.Check());
         Assert.Equal((uint)1, pdb.Age);
         Assert.Equal(Guid.Parse("99891B3E-D7AE-4C3B-ABFF-8A2B4A9B0C43"), pdb.Signature);
     }
 }
Exemple #8
0
        public void Write(PDBFile pdb, Stream stream)
        {
            TypeDataWriter w = new TypeDataWriter(pdb, stream, LeafType.LF_ARGLIST);

            w.WriteUInt16(NumberOfArguments);
            w.WriteUInt16(0x00);

            foreach (ILeafContainer leaf in ArgumentTypes)
            {
                w.WriteIndexedType(leaf);
            }

            w.WriteLeafHeader();
        }
Exemple #9
0
        private static ProteinGraph SetIsCore(PDBFile pdbFile)
        {
            var rasaRequest = new RequestRasa(pdbFile);

            rasaRequest.RequestInDefaultContext();

            foreach (var rasa in rasaRequest.Rasavalues)
            {
                rasa.Key.IsCore = rasa.Value <= 0.15;
            }
            var proteinGraph = CreateProteinGraph.Do(pdbFile, 7.0, ResidueNodeCenterDefinition.CAlpha).Values.First();

            return(proteinGraph);
        }
        private static bool TryGetPDBIndex(Stream stream, out string uuid)
        {
            uuid = null;
            try
            {
                PDBFile pdb = new PDBFile(new StreamAddressSpace(stream));

                if (!pdb.Header.IsMagicValid.Check())
                {
                    return(false);
                }

                uuid = pdb.Signature.ToString().Replace("-", "").ToLowerInvariant() + pdb.Age.ToString("x").ToLowerInvariant();

                return(true);
            }
            catch (InputParsingException)
            {
                return(false);
            }
        }
Exemple #11
0
        public void Write(PDBFile pdb, Stream stream)
        {
            SymbolDataWriter w = CreateWriter(SymbolType.S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE);

            w.WriteHeader();
        }
Exemple #12
0
        static void Main(string[] args)
        {
            ParseArguments(args);

            if (PdbFilePath == null)
            {
                Console.Error.WriteLine(@"PDBSharp.Dumper
  Usage: PDBSharp.Dumper <options> <file.pdb>
    [-dump]            Save individual PDB Streams to files
    [-dump-modules]    Verbose output for DBI Modules
    [-dump-leaves]     Verbose output for TPI Leaves
    [-dump-syms]       Verbose output for DBI Symbols
    [-print]           Extract and print type definitions");
                Environment.Exit(1);
            }

#if PERF
            Console.WriteLine("Starting...");
            Stopwatch sw = new Stopwatch();
            sw.Start();
#endif

            PDBFile           pdb = PDBFile.Open(PdbFilePath);
            IServiceContainer sc  = pdb.Services;

            DBIReader         dbi         = sc.GetService <DBIReader>();
            TPIReader         tpi         = sc.GetService <TPIReader>();
            StreamTableReader streamTable = sc.GetService <StreamTableReader>();

            if (OptPrintDecls)
            {
                var        tree = new GraphBuilder(sc).Build();
                CodeWriter cw   = new CodeWriter(tree);
#if !PERF
                cw.Write(Console.Out);
#endif
            }

            if (OptDumpLeaves)
            {
                pdb.OnTpiInit += Pdb_OnTpiInit;
            }
            if (OptDumpModules || OptDumpSymbols)
            {
                pdb.OnDbiInit += Pdb_OnDbiInit;
            }

            if (OptDumpStreams)
            {
                DirectoryInfo dumpDir = Directory.CreateDirectory(Path.GetFileNameWithoutExtension(PdbFilePath));
                for (int i = 1; i < streamTable.NumStreams; i++)
                {
                    string dumpPath = Path.Combine(dumpDir.ToString(), $"stream{i}.bin");

                    byte[] stream = streamTable.GetStream(i);
                    File.WriteAllBytes(dumpPath, stream);
                }
            }

            /*
             * foreach(var type in ctx.TpiReader.Types) {
             *      //Console.WriteLine(type);
             * }*/

            TPIHashReader      tpiHash      = sc.GetService <TPIHashReader>();
            UdtNameTableReader udtNameTable = sc.GetService <UdtNameTableReader>();
            TypeResolver       resolver     = sc.GetService <TypeResolver>();
            if (tpiHash != null && udtNameTable != null)
            {
                foreach (var pair in tpiHash.NameIndexToTypeIndex)
                {
                    string         name = udtNameTable.GetString(pair.Key);
                    ILeafContainer leaf = resolver.GetTypeByIndex(pair.Value);
#if !PERF
                    Console.WriteLine($"=> {name} [NI={pair.Key}] [TI={pair.Value}]");
                    Console.WriteLine(leaf.Data.GetType().Name);
#endif
                }
            }

            foreach (var contrib in dbi.SectionContribs.SectionContribs)
            {
#if !PERF
                ObjectDumper.Dump(contrib);
#endif
            }

            DebugReader debug = dbi.DebugInfo;
            if (debug != null && debug.FPO != null)
            {
                foreach (var frame in debug.FPO.Frames)
                {
#if !PERF
                    ObjectDumper.Dump(frame);
#endif
                }
            }

            foreach (var container in dbi.Modules)
            {
#if !PERF
                Console.WriteLine($"[MODULE => {container.Info.ModuleName}]");
                Console.WriteLine($"[OBJECT => {container.Info.ObjectFileName}]");
                Console.WriteLine($"[SRC    => {container.Info.SourceFileName}]");
                if (container.Module != null)
                {
                    Console.WriteLine($"[TYPE   => {container.Module.GetType().Name}");
                }
                Console.WriteLine();
#endif

                IModule mod = container.Module;
                if (mod != null)
                {
                    foreach (var sym in mod.Symbols)
                    {
#if !PERF
                        Console.WriteLine(sym);
#endif
                    }
                }
            }

            foreach (var type in tpi.Types)
            {
#if !PERF
                Console.WriteLine(type);
#endif
            }

#if !PERF
            Console.WriteLine("Press Enter to continue...");
            Console.ReadLine();
#else
            sw.Stop();
            Console.WriteLine($"Finished in {sw.Elapsed.TotalSeconds} seconds");
#endif
        }
        // reference: https://github.com/NuGet/NuGet.Jobs/blob/26c23697fee363d3133171f71e129cda9b5a3707/src/Validation.Symbols/SymbolsValidatorService.cs#L186

        private bool VerifyPdbChecksums()
        {
            // Nothing to verify as the pdb is inside the PE file
            if (_pdbType == PdbType.Embedded)
            {
                return(true);
            }

            if (_pdbType == PdbType.Portable)
            {
                if (_peReader == null)
                {
                    return(false);
                }

                var checksumRecords = _peReader.ReadDebugDirectory()
                                      .Where(entry => entry.Type == DebugDirectoryEntryType.PdbChecksum)
                                      .Select(_peReader.ReadPdbChecksumDebugDirectoryData)
                                      .ToList();

                if (checksumRecords.Count == 0)
                {
                    return(false);
                }

                var hashes = new Dictionary <string, byte[]>();

                if (_reader.DebugMetadataHeader == null)
                {
                    return(false);
                }

                var idOffset = _reader.DebugMetadataHeader.IdStartOffset;

                foreach (var checksumRecord in checksumRecords)
                {
                    if (!hashes.TryGetValue(checksumRecord.AlgorithmName, out var hash))
                    {
                        var han = new HashAlgorithmName(checksumRecord.AlgorithmName);
                        using (var hashAlg = IncrementalHash.CreateHash(han))
                        {
                            hashAlg.AppendData(_pdbBytes !, 0, idOffset);
                            hashAlg.AppendData(new byte[20]);
                            var offset = idOffset + 20;
                            var count  = _pdbBytes !.Length - offset;
                            hashAlg.AppendData(_pdbBytes !, offset, count);
                            hash = hashAlg.GetHashAndReset();
                        }
                        hashes.Add(checksumRecord.AlgorithmName, hash);
                    }
                    if (checksumRecord.Checksum.ToArray().SequenceEqual(hash))
                    {
                        // found the right checksum
                        return(true);
                    }
                }

                // Not found any checksum record that matches the PDB.
                return(false);
            }

            // Deal with Windows PDB's

            using var pdbBytesStream = new MemoryStream(_pdbBytes !);
            var pdbFile = new PDBFile(new StreamAddressSpace(pdbBytesStream));

            using var peBytesStream = new MemoryStream(_peBytes !);
            var peFile = new PEFile(new StreamAddressSpace(peBytesStream));

            var pdb = peFile.Pdbs.FirstOrDefault(p => p.Signature == pdbFile.Signature && p.Age == pdbFile.Age);

            if (pdb != null)
            {
                return(true);
            }

            return(false);
        }