-
Notifications
You must be signed in to change notification settings - Fork 384
Add --diagnostic-port option to dotnet-stack #5592
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
base: main
Are you sure you want to change the base?
Changes from 5 commits
47ce0d2
682a87d
1c97f11
07c8f6c
a72e921
877a49d
ea77b81
87df662
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,22 +27,44 @@ internal static class ReportCommandHandler | |
/// <param name="processId">The process to report the stack from.</param> | ||
/// <param name="name">The name of process to report the stack from.</param> | ||
/// <param name="duration">The duration of to trace the target for. </param> | ||
/// <param name="diagnosticPort">The diagnostic port to connect to.</param> | ||
/// <returns></returns> | ||
private static async Task<int> Report(CancellationToken ct, TextWriter stdOutput, TextWriter stdError, int processId, string name, TimeSpan duration) | ||
private static async Task<int> Report(CancellationToken ct, TextWriter stdOutput, TextWriter stdError, int processId, string name, TimeSpan duration, string diagnosticPort) | ||
{ | ||
string tempNetTraceFilename = Path.Join(Path.GetTempPath(), Path.GetRandomFileName() + ".nettrace"); | ||
string tempEtlxFilename = ""; | ||
|
||
try | ||
{ | ||
// Either processName or processId has to be specified. | ||
// Validate that only one of processId, name, or diagnosticPort is specified | ||
int optionCount = 0; | ||
if (processId != 0) | ||
{ | ||
optionCount++; | ||
} | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
optionCount++; | ||
} | ||
if (!string.IsNullOrEmpty(diagnosticPort)) | ||
{ | ||
optionCount++; | ||
} | ||
|
||
if (optionCount == 0) | ||
{ | ||
stdError.WriteLine("--process-id, --name, or --diagnostic-port is required"); | ||
return -1; | ||
} | ||
else if (optionCount > 1) | ||
{ | ||
stdError.WriteLine("Only one of --process-id, --name, or --diagnostic-port can be specified"); | ||
return -1; | ||
} | ||
|
||
// Resolve process name to ID if needed | ||
if (!string.IsNullOrEmpty(name)) | ||
{ | ||
if (processId != 0) | ||
{ | ||
Console.WriteLine("Can only specify either --name or --process-id option."); | ||
return -1; | ||
} | ||
processId = CommandUtils.FindProcessIdWithName(name); | ||
if (processId < 0) | ||
{ | ||
|
@@ -55,14 +77,31 @@ private static async Task<int> Report(CancellationToken ct, TextWriter stdOutput | |
stdError.WriteLine("Process ID should not be negative."); | ||
return -1; | ||
} | ||
else if (processId == 0) | ||
|
||
DiagnosticsClient client; | ||
if (!string.IsNullOrEmpty(diagnosticPort)) | ||
{ | ||
stdError.WriteLine("--process-id is required"); | ||
return -1; | ||
try | ||
{ | ||
IpcEndpointConfig diagnosticPortConfig = IpcEndpointConfig.Parse(diagnosticPort); | ||
if (!diagnosticPortConfig.IsConnectConfig) | ||
{ | ||
stdError.WriteLine("--diagnostic-port only supports connect mode to a runtime."); | ||
|
||
return -1; | ||
} | ||
client = new DiagnosticsClient(diagnosticPortConfig); | ||
} | ||
catch (Exception ex) | ||
{ | ||
stdError.WriteLine($"--diagnostic-port argument error: {ex.Message}"); | ||
return -1; | ||
} | ||
} | ||
else | ||
{ | ||
client = new DiagnosticsClient(processId); | ||
} | ||
|
||
|
||
DiagnosticsClient client = new(processId); | ||
List<EventPipeProvider> providers = new() | ||
{ | ||
new EventPipeProvider("Microsoft-DotNETCore-SampleProfiler", EventLevel.Informational) | ||
|
@@ -189,15 +228,17 @@ public static Command ReportCommand() | |
{ | ||
ProcessIdOption, | ||
NameOption, | ||
DurationOption | ||
DurationOption, | ||
DiagnosticPortOption | ||
}; | ||
|
||
reportCommand.SetAction((parseResult, ct) => Report(ct, | ||
stdOutput: parseResult.Configuration.Output, | ||
stdError: parseResult.Configuration.Error, | ||
processId: parseResult.GetValue(ProcessIdOption), | ||
name: parseResult.GetValue(NameOption), | ||
duration: parseResult.GetValue(DurationOption))); | ||
duration: parseResult.GetValue(DurationOption), | ||
diagnosticPort: parseResult.GetValue(DiagnosticPortOption))); | ||
|
||
return reportCommand; | ||
} | ||
|
@@ -221,5 +262,11 @@ public static Command ReportCommand() | |
{ | ||
Description = "The name of the process to report the stack." | ||
}; | ||
|
||
private static readonly Option<string> DiagnosticPortOption = | ||
new("--diagnostic-port", "--dport") | ||
{ | ||
Description = "The path to a diagnostic port to be used." | ||
}; | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please use DiagnosticsClientBuilder, as other tools such as dotnet-counters and dotnet-trace already do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Refactored to use
DiagnosticsClientBuilder
in commit c8e0a36. This provides consistency with other tools and automatically supports both connect and listen modes.