summaryrefslogtreecommitdiff
path: root/src/System.Private.CoreLib/shared/System/Diagnostics/DebugProvider.Unix.cs
blob: e1517179ec170bd26e0d79586563f0267a7c8de3 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// 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.

using Microsoft.Win32.SafeHandles;

namespace System.Diagnostics
{
    public partial class DebugProvider
    {
        private static readonly bool s_shouldWriteToStdErr = Environment.GetEnvironmentVariable("COMPlus_DebugWriteToStdErr") == "1";

        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);
            }
        }

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

            WriteToDebugger(message);

            if (s_shouldWriteToStdErr)
            {
                WriteToStderr(message);
            }
        }

        private static void WriteToDebugger(string message)
        {
            if (Debugger.IsLogging())
            {
                Debugger.Log(0, null, message);
            }
            else
            {
                Interop.Sys.SysLog(Interop.Sys.SysLogPriority.LOG_USER | Interop.Sys.SysLogPriority.LOG_DEBUG, "%s", message);
            }
        }

        private static void WriteToStderr(string message)
        {
            // We don't want to write UTF-16 to a file like standard error.  Ideally we would transcode this
            // to UTF8, but the downside of that is it pulls in a bunch of stuff into what is ideally
            // a path with minimal dependencies (as to prevent re-entrency), so we'll take the strategy
            // of just throwing away any non ASCII characters from the message and writing the rest

            const int BufferLength = 256;

            unsafe
            {
                byte* buf = stackalloc byte[BufferLength];
                int bufCount;
                int i = 0;

                while (i < message.Length)
                {
                    for (bufCount = 0; bufCount < BufferLength && i < message.Length; i++)
                    {
                        if (message[i] <= 0x7F)
                        {
                            buf[bufCount] = (byte)message[i];
                            bufCount++;
                        }
                    }

                    int totalBytesWritten = 0;
                    while (bufCount > 0)
                    {
                        int bytesWritten = Interop.Sys.Write(2 /* stderr */, buf + totalBytesWritten, bufCount);
                        if (bytesWritten < 0)
                        {
                            // On error, simply stop writing the debug output.  This could commonly happen if stderr
                            // was piped to a program that ended before this program did, resulting in EPIPE errors.
                            return;
                        }

                        bufCount -= bytesWritten;
                        totalBytesWritten += bytesWritten;
                    }
                }
            }
        }
    }
}