VirtualBox

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

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

build fix

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