Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Polyfill/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,21 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();

// Store it as the "remote" stack trace, which the runtime keeps and prepends to Exception.StackTrace.
// _stackTraceString would be cleared as soon as the exception is thrown.
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}

var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/net461/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/net462/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/net47/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/net471/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/net472/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/net48/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/net481/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/netcoreapp2.0/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/netcoreapp2.1/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/netcoreapp2.2/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/netcoreapp3.0/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/netcoreapp3.1/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/netstandard2.0/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/netstandard2.1/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
11 changes: 11 additions & 0 deletions src/Split/uap10.0/ExceptionDispatchInfoPolyfill.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ public static Exception SetCurrentStackTrace(Exception source)
{
if (source == null) throw new ArgumentNullException(nameof(source));
var stackTrace = new StackTrace(1, true).ToString();
var remoteField = typeof(Exception).GetField("_remoteStackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
if (remoteField != null)
{
var remoteStackTrace =
stackTrace.TrimEnd('\r', '\n') +
Environment.NewLine +
"--- End of stack trace from previous location where exception was thrown ---" +
Environment.NewLine;
remoteField.SetValue(source, remoteStackTrace);
return source;
}
var field = typeof(Exception).GetField("_stackTraceString", BindingFlags.NonPublic | BindingFlags.Instance);
field?.SetValue(source, stackTrace);
return source;
Expand Down
47 changes: 47 additions & 0 deletions src/Tests/PolyfillTests_ExceptionDispatchInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,51 @@ public async Task ExceptionDispatchInfoSetCurrentStackTrace()
await Assert.That(result).IsEqualTo(exception);
await Assert.That(exception.StackTrace).IsNotNull();
}

[MethodImpl(MethodImplOptions.NoInlining)]
static Exception CaptureStackTraceInNamedMethod()
{
var exception = ExceptionDispatchInfo.SetCurrentStackTrace(new Exception("test"));
// prevent a tail call from removing this frame
GC.KeepAlive(exception);
return exception;
}

[Test]
public async Task ExceptionDispatchInfoSetCurrentStackTrace_SurvivesThrow()
{
var exception = CaptureStackTraceInNamedMethod();
await Assert.That(exception.StackTrace!).Contains(nameof(CaptureStackTraceInNamedMethod));

string? thrownStackTrace = null;
try
{
throw exception;
}
catch (Exception caught)
{
thrownStackTrace = caught.StackTrace;
}

await Assert.That(thrownStackTrace!).Contains(nameof(CaptureStackTraceInNamedMethod));
await Assert.That(thrownStackTrace!).Contains("End of stack trace from previous location");
}

[Test]
public async Task ExceptionDispatchInfoSetCurrentStackTrace_SurvivesAwait()
{
var task = Task.FromException(CaptureStackTraceInNamedMethod());

string? thrownStackTrace = null;
try
{
await task;
}
catch (Exception caught)
{
thrownStackTrace = caught.StackTrace;
}

await Assert.That(thrownStackTrace!).Contains(nameof(CaptureStackTraceInNamedMethod));
}
}
Loading