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 unsigned int nb_debug_options = 0;
39 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
41 static struct __wine_debug_functions funcs;
43 static int cmp_name( const void *p1, const void *p2 )
45 const char *name = p1;
46 const struct __wine_debug_channel *chan = p2;
47 return strcmp( name, chan->name );
50 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
51 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
55 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
56 sizeof(debug_options[0]), cmp_name );
57 if (opt) return opt->flags;
59 /* no option for this channel */
60 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
64 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
65 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
66 unsigned char set, unsigned char clear )
70 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
71 sizeof(debug_options[0]), cmp_name );
74 opt->flags = (opt->flags & ~clear) | set;
81 /* add a new debug option at the end of the option list */
82 static void add_option( const char *name, unsigned char set, unsigned char clear )
84 int min = 0, max = nb_debug_options - 1, pos, res;
86 if (!name[0]) /* "all" option */
88 default_flags = (default_flags & ~clear) | set;
91 if (strlen(name) >= sizeof(debug_options[0].name)) return;
95 pos = (min + max) / 2;
96 res = strcmp( name, debug_options[pos].name );
99 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
102 if (res < 0) max = pos - 1;
105 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
108 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
109 (nb_debug_options - pos) * sizeof(debug_options[0]) );
110 strcpy( debug_options[pos].name, name );
111 debug_options[pos].flags = (default_flags & ~clear) | set;
115 /* parse a set of debugging option specifications and add them to the option list */
116 static void parse_options( const char *str )
118 char *opt, *next, *options;
121 if (!(options = strdup(str))) return;
122 for (opt = options; opt; opt = next)
125 unsigned char set = 0, clear = 0;
127 if ((next = strchr( opt, ',' ))) *next++ = 0;
129 p = opt + strcspn( opt, "+-" );
130 if (!p[0]) p = opt; /* assume it's a debug channel name */
134 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
136 int len = strlen(debug_classes[i]);
137 if (len != (p - opt)) continue;
138 if (!memcmp( opt, debug_classes[i], len )) /* found it */
140 if (*p == '+') set |= 1 << i;
141 else clear |= 1 << i;
145 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
150 if (*p == '-') clear = ~0;
153 if (*p == '+' || *p == '-') p++;
156 if (!strcmp( p, "all" ))
157 default_flags = (default_flags & ~clear) | set;
159 add_option( p, set, clear );
165 /* print the usage message */
166 static void debug_usage(void)
168 static const char usage[] =
169 "Syntax of the WINEDEBUG variable:\n"
170 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
171 "Example: WINEDEBUG=+all,warn-heap\n"
172 " turns on all messages except warning heap messages\n"
173 "Available message classes: err, warn, fixme, trace\n";
174 write( 2, usage, sizeof(usage) - 1 );
179 /* initialize all options at startup */
180 void debug_init(void)
184 if ((wine_debug = getenv("WINEDEBUG")))
186 if (!strcmp( wine_debug, "help" )) debug_usage();
187 parse_options( wine_debug );
191 /* varargs wrapper for funcs.dbg_vprintf */
192 int wine_dbg_printf( const char *format, ... )
197 va_start(valist, format);
198 ret = funcs.dbg_vprintf( format, valist );
203 /* printf with temp buffer allocation */
204 const char *wine_dbg_sprintf( const char *format, ... )
206 static const int max_size = 200;
211 va_start(valist, format);
212 ret = funcs.get_temp_buffer( max_size );
213 len = vsnprintf( ret, max_size, format, valist );
214 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
215 else funcs.release_temp_buffer( ret, len + 1 );
221 /* varargs wrapper for funcs.dbg_vlog */
222 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
223 const char *func, const char *format, ... )
228 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
230 va_start(valist, format);
231 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
237 /* allocate some tmp string space */
238 /* FIXME: this is not 100% thread-safe */
239 static char *get_temp_buffer( size_t size )
241 static char *list[32];
246 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
247 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
252 /* release unused part of the buffer */
253 static void release_temp_buffer( char *buffer, size_t size )
255 /* don't bother doing anything */
259 /* default implementation of wine_dbgstr_an */
260 static const char *default_dbgstr_an( const char *str, int n )
262 static const char hex[16] = "0123456789abcdef";
266 if (!((ULONG_PTR)str >> 16))
268 if (!str) return "(null)";
269 res = funcs.get_temp_buffer( 6 );
270 sprintf( res, "#%04x", LOWORD(str) );
273 if (n == -1) n = strlen(str);
275 size = 10 + min( 300, n * 4 );
276 dst = res = funcs.get_temp_buffer( size );
278 while (n-- > 0 && dst <= res + size - 9)
280 unsigned char c = *str++;
283 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
284 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
285 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
286 case '"': *dst++ = '\\'; *dst++ = '"'; break;
287 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
289 if (c >= ' ' && c <= 126)
295 *dst++ = hex[(c >> 4) & 0x0f];
296 *dst++ = hex[c & 0x0f];
308 funcs.release_temp_buffer( res, dst - res );
313 /* default implementation of wine_dbgstr_wn */
314 static const char *default_dbgstr_wn( const WCHAR *str, int n )
319 if (!((ULONG_PTR)str >> 16))
321 if (!str) return "(null)";
322 res = funcs.get_temp_buffer( 6 );
323 sprintf( res, "#%04x", LOWORD(str) );
328 const WCHAR *end = str;
333 size = 12 + min( 300, n * 5 );
334 dst = res = funcs.get_temp_buffer( n * 5 + 7 );
337 while (n-- > 0 && dst <= res + size - 10)
342 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
343 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
344 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
345 case '"': *dst++ = '\\'; *dst++ = '"'; break;
346 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
348 if (c >= ' ' && c <= 126)
353 sprintf(dst,"%04x",c);
366 funcs.release_temp_buffer( res, dst - res );
371 /* default implementation of wine_dbg_vprintf */
372 static int default_dbg_vprintf( const char *format, va_list args )
374 return vfprintf( stderr, format, args );
378 /* default implementation of wine_dbg_vlog */
379 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
380 const char *func, const char *format, va_list args )
384 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
385 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
387 ret += funcs.dbg_vprintf( format, args );
391 /* wrappers to use the function pointers */
393 const char *wine_dbgstr_an( const char * s, int n )
395 return funcs.dbgstr_an(s, n);
398 const char *wine_dbgstr_wn( const WCHAR *s, int n )
400 return funcs.dbgstr_wn(s, n);
403 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
404 struct __wine_debug_functions *old_funcs, size_t size )
406 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
407 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
410 static struct __wine_debug_functions funcs =