VirtualBox

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

Last change on this file since 93468 was 92903, checked in by vboxsync, 3 years ago

libWineStub: Don't include wrapper headers on non-windows platforms. bugref:10116

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