DebugProvider.Windows.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. namespace System.Diagnostics
  5. {
  6. public partial class DebugProvider
  7. {
  8. public static void FailCore(string stackTrace, string message, string detailMessage, string errorSource)
  9. {
  10. if (s_FailCore != null)
  11. {
  12. s_FailCore(stackTrace, message, detailMessage, errorSource);
  13. return;
  14. }
  15. if (Debugger.IsAttached)
  16. {
  17. Debugger.Break();
  18. }
  19. else
  20. {
  21. // In Core, we do not show a dialog.
  22. // Fail in order to avoid anyone catching an exception and masking
  23. // an assert failure.
  24. DebugAssertException ex;
  25. if (message == string.Empty)
  26. {
  27. ex = new DebugAssertException(stackTrace);
  28. }
  29. else if (detailMessage == string.Empty)
  30. {
  31. ex = new DebugAssertException(message, stackTrace);
  32. }
  33. else
  34. {
  35. ex = new DebugAssertException(message, detailMessage, stackTrace);
  36. }
  37. Environment.FailFast(ex.Message, ex, errorSource);
  38. }
  39. }
  40. private static readonly object s_ForLock = new object();
  41. public static void WriteCore(string message)
  42. {
  43. if (s_WriteCore != null)
  44. {
  45. s_WriteCore(message);
  46. return;
  47. }
  48. // really huge messages mess up both VS and dbmon, so we chop it up into
  49. // reasonable chunks if it's too big. This is the number of characters
  50. // that OutputDebugstring chunks at.
  51. const int WriteChunkLength = 4091;
  52. // We don't want output from multiple threads to be interleaved.
  53. lock (s_ForLock)
  54. {
  55. if (message == null || message.Length <= WriteChunkLength)
  56. {
  57. WriteToDebugger(message);
  58. }
  59. else
  60. {
  61. int offset;
  62. for (offset = 0; offset < message.Length - WriteChunkLength; offset += WriteChunkLength)
  63. {
  64. WriteToDebugger(message.Substring(offset, WriteChunkLength));
  65. }
  66. WriteToDebugger(message.Substring(offset));
  67. }
  68. }
  69. }
  70. private static void WriteToDebugger(string message)
  71. {
  72. if (Debugger.IsLogging())
  73. {
  74. Debugger.Log(0, null, message);
  75. }
  76. else
  77. {
  78. Interop.Kernel32.OutputDebugString(message ?? string.Empty);
  79. }
  80. }
  81. }
  82. }