Browse Source

Pdb2Xml command — DEBUG + Windows-only, matches WPF gating

WPF ships Pdb2XmlCommand behind `#if DEBUG && WINDOWS` because
Microsoft.DiaSymReader uses native COM interop. Avalonia parity uses the
same gate, with the package references conditional on the build host being
Windows + Debug, and the consuming command file `#if DEBUG && WINDOWS`.

Assisted-by: Claude:claude-opus-4-7:Claude Code
pull/3755/head
Siegfried Pammer 2 months ago
parent
commit
1f75ad9264
  1. 8
      ILSpy/Commands/FileCommands.cs
  2. 115
      ILSpy/Commands/Pdb2XmlCommand.cs
  3. 15
      ILSpy/ILSpy.csproj

8
ILSpy/Commands/FileCommands.cs

@ -129,12 +129,8 @@ namespace ILSpy.Commands @@ -129,12 +129,8 @@ namespace ILSpy.Commands
// DEBUG-only DisassembleAllCommand also moved to DecompileAllCommand.cs to keep all
// three parallel-decompile/disassemble stress-test commands in one place.
[ExportMainMenuCommand(ParentMenuID = nameof(Resources._File), Header = nameof(Resources.DEBUGDumpPDBAsXML), MenuCategory = nameof(Resources.Open), MenuOrder = 2.6)]
[Shared]
sealed class Pdb2XmlCommand : SimpleCommand
{
public override void Execute(object? parameter) => NotImplementedDialog.Show(Resources.DEBUGDumpPDBAsXML);
}
// DEBUG-only Pdb2XmlCommand moved to its own file with `#if DEBUG && WINDOWS` gating.
// On non-Windows or non-Debug builds the entry simply isn't compiled.
[ExportMainMenuCommand(ParentMenuID = nameof(Resources._File), Header = nameof(Resources._RemoveAssembliesWithLoadErrors), MenuCategory = nameof(Resources.Remove), MenuOrder = 2.6)]
[Shared]

115
ILSpy/Commands/Pdb2XmlCommand.cs

@ -0,0 +1,115 @@ @@ -0,0 +1,115 @@
// Copyright (c) 2026 AlphaSierraPapa for the SharpDevelop Team
//
// 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.
#if DEBUG && WINDOWS
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ICSharpCode.ILSpy.Properties;
using ILSpy.AssemblyTree;
using ILSpy.Docking;
using ILSpy.TextView;
using ILSpy.TreeNodes;
using Microsoft.DiaSymReader.Tools;
namespace ILSpy.Commands
{
/// <summary>
/// DEBUG-only main-menu entry: Dump every selected assembly's PDB as XML in the active
/// decompiler tab via <see cref="PdbToXmlConverter"/>. Windows-only because
/// <c>Microsoft.DiaSymReader</c> uses native COM interop. WPF gates this command
/// identically (<c>#if DEBUG &amp;&amp; WINDOWS</c>).
/// </summary>
[ExportMainMenuCommand(ParentMenuID = nameof(Resources._File), Header = nameof(Resources.DEBUGDumpPDBAsXML), MenuCategory = nameof(Resources.Open), MenuOrder = 2.7)]
[Shared]
sealed class Pdb2XmlCommand : SimpleCommand
{
readonly AssemblyTreeModel assemblyTreeModel;
readonly DockWorkspace dockWorkspace;
[ImportingConstructor]
public Pdb2XmlCommand(AssemblyTreeModel assemblyTreeModel, DockWorkspace dockWorkspace)
{
this.assemblyTreeModel = assemblyTreeModel;
this.dockWorkspace = dockWorkspace;
}
public override bool CanExecute(object? parameter)
{
var selected = assemblyTreeModel.SelectedItems;
return selected?.Count > 0
&& selected.All(n => n is AssemblyTreeNode asm && !asm.LoadedAssembly.HasLoadError);
}
public override void Execute(object? parameter)
{
var nodes = assemblyTreeModel.SelectedItems.OfType<AssemblyTreeNode>().ToArray();
if (nodes.Length == 0)
return;
_ = ExecuteAsync(nodes);
}
async Task ExecuteAsync(AssemblyTreeNode[] nodes)
{
try
{
var options = PdbToXmlOptions.IncludeEmbeddedSources
| PdbToXmlOptions.IncludeMethodSpans
| PdbToXmlOptions.IncludeTokens;
var output = await dockWorkspace.RunWithCancellation(token => Task.Run(() => {
var output = new AvaloniaEditTextOutput { Title = "PDB as XML", SyntaxExtensionOverride = ".xml" };
var writer = new TextOutputWriter(output);
foreach (var node in nodes)
{
var pdbFileName = Path.ChangeExtension(node.LoadedAssembly.FileName, ".pdb");
if (!File.Exists(pdbFileName))
continue;
using var pdbStream = File.OpenRead(pdbFileName);
using var peStream = File.OpenRead(node.LoadedAssembly.FileName);
PdbToXmlConverter.ToXml(writer, pdbStream, peStream, options);
}
return output;
}, token), "Dumping PDB as XML…");
dockWorkspace.ShowText(output);
}
catch (System.OperationCanceledException) { }
}
}
/// <summary>
/// Adapter that lets <see cref="PdbToXmlConverter"/>'s TextWriter API write into our
/// <see cref="AvaloniaEditTextOutput"/> sink.
/// </summary>
internal sealed class TextOutputWriter(ICSharpCode.Decompiler.ITextOutput output) : System.IO.TextWriter
{
public override System.Text.Encoding Encoding => System.Text.Encoding.UTF8;
public override void Write(char value) => output.Write(value);
public override void Write(string? value)
{
if (value != null)
output.Write(value);
}
public override void WriteLine() => output.WriteLine();
}
}
#endif

15
ILSpy/ILSpy.csproj

@ -77,6 +77,18 @@ @@ -77,6 +77,18 @@
<ProjectReference Include="..\ICSharpCode.BamlDecompiler\ICSharpCode.BamlDecompiler.csproj" />
</ItemGroup>
<!-- DEBUG-only Pdb2Xml command requires Microsoft.DiaSymReader, which uses native COM
interop and is Windows-only. The PackageReference is gated on the build host
being Windows AND the configuration being Debug; the consuming command file is
additionally `#if DEBUG && WINDOWS` so the symbols only resolve when both
conditions are true. Matches WPF's `#if DEBUG && WINDOWS` gating of the same
feature. -->
<ItemGroup Condition="'$(Configuration)' == 'Debug' AND $([MSBuild]::IsOSPlatform('Windows'))">
<PackageReference Include="Microsoft.DiaSymReader.Converter.Xml" />
<PackageReference Include="Microsoft.DiaSymReader" />
<PackageReference Include="Microsoft.DiaSymReader.Native" />
</ItemGroup>
<!-- The Debug Steps pane is a DEBUG-only diagnostic. The code-behind and viewmodel
are wrapped in `#if DEBUG`, so the C# type vanishes under Release; but Avalonia
compiles every .axaml file regardless of preprocessor state, and the XamlIl
@ -85,4 +97,7 @@ @@ -85,4 +97,7 @@
<ItemGroup Condition="'$(Configuration)' != 'Debug'">
<AvaloniaXaml Remove="Views\DebugSteps.axaml" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug' AND $([MSBuild]::IsOSPlatform('Windows'))">
<DefineConstants>$(DefineConstants);WINDOWS</DefineConstants>
</PropertyGroup>
</Project>

Loading…
Cancel
Save