summaryrefslogtreecommitdiff
path: root/src/VBox/GuestHost/OpenGL/util/error.c
blob: 48fbc577a947c43b7e29b6bb743fa3a79b8fa716 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/* Copyright (c) 2001, Stanford University
 * All rights reserved
 *
 * See the file LICENSE.txt for information on redistributing this software.
 */

#include "cr_environment.h"
#include "cr_error.h"
#include "cr_string.h"
#include "cr_net.h"
#include "cr_process.h"

#ifdef WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <signal.h>

#ifndef IN_GUEST
#define LOG_GROUP LOG_GROUP_SHARED_CROPENGL
#endif
#if !defined(IN_GUEST) || defined(CR_DEBUG_BACKDOOR_ENABLE)
#include <VBox/log.h>
#endif

#if defined(WINDOWS)
# define CR_DEBUG_CONSOLE_ENABLE
#endif

#if defined(WINDOWS) && defined(IN_GUEST)
# ifndef CR_DEBUG_BACKDOOR_ENABLE
#  error "CR_DEBUG_BACKDOOR_ENABLE is expected!"
# endif
#else
# ifdef CR_DEBUG_BACKDOOR_ENABLE
#  error "CR_DEBUG_BACKDOOR_ENABLE is NOT expected!"
# endif
#endif


#ifdef CR_DEBUG_BACKDOOR_ENABLE
# include <VBoxDispMpLogger.h>
# include <iprt/err.h>
#endif


static char my_hostname[256];
#ifdef WINDOWS
static HANDLE my_pid;
#else
static int my_pid = 0;
#endif
static int canada = 0;
static int swedish_chef = 0;
static int australia = 0;
static int warnings_enabled = 1;

#ifdef DEBUG_misha
//int g_VBoxFbgFBreakDdi = 0;
#define DebugBreak() Assert(0)
#endif

void __getHostInfo( void )
{
    char *temp;
    /* on windows guests we're typically get called in a context of VBoxOGL!DllMain ( which calls VBoxOGLcrutil!crNetInit ),
     * which may lead to deadlocks..
     * Avoid it as it is needed for debugging purposes only */
#if !defined(IN_GUEST) || !defined(RT_OS_WINDOWS)
    if ( crGetHostname( my_hostname, sizeof( my_hostname ) ) )
#endif
    {
        crStrcpy( my_hostname, "????" );
    }
    temp = crStrchr( my_hostname, '.' );
    if (temp)
    {
        *temp = '\0';
    }
    my_pid = crGetPID();
}

static void __crCheckCanada(void)
{
    static int first = 1;
    if (first)
    {
        const char *env = crGetenv( "CR_CANADA" );
        if (env)
            canada = 1;
        first = 0;
    }
}

static void __crCheckSwedishChef(void)
{
    static int first = 1;
    if (first)
    {
        const char *env = crGetenv( "CR_SWEDEN" );
        if (env)
            swedish_chef = 1;
        first = 0;
    }
}

static void __crCheckAustralia(void)
{
    static int first = 1;
    if (first)
    {
        const char *env = crGetenv( "CR_AUSTRALIA" );
        const char *env2 = crGetenv( "CR_AUSSIE" );
        if (env || env2)
            australia = 1;
        first = 0;
    }
}

static void outputChromiumMessage( FILE *output, char *str )
{
    fprintf( output, "%s%s%s%s\n", str,
            swedish_chef ? " BORK BORK BORK!" : "",
            canada ? ", eh?" : "",
            australia ? ", mate!" : ""
            );
    fflush( output );
}

#ifdef WINDOWS
static void crRedirectIOToConsole()
{
    int hConHandle;
    HANDLE StdHandle;
    FILE *fp;

    AllocConsole();

    StdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "w" );
    *stdout = *fp;
    *stderr = *fp;

    StdHandle = GetStdHandle(STD_INPUT_HANDLE);
    hConHandle = _open_osfhandle((long)StdHandle, _O_TEXT);
    fp = _fdopen( hConHandle, "r" );
    *stdin = *fp;
}
#endif


DECLEXPORT(void) crError(const char *format, ... )
{
    va_list args;
    static char txt[8092];
    int offset;
#ifdef WINDOWS
    DWORD err;
#endif

    __crCheckCanada();
    __crCheckSwedishChef();
    __crCheckAustralia();
    if (!my_hostname[0])
        __getHostInfo();
#ifdef WINDOWS
    if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
    {
        static char buf[8092], *temp;

        SetLastError(0);
        sprintf( buf, "err=%d", err );

        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
                MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
                (LPTSTR) &temp, 0, NULL );
        if ( temp )
        {
            crStrncpy( buf, temp, sizeof(buf)-1 );
            buf[sizeof(buf)-1] = 0;
        }

        temp = buf + crStrlen(buf) - 1;
        while ( temp > buf && isspace( *temp ) )
        {
            *temp = '\0';
            temp--;
        }

        offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t----------------------\nCR Error(%s:%d): ", buf, my_hostname, my_pid );
    }
    else
    {
        offset = sprintf( txt, "OpenGL Error: ");
    }
