Register zones.
[wine.git] / libs / wine / debug.c
blob95c911045ad221f3fd5807e648d975989d35bf97
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
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 static inline unsigned char get_channel_flags( struct __wine_debug_channel *channel )
54 if (nb_debug_options)
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;
62 return default_flags;
65 /* register a new set of channels for a dll */
66 void *__wine_dbg_register( struct __wine_debug_channel * const *channels, int nb )
68 int i;
70 for (i = 0; i < nb; i++)
72 channels[i]->flags = ~0;
73 get_channel_flags( channels[i] );
75 return NULL;
79 /* unregister a set of channels; must pass the pointer obtained from wine_dbg_register */
80 void __wine_dbg_unregister( void *channel )
85 /* add a new debug option at the end of the option list */
86 static void add_option( const char *name, unsigned char set, unsigned char clear )
88 int min = 0, max = nb_debug_options - 1, pos, res;
90 if (!name[0]) /* "all" option */
92 default_flags = (default_flags & ~clear) | set;
93 return;
95 if (strlen(name) >= sizeof(debug_options[0].name)) return;
97 while (min <= max)
99 pos = (min + max) / 2;
100 res = strcmp( name, debug_options[pos].name );
101 if (!res)
103 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
104 return;
106 if (res < 0) max = pos - 1;
107 else min = pos + 1;
109 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
111 pos = min;
112 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
113 (nb_debug_options - pos) * sizeof(debug_options[0]) );
114 strcpy( debug_options[pos].name, name );
115 debug_options[pos].flags = (default_flags & ~clear) | set;
116 nb_debug_options++;
119 /* parse a set of debugging option specifications and add them to the option list */
120 static void parse_options( const char *str )
122 char *opt, *next, *options;
123 unsigned int i;
125 if (!(options = strdup(str))) return;
126 for (opt = options; opt; opt = next)
128 const char *p;
129 unsigned char set = 0, clear = 0;
131 if ((next = strchr( opt, ',' ))) *next++ = 0;
133 p = opt + strcspn( opt, "+-" );
134 if (!p[0]) p = opt; /* assume it's a debug channel name */
136 if (p > opt)
138 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
140 int len = strlen(debug_classes[i]);
141 if (len != (p - opt)) continue;
142 if (!memcmp( opt, debug_classes[i], len )) /* found it */
144 if (*p == '+') set |= 1 << i;
145 else clear |= 1 << i;
146 break;
149 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
150 continue;
152 else
154 if (*p == '-') clear = ~0;
155 else set = ~0;
157 if (*p == '+' || *p == '-') p++;
158 if (!p[0]) continue;
160 if (!strcmp( p, "all" ))
161 default_flags = (default_flags & ~clear) | set;
162 else
163 add_option( p, set, clear );
165 free( options );
169 /* print the usage message */
170 static void debug_usage(void)
172 static const char usage[] =
173 "Syntax of the WINEDEBUG variable:\n"
174 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
175 "Example: WINEDEBUG=+all,warn-heap\n"
176 " turns on all messages except warning heap messages\n"
177 "Available message classes: err, warn, fixme, trace\n";
178 write( 2, usage, sizeof(usage) - 1 );
179 exit(1);
183 /* initialize all options at startup */
184 void debug_init(void)
186 char *wine_debug;
188 if ((wine_debug = getenv("WINEDEBUG")))
190 if (!strcmp( wine_debug, "help" )) debug_usage();
191 parse_options( wine_debug );
195 /* varargs wrapper for funcs.dbg_vprintf */
196 int wine_dbg_printf( const char *format, ... )
198 int ret;
199 va_list valist;
201 va_start(valist, format);
202 ret = funcs.dbg_vprintf( format, valist );
203 va_end(valist);
204 return ret;
207 /* printf with temp buffer allocation */
208 const char *wine_dbg_sprintf( const char *format, ... )
210 static const int max_size = 200;
211 char *ret;
212 int len;
213 va_list valist;
215 va_start(valist, format);
216 ret = funcs.get_temp_buffer( max_size );
217 len = vsnprintf( ret, max_size, format, valist );
218 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
219 else funcs.release_temp_buffer( ret, len + 1 );
220 va_end(valist);
221 return ret;
225 /* varargs wrapper for funcs.dbg_vlog */
226 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
227 const char *func, const char *format, ... )
229 int ret;
230 va_list valist;
232 if (!(get_channel_flags( channel ) & (1 << cls))) return -1;
233 if (!format) return 0;
235 va_start(valist, format);
236 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
237 va_end(valist);
238 return ret;
242 /* allocate some tmp string space */
243 /* FIXME: this is not 100% thread-safe */
244 static char *get_temp_buffer( size_t size )
246 static char *list[32];
247 static int pos;
248 char *ret;
249 int idx;
251 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
252 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
253 return ret;
257 /* release unused part of the buffer */
258 static void release_temp_buffer( char *buffer, size_t size )
260 /* don't bother doing anything */
264 /* default implementation of wine_dbgstr_an */
265 static const char *default_dbgstr_an( const char *str, int n )
267 static const char hex[16] = "0123456789abcdef";
268 char *dst, *res;
269 size_t size;
271 if (!((ULONG_PTR)str >> 16))
273 if (!str) return "(null)";
274 res = funcs.get_temp_buffer( 6 );
275 sprintf( res, "#%04x", LOWORD(str) );
276 return res;
278 if (n == -1) n = strlen(str);
279 if (n < 0) n = 0;
280 size = 10 + min( 300, n * 4 );
281 dst = res = funcs.get_temp_buffer( size );
282 *dst++ = '"';
283 while (n-- > 0 && dst <= res + size - 9)
285 unsigned char c = *str++;
286 switch (c)
288 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
289 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
290 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
291 case '"': *dst++ = '\\'; *dst++ = '"'; break;
292 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
293 default:
294 if (c >= ' ' && c <= 126)
295 *dst++ = c;
296 else
298 *dst++ = '\\';
299 *dst++ = 'x';
300 *dst++ = hex[(c >> 4) & 0x0f];
301 *dst++ = hex[c & 0x0f];
305 *dst++ = '"';
306 if (*str)
308 *dst++ = '.';
309 *dst++ = '.';
310 *dst++ = '.';
312 *dst++ = 0;
313 funcs.release_temp_buffer( res, dst - res );
314 return res;
318 /* default implementation of wine_dbgstr_wn */
319 static const char *default_dbgstr_wn( const WCHAR *str, int n )
321 char *dst, *res;
322 size_t size;
324 if (!((ULONG_PTR)str >> 16))
326 if (!str) return "(null)";
327 res = funcs.get_temp_buffer( 6 );
328 sprintf( res, "#%04x", LOWORD(str) );
329 return res;
331 if (n == -1) n = strlenW(str);
332 if (n < 0) n = 0;
333 size = 12 + min( 300, n * 5 );
334 dst = res = funcs.get_temp_buffer( n * 5 + 7 );
335 *dst++ = 'L';
336 *dst++ = '"';
337 while (n-- > 0 && dst <= res + size - 10)
339 WCHAR c = *str++;
340 switch (c)
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;
347 default:
348 if (c >= ' ' && c <= 126)
349 *dst++ = c;
350 else
352 *dst++ = '\\';
353 sprintf(dst,"%04x",c);
354 dst+=4;
358 *dst++ = '"';
359 if (*str)
361 *dst++ = '.';
362 *dst++ = '.';
363 *dst++ = '.';
365 *dst++ = 0;
366 funcs.release_temp_buffer( res, dst - res );
367 return 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 )
382 int ret = 0;
384 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
385 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
386 if (format)
387 ret += funcs.dbg_vprintf( format, args );
388 return ret;
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 const char *wine_dbgstr_a( const char *s )
405 return funcs.dbgstr_an( s, -1 );
408 const char *wine_dbgstr_w( const WCHAR *s )
410 return funcs.dbgstr_wn( s, -1 );
413 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
414 struct __wine_debug_functions *old_funcs, size_t size )
416 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
417 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
420 static struct __wine_debug_functions funcs =
422 get_temp_buffer,
423 release_temp_buffer,
424 default_dbgstr_an,
425 default_dbgstr_wn,
426 default_dbg_vprintf,
427 default_dbg_vlog