VirtualBox

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

Last change on this file since 62948 was 62948, checked in by vboxsync, 9 years ago

VBoxSVGA3D: warnings

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

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