{
try
{
- if (output == null)
- throw new ArgumentNullException(nameof(output));
+ Debug.Assert(output != null);
+ Debug.Assert(profile != null);
if (processId <= 0)
- throw new ArgumentException(nameof(processId));
- if (profile == null)
- throw new ArgumentNullException(nameof(profile));
+ {
+ Console.Error.WriteLine("Process ID should not be negative.");
+ return ErrorCodes.ArgumentError;
+ }
var selectedProfile = ListProfilesCommandHandler.DotNETRuntimeProfiles
.FirstOrDefault(p => p.Name.Equals(profile, StringComparison.OrdinalIgnoreCase));
if (selectedProfile == null)
- throw new ArgumentException($"Invalid profile name: {profile}");
+ {
+ Console.Error.WriteLine($"Invalid profile name: {profile}");
+ return ErrorCodes.ArgumentError;
+ }
var providerCollection = Extensions.ToProviders(providers);
var profileProviders = new List<Provider>();
providerCollection.AddRange(profileProviders);
if (providerCollection.Count <= 0)
- throw new ArgumentException("No providers were specified to start a trace.");
+ {
+ Console.Error.WriteLine("No providers were specified to start a trace.");
+ return ErrorCodes.ArgumentError;
+ }
PrintProviders(providerCollection);
providers: providerCollection);
var shouldExit = new ManualResetEvent(false);
+ var failed = false;
var terminated = false;
ulong sessionId = 0;
if (sessionId == 0)
{
Console.Error.WriteLine("Unable to create session.");
- return -1;
+ return ErrorCodes.SessionCreationError;
+ }
+ if (File.Exists(output.FullName))
+ {
+ Console.Error.WriteLine("Unable to create file.");
+ return ErrorCodes.FileCreationError;
}
-
var collectingTask = new Task(() => {
try
{
}
catch (Exception ex)
{
+ failed = true;
Console.Error.WriteLine($"[ERROR] {ex.ToString()}");
}
finally
{
EventPipeClient.StopTracing(processId, sessionId);
}
- collectingTask.Wait();
+ await collectingTask;
}
Console.Out.WriteLine();
if (format != TraceFileFormat.Netperf)
TraceFileFormatConverter.ConvertToFormat(format, output.FullName);
- await Task.FromResult(0);
- return sessionId != 0 ? 0 : 1;
+ return failed ? ErrorCodes.TracingError : 0;
}
catch (Exception ex)
{
Console.Error.WriteLine($"[ERROR] {ex.ToString()}");
- return 1;
+ return ErrorCodes.UnknownError;
}
}
--- /dev/null
+namespace Microsoft.Diagnostics.Tools.Trace
+{
+ internal static class ErrorCodes
+ {
+ public static int SessionCreationError = 1;
+ public static int FileCreationError = 2;
+ public static int TracingError = 3;
+ public static int ArgumentError = 4;
+ public static int UnknownError = 5;
+ }
+}
\ No newline at end of file