1 | /* Copyright (c) 2001, Stanford University
|
---|
2 | * All rights reserved
|
---|
3 | *
|
---|
4 | * See the file LICENSE.txt for information on redistributing this software.
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "cr_environment.h"
|
---|
8 | #include "cr_error.h"
|
---|
9 | #include "cr_string.h"
|
---|
10 | #include "cr_net.h"
|
---|
11 | #include "cr_process.h"
|
---|
12 |
|
---|
13 | #ifdef WINDOWS
|
---|
14 | #define WIN32_LEAN_AND_MEAN
|
---|
15 | #include <windows.h>
|
---|
16 | #include <io.h>
|
---|
17 | #include <fcntl.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include <stdio.h>
|
---|
21 | #include <stdlib.h>
|
---|
22 | #include <stdarg.h>
|
---|
23 | #include <signal.h>
|
---|
24 |
|
---|
25 | #ifndef IN_GUEST
|
---|
26 | #define LOG_GROUP LOG_GROUP_SHARED_CROPENGL
|
---|
27 | #endif
|
---|
28 | #if !defined(IN_GUEST) || defined(CR_DEBUG_BACKDOOR_ENABLE)
|
---|
29 | #include <VBox/log.h>
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #if defined(WINDOWS)
|
---|
33 | # define CR_DEBUG_CONSOLE_ENABLE
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #if defined(WINDOWS) && defined(IN_GUEST)
|
---|
37 | # ifndef CR_DEBUG_BACKDOOR_ENABLE
|
---|
38 | # error "CR_DEBUG_BACKDOOR_ENABLE is expected!"
|
---|
39 | # endif
|
---|
40 | #else
|
---|
41 | # ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
42 | # error "CR_DEBUG_BACKDOOR_ENABLE is NOT expected!"
|
---|
43 | # endif
|
---|
44 | #endif
|
---|
45 |
|
---|
46 |
|
---|
47 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
48 | # include <VBoxDispMpLogger.h>
|
---|
49 | # include <iprt/err.h>
|
---|
50 | #endif
|
---|
51 |
|
---|
52 |
|
---|
53 | static char my_hostname[256];
|
---|
54 | #ifdef WINDOWS
|
---|
55 | static HANDLE my_pid;
|
---|
56 | #else
|
---|
57 | static int my_pid = 0;
|
---|
58 | #endif
|
---|
59 | static int canada = 0;
|
---|
60 | static int swedish_chef = 0;
|
---|
61 | static int australia = 0;
|
---|
62 | static int warnings_enabled = 1;
|
---|
63 |
|
---|
64 | #ifdef DEBUG_misha
|
---|
65 | //int g_VBoxFbgFBreakDdi = 0;
|
---|
66 | #define DebugBreak() Assert(0)
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | void __getHostInfo( void )
|
---|
70 | {
|
---|
71 | char *temp;
|
---|
72 | /* on windows guests we're typically get called in a context of VBoxOGL!DllMain ( which calls VBoxOGLcrutil!crNetInit ),
|
---|
73 | * which may lead to deadlocks..
|
---|
74 | * Avoid it as it is needed for debugging purposes only */
|
---|
75 | #if !defined(IN_GUEST) || !defined(RT_OS_WINDOWS)
|
---|
76 | if ( crGetHostname( my_hostname, sizeof( my_hostname ) ) )
|
---|
77 | #endif
|
---|
78 | {
|
---|
79 | crStrcpy( my_hostname, "????" );
|
---|
80 | }
|
---|
81 | temp = crStrchr( my_hostname, '.' );
|
---|
82 | if (temp)
|
---|
83 | {
|
---|
84 | *temp = '\0';
|
---|
85 | }
|
---|
86 | my_pid = crGetPID();
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void __crCheckCanada(void)
|
---|
90 | {
|
---|
91 | static int first = 1;
|
---|
92 | if (first)
|
---|
93 | {
|
---|
94 | const char *env = crGetenv( "CR_CANADA" );
|
---|
95 | if (env)
|
---|
96 | canada = 1;
|
---|
97 | first = 0;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | static void __crCheckSwedishChef(void)
|
---|
102 | {
|
---|
103 | static int first = 1;
|
---|
104 | if (first)
|
---|
105 | {
|
---|
106 | const char *env = crGetenv( "CR_SWEDEN" );
|
---|
107 | if (env)
|
---|
108 | swedish_chef = 1;
|
---|
109 | first = 0;
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | static void __crCheckAustralia(void)
|
---|
114 | {
|
---|
115 | static int first = 1;
|
---|
116 | if (first)
|
---|
117 | {
|
---|
118 | const char *env = crGetenv( "CR_AUSTRALIA" );
|
---|
119 | const char *env2 = crGetenv( "CR_AUSSIE" );
|
---|
120 | if (env || env2)
|
---|
121 | australia = 1;
|
---|
122 | first = 0;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | static void outputChromiumMessage( FILE *output, char *str )
|
---|
127 | {
|
---|
128 | fprintf( output, "%s%s%s%s\n", str,
|
---|
129 | swedish_chef ? " BORK BORK BORK!" : "",
|
---|
130 | canada ? ", eh?" : "",
|
---|
131 | australia ? ", mate!" : ""
|
---|
132 | );
|
---|
133 | fflush( output );
|
---|
134 | }
|
---|
135 |
|
---|
136 | #ifdef WINDOWS
|
---|
137 | static void crRedirectIOToConsole()
|
---|
138 | {
|
---|
139 | int hConHandle;
|
---|
140 | HANDLE StdHandle;
|
---|
141 | FILE *fp;
|
---|
142 |
|
---|
143 | AllocConsole();
|
---|
144 |
|
---|
145 | StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
|
---|
146 | hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
|
---|
147 | fp = _fdopen( hConHandle, "w" );
|
---|
148 | *stdout = *fp;
|
---|
149 | *stderr = *fp;
|
---|
150 |
|
---|
151 | StdHandle = GetStdHandle(STD_INPUT_HANDLE);
|
---|
152 | hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
|
---|
153 | fp = _fdopen( hConHandle, "r" );
|
---|
154 | *stdin = *fp;
|
---|
155 | }
|
---|
156 | #endif
|
---|
157 |
|
---|
158 |
|
---|
159 | DECLEXPORT(void) crError(const char *format, ... )
|
---|
160 | {
|
---|
161 | va_list args;
|
---|
162 | static char txt[8092];
|
---|
163 | int offset;
|
---|
164 | #ifdef WINDOWS
|
---|
165 | DWORD err;
|
---|
166 | #endif
|
---|
167 |
|
---|
168 | __crCheckCanada();
|
---|
169 | __crCheckSwedishChef();
|
---|
170 | __crCheckAustralia();
|
---|
171 | if (!my_hostname[0])
|
---|
172 | __getHostInfo();
|
---|
173 | #ifdef WINDOWS
|
---|
174 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
175 | {
|
---|
176 | static char buf[8092], *temp;
|
---|
177 |
|
---|
178 | SetLastError(0);
|
---|
179 | sprintf( buf, "err=%d", err );
|
---|
180 |
|
---|
181 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
182 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
183 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
184 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
185 | (LPTSTR) &temp, 0, NULL );
|
---|
186 | if ( temp )
|
---|
187 | {
|
---|
188 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
189 | buf[sizeof(buf)-1] = 0;
|
---|
190 | }
|
---|
191 |
|
---|
192 | temp = buf + crStrlen(buf) - 1;
|
---|
193 | while ( temp > buf && isspace( *temp ) )
|
---|
194 | {
|
---|
195 | *temp = '\0';
|
---|
196 | temp--;
|
---|
197 | }
|
---|
198 |
|
---|
199 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t----------------------\nCR Error(%s:%d): ", buf, my_hostname, my_pid );
|
---|
200 | }
|
---|
201 | else
|
---|
202 | {
|
---|
203 | offset = sprintf( txt, "OpenGL Error: ");
|
---|
204 | }
|
---|
205 | #else
|
---|
206 | offset = sprintf( txt, "OpenGL Error: " );
|
---|
207 | #endif
|
---|
208 | va_start( args, format );
|
---|
209 | vsprintf( txt + offset, format, args );
|
---|
210 | #if defined(IN_GUEST)
|
---|
211 | crDebug("%s", txt);
|
---|
212 | outputChromiumMessage( stderr, txt );
|
---|
213 | #else
|
---|
214 | LogRel(("%s\n", txt));
|
---|
215 | #endif
|
---|
216 | #ifdef WINDOWS
|
---|
217 | if (crGetenv( "CR_GUI_ERROR" ) != NULL)
|
---|
218 | {
|
---|
219 | MessageBox( NULL, txt, "Chromium Error", MB_OK );
|
---|
220 | }
|
---|
221 | else
|
---|
222 | {
|
---|
223 | #endif
|
---|
224 | va_end( args );
|
---|
225 | #ifdef WINDOWS
|
---|
226 | }
|
---|
227 | #if !defined(DEBUG_leo) && !defined(DEBUG_ll158262) && !defined(DEBUG_misha)
|
---|
228 | if (crGetenv( "CR_DEBUG_ON_ERROR" ) != NULL)
|
---|
229 | #endif
|
---|
230 | {
|
---|
231 | DebugBreak();
|
---|
232 | }
|
---|
233 | #endif
|
---|
234 |
|
---|
235 | #ifdef IN_GUEST
|
---|
236 | /* Give chance for things to close down */
|
---|
237 | raise( SIGTERM );
|
---|
238 |
|
---|
239 | exit(1);
|
---|
240 | #endif
|
---|
241 | }
|
---|
242 |
|
---|
243 | void crEnableWarnings(int onOff)
|
---|
244 | {
|
---|
245 | warnings_enabled = onOff;
|
---|
246 | }
|
---|
247 |
|
---|
248 | #ifdef DEBUG_misha
|
---|
249 | # undef crWarning
|
---|
250 | #endif
|
---|
251 | DECLEXPORT(void) crWarning(const char *format, ... )
|
---|
252 | {
|
---|
253 | if (warnings_enabled) {
|
---|
254 | va_list args;
|
---|
255 | static char txt[8092];
|
---|
256 | int offset;
|
---|
257 |
|
---|
258 | __crCheckCanada();
|
---|
259 | __crCheckSwedishChef();
|
---|
260 | __crCheckAustralia();
|
---|
261 | if (!my_hostname[0])
|
---|
262 | __getHostInfo();
|
---|
263 | offset = sprintf( txt, "OpenGL Warning: ");
|
---|
264 | va_start( args, format );
|
---|
265 | vsprintf( txt + offset, format, args );
|
---|
266 | #if defined(IN_GUEST)
|
---|
267 | crDebug("%s", txt);
|
---|
268 | outputChromiumMessage( stderr, txt );
|
---|
269 | #else
|
---|
270 | LogRel(("%s\n", txt));
|
---|
271 | #endif
|
---|
272 | va_end( args );
|
---|
273 |
|
---|
274 | #if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST) && defined(DEBUG_misha)
|
---|
275 | DebugBreak();
|
---|
276 | #endif
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | DECLEXPORT(void) crInfo(const char *format, ... )
|
---|
281 | {
|
---|
282 | va_list args;
|
---|
283 | static char txt[8092];
|
---|
284 | int offset;
|
---|
285 |
|
---|
286 | __crCheckCanada();
|
---|
287 | __crCheckSwedishChef();
|
---|
288 | __crCheckAustralia();
|
---|
289 | if (!my_hostname[0])
|
---|
290 | __getHostInfo();
|
---|
291 | offset = sprintf( txt, "OpenGL Info: ");
|
---|
292 | va_start( args, format );
|
---|
293 | vsprintf( txt + offset, format, args );
|
---|
294 | #if defined(IN_GUEST)
|
---|
295 | crDebug("%s", txt);
|
---|
296 | outputChromiumMessage( stderr, txt );
|
---|
297 | #else
|
---|
298 | LogRel(("%s\n", txt));
|
---|
299 | #endif
|
---|
300 | va_end( args );
|
---|
301 | }
|
---|
302 |
|
---|
303 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
304 | static DECLCALLBACK(void) crDebugBackdoorRt(char* pcszStr)
|
---|
305 | {
|
---|
306 | RTLogBackdoorPrintf("%s", pcszStr);
|
---|
307 | }
|
---|
308 |
|
---|
309 | static DECLCALLBACK(void) crDebugBackdoorDispMp(char* pcszStr)
|
---|
310 | {
|
---|
311 | VBoxDispMpLoggerLog(pcszStr);
|
---|
312 | }
|
---|
313 | #endif
|
---|
314 |
|
---|
315 |
|
---|
316 | #if defined(DEBUG) && defined(WINDOWS) /* && (!defined(DEBUG_misha) || !defined(IN_GUEST) ) */
|
---|
317 | # define CR_DEBUG_DBGPRINT_ENABLE
|
---|
318 | #endif
|
---|
319 |
|
---|
320 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
321 | static void crDebugDbgPrint(const char *str)
|
---|
322 | {
|
---|
323 | OutputDebugString(str);
|
---|
324 | OutputDebugString("\n");
|
---|
325 | }
|
---|
326 | #endif
|
---|
327 |
|
---|
328 | DECLEXPORT(void) crDebug(const char *format, ... )
|
---|
329 | {
|
---|
330 | va_list args;
|
---|
331 | static char txt[8092];
|
---|
332 | int offset;
|
---|
333 | #ifdef WINDOWS
|
---|
334 | DWORD err;
|
---|
335 | #endif
|
---|
336 | static FILE *output;
|
---|
337 | static int first_time = 1;
|
---|
338 | static int silent = 0;
|
---|
339 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
340 | typedef DECLCALLBACK(void) FNCRGEDUGBACKDOOR(char* pcszStr);
|
---|
341 | typedef FNCRGEDUGBACKDOOR *PFNCRGEDUGBACKDOOR;
|
---|
342 | static PFNCRGEDUGBACKDOOR pfnLogBackdoor = NULL;
|
---|
343 | #endif
|
---|
344 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
345 | static int dbgPrintEnable = 0;
|
---|
346 | #endif
|
---|
347 |
|
---|
348 | if (first_time)
|
---|
349 | {
|
---|
350 | const char *fname = crGetenv( "CR_DEBUG_FILE" );
|
---|
351 | const char *fnamePrefix = crGetenv( "CR_DEBUG_FILE_PREFIX" );
|
---|
352 | char str[2048];
|
---|
353 | #ifdef CR_DEBUG_CONSOLE_ENABLE
|
---|
354 | int logToConsole = 0;
|
---|
355 | #endif
|
---|
356 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
357 | if (crGetenv( "CR_DEBUG_BACKDOOR" ))
|
---|
358 | {
|
---|
359 | int rc = VBoxDispMpLoggerInit();
|
---|
360 | if (RT_SUCCESS(rc))
|
---|
361 | pfnLogBackdoor = crDebugBackdoorDispMp;
|
---|
362 | else
|
---|
363 | pfnLogBackdoor = crDebugBackdoorRt;
|
---|
364 | }
|
---|
365 | #endif
|
---|
366 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
367 | if (crGetenv( "CR_DEBUG_DBGPRINT" ))
|
---|
368 | {
|
---|
369 | dbgPrintEnable = 1;
|
---|
370 | }
|
---|
371 | #endif
|
---|
372 |
|
---|
373 | if (!fname && fnamePrefix)
|
---|
374 | {
|
---|
375 | char pname[1024];
|
---|
376 | if (crStrlen(fnamePrefix) < sizeof (str) - sizeof (pname) - 20)
|
---|
377 | {
|
---|
378 | crGetProcName(pname, 1024);
|
---|
379 | sprintf(str,
|
---|
380 | #ifdef RT_OS_WINDOWS
|
---|
381 | "%s_%s_%u.txt", fnamePrefix, pname, GetCurrentProcessId()
|
---|
382 | #else
|
---|
383 | "%s_%s_%lu.txt", fnamePrefix, pname, crGetPID()
|
---|
384 | #endif
|
---|
385 | );
|
---|
386 | fname = &str[0];
|
---|
387 | }
|
---|
388 | }
|
---|
389 |
|
---|
390 | first_time = 0;
|
---|
391 | if (fname)
|
---|
392 | {
|
---|
393 | char debugFile[2048], *p;
|
---|
394 | crStrcpy(debugFile, fname);
|
---|
395 | p = crStrstr(debugFile, "%p");
|
---|
396 | if (p) {
|
---|
397 | /* replace %p with process number */
|
---|
398 | unsigned long n = (unsigned long) crGetPID();
|
---|
399 | sprintf(p, "%lu", n);
|
---|
400 | }
|
---|
401 | fname = debugFile;
|
---|
402 | output = fopen( fname, "w" );
|
---|
403 | if (!output)
|
---|
404 | {
|
---|
405 | crError( "Couldn't open debug log %s", fname );
|
---|
406 | }
|
---|
407 | }
|
---|
408 | else
|
---|
409 | {
|
---|
410 | #ifdef CR_DEBUG_CONSOLE_ENABLE
|
---|
411 | if (crGetenv( "CR_DEBUG_CONSOLE" ))
|
---|
412 | {
|
---|
413 | crRedirectIOToConsole();
|
---|
414 | logToConsole = 1;
|
---|
415 | }
|
---|
416 | #endif
|
---|
417 | output = stderr;
|
---|
418 | }
|
---|
419 |
|
---|
420 | #if !defined(DEBUG)/* || defined(DEBUG_misha)*/
|
---|
421 | /* Release mode: only emit crDebug messages if CR_DEBUG
|
---|
422 | * or CR_DEBUG_FILE is set.
|
---|
423 | */
|
---|
424 | if (!fname && !crGetenv("CR_DEBUG")
|
---|
425 | #ifdef CR_DEBUG_CONSOLE_ENABLE
|
---|
426 | && !logToConsole
|
---|
427 | #endif
|
---|
428 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
429 | && !pfnLogBackdoor
|
---|
430 | #endif
|
---|
431 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
432 | && !dbgPrintEnable
|
---|
433 | #endif
|
---|
434 | )
|
---|
435 | silent = 1;
|
---|
436 | #endif
|
---|
437 | }
|
---|
438 |
|
---|
439 | if (silent)
|
---|
440 | return;
|
---|
441 |
|
---|
442 | __crCheckCanada();
|
---|
443 | __crCheckSwedishChef();
|
---|
444 | __crCheckAustralia();
|
---|
445 | if (!my_hostname[0])
|
---|
446 | __getHostInfo();
|
---|
447 |
|
---|
448 | #ifdef WINDOWS
|
---|
449 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
450 | {
|
---|
451 | static char buf[8092], *temp;
|
---|
452 |
|
---|
453 | SetLastError(0);
|
---|
454 | sprintf( buf, "err=%d", err );
|
---|
455 |
|
---|
456 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
457 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
458 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
459 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
460 | (LPTSTR) &temp, 0, NULL );
|
---|
461 | if ( temp )
|
---|
462 | {
|
---|
463 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
464 | buf[sizeof(buf)-1] = 0;
|
---|
465 | }
|
---|
466 |
|
---|
467 | temp = buf + crStrlen(buf) - 1;
|
---|
468 | while ( temp > buf && isspace( *temp ) )
|
---|
469 | {
|
---|
470 | *temp = '\0';
|
---|
471 | temp--;
|
---|
472 | }
|
---|
473 |
|
---|
474 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
|
---|
475 | }
|
---|
476 | else
|
---|
477 | {
|
---|
478 | offset = sprintf( txt, "[0x%x.0x%x] OpenGL Debug: ", GetCurrentProcessId(), crThreadID());
|
---|
479 | }
|
---|
480 | #else
|
---|
481 | offset = sprintf( txt, "[0x%lx.0x%lx] OpenGL Debug: ", crGetPID(), crThreadID());
|
---|
482 | #endif
|
---|
483 | va_start( args, format );
|
---|
484 | vsprintf( txt + offset, format, args );
|
---|
485 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
486 | if (pfnLogBackdoor)
|
---|
487 | {
|
---|
488 | pfnLogBackdoor(txt);
|
---|
489 | }
|
---|
490 | #endif
|
---|
491 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
492 | if (dbgPrintEnable)
|
---|
493 | {
|
---|
494 | crDebugDbgPrint(txt);
|
---|
495 | }
|
---|
496 | #endif
|
---|
497 | #if defined(IN_GUEST)
|
---|
498 | outputChromiumMessage( output, txt );
|
---|
499 | #else
|
---|
500 | if (!output
|
---|
501 | || output==stderr
|
---|
502 | )
|
---|
503 | {
|
---|
504 | LogRel(("%s\n", txt));
|
---|
505 | }
|
---|
506 | else
|
---|
507 | {
|
---|
508 | LogRel(("%s\n", txt));
|
---|
509 | outputChromiumMessage(output, txt);
|
---|
510 | }
|
---|
511 | #endif
|
---|
512 | va_end( args );
|
---|
513 | }
|
---|