Skip to content

Commit cddfe4f

Browse files
[dsrouter] improve log message when launched as a subprocess (#5536)
`dotnet-trace collect --dsrouter android` logs an unnecessary message currently: Run diagnotic tool connecting application on android device through dotnet-dsrouter pid=38004: dotnet-trace collect -p 38004 `dotnet-trace` is launching and controlling `dotnet-dsrouter`, and so you would not need to run `dotnet-trace collect -p 38004`. To solve this: * Pass `--parentprocess pid:name` when launching `dotnet-dsrouter` from `dotnet-trace` * `dotnet-dsrouter` checks `--parentprocess` switch to tell if it is running as a subprocess * Only log this message when *not* a subprocess
1 parent 745ee21 commit cddfe4f

File tree

3 files changed

+39
-20
lines changed

3 files changed

+39
-20
lines changed

src/Tools/Common/DsRouterProcessLauncher.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ public int Start(string dsrouterCommand, CancellationToken ct)
127127
dotnetDsrouterTool = Path.Combine(toolsRoot, dotnetDsrouterTool);
128128
}
129129

130+
using Process currentProcess = Process.GetCurrentProcess();
131+
130132
// Block SIGINT and SIGQUIT in child process.
131-
dsrouterCommand += " --block-signals SIGINT;SIGQUIT";
133+
dsrouterCommand += $" --block-signals SIGINT;SIGQUIT --parentprocess \"{currentProcess.Id}:{currentProcess.ProcessName}\"";
132134

133135
_childProc = new Process();
134136

src/Tools/dotnet-dsrouter/DiagnosticsServerRouterCommands.cs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -357,41 +357,41 @@ public async Task<int> RunIpcClientWebSocketServerRouter(CancellationToken token
357357
}
358358
}
359359

360-
public async Task<int> RunIpcServerIOSSimulatorRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info)
360+
public async Task<int> RunIpcServerIOSSimulatorRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info, string parentProcess)
361361
{
362362
if (info || ParseLogLevel(verbose) <= LogLevel.Information)
363363
{
364-
logRouterUsageInfo("ios simulator", "127.0.0.1:9000", true);
364+
logRouterUsageInfo("ios simulator", "127.0.0.1:9000", true, parentProcess);
365365
}
366366

367367
return await RunIpcServerTcpClientRouter(token, "", "127.0.0.1:9000", runtimeTimeout, verbose, "").ConfigureAwait(false);
368368
}
369369

370-
public async Task<int> RunIpcServerIOSRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info)
370+
public async Task<int> RunIpcServerIOSRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info, string parentProcess)
371371
{
372372
if (info || ParseLogLevel(verbose) <= LogLevel.Information)
373373
{
374-
logRouterUsageInfo("ios device", "127.0.0.1:9000", true);
374+
logRouterUsageInfo("ios device", "127.0.0.1:9000", true, parentProcess);
375375
}
376376

377377
return await RunIpcServerTcpClientRouter(token, "", "127.0.0.1:9000", runtimeTimeout, verbose, "iOS").ConfigureAwait(false);
378378
}
379379

380-
public async Task<int> RunIpcServerAndroidEmulatorRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info)
380+
public async Task<int> RunIpcServerAndroidEmulatorRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info, string parentProcess)
381381
{
382382
if (info || ParseLogLevel(verbose) <= LogLevel.Information)
383383
{
384-
logRouterUsageInfo("android emulator", "10.0.2.2:9000", false);
384+
logRouterUsageInfo("android emulator", "10.0.2.2:9000", false, parentProcess);
385385
}
386386

387387
return await RunIpcServerTcpServerRouter(token, "", "127.0.0.1:9000", runtimeTimeout, verbose, "").ConfigureAwait(false);
388388
}
389389

390-
public async Task<int> RunIpcServerAndroidRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info)
390+
public async Task<int> RunIpcServerAndroidRouter(CancellationToken token, int runtimeTimeout, string verbose, bool info, string parentProcess)
391391
{
392392
if (info || ParseLogLevel(verbose) <= LogLevel.Information)
393393
{
394-
logRouterUsageInfo("android device", "127.0.0.1:9000", false);
394+
logRouterUsageInfo("android device", "127.0.0.1:9000", false, parentProcess);
395395
}
396396

397397
return await RunIpcServerTcpServerRouter(token, "", "127.0.0.1:9001", runtimeTimeout, verbose, "Android").ConfigureAwait(false);
@@ -501,7 +501,7 @@ private static LogLevel ParseLogLevel(string verbose)
501501
return logLevel;
502502
}
503503

