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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
30 #include "wine/debug.h"
31 #include "wine/library.h"
32 #include "wine/unicode.h"
34 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
36 #define MAX_DEBUG_OPTIONS 256
38 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
39 static unsigned int nb_debug_options = 0;
40 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
42 static struct __wine_debug_functions funcs;
44 static int cmp_name( const void *p1, const void *p2 )
46 const char *name = p1;
47 const struct __wine_debug_channel *chan = p2;
48 return strcmp( name, chan->name );
51 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
52 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
56 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
57 sizeof(debug_options[0]), cmp_name );
58 if (opt) return opt->flags;
60 /* no option for this channel */
61 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
65 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
66 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
67 unsigned char set, unsigned char clear )
71 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
72 sizeof(debug_options[0]), cmp_name );
75 opt->flags = (opt->flags & ~clear) | set;
82 /* add a new debug option at the end of the option list */
83 static void add_option( const char *name, unsigned char set, unsigned char clear )
85 int min = 0, max = nb_debug_options - 1, pos, res;
87 if (!name[0]) /* "all" option */
89 default_flags = (default_flags & ~clear) | set;
92 if (strlen(name) >= sizeof(debug_options[0].name)) return;
96 pos = (min + max) / 2;
97 res = strcmp( name, debug_options[pos].name );
100 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
103 if (res < 0) max = pos - 1;
106 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
109 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
110 (nb_debug_options - pos) * sizeof(debug_options[0]) );
111 strcpy( debug_options[pos].name, name );
112 debug_options[pos].flags = (default_flags & ~clear) | set;
116 /* parse a set of debugging option specifications and add them to the option list */
117 static void parse_options( const char *str )
119 char *opt, *next, *options;
122 if (!(options = strdup(str))) return;
123 for (opt = options; opt; opt = next)
126 unsigned char set = 0, clear = 0;
128 if ((next = strchr( opt, ',' ))) *next++ = 0;
130 p = opt + strcspn( opt, "+-" );
131 if (!p[0]) p = opt; /* assume it's a debug channel name */
135 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
137 int len = strlen(debug_classes[i]);
138 if (len != (p - opt)) continue;
139 if (!memcmp( opt, debug_classes[i], len )) /* found it */
141 if (*p == '+') set |= 1 << i;
142 else clear |= 1 << i;
146 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
151 if (*p == '-') clear = ~0;
154 if (*p == '+' || *p == '-') p++;
157 if (!strcmp( p, "all" ))
158 default_flags = (default_flags & ~clear) | set;
160 add_option( p, set, clear );
166 /* print the usage message */
167 static void debug_usage(void)
169 static const char usage[] =
170 "Syntax of the WINEDEBUG variable:\n"
171 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
172 "Example: WINEDEBUG=+all,warn-heap\n"
173 " turns on all messages except warning heap messages\n"
174 "Available message classes: err, warn, fixme, trace\n";
175 write( 2, usage, sizeof(usage) - 1 );
180 /* initialize all options at startup */
181 void debug_init(void)
185 if ((wine_debug = getenv("WINEDEBUG")))
187 if (!strcmp( wine_debug, "help" )) debug_usage();
188 parse_options( wine_debug );
192 /* varargs wrapper for funcs.dbg_vprintf */
193 int wine_dbg_printf( const char *format, ... )
198 va_start(valist, format);
199 ret = funcs.dbg_vprintf( format, valist );
204 /* printf with temp buffer allocation */
205 const char *wine_dbg_sprintf( const char *format, ... )
207 static const int max_size = 200;
212 va_start(valist, format);
213 ret = funcs.get_temp_buffer( max_size );
214 len = vsnprintf( ret, max_size, format, valist );
215 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
216 else funcs.release_temp_buffer( ret, len + 1 );
222 /* varargs wrapper for funcs.dbg_vlog */
223 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
224 const char *func, const char *format, ... )
229 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
231 va_start(valist, format);
232 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
238 /* allocate some tmp string space */
239 /* FIXME: this is not 100% thread-safe */
240 static char *get_temp_buffer( size_t size )
242 static char *list[32];
247 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
248 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
253 /* release unused part of the buffer */
254 static void release_temp_buffer( char *buffer, size_t size )
256 /* don't bother doing anything */
260 /* default implementation of wine_dbgstr_an */
261 static const char *default_dbgstr_an( const char *str, int n )
263 static const char hex[16] = "0123456789abcdef";
267 if (!((ULONG_PTR)str >> 16))
269 if (!str) return "(null)";
270 res = funcs.get_temp_buffer( 6 );
271 sprintf( res, "#%04x", LOWORD(str) );
274 if (n == -1) n = strlen(str);
276 size = 10 + min( 300, n * 4 );
277 dst = res = funcs.get_temp_buffer( size );
279 while (n-- > 0 && dst <= res + size - 9)
281 unsigned char c = *str++;
284 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
285 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
286 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
287 case '"': *dst++ = '\\'; *dst++ = '"'; break;
288 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
290 if (c >= ' ' && c <= 126)
296 *dst++ = hex[(c >> 4) & 0x0f];
297 *dst++ = hex[c & 0x0f];
309 funcs.release_temp_buffer( res, dst - res );
314 /* default implementation of wine_dbgstr_wn */
315 static const char *default_dbgstr_wn( const WCHAR *str, int n )
320 if (!((ULONG_PTR)str >> 16))
322 if (!str) return "(null)";
323 res = funcs.get_temp_buffer( 6 );
324 sprintf( res, "#%04x", LOWORD(str) );
327 if (n == -1) n = strlenW(str);
329 size = 12 + min( 300, n * 5 );
330 dst = res = funcs.get_temp_buffer( n * 5 + 7 );
333 while (n-- > 0 && dst <= res + size - 10)
338 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
339 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
340 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
341 case '"': *dst++ = '\\'; *dst++ = '"'; break;
342 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
344 if (c >= ' ' && c <= 126)
349 sprintf(dst,"%04x",c);
362 funcs.release_temp_buffer( res, dst - res );
367 /* default implementation of wine_dbg_vprintf */
368 static int default_dbg_vprintf( const char *format, va_list args )
370 return vfprintf( stderr, format, args );
374 /* default implementation of wine_dbg_vlog */
375 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
376 const char *func, const char *format, va_list args )
380 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
381 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
383 ret += funcs.dbg_vprintf( format, args );
387 /* wrappers to use the function pointers */
389 const char *wine_dbgstr_an( const char * s, int n )
391 return funcs.dbgstr_an(s, n);
394 const char *wine_dbgstr_wn( const WCHAR *s, int n )
396 return funcs.dbgstr_wn(s, n);
399 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
400 struct __wine_debug_functions *old_funcs, size_t size )
402 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
403 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
406 static struct __wine_debug_functions funcs =