AssertHelper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. // This class needs to function even if it was built retail. That is, a debug caller calling against a retail
  5. // build of this assembly should still have asserts fire. To achieve that, we need to define DEBUG here.
  6. // We do not do the registry override in retail because that would require shipping a test hook. We
  7. // do not generally ship test hooks today.
  8. #if DEBUG
  9. #define DEBUG_FOR_REALS
  10. #else
  11. #define DEBUG
  12. #endif
  13. namespace System.Runtime
  14. {
  15. using System;
  16. using System.Diagnostics;
  17. using System.Diagnostics.CodeAnalysis;
  18. using System.Runtime.CompilerServices;
  19. using System.Runtime.Interop;
  20. using System.Security;
  21. using System.Runtime.Versioning;
  22. static class AssertHelper
  23. {
  24. [SuppressMessage(FxCop.Category.ReliabilityBasic, FxCop.Rule.InvariantAssertRule, Justification = "Assert implementation")]
  25. [ResourceConsumption(ResourceScope.Process)]
  26. internal static void FireAssert(string message)
  27. {
  28. try
  29. {
  30. #if DEBUG_FOR_REALS
  31. InternalFireAssert(ref message);
  32. #endif
  33. }
  34. finally
  35. {
  36. Debug.Assert(false, message);
  37. }
  38. }
  39. #if DEBUG_FOR_REALS
  40. [SuppressMessage(FxCop.Category.Globalization, FxCop.Rule.DoNotPassLiteralsAsLocalizedParameters, Justification = "Debug Only")]
  41. [Fx.Tag.SecurityNote(Critical = "Calls into various critical methods",
  42. Safe = "Exists only on debug versions")]
  43. [SecuritySafeCritical]
  44. static void InternalFireAssert(ref string message)
  45. {
  46. try
  47. {
  48. string debugMessage = "Assert fired! --> " + message + "\r\n";
  49. if (Debugger.IsAttached)
  50. {
  51. Debugger.Log(0, Debugger.DefaultCategory, debugMessage);
  52. Debugger.Break();
  53. }
  54. if (UnsafeNativeMethods.IsDebuggerPresent())
  55. {
  56. UnsafeNativeMethods.OutputDebugString(debugMessage);
  57. UnsafeNativeMethods.DebugBreak();
  58. }
  59. if (Fx.AssertsFailFast)
  60. {
  61. try
  62. {
  63. Fx.Exception.TraceFailFast(message);
  64. }
  65. finally
  66. {
  67. Environment.FailFast(message);
  68. }
  69. }
  70. }
  71. catch (Exception exception)
  72. {
  73. if (Fx.IsFatal(exception))
  74. {
  75. throw;
  76. }
  77. string newMessage = "Exception during FireAssert!";
  78. try
  79. {
  80. newMessage = string.Concat(newMessage, " [", exception.GetType().Name, ": ", exception.Message, "] --> ", message);
  81. }
  82. finally
  83. {
  84. message = newMessage;
  85. }
  86. throw;
  87. }
  88. }
  89. #endif
  90. }
  91. }