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
22 #include "wine/port.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();
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;
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();
76 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
77 sizeof(debug_options[0]), cmp_name );
80 opt->flags = (opt->flags & ~clear) | set;
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;
97 if (strlen(name) >= sizeof(debug_options[0].name)) return;
101 pos = (min + max) / 2;
102 res = strcmp( name, debug_options[pos].name );
105 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
108 if (res < 0) max = pos - 1;
111 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
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;
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;
127 if (!(options = strdup(str))) return;
128 for (opt = options; opt; opt = next)
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 */
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;
151 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
156 if (*p == '-') clear = ~0;
159 if (*p == '+' || *p == '-') p++;
162 if (!strcmp( p, "all" ))
163 default_flags = (default_flags & ~clear) | set;
165 add_option( p, set, clear );
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 );
185 /* initialize all options at startup */
186 static void debug_init(void)
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, ... )
205 va_start(valist, format);
206 ret = funcs.dbg_vprintf( format, valist );
211 /* printf with temp buffer allocation */
212 const char *wine_dbg_sprintf( const char *format, ... )
214 static const int max_size = 200;
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 );
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, ... )
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 );
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];
254 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
255 if ((ret = realloc( list[idx], size ))) list[idx] = 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";
274 if (!((ULONG_PTR)str >> 16))
276 if (!str) return "(null)";
277 res = funcs.get_temp_buffer( 6 );
278 sprintf( res, "#%04x", LOWORD(str) );
281 if (n == -1) n = strlen(str);
283 size = 10 + min( 300, n * 4 );
284 dst = res = funcs.get_temp_buffer( size );
286 while (n-- > 0 && dst <= res + size - 9)
288 unsigned char c = *str++;
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;
297 if (c >= ' ' && c <= 126)
303 *dst++ = hex[(c >> 4) & 0x0f];
304 *dst++ = hex[c & 0x0f];
316 funcs.release_temp_buffer( res, dst - res );
321 /* default implementation of wine_dbgstr_wn */
322 static const char *default_dbgstr_wn( const WCHAR *str, int n )
327 if (!((ULONG_PTR)str >> 16))
329 if (!str) return "(null)";
330 res = funcs.get_temp_buffer( 6 );
331 sprintf( res, "#%04x", LOWORD(str) );
336 const WCHAR *end = str;
341 size = 12 + min( 300, n * 5 );
342 dst = res = funcs.get_temp_buffer( size );
345 while (n-- > 0 && dst <= res + size - 10)
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;
356 if (c >= ' ' && c <= 126)
361 sprintf(dst,"%04x",c);
374 funcs.release_temp_buffer( res, dst - 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 )
392 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
393 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
395 ret += funcs.dbg_vprintf( format, args );
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 =