Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
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
30 changes: 5 additions & 25 deletions src/System.Private.CoreLib/shared/System/Diagnostics/Debug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,7 @@ public static void Assert(bool condition, string message, string detailMessage)
{
if (!condition)
{
string stackTrace;
try
{
stackTrace = new StackTrace(0, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal);
}
catch
{
stackTrace = "";
}
WriteAssert(stackTrace, message, detailMessage);
s_provider.ShowDialog(stackTrace, message, detailMessage, "Assertion Failed");
Fail(message, detailMessage);
}
}

Expand All @@ -128,31 +118,21 @@ internal static void ContractFailure(bool condition, string message, string deta
{
stackTrace = "";
}
WriteAssert(stackTrace, message, detailMessage);
s_provider.ShowDialog(stackTrace, message, detailMessage, SR.GetResourceString(failureKindMessage));
s_provider.WriteAssert(stackTrace, message, detailMessage);
DebugProvider.FailCore(stackTrace, message, detailMessage, SR.GetResourceString(failureKindMessage));
}
}

[System.Diagnostics.Conditional("DEBUG")]
public static void Fail(string message)
{
Assert(false, message, string.Empty);
Fail(message, string.Empty);
}

[System.Diagnostics.Conditional("DEBUG")]
public static void Fail(string message, string detailMessage)
{
Assert(false, message, detailMessage);
}

private static void WriteAssert(string stackTrace, string message, string detailMessage)
{
WriteLine(SR.DebugAssertBanner + Environment.NewLine
+ SR.DebugAssertShortMessage + Environment.NewLine
+ message + Environment.NewLine
+ SR.DebugAssertLongMessage + Environment.NewLine
+ detailMessage + Environment.NewLine
+ stackTrace);
s_provider.Fail(message, detailMessage);
}

[System.Diagnostics.Conditional("DEBUG")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,14 @@ public partial class DebugProvider
{
private static readonly bool s_shouldWriteToStdErr = Environment.GetEnvironmentVariable("COMPlus_DebugWriteToStdErr") == "1";

public virtual void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource)
public static void FailCore(string stackTrace, string message, string detailMessage, string errorSource)
{
if (s_FailCore != null)
{
s_FailCore(stackTrace, message, detailMessage, errorSource);
return;
}

if (Debugger.IsAttached)
{
Debugger.Break();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,35 @@
namespace System.Diagnostics
{
/// <summary>
/// Provides default implementation for Write and ShowDialog methods in Debug class.
/// Provides default implementation for Write and Fail methods in Debug class.
/// </summary>
public partial class DebugProvider
{
public virtual void Fail(string message, string detailMessage)
{
string stackTrace;
try
{
stackTrace = new StackTrace(0, true).ToString(System.Diagnostics.StackTrace.TraceFormat.Normal);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do the extra stackframes between the user code and this place show up in the stacktrace now?

Copy link
Author

@maryamariyan maryamariyan Nov 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No they don't. Only shows up until the user code, the line that calls Debug.Fail or Trace.Fail.

That's what we want right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

}
catch
{
stackTrace = "";
}
WriteAssert(stackTrace, message, detailMessage);
FailCore(stackTrace, message, detailMessage, "Assertion Failed");
}

internal void WriteAssert(string stackTrace, string message, string detailMessage)
{
WriteLine(SR.DebugAssertBanner + Environment.NewLine
+ SR.DebugAssertShortMessage + Environment.NewLine
+ message + Environment.NewLine
+ SR.DebugAssertLongMessage + Environment.NewLine
+ detailMessage + Environment.NewLine
+ stackTrace);
}

public virtual void Write(string message)
{
lock (s_lock)
Expand Down Expand Up @@ -78,6 +103,7 @@ private string GetIndentString()
}

// internal and not readonly so that the tests can swap this out.
internal static Action<string, string, string, string> s_FailCore = null;
internal static Action<string> s_WriteCore = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ namespace System.Diagnostics
{
public partial class DebugProvider
{
public virtual void ShowDialog(string stackTrace, string message, string detailMessage, string errorSource)
public static void FailCore(string stackTrace, string message, string detailMessage, string errorSource)
{
if (s_FailCore != null)
{
s_FailCore(stackTrace, message, detailMessage, errorSource);
return;
}

if (Debugger.IsAttached)
{
Debugger.Break();
Expand Down