#else
    offset = sprintf( txt, "OpenGL Error: " );
#endif
    va_start( args, format );
    vsprintf( txt + offset, format, args );
#if defined(IN_GUEST)
    crDebug("%s", txt);
    outputChromiumMessage( stderr, txt );
#else
    LogRel(("%s\n", txt));
#endif
#ifdef WINDOWS
    if (crGetenv( "CR_GUI_ERROR" ) != NULL)
    {
        MessageBox( NULL, txt, "Chromium Error", MB_OK );
    }
    else
    {
#endif
        va_end( args );
#ifdef WINDOWS
    }
#if !defined(DEBUG_leo) && !defined(DEBUG_ll158262) && !defined(DEBUG_misha)
    if (crGetenv( "CR_DEBUG_ON_ERROR" ) != NULL)
#endif
    {
        DebugBreak();
    }
#endif

#ifdef IN_GUEST
    /* Give chance for things to close down */
    raise( SIGTERM );

    exit(1);
#endif
}

void crEnableWarnings(int onOff)
{
    warnings_enabled = onOff;
}

#ifdef DEBUG_misha
# undef crWarning
#endif
DECLEXPORT(void) crWarning(const char *format, ... )
{
    if (warnings_enabled) {
        va_list args;
        static char txt[8092];
        int offset;

        __crCheckCanada();
        __crCheckSwedishChef();
        __crCheckAustralia();
        if (!my_hostname[0])
            __getHostInfo();
        offset = sprintf( txt, "OpenGL Warning: ");
        va_start( args, format );
        vsprintf( txt + offset, format, args );
#if defined(IN_GUEST)
        crDebug("%s", txt);
        outputChromiumMessage( stderr, txt );
#else
        LogRel(("%s\n", txt));
#endif
        va_end( args );

#if defined(WINDOWS) && defined(DEBUG) && !defined(IN_GUEST) && defined(DEBUG_misha)
        DebugBreak();
#endif
    }
}

DECLEXPORT(void) crInfo(const char *format, ... )
{
    va_list args;
    static char txt[8092];
    int offset;

    __crCheckCanada();
    __crCheckSwedishChef();
    __crCheckAustralia();
    if (!my_hostname[0])
        __getHostInfo();
    offset = sprintf( txt, "OpenGL Info: ");
    va_start( args, format );
    vsprintf( txt + offset, format, args );
#if defined(IN_GUEST)
    crDebug("%s", txt);
    outputChromiumMessage( stderr, txt );
#else
    LogRel(("%s\n", txt));
#endif
    va_end( args );
}

#ifdef CR_DEBUG_BACKDOOR_ENABLE
static DECLCALLBACK(void) crDebugBackdoorRt(char* pcszStr)
{
    RTLogBackdoorPrintf("%s", pcszStr);
}

static DECLCALLBACK(void) crDebugBackdoorDispMp(char* pcszStr)
{
    VBoxDispMpLoggerLog(pcszStr);
}
#endif


#if defined(DEBUG) && defined(WINDOWS) /* && (!defined(DEBUG_misha) || !defined(IN_GUEST) ) */
# define CR_DEBUG_DBGPRINT_ENABLE
#endif

#ifdef CR_DEBUG_DBGPRINT_ENABLE
static void crDebugDbgPrint(const char *str)
{
    OutputDebugString(str);
    OutputDebugString("\n");
}
#endif

