From 1b821412796486ee362b1672a19b705275441b15 Mon Sep 17 00:00:00 2001 From: Jan Vorlicek Date: Tue, 22 Mar 2016 12:05:07 +0100 Subject: Fix exception to string in case of exception in callstack extraction This change fixes a problem when exception happens while converting an exception call stack to string. Without this change, Exception.ToString would fail and if the exception was unhandled, it would just print "Cannot print exception string because Exception.ToString() failed." without any details on what exception happened. The problem happens e.g. in case when a method on the call stack has a parameter that contains members of types from an assembly that cannot be resolved. The fix was to catch exception from parameter info extraction and if it happens, just exclude the parameter list from the reported frame. --- src/mscorlib/src/System/Diagnostics/Stacktrace.cs | 52 +++++++++++++++-------- 1 file changed, 34 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/mscorlib/src/System/Diagnostics/Stacktrace.cs b/src/mscorlib/src/System/Diagnostics/Stacktrace.cs index 897efc427b..85ec034eae 100644 --- a/src/mscorlib/src/System/Diagnostics/Stacktrace.cs +++ b/src/mscorlib/src/System/Diagnostics/Stacktrace.cs @@ -560,25 +560,41 @@ namespace System.Diagnostics { sb.Append(']'); } - // arguments printing - sb.Append('('); - ParameterInfo[] pi = mb.GetParameters(); - bool fFirstParam = true; - for (int j = 0; j < pi.Length; j++) + ParameterInfo[] pi = null; +#if FEATURE_CORECLR + try + { +#endif + pi = mb.GetParameters(); +#if FEATURE_CORECLR + } + catch + { + // The parameter info cannot be loaded, so we don't + // append the parameter list. + } +#endif + if (pi != null) { - if (fFirstParam == false) - sb.Append(", "); - else - fFirstParam = false; - - String typeName = ""; - if (pi[j].ParameterType != null) - typeName = pi[j].ParameterType.Name; - sb.Append(typeName); - sb.Append(' '); - sb.Append(pi[j].Name); - } - sb.Append(')'); + // arguments printing + sb.Append('('); + bool fFirstParam = true; + for (int j = 0; j < pi.Length; j++) + { + if (fFirstParam == false) + sb.Append(", "); + else + fFirstParam = false; + + String typeName = ""; + if (pi[j].ParameterType != null) + typeName = pi[j].ParameterType.Name; + sb.Append(typeName); + sb.Append(' '); + sb.Append(pi[j].Name); + } + sb.Append(')'); + } // source location printing if (displayFilenames && (sf.GetILOffset() != -1)) -- cgit v1.2.3