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
103 changes: 100 additions & 3 deletions src/Tools/dotnet-trace/CommandLine/Commands/CollectLinuxCommand.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.Internal.Common.Utils;

namespace Microsoft.Diagnostics.Tools.Trace
{
internal static class CollectLinuxCommandHandler
internal static partial class CollectLinuxCommandHandler
{
private static int s_recordStatus;

internal sealed record CollectLinuxArgs(
string[] Providers,
string ClrEventLevel,
Expand All @@ -30,7 +35,7 @@ private static int CollectLinux(CollectLinuxArgs args)
if (!OperatingSystem.IsLinux())
{
Console.Error.WriteLine("The collect-linux command is only supported on Linux.");
return (int)ReturnCode.PlatformNotSupportedError;
return (int)ReturnCode.ArgumentError;
}

return RunRecordTrace(args);
Expand Down Expand Up @@ -76,13 +81,105 @@ public static Command CollectLinuxCommand()

private static int RunRecordTrace(CollectLinuxArgs args)
{
return (int)ReturnCode.Ok;
s_recordStatus = 0;

ConsoleCancelEventHandler handler = (sender, e) =>
{
e.Cancel = true;
s_recordStatus = 1;
};
Console.CancelKeyPress += handler;

IEnumerable<string> recordTraceArgList = BuildRecordTraceArgs(args, out string scriptPath);

string options = string.Join(' ', recordTraceArgList);
byte[] command = Encoding.UTF8.GetBytes(options);
int rc;
try
{
rc = RecordTrace(command, (UIntPtr)command.Length, OutputHandler);
}
finally
{
Console.CancelKeyPress -= handler;
if (!string.IsNullOrEmpty(scriptPath))
{
try {
if (File.Exists(scriptPath))
{
File.Delete(scriptPath);
}
} catch { }
}
}

return rc;
}

private static List<string> BuildRecordTraceArgs(CollectLinuxArgs args, out string scriptPath)
{
Console.WriteLine($"{args.ProcessId}");
List<string> recordTraceArgs = new();

recordTraceArgs.Add("--on-cpu");

return recordTraceArgs;
}

private static int OutputHandler(uint type, IntPtr data, UIntPtr dataLen)
{
OutputType ot = (OutputType)type;
if (ot != OutputType.Progress)
{
int len = checked((int)dataLen);
if (len > 0)
{
byte[] buffer = new byte[len];
Marshal.Copy(data, buffer, 0, len);
string text = Encoding.UTF8.GetString(buffer);
switch (ot)
{
case OutputType.Normal:
case OutputType.Live:
Console.Out.WriteLine(text);
break;
case OutputType.Error:
Console.Error.WriteLine(text);
break;
default:
Console.Error.WriteLine($"[{ot}] {text}");
break;
}
}
}

return s_recordStatus;
}

private static readonly Option<string> PerfEventsOption =
new("--perf-events")
{
Description = @"Comma-separated list of kernel perf events (e.g. syscalls:sys_enter_execve,sched:sched_switch)."
};

private enum OutputType : uint
{
Normal = 0,
Live = 1,
Error = 2,
Progress = 3,
}

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int recordTraceCallback(
[In] uint type,
[In] IntPtr data,
[In] UIntPtr dataLen);

[LibraryImport("recordtrace")]
private static partial int RecordTrace(
byte[] command,
UIntPtr commandLen,
recordTraceCallback callback);
}
}
15 changes: 15 additions & 0 deletions src/Tools/dotnet-trace/dotnet-trace.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ItemGroup>
<PackageReference Include="System.CommandLine" Version="$(SystemCommandLineVersion)" />
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="$(MicrosoftDiagnosticsTracingTraceEventVersion)" GeneratePathProperty="true" />
<PackageReference Include="Microsoft.OneCollect.RecordTrace" Version="0.1.31522" PrivateAssets="All" GeneratePathProperty="true" />
</ItemGroup>

<ItemGroup>
Expand All @@ -29,4 +30,18 @@
<InternalsVisibleTo Include="DotnetTrace.UnitTests" />
</ItemGroup>

<Target Name="ResolveRecordTraceNative" BeforeTargets="CopyFilesToOutputDirectory">
<PropertyGroup>
<_RecordTraceFromPackage>$(PkgMicrosoft_OneCollect_RecordTrace)/runtimes/$(RuntimeIdentifier)/native/librecordtrace.so</_RecordTraceFromPackage>
</PropertyGroup>

<ItemGroup>
<_RecordTraceResolved Include="$(_RecordTraceLocal)" Condition="'$(RuntimeIdentifier)' != '' AND Exists('$(_RecordTraceLocal)')" />
<_RecordTraceResolved Include="$(_RecordTraceFromPackage)" Condition="'@(_RecordTraceResolved)' == '' AND '$(RuntimeIdentifier)' != '' AND Exists('$(_RecordTraceFromPackage)')" />
</ItemGroup>

<ItemGroup>
<None Include="@(_RecordTraceResolved)" CopyToOutputDirectory="PreserveNewest" Condition="'@(_RecordTraceResolved)' != ''" />
</ItemGroup>
</Target>
</Project>