VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/error.c@ 41432

Last change on this file since 41432 was 41432, checked in by vboxsync, 13 years ago

don't build libwine & don't include it in GA installer

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.3 KB
Line 
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
53static char my_hostname[256];
54#ifdef WINDOWS
55static HANDLE my_pid;
56#else
57static int my_pid = 0;
58#endif
59static int canada = 0;
60static int swedish_chef = 0;
61static int australia = 0;
62static int warnings_enabled = 1;
63
64#ifdef DEBUG_misha
65//int g_VBoxFbgFBreakDdi = 0;
66#define DebugBreak() Assert(0)
67#endif
68
69void __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
89static 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
101static 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
113static 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
126static 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#if defined(DEBUG) && defined(WINDOWS) /* && (!defined(DEBUG_misha) || !defined(IN_GUEST) ) */
136 OutputDebugString(str);
137 OutputDebugString("\n");
138#endif
139}
140
141#ifdef WINDOWS
142static void crRedirectIOToConsole()
143{
144 int hConHandle;
145 HANDLE StdHandle;
146 FILE *fp;
147
148 AllocConsole();
149
150 StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
151 hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
152 fp = _fdopen( hConHandle, "w" );
153 *stdout = *fp;
154 *stderr = *fp;
155
156 StdHandle = GetStdHandle(STD_INPUT_HANDLE);
157 hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
158 fp = _fdopen( hConHandle, "r" );
159 *stdin = *fp;
160}
161#endif
162
163
164DECLEXPORT(void) crError(const char *format, ... )
165{
166 va_list args;
167 static char txt[8092];
168 int offset;
169#ifdef WINDOWS
170 DWORD err;
171#endif
172
173 __crCheckCanada();
174 __crCheckSwedishChef();
175 __crCheckAustralia();
176 if (!my_hostname[0])
177 __getHostInfo();
178#ifdef WINDOWS
179 if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
180 {
181 static char buf[8092], *temp;
182
183 SetLastError(0);
184 sprintf( buf, "err=%d", err );
185
186 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
187 FORMAT_MESSAGE_FROM_SYSTEM |
188 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
189 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
190 (LPTSTR) &temp, 0, NULL );
191 if ( temp )
192 {
193 crStrncpy( buf, temp, sizeof(buf)-1 );
194 buf[sizeof(buf)-1] = 0;
195 }
196
197 temp = buf + crStrlen(buf) - 1;
198 while ( temp > buf && isspace( *temp ) )
199 {
200 *temp = '\0';
201 temp--;
202 }
203
204 offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t----------------------\nCR Error(%s:%d): ", buf, my_hostname, my_pid );
205 }
206 else
207 {
208 offset = sprintf( txt, "OpenGL Error: ");
209 }
210#else
211 offset = sprintf( txt, "OpenGL Error: " );
212#endif
213 va_start( args, format );
214 vsprintf( txt + offset, format, args );
215#if defined(IN_GUEST)
216 crDebug("%s", txt);
217 outputChromiumMessage( stderr, txt );
218#else
219 LogRel(("%s\n", txt));
220#endif
221#ifdef WINDOWS
222 if (crGetenv( "CR_GUI_ERROR" ) != NULL)
223 {
224 MessageBox( NULL, txt, "Chromium Error", MB_OK );
225 }
226 else
227 {
228#endif
229 va_end( args );
230#ifdef WINDOWS
231 }
232#if !defined(DEBUG_leo) && !defined(DEBUG_ll158262) && !defined(DEBUG_misha)
233 if (crGetenv( "CR_DEBUG_ON_ERROR" ) != NULL)
234#endif
235 {
236 DebugBreak();
237 }
238#endif
239
240#ifdef IN_GUEST
241 /* Give chance for things to close down */
242 raise( SIGTERM );
243
244 exit(1);
245#endif
246}
247
248void crEnableWarnings(int onOff)
249{
250 warnings_enabled = onOff;
251}
252
253DECLEXPORT(void) crWarning(const char *format, ... )
254{
255 if (warnings_enabled) {
256 va_list args;
257 static char txt[8092];
258 int offset;
259
260 __crCheckCanada();
261 __crCheckSwedishChef();
262 __crCheckAustralia();
263 if (!my_hostname[0])
264 __getHostInfo();
265 offset = sprintf( txt, "OpenGL Warning: ");
266 va_start( args, format );
267 vsprintf( txt + offset, format, args );
268#if defined(IN_GUEST)
269 crDebug("%s", txt);
270 outputChromiumMessage( stderr, txt );
271#else
272 LogRel(("%s\n", txt));
273#endif
274 va_end( args );
275
276#if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST)
277 DebugBreak();
278#endif
279 }
280}
281
282DECLEXPORT(void) crInfo(const char *format, ... )
283{
284 va_list args;
285 static char txt[8092];
286 int offset;
287
288 __crCheckCanada();
289 __crCheckSwedishChef();
290 __crCheckAustralia();
291 if (!my_hostname[0])
292 __getHostInfo();
293 offset = sprintf( txt, "OpenGL Info: ");
294 va_start( args, format );
295 vsprintf( txt + offset, format, args );
296#if defined(IN_GUEST)
297 crDebug("%s", txt);
298 outputChromiumMessage( stderr, txt );
299#else
300 LogRel(("%s\n", txt));
301#endif
302 va_end( args );
303}
304
305#ifdef CR_DEBUG_BACKDOOR_ENABLE
306static DECLCALLBACK(void) crDebugBackdoorRt(char* pcszStr)
307{
308 RTLogBackdoorPrintf("%s", pcszStr);
309}
310
311static DECLCALLBACK(void) crDebugBackdoorDispMp(char* pcszStr)
312{
313 VBoxDispMpLoggerLog(pcszStr);
314}
315#endif
316
317DECLEXPORT(void) crDebug(const char *format, ... )
318{
319 va_list args;
320 static char txt[8092];
321 int offset;
322#ifdef WINDOWS
323 DWORD err;
324#endif
325 static FILE *output;
326 static int first_time = 1;
327 static int silent = 0;
328#ifdef CR_DEBUG_BACKDOOR_ENABLE
329 typedef DECLCALLBACK(void) FNCRGEDUGBACKDOOR(char* pcszStr);
330 typedef FNCRGEDUGBACKDOOR *PFNCRGEDUGBACKDOOR;
331 static PFNCRGEDUGBACKDOOR pfnLogBackdoor = NULL;
332#endif
333
334 if (first_time)
335 {
336 const char *fname = crGetenv( "CR_DEBUG_FILE" );
337 const char *fnamePrefix = crGetenv( "CR_DEBUG_FILE_PREFIX" );
338 char str[2048];
339#ifdef CR_DEBUG_CONSOLE_ENABLE
340 int logToConsole = 0;
341#endif
342#ifdef CR_DEBUG_BACKDOOR_ENABLE
343 if (crGetenv( "CR_DEBUG_BACKDOOR" ))
344 {
345 int rc = VBoxDispMpLoggerInit();
346 if (RT_SUCCESS(rc))
347 pfnLogBackdoor = crDebugBackdoorDispMp;
348 else
349 pfnLogBackdoor = crDebugBackdoorRt;
350 }
351#endif
352
353 if (!fname && fnamePrefix)
354 {
355 char pname[1024];
356 if (crStrlen(fnamePrefix) < sizeof (str) - sizeof (pname) - 20)
357 {
358 crGetProcName(pname, 1024);
359 sprintf(str, "%s_%s_%u.txt", fnamePrefix, pname,
360#ifdef RT_OS_WINDOWS
361 GetCurrentProcessId()
362#else
363 crGetPID()
364#endif
365 );
366 fname = &str[0];
367 }
368 }
369
370 first_time = 0;
371 if (fname)
372 {
373 char debugFile[2048], *p;
374 crStrcpy(debugFile, fname);
375 p = crStrstr(debugFile, "%p");
376 if (p) {
377 /* replace %p with process number */
378 unsigned long n = (unsigned long) crGetPID();
379 sprintf(p, "%lu", n);
380 }
381 fname = debugFile;
382 output = fopen( fname, "w" );
383 if (!output)
384 {
385 crError( "Couldn't open debug log %s", fname );
386 }
387 }
388 else
389 {
390#ifdef CR_DEBUG_CONSOLE_ENABLE
391 if (crGetenv( "CR_DEBUG_CONSOLE" ))
392 {
393 crRedirectIOToConsole();
394 logToConsole = 1;
395 }
396#endif
397 output = stderr;
398 }
399
400#if !defined(DEBUG)/* || defined(DEBUG_misha)*/
401 /* Release mode: only emit crDebug messages if CR_DEBUG
402 * or CR_DEBUG_FILE is set.
403 */
404 if (!fname && !crGetenv("CR_DEBUG")
405#ifdef CR_DEBUG_CONSOLE_ENABLE
406 && !logToConsole
407#endif
408#ifdef CR_DEBUG_BACKDOOR_ENABLE
409 && !pfnLogBackdoor
410#endif
411 )
412 silent = 1;
413#endif
414 }
415
416 if (silent)
417 return;
418
419 __crCheckCanada();
420 __crCheckSwedishChef();
421 __crCheckAustralia();
422 if (!my_hostname[0])
423 __getHostInfo();
424
425#ifdef WINDOWS
426 if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
427 {
428 static char buf[8092], *temp;
429
430 SetLastError(0);
431 sprintf( buf, "err=%d", err );
432
433 FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
434 FORMAT_MESSAGE_FROM_SYSTEM |
435 FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
436 MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
437 (LPTSTR) &temp, 0, NULL );
438 if ( temp )
439 {
440 crStrncpy( buf, temp, sizeof(buf)-1 );
441 buf[sizeof(buf)-1] = 0;
442 }
443
444 temp = buf + crStrlen(buf) - 1;
445 while ( temp > buf && isspace( *temp ) )
446 {
447 *temp = '\0';
448 temp--;
449 }
450
451 offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
452 }
453 else
454 {
455 offset = sprintf( txt, "[0x%x.0x%x] OpenGL Debug: ", GetCurrentProcessId(), crThreadID());
456 }
457#else
458 offset = sprintf( txt, "[0x%lx.0x%lx] OpenGL Debug: ", crGetPID(), crThreadID());
459#endif
460 va_start( args, format );
461 vsprintf( txt + offset, format, args );
462#ifdef CR_DEBUG_BACKDOOR_ENABLE
463 if (pfnLogBackdoor)
464 {
465 pfnLogBackdoor(txt);
466 }
467#endif
468#if defined(IN_GUEST)
469 outputChromiumMessage( output, txt );
470#else
471 if (!output
472# ifndef DEBUG_misha
473 || output==stderr
474# endif
475 )
476 {
477 LogRel(("%s\n", txt));
478 }
479 else
480 {
481# ifndef DEBUG_misha
482 LogRel(("%s\n", txt));
483# endif
484 outputChromiumMessage(output, txt);
485 }
486#endif
487 va_end( args );
488}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette