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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.Win32;
using System.Security.Permissions;
using System.Security;
using System.Collections;
using System.Globalization;
using System.Threading;
#if !SILVERLIGHT
using System.Runtime.Versioning;
#endif
using System.Diagnostics.CodeAnalysis;
using NativeMethods = Microsoft.Win32.NativeMethods;
namespace System.Diagnostics {
internal static class AssertWrapper {
#if DEBUG && !FEATURE_PAL && !SILVERLIGHT
static BooleanSwitch DisableVsAssert = new BooleanSwitch("DisableVsAssert", "Switch to disable usage of VSASSERT for DefaultTraceListener.");
static volatile bool vsassertPresent = true;
static Hashtable ignoredAsserts = new Hashtable(StringComparer.OrdinalIgnoreCase);
[ResourceExposure(ResourceScope.None)]
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
private static void ShowVsAssert(string stackTrace, StackFrame frame, string message, string detailMessage) {
int[] disable = new int[1];
try {
string detailMessage2;
if (detailMessage == null)
detailMessage2 = stackTrace;
else
detailMessage2 = detailMessage + Environment.NewLine + stackTrace;
string fileName = (frame == null) ? string.Empty : frame.GetFileName();
if (fileName == null) {
fileName = string.Empty;
}
int lineNumber = (frame == null) ? 0 : frame.GetFileLineNumber();
int returnCode = VsAssert(detailMessage2, message, fileName, lineNumber, disable);
if (returnCode != 0) {
if (!System.Diagnostics.Debugger.IsAttached) {
System.Diagnostics.Debugger.Launch();
}
System.Diagnostics.Debugger.Break();
}
if (disable[0] != 0)
ignoredAsserts[MakeAssertKey(fileName, lineNumber)] = null;
}
catch (Exception) {
vsassertPresent = false;
}
}
[DllImport(ExternDll.Fxassert, CharSet=System.Runtime.InteropServices.CharSet.Ansi, BestFitMapping=true)]
[ResourceExposure(ResourceScope.None)]
[SuppressMessage("Microsoft.Globalization","CA2101:SpecifyMarshalingForPInvokeStringArguments", MessageId="0", Justification="[....]: VsAssert isn't making a security decision here and they don't provide Unicode versions, also it is internal to MS")]
[SuppressMessage("Microsoft.Globalization","CA2101:SpecifyMarshalingForPInvokeStringArguments", MessageId="1", Justification="[....]: VsAssert isn't making a security decision here and they don't provide Unicode versions, also it is internal to MS")]
[SuppressMessage("Microsoft.Globalization","CA2101:SpecifyMarshalingForPInvokeStringArguments", MessageId="2", Justification="[....]: VsAssert isn't making a security decision here and they don't provide Unicode versions, also it is internal to MS")]
public static extern int VsAssert(string message, string assert, string file, int line, [In, Out]int[] pfDisable);
[ResourceExposure(ResourceScope.None)]
public static void ShowAssert(string stackTrace, StackFrame frame, string message, string detailMessage) {
bool userInteractive = Environment.UserInteractive;
string fileName = (frame == null) ? string.Empty : frame.GetFileName();
if (fileName == null) {
fileName = string.Empty;
}
int lineNumber = (frame == null) ? 0 : frame.GetFileLineNumber();
if (ignoredAsserts.ContainsKey(MakeAssertKey(fileName, lineNumber)))
return;
if (vsassertPresent && !DisableVsAssert.Enabled && userInteractive)
ShowVsAssert(stackTrace, frame, message, detailMessage);
// the following is not in an 'else' because vsassertPresent might
// have gone from true to false.
if (!vsassertPresent || DisableVsAssert.Enabled || !userInteractive)
ShowMessageBoxAssert(stackTrace, message, detailMessage);
}
private static string MakeAssertKey(string fileName, int lineNumber) {
return fileName + ":" + lineNumber.ToString(CultureInfo.InvariantCulture);
}
#else // DEBUG && !FEATURE_PAL && !SILVERLIGHT
public static void ShowAssert(string stackTrace, StackFrame frame, string message, string detailMessage) {
#if SILVERLIGHT && FEATURE_PAL
ShowCFUserNotificationDisplayAlertAssert(stackTrace, message, detailMessage);
#else // !(SILVERLIGHT && FEATURE_PAL)
ShowMessageBoxAssert(stackTrace, message, detailMessage);
#endif
}
#endif // DEBUG && !FEATURE_PAL && !SILVERLIGHT
#if !SILVERLIGHT
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
#endif
[SecuritySafeCritical]
private static void ShowMessageBoxAssert(string stackTrace, string message, string detailMessage) {
string fullMessage = message + Environment.NewLine + detailMessage + Environment.NewLine + stackTrace;
#if !FEATURE_PAL && !FEATURE_CORESYSTEM
fullMessage = TruncateMessageToFitScreen(fullMessage);
#endif // !FEATURE_PAL && !FEATURE_CORESYSTEM
#if SILVERLIGHT
int flags = 0x00000001 /*OkCancel*/ | 0x00000010 /*IconHand*/ |
0x00040000 /* TopMost */;
#else
int flags = 0x00000002 /*AbortRetryIgnore*/ | 0x00000200 /*DefaultButton3*/ | 0x00000010 /*IconHand*/ |
0x00040000 /* TopMost */;
#endif
#if !SILVERLIGHT
if (!Environment.UserInteractive)
flags = flags | 0x00200000 /*ServiceNotification */;
#endif
if (IsRTLResources)
flags = flags | SafeNativeMethods.MB_RIGHT | SafeNativeMethods.MB_RTLREADING;
int rval = 0;
#if !SILVERLIGHT
if (!UnsafeNativeMethods.IsPackagedProcess.Value)
{
rval = SafeNativeMethods.MessageBox(IntPtr.Zero, fullMessage, SR.GetString(SR.DebugAssertTitle), flags);
}
else
{
#endif
// Run the message box on its own thread.
rval = new MessageBoxPopup(fullMessage, SR.GetString(SR.DebugAssertTitle), flags).ShowMessageBox();
#if !SILVERLIGHT
}
#endif
switch (rval) {
#if !SILVERLIGHT
case 3: // abort
System.Environment.Exit(1);
break;
case 4: // retry
#else
case 2: // cancel
#endif
if (!System.Diagnostics.Debugger.IsAttached) {
System.Diagnostics.Debugger.Launch();
}
System.Diagnostics.Debugger.Break();
break;
}
}
private static bool IsRTLResources {
get {
return SR.GetString(SR.RTL) != "RTL_False";
}
}
#if SILVERLIGHT && FEATURE_PAL
[SecuritySafeCritical]
private static void ShowCFUserNotificationDisplayAlertAssert(string stackTrace, string message, string detailMessage)
{
IntPtr cfsContinueText, cfsFullMessage, cfsTitle;
string fullMessage = message + Environment.NewLine + detailMessage + Environment.NewLine + stackTrace;
cfsFullMessage = SafeNativeMethods.CFStringCreateWithCharacters(IntPtr.Zero, fullMessage, fullMessage.Length);
cfsContinueText = SafeNativeMethods.CFStringCreateWithCharacters(IntPtr.Zero, SR.GetString(SR.ContinueButtonText), SR.GetString(SR.ContinueButtonText).Length);
cfsTitle = SafeNativeMethods.CFStringCreateWithCharacters(IntPtr.Zero, SR.GetString(SR.DebugAssertTitleShort), SR.GetString(SR.DebugAssertTitleShort).Length);
uint rval = 0;
SafeNativeMethods.CFUserNotificationDisplayAlert(0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, cfsTitle, cfsFullMessage, cfsContinueText, IntPtr.Zero, IntPtr.Zero, ref rval);
SafeNativeMethods.CFRelease(cfsFullMessage);
SafeNativeMethods.CFRelease(cfsContinueText);
SafeNativeMethods.CFRelease(cfsTitle);
// We don't care about the return value of CFUserNotificationDisplayAlert.
}
#endif
#if !FEATURE_PAL && !FEATURE_CORESYSTEM
// Since MessageBox will grow taller than the screen if there are too many lines do
// a rough calculation to make it fit.
#if !SILVERLIGHT
[ResourceConsumption(ResourceScope.Machine, ResourceScope.Machine)]
#endif
[SecuritySafeCritical]
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Microsoft.Win32.UnsafeNativeMethods.ReleaseDC(System.IntPtr,System.IntPtr)", Justification = "[....]: If the DC is not released there's not much we can do.")]
private static string TruncateMessageToFitScreen(string message) {
const int MaxCharsPerLine = 80;
IntPtr hFont = SafeNativeMethods.GetStockObject(NativeMethods.DEFAULT_GUI_FONT);
IntPtr hdc = UnsafeNativeMethods.GetDC(IntPtr.Zero);
NativeMethods.TEXTMETRIC tm = new NativeMethods.TEXTMETRIC();
hFont = UnsafeNativeMethods.SelectObject(hdc, hFont);
SafeNativeMethods.GetTextMetrics(hdc, tm);
UnsafeNativeMethods.SelectObject(hdc, hFont);
UnsafeNativeMethods.ReleaseDC(IntPtr.Zero, hdc);
hdc = IntPtr.Zero;
int cy = UnsafeNativeMethods.GetSystemMetrics(NativeMethods.SM_CYSCREEN);
int maxLines = cy / tm.tmHeight - 15;
int lineCount = 0;
int lineCharCount = 0;
int i = 0;
while (lineCount < maxLines && i < message.Length - 1) {
char ch = message[i];
lineCharCount++;
if (ch == '\n' || ch == '\r' || lineCharCount > MaxCharsPerLine) {
lineCount++;
lineCharCount = 0;
}
// treat \r\n or \n\r as a single line break
if (ch == '\r' && message[i + 1] == '\n')
i+=2;
else if (ch == '\n' && message[i + 1] == '\r')
i+=2;
else
i++;
}
if (i < message.Length - 1)
message = SR.GetString(SR.DebugMessageTruncated, message.Substring(0, i));
return message;
}
#endif // !FEATURE_PAL && !FEATURE_CORESYSTEM
}
internal class MessageBoxPopup {
public int ReturnValue { get; set; }
private AutoResetEvent m_Event;
private string m_Body;
private string m_Title;
private int m_Flags;
[SecurityCritical]
public MessageBoxPopup(string body, string title, int flags) {
m_Event = new AutoResetEvent(false);
m_Body = body;
m_Title = title;
m_Flags = flags;
}
public int ShowMessageBox() {
Thread t = new Thread(DoPopup);
t.Start();
m_Event.WaitOne();
return ReturnValue;
}
[SecuritySafeCritical]
public void DoPopup() {
ReturnValue = SafeNativeMethods.MessageBox(IntPtr.Zero, m_Body, m_Title, m_Flags);
m_Event.Set();
}
}
}
|