ntdll: Add a few more tests for RtlIsDosDeviceName_U, fix some failures on Windows.
[wine/multimedia.git] / libs / wine / debug.c
blob12dbeb81ec1bad74f6e49c18c17c599e142a2253
1 /*
2 * Management of the debugging channels
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
30 #include "wine/debug.h"
31 #include "wine/library.h"
33 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
35 #define MAX_DEBUG_OPTIONS 256
37 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
38 static int nb_debug_options = -1;
39 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
41 static struct __wine_debug_functions funcs;
43 static void debug_init(void);
45 static int cmp_name( const void *p1, const void *p2 )
47 const char *name = p1;
48 const struct __wine_debug_channel *chan = p2;
49 return strcmp( name, chan->name );
52 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
53 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
55 if (nb_debug_options == -1) debug_init();
57 if (nb_debug_options)
59 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
60 sizeof(debug_options[0]), cmp_name );
61 if (opt) return opt->flags;
63 /* no option for this channel */
64 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
65 return default_flags;
68 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
69 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
70 unsigned char set, unsigned char clear )
72 if (nb_debug_options == -1) debug_init();
74 if (nb_debug_options)
76 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
77 sizeof(debug_options[0]), cmp_name );
78 if (opt)
80 opt->flags = (opt->flags & ~clear) | set;
81 return 1;
84 return 0;
87 /* add a new debug option at the end of the option list */
88 static void add_option( const char *name, unsigned char set, unsigned char clear )
90 int min = 0, max = nb_debug_options - 1, pos, res;
92 if (!name[0]) /* "all" option */
94 default_flags = (default_flags & ~clear) | set;
95 return;
97 if (strlen(name) >= sizeof(debug_options[0].name)) return;
99 while (min <= max)
101 pos = (min + max) / 2;
102 res = strcmp( name, debug_options[pos].name );
103 if (!res)
105 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
106 return;
108 if (res < 0) max = pos - 1;
109 else min = pos + 1;
111 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
113 pos = min;
114 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
115 (nb_debug_options - pos) * sizeof(debug_options[0]) );
116 strcpy( debug_options[pos].name, name );
117 debug_options[pos].flags = (default_flags & ~clear) | set;
118 nb_debug_options++;
121 /* parse a set of debugging option specifications and add them to the option list */
122 static void parse_options( const char *str )
124 char *opt, *next, *options;
125 unsigned int i;
127 if (!(options = strdup(str))) return;
128 for (opt = options; opt; opt = next)
130 const char *p;
131 unsigned char set = 0, clear = 0;
133 if ((next = strchr( opt, ',' ))) *next++ = 0;
135 p = opt + strcspn( opt, "+-" );
136 if (!p[0]) p = opt; /* assume it's a debug channel name */
138 if (p > opt)
140 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
142 int len = strlen(debug_classes[i]);
143 if (len != (p - opt)) continue;
144 if (!memcmp( opt, debug_classes[i], len )) /* found it */
146 if (*p == '+') set |= 1 << i;
147 else clear |= 1 << i;
148 break;
151 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
152 continue;
154 else
156 if (*p == '-') clear = ~0;
157 else set = ~0;
159 if (*p == '+' || *p == '-') p++;
160 if (!p[0]) continue;
162 if (!strcmp( p, "all" ))
163 default_flags = (default_flags & ~clear) | set;
164 else
165 add_option( p, set, clear );
167 free( options );
171 /* print the usage message */
172 static void debug_usage(void)
174 static const char usage[] =
175 "Syntax of the WINEDEBUG variable:\n"
176 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
177 "Example: WINEDEBUG=+all,warn-heap\n"
178 " turns on all messages except warning heap messages\n"
179 "Available message classes: err, warn, fixme, trace\n";
180 write( 2, usage, sizeof(usage) - 1 );
181 exit(1);
185 /* initialize all options at startup */
186 static void debug_init(void)
188 char *wine_debug;
190 if (nb_debug_options != -1) return; /* already initialized */
191 nb_debug_options = 0;
192 if ((wine_debug = getenv("WINEDEBUG")))
194 if (!strcmp( wine_debug, "help" )) debug_usage();
195 parse_options( wine_debug );
199 /* varargs wrapper for funcs.dbg_vprintf */
200 int wine_dbg_printf( const char *format, ... )
202 int ret;
203 va_list valist;
205 va_start(valist, format);
206 ret = funcs.dbg_vprintf( format, valist );
207 va_end(valist);
208 return ret;
211 /* printf with temp buffer allocation */
212 const char *wine_dbg_sprintf( const char *format, ... )
214 static const int max_size = 200;
215 char *ret;
216 int len;
217 va_list valist;
219 va_start(valist, format);
220 ret = funcs.get_temp_buffer( max_size );
221 len = vsnprintf( ret, max_size, format, valist );
222 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
223 else funcs.release_temp_buffer( ret, len + 1 );
224 va_end(valist);
225 return ret;
229 /* varargs wrapper for funcs.dbg_vlog */
230 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
231 const char *func, const char *format, ... )
233 int ret;
234 va_list valist;
236 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
238 va_start(valist, format);
239 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
240 va_end(valist);
241 return ret;
245 /* allocate some tmp string space */
246 /* FIXME: this is not 100% thread-safe */
247 static char *get_temp_buffer( size_t size )
249 static char *list[32];
250 static int pos;
251 char *ret;
252 int idx;
254 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
255 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
256 return ret;
260 /* release unused part of the buffer */
261 static void release_temp_buffer( char *buffer, size_t size )
263 /* don't bother doing anything */
267 /* default implementation of wine_dbgstr_an */
268 static const char *default_dbgstr_an( const char *str, int n )
270 static const char hex[16] = "0123456789abcdef";
271 char *dst, *res;
272 size_t size;
274 if (!((ULONG_PTR)str >> 16))
276 if (!str) return "(null)";
277 res = funcs.get_temp_buffer( 6 );
278 sprintf( res, "#%04x", LOWORD(str) );
279 return res;
281 if (n == -1) n = strlen(str);
282 if (n < 0) n = 0;
283 size = 10 + min( 300, n * 4 );
284 dst = res = funcs.get_temp_buffer( size );
285 *dst++ = '"';
286 while (n-- > 0 && dst <= res + size - 9)
288 unsigned char c = *str++;
289 switch (c)
291 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
292 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
293 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
294 case '"': *dst++ = '\\'; *dst++ = '"'; break;
295 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
296 default:
297 if (c >= ' ' && c <= 126)
298 *dst++ = c;
299 else
301 *dst++ = '\\';
302 *dst++ = 'x';
303 *dst++ = hex[(c >> 4) & 0x0f];
304 *dst++ = hex[c & 0x0f];
308 *dst++ = '"';
309 if (n > 0)
311 *dst++ = '.';
312 *dst++ = '.';
313 *dst++ = '.';
315 *dst++ = 0;
316 funcs.release_temp_buffer( res, dst - res );
317 return res;
321 /* default implementation of wine_dbgstr_wn */
322 static const char *default_dbgstr_wn( const WCHAR *str, int n )
324 char *dst, *res;
325 size_t size;
327 if (!((ULONG_PTR)str >> 16))
329 if (!str) return "(null)";
330 res = funcs.get_temp_buffer( 6 );
331 sprintf( res, "#%04x", LOWORD(str) );
332 return res;
334 if (n == -1)
336 const WCHAR *end = str;
337 while (*end) end++;
338 n = end - str;
340 if (n < 0) n = 0;
341 size = 12 + min( 300, n * 5 );
342 dst = res = funcs.get_temp_buffer( size );
343 *dst++ = 'L';
344 *dst++ = '"';
345 while (n-- > 0 && dst <= res + size - 10)
347 WCHAR c = *str++;
348 switch (c)
350 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
351 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
352 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
353 case '"': *dst++ = '\\'; *dst++ = '"'; break;
354 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
355 default:
356 if (c >= ' ' && c <= 126)
357 *dst++ = c;
358 else
360 *dst++ = '\\';
361 sprintf(dst,"%04x",c);
362 dst+=4;
366 *dst++ = '"';
367 if (n > 0)
369 *dst++ = '.';
370 *dst++ = '.';
371 *dst++ = '.';
373 *dst++ = 0;
374 funcs.release_temp_buffer( res, dst - res );
375 return res;
379 /* default implementation of wine_dbg_vprintf */
380 static int default_dbg_vprintf( const char *format, va_list args )
382 return vfprintf( stderr, format, args );
386 /* default implementation of wine_dbg_vlog */
387 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
388 const char *func, const char *format, va_list args )
390 int ret = 0;
392 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
393 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
394 if (format)
395 ret += funcs.dbg_vprintf( format, args );
396 return ret;
399 /* wrappers to use the function pointers */
401 const char *wine_dbgstr_an( const char * s, int n )
403 return funcs.dbgstr_an(s, n);
406 const char *wine_dbgstr_wn( const WCHAR *s, int n )
408 return funcs.dbgstr_wn(s, n);
411 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
412 struct __wine_debug_functions *old_funcs, size_t size )
414 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
415 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
418 static struct __wine_debug_functions funcs =
420 get_temp_buffer,
421 release_temp_buffer,
422 default_dbgstr_an,
423 default_dbgstr_wn,
424 default_dbg_vprintf,
425 default_dbg_vlog