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 | DECLEXPORT(void) crWarning(const char *format, ... )
|
---|
249 | {
|
---|
250 | if (warnings_enabled) {
|
---|
251 | va_list args;
|
---|
252 | static char txt[8092];
|
---|
253 | int offset;
|
---|
254 |
|
---|
255 | __crCheckCanada();
|
---|
256 | __crCheckSwedishChef();
|
---|
257 | __crCheckAustralia();
|
---|
258 | if (!my_hostname[0])
|
---|
259 | __getHostInfo();
|
---|
260 | offset = sprintf( txt, "OpenGL Warning: ");
|
---|
261 | va_start( args, format );
|
---|
262 | vsprintf( txt + offset, format, args );
|
---|
263 | #if defined(IN_GUEST)
|
---|
264 | crDebug("%s", txt);
|
---|
265 | outputChromiumMessage( stderr, txt );
|
---|
266 | #else
|
---|
267 | LogRel(("%s\n", txt));
|
---|
268 | #endif
|
---|
269 | va_end( args );
|
---|
270 |
|
---|
271 | #if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST)
|
---|
272 | DebugBreak();
|
---|
273 | #endif
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | DECLEXPORT(void) crInfo(const char *format, ... )
|
---|
278 | {
|
---|
279 | va_list args;
|
---|
280 | static char txt[8092];
|
---|
281 | int offset;
|
---|
282 |
|
---|
283 | __crCheckCanada();
|
---|
284 | __crCheckSwedishChef();
|
---|
285 | __crCheckAustralia();
|
---|
286 | if (!my_hostname[0])
|
---|
287 | __getHostInfo();
|
---|
288 | offset = sprintf( txt, "OpenGL Info: ");
|
---|
289 | va_start( args, format );
|
---|
290 | vsprintf( txt + offset, format, args );
|
---|
291 | #if defined(IN_GUEST)
|
---|
292 | crDebug("%s", txt);
|
---|
293 | outputChromiumMessage( stderr, txt );
|
---|
294 | #else
|
---|
295 | LogRel(("%s\n", txt));
|
---|
296 | #endif
|
---|
297 | va_end( args );
|
---|
298 | }
|
---|
299 |
|
---|
300 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
301 | static DECLCALLBACK(void) crDebugBackdoorRt(char* pcszStr)
|
---|
302 | {
|
---|
303 | RTLogBackdoorPrintf("%s", pcszStr);
|
---|
304 | }
|
---|
305 |
|
---|
306 | static DECLCALLBACK(void) crDebugBackdoorDispMp(char* pcszStr)
|
---|
307 | {
|
---|
308 | VBoxDispMpLoggerLog(pcszStr);
|
---|
309 | }
|
---|
310 | #endif
|
---|
311 |
|
---|
312 |
|
---|
313 | #if defined(DEBUG) && defined(WINDOWS) /* && (!defined(DEBUG_misha) || !defined(IN_GUEST) ) */
|
---|
314 | # define CR_DEBUG_DBGPRINT_ENABLE
|
---|
315 | #endif
|
---|
316 |
|
---|
317 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
318 | static void crDebugDbgPrint(const char *str)
|
---|
319 | {
|
---|
320 | OutputDebugString(str);
|
---|
321 | OutputDebugString("\n");
|
---|
322 | }
|
---|
323 | #endif
|
---|
324 |
|
---|
325 | DECLEXPORT(void) crDebug(const char *format, ... )
|
---|
326 | {
|
---|
327 | va_list args;
|
---|
328 | static char txt[8092];
|
---|
329 | int offset;
|
---|
330 | #ifdef WINDOWS
|
---|
331 | DWORD err;
|
---|
332 | #endif
|
---|
333 | static FILE *output;
|
---|
334 | static int first_time = 1;
|
---|
335 | static int silent = 0;
|
---|
336 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
337 | typedef DECLCALLBACK(void) FNCRGEDUGBACKDOOR(char* pcszStr);
|
---|
338 | typedef FNCRGEDUGBACKDOOR *PFNCRGEDUGBACKDOOR;
|
---|
339 | static PFNCRGEDUGBACKDOOR pfnLogBackdoor = NULL;
|
---|
340 | #endif
|
---|
341 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
342 | static int dbgPrintEnable = 0;
|
---|
343 | #endif
|
---|
344 |
|
---|
345 | if (first_time)
|
---|
346 | {
|
---|
347 | const char *fname = crGetenv( "CR_DEBUG_FILE" );
|
---|
348 | const char *fnamePrefix = crGetenv( "CR_DEBUG_FILE_PREFIX" );
|
---|
349 | char str[2048];
|
---|
350 | #ifdef CR_DEBUG_CONSOLE_ENABLE
|
---|
351 | int logToConsole = 0;
|
---|
352 | #endif
|
---|
353 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
354 | if (crGetenv( "CR_DEBUG_BACKDOOR" ))
|
---|
355 | {
|
---|
356 | int rc = VBoxDispMpLoggerInit();
|
---|
357 | if (RT_SUCCESS(rc))
|
---|
358 | pfnLogBackdoor = crDebugBackdoorDispMp;
|
---|
359 | else
|
---|
360 | pfnLogBackdoor = crDebugBackdoorRt;
|
---|
361 | }
|
---|
362 | #endif
|
---|
363 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
364 | if (crGetenv( "CR_DEBUG_DBGPRINT" ))
|
---|
365 | {
|
---|
366 | dbgPrintEnable = 1;
|
---|
367 | }
|
---|
368 | #endif
|
---|
369 |
|
---|
370 | if (!fname && fnamePrefix)
|
---|
371 | {
|
---|
372 | char pname[1024];
|
---|
373 | if (crStrlen(fnamePrefix) < sizeof (str) - sizeof (pname) - 20)
|
---|
374 | {
|
---|
375 | crGetProcName(pname, 1024);
|
---|
376 | sprintf(str, "%s_%s_%u.txt", fnamePrefix, pname,
|
---|
377 | #ifdef RT_OS_WINDOWS
|
---|
378 | GetCurrentProcessId()
|
---|
379 | #else
|
---|
380 | crGetPID()
|
---|
381 | #endif
|
---|
382 | );
|
---|
383 | fname = &str[0];
|
---|
384 | }
|
---|
385 | }
|
---|
386 |
|
---|
387 | first_time = 0;
|
---|
388 | if (fname)
|
---|
389 | {
|
---|
390 | char debugFile[2048], *p;
|
---|
391 | crStrcpy(debugFile, fname);
|
---|
392 | p = crStrstr(debugFile, "%p");
|
---|
393 | if (p) {
|
---|
394 | /* replace %p with process number */
|
---|
395 | unsigned long n = (unsigned long) crGetPID();
|
---|
396 | sprintf(p, "%lu", n);
|
---|
397 | }
|
---|
398 | fname = debugFile;
|
---|
399 | output = fopen( fname, "w" );
|
---|
400 | if (!output)
|
---|
401 | {
|
---|
402 | crError( "Couldn't open debug log %s", fname );
|
---|
403 | }
|
---|
404 | }
|
---|
405 | else
|
---|
406 | {
|
---|
407 | #ifdef CR_DEBUG_CONSOLE_ENABLE
|
---|
408 | if (crGetenv( "CR_DEBUG_CONSOLE" ))
|
---|
409 | {
|
---|
410 | crRedirectIOToConsole();
|
---|
411 | logToConsole = 1;
|
---|
412 | }
|
---|
413 | #endif
|
---|
414 | output = stderr;
|
---|
415 | }
|
---|
416 |
|
---|
417 | #if !defined(DEBUG)/* || defined(DEBUG_misha)*/
|
---|
418 | /* Release mode: only emit crDebug messages if CR_DEBUG
|
---|
419 | * or CR_DEBUG_FILE is set.
|
---|
420 | */
|
---|
421 | if (!fname && !crGetenv("CR_DEBUG")
|
---|
422 | #ifdef CR_DEBUG_CONSOLE_ENABLE
|
---|
423 | && !logToConsole
|
---|
424 | #endif
|
---|
425 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
426 | && !pfnLogBackdoor
|
---|
427 | #endif
|
---|
428 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
429 | && !dbgPrintEnable
|
---|
430 | #endif
|
---|
431 | )
|
---|
432 | silent = 1;
|
---|
433 | #endif
|
---|
434 | }
|
---|
435 |
|
---|
436 | if (silent)
|
---|
437 | return;
|
---|
438 |
|
---|
439 | __crCheckCanada();
|
---|
440 | __crCheckSwedishChef();
|
---|
441 | __crCheckAustralia();
|
---|
442 | if (!my_hostname[0])
|
---|
443 | __getHostInfo();
|
---|
444 |
|
---|
445 | #ifdef WINDOWS
|
---|
446 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
447 | {
|
---|
448 | static char buf[8092], *temp;
|
---|
449 |
|
---|
450 | SetLastError(0);
|
---|
451 | sprintf( buf, "err=%d", err );
|
---|
452 |
|
---|
453 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
454 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
455 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
456 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
457 | (LPTSTR) &temp, 0, NULL );
|
---|
458 | if ( temp )
|
---|
459 | {
|
---|
460 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
461 | buf[sizeof(buf)-1] = 0;
|
---|
462 | }
|
---|
463 |
|
---|
464 | temp = buf + crStrlen(buf) - 1;
|
---|
465 | while ( temp > buf && isspace( *temp ) )
|
---|
466 | {
|
---|
467 | *temp = '\0';
|
---|
468 | temp--;
|
---|
469 | }
|
---|
470 |
|
---|
471 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
|
---|
472 | }
|
---|
473 | else
|
---|
474 | {
|
---|
475 | offset = sprintf( txt, "[0x%x.0x%x] OpenGL Debug: ", GetCurrentProcessId(), crThreadID());
|
---|
476 | }
|
---|
477 | #else
|
---|
478 | offset = sprintf( txt, "[0x%lx.0x%lx] OpenGL Debug: ", crGetPID(), crThreadID());
|
---|
479 | #endif
|
---|
480 | va_start( args, format );
|
---|
481 | vsprintf( txt + offset, format, args );
|
---|
482 | #ifdef CR_DEBUG_BACKDOOR_ENABLE
|
---|
483 | if (pfnLogBackdoor)
|
---|
484 | {
|
---|
485 | pfnLogBackdoor(txt);
|
---|
486 | }
|
---|
487 | #endif
|
---|
488 | #ifdef CR_DEBUG_DBGPRINT_ENABLE
|
---|
489 | if (dbgPrintEnable)
|
---|
490 | {
|
---|
491 | crDebugDbgPrint(txt);
|
---|
492 | }
|
---|
493 | #endif
|
---|
494 | #if defined(IN_GUEST)
|
---|
495 | outputChromiumMessage( output, txt );
|
---|
496 | #else
|
---|
497 | if (!output
|
---|
498 | # ifndef DEBUG_misha
|
---|
499 | || output==stderr
|
---|
500 | # endif
|
---|
501 | )
|
---|
502 | {
|
---|
503 | LogRel(("%s\n", txt));
|
---|
504 | }
|
---|
505 | else
|
---|
506 | {
|
---|
507 | # ifndef DEBUG_misha
|
---|
508 | LogRel(("%s\n", txt));
|
---|
509 | # endif
|
---|
510 | outputChromiumMessage(output, txt);
|
---|
511 | }
|
---|
512 | #endif
|
---|
513 | va_end( args );
|
---|
514 | }
|
---|