update for HEAD-2003091401
[reactos.git] / lib / winedbgc / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 /*
21 #include <ntddk.h>
22 #include <wine/debugtools.h>
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <ctype.h>
28  */
29 #include <windows.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include "porting.h"
33 /*
34 #include "config.h"
35 #include "wine/port.h"
36
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <stdarg.h>
40 #include <string.h>
41 #include <ctype.h>
42
43 #include "wine/debug.h"
44 #include "wine/library.h"
45 #include "wine/unicode.h"
46  */
47 struct dll
48 {
49     struct dll   *next;        /* linked list of dlls */
50     struct dll   *prev;
51     char * const *channels;    /* array of channels */
52     int           nb_channels; /* number of channels in array */
53 };
54
55 static struct dll *first_dll;
56
57 struct debug_option
58 {
59     struct debug_option *next;       /* next option in list */
60     unsigned char        set;        /* bits to set */
61     unsigned char        clear;      /* bits to clear */
62     char                 name[14];   /* channel name, or empty for "all" */
63 };
64
65 static struct debug_option *first_option;
66 static struct debug_option *last_option;
67
68 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
69
70 static int cmp_name( const void *p1, const void *p2 )
71 {
72     const char *name = p1;
73     const char * const *chan = p2;
74     return strcmp( name, *chan + 1 );
75 }
76
77 /* apply a debug option to the channels of a given dll */
78 static void apply_option( struct dll *dll, const struct debug_option *opt )
79 {
80     if (opt->name[0])
81     {
82         char **dbch = bsearch( opt->name, dll->channels, dll->nb_channels,
83                                sizeof(*dll->channels), cmp_name );
84         if (dbch) **dbch = (**dbch & ~opt->clear) | opt->set;
85     }
86     else /* all */
87     {
88         int i;
89         for (i = 0; i < dll->nb_channels; i++)
90             dll->channels[i][0] = (dll->channels[i][0] & ~opt->clear) | opt->set;
91     }
92 }
93
94 /* register a new set of channels for a dll */
95 void *__wine_dbg_register( char * const *channels, int nb )
96 {
97     struct debug_option *opt = first_option;
98     struct dll *dll = malloc( sizeof(*dll) );
99     if (dll)
100     {
101         dll->channels = channels;
102         dll->nb_channels = nb;
103         dll->prev = NULL;
104         if ((dll->next = first_dll)) dll->next->prev = dll;
105         first_dll = dll;
106
107         /* apply existing options to this dll */
108         while (opt)
109         {
110             apply_option( dll, opt );
111             opt = opt->next;
112         }
113     }
114     return dll;
115 }
116
117
118 /* unregister a set of channels; must pass the pointer obtained from wine_dbg_register */
119 void __wine_dbg_unregister( void *channel )
120 {
121     struct dll *dll = channel;
122     if (dll)
123     {
124         if (dll->next) dll->next->prev = dll->prev;
125         if (dll->prev) dll->prev->next = dll->next;
126         else first_dll = dll->next;
127         free( dll );
128     }
129 }
130
131
132 /* add a new debug option at the end of the option list */
133 void wine_dbg_add_option( const char *name, unsigned char set, unsigned char clear )
134 {
135     struct dll *dll = first_dll;
136     struct debug_option *opt;
137
138     if (!(opt = malloc( sizeof(*opt) ))) return;
139     opt->next  = NULL;
140     opt->set   = set;
141     opt->clear = clear;
142     strncpy( opt->name, name, sizeof(opt->name) );
143     opt->name[sizeof(opt->name)-1] = 0;
144     if (last_option) last_option->next = opt;
145     else first_option = opt;
146     last_option = opt;
147
148     /* apply option to all existing dlls */
149     while (dll)
150     {
151         apply_option( dll, opt );
152         dll = dll->next;
153     }
154 }
155
156 /* parse a set of debugging option specifications and add them to the option list */
157 int wine_dbg_parse_options( const char *str )
158 {
159     char *p, *opt, *next, *options;
160     int i, errors = 0;
161
162     if (!(options = strdup(str))) return -1;
163     for (opt = options; opt; opt = next)
164     {
165         unsigned char set = 0, clear = 0;
166
167         if ((next = strchr( opt, ',' ))) *next++ = 0;
168
169         p = opt + strcspn( opt, "+-" );
170         if (!p[0] || !p[1])  /* bad option, skip it */
171         {
172             errors++;
173             continue;
174         }
175
176         if (p > opt)
177         {
178             for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
179             {
180                 int len = strlen(debug_classes[i]);
181                 if (len != (p - opt)) continue;
182                 if (!memcmp( opt, debug_classes[i], len ))  /* found it */
183                 {
184                     if (*p == '+') set |= 1 << i;
185                     else clear |= 1 << i;
186                     break;
187                 }
188             }
189             if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
190             {
191                 errors++;
192                 continue;
193             }
194         }
195         else
196         {
197             if (*p == '+') set = ~0;
198             else clear = ~0;
199         }
200         p++;
201         if (!strcmp( p, "all" )) p = "";  /* empty string means all */
202         wine_dbg_add_option( p, set, clear );
203     }
204     free( options );
205     return errors;
206 }
207
208 #ifdef __WINE__
209
210 /* varargs wrapper for __wine_dbg_vprintf */
211 int wine_dbg_printf( const char *format, ... )
212 {
213     int ret;
214     va_list valist;
215
216     va_start(valist, format);
217     ret = __wine_dbg_vprintf( format, valist );
218     va_end(valist);
219     return ret;
220 }
221
222
223 /* varargs wrapper for __wine_dbg_vlog */
224 int wine_dbg_log( int cls, const char *channel, const char *func, const char *format, ... )
225 {
226     int ret;
227     va_list valist;
228
229     va_start(valist, format);
230     ret = __wine_dbg_vlog( cls, channel, func, format, valist );
231     va_end(valist);
232     return ret;
233 }
234
235 #endif /*__WINE__*/
236
237 /* allocate some tmp string space */
238 /* FIXME: this is not 100% thread-safe */
239 static char *get_tmp_space( int size )
240 {
241     static char *list[32];
242     static long 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 /* default implementation of wine_dbgstr_an */
253 static const char *default_dbgstr_an( const char *str, int n )
254 {
255     char *dst, *res;
256
257     if (!HIWORD(str))
258     {
259         if (!str) return "(null)";
260         res = get_tmp_space( 6 );
261         sprintf( res, "#%04x", LOWORD(str) );
262         return res;
263     }
264     if (n == -1) n = strlen(str);
265     if (n < 0) n = 0;
266     else if (n > 200) n = 200;
267     dst = res = get_tmp_space( n * 4 + 6 );
268     *dst++ = '"';
269     while (n-- > 0)
270     {
271         unsigned char c = *str++;
272         switch (c)
273         {
274         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
275         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
276         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
277         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
278         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
279         default:
280             if (c >= ' ' && c <= 126)
281                 *dst++ = c;
282             else
283             {
284                 *dst++ = '\\';
285                 *dst++ = '0' + ((c >> 6) & 7);
286                 *dst++ = '0' + ((c >> 3) & 7);
287                 *dst++ = '0' + ((c >> 0) & 7);
288             }
289         }
290     }
291     *dst++ = '"';
292     if (*str)
293     {
294         *dst++ = '.';
295         *dst++ = '.';
296         *dst++ = '.';
297     }
298     *dst = 0;
299     return res;
300 }
301
302
303 /* default implementation of wine_dbgstr_wn */
304 static const char *default_dbgstr_wn( const WCHAR *str, int n )
305 {
306     char *dst, *res;
307
308     if (!HIWORD(str))
309     {
310         if (!str) return "(null)";
311         res = get_tmp_space( 6 );
312         sprintf( res, "#%04x", LOWORD(str) );
313         return res;
314     }
315     if (n == -1) n = strlenW(str);
316     if (n < 0) n = 0;
317     else if (n > 200) n = 200;
318     dst = res = get_tmp_space( n * 5 + 7 );
319     *dst++ = 'L';
320     *dst++ = '"';
321     while (n-- > 0)
322     {
323         WCHAR c = *str++;
324         switch (c)
325         {
326         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
327         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
328         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
329         case '"':  *dst++ = '\\'; *dst++ = '"'; break;
330         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
331         default:
332             if (c >= ' ' && c <= 126)
333                 *dst++ = c;
334             else
335             {
336                 *dst++ = '\\';
337                 sprintf(dst,"%04x",c);
338                 dst+=4;
339             }
340         }
341     }
342     *dst++ = '"';
343     if (*str)
344     {
345         *dst++ = '.';
346         *dst++ = '.';
347         *dst++ = '.';
348     }
349     *dst = 0;
350     return res;
351 }
352
353
354 /* default implementation of wine_dbgstr_guid */
355 static const char *default_dbgstr_guid( const struct _GUID *id )
356 {
357     char *str;
358
359     if (!id) return "(null)";
360     if (!((int)id >> 16))
361     {
362         str = get_tmp_space( 12 );
363         sprintf( str, "<guid-0x%04x>", (int)id & 0xffff );
364     }
365     else
366     {
367         str = get_tmp_space( 40 );
368         sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
369                  id->Data1, id->Data2, id->Data3,
370                  id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
371                  id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
372     }
373     return str;
374 }
375
376
377 /* default implementation of wine_dbg_vprintf */
378 static int default_dbg_vprintf( const char *format, va_list args )
379 {
380     return vfprintf( stderr, format, args );
381 }
382
383
384 /* default implementation of wine_dbg_vlog */
385 static int default_dbg_vlog( int cls, const char *channel, const char *func,
386                              const char *format, va_list args )
387 {
388     int ret = 0;
389
390     if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
391         ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel + 1, func );
392     if (format)
393         ret += wine_dbg_vprintf( format, args );
394     return ret;
395 }
396
397
398 /* exported function pointers so that debugging functions can be redirected at run-time */
399
400 const char * (*__wine_dbgstr_an)( const char * s, int n ) = default_dbgstr_an;
401 const char * (*__wine_dbgstr_wn)( const WCHAR *s, int n ) = default_dbgstr_wn;
402 const char * (*__wine_dbgstr_guid)( const struct _GUID *id ) = default_dbgstr_guid;
403 int (*__wine_dbg_vprintf)( const char *format, va_list args ) = default_dbg_vprintf;
404 int (*__wine_dbg_vlog)( int cls, const char *channel, const char *function,
405                         const char *format, va_list args ) = default_dbg_vlog;
406
407 /* wrappers to use the function pointers */
408
409 #ifdef __WINE__
410
411 const char *wine_dbgstr_guid( const struct _GUID *id )
412 {
413     return __wine_dbgstr_guid(id);
414 }
415
416 const char *wine_dbgstr_an( const char * s, int n )
417 {
418     return __wine_dbgstr_an(s, n);
419 }
420
421 const char *wine_dbgstr_wn( const WCHAR *s, int n )
422 {
423     return __wine_dbgstr_wn(s, n);
424 }
425
426 const char *wine_dbgstr_a( const char *s )
427 {
428     return __wine_dbgstr_an( s, -1 );
429 }
430
431 const char *wine_dbgstr_w( const WCHAR *s )
432 {
433     return __wine_dbgstr_wn( s, -1 );
434 }
435
436 #endif /*__WINE__*/