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
14 changes: 9 additions & 5 deletions src/TraceEvent/PredefinedDynamicEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,20 @@ protected internal override Delegate Target
}

/// <summary>
/// Creates a clone of this template.
/// Copies this event, or clones an unpopulated template for registration.
/// </summary>
/// <returns>A new instance of the template</returns>
/// <returns>An independent event copy or template.</returns>
public override unsafe TraceEvent Clone()
{
var clone = (PredefinedDynamicEvent)base.Clone();

// Reset source-specific fields to ensure the clone can be registered with a new source
clone.traceEventSource = null;
clone.Target = null;
// Reset source-specific fields only for templates so they can be registered with a new source.
// Populated event copies retain their source and callback, matching the base Clone behavior.
if (eventRecord == null)
{
clone.traceEventSource = null;
clone.Target = null;
}

return clone;
}
Expand Down
43 changes: 42 additions & 1 deletion src/TraceEvent/TraceEvent.Tests/Universal/UniversalTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.Diagnostics.Tracing.Parsers.Universal.Events;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Etlx;
using Microsoft.Diagnostics.Tracing.Parsers.Universal.Events;
using System;
using Xunit;
using Xunit.Abstractions;
Expand All @@ -12,6 +14,45 @@ public UniversalTests(ITestOutputHelper output)
{
}

[Theory]
[InlineData("cpu")]
[InlineData("cswitch")]
public unsafe void SampleClone_PreservesSourceAndCallback(string eventName)
{
using var source = (TraceLog)Activator.CreateInstance(typeof(TraceLog), true);
Action<TraceEvent> callback = _ => { };
TraceEventNativeMethods.EVENT_RECORD record = default;
var original = new SampleTraceData(eventName)
{
traceEventSource = source,
eventRecord = &record,
Target = callback,
};

var clone = original.Clone();

Assert.Same(source, clone.Source);
Assert.Same(callback, clone.Target);
}

[Fact]
public void SampleTemplateClone_ClearsSourceAndCallback()
{
using var source = (TraceLog)Activator.CreateInstance(typeof(TraceLog), true);
var template = new SampleTraceData("cpu")
{
traceEventSource = source,
Target = new Action<TraceEvent>(_ => { }),
};

var clone = template.Clone();

Assert.Null(clone.Source);
Assert.Null(clone.Target);
Assert.Same(source, template.Source);
Assert.NotNull(template.Target);
}

[Fact]
public void ProcessMappingMetadataParser_Empty()
{
Expand Down