summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Windows.cs
blob: 1eb445df17814497bfd2641df2ed8867f372a2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

namespace System.Diagnostics
{
    public partial class DebugProvider
    {
        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();
            }
            else
            {
                // In Core, we do not show a dialog.
                // Fail in order to avoid anyone catching an exception and masking
                // an assert failure.
                DebugAssertException ex;
                if (message == string.Empty) 
                {
                    ex = new DebugAssertException(stackTrace);
                }
                else if (detailMessage == string.Empty) 
                {
                    ex = new DebugAssertException(message, stackTrace);
                }
                else
                {
                    ex = new DebugAssertException(message, detailMessage, stackTrace);
                }
                Environment.FailFast(ex.Message, ex, errorSource);
            }
        }

        private static readonly object s_ForLock = new object();

        public static void WriteCore(string message)
        {
            if (s_WriteCore != null)
            {
                s_WriteCore(message); 
                return;
            }

            // really huge messages mess up both VS and dbmon, so we chop it up into 
            // reasonable chunks if it's too big. This is the number of characters 
            // that OutputDebugstring chunks at.
            const int WriteChunkLength = 4091;

            // We don't want output from multiple threads to be interleaved.
            lock (s_ForLock)
            {
                if (message == null || message.Length <= WriteChunkLength)
                {
                    WriteToDebugger(message);
                }
                else
                {
                    int offset;
                    for (offset = 0; offset < message.Length - WriteChunkLength; offset += WriteChunkLength)
                    {
                        WriteToDebugger(message.Substring(offset, WriteChunkLength));
                    }
                    WriteToDebugger(message.Substring(offset));
                }
            }
        }

        private static void WriteToDebugger(string message)
        {
            if (Debugger.IsLogging())
            {
                Debugger.Log(0, null, message);
            }
            else
            {
                Interop.Kernel32.OutputDebugString(message ?? string.Empty);
            }
        }
    }
}