2000-11-05 05:51:34 +01:00
|
|
|
/*
|
|
|
|
* Management of the debugging channels
|
|
|
|
*
|
|
|
|
* Copyright 2000 Alexandre Julliard
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
2000-11-05 05:51:34 +01:00
|
|
|
*/
|
|
|
|
|
2002-05-17 04:55:48 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2000-11-05 05:51:34 +01:00
|
|
|
#include <stdlib.h>
|
2002-05-17 04:55:48 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2000-11-05 05:51:34 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2002-05-17 04:55:48 +02:00
|
|
|
#include "wine/debug.h"
|
|
|
|
#include "wine/library.h"
|
|
|
|
#include "wine/unicode.h"
|
|
|
|
|
2000-11-05 05:51:34 +01:00
|
|
|
struct dll
|
|
|
|
{
|
|
|
|
struct dll *next; /* linked list of dlls */
|
|
|
|
struct dll *prev;
|
|
|
|
char * const *channels; /* array of channels */
|
|
|
|
int nb_channels; /* number of channels in array */
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct dll *first_dll;
|
|
|
|
|
2001-11-06 23:24:02 +01:00
|
|
|
struct debug_option
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
2001-11-06 23:24:02 +01:00
|
|
|
struct debug_option *next; /* next option in list */
|
|
|
|
unsigned char set; /* bits to set */
|
|
|
|
unsigned char clear; /* bits to clear */
|
|
|
|
char name[14]; /* channel name, or empty for "all" */
|
2000-11-05 05:51:34 +01:00
|
|
|
};
|
|
|
|
|
2001-11-06 23:24:02 +01:00
|
|
|
static struct debug_option *first_option;
|
|
|
|
static struct debug_option *last_option;
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2002-06-04 19:48:41 +02:00
|
|
|
static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
|
2000-11-05 05:51:34 +01:00
|
|
|
|
|
|
|
static int cmp_name( const void *p1, const void *p2 )
|
|
|
|
{
|
|
|
|
const char *name = p1;
|
|
|
|
const char * const *chan = p2;
|
|
|
|
return strcmp( name, *chan + 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* apply a debug option to the channels of a given dll */
|
2001-11-06 23:24:02 +01:00
|
|
|
static void apply_option( struct dll *dll, const struct debug_option *opt )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
if (opt->name[0])
|
|
|
|
{
|
|
|
|
char **dbch = bsearch( opt->name, dll->channels, dll->nb_channels,
|
|
|
|
sizeof(*dll->channels), cmp_name );
|
|
|
|
if (dbch) **dbch = (**dbch & ~opt->clear) | opt->set;
|
|
|
|
}
|
|
|
|
else /* all */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < dll->nb_channels; i++)
|
|
|
|
dll->channels[i][0] = (dll->channels[i][0] & ~opt->clear) | opt->set;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* register a new set of channels for a dll */
|
2000-11-08 05:33:20 +01:00
|
|
|
void *__wine_dbg_register( char * const *channels, int nb )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
2001-11-06 23:24:02 +01:00
|
|
|
struct debug_option *opt = first_option;
|
2000-11-05 05:51:34 +01:00
|
|
|
struct dll *dll = malloc( sizeof(*dll) );
|
|
|
|
if (dll)
|
|
|
|
{
|
|
|
|
dll->channels = channels;
|
|
|
|
dll->nb_channels = nb;
|
|
|
|
dll->prev = NULL;
|
|
|
|
if ((dll->next = first_dll)) dll->next->prev = dll;
|
|
|
|
first_dll = dll;
|
|
|
|
|
|
|
|
/* apply existing options to this dll */
|
|
|
|
while (opt)
|
|
|
|
{
|
|
|
|
apply_option( dll, opt );
|
|
|
|
opt = opt->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dll;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* unregister a set of channels; must pass the pointer obtained from wine_dbg_register */
|
2000-11-08 05:33:20 +01:00
|
|
|
void __wine_dbg_unregister( void *channel )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
struct dll *dll = channel;
|
|
|
|
if (dll)
|
|
|
|
{
|
|
|
|
if (dll->next) dll->next->prev = dll->prev;
|
|
|
|
if (dll->prev) dll->prev->next = dll->next;
|
|
|
|
else first_dll = dll->next;
|
|
|
|
free( dll );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* add a new debug option at the end of the option list */
|
|
|
|
void wine_dbg_add_option( const char *name, unsigned char set, unsigned char clear )
|
|
|
|
{
|
|
|
|
struct dll *dll = first_dll;
|
2001-11-06 23:24:02 +01:00
|
|
|
struct debug_option *opt;
|
2000-11-05 05:51:34 +01:00
|
|
|
|
|
|
|
if (!(opt = malloc( sizeof(*opt) ))) return;
|
|
|
|
opt->next = NULL;
|
|
|
|
opt->set = set;
|
|
|
|
opt->clear = clear;
|
|
|
|
strncpy( opt->name, name, sizeof(opt->name) );
|
|
|
|
opt->name[sizeof(opt->name)-1] = 0;
|
|
|
|
if (last_option) last_option->next = opt;
|
|
|
|
else first_option = opt;
|
|
|
|
last_option = opt;
|
|
|
|
|
|
|
|
/* apply option to all existing dlls */
|
|
|
|
while (dll)
|
|
|
|
{
|
|
|
|
apply_option( dll, opt );
|
|
|
|
dll = dll->next;
|
|
|
|
}
|
|
|
|
}
|
2002-05-17 04:55:48 +02:00
|
|
|
|
2002-06-04 19:48:41 +02:00
|
|
|
/* parse a set of debugging option specifications and add them to the option list */
|
|
|
|
int wine_dbg_parse_options( const char *str )
|
|
|
|
{
|
2003-10-03 05:35:20 +02:00
|
|
|
char *opt, *next, *options;
|
2002-06-04 19:48:41 +02:00
|
|
|
int i, errors = 0;
|
|
|
|
|
|
|
|
if (!(options = strdup(str))) return -1;
|
|
|
|
for (opt = options; opt; opt = next)
|
|
|
|
{
|
2003-10-03 05:35:20 +02:00
|
|
|
const char *p;
|
2002-06-04 19:48:41 +02:00
|
|
|
unsigned char set = 0, clear = 0;
|
|
|
|
|
|
|
|
if ((next = strchr( opt, ',' ))) *next++ = 0;
|
|
|
|
|
|
|
|
p = opt + strcspn( opt, "+-" );
|
2003-12-05 01:17:41 +01:00
|
|
|
if (!p[0]) p = opt; /* assume it's a debug channel name */
|
2002-06-04 19:48:41 +02:00
|
|
|
|
|
|
|
if (p > opt)
|
|
|
|
{
|
|
|
|
for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
|
|
|
|
{
|
|
|
|
int len = strlen(debug_classes[i]);
|
|
|
|
if (len != (p - opt)) continue;
|
|
|
|
if (!memcmp( opt, debug_classes[i], len )) /* found it */
|
|
|
|
{
|
|
|
|
if (*p == '+') set |= 1 << i;
|
|
|
|
else clear |= 1 << i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
|
|
|
|
{
|
|
|
|
errors++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-05 01:17:41 +01:00
|
|
|
if (*p == '-') clear = ~0;
|
|
|
|
else set = ~0;
|
|
|
|
}
|
|
|
|
if (*p == '+' || *p == '-') p++;
|
|
|
|
if (!p[0])
|
|
|
|
{
|
|
|
|
errors++;
|
|
|
|
continue;
|
2002-06-04 19:48:41 +02:00
|
|
|
}
|
|
|
|
if (!strcmp( p, "all" )) p = ""; /* empty string means all */
|
|
|
|
wine_dbg_add_option( p, set, clear );
|
|
|
|
}
|
|
|
|
free( options );
|
|
|
|
return errors;
|
|
|
|
}
|
2002-05-17 04:55:48 +02:00
|
|
|
|
|
|
|
/* varargs wrapper for __wine_dbg_vprintf */
|
|
|
|
int wine_dbg_printf( const char *format, ... )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list valist;
|
|
|
|
|
|
|
|
va_start(valist, format);
|
|
|
|
ret = __wine_dbg_vprintf( format, valist );
|
|
|
|
va_end(valist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-03 04:12:58 +01:00
|
|
|
/* varargs wrapper for __wine_dbg_vsprintf */
|
|
|
|
const char *wine_dbg_sprintf( const char *format, ... )
|
|
|
|
{
|
|
|
|
const char *ret;
|
|
|
|
va_list valist;
|
|
|
|
|
|
|
|
va_start(valist, format);
|
|
|
|
ret = __wine_dbg_vsprintf( format, valist );
|
|
|
|
va_end(valist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-17 04:55:48 +02:00
|
|
|
/* varargs wrapper for __wine_dbg_vlog */
|
2003-01-03 04:12:58 +01:00
|
|
|
int wine_dbg_log( unsigned int cls, const char *channel, const char *func, const char *format, ... )
|
2002-05-17 04:55:48 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list valist;
|
|
|
|
|
|
|
|
va_start(valist, format);
|
|
|
|
ret = __wine_dbg_vlog( cls, channel, func, format, valist );
|
|
|
|
va_end(valist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* allocate some tmp string space */
|
|
|
|
/* FIXME: this is not 100% thread-safe */
|
|
|
|
static char *get_tmp_space( int size )
|
|
|
|
{
|
|
|
|
static char *list[32];
|
|
|
|
static long pos;
|
|
|
|
char *ret;
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
|
|
|
|
if ((ret = realloc( list[idx], size ))) list[idx] = ret;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* default implementation of wine_dbgstr_an */
|
|
|
|
static const char *default_dbgstr_an( const char *str, int n )
|
|
|
|
{
|
|
|
|
char *dst, *res;
|
|
|
|
|
2002-05-17 06:17:48 +02:00
|
|
|
if (!HIWORD(str))
|
|
|
|
{
|
|
|
|
if (!str) return "(null)";
|
|
|
|
res = get_tmp_space( 6 );
|
|
|
|
sprintf( res, "#%04x", LOWORD(str) );
|
|
|
|
return res;
|
|
|
|
}
|
2002-05-17 04:55:48 +02:00
|
|
|
if (n == -1) n = strlen(str);
|
|
|
|
if (n < 0) n = 0;
|
|
|
|
else if (n > 200) n = 200;
|
|
|
|
dst = res = get_tmp_space( n * 4 + 6 );
|
|
|
|
*dst++ = '"';
|
|
|
|
while (n-- > 0)
|
|
|
|
{
|
|
|
|
unsigned char c = *str++;
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
|
|
|
|
case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
|
|
|
|
case '\t': *dst++ = '\\'; *dst++ = 't'; break;
|
|
|
|
case '"': *dst++ = '\\'; *dst++ = '"'; break;
|
|
|
|
case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
|
|
|
|
default:
|
|
|
|
if (c >= ' ' && c <= 126)
|
|
|
|
*dst++ = c;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dst++ = '\\';
|
|
|
|
*dst++ = '0' + ((c >> 6) & 7);
|
|
|
|
*dst++ = '0' + ((c >> 3) & 7);
|
|
|
|
*dst++ = '0' + ((c >> 0) & 7);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst++ = '"';
|
|
|
|
if (*str)
|
|
|
|
{
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* default implementation of wine_dbgstr_wn */
|
|
|
|
static const char *default_dbgstr_wn( const WCHAR *str, int n )
|
|
|
|
{
|
|
|
|
char *dst, *res;
|
|
|
|
|
2002-05-17 06:17:48 +02:00
|
|
|
if (!HIWORD(str))
|
|
|
|
{
|
|
|
|
if (!str) return "(null)";
|
|
|
|
res = get_tmp_space( 6 );
|
|
|
|
sprintf( res, "#%04x", LOWORD(str) );
|
|
|
|
return res;
|
|
|
|
}
|
2002-05-17 04:55:48 +02:00
|
|
|
if (n == -1) n = strlenW(str);
|
|
|
|
if (n < 0) n = 0;
|
|
|
|
else if (n > 200) n = 200;
|
|
|
|
dst = res = get_tmp_space( n * 5 + 7 );
|
|
|
|
*dst++ = 'L';
|
|
|
|
*dst++ = '"';
|
|
|
|
while (n-- > 0)
|
|
|
|
{
|
|
|
|
WCHAR c = *str++;
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
|
|
|
|
case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
|
|
|
|
case '\t': *dst++ = '\\'; *dst++ = 't'; break;
|
|
|
|
case '"': *dst++ = '\\'; *dst++ = '"'; break;
|
|
|
|
case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
|
|
|
|
default:
|
|
|
|
if (c >= ' ' && c <= 126)
|
|
|
|
*dst++ = c;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*dst++ = '\\';
|
|
|
|
sprintf(dst,"%04x",c);
|
|
|
|
dst+=4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dst++ = '"';
|
|
|
|
if (*str)
|
|
|
|
{
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
}
|
|
|
|
*dst = 0;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-01-03 04:12:58 +01:00
|
|
|
/* default implementation of wine_dbg_vsprintf */
|
|
|
|
static const char *default_dbg_vsprintf( const char *format, va_list args )
|
2002-05-17 04:55:48 +02:00
|
|
|
{
|
2003-01-03 04:12:58 +01:00
|
|
|
static const int max_size = 200;
|
2002-05-17 04:55:48 +02:00
|
|
|
|
2003-01-03 04:12:58 +01:00
|
|
|
char *res = get_tmp_space( max_size );
|
|
|
|
int len = vsnprintf( res, max_size, format, args );
|
|
|
|
if (len == -1 || len >= max_size) res[max_size-1] = 0;
|
|
|
|
return res;
|
2002-05-17 04:55:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* default implementation of wine_dbg_vprintf */
|
|
|
|
static int default_dbg_vprintf( const char *format, va_list args )
|
|
|
|
{
|
|
|
|
return vfprintf( stderr, format, args );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* default implementation of wine_dbg_vlog */
|
2003-01-03 04:12:58 +01:00
|
|
|
static int default_dbg_vlog( unsigned int cls, const char *channel, const char *func,
|
2002-05-17 04:55:48 +02:00
|
|
|
const char *format, va_list args )
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
2002-06-04 19:48:41 +02:00
|
|
|
if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
|
|
|
|
ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel + 1, func );
|
2002-05-17 04:55:48 +02:00
|
|
|
if (format)
|
|
|
|
ret += __wine_dbg_vprintf( format, args );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* exported function pointers so that debugging functions can be redirected at run-time */
|
|
|
|
|
|
|
|
const char * (*__wine_dbgstr_an)( const char * s, int n ) = default_dbgstr_an;
|
|
|
|
const char * (*__wine_dbgstr_wn)( const WCHAR *s, int n ) = default_dbgstr_wn;
|
2003-01-03 04:12:58 +01:00
|
|
|
const char * (*__wine_dbg_vsprintf)( const char *format, va_list args ) = default_dbg_vsprintf;
|
2002-05-17 04:55:48 +02:00
|
|
|
int (*__wine_dbg_vprintf)( const char *format, va_list args ) = default_dbg_vprintf;
|
2003-01-03 04:12:58 +01:00
|
|
|
int (*__wine_dbg_vlog)( unsigned int cls, const char *channel, const char *function,
|
2002-05-17 04:55:48 +02:00
|
|
|
const char *format, va_list args ) = default_dbg_vlog;
|
2002-05-20 00:29:01 +02:00
|
|
|
|
|
|
|
/* wrappers to use the function pointers */
|
|
|
|
|
|
|
|
const char *wine_dbgstr_an( const char * s, int n )
|
|
|
|
{
|
|
|
|
return __wine_dbgstr_an(s, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *wine_dbgstr_wn( const WCHAR *s, int n )
|
|
|
|
{
|
|
|
|
return __wine_dbgstr_wn(s, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *wine_dbgstr_a( const char *s )
|
|
|
|
{
|
|
|
|
return __wine_dbgstr_an( s, -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *wine_dbgstr_w( const WCHAR *s )
|
|
|
|
{
|
|
|
|
return __wine_dbgstr_wn( s, -1 );
|
|
|
|
}
|