/// <summary>Gets an array of module infos for the specified process.</summary>
/// <param name="processId">The ID of the process whose modules should be enumerated.</param>
/// <returns>The array of modules.</returns>
internal static ProcessModuleCollection GetModules(int processId)
{
ProcessModuleCollection modules = Interop.procfs.ParseMapsModules(processId) ?? new(capacity : 0);
// Move the main executable module to be the first in the list if it's not already
if (Process.GetExePath(processId) is string exePath)
{
for (int i = 0; i < modules.Count; i++)
{
ProcessModule module = modules[i];
if (module.FileName == exePath)
{
if (i > 0)
{
modules.RemoveAt(i);
modules.Insert(0, module);
}
break;
}
}
}
// Return the set of modules found
return(modules);
}