Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
536cd50
[DotnetTrace] Update CLREventKeywords
mdh1418 Sep 16, 2025
49a875b
[DotnetTrace] Move MergeProfileAndProviders to Extensions
mdh1418 Sep 17, 2025
b4787fc
[DotnetTrace] Rename Extensions to ProviderUtils
mdh1418 Sep 17, 2025
c921869
[DotnetTrace] Add provider unifying helper
mdh1418 Sep 17, 2025
e1c15c7
[DotnetTrace] Move shared options to CommonOptions
mdh1418 Sep 17, 2025
b4112b5
[DotnetTrace] Add collect-linux skeleton
mdh1418 Sep 17, 2025
e5f3975
[DotnetTrace][CollectLinux] Start record-trace
mdh1418 Sep 17, 2025
677e54e
[DotnetTrace][CollectLinux] Build record-trace args
mdh1418 Sep 17, 2025
31186a8
[DotnetTrace] Update profiles
mdh1418 Sep 17, 2025
44593bb
[DotnetTrace] Update collect to new provider unifier
mdh1418 Sep 17, 2025
b190d73
[DotnetCounters] Remove Extensions reference
mdh1418 Sep 19, 2025
9f1ef6d
[DotnetTrace] Update tests
mdh1418 Sep 19, 2025
c8e53ea
Address Feedback
mdh1418 Sep 19, 2025
590a203
[DotnetTrace] Print profile effects and clrevents ignore warning
mdh1418 Sep 19, 2025
8e2453f
[DotnetTrace] Print PerfEvents and include in default condition
mdh1418 Sep 19, 2025
573d769
[DotnetTrace] Update OneCollect package with FFI
mdh1418 Sep 30, 2025
1b3b583
Fix dotnet-trace build for repo root build
mdh1418 Oct 3, 2025
e6d9de9
Add Linux events table
mdh1418 Oct 6, 2025
676efa9
Add Progress status and Address feedback
mdh1418 Oct 6, 2025
d90d1be
Merge remote-tracking branch 'upstream/main' into dotnet_trace_collec…
mdh1418 Oct 6, 2025
096f325
Update collect functional tests for ProviderUtils refactor
mdh1418 Oct 6, 2025
1f75125
[DotnetTrace] Add Collect Linux Functional Tests
mdh1418 Oct 6, 2025
55bc84a
Adjust CollectLinux tests for non-Linux platforms
mdh1418 Oct 7, 2025
4c1a361
[DotnetTrace][CollectLinux] Remove process specifier
mdh1418 Oct 7, 2025
208086f
Adjust functional tests and add failure cases
mdh1418 Oct 8, 2025
40c5905
[DotnetTrace][CollectLinux] Add ProgressStatus and output file to tests
mdh1418 Oct 8, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/Tools/dotnet-trace/CommandLine/Commands/CollectCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,6 @@ internal async Task<int> Collect(CancellationToken ct, CommandLineConfiguration
{
ConsoleWriteLine($"Trace Duration : {duration:dd\\:hh\\:mm\\:ss}");
}

ConsoleWriteLine();
ConsoleWriteLine();

EventMonitor eventMonitor = null;
Expand Down Expand Up @@ -391,10 +389,19 @@ internal async Task<int> Collect(CancellationToken ct, CommandLineConfiguration
}

FileInfo fileInfo = new(output.FullName);
bool wroteStatus = false;
Action printStatus = () => {
if (printStatusOverTime && rewriter.IsRewriteConsoleLineSupported)
{
rewriter?.RewriteConsoleLine();
if (wroteStatus)
{
rewriter?.RewriteConsoleLine();
}
else
{
// First time writing status, so don't rewrite console yet.
wroteStatus = true;
}
fileInfo.Refresh();
ConsoleWriteLine($"[{stopwatch.Elapsed:dd\\:hh\\:mm\\:ss}]\tRecording trace {GetSize(fileInfo.Length)}");
ConsoleWriteLine("Press <Enter> or <Ctrl+C> to exit...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ namespace Microsoft.Diagnostics.Tools.Trace
{
internal partial class CollectLinuxCommandHandler
{
private static bool s_stopTracing;
private static Stopwatch s_stopwatch = new();
private static LineRewriter s_rewriter;
private static bool s_printingStatus;
private bool s_stopTracing;
private Stopwatch s_stopwatch = new();
private LineRewriter s_rewriter;
private bool s_printingStatus;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: non-static variables shouldn't have s_ prefixes on them.


internal sealed record CollectLinuxArgs(
CancellationToken Ct,
Expand All @@ -48,7 +48,7 @@ internal int CollectLinux(CollectLinuxArgs args)
if (!OperatingSystem.IsLinux())
{
Console.Error.WriteLine("The collect-linux command is only supported on Linux.");
return (int)ReturnCode.ArgumentError;
return (int)ReturnCode.PlatformNotSupportedError;
}

args.Ct.Register(() => s_stopTracing = true);
Expand All @@ -72,6 +72,11 @@ internal int CollectLinux(CollectLinuxArgs args)
s_stopwatch.Start();
ret = RecordTraceInvoker(command, (UIntPtr)command.Length, OutputHandler);
}
catch (Exception ex)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In dotnet-trace collect we have the CommandLineErrorException catch handler that I think we should replicate here. The intent is to separate Exceptions into two categories:

  1. Exceptions that handle anticipated, well understood error conditions where the Exception message was written to make sense as a console error message. In this case we want to just print the error message without exception types or stack traces making the output look scary or cluttered.
  2. Other exceptions that we didn't anticipate. If these exceptions leak out its probably a bug or some kind of OS/hardware issue outside of our control. These benefit from Exception types and stack traces to help diagnose the issue and also to communicate to devs that something truly unexpected occurred.

All of the issues handling command-line arguments should fall in the first group.

{
Console.Error.WriteLine($"[ERROR] {ex}");
ret = (int)ReturnCode.TracingError;
}
finally
{
if (!string.IsNullOrEmpty(scriptPath))
Expand Down
9 changes: 6 additions & 3 deletions src/tests/Common/MockConsole.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void AssertLinesEqual(int startLine, params string[] expectedLines)
}
}

public void AssertSanitizedLinesEqual(Func<string[], string[]> sanitizer, params string[] expectedLines)
public void AssertSanitizedLinesEqual(Func<string[], string[]> sanitizer, bool ignorePastExpected = false, params string[] expectedLines)
{
string[] actualLines = Lines;
if (sanitizer is not null)
Expand All @@ -178,9 +178,12 @@ public void AssertSanitizedLinesEqual(Func<string[], string[]> sanitizer, params
$"Line {i,2} Actual : {actualLines[i]}");
}
}
for (int i = expectedLines.Length; i < actualLines.Length; i++)
if (!ignorePastExpected)
{
Assert.True(string.IsNullOrEmpty(actualLines[i]), $"Actual line #{i} beyond expected lines is not empty: {actualLines[i]}");
for (int i = expectedLines.Length; i < actualLines.Length; i++)
{
Assert.True(string.IsNullOrEmpty(actualLines[i]), $"Actual line #{i} beyond expected lines is not empty: {actualLines[i]}");
}
}
}
}
Expand Down
64 changes: 52 additions & 12 deletions src/tests/dotnet-trace/CollectCommandFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Threading.Tasks;
using Microsoft.Diagnostics.Tests.Common;
using Microsoft.Diagnostics.Tools.Trace;
using Microsoft.Internal.Common.Utils;
using Xunit;

namespace Microsoft.Diagnostics.Tools.Trace
Expand Down Expand Up @@ -51,22 +52,33 @@ public sealed record CollectArgs(
public async Task CollectCommandProviderConfigurationConsolidation(CollectArgs args, string[] expectedSubset)
{
MockConsole console = new(200, 30);
string[] rawLines = await RunAsync(args, console).ConfigureAwait(true);
console.AssertSanitizedLinesEqual(CollectSanitizer, expectedSubset);
int exitCode = await RunAsync(args, console).ConfigureAwait(true);
Assert.Equal((int)ReturnCode.Ok, exitCode);
console.AssertSanitizedLinesEqual(CollectSanitizer, expectedLines: expectedSubset);

byte[] expected = Encoding.UTF8.GetBytes(ExpectedPayload);
Assert.Equal(expected, args.EventStream.ToArray());
}

private static async Task<string[]> RunAsync(CollectArgs config, MockConsole console)
[Theory]
[MemberData(nameof(InvalidProviders))]
public async Task CollectCommandInvalidProviderConfiguration_Throws(CollectArgs args, string[] expectedException)
{
MockConsole console = new(200, 30);
int exitCode = await RunAsync(args, console).ConfigureAwait(true);
Assert.Equal((int)ReturnCode.TracingError, exitCode);
console.AssertSanitizedLinesEqual(CollectSanitizer, true, expectedException);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the reason this test is ignoring past expected is because there is an unpredictable StackTrace there? Hopefully my suggestion around the catch handlers and removing the stack trace text above also means we no longer need to ignore extra output.

}

private static async Task<int> RunAsync(CollectArgs config, MockConsole console)
{
var handler = new CollectCommandHandler();
handler.StartTraceSessionAsync = (client, cfg, ct) => Task.FromResult<CollectCommandHandler.ICollectSession>(new TestCollectSession());
handler.ResumeRuntimeAsync = (client, ct) => Task.CompletedTask;
handler.CollectSessionEventStream = (name) => config.EventStream;
handler.Console = console;

int exit = await handler.Collect(
return await handler.Collect(
config.ct,
config.cliConfig,
config.ProcessId,
Expand All @@ -87,12 +99,7 @@ private static async Task<string[]> RunAsync(CollectArgs config, MockConsole con
config.stoppingEventPayloadFilter,
config.rundown,
config.dsrouter
).ConfigureAwait(true);
if (exit != 0)
{
throw new InvalidOperationException($"Collect exited with return code {exit}.");
}
return console.Lines;
).ConfigureAwait(false);
}

private static string[] CollectSanitizer(string[] lines)
Expand Down Expand Up @@ -123,7 +130,6 @@ public void Stop() {}

public static IEnumerable<object[]> BasicCases()
{
FileInfo fi = new("trace.nettrace");
yield return new object[]
{
new CollectArgs(),
Expand Down Expand Up @@ -214,14 +220,46 @@ public static IEnumerable<object[]> BasicCases()
};
}

public static IEnumerable<object[]> InvalidProviders()
{
yield return new object[]
{
new CollectArgs(profile: new[] { "cpu-sampling" }),
new [] { FormatException("The specified profile 'cpu-sampling' does not apply to `dotnet-trace collect`.", "System.ArgumentException") }
};

yield return new object[]
{
new CollectArgs(profile: new[] { "unknown" }),
new [] { FormatException("Invalid profile name: unknown", "System.ArgumentException") }
};

yield return new object[]
{
new CollectArgs(providers: new[] { "Foo:::Bar=0", "Foo:::Bar=1" }),
new [] { FormatException($"Provider \"Foo\" is declared multiple times with filter arguments.", "System.ArgumentException") }
};

yield return new object[]
{
new CollectArgs(clrevents: "unknown"),
new [] { FormatException("unknown is not a valid CLR event keyword", "System.ArgumentException") }
};

yield return new object[]
{
new CollectArgs(clrevents: "gc", clreventlevel: "unknown"),
new [] { FormatException("Unknown EventLevel: unknown", "System.ArgumentException") }
};
}

private static string outputFile = $"Output File : {Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar}trace.nettrace";
private const string ProviderHeader = "Provider Name Keywords Level Enabled By";
private static readonly string[] CommonTail = [
"Process : <PROCESS>",
outputFile,
"",
"",
"",
"Trace completed."
];

Expand All @@ -238,5 +276,7 @@ private static string FormatProvider(string name, string keywordsHex, string lev
string.Format("{0, -8}", $"{levelName}({levelValue})");
return string.Format("{0, -80}", display) + enabledBy;
}

private static string FormatException(string message, string exceptionType) => $"[ERROR] {exceptionType}: {message}";
}
}
87 changes: 74 additions & 13 deletions src/tests/dotnet-trace/CollectLinuxCommandFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using Microsoft.Diagnostics.Tests.Common;
using Microsoft.Diagnostics.Tools.Trace;
using Microsoft.Internal.Common.Utils;
using Xunit;

namespace Microsoft.Diagnostics.Tools.Trace
Expand All @@ -23,7 +24,7 @@ private static CollectLinuxCommandHandler.CollectLinuxArgs TestArgs(
string clrEventLevel = "",
string clrEvents = "",
string[] perfEvents = null,
string[] profiles = null,
string[] profile = null,
FileInfo output = null,
TimeSpan duration = default)
{
Expand All @@ -32,7 +33,7 @@ private static CollectLinuxCommandHandler.CollectLinuxArgs TestArgs(
clrEventLevel,
clrEvents,
perfEvents ?? Array.Empty<string>(),
profiles ?? Array.Empty<string>(),
profile ?? Array.Empty<string>(),
output ?? new FileInfo(CommonOptions.DefaultTraceName),
duration);
}
Expand All @@ -42,23 +43,49 @@ private static CollectLinuxCommandHandler.CollectLinuxArgs TestArgs(
public void CollectLinuxCommandProviderConfigurationConsolidation(object testArgs, string[] expectedLines)
{
MockConsole console = new(200, 30);
var handler = new CollectLinuxCommandHandler(console);
handler.RecordTraceInvoker = (cmd, len, cb) => 0;
int exit = handler.CollectLinux((CollectLinuxCommandHandler.CollectLinuxArgs)testArgs);
int exitCode = Run(testArgs, console);
if (OperatingSystem.IsLinux())
{
Assert.Equal(0, exit);
console.AssertSanitizedLinesEqual(null, expectedLines);
Assert.Equal((int)ReturnCode.Ok, exitCode);
console.AssertSanitizedLinesEqual(null, expectedLines: expectedLines);
}
else
{
Assert.Equal(3, exit);
console.AssertSanitizedLinesEqual(null, new string[] {
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
console.AssertSanitizedLinesEqual(null, expectedLines: new string[] {
"The collect-linux command is only supported on Linux.",
});
}
}

[Theory]
[MemberData(nameof(InvalidProviders))]
public void CollectLinuxCommandProviderConfigurationConsolidation_Throws(object testArgs, string[] expectedException)
{
MockConsole console = new(200, 30);
int exitCode = Run(testArgs, console);
if (OperatingSystem.IsLinux())
{
Assert.Equal((int)ReturnCode.TracingError, exitCode);
console.AssertSanitizedLinesEqual(null, true, expectedLines: expectedException);
}
else
{
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
console.AssertSanitizedLinesEqual(null, expectedLines: new string[] {
"The collect-linux command is only supported on Linux.",
});
}
}

private static int Run(object args, MockConsole console)
{
var handler = new CollectLinuxCommandHandler(console);
handler.RecordTraceInvoker = (cmd, len, cb) => 0;
return handler.CollectLinux((CollectLinuxCommandHandler.CollectLinuxArgs)args);
}


public static IEnumerable<object[]> BasicCases()
{
yield return new object[] {
Expand Down Expand Up @@ -98,7 +125,7 @@ public static IEnumerable<object[]> BasicCases()
}
};
yield return new object[] {
TestArgs(profiles: new[]{"cpu-sampling"}),
TestArgs(profile: new[]{"cpu-sampling"}),
new string[] {
"No .NET providers were configured.",
"",
Expand All @@ -108,7 +135,7 @@ public static IEnumerable<object[]> BasicCases()
}
};
yield return new object[] {
TestArgs(providers: new[]{"Foo:0x1:4"}, profiles: new[]{"cpu-sampling"}),
TestArgs(providers: new[]{"Foo:0x1:4"}, profile: new[]{"cpu-sampling"}),
new string[] {
"",
ProviderHeader,
Expand All @@ -120,7 +147,7 @@ public static IEnumerable<object[]> BasicCases()
}
};
yield return new object[] {
TestArgs(clrEvents: "gc", profiles: new[]{"cpu-sampling"}),
TestArgs(clrEvents: "gc", profile: new[]{"cpu-sampling"}),
new string[] {
"",
ProviderHeader,
Expand All @@ -132,7 +159,7 @@ public static IEnumerable<object[]> BasicCases()
}
};
yield return new object[] {
TestArgs(providers: new[]{"Microsoft-Windows-DotNETRuntime:0x1:4"}, profiles: new[]{"cpu-sampling"}),
TestArgs(providers: new[]{"Microsoft-Windows-DotNETRuntime:0x1:4"}, profile: new[]{"cpu-sampling"}),
new string[] {
"",
ProviderHeader,
Expand Down Expand Up @@ -189,6 +216,39 @@ public static IEnumerable<object[]> BasicCases()
};
}

public static IEnumerable<object[]> InvalidProviders()
{
yield return new object[]
{
TestArgs(profile: new[] { "dotnet-sampled-thread-time" }),
new [] { FormatException("The specified profile 'dotnet-sampled-thread-time' does not apply to `dotnet-trace collect-linux`.", "System.ArgumentException") }
};

yield return new object[]
{
TestArgs(profile: new[] { "unknown" }),
new [] { FormatException("Invalid profile name: unknown", "System.ArgumentException") }
};

yield return new object[]
{
TestArgs(providers: new[] { "Foo:::Bar=0", "Foo:::Bar=1" }),
new [] { FormatException($"Provider \"Foo\" is declared multiple times with filter arguments.", "System.ArgumentException") }
};

yield return new object[]
{
TestArgs(clrEvents: "unknown"),
new [] { FormatException("unknown is not a valid CLR event keyword", "System.ArgumentException") }
};

yield return new object[]
{
TestArgs(clrEvents: "gc", clrEventLevel: "unknown"),
new [] { FormatException("Unknown EventLevel: unknown", "System.ArgumentException") }
};
}

private const string ProviderHeader = "Provider Name Keywords Level Enabled By";
private static string LinuxHeader => $"{"Linux Events",-80}Enabled By";
private static string LinuxProfile(string name) => $"{name,-80}--profile";
Expand All @@ -200,5 +260,6 @@ private static string FormatProvider(string name, string keywordsHex, string lev
string.Format("{0, -8}", $"{levelName}({levelValue})");
return string.Format("{0, -80}", display) + enabledBy;
}
private static string FormatException(string message, string exceptionType) => $"[ERROR] {exceptionType}: {message}";
}
}