-
Notifications
You must be signed in to change notification settings - Fork 383
[ToolsCommon] Refactor Utils.cs #5622
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
Changes from 10 commits
155b9af
079b98a
3422ff6
30d433d
b116e70
44f7364
7a8450a
2e9cab0
f7f7704
90f7841
7b0aa79
6be25ae
0e29459
e2d336a
61d6413
2814b51
bdeb602
cced459
56d7451
944c6ce
f35c402
0184d7c
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 | ||
|---|---|---|---|---|
|
|
@@ -30,35 +30,23 @@ internal static class CollectCommandHandler | |||
| /// <returns></returns> | ||||
| private static async Task<int> Collect(CancellationToken ct, int processId, string output, int timeout, bool verbose, string name, string diagnosticPort, string dsrouter) | ||||
| { | ||||
| if (!CommandUtils.ResolveProcessForAttach(processId, name, diagnosticPort, dsrouter, out int resolvedProcessId)) | ||||
| try | ||||
| { | ||||
| return -1; | ||||
| } | ||||
| CommandUtils.ResolveProcessForAttach(processId, name, diagnosticPort, dsrouter, out int resolvedProcessId); | ||||
| processId = resolvedProcessId; | ||||
|
|
||||
| processId = resolvedProcessId; | ||||
|
|
||||
| if (!string.IsNullOrEmpty(diagnosticPort)) | ||||
| { | ||||
| try | ||||
| if (!string.IsNullOrEmpty(diagnosticPort)) | ||||
| { | ||||
| IpcEndpointConfig config = IpcEndpointConfig.Parse(diagnosticPort); | ||||
| if (!config.IsConnectConfig) | ||||
| { | ||||
| Console.Error.WriteLine("--diagnostic-port is only supporting connect mode."); | ||||
| return -1; | ||||
| } | ||||
| } | ||||
| catch (Exception ex) | ||||
| { | ||||
| Console.Error.WriteLine($"--diagnostic-port argument error: {ex.Message}"); | ||||
| return -1; | ||||
| } | ||||
|
|
||||
| processId = 0; | ||||
| } | ||||
| processId = 0; | ||||
| } | ||||
|
|
||||
| try | ||||
| { | ||||
| output = string.IsNullOrEmpty(output) | ||||
| ? $"{DateTime.Now:yyyyMMdd\\_HHmmss}_{processId}.gcdump" | ||||
| : output; | ||||
|
|
@@ -106,6 +94,16 @@ private static async Task<int> Collect(CancellationToken ct, int processId, stri | |||
| return -1; | ||||
| } | ||||
| } | ||||
| catch (CommandLineErrorException e) | ||||
| { | ||||
| Console.Error.WriteLine($"[ERROR] {e.Message}"); | ||||
| return -1; | ||||
| } | ||||
| catch (FormatException fe) | ||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was considering changing IpcEndpointConfig.Parse to throw diagnostics/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClientConnector.cs Line 61 in c0ea514
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the point why that would be needed, it doesn't log anything directly to console, but instead throws different exceptions with message based on identified error conditions, and its up to the caller to handle the exceptions it might throw and take actions based on exception. I believe this falls into comment above that API's should throw exceptions that makes sense for the errors identified and callers should handle them accordingly.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, I was just thinking it would be good to have consistency across the Tools, where any invalid combination of user passed-in args results in a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree such consistency would be good, we just need to do the CommandLineErrorException on the tool side of the code rather than in M.D.NetCore.Client library. From what I can see we've got at least three different helpers that play a role in converting the command-line args into a DiagnosticClient: ProcessLauncher.Launcher, DiagnosticsClientBuilder, and CommandUtils.ResolveProcessForAttach. Our tools don't use them consistently nor do they all handle errors in a consistent way, but those things could be fixed. With refactoring all the tools might converge to a unified helper something like: // this does any work needed to get an active connection to the target process including
// launching a new process, binding by name, binding by id, binding by port,
// routing through dsrouter, resuming the process, and hooking up child process
// IO to the current console.
// If anything goes wrong it throws CommandLineErrorException
using (var target = new ProcessTarget(commandLineArgs, console, cancellationToken))
{
// do whatever tool specific work we want
// target can have any APIs needed to describe or interact with the target process
target.DiagnosticClient.SendWhateverCommand(...);
...
}I'm not encouraging you to do that in the scope of this PR, but I hope we are making progress in that direction.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agree, we should at least stay to conventional error handling on the library side. If we would like to do something different on the tool side where we previously would have passed around IConsole to log to stdout and/or stderr and we would like to not do that if a utility function only logs errors and return an error code, then fine. In that case its more like a generic DiagnosticToolException that could be thrown that would only be known to tools and something that can be catched and logged to stderr. |
||||
| { | ||||
| Console.Error.WriteLine($"--diagnostic-port argument error: {fe.Message}"); | ||||
| return -1; | ||||
| } | ||||
| catch (Exception ex) | ||||
| { | ||||
| Console.Error.WriteLine($"[ERROR] {ex}"); | ||||
|
|
||||
Uh oh!
There was an error while loading. Please reload this page.