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"
32 #ifdef HAVE_SYS_STAT_H
33 # include <sys/stat.h>
36 #include "wine/debug.h"
38 struct __wine_debug_functions
40 char * (*get_temp_buffer
)( size_t n
);
41 void (*release_temp_buffer
)( char *buffer
, size_t n
);
42 const char * (*dbgstr_an
)( const char * s
, int n
);
43 const char * (*dbgstr_wn
)( const WCHAR
*s
, int n
);
44 int (*dbg_vprintf
)( const char *format
, va_list args
);
45 int (*dbg_vlog
)( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
46 const char *function
, const char *format
, va_list args
);
49 static const char * const debug_classes
[] = { "fixme", "err", "warn", "trace" };
51 #define MAX_DEBUG_OPTIONS 256
53 static unsigned char default_flags
= (1 << __WINE_DBCL_ERR
) | (1 << __WINE_DBCL_FIXME
);
54 static int nb_debug_options
= -1;
55 static struct __wine_debug_channel debug_options
[MAX_DEBUG_OPTIONS
];
57 static struct __wine_debug_functions funcs
;
59 static void debug_init(void);
61 static int cmp_name( const void *p1
, const void *p2
)
63 const char *name
= p1
;
64 const struct __wine_debug_channel
*chan
= p2
;
65 return strcmp( name
, chan
->name
);
68 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
69 unsigned char __wine_dbg_get_channel_flags_obsolete( struct __wine_debug_channel
*channel
)
71 if (nb_debug_options
== -1) debug_init();
75 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
76 sizeof(debug_options
[0]), cmp_name
);
77 if (opt
) return opt
->flags
;
79 /* no option for this channel */
80 if (channel
->flags
& (1 << __WINE_DBCL_INIT
)) channel
->flags
= default_flags
;
84 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
85 int __wine_dbg_set_channel_flags_obsolete( struct __wine_debug_channel
*channel
,
86 unsigned char set
, unsigned char clear
)
88 if (nb_debug_options
== -1) debug_init();
92 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
93 sizeof(debug_options
[0]), cmp_name
);
96 opt
->flags
= (opt
->flags
& ~clear
) | set
;
103 /* add a new debug option at the end of the option list */
104 static void add_option( const char *name
, unsigned char set
, unsigned char clear
)
106 int min
= 0, max
= nb_debug_options
- 1, pos
, res
;
108 if (!name
[0]) /* "all" option */
110 default_flags
= (default_flags
& ~clear
) | set
;
113 if (strlen(name
) >= sizeof(debug_options
[0].name
)) return;
117 pos
= (min
+ max
) / 2;
118 res
= strcmp( name
, debug_options
[pos
].name
);
121 debug_options
[pos
].flags
= (debug_options
[pos
].flags
& ~clear
) | set
;
124 if (res
< 0) max
= pos
- 1;
127 if (nb_debug_options
>= MAX_DEBUG_OPTIONS
) return;
130 if (pos
< nb_debug_options
) memmove( &debug_options
[pos
+ 1], &debug_options
[pos
],
131 (nb_debug_options
- pos
) * sizeof(debug_options
[0]) );
132 strcpy( debug_options
[pos
].name
, name
);
133 debug_options
[pos
].flags
= (default_flags
& ~clear
) | set
;
137 /* parse a set of debugging option specifications and add them to the option list */
138 static void parse_options( const char *str
)
140 char *opt
, *next
, *options
;
143 if (!(options
= strdup(str
))) return;
144 for (opt
= options
; opt
; opt
= next
)
147 unsigned char set
= 0, clear
= 0;
149 if ((next
= strchr( opt
, ',' ))) *next
++ = 0;
151 p
= opt
+ strcspn( opt
, "+-" );
152 if (!p
[0]) p
= opt
; /* assume it's a debug channel name */
156 for (i
= 0; i
< ARRAY_SIZE(debug_classes
); i
++)
158 int len
= strlen(debug_classes
[i
]);
159 if (len
!= (p
- opt
)) continue;
160 if (!memcmp( opt
, debug_classes
[i
], len
)) /* found it */
162 if (*p
== '+') set
|= 1 << i
;
163 else clear
|= 1 << i
;
167 if (i
== ARRAY_SIZE(debug_classes
)) /* bad class name, skip it */
172 if (*p
== '-') clear
= ~0;
175 if (*p
== '+' || *p
== '-') p
++;
178 if (!strcmp( p
, "all" ))
179 default_flags
= (default_flags
& ~clear
) | set
;
181 add_option( p
, set
, clear
);
187 /* print the usage message */
188 static void debug_usage(void)
190 static const char usage
[] =
191 "Syntax of the WINEDEBUG variable:\n"
192 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
193 "Example: WINEDEBUG=+all,warn-heap\n"
194 " turns on all messages except warning heap messages\n"
195 "Available message classes: err, warn, fixme, trace\n";
196 write( 2, usage
, sizeof(usage
) - 1 );
201 /* initialize all options at startup */
202 static void debug_init(void)
205 struct stat st1
, st2
;
207 if (nb_debug_options
!= -1) return; /* already initialized */
208 nb_debug_options
= 0;
210 /* check for stderr pointing to /dev/null */
211 if (!fstat( 2, &st1
) && S_ISCHR(st1
.st_mode
) &&
212 !stat( "/dev/null", &st2
) && S_ISCHR(st2
.st_mode
) &&
213 st1
.st_rdev
== st2
.st_rdev
)
218 if ((wine_debug
= getenv("WINEDEBUG")))
220 if (!strcmp( wine_debug
, "help" )) debug_usage();
221 parse_options( wine_debug
);
225 /* varargs wrapper for funcs.dbg_vprintf */
226 int wine_dbg_printf_obsolete( const char *format
, ... )
231 va_start(valist
, format
);
232 ret
= funcs
.dbg_vprintf( format
, valist
);
237 /* printf with temp buffer allocation */
238 const char *wine_dbg_sprintf_obsolete( const char *format
, ... )
240 static const int max_size
= 200;
245 va_start(valist
, format
);
246 ret
= funcs
.get_temp_buffer( max_size
);
247 len
= vsnprintf( ret
, max_size
, format
, valist
);
248 if (len
== -1 || len
>= max_size
) ret
[max_size
-1] = 0;
249 else funcs
.release_temp_buffer( ret
, len
+ 1 );
255 /* varargs wrapper for funcs.dbg_vlog */
256 int wine_dbg_log_obsolete( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
257 const char *func
, const char *format
, ... )
262 if (!(__wine_dbg_get_channel_flags_obsolete( channel
) & (1 << cls
))) return -1;
264 va_start(valist
, format
);
265 ret
= funcs
.dbg_vlog( cls
, channel
, func
, format
, valist
);
271 /* allocate some tmp string space */
272 /* FIXME: this is not 100% thread-safe */
273 static char *get_temp_buffer( size_t size
)
275 static char *list
[32];
280 idx
= pos
++ % ARRAY_SIZE(list
);
281 if ((ret
= realloc( list
[idx
], size
))) list
[idx
] = ret
;
286 /* release unused part of the buffer */
287 static void release_temp_buffer( char *buffer
, size_t size
)
289 /* don't bother doing anything */
293 /* default implementation of wine_dbgstr_an */
294 static const char *default_dbgstr_an( const char *str
, int n
)
296 static const char hex
[16] = "0123456789abcdef";
300 if (!((ULONG_PTR
)str
>> 16))
302 if (!str
) return "(null)";
303 res
= funcs
.get_temp_buffer( 6 );
304 sprintf( res
, "#%04x", LOWORD(str
) );
307 if (n
== -1) n
= strlen(str
);
309 size
= 10 + min( 300, n
* 4 );
310 dst
= res
= funcs
.get_temp_buffer( size
);
312 while (n
-- > 0 && dst
<= res
+ size
- 9)
314 unsigned char c
= *str
++;
317 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
318 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
319 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
320 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
321 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
323 if (c
>= ' ' && c
<= 126)
329 *dst
++ = hex
[(c
>> 4) & 0x0f];
330 *dst
++ = hex
[c
& 0x0f];
342 funcs
.release_temp_buffer( res
, dst
- res
);
347 /* default implementation of wine_dbgstr_wn */
348 static const char *default_dbgstr_wn( const WCHAR
*str
, int n
)
353 if (!((ULONG_PTR
)str
>> 16))
355 if (!str
) return "(null)";
356 res
= funcs
.get_temp_buffer( 6 );
357 sprintf( res
, "#%04x", LOWORD(str
) );
362 const WCHAR
*end
= str
;
367 size
= 12 + min( 300, n
* 5 );
368 dst
= res
= funcs
.get_temp_buffer( size
);
371 while (n
-- > 0 && dst
<= res
+ size
- 10)
376 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
377 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
378 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
379 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
380 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
382 if (c
>= ' ' && c
<= 126)
387 sprintf(dst
,"%04x",c
);
400 funcs
.release_temp_buffer( res
, dst
- res
);
405 /* default implementation of wine_dbg_vprintf */
406 static int default_dbg_vprintf( const char *format
, va_list args
)
408 return vfprintf( stderr
, format
, args
);
412 /* default implementation of wine_dbg_vlog */
413 static int default_dbg_vlog( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
414 const char *func
, const char *format
, va_list args
)
418 if (cls
< ARRAY_SIZE(debug_classes
))
419 ret
+= wine_dbg_printf_obsolete( "%s:%s:%s ", debug_classes
[cls
], channel
->name
, func
);
421 ret
+= funcs
.dbg_vprintf( format
, args
);
425 /* wrappers to use the function pointers */
427 const char *wine_dbgstr_an_obsolete( const char * s
, int n
)
429 return funcs
.dbgstr_an(s
, n
);
432 const char *wine_dbgstr_wn_obsolete( const WCHAR
*s
, int n
)
434 return funcs
.dbgstr_wn(s
, n
);
437 void __wine_dbg_set_functions_obsolete( const struct __wine_debug_functions
*new_funcs
,
438 struct __wine_debug_functions
*old_funcs
, size_t size
)
440 if (old_funcs
) memcpy( old_funcs
, &funcs
, min(sizeof(funcs
),size
) );
441 if (new_funcs
) memcpy( &funcs
, new_funcs
, min(sizeof(funcs
),size
) );
444 static struct __wine_debug_functions funcs
=
454 __ASM_OBSOLETE(__wine_dbg_get_channel_flags
);
455 __ASM_OBSOLETE(__wine_dbg_set_channel_flags
);
456 __ASM_OBSOLETE(__wine_dbg_set_functions
);
457 __ASM_OBSOLETE(wine_dbg_log
);
458 __ASM_OBSOLETE(wine_dbg_printf
);
459 __ASM_OBSOLETE(wine_dbg_sprintf
);
460 __ASM_OBSOLETE(wine_dbgstr_an
);
461 __ASM_OBSOLETE(wine_dbgstr_wn
);
463 #endif /* __ASM_OBSOLETE */