-
Notifications
You must be signed in to change notification settings - Fork 1
/
crt0msg.c
executable file
·303 lines (248 loc) · 8.58 KB
/
crt0msg.c
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/***
*crt0msg.c - startup error messages
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* Prints out banner for runtime error messages.
*
*******************************************************************************/
#include <cruntime.h>
#include <internal.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rterr.h>
#include <cmsgs.h>
#include <awint.h>
#include <windows.h>
#include <dbgint.h>
/*
* __app_type, together with __error_mode, determine how error messages
* are written out.
*/
int __app_type = _UNKNOWN_APP;
/* struct used to lookup and access runtime error messages */
struct rterrmsgs {
int rterrno; /* error number */
const wchar_t *rterrtxt; /* text of error message */
};
/* runtime error messages */
static const struct rterrmsgs rterrs[] = {
/* 2 */
{ _RT_FLOAT, _RT_FLOAT_TXT },
/* 8 */
{ _RT_SPACEARG, _RT_SPACEARG_TXT },
/* 9 */
{ _RT_SPACEENV, _RT_SPACEENV_TXT },
/* 10 */
{ _RT_ABORT, _RT_ABORT_TXT },
/* 16 */
{ _RT_THREAD, _RT_THREAD_TXT },
/* 17 */
{ _RT_LOCK, _RT_LOCK_TXT },
/* 18 */
{ _RT_HEAP, _RT_HEAP_TXT },
/* 19 */
{ _RT_OPENCON, _RT_OPENCON_TXT },
/* 22 */
/* { _RT_NONCONT, _RT_NONCONT_TXT }, */
/* 23 */
/* { _RT_INVALDISP, _RT_INVALDISP_TXT }, */
/* 24 */
{ _RT_ONEXIT, _RT_ONEXIT_TXT },
/* 25 */
{ _RT_PUREVIRT, _RT_PUREVIRT_TXT },
/* 26 */
{ _RT_STDIOINIT, _RT_STDIOINIT_TXT },
/* 27 */
{ _RT_LOWIOINIT, _RT_LOWIOINIT_TXT },
/* 28 */
{ _RT_HEAPINIT, _RT_HEAPINIT_TXT },
///* 29 */
//{ _RT_BADCLRVERSION, _RT_BADCLRVERSION_TXT },
/* 30 */
{ _RT_CRT_NOTINIT, _RT_CRT_NOTINIT_TXT },
/* 31 */
{ _RT_CRT_INIT_CONFLICT, _RT_CRT_INIT_CONFLICT_TXT},
/* 32 */
{ _RT_LOCALE, _RT_LOCALE_TXT},
/* 33 */
{ _RT_CRT_INIT_MANAGED_CONFLICT, _RT_CRT_INIT_MANAGED_CONFLICT_TXT},
/* 34 */
{ _RT_ONEXIT_VAR, _RT_ONEXIT_VAR_TXT },
///* 35 - not for _NMSG_WRITE, text passed directly to FatalAppExit */
//{ _RT_COOKIE_INIT, _RT_COOKIE_INIT_TXT},
/* 120 */
{ _RT_DOMAIN, _RT_DOMAIN_TXT },
/* 121 */
{ _RT_SING, _RT_SING_TXT },
/* 122 */
{ _RT_TLOSS, _RT_TLOSS_TXT },
/* 252 */
{ _RT_CRNL, _RT_CRNL_TXT },
/* 255 */
{ _RT_BANNER, _RT_BANNER_TXT }
};
/***
*_FF_MSGBANNER - writes out first part of run-time error messages
*
*Purpose:
* This routine writes "\r\nrun-time error " to standard error.
*
*Entry:
* No arguments.
*
*Exit:
* Nothing returned.
*
*Exceptions:
* None handled.
*
*******************************************************************************/
void __cdecl _FF_MSGBANNER (
void
)
{
if ( (_set_error_mode(_REPORT_ERRMODE) == _OUT_TO_STDERR) ||
((_set_error_mode(_REPORT_ERRMODE) == _OUT_TO_DEFAULT) &&
(__app_type == _CONSOLE_APP)) )
{
_NMSG_WRITE(_RT_CRNL); /* new line to begin error message */
_NMSG_WRITE(_RT_BANNER); /* run-time error message banner */
}
}
/***
*_GET_RTERRMSG(message) - returns ptr to error text for given runtime error
*
*Purpose:
* This routine returns the message associated with rterrnum
*
*Entry:
* int rterrnum - runtime error number
*
*Exit:
* no return value
*
*Exceptions:
* none
*
*******************************************************************************/
const wchar_t * __cdecl _GET_RTERRMSG (
int rterrnum
)
{
int i;
for ( i = 0 ; i < _countof(rterrs); i++ )
if ( rterrnum == rterrs[i].rterrno )
return rterrs[i].rterrtxt;
return NULL;
}
/***
*_NMSG_WRITE(message) - write a given message to handle 2 (stderr)
*
*Purpose:
* This routine writes the message associated with rterrnum
* to stderr.
*
*Entry:
* int rterrnum - runtime error number
*
*Exit:
* no return value
*
*Exceptions:
* none
*
*******************************************************************************/
void __cdecl _NMSG_WRITE (
int rterrnum
)
{
const wchar_t * const error_text = _GET_RTERRMSG(rterrnum);
if (error_text)
{
int msgshown = 0;
#ifdef _DEBUG
/*
* Report error.
*
* If _CRT_ERROR has _CRTDBG_REPORT_WNDW on, and user chooses
* "Retry", call the debugger.
*
* Otherwise, continue execution.
*
*/
if (rterrnum != _RT_CRNL && rterrnum != _RT_BANNER && rterrnum != _RT_CRT_NOTINIT)
{
switch (_CrtDbgReportW(_CRT_ERROR, NULL, 0, NULL, L"%s", error_text))
{
case 1: _CrtDbgBreak(); msgshown = 1; break;
case 0: msgshown = 1; break;
}
}
#endif /* _DEBUG */
if (!msgshown)
{
if ( (_set_error_mode(_REPORT_ERRMODE) == _OUT_TO_STDERR) ||
((_set_error_mode(_REPORT_ERRMODE) == _OUT_TO_DEFAULT) &&
(__app_type == _CONSOLE_APP)) )
{
HANDLE hStdErr = GetStdHandle(STD_ERROR_HANDLE);
if (hStdErr && hStdErr != INVALID_HANDLE_VALUE)
{
/*
* This purposely truncates wchar_t to char.
* error_text doesn't contain the application's name.
* Currently, error_text contains only ASCII,
* so this truncation preserves VC9's behavior.
* If error_text is ever localized, this will have to be changed.
*
*/
char buffer[500];
int i;
DWORD bytes_written;
for (i = 0; i < _countof(buffer); ++i)
{
buffer[i] = (char) error_text[i];
if (error_text[i] == L'\0')
{
break;
}
}
buffer[_countof(buffer) - 1] = '\0';
WriteFile( hStdErr,
buffer,
(unsigned long)strlen(buffer),
&bytes_written,
NULL );
}
}
else if (rterrnum != _RT_CRNL)
{
#define MSGTEXTPREFIX L"Runtime Error!\n\nProgram: "
static wchar_t outmsg[sizeof(MSGTEXTPREFIX) / sizeof(wchar_t) + _MAX_PATH + 2 + 500];
// runtime error msg + progname + 2 newline + runtime error text.
wchar_t * progname = &outmsg[sizeof(MSGTEXTPREFIX) / sizeof(wchar_t) - 1];
size_t progname_size = _countof(outmsg) - (progname - outmsg);
wchar_t * pch = progname;
_ERRCHECK(wcscpy_s(outmsg, _countof(outmsg), MSGTEXTPREFIX));
progname[MAX_PATH] = L'\0';
if (!GetModuleFileNameW(NULL, progname, MAX_PATH))
_ERRCHECK(wcscpy_s(progname, progname_size, L"<program name unknown>"));
#define MAXLINELEN 60
if (wcslen(pch) + 1 > MAXLINELEN)
{
pch += wcslen(progname) + 1 - MAXLINELEN;
_ERRCHECK(wcsncpy_s(pch, progname_size - (pch - progname), L"...", 3));
}
_ERRCHECK(wcscat_s(outmsg, _countof(outmsg), L"\n\n"));
_ERRCHECK(wcscat_s(outmsg, _countof(outmsg), error_text));
__crtMessageBoxW(outmsg,
L"Microsoft Visual C++ Runtime Library",
MB_OK|MB_ICONHAND|MB_SETFOREGROUND|MB_TASKMODAL);
}
}
}
}