Skip to content

Commit 0184d7c

Browse files
committed
Merge remote-tracking branch 'upstream/main' into enable_command_utils_console_redirection
2 parents f35c402 + 39435e1 commit 0184d7c

File tree

5 files changed

+48
-16
lines changed

5 files changed

+48
-16
lines changed

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
<MicrosoftExtensionsConfigurationVersion>8.0.0</MicrosoftExtensionsConfigurationVersion>
3737
<MicrosoftExtensionsConfigurationJsonVersion>8.0.1</MicrosoftExtensionsConfigurationJsonVersion>
3838
<MicrosoftExtensionsLoggingConfigurationVersion>8.0.1</MicrosoftExtensionsLoggingConfigurationVersion>
39+
<MicrosoftOneCollectRecordTraceVersions>0.1.32221</MicrosoftOneCollectRecordTraceVersions>
3940
<!-- Need version that understands UseAppFilters sentinel. -->
4041
<MicrosoftExtensionsLoggingEventSourceVersion>5.0.1</MicrosoftExtensionsLoggingEventSourceVersion>
4142
<SystemComponentModelAnnotationsVersion>5.0.0</SystemComponentModelAnnotationsVersion>

src/Tools/Common/CommandUtils.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Diagnostics;
65
using System.Collections.Generic;
6+
using System.Diagnostics;
77
using Microsoft.Diagnostics.NETCore.Client;
88
using Microsoft.Diagnostics.Tools;
99

src/Tools/dotnet-trace/CommandLine/Commands/CollectLinuxCommand.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,33 @@ public CollectLinuxCommandHandler(IConsole console = null)
4141
rewriter = new LineRewriter(Console);
4242
}
4343

