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
31 #include <sys/types.h>
35 #include "wine/debug.h"
37 struct __wine_debug_functions
39 char * (*get_temp_buffer
)( size_t n
);
40 void (*release_temp_buffer
)( char *buffer
, size_t n
);
41 const char * (*dbgstr_an
)( const char * s
, int n
);
42 const char * (*dbgstr_wn
)( const WCHAR
*s
, int n
);
43 int (*dbg_vprintf
)( const char *format
, va_list args
);
44 int (*dbg_vlog
)( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
45 const char *function
, const char *format
, va_list args
);
48 static const char * const debug_classes
[] = { "fixme", "err", "warn", "trace" };
50 #define MAX_DEBUG_OPTIONS 256
52 static unsigned char default_flags
= (1 << __WINE_DBCL_ERR
) | (1 << __WINE_DBCL_FIXME
);
53 static int nb_debug_options
= -1;
54 static struct __wine_debug_channel debug_options
[MAX_DEBUG_OPTIONS
];
56 static struct __wine_debug_functions funcs
;
58 static void debug_init(void);
60 static int cmp_name( const void *p1
, const void *p2
)
62 const char *name
= p1
;
63 const struct __wine_debug_channel
*chan
= p2
;
64 return strcmp( name
, chan
->name
);
67 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
68 unsigned char __wine_dbg_get_channel_flags_obsolete( struct __wine_debug_channel
*channel
)
70 if (nb_debug_options
== -1) debug_init();
74 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
75 sizeof(debug_options
[0]), cmp_name
);
76 if (opt
) return opt
->flags
;
78 /* no option for this channel */
79 if (channel
->flags
& (1 << __WINE_DBCL_INIT
)) channel
->flags
= default_flags
;
83 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
84 int __wine_dbg_set_channel_flags_obsolete( struct __wine_debug_channel
*channel
,
85 unsigned char set
, unsigned char clear
)
87 if (nb_debug_options
== -1) debug_init();
91 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
92 sizeof(debug_options
[0]), cmp_name
);
95 opt
->flags
= (opt
->flags
& ~clear
) | set
;
102 /* add a new debug option at the end of the option list */
103 static void add_option( const char *name
, unsigned char set
, unsigned char clear
)
105 int min
= 0, max
= nb_debug_options
- 1, pos
, res
;
107 if (!name
[0]) /* "all" option */
109 default_flags
= (default_flags
& ~clear
) | set
;
112 if (strlen(name
) >= sizeof(debug_options
[0].name
)) return;
116 pos
= (min
+ max
) / 2;
117 res
= strcmp( name
, debug_options
[pos
].name
);
120 debug_options
[pos
].flags
= (debug_options
[pos
].flags
& ~clear
) | set
;
123 if (res
< 0) max
= pos
- 1;
126 if (nb_debug_options
>= MAX_DEBUG_OPTIONS
) return;
129 if (pos
< nb_debug_options
) memmove( &debug_options
[pos
+ 1], &debug_options
[pos
],
130 (nb_debug_options
- pos
) * sizeof(debug_options
[0]) );
131 strcpy( debug_options
[pos
].name
, name
);
132 debug_options
[pos
].flags
= (default_flags
& ~clear
) | set
;
136 /* parse a set of debugging option specifications and add them to the option list */
137 static void parse_options( const char *str
)
139 char *opt
, *next
, *options
;
142 if (!(options
= strdup(str
))) return;
143 for (opt
= options
; opt
; opt
= next
)
146 unsigned char set
= 0, clear
= 0;
148 if ((next
= strchr( opt
, ',' ))) *next
++ = 0;
150 p
= opt
+ strcspn( opt
, "+-" );
151 if (!p
[0]) p
= opt
; /* assume it's a debug channel name */
155 for (i
= 0; i
< ARRAY_SIZE(debug_classes
); i
++)
157 int len
= strlen(debug_classes
[i
]);
158 if (len
!= (p
- opt
)) continue;
159 if (!memcmp( opt
, debug_classes
[i
], len
)) /* found it */
161 if (*p
== '+') set
|= 1 << i
;
162 else clear
|= 1 << i
;
166 if (i
== ARRAY_SIZE(debug_classes
)) /* bad class name, skip it */
171 if (*p
== '-') clear
= ~0;
174 if (*p
== '+' || *p
== '-') p
++;
177 if (!strcmp( p
, "all" ))
178 default_flags
= (default_flags
& ~clear
) | set
;
180 add_option( p
, set
, clear
);
186 /* print the usage message */
187 static void debug_usage(void)
189 static const char usage
[] =
190 "Syntax of the WINEDEBUG variable:\n"
191 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
192 "Example: WINEDEBUG=+all,warn-heap\n"
193 " turns on all messages except warning heap messages\n"
194 "Available message classes: err, warn, fixme, trace\n";
195 write( 2, usage
, sizeof(usage
) - 1 );
200 /* initialize all options at startup */
201 static void debug_init(void)
204 struct stat st1
, st2
;
206 if (nb_debug_options
!= -1) return; /* already initialized */
207 nb_debug_options
= 0;
209 /* check for stderr pointing to /dev/null */
210 if (!fstat( 2, &st1
) && S_ISCHR(st1
.st_mode
) &&
211 !stat( "/dev/null", &st2
) && S_ISCHR(st2
.st_mode
) &&
212 st1
.st_rdev
== st2
.st_rdev
)
217 if ((wine_debug
= getenv("WINEDEBUG")))
219 if (!strcmp( wine_debug
, "help" )) debug_usage();
220 parse_options( wine_debug
);
224 /* varargs wrapper for funcs.dbg_vprintf */
225 int wine_dbg_printf_obsolete( const char *format
, ... )
230 va_start(valist
, format
);
231 ret
= funcs
.dbg_vprintf( format
, valist
);
236 /* printf with temp buffer allocation */
237 const char *wine_dbg_sprintf_obsolete( const char *format
, ... )
239 static const int max_size
= 200;
244 va_start(valist
, format
);
245 ret
= funcs
.get_temp_buffer( max_size
);
246 len
= vsnprintf( ret
, max_size
, format
, valist
);
247 if (len
== -1 || len
>= max_size
) ret
[max_size
-1] = 0;
248 else funcs
.release_temp_buffer( ret
, len
+ 1 );
254 /* varargs wrapper for funcs.dbg_vlog */
255 int wine_dbg_log_obsolete( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
256 const char *func
, const char *format
, ... )
261 if (!(__wine_dbg_get_channel_flags_obsolete( channel
) & (1 << cls
))) return -1;
263 va_start(valist
, format
);
264 ret
= funcs
.dbg_vlog( cls
, channel
, func
, format
, valist
);
270 /* allocate some tmp string space */
271 /* FIXME: this is not 100% thread-safe */
272 static char *get_temp_buffer( size_t size
)
274 static char *list
[32];
279 idx
= pos
++ % ARRAY_SIZE(list
);
280 if ((ret
= realloc( list
[idx
], size
))) list
[idx
] = ret
;
285 /* release unused part of the buffer */
286 static void release_temp_buffer( char *buffer
, size_t size
)
288 /* don't bother doing anything */
292 /* default implementation of wine_dbgstr_an */
293 static const char *default_dbgstr_an( const char *str
, int n
)
295 static const char hex
[16] = "0123456789abcdef";
299 if (!((ULONG_PTR
)str
>> 16))
301 if (!str
) return "(null)";
302 res
= funcs
.get_temp_buffer( 6 );
303 sprintf( res
, "#%04x", LOWORD(str
) );
306 if (n
== -1) n
= strlen(str
);
308 size
= 10 + min( 300, n
* 4 );
309 dst
= res
= funcs
.get_temp_buffer( size
);
311 while (n
-- > 0 && dst
<= res
+ size
- 9)
313 unsigned char c
= *str
++;
316 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
317 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
318 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
319 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
320 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
322 if (c
>= ' ' && c
<= 126)
328 *dst
++ = hex
[(c
>> 4) & 0x0f];
329 *dst
++ = hex
[c
& 0x0f];
341 funcs
.release_temp_buffer( res
, dst
- res
);
346 /* default implementation of wine_dbgstr_wn */
347 static const char *default_dbgstr_wn( const WCHAR
*str
, int n
)
352 if (!((ULONG_PTR
)str
>> 16))
354 if (!str
) return "(null)";
355 res
= funcs
.get_temp_buffer( 6 );
356 sprintf( res
, "#%04x", LOWORD(str
) );
361 const WCHAR
*end
= str
;
366 size
= 12 + min( 300, n
* 5 );
367 dst
= res
= funcs
.get_temp_buffer( size
);
370 while (n
-- > 0 && dst
<= res
+ size
- 10)
375 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
376 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
377 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
378 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
379 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
381 if (c
>= ' ' && c
<= 126)
386 sprintf(dst
,"%04x",c
);
399 funcs
.release_temp_buffer( res
, dst
- res
);
404 /* default implementation of wine_dbg_vprintf */
405 static int default_dbg_vprintf( const char *format
, va_list args
)
407 return vfprintf( stderr
, format
, args
);
411 /* default implementation of wine_dbg_vlog */
412 static int default_dbg_vlog( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
413 const char *func
, const char *format
, va_list args
)
417 if (cls
< ARRAY_SIZE(debug_classes
))
418 ret
+= wine_dbg_printf_obsolete( "%s:%s:%s ", debug_classes
[cls
], channel
->name
, func
);
420 ret
+= funcs
.dbg_vprintf( format
, args
);
424 /* wrappers to use the function pointers */
426 const char *wine_dbgstr_an_obsolete( const char * s
, int n
)
428 return funcs
.dbgstr_an(s
, n
);
431 const char *wine_dbgstr_wn_obsolete( const WCHAR
*s
, int n
)
433 return funcs
.dbgstr_wn(s
, n
);
436 void __wine_dbg_set_functions_obsolete( const struct __wine_debug_functions
*new_funcs
,
437 struct __wine_debug_functions
*old_funcs
, size_t size
)
439 if (old_funcs
) memcpy( old_funcs
, &funcs
, min(sizeof(funcs
),size
) );
440 if (new_funcs
) memcpy( &funcs
, new_funcs
, min(sizeof(funcs
),size
) );
443 static struct __wine_debug_functions funcs
=
453 __ASM_OBSOLETE(__wine_dbg_get_channel_flags
);
454 __ASM_OBSOLETE(__wine_dbg_set_channel_flags
);
455 __ASM_OBSOLETE(__wine_dbg_set_functions
);
456 __ASM_OBSOLETE(wine_dbg_log
);
457 __ASM_OBSOLETE(wine_dbg_printf
);
458 __ASM_OBSOLETE(wine_dbg_sprintf
);
459 __ASM_OBSOLETE(wine_dbgstr_an
);
460 __ASM_OBSOLETE(wine_dbgstr_wn
);
462 #endif /* __ASM_OBSOLETE */