diff --git a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs
index 3d18ead32d116d..f7549adf6a9c32 100644
--- a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs
+++ b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs
@@ -74,7 +74,7 @@ internal static unsafe int[] ListAllPids()
/// Gets executable name for process given it's PID
///
/// The PID of the process
- public static unsafe string? GetProcPath(int pid)
+ public static unsafe string GetProcPath(int pid)
{
Span sysctlName = stackalloc int[] { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid };
byte* pBuffer = null;
@@ -83,7 +83,7 @@ internal static unsafe int[] ListAllPids()
try
{
Interop.Sys.Sysctl(sysctlName, ref pBuffer, ref bytesLength);
- return System.Text.Encoding.UTF8.GetString(pBuffer, (int)bytesLength - 1);
+ return System.Text.Encoding.UTF8.GetString(pBuffer, bytesLength - 1);
}
finally
{
diff --git a/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.ParseMapModules.cs b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.ParseMapModules.cs
index 8be9f32cbedee3..0f2287ce4164bd 100644
--- a/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.ParseMapModules.cs
+++ b/src/libraries/Common/src/Interop/Linux/procfs/Interop.ProcFsStat.ParseMapModules.cs
@@ -61,10 +61,8 @@ private static ProcessModuleCollection ParseMapsModulesCore(IEnumerable
// Not a continuation, commit any current modules and create a new one.
CommitCurrentModule();
- module = new ProcessModule
+ module = new ProcessModule(parsedLine.Path, Path.GetFileName(parsedLine.Path))
{
- FileName = parsedLine.Path,
- ModuleName = Path.GetFileName(parsedLine.Path),
ModuleMemorySize = parsedLine.Size,
EntryPointAddress = IntPtr.Zero // unknown
};
diff --git a/src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.cs b/src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.cs
index 7e1f6d5d77adec..d6975bd7274153 100644
--- a/src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.cs
+++ b/src/libraries/System.Diagnostics.Process/ref/System.Diagnostics.Process.cs
@@ -178,10 +178,10 @@ public partial class ProcessModule : System.ComponentModel.Component
internal ProcessModule() { }
public System.IntPtr BaseAddress { get { throw null; } }
public System.IntPtr EntryPointAddress { get { throw null; } }
- public string? FileName { get { throw null; } }
+ public string FileName { get { throw null; } }
public System.Diagnostics.FileVersionInfo FileVersionInfo { get { throw null; } }
public int ModuleMemorySize { get { throw null; } }
- public string? ModuleName { get { throw null; } }
+ public string ModuleName { get { throw null; } }
public override string ToString() { throw null; }
}
public partial class ProcessModuleCollection : System.Collections.ReadOnlyCollectionBase
diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.BSD.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.BSD.cs
index c6c43dd36a91c5..2d1806842d35bd 100644
--- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.BSD.cs
+++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.BSD.cs
@@ -42,16 +42,12 @@ internal static ProcessModuleCollection GetModules(int processId)
// and why MainModule exists.
try
{
- string? exePath = GetProcPath(processId);
+ string exePath = GetProcPath(processId);
if (!string.IsNullOrEmpty(exePath))
{
return new ProcessModuleCollection(1)
{
- new ProcessModule()
- {
- FileName = exePath,
- ModuleName = Path.GetFileName(exePath)
- }
+ new ProcessModule(exePath, Path.GetFileName(exePath))
};
}
}
diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.FreeBSD.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.FreeBSD.cs
index bc2bd2e0753285..0546c4b3605fc8 100644
--- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.FreeBSD.cs
+++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.FreeBSD.cs
@@ -15,7 +15,7 @@ public static int[] GetProcessIds()
return Interop.Process.ListAllPids();
}
- internal static string? GetProcPath(int processId)
+ internal static string GetProcPath(int processId)
{
return Interop.Process.GetProcPath(processId);
}
diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs
index 9dede5b6685ad0..1f85741f1c77d4 100644
--- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs
+++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs
@@ -41,18 +41,20 @@ 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
- string? exePath = Process.GetExePath(processId);
- for (int i = 0; i < modules.Count; i++)
+ if (Process.GetExePath(processId) is string exePath)
{
- ProcessModule module = modules[i];
- if (module.FileName == exePath)
+ for (int i = 0; i < modules.Count; i++)
{
- if (i > 0)
+ ProcessModule module = modules[i];
+ if (module.FileName == exePath)
{
- modules.RemoveAt(i);
- modules.Insert(0, module);
+ if (i > 0)
+ {
+ modules.RemoveAt(i);
+ modules.Insert(0, module);
+ }
+ break;
}
- break;
}
}
diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs
index 665bfb455c80e9..e64b359b6e770f 100644
--- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs
+++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Win32.cs
@@ -161,13 +161,6 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
continue;
}
- var module = new ProcessModule()
- {
- ModuleMemorySize = ntModuleInfo.SizeOfImage,
- EntryPointAddress = ntModuleInfo.EntryPoint,
- BaseAddress = ntModuleInfo.BaseOfDll
- };
-
int length = 0;
while ((length = Interop.Kernel32.GetModuleBaseName(processHandle, moduleHandle, chars, chars.Length)) == chars.Length)
{
@@ -178,12 +171,11 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
if (length == 0)
{
- module.Dispose();
HandleLastWin32Error();
continue;
}
- module.ModuleName = new string(chars, 0, length);
+ string moduleName = new string(chars, 0, length);
while ((length = Interop.Kernel32.GetModuleFileNameEx(processHandle, moduleHandle, chars, chars.Length)) == chars.Length)
{
@@ -194,7 +186,6 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
if (length == 0)
{
- module.Dispose();
HandleLastWin32Error();
continue;
}
@@ -205,9 +196,13 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
{
charsSpan = charsSpan.Slice(NtPathPrefix.Length);
}
- module.FileName = charsSpan.ToString();
- modules.Add(module);
+ modules.Add(new ProcessModule(charsSpan.ToString(), moduleName)
+ {
+ ModuleMemorySize = ntModuleInfo.SizeOfImage,
+ EntryPointAddress = ntModuleInfo.EntryPoint,
+ BaseAddress = ntModuleInfo.BaseOfDll,
+ });
}
}
finally
diff --git a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessModule.cs b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessModule.cs
index b45513b076c60b..9d0a3338a38edf 100644
--- a/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessModule.cs
+++ b/src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessModule.cs
@@ -13,19 +13,25 @@ namespace System.Diagnostics
[Designer("System.Diagnostics.Design.ProcessModuleDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class ProcessModule : Component
{
+ private readonly string _fileName;
+ private readonly string _moduleName;
private FileVersionInfo? _fileVersionInfo;
- internal ProcessModule() { }
+ internal ProcessModule(string fileName, string moduleName)
+ {
+ _fileName = fileName;
+ _moduleName = moduleName;
+ }
///
/// Returns the name of the Module.
///
- public string? ModuleName { get; internal set; }
+ public string ModuleName => _moduleName;
///
/// Returns the full file path for the location of the module.
///
- public string? FileName { get; internal set; }
+ public string FileName => _fileName;
///
/// Returns the memory address that the module was loaded at.
@@ -49,7 +55,7 @@ internal ProcessModule() { }
///
/// Returns version information about the module.
///
- public FileVersionInfo FileVersionInfo => _fileVersionInfo ?? (_fileVersionInfo = FileVersionInfo.GetVersionInfo(FileName!));
+ public FileVersionInfo FileVersionInfo => _fileVersionInfo ??= FileVersionInfo.GetVersionInfo(_fileName);
public override string ToString() => $"{base.ToString()} ({ModuleName})";
}