VirtualBox

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

最後變更 在這個檔案從100776是 93498,由 vboxsync 提交於 3 年 前

Devices/Graphics/libWineStub/debug.c: Tweaked stupid guest-side debug code probably not used on the host side so it builds for arm64. bugref:9898

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

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