504-
private static void logRouterUsageInfo(string deviceName, string deviceTcpIpAddress, bool deviceListenMode)
504+
private static void logRouterUsageInfo(string deviceName, string deviceTcpIpAddress, bool deviceListenMode, string parentProcess)
505505
{
506506
StringBuilder message = new();
507507

@@ -514,8 +514,11 @@ private static void logRouterUsageInfo(string deviceName, string deviceTcpIpAddr
514514
message.AppendLine($"DOTNET_DiagnosticPorts={deviceTcpIpAddress},nosuspend,{listenMode}");
515515
message.AppendLine("[Startup Tracing]");
516516
message.AppendLine($"DOTNET_DiagnosticPorts={deviceTcpIpAddress},suspend,{listenMode}");
517-
message.AppendLine($"Run diagnotic tool connecting application on {deviceName} through dotnet-dsrouter pid={pid}:");
518-
message.AppendLine($"dotnet-trace collect -p {pid}");
517+
if (string.IsNullOrEmpty(parentProcess))
518+
{
519+
message.AppendLine($"Run diagnotic tool connecting application on {deviceName} through dotnet-dsrouter pid={pid}:");
520+
message.AppendLine($"dotnet-trace collect -p {pid}");
521+
}
519522
message.AppendLine($"See https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-dsrouter for additional details and examples.");
520523

521524
ConsoleColor currentColor = Console.ForegroundColor;

src/Tools/dotnet-dsrouter/Program.cs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,15 @@ private static Command IOSSimulatorRouterCommand()
156156
"Router is configured using an IPC server (connecting to by diagnostic tools) " +
157157
"and a TCP/IP server (accepting runtime TCP client).")
158158
{
159-
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption
159+
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption, ParentProcessOption
160160
};
161161

162162
command.SetAction((parseResult, ct) => new DiagnosticsServerRouterCommands().RunIpcServerIOSSimulatorRouter(
163163
ct,
164164
runtimeTimeout: parseResult.GetValue(RuntimeTimeoutOption),
165165
verbose: parseResult.GetValue(VerboseOption),
166-
info: parseResult.GetValue(InfoOption)
166+
info: parseResult.GetValue(InfoOption),
167+
parentProcess: parseResult.GetValue(ParentProcessOption)
167168
));
168169

169170
return command;
@@ -177,14 +178,15 @@ private static Command IOSRouterCommand()
177178
"Router is configured using an IPC server (connecting to by diagnostic tools) " +
178179
"and a TCP/IP client (connecting runtime TCP server over usbmux).")
179180
{
180-
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption
181+
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption, ParentProcessOption
181182
};
182183

183184
command.SetAction((parseResult, ct) => new DiagnosticsServerRouterCommands().RunIpcServerIOSRouter(
184185
ct,
185186
runtimeTimeout: parseResult.GetValue(RuntimeTimeoutOption),
186187
verbose: parseResult.GetValue(VerboseOption),
187-
info: parseResult.GetValue(InfoOption)
188+
info: parseResult.GetValue(InfoOption),
189+
parentProcess: parseResult.GetValue(ParentProcessOption)
188190
));
189191

190192
return command;
@@ -198,14 +200,16 @@ private static Command AndroidEmulatorRouterCommand()
198200
"Router is configured using an IPC server (connecting to by diagnostic tools) " +
199201
"and a TCP/IP server (accepting runtime TCP client).")
200202
{
201-
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption
203+
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption, ParentProcessOption
202204
};
203205

204206
command.SetAction((parseResult, ct) => new DiagnosticsServerRouterCommands().RunIpcServerAndroidEmulatorRouter(
205207
ct,
206208
runtimeTimeout: parseResult.GetValue(RuntimeTimeoutOption),
207209
verbose: parseResult.GetValue(VerboseOption),
208-
info: parseResult.GetValue(InfoOption)));
210+
info: parseResult.GetValue(InfoOption),
211+
parentProcess: parseResult.GetValue(ParentProcessOption)
212+
));
209213

210214
return command;
211215
}
@@ -218,14 +222,16 @@ private static Command AndroidRouterCommand()
218222
"Router is configured using an IPC server (connecting to by diagnostic tools) " +
219223
"and a TCP/IP server (accepting runtime TCP client).")
220224
{
221-
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption
225+
RuntimeTimeoutOption, VerboseOption, InfoOption, BlockedSignalsOption, ParentProcessOption
222226
};
223227

224228
command.SetAction((parseResult, ct) => new DiagnosticsServerRouterCommands().RunIpcServerAndroidRouter(
225229
ct,
226230
runtimeTimeout: parseResult.GetValue(RuntimeTimeoutOption),
227231
verbose: parseResult.GetValue(VerboseOption),
228-
info: parseResult.GetValue(InfoOption)));
232+
info: parseResult.GetValue(InfoOption),
233+
parentProcess: parseResult.GetValue(ParentProcessOption)
234+
));
229235

230236
return command;
231237
}
@@ -304,6 +310,14 @@ private static Command AndroidRouterCommand()
304310
Description = "Print info on how to use current dotnet-dsrouter instance with application and diagnostic tooling."
305311
};
306312

313+
private static readonly Option<string> ParentProcessOption =
314+
new("--parentprocess")
315+
{
316+
Description = "If dsrouter was launched from dotnet-trace or another .NET diagnostic tool, contains the parent process ID and name: --parentprocess pid:name",
317+
DefaultValueFactory = _ => "",
318+
Hidden = true
319+
};
320+
307321
private static Task<int> Main(string[] args)
308322
{
309323
RootCommand rootCommand = new()

0 commit comments

Comments
 (0)