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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
30 #include "wine/debug.h"
31 #include "wine/library.h"
32 #include "wine/unicode.h"
34 static const char * const debug_classes
[] = { "fixme", "err", "warn", "trace" };
36 #define MAX_DEBUG_OPTIONS 256
38 static unsigned char default_flags
= (1 << __WINE_DBCL_ERR
) | (1 << __WINE_DBCL_FIXME
);
39 static unsigned int nb_debug_options
= 0;
40 static struct __wine_debug_channel debug_options
[MAX_DEBUG_OPTIONS
];
42 static struct __wine_debug_functions funcs
;
44 static int cmp_name( const void *p1
, const void *p2
)
46 const char *name
= p1
;
47 const struct __wine_debug_channel
*chan
= p2
;
48 return strcmp( name
, chan
->name
);
51 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
52 static inline unsigned char get_channel_flags( struct __wine_debug_channel
*channel
)
56 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
57 sizeof(debug_options
[0]), cmp_name
);
58 if (opt
) return opt
->flags
;
60 /* no option for this channel */
61 if (channel
->flags
& (1 << __WINE_DBCL_INIT
)) channel
->flags
= default_flags
;
65 /* register a new set of channels for a dll */
66 void *__wine_dbg_register( struct __wine_debug_channel
* const *channels
, int nb
)
70 for (i
= 0; i
< nb
; i
++)
72 channels
[i
]->flags
= ~0;
73 get_channel_flags( channels
[i
] );
79 /* unregister a set of channels; must pass the pointer obtained from wine_dbg_register */
80 void __wine_dbg_unregister( void *channel
)
85 /* add a new debug option at the end of the option list */
86 static void add_option( const char *name
, unsigned char set
, unsigned char clear
)
88 int min
= 0, max
= nb_debug_options
- 1, pos
, res
;
90 if (!name
[0]) /* "all" option */
92 default_flags
= (default_flags
& ~clear
) | set
;
95 if (strlen(name
) >= sizeof(debug_options
[0].name
)) return;
99 pos
= (min
+ max
) / 2;
100 res
= strcmp( name
, debug_options
[pos
].name
);
103 debug_options
[pos
].flags
= (debug_options
[pos
].flags
& ~clear
) | set
;
106 if (res
< 0) max
= pos
- 1;
109 if (nb_debug_options
>= MAX_DEBUG_OPTIONS
) return;
112 if (pos
< nb_debug_options
) memmove( &debug_options
[pos
+ 1], &debug_options
[pos
],
113 (nb_debug_options
- pos
) * sizeof(debug_options
[0]) );
114 strcpy( debug_options
[pos
].name
, name
);
115 debug_options
[pos
].flags
= (default_flags
& ~clear
) | set
;
119 /* parse a set of debugging option specifications and add them to the option list */
120 static void parse_options( const char *str
)
122 char *opt
, *next
, *options
;
125 if (!(options
= strdup(str
))) return;
126 for (opt
= options
; opt
; opt
= next
)
129 unsigned char set
= 0, clear
= 0;
131 if ((next
= strchr( opt
, ',' ))) *next
++ = 0;
133 p
= opt
+ strcspn( opt
, "+-" );
134 if (!p
[0]) p
= opt
; /* assume it's a debug channel name */
138 for (i
= 0; i
< sizeof(debug_classes
)/sizeof(debug_classes
[0]); i
++)
140 int len
= strlen(debug_classes
[i
]);
141 if (len
!= (p
- opt
)) continue;
142 if (!memcmp( opt
, debug_classes
[i
], len
)) /* found it */
144 if (*p
== '+') set
|= 1 << i
;
145 else clear
|= 1 << i
;
149 if (i
== sizeof(debug_classes
)/sizeof(debug_classes
[0])) /* bad class name, skip it */
154 if (*p
== '-') clear
= ~0;
157 if (*p
== '+' || *p
== '-') p
++;
160 if (!strcmp( p
, "all" ))
161 default_flags
= (default_flags
& ~clear
) | set
;
163 add_option( p
, set
, clear
);
169 /* print the usage message */
170 static void debug_usage(void)
172 static const char usage
[] =
173 "Syntax of the WINEDEBUG variable:\n"
174 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
175 "Example: WINEDEBUG=+all,warn-heap\n"
176 " turns on all messages except warning heap messages\n"
177 "Available message classes: err, warn, fixme, trace\n";
178 write( 2, usage
, sizeof(usage
) - 1 );
183 /* initialize all options at startup */
184 void debug_init(void)
188 if ((wine_debug
= getenv("WINEDEBUG")))
190 if (!strcmp( wine_debug
, "help" )) debug_usage();
191 parse_options( wine_debug
);
195 /* varargs wrapper for funcs.dbg_vprintf */
196 int wine_dbg_printf( const char *format
, ... )
201 va_start(valist
, format
);
202 ret
= funcs
.dbg_vprintf( format
, valist
);
207 /* printf with temp buffer allocation */
208 const char *wine_dbg_sprintf( const char *format
, ... )
210 static const int max_size
= 200;
215 va_start(valist
, format
);
216 ret
= funcs
.get_temp_buffer( max_size
);
217 len
= vsnprintf( ret
, max_size
, format
, valist
);
218 if (len
== -1 || len
>= max_size
) ret
[max_size
-1] = 0;
219 else funcs
.release_temp_buffer( ret
, len
+ 1 );
225 /* varargs wrapper for funcs.dbg_vlog */
226 int wine_dbg_log( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
227 const char *func
, const char *format
, ... )
232 if (!(get_channel_flags( channel
) & (1 << cls
))) return -1;
233 if (!format
) return 0;
235 va_start(valist
, format
);
236 ret
= funcs
.dbg_vlog( cls
, channel
, func
, format
, valist
);
242 /* allocate some tmp string space */
243 /* FIXME: this is not 100% thread-safe */
244 static char *get_temp_buffer( size_t size
)
246 static char *list
[32];
251 idx
= interlocked_xchg_add( &pos
, 1 ) % (sizeof(list
)/sizeof(list
[0]));
252 if ((ret
= realloc( list
[idx
], size
))) list
[idx
] = ret
;
257 /* release unused part of the buffer */
258 static void release_temp_buffer( char *buffer
, size_t size
)
260 /* don't bother doing anything */
264 /* default implementation of wine_dbgstr_an */
265 static const char *default_dbgstr_an( const char *str
, int n
)
267 static const char hex
[16] = "0123456789abcdef";
271 if (!((ULONG_PTR
)str
>> 16))
273 if (!str
) return "(null)";
274 res
= funcs
.get_temp_buffer( 6 );
275 sprintf( res
, "#%04x", LOWORD(str
) );
278 if (n
== -1) n
= strlen(str
);
280 size
= 10 + min( 300, n
* 4 );
281 dst
= res
= funcs
.get_temp_buffer( size
);
283 while (n
-- > 0 && dst
<= res
+ size
- 9)
285 unsigned char c
= *str
++;
288 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
289 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
290 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
291 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
292 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
294 if (c
>= ' ' && c
<= 126)
300 *dst
++ = hex
[(c
>> 4) & 0x0f];
301 *dst
++ = hex
[c
& 0x0f];
313 funcs
.release_temp_buffer( res
, dst
- res
);
318 /* default implementation of wine_dbgstr_wn */
319 static const char *default_dbgstr_wn( const WCHAR
*str
, int n
)
324 if (!((ULONG_PTR
)str
>> 16))
326 if (!str
) return "(null)";
327 res
= funcs
.get_temp_buffer( 6 );
328 sprintf( res
, "#%04x", LOWORD(str
) );
331 if (n
== -1) n
= strlenW(str
);
333 size
= 12 + min( 300, n
* 5 );
334 dst
= res
= funcs
.get_temp_buffer( n
* 5 + 7 );
337 while (n
-- > 0 && dst
<= res
+ size
- 10)
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;
348 if (c
>= ' ' && c
<= 126)
353 sprintf(dst
,"%04x",c
);
366 funcs
.release_temp_buffer( res
, dst
- res
);
371 /* default implementation of wine_dbg_vprintf */
372 static int default_dbg_vprintf( const char *format
, va_list args
)
374 return vfprintf( stderr
, format
, args
);
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
)
384 if (cls
< sizeof(debug_classes
)/sizeof(debug_classes
[0]))
385 ret
+= wine_dbg_printf( "%s:%s:%s ", debug_classes
[cls
], channel
->name
, func
);
387 ret
+= funcs
.dbg_vprintf( format
, args
);
391 /* wrappers to use the function pointers */
393 const char *wine_dbgstr_an( const char * s
, int n
)
395 return funcs
.dbgstr_an(s
, n
);
398 const char *wine_dbgstr_wn( const WCHAR
*s
, int n
)
400 return funcs
.dbgstr_wn(s
, n
);
403 const char *wine_dbgstr_a( const char *s
)
405 return funcs
.dbgstr_an( s
, -1 );
408 const char *wine_dbgstr_w( const WCHAR
*s
)
410 return funcs
.dbgstr_wn( s
, -1 );
413 void __wine_dbg_set_functions( const struct __wine_debug_functions
*new_funcs
,
414 struct __wine_debug_functions
*old_funcs
, size_t size
)
416 if (old_funcs
) memcpy( old_funcs
, &funcs
, min(sizeof(funcs
),size
) );
417 if (new_funcs
) memcpy( &funcs
, new_funcs
, min(sizeof(funcs
),size
) );
420 static struct __wine_debug_functions funcs
=