Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/Tools/Common/Commands/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tools.Common;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,33 @@ public CollectLinuxCommandHandler(IConsole console = null)
rewriter = new LineRewriter(Console);
}

internal static bool IsSupported()
{
bool isSupportedLinuxPlatform = false;
if (OperatingSystem.IsLinux())
{
isSupportedLinuxPlatform = true;
try
{
string ostype = File.ReadAllText("/etc/os-release");
isSupportedLinuxPlatform = !ostype.Contains("ID=alpine");
}
catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException or IOException) {}
}

return isSupportedLinuxPlatform;
}

/// <summary>
/// Collects diagnostic traces using perf_events, a Linux OS technology. collect-linux requires admin privileges to capture kernel- and user-mode events, and by default, captures events from all processes.
/// This Linux-only command includes the same .NET events as dotnet-trace collect, and it uses the kernel’s user_events mechanism to emit .NET events as perf events, enabling unification of user-space .NET events with kernel-space system events.
/// </summary>
internal int CollectLinux(CollectLinuxArgs args)
{
if (!OperatingSystem.IsLinux())
if (!IsSupported())
{
Console.Error.WriteLine("The collect-linux command is only supported on Linux.");
Console.Error.WriteLine("The collect-linux command is not supported on this platform.");
Console.Error.WriteLine("For requirements, please visit https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace.");
return (int)ReturnCode.PlatformNotSupportedError;
}

Expand Down
10 changes: 6 additions & 4 deletions src/tests/dotnet-trace/CollectLinuxCommandFunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void CollectLinuxCommandProviderConfigurationConsolidation(object testArg
{
MockConsole console = new(200, 30);
int exitCode = Run(testArgs, console);
if (OperatingSystem.IsLinux())
if (CollectLinuxCommandHandler.IsSupported())
{
Assert.Equal((int)ReturnCode.Ok, exitCode);
console.AssertSanitizedLinesEqual(CollectLinuxSanitizer, expectedLines);
Expand All @@ -57,7 +57,8 @@ public void CollectLinuxCommandProviderConfigurationConsolidation(object testArg
{
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
console.AssertSanitizedLinesEqual(null, new string[] {
"The collect-linux command is only supported on Linux.",
$"The collect-linux command is not supported on this platform.",
$"For requirements, please visit https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace."
});
}
}
Expand All @@ -68,7 +69,7 @@ public void CollectLinuxCommandProviderConfigurationConsolidation_Throws(object
{
MockConsole console = new(200, 30);
int exitCode = Run(testArgs, console);
if (OperatingSystem.IsLinux())
if (CollectLinuxCommandHandler.IsSupported())
{
Assert.Equal((int)ReturnCode.TracingError, exitCode);
console.AssertSanitizedLinesEqual(null, expectedException);
Expand All @@ -77,7 +78,8 @@ public void CollectLinuxCommandProviderConfigurationConsolidation_Throws(object
{
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
console.AssertSanitizedLinesEqual(null, new string[] {
"The collect-linux command is only supported on Linux.",
$"The collect-linux command is not supported on this platform.",
$"For requirements, please visit https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace."
});
}
}
Expand Down