// internal for testing internal static bool Convert(Args args) { bool success = true; var reporter = args.Verbose ? new Action <PdbDiagnostic>(d => { Console.Error.WriteLine(d.ToString(CultureInfo.CurrentCulture)); success = false; }) : null; var converter = new PdbConverter(reporter); using (var peStream = new FileStream(args.PEFilePath, FileMode.Open, FileAccess.Read)) using (var peReader = new PEReader(peStream, PEStreamOptions.LeaveOpen)) { string portablePdbFileCandidate = null; if (args.PdbFilePathOpt != null) { Debug.Assert(!args.Extract); Debug.Assert(args.OutPdbFilePathOpt != null); using (var srcPdbStreamOpt = OpenFileForRead(args.PdbFilePathOpt)) { var outPdbStream = new MemoryStream(); if (PdbConverter.IsPortable(srcPdbStreamOpt)) { converter.ConvertPortableToWindows(peReader, srcPdbStreamOpt, outPdbStream, args.Options); } else { converter.ConvertWindowsToPortable(peReader, srcPdbStreamOpt, outPdbStream); } WriteAllBytes(args.OutPdbFilePathOpt, outPdbStream); } } else if (peReader.TryOpenAssociatedPortablePdb(args.PEFilePath, path => File.OpenRead(portablePdbFileCandidate = path), out var pdbReaderProvider, out _)) { using (pdbReaderProvider) { var pdbReader = pdbReaderProvider.GetMetadataReader(); if (args.Extract) { string pdbPath = args.OutPdbFilePathOpt ?? GetPdbPathFromCodeViewEntry(peReader, args.PEFilePath, portable: true) ?? Path.ChangeExtension(args.PEFilePath, "pdb"); File.WriteAllBytes(pdbPath, ReadAllBytes(pdbReader)); } else { Debug.Assert(args.OutPdbFilePathOpt != null); var dstPdbStream = new MemoryStream(); converter.ConvertPortableToWindows(peReader, pdbReader, dstPdbStream, args.Options); WriteAllBytes(args.OutPdbFilePathOpt, dstPdbStream); } } }
public static int Main(string[] args) { if (args.Length < 1) { Console.Error.WriteLine(Resources.Pdb2PdbUsage); return(1); } string peFile = args[0]; var srcPdb = GetArgumentValue(args, "/src:") ?? Path.ChangeExtension(peFile, "pdb"); var dstPdb = GetArgumentValue(args, "/dst:") ?? Path.ChangeExtension(peFile, "pdb2"); if (!File.Exists(peFile)) { Console.Error.WriteLine(string.Format(Resources.FileNotFound, peFile)); return(2); } if (!File.Exists(srcPdb)) { Console.Error.WriteLine(string.Format(Resources.FileNotFound, srcPdb)); return(2); } try { using (var peStream = new FileStream(peFile, FileMode.Open, FileAccess.Read)) using (var srcPdbStream = new FileStream(srcPdb, FileMode.Open, FileAccess.Read)) using (var dstPdbStream = new FileStream(dstPdb, FileMode.Create, FileAccess.ReadWrite)) { PdbConverter.Convert(peStream, srcPdbStream, dstPdbStream); } } catch (Exception e) { Console.Error.WriteLine(e.Message); return(3); } return(0); }
public static int Main(string[] args) { if (args.Length < 1) { Console.WriteLine("Usage: Pdb2Pdb <PE file> [/src:<PDB path>] [/dst:<Portable PDB path>]"); return(1); } string peFile = args[0]; var srcPdb = GetArgumentValue(args, "/src:") ?? Path.ChangeExtension(peFile, "pdb"); var dstPdb = GetArgumentValue(args, "/dst:") ?? Path.ChangeExtension(peFile, "pdbx"); if (!File.Exists(peFile)) { Console.WriteLine($"PE file not: {peFile}"); return(1); } if (!File.Exists(srcPdb)) { Console.WriteLine($"PDB file not: {srcPdb}"); return(1); } using (var peStream = new FileStream(peFile, FileMode.Open, FileAccess.Read)) { using (var nativePdbStream = new FileStream(srcPdb, FileMode.Open, FileAccess.Read)) { using (var portablePdbStream = new FileStream(dstPdb, FileMode.Create, FileAccess.ReadWrite)) { PdbConverter.Convert(peStream, nativePdbStream, portablePdbStream); } } } return(0); }