How to enumerate Modules in each App Domain using ClrMD
Lets look at the code to enumerate Modules in each App Domain. But first questions, what is AppDomain?
Application domain or AppDomain is nothing but a logical region where .NET runtime runs and execute code. It provides security and isolation for executing managed code. We can use application domain to isolate any task that might bring down process.
This is specially useful when you create your own custom AppDomain and would like to see if your code is loading the necessary modules in that AppDomain itself. Keep in mind the below code only enumerates the AppDomain for the first loaded CLR runtime.
Code to get Modules under each AppDomain
using DataTarget dataTarget = DataTarget.LoadDump(args[0]);
ClrRuntime runtime = dataTarget.ClrVersions[0].CreateRuntime();
foreach (ClrAppDomain domain in runtime.AppDomains)
{
Console.WriteLine("\n>>> AppDomain");
Console.WriteLine("ID.....: {0}", domain.Id);
Console.WriteLine("Name...: {0}", domain.Name);
Console.WriteLine("Address: {0:x16}", domain.Address);
Console.WriteLine("\t{0,16} {1,16} {2}", "Assembly", "Module", "Module Name");
foreach (ClrModule module in domain.Modules)
{
Console.WriteLine("\t{0:x16} {1:x16} {2}",
module.AssemblyAddress, module.Address,
string.IsNullOrEmpty(module.Name) ? "[Dynamic Module]" : module.Name);
}
Console.WriteLine();
}
How the output of the above code looks like
>>> AppDomain
ID.....: 1
Name...: clrhost
Address: 000001d13e0423c0
Assembly Module Module Name
000001d143f66b80 00007ffb05214020 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Private.CoreLib.dll
000001d143f63860 00007ffb053ef7e0 C:\DAv2\ASPNETCoreWeb\ASPNETCoreWeb\bin\Debug\netcoreapp3.1\ASPNETCoreWeb.dll
000001d143f63560 00007ffb054014e8 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Runtime.dll
...
000001d144120bf0 00007ffb05633178 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Text.Encoding.Extensions.dll
000001d14411fb70 00007ffb056628c0 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.IO.FileSystem.Watcher.dll
000001d1441215f0 00007ffb05662fe8 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.ComponentModel.Primitives.dll
000001d1441292c0 00007ffb05c571c0 C:\DAv2\ASPNETCoreWeb\ASPNETCoreWeb\bin\Debug\netcoreapp3.1\Microsoft.EntityFrameworkCore.Relational.dll
000001d58f172680 00007ffb05f0ea18 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Transactions.Local.dll
000001d58f172800 00007ffb05f92040 [Dynamic Module]
000001d58f172100 00007ffb05ff1c50 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Security.Cryptography.Csp.dll
000001d58f172b00 00007ffb05ff2490 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Security.Cryptography.Primitives.dll
000001d58f171480 00007ffb06122688 C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\3.1.4\Microsoft.AspNetCore.Http.Extensions.dll
000001d58f172c00 00007ffb06150020 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Net.WebSockets.dll
000001d58f171500 00007ffb0615bd58 C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\System.Security.Principal.Windows.dll
Recent Comments