Skip to content

Commit

Permalink
Merge pull request #1 from dotnet-campus/t/lindexi/TestIpc
Browse files Browse the repository at this point in the history
修复在 Linux 平台因为抛出 PlatformNotSupportedException 异常而无法找到调试项目
  • Loading branch information
lindexi authored Apr 1, 2024
2 parents 1601208 + e761fbf commit b03a118
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnoSpySnoopDebugger.IpcCommunicationContext;

public static class DebugIpcPeerNameGenerator
{
public static string GetPeerNameFromProcess(Process process)
{
return $"UnoSpySnoop_{process.ProcessName.Replace('/', '_')}_{process.Id}";
}
}
2 changes: 1 addition & 1 deletion UnoSpySnoop/Lib/UnoSpySnoopProvider/SpySnoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static void StartSpyUI(Grid spySnoopRootGrid, string? debugIpcName = null
{
var currentProcess = Process.GetCurrentProcess();

debugIpcName = $"UnoSpySnoop_{currentProcess.ProcessName}_{currentProcess.Id}";
debugIpcName = DebugIpcPeerNameGenerator.GetPeerNameFromProcess(currentProcess);
}

var unoSpySnoop = new SpySnoop(spySnoopRootGrid, debugIpcName);
Expand Down
125 changes: 109 additions & 16 deletions UnoSpySnoop/UI/UnoSpySnoopDebugger/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Diagnostics;

using System.Text.RegularExpressions;
using dotnetCampus.Ipc.Context;
using dotnetCampus.Ipc.Exceptions;
using dotnetCampus.Ipc.IpcRouteds.DirectRouteds;
using dotnetCampus.Ipc.Pipes;
using dotnetCampus.Ipc.Threading;

using Microsoft.UI.Xaml.Data;

using UnoSpySnoopDebugger.Communications;
using UnoSpySnoopDebugger.IpcCommunicationContext;
using UnoSpySnoopDebugger.Models;
Expand Down Expand Up @@ -48,14 +47,10 @@ private async Task RefreshProcessInfoList()

var processes = Process.GetProcesses().ToList();

foreach (Process process in processes)
{
Console.WriteLine($"Process: {process.ProcessName}");
}

#if DEBUG
var currentProcess = Process.GetCurrentProcess();
var otherInstance = processes.FirstOrDefault(p => p.Id != currentProcess.Id && p.ProcessName == currentProcess.ProcessName);
var otherInstance =
processes.FirstOrDefault(p => p.Id != currentProcess.Id && p.ProcessName == currentProcess.ProcessName);
if (otherInstance != null)
{
processes.Remove(otherInstance);
Expand All @@ -68,13 +63,106 @@ private async Task RefreshProcessInfoList()

#endif

await Parallel.ForEachAsync(processes, async (process, _) => { await PeekProcess(process); });
HashSet<string> ignoreProcessNameHashSet;
if (OperatingSystem.IsLinux())
{
ignoreProcessNameHashSet = new HashSet<string>()
{
"(sd-pam)",
"ModemManager",
"NetworkManager",
"Xorg",
"accounts_daemon",
"acpi_thermal_pm",
"agent",
"at-spi-bus-launcher",
"ata_sff",
"bamfdaemon",
"bash",
"cron",
"cryptd",
"cupsd",
"dbus-daemon",
"kwin_x11",
"kwin_no_scale",
"sh",
"ssh",
"sshd",
"su",
"sudo",
"systemd",
};
}
else if (OperatingSystem.IsWindows())
{
ignoreProcessNameHashSet = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
"cmd",
"conhost",
"csrss",
"ctfmon",
"dllhost",
"dwm",
"explorer",
"GameBar",
"OpenConsole",
"RuntimeBroker",
"SearchHost",
"SearchIndexer",
"svchost",
"Taskmgr",
};
}
else
{
ignoreProcessNameHashSet = new HashSet<string>();
}


await Parallel.ForEachAsync(processes.Where(t => !CanIgnore(t)), async (process, _) =>
{
await PeekProcess(process);
process.Dispose();
});
return;

bool CanIgnore(Process process)
{
if (ignoreProcessNameHashSet.Contains(process.ProcessName))
{
return true;
}

if (OperatingSystem.IsLinux())
{
if (Regex.IsMatch(process.ProcessName, @"cpuhp/\d+"))
{
return true;
}

if (Regex.IsMatch(process.ProcessName, @"idle_inject/\d+"))
{
return true;
}

if (Regex.IsMatch(process.ProcessName, @"ksoftirqd/\d+"))
{
return true;
}

if (Regex.IsMatch(process.ProcessName, @"kworker/\d+\:\d"))
{
return true;
}
}

return false;
}
}

private async Task PeekProcess(Process process)
{
var peerName = $"UnoSpySnoop_{process.ProcessName}_{process.Id}";
Console.WriteLine($"Try peek {peerName}");
var peerName = DebugIpcPeerNameGenerator.GetPeerNameFromProcess(process);

try
{
Expand All @@ -99,16 +187,21 @@ private async Task PeekProcess(Process process)
ProcessName = response.ProcessName,
};

DispatcherQueue.TryEnqueue(() =>
{
ProcessInfoList.Add(info);
});
DispatcherQueue.TryEnqueue(() => { ProcessInfoList.Add(info); });
}
catch (IpcClientPipeConnectionException e)
{
// Connection Fail
Console.WriteLine($"Connection Fail {peerName}");
}
catch (PlatformNotSupportedException e)
{
Console.WriteLine($"PlatformNotSupportedException {peerName}");
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

public JsonIpcDirectRoutedProvider IpcProvider { get; set; }
Expand Down

0 comments on commit b03a118

Please sign in to comment.