diff options
author | Marek Safar <marek.safar@gmail.com> | 2015-08-17 17:25:04 +0300 |
---|---|---|
committer | Marek Safar <marek.safar@gmail.com> | 2015-08-17 17:28:16 +0300 |
commit | a22465351be5c8688fee6d6cf3029bd639696f05 (patch) | |
tree | 5b5edca4c779eafbe8c91493bfba238198583a53 | |
parent | 4fd7f72a553981a7713a285c8bdc9dc0eb7c7a42 (diff) |
[corlib] Propagate captured ExceptionDispatchInfo to user's StackTrace copies. Fixes #32918mono-4.2.0.179
3 files changed, 40 insertions, 21 deletions
diff --git a/mcs/class/corlib/System.Diagnostics/StackTrace.cs b/mcs/class/corlib/System.Diagnostics/StackTrace.cs index 3e775808fdc..721c38ef786 100644 --- a/mcs/class/corlib/System.Diagnostics/StackTrace.cs +++ b/mcs/class/corlib/System.Diagnostics/StackTrace.cs @@ -57,6 +57,7 @@ namespace System.Diagnostics { public const int METHODS_TO_SKIP = 0; private StackFrame[] frames; + readonly StackTrace[] captured_traces; private bool debug_info; public StackTrace () @@ -148,6 +149,8 @@ namespace System.Diagnostics { frames = l.ToArray (); } } + + captured_traces = e.captured_traces; } public StackTrace (StackFrame frame) @@ -189,7 +192,7 @@ namespace System.Diagnostics { return frames; } - internal bool AddFrames (StringBuilder sb, bool isException = false) + bool AddFrames (StringBuilder sb, bool isException = false) { bool printOffset; string debugInfo, indentation; @@ -306,6 +309,21 @@ namespace System.Diagnostics { public override string ToString () { StringBuilder sb = new StringBuilder (); + + // + // Add traces captured using ExceptionDispatchInfo + // + if (captured_traces != null) { + foreach (var t in captured_traces) { + if (!t.AddFrames (sb, true)) + continue; + + sb.Append (Environment.NewLine); + sb.Append ("--- End of stack trace from previous location where exception was thrown ---"); + sb.Append (Environment.NewLine); + } + } + AddFrames (sb); return sb.ToString (); } diff --git a/mcs/class/corlib/System/Exception.cs b/mcs/class/corlib/System/Exception.cs index 7670ce338e6..19833f0f854 100644 --- a/mcs/class/corlib/System/Exception.cs +++ b/mcs/class/corlib/System/Exception.cs @@ -199,26 +199,8 @@ namespace System /* Not thrown yet */ return null; - StringBuilder sb = new StringBuilder (); - - // Add traces captured using ExceptionDispatchInfo - if (captured_traces != null) { - foreach (var t in captured_traces) { - if (!t.AddFrames (sb, true)) - continue; - - sb.Append (Environment.NewLine); - sb.Append ("--- End of stack trace from previous location where exception was thrown ---"); - sb.Append (Environment.NewLine); - } - } - StackTrace st = new StackTrace (this, 0, true, true); - st.AddFrames (sb, true); - - stack_trace = sb.ToString (); - - return stack_trace; + return stack_trace = st.ToString (); } } diff --git a/mcs/class/corlib/Test/System.Runtime.ExceptionServices/ExceptionDispatchInfoTest.cs b/mcs/class/corlib/Test/System.Runtime.ExceptionServices/ExceptionDispatchInfoTest.cs index bef832b90c4..bd5e8dad948 100644 --- a/mcs/class/corlib/Test/System.Runtime.ExceptionServices/ExceptionDispatchInfoTest.cs +++ b/mcs/class/corlib/Test/System.Runtime.ExceptionServices/ExceptionDispatchInfoTest.cs @@ -30,6 +30,7 @@ using System; using NUnit.Framework; using System.Runtime.ExceptionServices; using System.Threading.Tasks; +using System.Diagnostics; namespace MonoTests.System.Runtime.ExceptionServices { @@ -150,7 +151,25 @@ namespace MonoTests.System.Runtime.ExceptionServices Assert.IsTrue (split [1].Contains ("---"), "#2"); Assert.IsTrue (split [4].Contains ("---"), "#3"); } - } + } + + [Test] + public void StackTraceUserCopy () + { + try { + try { + throw new NotImplementedException (); + } catch (Exception e) { + var edi = ExceptionDispatchInfo.Capture (e); + edi.Throw(); + } + } catch (Exception ex) { + var st = new StackTrace (ex, true); + var split = ex.StackTrace.Split (new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); + Assert.AreEqual (4, split.Length, "#1"); + Assert.IsTrue (split [1].Contains ("---"), "#2"); + } + } } } |