DECLEXPORT(void) crDebug(const char *format, ... )
{
    va_list args;
    static char txt[8092];
    int offset;
#ifdef WINDOWS
    DWORD err;
#endif
    static FILE *output;
    static int first_time = 1;
    static int silent = 0;
#ifdef CR_DEBUG_BACKDOOR_ENABLE
    typedef DECLCALLBACK(void) FNCRGEDUGBACKDOOR(char* pcszStr);
    typedef FNCRGEDUGBACKDOOR *PFNCRGEDUGBACKDOOR;
    static PFNCRGEDUGBACKDOOR pfnLogBackdoor = NULL;
#endif
#ifdef CR_DEBUG_DBGPRINT_ENABLE
    static int dbgPrintEnable = 0;
#endif

    if (first_time)
    {
        const char *fname = crGetenv( "CR_DEBUG_FILE" );
        const char *fnamePrefix = crGetenv( "CR_DEBUG_FILE_PREFIX" );
        char str[2048];
#ifdef CR_DEBUG_CONSOLE_ENABLE
        int logToConsole = 0;
#endif
#ifdef CR_DEBUG_BACKDOOR_ENABLE
        if (crGetenv( "CR_DEBUG_BACKDOOR" ))
        {
            int rc = VBoxDispMpLoggerInit();
            if (RT_SUCCESS(rc))
                pfnLogBackdoor = crDebugBackdoorDispMp;
            else
                pfnLogBackdoor = crDebugBackdoorRt;
        }
#endif
#ifdef CR_DEBUG_DBGPRINT_ENABLE
        if (crGetenv( "CR_DEBUG_DBGPRINT" ))
        {
            dbgPrintEnable = 1;
        }
#endif

        if (!fname && fnamePrefix)
        {
            char pname[1024];
            if (crStrlen(fnamePrefix) < sizeof (str) - sizeof (pname) - 20)
            {
                crGetProcName(pname, 1024);
                sprintf(str,
#ifdef RT_OS_WINDOWS
                "%s_%s_%u.txt", fnamePrefix, pname, GetCurrentProcessId()
#else
                "%s_%s_%lu.txt", fnamePrefix, pname, crGetPID()
#endif
                );
                fname = &str[0];
            }
        }

        first_time = 0;
        if (fname)
        {
            char debugFile[2048], *p;
            crStrcpy(debugFile, fname);
            p = crStrstr(debugFile, "%p");
            if (p) {
                /* replace %p with process number */
                unsigned long n = (unsigned long) crGetPID();
                sprintf(p, "%lu", n);
            }
            fname = debugFile;
            output = fopen( fname, "w" );
            if (!output)
            {
                crError( "Couldn't open debug log %s", fname );
            }
        }
        else
        {
#ifdef CR_DEBUG_CONSOLE_ENABLE
            if (crGetenv( "CR_DEBUG_CONSOLE" ))
            {
                crRedirectIOToConsole();
                logToConsole = 1;
            }
#endif
            output = stderr;
        }

#if !defined(DEBUG)/* || defined(DEBUG_misha)*/
        /* Release mode: only emit crDebug messages if CR_DEBUG
         * or CR_DEBUG_FILE is set.
         */
        if (!fname && !crGetenv("CR_DEBUG")
#ifdef CR_DEBUG_CONSOLE_ENABLE
                    && !logToConsole
#endif
#ifdef CR_DEBUG_BACKDOOR_ENABLE
                    && !pfnLogBackdoor
#endif
#ifdef CR_DEBUG_DBGPRINT_ENABLE
                    && !dbgPrintEnable
#endif
                )
            silent = 1;
#endif
    }

    if (silent)
        return;

    __crCheckCanada();
    __crCheckSwedishChef();
    __crCheckAustralia();
    if (!my_hostname[0])
        __getHostInfo();

#ifdef WINDOWS
    if ((err = GetLastError()) != 0 && crGetenv( "CR_WINDOWS_ERRORS" ) != NULL )
    {
        static char buf[8092], *temp;

        SetLastError(0);
        sprintf( buf, "err=%d", err );

        FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, err,
                MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ),
                (LPTSTR) &temp, 0, NULL );
        if ( temp )
        {
            crStrncpy( buf, temp, sizeof(buf)-1 );
            buf[sizeof(buf)-1] = 0;
        }

        temp = buf + crStrlen(buf) - 1;
        while ( temp > buf && isspace( *temp ) )
        {
            *temp = '\0';
            temp--;
        }

        offset = sprintf( txt, "\t-----------------------\n\tWindows ERROR: %s\n\t-----------------\nCR Debug(%s:%d): ", buf, my_hostname, my_pid );
    }
    else
    {
        offset = sprintf( txt, "[0x%x.0x%x] OpenGL Debug: ", GetCurrentProcessId(), crThreadID());
    }
#else
    offset = sprintf( txt, "[0x%lx.0x%lx] OpenGL Debug: ", crGetPID(), crThreadID());
#endif
    va_start( args, format );
    vsprintf( txt + offset, format, args );
#ifdef CR_DEBUG_BACKDOOR_ENABLE
    if (pfnLogBackdoor)
    {
        pfnLogBackdoor(txt);
    }
#endif
#ifdef CR_DEBUG_DBGPRINT_ENABLE
    if (dbgPrintEnable)
    {
        crDebugDbgPrint(txt);
    }
#endif
#if defined(IN_GUEST)
    outputChromiumMessage( output, txt );
#else
    if (!output
#ifndef DEBUG_misha
            || output==stderr
#endif
            )
    {
        LogRel(("%s\n", txt));
    }
    else
    {
        LogRel(("%s\n", txt));
        outputChromiumMessage(output, txt);
    }
#endif
    va_end( args );
}