adsldp: Fix memory leak on error path in search_ExecuteSearch (Coverity).
[wine.git] / libs / wine / debug.c
blob9cb918d272da82a5f6f97838a7bba5b4abd3028f
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/asm.h"
24 #ifdef __ASM_OBSOLETE
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <string.h>
30 #include <ctype.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
35 #include "wine/debug.h"
37 struct __wine_debug_functions
39 char * (*get_temp_buffer)( size_t n );
40 void (*release_temp_buffer)( char *buffer, size_t n );
41 const char * (*dbgstr_an)( const char * s, int n );
42 const char * (*dbgstr_wn)( const WCHAR *s, int n );
43 int (*dbg_vprintf)( const char *format, va_list args );
44 int (*dbg_vlog)( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
45 const char *function, const char *format, va_list args );
48 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
50 #define MAX_DEBUG_OPTIONS 256
52 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
53 static int nb_debug_options = -1;
54 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
56 static struct __wine_debug_functions funcs;
58 static void debug_init(void);
60 static int cmp_name( const void *p1, const void *p2 )
62 const char *name = p1;
63 const struct __wine_debug_channel *chan = p2;
64 return strcmp( name, chan->name );
67 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
68 unsigned char __wine_dbg_get_channel_flags_obsolete( struct __wine_debug_channel *channel )
70 if (nb_debug_options == -1) debug_init();
72 if (nb_debug_options)
74 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
75 sizeof(debug_options[0]), cmp_name );
76 if (opt) return opt->flags;
78 /* no option for this channel */
79 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
80 return default_flags;
83 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
84 int __wine_dbg_set_channel_flags_obsolete( struct __wine_debug_channel *channel,
85 unsigned char set, unsigned char clear )
87 if (nb_debug_options == -1) debug_init();
89 if (nb_debug_options)
91 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
92 sizeof(debug_options[0]), cmp_name );
93 if (opt)
95 opt->flags = (opt->flags & ~clear) | set;
96 return 1;
99 return 0;
102 /* add a new debug option at the end of the option list */
103 static void add_option( const char *name, unsigned char set, unsigned char clear )
105 int min = 0, max = nb_debug_options - 1, pos, res;
107 if (!name[0]) /* "all" option */
109 default_flags = (default_flags & ~clear) | set;
110 return;
112 if (strlen(name) >= sizeof(debug_options[0].name)) return;
114 while (min <= max)
116 pos = (min + max) / 2;
117 res = strcmp( name, debug_options[pos].name );
118 if (!res)
120 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
121 return;
123 if (res < 0) max = pos - 1;
124 else min = pos + 1;
126 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
128 pos = min;
129 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
130 (nb_debug_options - pos) * sizeof(debug_options[0]) );
131 strcpy( debug_options[pos].name, name );
132 debug_options[pos].flags = (default_flags & ~clear) | set;
133 nb_debug_options++;
136 /* parse a set of debugging option specifications and add them to the option list */
137 static void parse_options( const char *str )
139 char *opt, *next, *options;
140 unsigned int i;
142 if (!(options = strdup(str))) return;
143 for (opt = options; opt; opt = next)
145 const char *p;
146 unsigned char set = 0, clear = 0;
148 if ((next = strchr( opt, ',' ))) *next++ = 0;
150 p = opt + strcspn( opt, "+-" );
151 if (!p[0]) p = opt; /* assume it's a debug channel name */
153 if (p > opt)
155 for (i = 0; i < ARRAY_SIZE(debug_classes); i++)
157 int len = strlen(debug_classes[i]);
158 if (len != (p - opt)) continue;
159 if (!memcmp( opt, debug_classes[i], len )) /* found it */
161 if (*p == '+') set |= 1 << i;
162 else clear |= 1 << i;
163 break;
166 if (i == ARRAY_SIZE(debug_classes)) /* bad class name, skip it */
167 continue;
169 else
171 if (*p == '-') clear = ~0;
172 else set = ~0;
174 if (*p == '+' || *p == '-') p++;
175 if (!p[0]) continue;
177 if (!strcmp( p, "all" ))
178 default_flags = (default_flags & ~clear) | set;
179 else
180 add_option( p, set, clear );
182 free( options );
186 /* print the usage message */
187 static void debug_usage(void)
189 static const char usage[] =
190 "Syntax of the WINEDEBUG variable:\n"
191 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
192 "Example: WINEDEBUG=+all,warn-heap\n"
193 " turns on all messages except warning heap messages\n"
194 "Available message classes: err, warn, fixme, trace\n";
195 write( 2, usage, sizeof(usage) - 1 );
196 exit(1);
200 /* initialize all options at startup */
201 static void debug_init(void)
203 char *wine_debug;
204 struct stat st1, st2;
206 if (nb_debug_options != -1) return; /* already initialized */
207 nb_debug_options = 0;
209 /* check for stderr pointing to /dev/null */
210 if (!fstat( 2, &st1 ) && S_ISCHR(st1.st_mode) &&
211 !stat( "/dev/null", &st2 ) && S_ISCHR(st2.st_mode) &&
212 st1.st_rdev == st2.st_rdev)
214 default_flags = 0;
215 return;
217 if ((wine_debug = getenv("WINEDEBUG")))
219 if (!strcmp( wine_debug, "help" )) debug_usage();
220 parse_options( wine_debug );
224 /* varargs wrapper for funcs.dbg_vprintf */
225 int wine_dbg_printf_obsolete( const char *format, ... )
227 int ret;
228 va_list valist;
230 va_start(valist, format);
231 ret = funcs.dbg_vprintf( format, valist );
232 va_end(valist);
233 return ret;
236 /* printf with temp buffer allocation */
237 const char *wine_dbg_sprintf_obsolete( const char *format, ... )
239 static const int max_size = 200;
240 char *ret;
241 int len;
242 va_list valist;
244 va_start(valist, format);
245 ret = funcs.get_temp_buffer( max_size );
246 len = vsnprintf( ret, max_size, format, valist );
247 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
248 else funcs.release_temp_buffer( ret, len + 1 );
249 va_end(valist);
250 return ret;
254 /* varargs wrapper for funcs.dbg_vlog */
255 int wine_dbg_log_obsolete( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
256 const char *func, const char *format, ... )
258 int ret;
259 va_list valist;
261 if (!(__wine_dbg_get_channel_flags_obsolete( channel ) & (1 << cls))) return -1;
263 va_start(valist, format);
264 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
265 va_end(valist);
266 return ret;
270 /* allocate some tmp string space */
271 /* FIXME: this is not 100% thread-safe */
272 static char *get_temp_buffer( size_t size )
274 static char *list[32];
275 static int pos;
276 char *ret;
277 int idx;
279 idx = pos++ % ARRAY_SIZE(list);
280 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
281 return ret;
285 /* release unused part of the buffer */
286 static void release_temp_buffer( char *buffer, size_t size )
288 /* don't bother doing anything */
292 /* default implementation of wine_dbgstr_an */
293 static const char *default_dbgstr_an( const char *str, int n )
295 static const char hex[16] = "0123456789abcdef";
296 char *dst, *res;
297 size_t size;
299 if (!((ULONG_PTR)str >> 16))
301 if (!str) return "(null)";
302 res = funcs.get_temp_buffer( 6 );
303 sprintf( res, "#%04x", LOWORD(str) );
304 return res;
306 if (n == -1) n = strlen(str);
307 if (n < 0) n = 0;
308 size = 10 + min( 300, n * 4 );
309 dst = res = funcs.get_temp_buffer( size );
310 *dst++ = '"';
311 while (n-- > 0 && dst <= res + size - 9)
313 unsigned char c = *str++;
314 switch (c)
316 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
317 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
318 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
319 case '"': *dst++ = '\\'; *dst++ = '"'; break;
320 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
321 default:
322 if (c >= ' ' && c <= 126)
323 *dst++ = c;
324 else
326 *dst++ = '\\';
327 *dst++ = 'x';
328 *dst++ = hex[(c >> 4) & 0x0f];
329 *dst++ = hex[c & 0x0f];
333 *dst++ = '"';
334 if (n > 0)
336 *dst++ = '.';
337 *dst++ = '.';
338 *dst++ = '.';
340 *dst++ = 0;
341 funcs.release_temp_buffer( res, dst - res );
342 return res;
346 /* default implementation of wine_dbgstr_wn */
347 static const char *default_dbgstr_wn( const WCHAR *str, int n )
349 char *dst, *res;
350 size_t size;
352 if (!((ULONG_PTR)str >> 16))
354 if (!str) return "(null)";
355 res = funcs.get_temp_buffer( 6 );
356 sprintf( res, "#%04x", LOWORD(str) );
357 return res;
359 if (n == -1)
361 const WCHAR *end = str;
362 while (*end) end++;
363 n = end - str;
365 if (n < 0) n = 0;
366 size = 12 + min( 300, n * 5 );
367 dst = res = funcs.get_temp_buffer( size );
368 *dst++ = 'L';
369 *dst++ = '"';
370 while (n-- > 0 && dst <= res + size - 10)
372 WCHAR c = *str++;
373 switch (c)
375 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
376 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
377 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
378 case '"': *dst++ = '\\'; *dst++ = '"'; break;
379 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
380 default:
381 if (c >= ' ' && c <= 126)
382 *dst++ = c;
383 else
385 *dst++ = '\\';
386 sprintf(dst,"%04x",c);
387 dst+=4;
391 *dst++ = '"';
392 if (n > 0)
394 *dst++ = '.';
395 *dst++ = '.';
396 *dst++ = '.';
398 *dst++ = 0;
399 funcs.release_temp_buffer( res, dst - res );
400 return res;
404 /* default implementation of wine_dbg_vprintf */
405 static int default_dbg_vprintf( const char *format, va_list args )
407 return vfprintf( stderr, format, args );
411 /* default implementation of wine_dbg_vlog */
412 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
413 const char *func, const char *format, va_list args )
415 int ret = 0;
417 if (cls < ARRAY_SIZE(debug_classes))
418 ret += wine_dbg_printf_obsolete( "%s:%s:%s ", debug_classes[cls], channel->name, func );
419 if (format)
420 ret += funcs.dbg_vprintf( format, args );
421 return ret;
424 /* wrappers to use the function pointers */
426 const char *wine_dbgstr_an_obsolete( const char * s, int n )
428 return funcs.dbgstr_an(s, n);
431 const char *wine_dbgstr_wn_obsolete( const WCHAR *s, int n )
433 return funcs.dbgstr_wn(s, n);
436 void __wine_dbg_set_functions_obsolete( const struct __wine_debug_functions *new_funcs,
437 struct __wine_debug_functions *old_funcs, size_t size )
439 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
440 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
443 static struct __wine_debug_functions funcs =
445 get_temp_buffer,
446 release_temp_buffer,
447 default_dbgstr_an,
448 default_dbgstr_wn,
449 default_dbg_vprintf,
450 default_dbg_vlog
453 __ASM_OBSOLETE(__wine_dbg_get_channel_flags);
454 __ASM_OBSOLETE(__wine_dbg_set_channel_flags);
455 __ASM_OBSOLETE(__wine_dbg_set_functions);
456 __ASM_OBSOLETE(wine_dbg_log);
457 __ASM_OBSOLETE(wine_dbg_printf);
458 __ASM_OBSOLETE(wine_dbg_sprintf);
459 __ASM_OBSOLETE(wine_dbgstr_an);
460 __ASM_OBSOLETE(wine_dbgstr_wn);
462 #endif /* __ASM_OBSOLETE */