|
| 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 | +} |
0 commit comments