Files
ILSpy/ICSharpCode.ILSpyX/PdbProvider/DebugInfoUtils.cs
T
Siegfried Pammer 30dbce447e Add --dump-pdb, a cross-platform replacement for Pdb2Xml
The Pdb2Xml command that just left the app was the only way to see a PDB's
contents, and it was Windows-only. Mono.Cecil's symbol readers cover the
Portable and the Windows (native MSF) format in managed code and pick the
right one themselves, so a single dump path serves both on every OS.

Cecil's object model, rather than raw metadata, is what makes this work for
Windows PDBs at all: they have no metadata tables for --dump-table to read.
That also settles the split between the two options - --dump-table stays the
raw row view of a Portable PDB, --dump-pdb is the decoded one.

Neither symbol reader exposes the document list, so the documents are
collected from the sequence points that reference them.

Assisted-by: Claude:claude-opus-5:Claude Code
2026-10-02 07:44:55 +02:00

149 lines
4.9 KiB
C#

// Copyright (c) 2018 Siegfried Pammer
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Runtime.InteropServices;
using ICSharpCode.Decompiler.DebugInfo;
using ICSharpCode.Decompiler.Metadata;
#nullable enable
namespace ICSharpCode.ILSpyX.PdbProvider
{
public static class DebugInfoUtils
{
public static IDebugInfoProvider? LoadSymbols(PEFile module)
{
try
{
// try to open portable pdb file/embedded pdb info:
if (TryOpenPortablePdb(module, out var provider, out var pdbFileName))
{
return new PortableDebugInfoProvider(module.FileName, provider, MetadataReaderOptions.Default, pdbFileName);
}
else
{
// search for pdb in same directory as dll
pdbFileName = Path.Combine(
Path.GetDirectoryName(module.FileName)!,
Path.GetFileNameWithoutExtension(module.FileName) + ".pdb"
);
if (File.Exists(pdbFileName))
{
return new MonoCecilDebugInfoProvider(module, pdbFileName);
}
}
}
catch (Exception ex) when (ex is BadImageFormatException || ex is COMException)
{
// Ignore PDB load errors
}
return null;
}
public static IDebugInfoProvider? FromFile(PEFile module, string pdbFileName)
{
if (string.IsNullOrEmpty(pdbFileName))
return null;
Stream? stream = OpenStream(pdbFileName);
if (stream == null)
return null;
if (stream.Read(buffer, 0, buffer.Length) == LegacyPDBPrefix.Length
&& System.Text.Encoding.ASCII.GetString(buffer) == LegacyPDBPrefix)
{
stream.Position = 0;
return new MonoCecilDebugInfoProvider(module, pdbFileName);
}
else
{
stream.Position = 0;
var provider = MetadataReaderProvider.FromPortablePdbStream(stream);
return new PortableDebugInfoProvider(module.FileName, provider, MetadataReaderOptions.Default, pdbFileName);
}
}
const string LegacyPDBPrefix = "Microsoft C/C++ MSF 7.00";
static readonly byte[] buffer = new byte[LegacyPDBPrefix.Length];
/// <summary>
/// True for a Windows (native MSF) PDB, false for a Portable PDB. Reads from the current
/// position and leaves the stream positioned after the signature.
/// </summary>
public static bool IsWindowsPdb(Stream stream)
{
var signature = new byte[LegacyPDBPrefix.Length];
return stream.Read(signature, 0, signature.Length) == signature.Length
&& System.Text.Encoding.ASCII.GetString(signature) == LegacyPDBPrefix;
}
static bool TryOpenPortablePdb(PEFile module,
[NotNullWhen(true)] out MetadataReaderProvider? provider,
[NotNullWhen(true)] out string? pdbFileName)
{
provider = null;
pdbFileName = null;
var reader = module.Reader;
foreach (var entry in reader.ReadDebugDirectory())
{
if (entry.IsPortableCodeView)
{
return reader.TryOpenAssociatedPortablePdb(module.FileName, OpenStream,
out provider, out pdbFileName);
}
if (entry.Type == DebugDirectoryEntryType.CodeView)
{
string pdbDirectory = Path.GetDirectoryName(module.FileName)!;
pdbFileName = Path.Combine(
pdbDirectory, Path.GetFileNameWithoutExtension(module.FileName) + ".pdb");
Stream? stream = OpenStream(pdbFileName);
if (stream != null)
{
if (stream.Read(buffer, 0, buffer.Length) == LegacyPDBPrefix.Length
&& System.Text.Encoding.ASCII.GetString(buffer) == LegacyPDBPrefix)
{
return false;
}
stream.Position = 0;
provider = MetadataReaderProvider.FromPortablePdbStream(stream);
return true;
}
}
}
return false;
}
static Stream? OpenStream(string fileName)
{
if (!File.Exists(fileName))
return null;
var memory = new MemoryStream();
using (var stream = File.OpenRead(fileName))
stream.CopyTo(memory);
memory.Position = 0;
return memory;
}
}
}