Skip to content

Commit a46358c

Browse files
authored
Add some tests that validate debug info through ETW events (#61962)
Validate the debug mappings generated by the JIT using the MethodILToNative event. Unfortunately we can not use EventListener as the event there does not contain the actual mappings (#12678) so this reuses some of the facilities from the tracing tests to use EventPipe and TraceEvent. This only adds the infrastructure and a small number of tests, but at least this should make it easier to add more tests in this area in the future. There are some more limitations, for example we cannot validate the CALL_INSTRUCTION mappings generated for the managed return value feature because the debugger filters them out of the table reported. I am hoping we can change these mappings to be included as normal in the future. The tests themselves are added by adding a method to tests.il with an ExpectedILMappings attribute that allows specifying a subset of IL offsets that we expect mappings to be generated for, with separate subsets under Debug and when optimizing. This was the best way I could think of to be able to refer to the right IL offsets.
1 parent 90773ac commit a46358c

File tree

11 files changed

+492
-0
lines changed

11 files changed

+492
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This directory contains tests for debugging information generated by the JIT.
2+
The tests are written in IL inside the tests.il file by creating a method in the
3+
DebugInfoMethods class and marking them with the ExpectedILMappings attribute.
4+
In that attribute, the IL offsets at which mappings are expected to be generated
5+
can be specified for both debug (DebuggableAttribute with
6+
DisableOptimizations) and optimized builds.
7+
8+
To debug these tests, run the 'tester' project, which will JIT all methods in
9+
tests.il in both debug and release (you may need to turn off tiered
10+
compilation).
11+
12+
* attribute.cs/csproj: Project containing ExpectedILMappingsAttribute, to avoid
13+
circular dependencies
14+
15+
* tests.il: File containing the tests marked with ExpectedILMappings
16+
17+
* tests_d.ilproj/tests_r.ilproj: Both these projects just add tests.il, the only
18+
difference is that the former has DebuggableAttribute with
19+
DisableOptimizations and the latter does not.
20+
21+
* tester.cs/csproj: The orchestrator of the tests, references tests_d and
22+
tests_r and jits the test methods using RuntimeHelpers.PrepareMethod, collects
23+
the IL mappings emitted using runtime events, and validates that the mappings
24+
match the data in ExpectedILMappings.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
6+
[AttributeUsage(AttributeTargets.Method)]
7+
public class ExpectedILMappings : Attribute
8+
{
9+
public int[] Debug { get; set; }
10+
public int[] Opts { get; set; }
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Library</OutputType>
4+
<Optimize>false</Optimize>
5+
<CLRTestKind>BuildOnly</CLRTestKind>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<Compile Include="$(MSBuildProjectName).cs" />
9+
</ItemGroup>
10+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#define DEBUG
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
extern alias tests_d;
2+
extern alias tests_r;
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Diagnostics;
7+
using System.Diagnostics.Tracing;
8+
using System.Linq;
9+
using System.Reflection;
10+
using System.Runtime.CompilerServices;
11+
using Microsoft.Diagnostics.Tools.RuntimeClient;
12+
using Microsoft.Diagnostics.Tracing;
13+
using Microsoft.Diagnostics.Tracing.Parsers;
14+
using Microsoft.Diagnostics.Tracing.Parsers.Clr;
15+
using Tracing.Tests.Common;
16+
using DebugInfoMethodsD = tests_d::DebugInfoMethods;
17+
using DebugInfoMethodsR = tests_r::DebugInfoMethods;
18+
19+
public unsafe class DebugInfoTest
20+
{
21+
public static unsafe int Main()
22+
{
23+
var keywords =
24+
ClrTraceEventParser.Keywords.Jit | ClrTraceEventParser.Keywords.JittedMethodILToNativeMap;
25+
26+
var dotnetRuntimeProvider = new List<Provider>
27+
{
28+
new Provider("Microsoft-Windows-DotNETRuntime", eventLevel: EventLevel.Verbose, keywords: (ulong)keywords)
29+
};
30+
31+
var config = new SessionConfiguration(1024, EventPipeSerializationFormat.NetTrace, dotnetRuntimeProvider);
32+
33+
return
34+
IpcTraceTest.RunAndValidateEventCounts(
35+
new Dictionary<string, ExpectedEventCount>(),
36+
JitMethods,
37+
config,
38+
ValidateMappings);
39+
}
40+
41+
private static void JitMethods()
42+
{
43+
ProcessType(typeof(DebugInfoMethodsD));
44+
ProcessType(typeof(DebugInfoMethodsR));
45+
}
46+
47+
private static void ProcessType(Type t)
48+
{
49+
foreach (MethodInfo mi in t.GetMethods())
50+
{
51+
if (mi.GetCustomAttribute<ExpectedILMappings>() != null)
52+
{
53+
RuntimeHelpers.PrepareMethod(mi.MethodHandle);
54+
}
55+
}
56+
}
57+
58+
private static Func<int> ValidateMappings(EventPipeEventSource source)
59+
{
60+
List<(long MethodID, OptimizationTier Tier, (int ILOffset, int NativeOffset)[] Mappings)> methodsWithMappings = new();
61+
Dictionary<long, OptimizationTier> methodTier = new();
62+
63+
source.Clr.MethodLoad += e => methodTier[e.MethodID] = e.OptimizationTier;
64+
source.Clr.MethodLoadVerbose += e => methodTier[e.MethodID] = e.OptimizationTier;
65+
source.Clr.MethodILToNativeMap += e =>
66+
{
67+
var mappings = new (int, int)[e.CountOfMapEntries];
68+
for (int i = 0; i < mappings.Length; i++)
69+
mappings[i] = (e.ILOffset(i), e.NativeOffset(i));
70+
71+
if (!methodTier.TryGetValue(e.MethodID, out OptimizationTier tier))
72+
tier = OptimizationTier.Unknown;
73+
74+
methodsWithMappings.Add((e.MethodID, tier, mappings));
75+
};
76+
77+
return () =>
78+
{
79+
int result = 100;
80+
foreach ((long methodID, OptimizationTier tier, (int ILOffset, int NativeOffset)[] mappings) in methodsWithMappings)
81+
{
82+
MethodBase meth = s_getMethodBaseByHandle(null, (IntPtr)(void*)methodID);
83+
ExpectedILMappings attrib = meth.GetCustomAttribute<ExpectedILMappings>();
84+
if (attrib == null)
85+
{
86+
continue;
87+
}
88+
89+
string name = $"[{meth.DeclaringType.Assembly.GetName().Name}]{meth.DeclaringType.FullName}.{meth.Name}";
90+
91+
// If DebuggableAttribute is saying that the assembly must be debuggable, then verify debug mappings.
92+
// Otherwise verify release mappings.
93+
// This may seem a little strange since we do not use the tier at all -- however, we expect debug
94+
// to never tier and in release, we expect the release mappings to be the "least common denominator",
95+
// i.e. tier0 and tier1 mappings should both be a superset.
96+
// Note that tier0 and MinOptJitted differs in mappings generated exactly due to DebuggableAttribute.
97+
DebuggableAttribute debuggableAttrib = meth.DeclaringType.Assembly.GetCustomAttribute<DebuggableAttribute>();
98+
bool debuggableMappings = debuggableAttrib != null && debuggableAttrib.IsJITOptimizerDisabled;
99+
100+
Console.WriteLine("{0}: Validate mappings for {1} codegen (tier: {2})", name, debuggableMappings ? "debuggable" : "optimized", tier);
101+
102+
int[] expected = debuggableMappings ? attrib.Debug : attrib.Opts;
103+
if (expected == null)
104+
{
105+
continue;
106+
}
107+
108+
if (!ValidateSingle(expected, mappings))
109+
{
110+
Console.WriteLine(" Validation failed: expected mappings at IL offsets {0}", string.Join(", ", expected.Select(il => $"{il:x3}")));
111+
Console.WriteLine(" Actual (IL <-> native):");
112+
foreach ((int ilOffset, int nativeOffset) in mappings)
113+
{
114+
string ilOffsetName = Enum.IsDefined((SpecialILOffset)ilOffset) ? ((SpecialILOffset)ilOffset).ToString() : $"{ilOffset:x3}";
115+
Console.WriteLine(" {0:x3} <-> {1:x3}", ilOffsetName, nativeOffset);
116+
}
117+
118+
result = -1;
119+
}
120+
}
121+
122+
return result;
123+
};
124+
}
125+
126+
// Validate that all IL offsets we expected had mappings generated for them.
127+
private static bool ValidateSingle(int[] expected, (int ILOffset, int NativeOffset)[] mappings)
128+
{
129+
return expected.All(il => mappings.Any(t => t.ILOffset == il));
130+
}
131+
132+
private enum SpecialILOffset
133+
{
134+
NoMapping = -1,
135+
Prolog = -2,
136+
Epilog = -3,
137+
}
138+
139+
static DebugInfoTest()
140+
{
141+
Type runtimeMethodHandleInternalType = typeof(RuntimeMethodHandle).Assembly.GetType("System.RuntimeMethodHandleInternal");
142+
Type runtimeTypeType = typeof(RuntimeMethodHandle).Assembly.GetType("System.RuntimeType");
143+
MethodInfo getMethodBaseMethod = runtimeTypeType.GetMethod("GetMethodBase", BindingFlags.NonPublic | BindingFlags.Static, new[] { runtimeTypeType, runtimeMethodHandleInternalType });
144+
s_getMethodBaseByHandle = (delegate*<object, IntPtr, MethodBase>)getMethodBaseMethod.MethodHandle .GetFunctionPointer();
145+
}
146+
147+
// Needed to go from MethodID -> MethodBase
148+
private static readonly delegate*<object, IntPtr, MethodBase> s_getMethodBaseByHandle;
149+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<OutputType>Exe</OutputType>
4+
<DebugType>PdbOnly</DebugType>
5+
<Optimize>True</Optimize>
6+
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<ProjectReference Include="tests_d.ilproj" Aliases="tests_d" />
10+
<ProjectReference Include="tests_r.ilproj" Aliases="tests_r" />
11+
<ProjectReference Include="attribute.csproj" />
12+
<ProjectReference Include="../../../../tracing/eventpipe/common/common.csproj" />
13+
<Compile Include="$(MSBuildProjectName).cs" />
14+
</ItemGroup>
15+
</Project>

0 commit comments

Comments
 (0)