Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standardize EventSource behavior in ETW for null values #77172

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,78 @@ public unsafe void Test_WriteEvent_ArgsBasicTypes()

#endregion

#region Validate "null" arguments

log.EventS(null);
Assert.Equal(8, LoudListener.t_lastEvent.EventId);
Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);

log.EventSS(null, null);
Assert.Equal(9, LoudListener.t_lastEvent.EventId);
Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[1]);

log.EventSSS(null, null, null);
Assert.Equal(10, LoudListener.t_lastEvent.EventId);
Assert.Equal(3, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[1]);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[2]);

log.EventSI(null, 10);
Assert.Equal(11, LoudListener.t_lastEvent.EventId);
Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[1]);

log.EventSL(null, 10);
Assert.Equal(12, LoudListener.t_lastEvent.EventId);
Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal(10, (long)LoudListener.t_lastEvent.Payload[1]);

log.EventSII(null, 10, 11);
Assert.Equal(13, LoudListener.t_lastEvent.EventId);
Assert.Equal(3, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[1]);
Assert.Equal(11, (int)LoudListener.t_lastEvent.Payload[2]);

log.EventWithLongAndString(10, null);
Assert.Equal(43, LoudListener.t_lastEvent.EventId);
Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(10, (long)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[1]);

log.EventWithIntAndString(10, null);
Assert.Equal(42, LoudListener.t_lastEvent.EventId);
Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[1]);

log.EventWithByteArray(null);
Assert.Equal(52, LoudListener.t_lastEvent.EventId);
Assert.Equal(1, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(new byte[0], (byte[])LoudListener.t_lastEvent.Payload[0]);

log.EventWithLongAndByteArray(10, null);
Assert.Equal(55, LoudListener.t_lastEvent.EventId);
Assert.Equal(2, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal(10, (long)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal(new byte[0], (byte[])LoudListener.t_lastEvent.Payload[1]);

log.EventWithFallbackArgs(null, 10, 11, 12);
Assert.Equal(56, LoudListener.t_lastEvent.EventId);
Assert.Equal(4, LoudListener.t_lastEvent.Payload.Count);
Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);
Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[1]);
Assert.Equal(11, (float)LoudListener.t_lastEvent.Payload[2]);
Assert.Equal(12, (long)LoudListener.t_lastEvent.Payload[3]);

#endregion

#region Validate DateTime
Test_WriteEvent_ArgsBasicTypes_Etw_Validate_DateTime(log);
#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,18 @@ public unsafe void EventWithBytePointer(byte* ptr, int length)
WriteEventCore(54, 1, &data);
}

[Event(55)]
public unsafe void EventWithLongAndByteArray(long l, byte[] arr)
{
this.WriteEvent(55, l, arr);
}

[Event(56)]
public unsafe void EventWithFallbackArgs(string str, int i, float f, long l)
{
this.WriteEvent(56, str, i, f, l);
}

#region Keywords / Tasks /Opcodes / Channels
public class Keywords
{
Expand Down
80 changes: 80 additions & 0 deletions src/tests/tracing/eventpipe/nullevent/ETWNullEvent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using BasicEventSourceTests;
using Microsoft.Diagnostics.NETCore.Client;
using Microsoft.Diagnostics.Tracing;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Tracing;
using System.Linq;
using System.Threading.Tasks;
using Tracing.Tests.Common;
using Xunit;

[EventSource(Name = "Test.EventSourceNull")]
class EventSourceNullTest : EventSource
{
[Event(1)]
public void EventNullString(string str, int i, float f, long l)
{
WriteEvent(1, str, i, f, l);
}

[Event(2)]
public void EventNullByteArray(byte[] bytes, int i, float f, long l)
{
WriteEvent(2, bytes, i, f, l);
}
}


namespace Tracing.Tests.ETWNullEvent
{
public class ProviderValidation
{
public static void Main(string[] args)
{
List<EventPipeProvider> providers = new List<EventPipeProvider>
{
new EventPipeProvider("Test.EventSourceNull", EventLevel.Verbose)
};

int processId = Process.GetCurrentProcess().Id;
DiagnosticsClient client = new DiagnosticsClient(processId);
using (EventPipeSession session = client.StartEventPipeSession(providers, /* requestRunDown */ false))
{

using (var log = new EventSourceNullTest())
{
log.EventNullString(null, 10, 11, 12);
var events = new EventPipeEventSource(session.EventStream);
events.Process();
// using (var el = new LoudListener(log))
// {
// log.EventNullString(null, 10, 11, 12);
// Assert.Equal(1, LoudListener.t_lastEvent.EventId);
// Assert.Equal(4, LoudListener.t_lastEvent.Payload.Count);
// Assert.Equal("", (string)LoudListener.t_lastEvent.Payload[0]);
// Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[1]);
// Assert.Equal(11, (float)LoudListener.t_lastEvent.Payload[2]);
// Assert.Equal(12, (long)LoudListener.t_lastEvent.Payload[3]);

// log.EventNullByteArray(null, 10, 11, 12);
// Assert.Equal(2, LoudListener.t_lastEvent.EventId);
// Assert.Equal(4, LoudListener.t_lastEvent.Payload.Count);
// Assert.Equal(new byte[0], (byte[])LoudListener.t_lastEvent.Payload[0]);
// Assert.Equal(10, (int)LoudListener.t_lastEvent.Payload[1]);
// Assert.Equal(11, (float)LoudListener.t_lastEvent.Payload[2]);
// Assert.Equal(12, (long)LoudListener.t_lastEvent.Payload[3]);

// var events = new EventPipeEventSource(session.EventStream);
// events.Process();
// }
session.Stop();
}
}
}
}
}
18 changes: 18 additions & 0 deletions src/tests/tracing/eventpipe/nullevent/ETWNullEvent.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)</TargetFrameworks>
<TestRuntime>true</TestRuntime>
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
</PropertyGroup>
<PropertyGroup>
<RuntimeComponents Condition="'$(TargetsAppleMobile)' == 'true' or '$(TargetOS)' == 'Android'">diagnostics_tracing;marshal-ilgen</RuntimeComponents>
</PropertyGroup>
<ItemGroup>
<Compile Include="ETWNullEvent.cs" />
<Compile Include="..\..\..\..\libraries\System.Diagnostics.Tracing\tests\BasicEventSourceTest\LoudListener.cs" />
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="$(TraceEventVersion)" />
<ProjectReference Include="../common/common.csproj" />
<ProjectReference Include="../common/Microsoft.Diagnostics.NETCore.Client/Microsoft.Diagnostics.NETCore.Client.csproj" />
</ItemGroup>
</Project>