VirtualBox

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

Last change on this file since 85121 was 85121, checked in by vboxsync, 5 years ago

iprt/cdefs.h: Refactored the typedef use of DECLCALLBACK as well as DECLCALLBACKMEMBER to wrap the whole expression, similar to the DECLR?CALLBACKMEMBER macros. This allows adding a throw() at the end when compiling with the VC++ compiler to indicate that the callbacks won't throw anything, so we can stop supressing the C5039 warning about passing functions that can potential throw C++ exceptions to extern C code that can't necessarily cope with such (unwind,++). Introduced a few _EX variations that allows specifying different/no calling convention too, as that's handy when dynamically resolving host APIs. Fixed numerous places missing DECLCALLBACK and such. Left two angry @todos regarding use of CreateThread. bugref:9794

  • 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# 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/errcore.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 DECLCALLBACKTYPE(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 int res = write( 2, usage, sizeof(usage) - 1 );
222 NOREF(res);
223 exit(1);
224}
225
226#ifndef VBOX_WITH_WDDM
227static DECLCALLBACK(void) vbox_log_backdoor_rt(char* pcszStr)
228{
229 RTLogPrintf("%s", pcszStr);
230}
231#else
232static DECLCALLBACK(void) vbox_log_backdoor_dispmp(char* pcszStr)
233{
234 VBoxDispMpLoggerLog(pcszStr);
235}
236#endif
237static void vbox_log_v(const char *pszFormat, va_list args)
238{
239 if (vbox_log_backdoor)
240 {
241 static char buf[8092];
242 int offset = sprintf(buf, "[0x%lx.0x%lx] Wine Debug: ", (unsigned long)GetCurrentProcessId(), (unsigned long)GetCurrentThreadId());
243 vsprintf(buf + offset, pszFormat, args);
244 vbox_log_backdoor(buf);
245 }
246}
247
248/* initialize all options at startup */
249static void debug_init(void)
250{
251 char *wine_debug;
252
253 if (nb_debug_options != -1) return; /* already initialized */
254 nb_debug_options = 0;
255 if ((wine_debug = getenv("WINEDEBUG")))
256 {
257#ifndef VBOX_WITH_VMSVGA
258 Assert(0);
259#endif
260 if (!strcmp( wine_debug, "help" ))
261 debug_usage();
262 else if (getenv("WINEDEBUG_BACKDOOR"))
263 {
264#ifdef VBOX_WITH_WDDM
265 int rc = VBoxDispMpLoggerInit();
266 if (RT_SUCCESS(rc))
267 vbox_log_backdoor = vbox_log_backdoor_dispmp;
268// else
269#else
270 vbox_log_backdoor = vbox_log_backdoor_rt;
271#endif
272 }
273 parse_options( wine_debug );
274 }
275}
276
277/* varargs wrapper for funcs.dbg_vprintf */
278int wine_dbg_printf( const char *format, ... )
279{
280 int ret;
281 va_list valist;
282
283 va_start(valist, format);
284 ret = funcs.dbg_vprintf( format, valist );
285 va_end(valist);
286 return ret;
287}
288
289/* printf with temp buffer allocation */
290const char *wine_dbg_sprintf( const char *format, ... )
291{
292 static const int max_size = 200;
293 char *ret;
294 int len;
295 va_list valist;
296
297 va_start(valist, format);
298 ret = funcs.get_temp_buffer( max_size );
299 len = vsnprintf( ret, max_size, format, valist );
300 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
301 else funcs.release_temp_buffer( ret, len + 1 );
302 va_end(valist);
303 return ret;
304}
305
306
307/* varargs wrapper for funcs.dbg_vlog */
308int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
309 const char *func, const char *format, ... )
310{
311 int ret;
312 va_list valist;
313
314 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
315
316 va_start(valist, format);
317 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
318 va_end(valist);
319 return ret;
320}
321
322#if !defined(VBOX_WITH_VMSVGA) || defined(RT_OS_WINDOWS)
323int interlocked_xchg_add( int *dest, int incr )
324{
325 return InterlockedExchangeAdd((LONG *)dest, incr);
326}
327#endif
328
329/* allocate some tmp string space */
330/* FIXME: this is not 100% thread-safe */
331static char *get_temp_buffer( size_t size )
332{
333 static char *list[32];
334 static int pos;
335 char *ret;
336 int idx;
337
338 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
339 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
340 return ret;
341}
342
343
344/* release unused part of the buffer */
345static void release_temp_buffer( char *buffer, size_t size )
346{
347 /* don't bother doing anything */
348}
349
350
351/* default implementation of wine_dbgstr_an */
352static const char *default_dbgstr_an( const char *str, int n )
353{
354 static const char hex[16+1] = "0123456789abcdef";
355 char *dst, *res;
356 size_t size;
357
358 if (!((ULONG_PTR)str >> 16))
359 {
360 if (!str) return "(null)";
361 res = funcs.get_temp_buffer( 6 );
362 sprintf( res, "#%04x", LOWORD(str) );
363 return res;
364 }
365 if (n == -1) n = (int)strlen(str);
366 if (n < 0) n = 0;
367 size = 10 + min( 300, n * 4 );
368 dst = res = funcs.get_temp_buffer( size );
369 *dst++ = '"';
370 while (n-- > 0 && dst <= res + size - 9)
371 {
372 unsigned char c = *str++;
373 switch (c)
374 {
375 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
376 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
377 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
378 case '"': *dst++ = '\\'; *dst++ = '"'; break;
379 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
380 default:
381 if (c >= ' ' && c <= 126)
382 *dst++ = c;
383 else
384 {
385 *dst++ = '\\';
386 *dst++ = 'x';
387 *dst++ = hex[(c >> 4) & 0x0f];
388 *dst++ = hex[c & 0x0f];
389 }
390 }
391 }
392 *dst++ = '"';
393 if (n > 0)
394 {
395 *dst++ = '.';
396 *dst++ = '.';
397 *dst++ = '.';
398 }
399 *dst++ = 0;
400 funcs.release_temp_buffer( res, dst - res );
401 return res;
402}
403
404
405/* default implementation of wine_dbgstr_wn */
406static const char *default_dbgstr_wn( const WCHAR *str, int n )
407{
408 char *dst, *res;
409 size_t size;
410
411 if (!((ULONG_PTR)str >> 16))
412 {
413 if (!str) return "(null)";
414 res = funcs.get_temp_buffer( 6 );
415 sprintf( res, "#%04x", LOWORD(str) );
416 return res;
417 }
418 if (n == -1)
419 {
420 const WCHAR *end = str;
421 while (*end) end++;
422 n = end - str;
423 }
424 if (n < 0) n = 0;
425 size = 12 + min( 300, n * 5 );
426 dst = res = funcs.get_temp_buffer( size );
427 *dst++ = 'L';
428 *dst++ = '"';
429 while (n-- > 0 && dst <= res + size - 10)
430 {
431 WCHAR c = *str++;
432 switch (c)
433 {
434 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
435 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
436 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
437 case '"': *dst++ = '\\'; *dst++ = '"'; break;
438 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
439 default:
440 if (c >= ' ' && c <= 126)
441 *dst++ = c;
442 else
443 {
444 *dst++ = '\\';
445 sprintf(dst,"%04x",c);
446 dst+=4;
447 }
448 }
449 }
450 *dst++ = '"';
451 if (n > 0)
452 {
453 *dst++ = '.';
454 *dst++ = '.';
455 *dst++ = '.';
456 }
457 *dst++ = 0;
458 funcs.release_temp_buffer( res, dst - res );
459 return res;
460}
461
462
463/* default implementation of wine_dbg_vprintf */
464static int default_dbg_vprintf( const char *format, va_list args )
465{
466 vbox_log_v(format, args);
467#ifdef DEBUG_leo
468 static FILE *output=NULL;
469 static int first_time = 1;
470
471 if (first_time)
472 {
473 first_time = 0;
474 output = fopen( "winelog.txt", "w" );
475 }
476
477 if (output) vfprintf( output, format, args );
478#endif
479 return vfprintf( stdout, format, args );
480}
481
482
483/* default implementation of wine_dbg_vlog */
484static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
485 const char *func, const char *format, va_list args )
486{
487 int ret = 0;
488
489 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
490 ret += wine_dbg_printf( "%s:[%#x]:%s:%s ", debug_classes[cls], GetCurrentThreadId(), channel->name, func );
491 if (format)
492 ret += funcs.dbg_vprintf( format, args );
493 return ret;
494}
495
496/* wrappers to use the function pointers */
497
498const char *wine_dbgstr_an( const char * s, int n )
499{
500 return funcs.dbgstr_an(s, n);
501}
502
503const char *wine_dbgstr_wn( const WCHAR *s, int n )
504{
505 return funcs.dbgstr_wn(s, n);
506}
507
508void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
509 struct __wine_debug_functions *old_funcs, size_t size )
510{
511 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
512 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
513}
514
515static struct __wine_debug_functions funcs =
516{
517 get_temp_buffer,
518 release_temp_buffer,
519 default_dbgstr_an,
520 default_dbgstr_wn,
521 default_dbg_vprintf,
522 default_dbg_vlog
523};
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