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"
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
33 #include "wine/debug.h"
34 #include "wine/library.h"
36 #if defined(__MINGW32__) || defined(_MSC_VER)
37 WINE_DECLARE_DEBUG_CHANNEL(tid
);
38 WINE_DECLARE_DEBUG_CHANNEL(pid
);
41 static const char * const debug_classes
[] = { "fixme", "err", "warn", "trace" };
43 #define MAX_DEBUG_OPTIONS 256
45 static unsigned char default_flags
= (1 << __WINE_DBCL_ERR
) | (1 << __WINE_DBCL_FIXME
);
46 static int nb_debug_options
= -1;
47 static struct __wine_debug_channel debug_options
[MAX_DEBUG_OPTIONS
];
49 static struct __wine_debug_functions funcs
;
51 static void debug_init(void);
53 static int cmp_name( const void *p1
, const void *p2
)
55 const char *name
= p1
;
56 const struct __wine_debug_channel
*chan
= p2
;
57 return strcmp( name
, chan
->name
);
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();
67 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
68 sizeof(debug_options
[0]), cmp_name
);
69 if (opt
) return opt
->flags
;
71 /* no option for this channel */
72 if (channel
->flags
& (1 << __WINE_DBCL_INIT
)) channel
->flags
= default_flags
;
76 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
77 int __wine_dbg_set_channel_flags( struct __wine_debug_channel
*channel
,
78 unsigned char set
, unsigned char clear
)
80 if (nb_debug_options
== -1) debug_init();
84 struct __wine_debug_channel
*opt
= bsearch( channel
->name
, debug_options
, nb_debug_options
,
85 sizeof(debug_options
[0]), cmp_name
);
88 opt
->flags
= (opt
->flags
& ~clear
) | set
;
95 /* add a new debug option at the end of the option list */
96 static void add_option( const char *name
, unsigned char set
, unsigned char clear
)
98 int min
= 0, max
= nb_debug_options
- 1, pos
, res
;
100 if (!name
[0]) /* "all" option */
102 default_flags
= (default_flags
& ~clear
) | set
;
105 if (strlen(name
) >= sizeof(debug_options
[0].name
)) return;
109 pos
= (min
+ max
) / 2;
110 res
= strcmp( name
, debug_options
[pos
].name
);
113 debug_options
[pos
].flags
= (debug_options
[pos
].flags
& ~clear
) | set
;
116 if (res
< 0) max
= pos
- 1;
119 if (nb_debug_options
>= MAX_DEBUG_OPTIONS
) return;
122 if (pos
< nb_debug_options
) memmove( &debug_options
[pos
+ 1], &debug_options
[pos
],
123 (nb_debug_options
- pos
) * sizeof(debug_options
[0]) );
124 strcpy( debug_options
[pos
].name
, name
);
125 debug_options
[pos
].flags
= (default_flags
& ~clear
) | set
;
129 /* parse a set of debugging option specifications and add them to the option list */
130 static void parse_options( const char *str
)
132 char *opt
, *next
, *options
;
135 if (!(options
= strdup(str
))) return;
136 for (opt
= options
; opt
; opt
= next
)
139 unsigned char set
= 0, clear
= 0;
141 if ((next
= strchr( opt
, ',' ))) *next
++ = 0;
143 p
= opt
+ strcspn( opt
, "+-" );
144 if (!p
[0]) p
= opt
; /* assume it's a debug channel name */
148 for (i
= 0; i
< sizeof(debug_classes
)/sizeof(debug_classes
[0]); i
++)
150 int len
= strlen(debug_classes
[i
]);
151 if (len
!= (p
- opt
)) continue;
152 if (!memcmp( opt
, debug_classes
[i
], len
)) /* found it */
154 if (*p
== '+') set
|= 1 << i
;
155 else clear
|= 1 << i
;
159 if (i
== sizeof(debug_classes
)/sizeof(debug_classes
[0])) /* bad class name, skip it */
164 if (*p
== '-') clear
= ~0;
167 if (*p
== '+' || *p
== '-') p
++;
170 if (!strcmp( p
, "all" ))
171 default_flags
= (default_flags
& ~clear
) | set
;
173 add_option( p
, set
, clear
);
179 /* print the usage message */
180 static void debug_usage(void)
182 static const char usage
[] =
183 "Syntax of the WINEDEBUG variable:\n"
184 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
185 "Example: WINEDEBUG=+all,warn-heap\n"
186 " turns on all messages except warning heap messages\n"
187 "Available message classes: err, warn, fixme, trace\n";
188 write( 2, usage
, sizeof(usage
) - 1 );
193 /* initialize all options at startup */
194 static void debug_init(void)
197 struct stat st1
, st2
;
199 if (nb_debug_options
!= -1) return; /* already initialized */
200 nb_debug_options
= 0;
202 /* check for stderr pointing to /dev/null */
203 if (!fstat( 2, &st1
) && S_ISCHR(st1
.st_mode
) &&
204 !stat( "/dev/null", &st2
) && S_ISCHR(st2
.st_mode
) &&
205 st1
.st_rdev
== st2
.st_rdev
)
210 if ((wine_debug
= getenv("WINEDEBUG")))
212 if (!strcmp( wine_debug
, "help" )) debug_usage();
213 parse_options( wine_debug
);
217 /* varargs wrapper for funcs.dbg_vprintf */
218 int wine_dbg_printf( const char *format
, ... )
223 va_start(valist
, format
);
224 ret
= funcs
.dbg_vprintf( format
, valist
);
229 /* printf with temp buffer allocation */
230 const char *wine_dbg_sprintf( const char *format
, ... )
232 static const int max_size
= 200;
237 va_start(valist
, format
);
238 ret
= funcs
.get_temp_buffer( max_size
);
239 len
= vsnprintf( ret
, max_size
, format
, valist
);
240 if (len
== -1 || len
>= max_size
) ret
[max_size
-1] = 0;
241 else funcs
.release_temp_buffer( ret
, len
+ 1 );
247 /* varargs wrapper for funcs.dbg_vlog */
248 int wine_dbg_log( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
249 const char *func
, const char *format
, ... )
254 if (!(__wine_dbg_get_channel_flags( channel
) & (1 << cls
))) return -1;
256 va_start(valist
, format
);
257 ret
= funcs
.dbg_vlog( cls
, channel
, func
, format
, valist
);
263 /* allocate some tmp string space */
264 /* FIXME: this is not 100% thread-safe */
265 static char *get_temp_buffer( size_t size
)
267 static char *list
[32];
272 idx
= interlocked_xchg_add( &pos
, 1 ) % (sizeof(list
)/sizeof(list
[0]));
273 if ((ret
= realloc( list
[idx
], size
))) list
[idx
] = ret
;
278 /* release unused part of the buffer */
279 static void release_temp_buffer( char *buffer
, size_t size
)
281 /* don't bother doing anything */
285 /* default implementation of wine_dbgstr_an */
286 static const char *default_dbgstr_an( const char *str
, int n
)
288 static const char hex
[16] = "0123456789abcdef";
292 if (!((ULONG_PTR
)str
>> 16))
294 if (!str
) return "(null)";
295 res
= funcs
.get_temp_buffer( 6 );
296 sprintf( res
, "#%04x", LOWORD(str
) );
299 if (n
== -1) n
= strlen(str
);
301 size
= 10 + min( 300, n
* 4 );
302 dst
= res
= funcs
.get_temp_buffer( size
);
304 while (n
-- > 0 && dst
<= res
+ size
- 9)
306 unsigned char c
= *str
++;
309 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
310 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
311 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
312 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
313 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
315 if (c
>= ' ' && c
<= 126)
321 *dst
++ = hex
[(c
>> 4) & 0x0f];
322 *dst
++ = hex
[c
& 0x0f];
334 funcs
.release_temp_buffer( res
, dst
- res
);
339 /* default implementation of wine_dbgstr_wn */
340 static const char *default_dbgstr_wn( const WCHAR
*str
, int n
)
345 if (!((ULONG_PTR
)str
>> 16))
347 if (!str
) return "(null)";
348 res
= funcs
.get_temp_buffer( 6 );
349 sprintf( res
, "#%04x", LOWORD(str
) );
354 const WCHAR
*end
= str
;
359 size
= 12 + min( 300, n
* 5 );
360 dst
= res
= funcs
.get_temp_buffer( size
);
363 while (n
-- > 0 && dst
<= res
+ size
- 10)
368 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
369 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
370 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
371 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
372 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
374 if (c
>= ' ' && c
<= 126)
379 sprintf(dst
,"%04x",c
);
392 funcs
.release_temp_buffer( res
, dst
- res
);
397 /* default implementation of wine_dbg_vprintf */
398 static int default_dbg_vprintf( const char *format
, va_list args
)
400 return vfprintf( stderr
, format
, args
);
404 /* default implementation of wine_dbg_vlog */
405 static int default_dbg_vlog( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
406 const char *func
, const char *format
, va_list args
)
410 #if defined(__MINGW32__) || defined(_MSC_VER)
412 ret
+= wine_dbg_printf( "%04x:", GetCurrentProcessId() );
414 ret
+= wine_dbg_printf( "%04x:", GetCurrentThreadId() );
416 if (cls
< sizeof(debug_classes
)/sizeof(debug_classes
[0]))
417 ret
+= wine_dbg_printf( "%s:%s:%s ", debug_classes
[cls
], channel
->name
, func
);
419 ret
+= funcs
.dbg_vprintf( format
, args
);
423 /* wrappers to use the function pointers */
425 const char *wine_dbgstr_an( const char * s
, int n
)
427 return funcs
.dbgstr_an(s
, n
);
430 const char *wine_dbgstr_wn( const WCHAR
*s
, int n
)
432 return funcs
.dbgstr_wn(s
, n
);
435 void __wine_dbg_set_functions( const struct __wine_debug_functions
*new_funcs
,
436 struct __wine_debug_functions
*old_funcs
, size_t size
)
438 if (old_funcs
) memcpy( old_funcs
, &funcs
, min(sizeof(funcs
),size
) );
439 if (new_funcs
) memcpy( &funcs
, new_funcs
, min(sizeof(funcs
),size
) );
442 static struct __wine_debug_functions funcs
=