VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/shaderlib/libWineStub/debug.c@ 55747

Last change on this file since 55747 was 53222, checked in by vboxsync, 10 years ago

Devices/vmsvga: enabled 3D and a few warning fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/*
2 * Management of the debugging channels
3 *
4 * Copyright 2000 Alexandre Julliard
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21/*
22 * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
23 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
24 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
25 * a choice of LGPL license versions is made available with the language indicating
26 * that LGPLv2 or any later version may be used, or where a choice of which version
27 * of the LGPL is applied is otherwise unspecified.
28 */
29
30#include "config.h"
31#include "wine/port.h"
32
33#include "initguid.h"
34#include <objbase.h>
35#include <wine/wined3d.h>
36#include <windows.h>
37
38#include <stdlib.h>
39#include <stdio.h>
40#include <stdarg.h>
41#include <string.h>
42#include <ctype.h>
43
44#include "wine/debug.h"
45//#include "wine/library.h"
46
47#ifdef VBOX_WITH_WDDM
48#include <VBoxDispMpLogger.h>
49#include <iprt/err.h>
50#else
51#include <iprt/log.h>
52#endif
53
54#if VBOX_WITH_VMSVGA
55/* WINE defines this as inline in its headers, directly accessing a memory location */
56#ifndef RT_OS_WINDOWS
57#define GetCurrentThreadId() (1)
58#endif
59#endif
60
61static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
62
63#define MAX_DEBUG_OPTIONS 256
64
65typedef DECLCALLBACK(void) FNVBOXWINELOGBACKDOOR(char* pcszStr);
66typedef FNVBOXWINELOGBACKDOOR *PFNVBOXWINELOGBACKDOOR;
67static PFNVBOXWINELOGBACKDOOR vbox_log_backdoor = NULL;
68static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME) | (1 << __WINE_DBCL_WARN);
69static int nb_debug_options = -1;
70static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
71
72static struct __wine_debug_functions funcs;
73
74static void debug_init(void);
75
76static int cmp_name( const void *p1, const void *p2 )
77{
78 const char *name = p1;
79 const struct __wine_debug_channel *chan = p2;
80 return strcmp( name, chan->name );
81}
82
83/* get the flags to use for a given channel, possibly setting them too in case of lazy init */
84unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
85{
86 if (nb_debug_options == -1) debug_init();
87
88 if (nb_debug_options)
89 {
90 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
91 sizeof(debug_options[0]), cmp_name );
92 if (opt) return opt->flags;
93 }
94 /* no option for this channel */
95 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
96 return default_flags;
97}
98
99/* set the flags to use for a given channel; return 0 if the channel is not available to set */
100int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
101 unsigned char set, unsigned char clear )
102{
103 if (nb_debug_options == -1) debug_init();
104
105 if (nb_debug_options)
106 {
107 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
108 sizeof(debug_options[0]), cmp_name );
109 if (opt)
110 {
111 opt->flags = (opt->flags & ~clear) | set;
112 return 1;
113 }
114 }
115 return 0;
116}
117
118/* add a new debug option at the end of the option list */
119static void add_option( const char *name, unsigned char set, unsigned char clear )
120{
121 int min = 0, max = nb_debug_options - 1, pos, res;
122
123 if (!name[0]) /* "all" option */
124 {
125 default_flags = (default_flags & ~clear) | set;
126 return;
127 }
128 if (strlen(name) >= sizeof(debug_options[0].name)) return;
129
130 while (min <= max)
131 {
132 pos = (min + max) / 2;
133 res = strcmp( name, debug_options[pos].name );
134 if (!res)
135 {
136 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
137 return;
138 }
139 if (res < 0) max = pos - 1;
140 else min = pos + 1;
141 }
142 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
143
144 pos = min;
145 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
146 (nb_debug_options - pos) * sizeof(debug_options[0]) );
147 strcpy( debug_options[pos].name, name );
148 debug_options[pos].flags = (default_flags & ~clear) | set;
149 nb_debug_options++;
150}
151
152/* parse a set of debugging option specifications and add them to the option list */
153static void parse_options( const char *str )
154{
155 char *opt, *next, *options;
156 unsigned int i;
157
158 if (!(options = strdup(str))) return;
159 for (opt = options; opt; opt = next)
160 {
161 const char *p;
162 unsigned char set = 0, clear = 0;
163
164 if ((next = strchr( opt, ',' ))) *next++ = 0;
165
166 p = opt + strcspn( opt, "+-" );
167 if (!p[0]) p = opt; /* assume it's a debug channel name */
168
169 if (p > opt)
170 {
171 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
172 {
173 int len = strlen(debug_classes[i]);
174 if (len != (p - opt)) continue;
175 if (!memcmp( opt, debug_classes[i], len )) /* found it */
176 {
177 if (*p == '+') set |= 1 << i;
178 else clear |= 1 << i;
179 break;
180 }
181 }
182 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
183 continue;
184 }
185 else
186 {
187 if (*p == '-') clear = ~0;
188 else set = ~0;
189 }
190 if (*p == '+' || *p == '-') p++;
191 if (!p[0]) continue;
192
193 if (!strcmp( p, "all" ))
194 default_flags = (default_flags & ~clear) | set;
195 else
196 add_option( p, set, clear );
197 }
198 free( options );
199}
200
201
202/* print the usage message */
203static void debug_usage(void)
204{
205 static const char usage[] =
206 "Syntax of the WINEDEBUG variable:\n"
207 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
208 "Example: WINEDEBUG=+all,warn-heap\n"
209 " turns on all messages except warning heap messages\n"
210 "Available message classes: err, warn, fixme, trace\n";
211 write( 2, usage, sizeof(usage) - 1 );
212 exit(1);
213}
214
215#ifndef VBOX_WITH_WDDM
216static DECLCALLBACK(void) vbox_log_backdoor_rt(char* pcszStr)
217{
218 RTLogPrintf("%s", pcszStr);
219}
220#else
221static DECLCALLBACK(void) vbox_log_backdoor_dispmp(char* pcszStr)
222{
223 VBoxDispMpLoggerLog(pcszStr);
224}
225#endif
226static void vbox_log_v(const char *pszFormat, va_list args)
227{
228 if (vbox_log_backdoor)
229 {
230 static char buf[8092];
231 int offset = sprintf(buf, "[0x%lx.0x%lx] Wine Debug: ", (unsigned long)GetCurrentProcessId(), (unsigned long)GetCurrentThreadId());
232 vsprintf(buf + offset, pszFormat, args);
233 vbox_log_backdoor(buf);
234 }
235}
236
237/* initialize all options at startup */
238static void debug_init(void)
239{
240 char *wine_debug;
241
242 if (nb_debug_options != -1) return; /* already initialized */
243 nb_debug_options = 0;
244 if ((wine_debug = getenv("WINEDEBUG")))
245 {
246#ifndef VBOX_WITH_VMSVGA
247 Assert(0);
248#endif
249 if (!strcmp( wine_debug, "help" ))
250 debug_usage();
251 else if (getenv("WINEDEBUG_BACKDOOR"))
252 {
253#ifdef VBOX_WITH_WDDM
254 int rc = VBoxDispMpLoggerInit();
255 if (RT_SUCCESS(rc))
256 vbox_log_backdoor = vbox_log_backdoor_dispmp;
257// else
258#else
259 vbox_log_backdoor = vbox_log_backdoor_rt;
260#endif
261 }
262 parse_options( wine_debug );
263 }
264}
265
266/* varargs wrapper for funcs.dbg_vprintf */
267int wine_dbg_printf( const char *format, ... )
268{
269 int ret;
270 va_list valist;
271
272 va_start(valist, format);
273 ret = funcs.dbg_vprintf( format, valist );
274 va_end(valist);
275 return ret;
276}
277
278/* printf with temp buffer allocation */
279const char *wine_dbg_sprintf( const char *format, ... )
280{
281 static const int max_size = 200;
282 char *ret;
283 int len;
284 va_list valist;
285
286 va_start(valist, format);
287 ret = funcs.get_temp_buffer( max_size );
288 len = vsnprintf( ret, max_size, format, valist );
289 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
290 else funcs.release_temp_buffer( ret, len + 1 );
291 va_end(valist);
292 return ret;
293}
294
295
296/* varargs wrapper for funcs.dbg_vlog */
297int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
298 const char *func, const char *format, ... )
299{
300 int ret;
301 va_list valist;
302
303 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
304
305 va_start(valist, format);
306 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
307 va_end(valist);
308 return ret;
309}
310
311#if !defined(VBOX_WITH_VMSVGA) || defined(RT_OS_WINDOWS)
312int interlocked_xchg_add( int *dest, int incr )
313{
314 return InterlockedExchangeAdd(dest, incr);
315}
316#endif
317
318/* allocate some tmp string space */
319/* FIXME: this is not 100% thread-safe */
320static char *get_temp_buffer( size_t size )
321{
322 static char *list[32];
323 static int pos;
324 char *ret;
325 int idx;
326
327 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
328 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
329 return ret;
330}
331
332
333/* release unused part of the buffer */
334static void release_temp_buffer( char *buffer, size_t size )
335{
336 /* don't bother doing anything */
337}
338
339
340/* default implementation of wine_dbgstr_an */
341static const char *default_dbgstr_an( const char *str, int n )
342{
343 static const char hex[16] = "0123456789abcdef";
344 char *dst, *res;
345 size_t size;
346
347 if (!((ULONG_PTR)str >> 16))
348 {
349 if (!str) return "(null)";
350 res = funcs.get_temp_buffer( 6 );
351 sprintf( res, "#%04x", LOWORD(str) );
352 return res;
353 }
354 if (n == -1) n = strlen(str);
355 if (n < 0) n = 0;
356 size = 10 + min( 300, n * 4 );
357 dst = res = funcs.get_temp_buffer( size );
358 *dst++ = '"';
359 while (n-- > 0 && dst <= res + size - 9)
360 {
361 unsigned char c = *str++;
362 switch (c)
363 {
364 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
365 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
366 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
367 case '"': *dst++ = '\\'; *dst++ = '"'; break;
368 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
369 default:
370 if (c >= ' ' && c <= 126)
371 *dst++ = c;
372 else
373 {
374 *dst++ = '\\';
375 *dst++ = 'x';
376 *dst++ = hex[(c >> 4) & 0x0f];
377 *dst++ = hex[c & 0x0f];
378 }
379 }
380 }
381 *dst++ = '"';
382 if (n > 0)
383 {
384 *dst++ = '.';
385 *dst++ = '.';
386 *dst++ = '.';
387 }
388 *dst++ = 0;
389 funcs.release_temp_buffer( res, dst - res );
390 return res;
391}
392
393
394/* default implementation of wine_dbgstr_wn */
395static const char *default_dbgstr_wn( const WCHAR *str, int n )
396{
397 char *dst, *res;
398 size_t size;
399
400 if (!((ULONG_PTR)str >> 16))
401 {
402 if (!str) return "(null)";
403 res = funcs.get_temp_buffer( 6 );
404 sprintf( res, "#%04x", LOWORD(str) );
405 return res;
406 }
407 if (n == -1)
408 {
409 const WCHAR *end = str;
410 while (*end) end++;
411 n = end - str;
412 }
413 if (n < 0) n = 0;
414 size = 12 + min( 300, n * 5 );
415 dst = res = funcs.get_temp_buffer( size );
416 *dst++ = 'L';
417 *dst++ = '"';
418 while (n-- > 0 && dst <= res + size - 10)
419 {
420 WCHAR c = *str++;
421 switch (c)
422 {
423 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
424 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
425 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
426 case '"': *dst++ = '\\'; *dst++ = '"'; break;
427 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
428 default:
429 if (c >= ' ' && c <= 126)
430 *dst++ = c;
431 else
432 {
433 *dst++ = '\\';
434 sprintf(dst,"%04x",c);
435 dst+=4;
436 }
437 }
438 }
439 *dst++ = '"';
440 if (n > 0)
441 {
442 *dst++ = '.';
443 *dst++ = '.';
444 *dst++ = '.';
445 }
446 *dst++ = 0;
447 funcs.release_temp_buffer( res, dst - res );
448 return res;
449}
450
451
452/* default implementation of wine_dbg_vprintf */
453static int default_dbg_vprintf( const char *format, va_list args )
454{
455 vbox_log_v(format, args);
456#ifdef DEBUG_leo
457 static FILE *output=NULL;
458 static int first_time = 1;
459
460 if (first_time)
461 {
462 first_time = 0;
463 output = fopen( "winelog.txt", "w" );
464 }
465
466 if (output) vfprintf( output, format, args );
467#endif
468 return vfprintf( stdout, format, args );
469}
470
471
472/* default implementation of wine_dbg_vlog */
473static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
474 const char *func, const char *format, va_list args )
475{
476 int ret = 0;
477
478 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
479 ret += wine_dbg_printf( "%s:[%#x]:%s:%s ", debug_classes[cls], GetCurrentThreadId(), channel->name, func );
480 if (format)
481 ret += funcs.dbg_vprintf( format, args );
482 return ret;
483}
484
485/* wrappers to use the function pointers */
486
487const char *wine_dbgstr_an( const char * s, int n )
488{
489 return funcs.dbgstr_an(s, n);
490}
491
492const char *wine_dbgstr_wn( const WCHAR *s, int n )
493{
494 return funcs.dbgstr_wn(s, n);
495}
496
497void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
498 struct __wine_debug_functions *old_funcs, size_t size )
499{
500 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
501 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
502}
503
504static struct __wine_debug_functions funcs =
505{
506 get_temp_buffer,
507 release_temp_buffer,
508 default_dbgstr_an,
509 default_dbgstr_wn,
510 default_dbg_vprintf,
511 default_dbg_vlog
512};
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