Skip to content

Fix nullability annotations on ProcessModule.FileName/ModuleName #63272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal static unsafe int[] ListAllPids()
/// Gets executable name for process given it's PID
/// </summary>
/// <param name="pid">The PID of the process</param>
public static unsafe string? GetProcPath(int pid)
public static unsafe string GetProcPath(int pid)
{
Span<int> sysctlName = stackalloc int[] { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, pid };
byte* pBuffer = null;
Expand All @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ private static ProcessModuleCollection ParseMapsModulesCore(IEnumerable<string>
// 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
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,6 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul
continue;
}

var module = new ProcessModule()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice refactor! 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was either that or do weird things will null suppressions :)

{
ModuleMemorySize = ntModuleInfo.SizeOfImage,
EntryPointAddress = ntModuleInfo.EntryPoint,
BaseAddress = ntModuleInfo.BaseOfDll
};

int length = 0;
while ((length = Interop.Kernel32.GetModuleBaseName(processHandle, moduleHandle, chars, chars.Length)) == chars.Length)
{
Expand All @@ -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)
{
Expand All @@ -194,7 +186,6 @@ private static ProcessModuleCollection GetModules(int processId, bool firstModul

if (length == 0)
{
module.Dispose();
HandleLastWin32Error();
continue;
}
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <devdoc>
/// Returns the name of the Module.
/// </devdoc>
public string? ModuleName { get; internal set; }
public string ModuleName => _moduleName;

/// <devdoc>
/// Returns the full file path for the location of the module.
/// </devdoc>
public string? FileName { get; internal set; }
public string FileName => _fileName;

/// <devdoc>
/// Returns the memory address that the module was loaded at.
Expand All @@ -49,7 +55,7 @@ internal ProcessModule() { }
/// <devdoc>
/// Returns version information about the module.
/// </devdoc>
public FileVersionInfo FileVersionInfo => _fileVersionInfo ?? (_fileVersionInfo = FileVersionInfo.GetVersionInfo(FileName!));
public FileVersionInfo FileVersionInfo => _fileVersionInfo ??= FileVersionInfo.GetVersionInfo(_fileName);

public override string ToString() => $"{base.ToString()} ({ModuleName})";
}
Expand Down