Skip to content

Commit c0ea514

Browse files
authored
Allow crashinfo to work correctly on dotnet-dump and hide on live attach (dotnet#5616)
This modifies the `crashinfo` command to only load if the ICrashInfoModuleService loads, which means that the data target is a dump either hosted by a debugger or dotnet-dump. The EntryPoint module on windows might not be the first module loaded, so it will locate the first module that has an EXE file extension.
1 parent 35a3ffd commit c0ea514

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/Microsoft.Diagnostics.DebugServices.Implementation/ModuleServiceFromDataReader.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Immutable;
77
using System.Diagnostics;
88
using System.Linq;
9+
using System.Runtime.InteropServices;
910
using Microsoft.Diagnostics.Runtime;
1011

1112
namespace Microsoft.Diagnostics.DebugServices.Implementation
@@ -161,5 +162,29 @@ protected override Dictionary<ulong, IModule> GetModulesInner()
161162
}
162163
return modules;
163164
}
165+
166+
public override IModule EntryPointModule
167+
{
168+
get
169+
{
170+
foreach (IModule module in ((IModuleService)this).EnumerateModules())
171+
{
172+
if (Target.OperatingSystem == OSPlatform.Windows)
173+
{
174+
// The entry point module is not necessarily the first module in the sorted list of modules.
175+
if (module.FileName?.EndsWith(".exe", StringComparison.OrdinalIgnoreCase) == true)
176+
{
177+
return module;
178+
}
179+
}
180+
else
181+
{
182+
// On non-Windows, assume the entry point module is the first module.
183+
return module;
184+
}
185+
}
186+
return null;
187+
}
188+
}
164189
}
165190
}

src/Microsoft.Diagnostics.ExtensionCommands/CrashInfoCommand.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@ public class CrashInfoCommand : CommandBase
2222
[Option(Name = "--moduleEnumerationScheme", Aliases = new string[] { "-e" }, Help = "Enables searching modules for the NativeAOT crashinfo data. Default is None")]
2323
public ModuleEnumerationScheme ModuleEnumerationScheme { get; set; } = ModuleEnumerationScheme.None;
2424

25+
[FilterInvoke(Message = "This command is only supported with dumps.")]
26+
public static bool FilterInvoke([ServiceImport(Optional = true)] ICrashInfoModuleService crashInfoFactory) => crashInfoFactory != null;
27+
2528
public override void Invoke()
2629
{
27-
ICrashInfoService crashInfo = CrashInfo ?? CrashInfoFactory.Create(ModuleEnumerationScheme);
30+
ICrashInfoService crashInfo = CrashInfo ?? CrashInfoFactory?.Create(ModuleEnumerationScheme);
2831
if (crashInfo == null)
2932
{
3033
throw new DiagnosticsException("No crash info to display");

0 commit comments

Comments
 (0)