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