-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate PDBs locally if needed (#22)
* generate PDBs locally if needed * integrate ILSpy for decompiling DLLs if needed * if PDB doesn't exist already, use ILSpy to generate it * bring PDB writer from ILSpy decompiler here * continue if generation fails * fix few null dereference issues
- Loading branch information
Showing
4 changed files
with
718 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using ICSharpCode.Decompiler; | ||
using ICSharpCode.Decompiler.CSharp; | ||
using ICSharpCode.Decompiler.CSharp.ProjectDecompiler; | ||
using ICSharpCode.Decompiler.DebugInfo; | ||
using ICSharpCode.Decompiler.Disassembler; | ||
using ICSharpCode.Decompiler.Metadata; | ||
using ICSharpCode.Decompiler.Solution; | ||
using ICSharpCode.Decompiler.TypeSystem; | ||
using ICSharpCode.ILSpyX.PdbProvider; | ||
|
||
using System.Reflection.Metadata; | ||
using System.Reflection.PortableExecutable; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace DotNetAstGen | ||
{ | ||
public class PDBGenerator | ||
{ | ||
public static ILoggerFactory? LoggerFactory; | ||
private static ILogger<PDBGenerator>? _logger; | ||
|
||
private static DecompilerSettings GetSettings(PEFile module) | ||
{ | ||
return new DecompilerSettings(ICSharpCode.Decompiler.CSharp.LanguageVersion.Latest) | ||
{ | ||
ThrowOnAssemblyResolveErrors = false, | ||
RemoveDeadCode = false, | ||
RemoveDeadStores = false, | ||
UseSdkStyleProjectFormat = WholeProjectDecompiler.CanUseSdkStyleProjectFormat(module), | ||
UseNestedDirectoriesForNamespaces = false, | ||
}; | ||
} | ||
private static CSharpDecompiler GetDecompiler(string assemblyFileName) | ||
{ | ||
var module = new PEFile(assemblyFileName); | ||
var resolver = new UniversalAssemblyResolver(assemblyFileName, false, module.Metadata.DetectTargetFrameworkId()); | ||
return new CSharpDecompiler(assemblyFileName, resolver, GetSettings(module)); | ||
} | ||
public void GeneratePDBforDLLFile(string dllFileName, string pdbFileName) | ||
{ | ||
using ILoggerFactory factory = Microsoft.Extensions.Logging.LoggerFactory.Create(builder => builder.AddConsole()); | ||
_logger = factory.CreateLogger<PDBGenerator>(); | ||
|
||
var module = new PEFile(dllFileName, | ||
new FileStream(dllFileName, FileMode.Open, FileAccess.Read), | ||
PEStreamOptions.PrefetchEntireImage, | ||
metadataOptions: MetadataReaderOptions.None); | ||
|
||
if (!PDBWriter.HasCodeViewDebugDirectoryEntry(module)) | ||
{ | ||
_logger?.LogWarning($"Cannot create PDB file for {dllFileName}, because it does not contain a PE Debug Directory Entry of type 'CodeView'. Skipping..."); | ||
} | ||
else | ||
{ | ||
using (FileStream stream = new FileStream(pdbFileName, FileMode.OpenOrCreate, FileAccess.Write)) | ||
{ | ||
var decompiler = GetDecompiler(dllFileName); | ||
PDBWriter.WritePdb(module, decompiler, GetSettings(module), stream); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.