msi/tests: Add missing return value checks to package tests (Coverity).
[wine.git] / libs / wine / debug.c
blob4116b9614a821d828e2cf1e9e6e1540e528a0292
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"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
33 #include "wine/debug.h"
34 #include "wine/library.h"
36 #if defined(__MINGW32__) || defined(_MSC_VER)
37 WINE_DECLARE_DEBUG_CHANNEL(pid);
38 #endif
40 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
42 #define MAX_DEBUG_OPTIONS 256
44 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
45 static int nb_debug_options = -1;
46 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
48 static struct __wine_debug_functions funcs;
50 static void debug_init(void);
52 static int cmp_name( const void *p1, const void *p2 )
54 const char *name = p1;
55 const struct __wine_debug_channel *chan = p2;
56 return strcmp( name, chan->name );
59 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
60 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
62 if (nb_debug_options == -1) debug_init();
64 if (nb_debug_options)
66 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
67 sizeof(debug_options[0]), cmp_name );
68 if (opt) return opt->flags;
70 /* no option for this channel */
71 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
72 return default_flags;
75 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
76 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
77 unsigned char set, unsigned char clear )
79 if (nb_debug_options == -1) debug_init();
81 if (nb_debug_options)
83 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
84 sizeof(debug_options[0]), cmp_name );
85 if (opt)
87 opt->flags = (opt->flags & ~clear) | set;
88 return 1;
91 return 0;
94 /* add a new debug option at the end of the option list */
95 static void add_option( const char *name, unsigned char set, unsigned char clear )
97 int min = 0, max = nb_debug_options - 1, pos, res;
99 if (!name[0]) /* "all" option */
101 default_flags = (default_flags & ~clear) | set;
102 return;
104 if (strlen(name) >= sizeof(debug_options[0].name)) return;
106 while (min <= max)
108 pos = (min + max) / 2;
109 res = strcmp( name, debug_options[pos].name );
110 if (!res)
112 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
113 return;
115 if (res < 0) max = pos - 1;
116 else min = pos + 1;
118 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
120 pos = min;
121 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
122 (nb_debug_options - pos) * sizeof(debug_options[0]) );
123 strcpy( debug_options[pos].name, name );
124 debug_options[pos].flags = (default_flags & ~clear) | set;
125 nb_debug_options++;
128 /* parse a set of debugging option specifications and add them to the option list */
129 static void parse_options( const char *str )
131 char *opt, *next, *options;
132 unsigned int i;
134 if (!(options = strdup(str))) return;
135 for (opt = options; opt; opt = next)
137 const char *p;
138 unsigned char set = 0, clear = 0;
140 if ((next = strchr( opt, ',' ))) *next++ = 0;
142 p = opt + strcspn( opt, "+-" );
143 if (!p[0]) p = opt; /* assume it's a debug channel name */
145 if (p > opt)
147 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
149 int len = strlen(debug_classes[i]);
150 if (len != (p - opt)) continue;
151 if (!memcmp( opt, debug_classes[i], len )) /* found it */
153 if (*p == '+') set |= 1 << i;
154 else clear |= 1 << i;
155 break;
158 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
159 continue;
161 else
163 if (*p == '-') clear = ~0;
164 else set = ~0;
166 if (*p == '+' || *p == '-') p++;
167 if (!p[0]) continue;
169 if (!strcmp( p, "all" ))
170 default_flags = (default_flags & ~clear) | set;
171 else
172 add_option( p, set, clear );
174 free( options );
178 /* print the usage message */
179 static void debug_usage(void)
181 static const char usage[] =
182 "Syntax of the WINEDEBUG variable:\n"
183 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
184 "Example: WINEDEBUG=+all,warn-heap\n"
185 " turns on all messages except warning heap messages\n"
186 "Available message classes: err, warn, fixme, trace\n";
187 write( 2, usage, sizeof(usage) - 1 );
188 exit(1);
192 /* initialize all options at startup */
193 static void debug_init(void)
195 char *wine_debug;
196 struct stat st1, st2;
198 if (nb_debug_options != -1) return; /* already initialized */
199 nb_debug_options = 0;
201 /* check for stderr pointing to /dev/null */
202 if (!fstat( 2, &st1 ) && S_ISCHR(st1.st_mode) &&
203 !stat( "/dev/null", &st2 ) && S_ISCHR(st2.st_mode) &&
204 st1.st_rdev == st2.st_rdev)
206 default_flags = 0;
207 return;
209 if ((wine_debug = getenv("WINEDEBUG")))
211 if (!strcmp( wine_debug, "help" )) debug_usage();
212 parse_options( wine_debug );
216 /* varargs wrapper for funcs.dbg_vprintf */
217 int wine_dbg_printf( const char *format, ... )
219 int ret;
220 va_list valist;
222 va_start(valist, format);
223 ret = funcs.dbg_vprintf( format, valist );
224 va_end(valist);
225 return ret;
228 /* printf with temp buffer allocation */
229 const char *wine_dbg_sprintf( const char *format, ... )
231 static const int max_size = 200;
232 char *ret;
233 int len;
234 va_list valist;
236 va_start(valist, format);
237 ret = funcs.get_temp_buffer( max_size );
238 len = vsnprintf( ret, max_size, format, valist );
239 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
240 else funcs.release_temp_buffer( ret, len + 1 );
241 va_end(valist);
242 return ret;
246 /* varargs wrapper for funcs.dbg_vlog */
247 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
248 const char *func, const char *format, ... )
250 int ret;
251 va_list valist;
253 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
255 va_start(valist, format);
256 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
257 va_end(valist);
258 return ret;
262 /* allocate some tmp string space */
263 /* FIXME: this is not 100% thread-safe */
264 static char *get_temp_buffer( size_t size )
266 static char *list[32];
267 static int pos;
268 char *ret;
269 int idx;
271 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
272 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
273 return ret;
277 /* release unused part of the buffer */
278 static void release_temp_buffer( char *buffer, size_t size )
280 /* don't bother doing anything */
284 /* default implementation of wine_dbgstr_an */
285 static const char *default_dbgstr_an( const char *str, int n )
287 static const char hex[16] = "0123456789abcdef";
288 char *dst, *res;
289 size_t size;
291 if (!((ULONG_PTR)str >> 16))
293 if (!str) return "(null)";
294 res = funcs.get_temp_buffer( 6 );
295 sprintf( res, "#%04x", LOWORD(str) );
296 return res;
298 if (n == -1) n = strlen(str);
299 if (n < 0) n = 0;
300 size = 10 + min( 300, n * 4 );
301 dst = res = funcs.get_temp_buffer( size );
302 *dst++ = '"';
303 while (n-- > 0 && dst <= res + size - 9)
305 unsigned char c = *str++;
306 switch (c)
308 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
309 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
310 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
311 case '"': *dst++ = '\\'; *dst++ = '"'; break;
312 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
313 default:
314 if (c >= ' ' && c <= 126)
315 *dst++ = c;
316 else
318 *dst++ = '\\';
319 *dst++ = 'x';
320 *dst++ = hex[(c >> 4) & 0x0f];
321 *dst++ = hex[c & 0x0f];
325 *dst++ = '"';
326 if (n > 0)
328 *dst++ = '.';
329 *dst++ = '.';
330 *dst++ = '.';
332 *dst++ = 0;
333 funcs.release_temp_buffer( res, dst - res );
334 return res;
338 /* default implementation of wine_dbgstr_wn */
339 static const char *default_dbgstr_wn( const WCHAR *str, int n )
341 char *dst, *res;
342 size_t size;
344 if (!((ULONG_PTR)str >> 16))
346 if (!str) return "(null)";
347 res = funcs.get_temp_buffer( 6 );
348 sprintf( res, "#%04x", LOWORD(str) );
349 return res;
351 if (n == -1)
353 const WCHAR *end = str;
354 while (*end) end++;
355 n = end - str;
357 if (n < 0) n = 0;
358 size = 12 + min( 300, n * 5 );
359 dst = res = funcs.get_temp_buffer( size );
360 *dst++ = 'L';
361 *dst++ = '"';
362 while (n-- > 0 && dst <= res + size - 10)
364 WCHAR c = *str++;
365 switch (c)
367 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
368 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
369 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
370 case '"': *dst++ = '\\'; *dst++ = '"'; break;
371 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
372 default:
373 if (c >= ' ' && c <= 126)
374 *dst++ = c;
375 else
377 *dst++ = '\\';
378 sprintf(dst,"%04x",c);
379 dst+=4;
383 *dst++ = '"';
384 if (n > 0)
386 *dst++ = '.';
387 *dst++ = '.';
388 *dst++ = '.';
390 *dst++ = 0;
391 funcs.release_temp_buffer( res, dst - res );
392 return res;
396 /* default implementation of wine_dbg_vprintf */
397 static int default_dbg_vprintf( const char *format, va_list args )
399 return vfprintf( stderr, format, args );
403 /* default implementation of wine_dbg_vlog */
404 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
405 const char *func, const char *format, va_list args )
407 int ret = 0;
409 #if defined(__MINGW32__) || defined(_MSC_VER)
410 if (TRACE_ON(pid))
411 ret += wine_dbg_printf( "%04x:", GetCurrentProcessId() );
412 ret += wine_dbg_printf( "%04x:", GetCurrentThreadId() );
413 #endif
414 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
415 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
416 if (format)
417 ret += funcs.dbg_vprintf( format, args );
418 return ret;
421 /* wrappers to use the function pointers */
423 const char *wine_dbgstr_an( const char * s, int n )
425 return funcs.dbgstr_an(s, n);
428 const char *wine_dbgstr_wn( const WCHAR *s, int n )
430 return funcs.dbgstr_wn(s, n);
433 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
434 struct __wine_debug_functions *old_funcs, size_t size )
436 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
437 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
440 static struct __wine_debug_functions funcs =
442 get_temp_buffer,
443 release_temp_buffer,
444 default_dbgstr_an,
445 default_dbgstr_wn,
446 default_dbg_vprintf,
447 default_dbg_vlog