44+
internal static bool IsSupported()
45+
{
46+
bool isSupportedLinuxPlatform = false;
47+
if (OperatingSystem.IsLinux())
48+
{
49+
isSupportedLinuxPlatform = true;
50+
try
51+
{
52+
string ostype = File.ReadAllText("/etc/os-release");
53+
isSupportedLinuxPlatform = !ostype.Contains("ID=alpine");
54+
}
55+
catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException or IOException) {}
56+
}
57+
58+
return isSupportedLinuxPlatform;
59+
}
60+
4461
/// <summary>
4562
/// 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.
4663
/// 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.
4764
/// </summary>
4865
internal int CollectLinux(CollectLinuxArgs args)
4966
{
50-
if (!OperatingSystem.IsLinux())
67+
if (!IsSupported())
5168
{
52-
Console.Error.WriteLine("The collect-linux command is only supported on Linux.");
69+
Console.Error.WriteLine("The collect-linux command is not supported on this platform.");
70+
Console.Error.WriteLine("For requirements, please visit https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace.");
5371
return (int)ReturnCode.PlatformNotSupportedError;
5472
}
5573

@@ -92,6 +110,12 @@ internal int CollectLinux(CollectLinuxArgs args)
92110
Console.Error.WriteLine($"[ERROR] {dte.Message}");
93111
ret = (int)dte.ReturnCode;
94112
}
113+
catch (DllNotFoundException dnfe)
114+
{
115+
Console.Error.WriteLine($"[ERROR] Could not find or load dependencies for collect-linux. For requirements, please visit https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace");
116+
Console.Error.WriteLine($"[ERROR] {dnfe.Message}");
117+
ret = (int)ReturnCode.PlatformNotSupportedError;
118+
}
95119
catch (Exception ex)
96120
{
97121
Console.Error.WriteLine($"[ERROR] {ex}");

src/Tools/dotnet-trace/dotnet-trace.csproj

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@
1414
<ItemGroup>
1515
<PackageReference Include="System.CommandLine" Version="$(SystemCommandLineVersion)" />
1616
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="$(MicrosoftDiagnosticsTracingTraceEventVersion)" GeneratePathProperty="true" />
17-
<PackageReference Include="Microsoft.OneCollect.RecordTrace" Version="0.1.32221" PrivateAssets="All" GeneratePathProperty="true"
18-
Condition="'$(TargetRid)' != '' and $([System.String]::Copy('$(TargetRid)').StartsWith('linux'))" />
17+
<PackageReference Include="Microsoft.OneCollect.RecordTrace" Version="$(MicrosoftOneCollectRecordTraceVersions)" PrivateAssets="All" GeneratePathProperty="true" />
1918
</ItemGroup>
2019

2120
<ItemGroup>
@@ -31,12 +30,18 @@
3130
<InternalsVisibleTo Include="DotnetTrace.UnitTests" />
3231
</ItemGroup>
3332

34-
<ItemGroup Condition="'$(TargetRid)' != '' and $([System.String]::Copy('$(TargetRid)').StartsWith('linux'))">
35-
<None Include="$(PkgMicrosoft_OneCollect_RecordTrace)/runtimes/$(TargetRid)/native/librecordtrace.so"
36-
Pack="true"
37-
Visible="false"
38-
CopyToOutputDirectory="PreserveNewest"
39-
CopyToPublishDirectory="PreserveNewest"
40-
Condition="Exists('$(PkgMicrosoft_OneCollect_RecordTrace)/runtimes/$(TargetRid)/native/librecordtrace.so')" />
33+
<ItemGroup>
34+
<None Include="$(PkgMicrosoft_OneCollect_RecordTrace)/runtimes/linux-x64/native/librecordtrace.so">
35+
<Visible>false</Visible>
36+
<Link Condition="'$(PublishSingleFile)' != 'true'">runtimes/linux-x64/native/librecordtrace.so</Link>
37+
<CopyToOutputDirectory Condition="'$(PublishSingleFile)' != 'true'">PreserveNewest</CopyToOutputDirectory>
38+
<CopyToPublishDirectory Condition="'$(PublishSingleFile)' == 'true' and 'linux-x64' == '$(RuntimeIdentifier)'">PreserveNewest</CopyToPublishDirectory>
39+
</None>
40+
<None Include="$(PkgMicrosoft_OneCollect_RecordTrace)/runtimes/linux-arm64/native/librecordtrace.so">
41+
<Visible>false</Visible>
42+
<Link Condition="'$(PublishSingleFile)' != 'true'">runtimes/linux-arm64/native/librecordtrace.so</Link>
43+
<CopyToOutputDirectory Condition="'$(PublishSingleFile)' != 'true'">PreserveNewest</CopyToOutputDirectory>
44+
<CopyToPublishDirectory Condition="'$(PublishSingleFile)' == 'true' and 'linux-arm64' == '$(RuntimeIdentifier)'">PreserveNewest</CopyToPublishDirectory>
45+
</None>
4146
</ItemGroup>
4247
</Project>

src/tests/dotnet-trace/CollectLinuxCommandFunctionalTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public void CollectLinuxCommandProviderConfigurationConsolidation(object testArg
4848
{
4949
MockConsole console = new(200, 30);
5050
int exitCode = Run(testArgs, console);
51-
if (OperatingSystem.IsLinux())
51+
if (CollectLinuxCommandHandler.IsSupported())
5252
{
5353
Assert.Equal((int)ReturnCode.Ok, exitCode);
5454
console.AssertSanitizedLinesEqual(CollectLinuxSanitizer, expectedLines);
@@ -57,7 +57,8 @@ public void CollectLinuxCommandProviderConfigurationConsolidation(object testArg
5757
{
5858
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
5959
console.AssertSanitizedLinesEqual(null, new string[] {
60-
"The collect-linux command is only supported on Linux.",
60+
$"The collect-linux command is not supported on this platform.",
61+
$"For requirements, please visit https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace."
6162
});
6263
}
6364
}
@@ -68,7 +69,7 @@ public void CollectLinuxCommandProviderConfigurationConsolidation_Throws(object
6869
{
6970
MockConsole console = new(200, 30);
7071
int exitCode = Run(testArgs, console);
71-
if (OperatingSystem.IsLinux())
72+
if (CollectLinuxCommandHandler.IsSupported())
7273
{
7374
Assert.Equal((int)ReturnCode.ArgumentError, exitCode);
7475
console.AssertSanitizedLinesEqual(null, expectedException);
@@ -77,7 +78,8 @@ public void CollectLinuxCommandProviderConfigurationConsolidation_Throws(object
7778
{
7879
Assert.Equal((int)ReturnCode.PlatformNotSupportedError, exitCode);
7980
console.AssertSanitizedLinesEqual(null, new string[] {
80-
"The collect-linux command is only supported on Linux.",
81+
$"The collect-linux command is not supported on this platform.",
82+
$"For requirements, please visit https://learn.microsoft.com/en-us/dotnet/core/diagnostics/dotnet-trace."
8183
});
8284
}
8385
}

0 commit comments

Comments
 (0)