From b8e0c6eb922b4d3d3abc411b5d953b2b5d3748c2 Mon Sep 17 00:00:00 2001 From: Jan Kotas Date: Fri, 10 Mar 2017 21:39:25 -0800 Subject: [PATCH] Strip internal frame in Environment.StackTrace Contributes to dotnet/coreclr#6209 Commit migrated from https://github.com/dotnet/coreclr/commit/7e50e48a24c12e19d0a0f52ac1b3d74e5d1dac77 --- .../src/Internal/Runtime/Augments/EnvironmentAugments.cs | 12 +++++++++++- src/coreclr/src/mscorlib/src/System/Environment.cs | 4 ++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/coreclr/src/mscorlib/src/Internal/Runtime/Augments/EnvironmentAugments.cs b/src/coreclr/src/mscorlib/src/Internal/Runtime/Augments/EnvironmentAugments.cs index b22310e..b5a56b8 100644 --- a/src/coreclr/src/mscorlib/src/Internal/Runtime/Augments/EnvironmentAugments.cs +++ b/src/coreclr/src/mscorlib/src/Internal/Runtime/Augments/EnvironmentAugments.cs @@ -4,6 +4,8 @@ using System; using System.Collections; +using System.Diagnostics; +using System.Runtime.CompilerServices; namespace Internal.Runtime.Augments { @@ -16,7 +18,6 @@ namespace Internal.Runtime.Augments public static void FailFast(string message, Exception error) => Environment.FailFast(message, error); public static string[] GetCommandLineArgs() => Environment.GetCommandLineArgs(); public static bool HasShutdownStarted => Environment.HasShutdownStarted; - public static string StackTrace => Environment.StackTrace; public static int TickCount => Environment.TickCount; public static string GetEnvironmentVariable(string variable) => Environment.GetEnvironmentVariable(variable); public static string GetEnvironmentVariable(string variable, EnvironmentVariableTarget target) => Environment.GetEnvironmentVariable(variable, target); @@ -24,5 +25,14 @@ namespace Internal.Runtime.Augments public static IDictionary GetEnvironmentVariables(EnvironmentVariableTarget target) => Environment.GetEnvironmentVariables(target); public static void SetEnvironmentVariable(string variable, string value) => Environment.SetEnvironmentVariable(variable, value); public static void SetEnvironmentVariable(string variable, string value, EnvironmentVariableTarget target) => Environment.SetEnvironmentVariable(variable, value, target); + + public static string StackTrace + { + [MethodImpl(MethodImplOptions.NoInlining)] // Prevent inlining from affecting where the stacktrace starts + get + { + return new StackTrace(1 /* skip this one frame */, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal); + } + } } } diff --git a/src/coreclr/src/mscorlib/src/System/Environment.cs b/src/coreclr/src/mscorlib/src/System/Environment.cs index 4f1971c..9234dcc 100644 --- a/src/coreclr/src/mscorlib/src/System/Environment.cs +++ b/src/coreclr/src/mscorlib/src/System/Environment.cs @@ -576,11 +576,11 @@ namespace System ==============================================================================*/ public static String StackTrace { + [MethodImpl(MethodImplOptions.NoInlining)] // Prevent inlining from affecting where the stacktrace starts get { Contract.Ensures(Contract.Result() != null); - - return GetStackTrace(null, true); + return global::Internal.Runtime.Augments.EnvironmentAugments.StackTrace; } } -- 2.7.4