msi: Add more tests for MsiOpenPackage.
[wine] / libs / wine / debug.c
1 /*
2  * Management of the debugging channels
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
29
30 #include "wine/debug.h"
31 #include "wine/library.h"
32
33 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
34
35 #define MAX_DEBUG_OPTIONS 256
36
37 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
38 static int nb_debug_options = -1;
39 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
40
41 static struct __wine_debug_functions funcs;
42
43 static void debug_init(void);
44
45 static int cmp_name( const void *p1, const void *p2 )
46 {
47     const char *name = p1;
48     const struct __wine_debug_channel *chan = p2;
49     return strcmp( name, chan->name );
50 }
51
52 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
53 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
54 {
55     if (nb_debug_options == -1) debug_init();
56
57     if (nb_debug_options)
58     {
59         struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
60                                                     sizeof(debug_options[0]), cmp_name );
61         if (opt) return opt->flags;
62     }
63     /* no option for this channel */
64     if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
65     return default_flags;
66 }
67
68 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
69 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
70                                   unsigned char set, unsigned char clear )
71 {
72     if (nb_debug_options == -1) debug_init();
73
74     if (nb_debug_options)
75     {
76         struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
77                                                     sizeof(debug_options[0]), cmp_name );
78         if (opt)
79         {
80             opt->flags = (opt->flags & ~clear) | set;
81             return 1;
82         }
83     }
84     return 0;
85 }
86
87 /* add a new debug option at the end of the option list */
88 static void add_option( const char *name, unsigned char set, unsigned char clear )
89 {
90     int min = 0, max = nb_debug_options - 1, pos, res;
91
92     if (!name[0])  /* "all" option */
93     {
94         default_flags = (default_flags & ~clear) | set;
95         return;
96     }
97     if (strlen(name) >= sizeof(debug_options[0].name)) return;
98
99     while (min <= max)
100     {
101         pos = (min + max) / 2;
102         res = strcmp( name, debug_options[pos].name );
103         if (!res)
104         {
105             debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
106             return;
107         }
108         if (res < 0) max = pos - 1;
109         else min = pos + 1;
110     }
111     if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
112
113     pos = min;
114     if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
115                                          (nb_debug_options - pos) * sizeof(debug_options[0]) );
116     strcpy( debug_options[pos].name, name );
117     debug_options[pos].flags = (default_flags & ~clear) | set;
118     nb_debug_options++;
119 }
120
121 /* parse a set of debugging option specifications and add them to the option list */
122 static void parse_options( const char *str )
123 {
124     char *opt, *next, *options;
125     unsigned int i;
126
127     if (!(options = strdup(str))) return;
128     for (opt = options; opt; opt = next)
129     {
130         const char *p;
131         unsigned char set = 0, clear = 0;
132
133         if ((next = strchr( opt, ',' ))) *next++ = 0;
134
135         p = opt + strcspn( opt, "+-" );
136         if (!p[0]) p = opt;  /* assume it's a debug channel name */
137
138         if (p > opt)
139         {
140             for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
141             {
142                 int len = strlen(debug_classes[i]);
143                 if (len != (p - opt)) continue;
144                 if (!memcmp( opt, debug_classes[i], len ))  /* found it */
145                 {
146                     if (*p == '+') set |= 1 << i;
147                     else clear |= 1 << i;
148                     break;
149                 }
150             }
151             if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
152                 continue;
153         }
154         else
155         {
156             if (*p == '-') clear = ~0;
157             else set = ~0;
158         }
159         if (*p == '+' || *p == '-') p++;
160         if (!p[0]) continue;
161
162         if (!strcmp( p, "all" ))
163             default_flags = (default_flags & ~clear) | set;
164         else
165             add_option( p, set, clear );
166     }
167     free( options );
168 }
169
170
171 /* print the usage message */
172 static void debug_usage(void)
173 {
174     static const char usage[] =
175         "Syntax of the WINEDEBUG variable:\n"
176         "  WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
177         "Example: WINEDEBUG=+all,warn-heap\n"
178         "    turns on all messages except warning heap messages\n"
179         "Available message classes: err, warn, fixme, trace\n";
180     write( 2, usage, sizeof(usage) - 1 );
181     exit(1);
182 }
183
184
185 /* initialize all options at startup */
186 static void debug_init(void)
187 {
188     char *wine_debug;
189
190     if (nb_debug_options != -1) return;  /* already initialized */
191     nb_debug_options = 0;
192     if ((wine_debug = getenv("WINEDEBUG")))
193     {
194         if (!strcmp( wine_debug, "help" )) debug_usage();
195         parse_options( wine_debug );
196     }
197 }
198
199 /* varargs wrapper for funcs.dbg_vprintf */
200 int wine_dbg_printf( const char *format, ... )
201 {
202     int ret;
203     va_list valist;
204
205     va_start(valist, format);
206     ret = funcs.dbg_vprintf( format, valist );
207     va_end(valist);
208     return ret;
209 }
210
211 /* printf with temp buffer allocation */
212 const char *wine_dbg_sprintf( const char *format, ... )
213 {
214     static const int max_size = 200;
215     char *ret;
216     int len;
217     va_list valist;
218
219     va_start(valist, format);
220     ret = funcs.get_temp_buffer( max_size );
221     len = vsnprintf( ret, max_size, format, valist );
222     if (len == -1 || len >= max_size) ret[max_size-1] = 0;
223     else funcs.release_temp_buffer( ret, len + 1 );
224     va_end(valist);
225     return ret;
226 }
227
228
229 /* varargs wrapper for funcs.dbg_vlog */
230 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
231                   const char *func, const char *format, ... )
232 {
233     int ret;
234     va_list valist;
235
236     if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
237
238     va_start(valist, format);
239     ret = funcs.dbg_vlog( cls, channel, func, format, valist );
240     va_end(valist);
241     return ret;
242 }
243
244
245 /* allocate some tmp string space */
246 /* FIXME: this is not 100% thread-safe */
247 static char *get_temp_buffer( size_t size )
248 {
249     static char *list[32];
250     static int pos;
251     char *ret;
252     int idx;
253
254     idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
255     if ((ret = realloc( list[idx], size ))) list[idx] = ret;
256     return ret;
257 }
258
259
260 /* release unused part of the buffer */
261 static void release_temp_buffer( char *buffer, size_t size )
262 {
263     /* don't bother doing anything */
264 }
265
266
267 /* default implementation of wine_dbgstr_an */
268 static const char *default_dbgstr_an( const char *str, int n )
269 {
270     static const char hex[16] = "0123456789abcdef";
271     char *dst, *res;
272     size_t size;
273
274     if (!((ULONG_PTR)str >> 16))
275     {
276         if (!str) return "(null)";
277         res = funcs.get_temp_buffer( 6 );
278         sprintf( res, "#%04x", LOWORD(str) );
279         return res;
280     }
281     if (n == -1) n = strlen(str);
282     if (n < 0) n = 0;
283     size = 10 + min( 300, n * 4 );
284     dst = res = funcs.get_temp_buffer( size );
285     *dst++ = '"';
286     while (n-- > 0 && dst <= res + size - 9)
287     {
288         unsigned char c = *str++;
289         switch (c)
290         {
291         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
292         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
293         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
294         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
295         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
296         default:
297             if (c >= ' ' && c <= 126)
298                 *dst++ = c;
299             else
300             {
301                 *dst++ = '\\';
302                 *dst++ = 'x';
303                 *dst++ = hex[(c >> 4) & 0x0f];
304                 *dst++ = hex[c & 0x0f];
305             }
306         }
307     }
308     *dst++ = '"';
309     if (n > 0)
310     {
311         *dst++ = '.';
312         *dst++ = '.';
313         *dst++ = '.';
314     }
315     *dst++ = 0;
316     funcs.release_temp_buffer( res, dst - res );
317     return res;
318 }
319
320
321 /* default implementation of wine_dbgstr_wn */
322 static const char *default_dbgstr_wn( const WCHAR *str, int n )
323 {
324     char *dst, *res;
325     size_t size;
326
327     if (!((ULONG_PTR)str >> 16))
328     {
329         if (!str) return "(null)";
330         res = funcs.get_temp_buffer( 6 );
331         sprintf( res, "#%04x", LOWORD(str) );
332         return res;
333     }
334     if (n == -1)
335     {
336         const WCHAR *end = str;
337         while (*end) end++;
338         n = end - str;
339     }
340     if (n < 0) n = 0;
341     size = 12 + min( 300, n * 5 );
342     dst = res = funcs.get_temp_buffer( n * 5 + 7 );
343     *dst++ = 'L';
344     *dst++ = '"';
345     while (n-- > 0 && dst <= res + size - 10)
346     {
347         WCHAR c = *str++;
348         switch (c)
349         {
350         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
351         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
352         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
353         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
354         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
355         default:
356             if (c >= ' ' && c <= 126)
357                 *dst++ = c;
358             else
359             {
360                 *dst++ = '\\';
361                 sprintf(dst,"%04x",c);
362                 dst+=4;
363             }
364         }
365     }
366     *dst++ = '"';
367     if (n > 0)
368     {
369         *dst++ = '.';
370         *dst++ = '.';
371         *dst++ = '.';
372     }
373     *dst++ = 0;
374     funcs.release_temp_buffer( res, dst - res );
375     return res;
376 }
377
378
379 /* default implementation of wine_dbg_vprintf */
380 static int default_dbg_vprintf( const char *format, va_list args )
381 {
382     return vfprintf( stderr, format, args );
383 }
384
385
386 /* default implementation of wine_dbg_vlog */
387 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
388                              const char *func, const char *format, va_list args )
389 {
390     int ret = 0;
391
392     if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
393         ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
394     if (format)
395         ret += funcs.dbg_vprintf( format, args );
396     return ret;
397 }
398
399 /* wrappers to use the function pointers */
400
401 const char *wine_dbgstr_an( const char * s, int n )
402 {
403     return funcs.dbgstr_an(s, n);
404 }
405
406 const char *wine_dbgstr_wn( const WCHAR *s, int n )
407 {
408     return funcs.dbgstr_wn(s, n);
409 }
410
411 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
412                                struct __wine_debug_functions *old_funcs, size_t size )
413 {
414     if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
415     if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
416 }
417
418 static struct __wine_debug_functions funcs =
419 {
420     get_temp_buffer,
421     release_temp_buffer,
422     default_dbgstr_an,
423     default_dbgstr_wn,
424     default_dbg_vprintf,
425     default_dbg_vlog
426 };