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