jscript: Fix GetIDsOfNames for more than one name.
[wine.git] / libs / wine / debug.c
blob95e6fe6e58c6cac4870c37710fbef8d54e499f09
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
23 #include "wine/asm.h"
25 #ifdef __ASM_OBSOLETE
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <ctype.h>
32 #ifdef HAVE_SYS_STAT_H
33 # include <sys/stat.h>
34 #endif
36 #include "wine/debug.h"
38 struct __wine_debug_functions
40 char * (*get_temp_buffer)( size_t n );
41 void (*release_temp_buffer)( char *buffer, size_t n );
42 const char * (*dbgstr_an)( const char * s, int n );
43 const char * (*dbgstr_wn)( const WCHAR *s, int n );
44 int (*dbg_vprintf)( const char *format, va_list args );
45 int (*dbg_vlog)( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
46 const char *function, const char *format, va_list args );
49 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
51 #define MAX_DEBUG_OPTIONS 256
53 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
54 static int nb_debug_options = -1;
55 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
57 static struct __wine_debug_functions funcs;
59 static void debug_init(void);
61 static int cmp_name( const void *p1, const void *p2 )
63 const char *name = p1;
64 const struct __wine_debug_channel *chan = p2;
65 return strcmp( name, chan->name );
68 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
69 unsigned char __wine_dbg_get_channel_flags_obsolete( struct __wine_debug_channel *channel )
71 if (nb_debug_options == -1) debug_init();
73 if (nb_debug_options)
75 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
76 sizeof(debug_options[0]), cmp_name );
77 if (opt) return opt->flags;
79 /* no option for this channel */
80 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
81 return default_flags;
84 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
85 int __wine_dbg_set_channel_flags_obsolete( struct __wine_debug_channel *channel,
86 unsigned char set, unsigned char clear )
88 if (nb_debug_options == -1) debug_init();
90 if (nb_debug_options)
92 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
93 sizeof(debug_options[0]), cmp_name );
94 if (opt)
96 opt->flags = (opt->flags & ~clear) | set;
97 return 1;
100 return 0;
103 /* add a new debug option at the end of the option list */
104 static void add_option( const char *name, unsigned char set, unsigned char clear )
106 int min = 0, max = nb_debug_options - 1, pos, res;
108 if (!name[0]) /* "all" option */
110 default_flags = (default_flags & ~clear) | set;
111 return;
113 if (strlen(name) >= sizeof(debug_options[0].name)) return;
115 while (min <= max)
117 pos = (min + max) / 2;
118 res = strcmp( name, debug_options[pos].name );
119 if (!res)
121 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
122 return;
124 if (res < 0) max = pos - 1;
125 else min = pos + 1;
127 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
129 pos = min;
130 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
131 (nb_debug_options - pos) * sizeof(debug_options[0]) );
132 strcpy( debug_options[pos].name, name );
133 debug_options[pos].flags = (default_flags & ~clear) | set;
134 nb_debug_options++;
137 /* parse a set of debugging option specifications and add them to the option list */
138 static void parse_options( const char *str )
140 char *opt, *next, *options;
141 unsigned int i;
143 if (!(options = strdup(str))) return;
144 for (opt = options; opt; opt = next)
146 const char *p;
147 unsigned char set = 0, clear = 0;
149 if ((next = strchr( opt, ',' ))) *next++ = 0;
151 p = opt + strcspn( opt, "+-" );
152 if (!p[0]) p = opt; /* assume it's a debug channel name */
154 if (p > opt)
156 for (i = 0; i < ARRAY_SIZE(debug_classes); i++)
158 int len = strlen(debug_classes[i]);
159 if (len != (p - opt)) continue;
160 if (!memcmp( opt, debug_classes[i], len )) /* found it */
162 if (*p == '+') set |= 1 << i;
163 else clear |= 1 << i;
164 break;
167 if (i == ARRAY_SIZE(debug_classes)) /* bad class name, skip it */
168 continue;
170 else
172 if (*p == '-') clear = ~0;
173 else set = ~0;
175 if (*p == '+' || *p == '-') p++;
176 if (!p[0]) continue;
178 if (!strcmp( p, "all" ))
179 default_flags = (default_flags & ~clear) | set;
180 else
181 add_option( p, set, clear );
183 free( options );
187 /* print the usage message */
188 static void debug_usage(void)
190 static const char usage[] =
191 "Syntax of the WINEDEBUG variable:\n"
192 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
193 "Example: WINEDEBUG=+all,warn-heap\n"
194 " turns on all messages except warning heap messages\n"
195 "Available message classes: err, warn, fixme, trace\n";
196 write( 2, usage, sizeof(usage) - 1 );
197 exit(1);
201 /* initialize all options at startup */
202 static void debug_init(void)
204 char *wine_debug;
205 struct stat st1, st2;
207 if (nb_debug_options != -1) return; /* already initialized */
208 nb_debug_options = 0;
210 /* check for stderr pointing to /dev/null */
211 if (!fstat( 2, &st1 ) && S_ISCHR(st1.st_mode) &&
212 !stat( "/dev/null", &st2 ) && S_ISCHR(st2.st_mode) &&
213 st1.st_rdev == st2.st_rdev)
215 default_flags = 0;
216 return;
218 if ((wine_debug = getenv("WINEDEBUG")))
220 if (!strcmp( wine_debug, "help" )) debug_usage();
221 parse_options( wine_debug );
225 /* varargs wrapper for funcs.dbg_vprintf */
226 int wine_dbg_printf_obsolete( const char *format, ... )
228 int ret;
229 va_list valist;
231 va_start(valist, format);
232 ret = funcs.dbg_vprintf( format, valist );
233 va_end(valist);
234 return ret;
237 /* printf with temp buffer allocation */
238 const char *wine_dbg_sprintf_obsolete( const char *format, ... )
240 static const int max_size = 200;
241 char *ret;
242 int len;
243 va_list valist;
245 va_start(valist, format);
246 ret = funcs.get_temp_buffer( max_size );
247 len = vsnprintf( ret, max_size, format, valist );
248 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
249 else funcs.release_temp_buffer( ret, len + 1 );
250 va_end(valist);
251 return ret;
255 /* varargs wrapper for funcs.dbg_vlog */
256 int wine_dbg_log_obsolete( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
257 const char *func, const char *format, ... )
259 int ret;
260 va_list valist;
262 if (!(__wine_dbg_get_channel_flags_obsolete( channel ) & (1 << cls))) return -1;
264 va_start(valist, format);
265 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
266 va_end(valist);
267 return ret;
271 /* allocate some tmp string space */
272 /* FIXME: this is not 100% thread-safe */
273 static char *get_temp_buffer( size_t size )
275 static char *list[32];
276 static int pos;
277 char *ret;
278 int idx;
280 idx = pos++ % ARRAY_SIZE(list);
281 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
282 return ret;
286 /* release unused part of the buffer */
287 static void release_temp_buffer( char *buffer, size_t size )
289 /* don't bother doing anything */
293 /* default implementation of wine_dbgstr_an */
294 static const char *default_dbgstr_an( const char *str, int n )
296 static const char hex[16] = "0123456789abcdef";
297 char *dst, *res;
298 size_t size;
300 if (!((ULONG_PTR)str >> 16))
302 if (!str) return "(null)";
303 res = funcs.get_temp_buffer( 6 );
304 sprintf( res, "#%04x", LOWORD(str) );
305 return res;
307 if (n == -1) n = strlen(str);
308 if (n < 0) n = 0;
309 size = 10 + min( 300, n * 4 );
310 dst = res = funcs.get_temp_buffer( size );
311 *dst++ = '"';
312 while (n-- > 0 && dst <= res + size - 9)
314 unsigned char c = *str++;
315 switch (c)
317 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
318 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
319 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
320 case '"': *dst++ = '\\'; *dst++ = '"'; break;
321 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
322 default:
323 if (c >= ' ' && c <= 126)
324 *dst++ = c;
325 else
327 *dst++ = '\\';
328 *dst++ = 'x';
329 *dst++ = hex[(c >> 4) & 0x0f];
330 *dst++ = hex[c & 0x0f];
334 *dst++ = '"';
335 if (n > 0)
337 *dst++ = '.';
338 *dst++ = '.';
339 *dst++ = '.';
341 *dst++ = 0;
342 funcs.release_temp_buffer( res, dst - res );
343 return res;
347 /* default implementation of wine_dbgstr_wn */
348 static const char *default_dbgstr_wn( const WCHAR *str, int n )
350 char *dst, *res;
351 size_t size;
353 if (!((ULONG_PTR)str >> 16))
355 if (!str) return "(null)";
356 res = funcs.get_temp_buffer( 6 );
357 sprintf( res, "#%04x", LOWORD(str) );
358 return res;
360 if (n == -1)
362 const WCHAR *end = str;
363 while (*end) end++;
364 n = end - str;
366 if (n < 0) n = 0;
367 size = 12 + min( 300, n * 5 );
368 dst = res = funcs.get_temp_buffer( size );
369 *dst++ = 'L';
370 *dst++ = '"';
371 while (n-- > 0 && dst <= res + size - 10)
373 WCHAR c = *str++;
374 switch (c)
376 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
377 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
378 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
379 case '"': *dst++ = '\\'; *dst++ = '"'; break;
380 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
381 default:
382 if (c >= ' ' && c <= 126)
383 *dst++ = c;
384 else
386 *dst++ = '\\';
387 sprintf(dst,"%04x",c);
388 dst+=4;
392 *dst++ = '"';
393 if (n > 0)
395 *dst++ = '.';
396 *dst++ = '.';
397 *dst++ = '.';
399 *dst++ = 0;
400 funcs.release_temp_buffer( res, dst - res );
401 return res;
405 /* default implementation of wine_dbg_vprintf */
406 static int default_dbg_vprintf( const char *format, va_list args )
408 return vfprintf( stderr, format, args );
412 /* default implementation of wine_dbg_vlog */
413 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
414 const char *func, const char *format, va_list args )
416 int ret = 0;
418 if (cls < ARRAY_SIZE(debug_classes))
419 ret += wine_dbg_printf_obsolete( "%s:%s:%s ", debug_classes[cls], channel->name, func );
420 if (format)
421 ret += funcs.dbg_vprintf( format, args );
422 return ret;
425 /* wrappers to use the function pointers */
427 const char *wine_dbgstr_an_obsolete( const char * s, int n )
429 return funcs.dbgstr_an(s, n);
432 const char *wine_dbgstr_wn_obsolete( const WCHAR *s, int n )
434 return funcs.dbgstr_wn(s, n);
437 void __wine_dbg_set_functions_obsolete( const struct __wine_debug_functions *new_funcs,
438 struct __wine_debug_functions *old_funcs, size_t size )
440 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
441 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
444 static struct __wine_debug_functions funcs =
446 get_temp_buffer,
447 release_temp_buffer,
448 default_dbgstr_an,
449 default_dbgstr_wn,
450 default_dbg_vprintf,
451 default_dbg_vlog
454 __ASM_OBSOLETE(__wine_dbg_get_channel_flags);
455 __ASM_OBSOLETE(__wine_dbg_set_channel_flags);
456 __ASM_OBSOLETE(__wine_dbg_set_functions);
457 __ASM_OBSOLETE(wine_dbg_log);
458 __ASM_OBSOLETE(wine_dbg_printf);
459 __ASM_OBSOLETE(wine_dbg_sprintf);
460 __ASM_OBSOLETE(wine_dbgstr_an);
461 __ASM_OBSOLETE(wine_dbgstr_wn);
463 #endif /* __ASM_OBSOLETE */