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 /* add a new debug option at the end of the option list */
66 static void add_option( const char *name, unsigned char set, unsigned char clear )
68 int min = 0, max = nb_debug_options - 1, pos, res;
70 if (!name[0]) /* "all" option */
72 default_flags = (default_flags & ~clear) | set;
75 if (strlen(name) >= sizeof(debug_options[0].name)) return;
79 pos = (min + max) / 2;
80 res = strcmp( name, debug_options[pos].name );
83 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
86 if (res < 0) max = pos - 1;
89 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
92 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
93 (nb_debug_options - pos) * sizeof(debug_options[0]) );
94 strcpy( debug_options[pos].name, name );
95 debug_options[pos].flags = (default_flags & ~clear) | set;
99 /* parse a set of debugging option specifications and add them to the option list */
100 static void parse_options( const char *str )
102 char *opt, *next, *options;
105 if (!(options = strdup(str))) return;
106 for (opt = options; opt; opt = next)
109 unsigned char set = 0, clear = 0;
111 if ((next = strchr( opt, ',' ))) *next++ = 0;
113 p = opt + strcspn( opt, "+-" );
114 if (!p[0]) p = opt; /* assume it's a debug channel name */
118 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
120 int len = strlen(debug_classes[i]);
121 if (len != (p - opt)) continue;
122 if (!memcmp( opt, debug_classes[i], len )) /* found it */
124 if (*p == '+') set |= 1 << i;
125 else clear |= 1 << i;
129 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
134 if (*p == '-') clear = ~0;
137 if (*p == '+' || *p == '-') p++;
140 if (!strcmp( p, "all" ))
141 default_flags = (default_flags & ~clear) | set;
143 add_option( p, set, clear );
149 /* print the usage message */
150 static void debug_usage(void)
152 static const char usage[] =
153 "Syntax of the WINEDEBUG variable:\n"
154 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
155 "Example: WINEDEBUG=+all,warn-heap\n"
156 " turns on all messages except warning heap messages\n"
157 "Available message classes: err, warn, fixme, trace\n";
158 write( 2, usage, sizeof(usage) - 1 );
163 /* initialize all options at startup */
164 void debug_init(void)
168 if ((wine_debug = getenv("WINEDEBUG")))
170 if (!strcmp( wine_debug, "help" )) debug_usage();
171 parse_options( wine_debug );
175 /* varargs wrapper for funcs.dbg_vprintf */
176 int wine_dbg_printf( const char *format, ... )
181 va_start(valist, format);
182 ret = funcs.dbg_vprintf( format, valist );
187 /* printf with temp buffer allocation */
188 const char *wine_dbg_sprintf( const char *format, ... )
190 static const int max_size = 200;
195 va_start(valist, format);
196 ret = funcs.get_temp_buffer( max_size );
197 len = vsnprintf( ret, max_size, format, valist );
198 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
199 else funcs.release_temp_buffer( ret, len + 1 );
205 /* varargs wrapper for funcs.dbg_vlog */
206 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
207 const char *func, const char *format, ... )
212 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
214 va_start(valist, format);
215 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
221 /* allocate some tmp string space */
222 /* FIXME: this is not 100% thread-safe */
223 static char *get_temp_buffer( size_t size )
225 static char *list[32];
230 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
231 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
236 /* release unused part of the buffer */
237 static void release_temp_buffer( char *buffer, size_t size )
239 /* don't bother doing anything */
243 /* default implementation of wine_dbgstr_an */
244 static const char *default_dbgstr_an( const char *str, int n )
246 static const char hex[16] = "0123456789abcdef";
250 if (!((ULONG_PTR)str >> 16))
252 if (!str) return "(null)";
253 res = funcs.get_temp_buffer( 6 );
254 sprintf( res, "#%04x", LOWORD(str) );
257 if (n == -1) n = strlen(str);
259 size = 10 + min( 300, n * 4 );
260 dst = res = funcs.get_temp_buffer( size );
262 while (n-- > 0 && dst <= res + size - 9)
264 unsigned char c = *str++;
267 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
268 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
269 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
270 case '"': *dst++ = '\\'; *dst++ = '"'; break;
271 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
273 if (c >= ' ' && c <= 126)
279 *dst++ = hex[(c >> 4) & 0x0f];
280 *dst++ = hex[c & 0x0f];
292 funcs.release_temp_buffer( res, dst - res );
297 /* default implementation of wine_dbgstr_wn */
298 static const char *default_dbgstr_wn( const WCHAR *str, int n )
303 if (!((ULONG_PTR)str >> 16))
305 if (!str) return "(null)";
306 res = funcs.get_temp_buffer( 6 );
307 sprintf( res, "#%04x", LOWORD(str) );
310 if (n == -1) n = strlenW(str);
312 size = 12 + min( 300, n * 5 );
313 dst = res = funcs.get_temp_buffer( n * 5 + 7 );
316 while (n-- > 0 && dst <= res + size - 10)
321 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
322 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
323 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
324 case '"': *dst++ = '\\'; *dst++ = '"'; break;
325 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
327 if (c >= ' ' && c <= 126)
332 sprintf(dst,"%04x",c);
345 funcs.release_temp_buffer( res, dst - res );
350 /* default implementation of wine_dbg_vprintf */
351 static int default_dbg_vprintf( const char *format, va_list args )
353 return vfprintf( stderr, format, args );
357 /* default implementation of wine_dbg_vlog */
358 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
359 const char *func, const char *format, va_list args )
363 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
364 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
366 ret += funcs.dbg_vprintf( format, args );
370 /* wrappers to use the function pointers */
372 const char *wine_dbgstr_an( const char * s, int n )
374 return funcs.dbgstr_an(s, n);
377 const char *wine_dbgstr_wn( const WCHAR *s, int n )
379 return funcs.dbgstr_wn(s, n);
382 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
383 struct __wine_debug_functions *old_funcs, size_t size )
385 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
386 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
389 static struct __wine_debug_functions funcs =