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
22 #include "wine/port.h"
30 #include "wine/debug.h"
31 #include "wine/library.h"
33 static const char * const debug_classes
[] = { "fixme", "err", "warn", "trace" };
35 #define MAX_DEBUG_OPTIONS 256
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
];
41 static struct __wine_debug_functions funcs
;
43 static void debug_init(void);
45 static int cmp_name( const void *p1
, const void *p2
)
47 const char *name
= p1
;
48 const struct __wine_debug_channel
*chan
= p2
;
49 return strcmp( name
, chan
->name
);
52 static int block_traces
= 0;
54 /* enable or disable printing of debug messages temporarily */
55 void __wine_dbg_toggle_block() {
56 block_traces
= !block_traces
;
57 fprintf(stderr
, "wine: tracing is now %s\n", block_traces
? "disabled" : "enabled");
60 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
61 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel
*channel
)
63 if (nb_debug_options
== -1) debug_init();
68 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
69 sizeof(debug_options
[0]), cmp_name
);
70 if (opt
) return opt
->flags
;
71 } else return default_flags
;
73 /* no option for this channel */
74 if (channel
->flags
& (1 << __WINE_DBCL_INIT
)) channel
->flags
= default_flags
;
78 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
79 int __wine_dbg_set_channel_flags( struct __wine_debug_channel
*channel
,
80 unsigned char set
, unsigned char clear
)
82 if (nb_debug_options
== -1) debug_init();
86 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
87 sizeof(debug_options
[0]), cmp_name
);
90 opt
->flags
= (opt
->flags
& ~clear
) | set
;
97 /* add a new debug option at the end of the option list */
98 static void add_option( const char *name
, unsigned char set
, unsigned char clear
)
100 int min
= 0, max
= nb_debug_options
- 1, pos
, res
;
102 if (!name
[0]) /* "all" option */
104 default_flags
= (default_flags
& ~clear
) | set
;
107 if (strlen(name
) >= sizeof(debug_options
[0].name
)) return;
111 pos
= (min
+ max
) / 2;
112 res
= strcmp( name
, debug_options
[pos
].name
);
115 debug_options
[pos
].flags
= (debug_options
[pos
].flags
& ~clear
) | set
;
118 if (res
< 0) max
= pos
- 1;
121 if (nb_debug_options
>= MAX_DEBUG_OPTIONS
) return;
124 if (pos
< nb_debug_options
) memmove( &debug_options
[pos
+ 1], &debug_options
[pos
],
125 (nb_debug_options
- pos
) * sizeof(debug_options
[0]) );
126 strcpy( debug_options
[pos
].name
, name
);
127 debug_options
[pos
].flags
= (default_flags
& ~clear
) | set
;
131 /* parse a set of debugging option specifications and add them to the option list */
132 static void parse_options( const char *str
)
134 char *opt
, *next
, *options
;
137 if (!(options
= strdup(str
))) return;
138 for (opt
= options
; opt
; opt
= next
)
141 unsigned char set
= 0, clear
= 0;
143 if ((next
= strchr( opt
, ',' ))) *next
++ = 0;
145 p
= opt
+ strcspn( opt
, "+-" );
146 if (!p
[0]) p
= opt
; /* assume it's a debug channel name */
150 for (i
= 0; i
< sizeof(debug_classes
)/sizeof(debug_classes
[0]); i
++)
152 int len
= strlen(debug_classes
[i
]);
153 if (len
!= (p
- opt
)) continue;
154 if (!memcmp( opt
, debug_classes
[i
], len
)) /* found it */
156 if (*p
== '+') set
|= 1 << i
;
157 else clear
|= 1 << i
;
161 if (i
== sizeof(debug_classes
)/sizeof(debug_classes
[0])) /* bad class name, skip it */
166 if (*p
== '-') clear
= ~0;
169 if (*p
== '+' || *p
== '-') p
++;
172 if (!strcmp( p
, "all" ))
173 default_flags
= (default_flags
& ~clear
) | set
;
175 add_option( p
, set
, clear
);
181 /* print the usage message */
182 static void debug_usage(void)
184 static const char usage
[] =
185 "Syntax of the WINEDEBUG variable:\n"
186 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
187 "Example: WINEDEBUG=+all,warn-heap\n"
188 " turns on all messages except warning heap messages\n"
189 "Available message classes: err, warn, fixme, trace\n";
190 write( 2, usage
, sizeof(usage
) - 1 );
195 /* initialize all options at startup */
196 static void debug_init(void)
200 if (nb_debug_options
!= -1) return; /* already initialized */
201 nb_debug_options
= 0;
202 if ((wine_debug
= getenv("WINEDEBUG")))
204 if (!strcmp( wine_debug
, "help" )) debug_usage();
205 parse_options( wine_debug
);
207 if(!block_traces
&& getenv("WINEDELAY") && 0 == strncasecmp(getenv("WINEDELAY"), "enable", 6) ) {
208 __wine_dbg_toggle_block();
212 /* varargs wrapper for funcs.dbg_vprintf */
213 int wine_dbg_printf( const char *format
, ... )
218 va_start(valist
, format
);
219 ret
= funcs
.dbg_vprintf( format
, valist
);
224 /* printf with temp buffer allocation */
225 const char *wine_dbg_sprintf( const char *format
, ... )
227 static const int max_size
= 200;
232 va_start(valist
, format
);
233 ret
= funcs
.get_temp_buffer( max_size
);
234 len
= vsnprintf( ret
, max_size
, format
, valist
);
235 if (len
== -1 || len
>= max_size
) ret
[max_size
-1] = 0;
236 else funcs
.release_temp_buffer( ret
, len
+ 1 );
242 /* varargs wrapper for funcs.dbg_vlog */
243 int wine_dbg_log( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
244 const char *func
, const char *format
, ... )
249 if (!(__wine_dbg_get_channel_flags( channel
) & (1 << cls
))) return -1;
251 va_start(valist
, format
);
252 ret
= funcs
.dbg_vlog( cls
, channel
, func
, format
, valist
);
258 /* allocate some tmp string space */
259 /* FIXME: this is not 100% thread-safe */
260 static char *get_temp_buffer( size_t size
)
262 static char *list
[32];
267 idx
= interlocked_xchg_add( &pos
, 1 ) % (sizeof(list
)/sizeof(list
[0]));
268 if ((ret
= realloc( list
[idx
], size
))) list
[idx
] = ret
;
273 /* release unused part of the buffer */
274 static void release_temp_buffer( char *buffer
, size_t size
)
276 /* don't bother doing anything */
280 /* default implementation of wine_dbgstr_an */
281 static const char *default_dbgstr_an( const char *str
, int n
)
283 static const char hex
[16] = "0123456789abcdef";
287 if (!((ULONG_PTR
)str
>> 16))
289 if (!str
) return "(null)";
290 res
= funcs
.get_temp_buffer( 6 );
291 sprintf( res
, "#%04x", LOWORD(str
) );
294 if (n
== -1) n
= strlen(str
);
296 size
= 10 + min( 300, n
* 4 );
297 dst
= res
= funcs
.get_temp_buffer( size
);
299 while (n
-- > 0 && dst
<= res
+ size
- 9)
301 unsigned char c
= *str
++;
304 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
305 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
306 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
307 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
308 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
310 if (c
>= ' ' && c
<= 126)
316 *dst
++ = hex
[(c
>> 4) & 0x0f];
317 *dst
++ = hex
[c
& 0x0f];
329 funcs
.release_temp_buffer( res
, dst
- res
);
334 /* default implementation of wine_dbgstr_wn */
335 static const char *default_dbgstr_wn( const WCHAR
*str
, int n
)
340 if (!((ULONG_PTR
)str
>> 16))
342 if (!str
) return "(null)";
343 res
= funcs
.get_temp_buffer( 6 );
344 sprintf( res
, "#%04x", LOWORD(str
) );
349 const WCHAR
*end
= str
;
354 size
= 12 + min( 300, n
* 5 );
355 dst
= res
= funcs
.get_temp_buffer( size
);
358 while (n
-- > 0 && dst
<= res
+ size
- 10)
363 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
364 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
365 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
366 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
367 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
369 if (c
>= ' ' && c
<= 126)
374 sprintf(dst
,"%04x",c
);
387 funcs
.release_temp_buffer( res
, dst
- res
);
392 /* default implementation of wine_dbg_vprintf */
393 static int default_dbg_vprintf( const char *format
, va_list args
)
395 return vfprintf( stderr
, format
, args
);
399 /* default implementation of wine_dbg_vlog */
400 static int default_dbg_vlog( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
401 const char *func
, const char *format
, va_list args
)
405 if (cls
< sizeof(debug_classes
)/sizeof(debug_classes
[0]))
406 ret
+= wine_dbg_printf( "%s:%s:%s ", debug_classes
[cls
], channel
->name
, func
);
408 ret
+= funcs
.dbg_vprintf( format
, args
);
412 /* wrappers to use the function pointers */
414 const char *wine_dbgstr_an( const char * s
, int n
)
416 return funcs
.dbgstr_an(s
, n
);
419 const char *wine_dbgstr_wn( const WCHAR
*s
, int n
)
421 return funcs
.dbgstr_wn(s
, n
);
424 void __wine_dbg_set_functions( const struct __wine_debug_functions
*new_funcs
,
425 struct __wine_debug_functions
*old_funcs
, size_t size
)
427 if (old_funcs
) memcpy( old_funcs
, &funcs
, min(sizeof(funcs
),size
) );
428 if (new_funcs
) memcpy( &funcs
, new_funcs
, min(sizeof(funcs
),size
) );
431 static struct __wine_debug_functions funcs
=