Skip to content

Commit fd5c7e9

Browse files
committed
Fix dotnet-gcdump to use wildcard process id for dotnet-dsrouter.
1 parent d0f0789 commit fd5c7e9

File tree

4 files changed

+54
-24
lines changed

4 files changed

+54
-24
lines changed

src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsIpc/IpcTransport.cs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,7 @@ public override async Task WaitForConnectionAsync(CancellationToken token)
262262

263263
private string GetDefaultAddress()
264264
{
265-
try
266-
{
267-
Process process = Process.GetProcessById(_pid);
268-
}
269-
catch (ArgumentException)
270-
{
271-
throw new ServerNotAvailableException($"Process {_pid} is not running.");
272-
}
273-
catch (InvalidOperationException)
274-
{
275-
throw new ServerNotAvailableException($"Process {_pid} seems to be elevated.");
276-
}
277-
278-
if (!TryGetDefaultAddress(_pid, out string transportName))
279-
{
280-
throw new ServerNotAvailableException($"Process {_pid} not running compatible .NET runtime.");
281-
}
282-
283-
return transportName;
265+
return GetDefaultAddress(_pid);
284266
}
285267

286268
private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
@@ -330,6 +312,40 @@ private static bool TryGetDefaultAddress(int pid, out string defaultAddress)
330312
return !string.IsNullOrEmpty(defaultAddress);
331313
}
332314

315+
public static string GetDefaultAddress(int pid)
316+
{
317+
try
318+
{
319+
Process process = Process.GetProcessById(pid);
320+
}
321+
catch (ArgumentException)
322+
{
323+
throw new ServerNotAvailableException($"Process {pid} is not running.");
324+
}
325+
catch (InvalidOperationException)
326+
{
327+
throw new ServerNotAvailableException($"Process {pid} seems to be elevated.");
328+
}
329+
330+
if (!TryGetDefaultAddress(pid, out string defaultAddress))
331+
{
332+
throw new ServerNotAvailableException($"Process {pid} not running compatible .NET runtime.");
333+
}
334+
335+
return defaultAddress;
336+
}
337+
338+
public static bool IsDefaultAddressDSRouter(int pid, string address)
339+
{
340+
if (address.StartsWith(IpcRootPath, StringComparison.OrdinalIgnoreCase))
341+
{
342+
address = address.Substring(IpcRootPath.Length);
343+
}
344+
345+
string dsrouterAddress = $"dotnet-diagnostic-dsrouter-{pid}";
346+
return address.StartsWith(dsrouterAddress, StringComparison.OrdinalIgnoreCase);
347+
}
348+
333349
public override bool Equals(object obj)
334350
{
335351
return Equals(obj as PidIpcEndpoint);

src/Tools/dotnet-dsrouter/DiagnosticsServerRouterCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ private static string GetDefaultIpcServerPath(ILogger logger)
369369
}
370370

371371
logger?.LogDebug($"Using default IPC server path, {path}.");
372-
logger?.LogDebug($"Attach to default dotnet-dsrouter IPC server using -p {processId} diagnostic tooling argument.");
372+
logger?.LogDebug($"Attach to default dotnet-dsrouter IPC server using --process-id {processId} diagnostic tooling argument.");
373373

374374
return path;
375375
}

src/Tools/dotnet-gcdump/CommandLine/CollectCommandHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal static class CollectCommandHandler
3232
/// <returns></returns>
3333
private static async Task<int> Collect(CancellationToken ct, IConsole console, int processId, string output, int timeout, bool verbose, string name, string diagnosticPort)
3434
{
35-
if (!CommandUtils.ValidateArgumentsForAttach (processId, name, diagnosticPort, out int resolvedProcessId))
35+
if (!CommandUtils.ValidateArgumentsForAttach(processId, name, diagnosticPort, out int resolvedProcessId))
3636
{
3737
return -1;
3838
}

src/Tools/dotnet-gcdump/DotNetHeapDump/EventPipeDotNetHeapDumper.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,9 @@ public static bool DumpFromEventPipe(CancellationToken ct, int processId, string
165165

166166
gcDumpSession.Source.Clr.GCStart += delegate (GCStartTraceData data)
167167
{
168-
if (processId == 0)
168+
if (gcDumpSession.UseWildcardProcessId)
169169
{
170170
processId = data.ProcessID;
171-
log.WriteLine("Process wildcard selects process id {0}", processId);
172171
}
173172
if (data.ProcessID != processId)
174173
{
@@ -217,7 +216,7 @@ public static bool DumpFromEventPipe(CancellationToken ct, int processId, string
217216

218217
if (memoryGraph != null)
219218
{
220-
dumper.SetupCallbacks(memoryGraph, gcDumpSession.Source, processId.ToString());
219+
dumper.SetupCallbacks(memoryGraph, gcDumpSession.Source, gcDumpSession.UseWildcardProcessId ? null : processId.ToString());
221220
}
222221

223222
// Set up a separate thread that will listen for EventPipe events coming back telling us we succeeded.
@@ -323,8 +322,23 @@ internal sealed class EventPipeSessionController : IDisposable
323322
public IReadOnlyList<EventPipeProvider> Providers => _providers.AsReadOnly();
324323
public EventPipeEventSource Source => _source;
325324

325+
public bool UseWildcardProcessId => _diagnosticPort != null;
326+
326327
public EventPipeSessionController(int pid, string diagnosticPort, List<EventPipeProvider> providers, bool requestRundown = true)
327328
{
329+
if (string.IsNullOrEmpty(diagnosticPort))
330+
{
331+
try
332+
{
333+
string defaultAddress = PidIpcEndpoint.GetDefaultAddress(pid);
334+
if (!string.IsNullOrEmpty(defaultAddress) && PidIpcEndpoint.IsDefaultAddressDSRouter(pid, defaultAddress))
335+
{
336+
diagnosticPort = defaultAddress + ",connect";
337+
}
338+
}
339+
catch { }
340+
}
341+
328342
if (!string.IsNullOrEmpty(diagnosticPort))
329343
{
330344
_diagnosticPort = IpcEndpointConfig.Parse(diagnosticPort);

0 commit comments

Comments
 (0)