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_TO_CONSOLE_ENABLE
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #if defined(WINDOWS) && defined(IN_GUEST)
|
---|
35 | # ifndef CR_DEBUG_TO_BACKDOOR_ENABLE
|
---|
36 | # error "CR_DEBUG_TO_BACKDOOR_ENABLE is expected!"
|
---|
37 | # endif
|
---|
38 | #else
|
---|
39 | # ifdef CR_DEBUG_TO_BACKDOOR_ENABLE
|
---|
40 | # error "CR_DEBUG_TO_BACKDOOR_ENABLE is NOT expected!"
|
---|
41 | # endif
|
---|
42 | #endif
|
---|
43 |
|
---|
44 |
|
---|
45 | #ifdef CR_DEBUG_TO_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 | DECLEXPORT(void) crDebug(const char *format, ... )
|
---|
304 | {
|
---|
305 | va_list args;
|
---|
306 | static char txt[8092];
|
---|
307 | int offset;
|
---|
308 | #ifdef WINDOWS
|
---|
309 | DWORD err;
|
---|
310 | #endif
|
---|
311 | static FILE *output;
|
---|
312 | static int first_time = 1;
|
---|
313 | static int silent = 0;
|
---|
314 | #ifdef CR_DEBUG_TO_BACKDOOR_ENABLE
|
---|
315 | static int logBackdoor = 0;
|
---|
316 | #endif
|
---|
317 |
|
---|
318 | if (first_time)
|
---|
319 | {
|
---|
320 | const char *fname = crGetenv( "CR_DEBUG_FILE" );
|
---|
321 | const char *fnamePrefix = crGetenv( "CR_DEBUG_FILE_PREFIX" );
|
---|
322 | char str[2048];
|
---|
323 | #ifdef CR_DEBUG_TO_CONSOLE_ENABLE
|
---|
324 | int logToConsole = 0;
|
---|
325 | #endif
|
---|
326 | #ifdef CR_DEBUG_TO_BACKDOOR_ENABLE
|
---|
327 | if (crGetenv( "CR_DEBUG_TO_BACKDOOR" ))
|
---|
328 | {
|
---|
329 | int rc = VBoxDispMpLoggerInit();
|
---|
330 | if (RT_SUCCESS(rc))
|
---|
331 | logBackdoor = 1;
|
---|
332 | }
|
---|
333 | #endif
|
---|
334 |
|
---|
335 | if (!fname && fnamePrefix)
|
---|
336 | {
|
---|
337 | char pname[1024];
|
---|
338 | if (crStrlen(fnamePrefix) < sizeof (str) - sizeof (pname) - 20)
|
---|
339 | {
|
---|
340 | crGetProcName(pname, 1024);
|
---|
341 | sprintf(str, "%s_%s_%u.txt", fnamePrefix, pname,
|
---|
342 | #ifdef RT_OS_WINDOWS
|
---|
343 | GetCurrentProcessId()
|
---|
344 | #else
|
---|
345 | crGetPID()
|
---|
346 | #endif
|
---|
347 | );
|
---|
348 | fname = &str[0];
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | first_time = 0;
|
---|
353 | if (fname)
|
---|
354 | {
|
---|
355 | char debugFile[2048], *p;
|
---|
356 | crStrcpy(debugFile, fname);
|
---|
357 | p = crStrstr(debugFile, "%p");
|
---|
358 | if (p) {
|
---|
359 | /* replace %p with process number */
|
---|
360 | unsigned long n = (unsigned long) crGetPID();
|
---|
361 | sprintf(p, "%lu", n);
|
---|
362 | }
|
---|
363 | fname = debugFile;
|
---|
364 | output = fopen( fname, "w" );
|
---|
365 | if (!output)
|
---|
366 | {
|
---|
367 | crError( "Couldn't open debug log %s", fname );
|
---|
368 | }
|
---|
369 | }
|
---|
370 | else
|
---|
371 | {
|
---|
372 | #ifdef CR_DEBUG_TO_CONSOLE_ENABLE
|
---|
373 | if (crGetenv( "CR_DEBUG_TO_CONSOLE" ))
|
---|
374 | {
|
---|
375 | crRedirectIOToConsole();
|
---|
376 | logToConsole = 1;
|
---|
377 | }
|
---|
378 | #endif
|
---|
379 | output = stderr;
|
---|
380 | }
|
---|
381 |
|
---|
382 | #if !defined(DEBUG)/* || defined(DEBUG_misha)*/
|
---|
383 | /* Release mode: only emit crDebug messages if CR_DEBUG
|
---|
384 | * or CR_DEBUG_FILE is set.
|
---|
385 | */
|
---|
386 | if (!fname && !crGetenv("CR_DEBUG")
|
---|
387 | #ifdef CR_DEBUG_TO_CONSOLE_ENABLE
|
---|
388 | && !logToConsole
|
---|
389 | #endif
|
---|
390 | #ifdef CR_DEBUG_TO_BACKDOOR_ENABLE
|
---|
391 | && !logBackdoor
|
---|
392 | #endif
|
---|
393 | )
|
---|
394 | silent = 1;
|
---|
395 | #endif
|
---|
396 | }
|
---|
397 |
|
---|
398 | if (silent)
|
---|
399 | return;
|
---|
400 |
|
---|
401 | __crCheckCanada();
|
---|
402 | __crCheckSwedishChef();
|
---|
403 | __crCheckAustralia();
|
---|
404 | if (!my_hostname[0])
|
---|
405 | __getHostInfo();
|
---|
406 |
|
---|
407 | #ifdef WINDOWS
|
---|
408 | if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
|
---|
409 | {
|
---|
410 | static char buf[8092], *temp;
|
---|
411 |
|
---|
412 | SetLastError(0);
|
---|
413 | sprintf( buf, "err=%d", err );
|
---|
414 |
|
---|
415 | FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
---|
416 | FORMAT_MESSAGE_FROM_SYSTEM |
|
---|
417 | FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
|
---|
418 | MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
|
---|
419 | (LPTSTR) &temp, 0, NULL );
|
---|
420 | if ( temp )
|
---|
421 | {
|
---|
422 | crStrncpy( buf, temp, sizeof(buf)-1 );
|
---|
423 | buf[sizeof(buf)-1] = 0;
|
---|
424 | }
|
---|
425 |
|
---|
426 | temp = buf + crStrlen(buf) - 1;
|
---|
427 | while ( temp > buf && isspace( *temp ) )
|
---|
428 | {
|
---|
429 | *temp = '\0';
|
---|
430 | temp--;
|
---|
431 | }
|
---|
432 |
|
---|
433 | offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
|
---|
434 | }
|
---|
435 | else
|
---|
436 | {
|
---|
437 | offset = sprintf( txt, "[0x%x.0x%x] OpenGL Debug: ", GetCurrentProcessId(), crThreadID());
|
---|
438 | }
|
---|
439 | #else
|
---|
440 | offset = sprintf( txt, "[0x%lx.0x%lx] OpenGL Debug: ", crGetPID(), crThreadID());
|
---|
441 | #endif
|
---|
442 | va_start( args, format );
|
---|
443 | vsprintf( txt + offset, format, args );
|
---|
444 | #ifdef CR_DEBUG_TO_BACKDOOR_ENABLE
|
---|
445 | if (logBackdoor)
|
---|
446 | {
|
---|
447 | VBoxDispMpLoggerLog(txt);
|
---|
448 | }
|
---|
449 | #endif
|
---|
450 | #if defined(IN_GUEST)
|
---|
451 | outputChromiumMessage( output, txt );
|
---|
452 | #else
|
---|
453 | if (!output
|
---|
454 | # ifndef DEBUG_misha
|
---|
455 | || output==stderr
|
---|
456 | # endif
|
---|
457 | )
|
---|
458 | {
|
---|
459 | LogRel(("%s\n", txt));
|
---|
460 | }
|
---|
461 | else
|
---|
462 | {
|
---|
463 | # ifndef DEBUG_misha
|
---|
464 | LogRel(("%s\n", txt));
|
---|
465 | # endif
|
---|
466 | outputChromiumMessage(output, txt);
|
---|
467 | }
|
---|
468 | #endif
|
---|
469 | va_end( args );
|
---|
470 | }
|
---|