VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/libWine/debug.c@ 30919

Last change on this file since 30919 was 16477, checked in by vboxsync, 16 years ago

LGPL disclaimer by filemuncher

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