2000-11-05 05:51:34 +01:00
|
|
|
/*
|
|
|
|
* Debugging functions
|
2002-03-10 00:29:33 +01:00
|
|
|
*
|
|
|
|
* Copyright 2000 Alexandre Julliard
|
|
|
|
*
|
|
|
|
* 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
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2000-11-05 05:51:34 +01:00
|
|
|
*/
|
|
|
|
|
2020-05-17 11:07:02 +02:00
|
|
|
#if 0
|
|
|
|
#pragma makedep unix
|
|
|
|
#endif
|
|
|
|
|
2002-07-22 22:51:02 +02:00
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
2002-08-26 23:53:24 +02:00
|
|
|
|
2001-02-16 20:52:50 +01:00
|
|
|
#include <assert.h>
|
2000-11-05 05:51:34 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2002-08-17 02:43:16 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2003-10-09 00:34:37 +02:00
|
|
|
#include "ntdll_misc.h"
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2015-08-25 12:31:21 +02:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(pid);
|
2011-09-01 09:21:06 +02:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(timestamp);
|
2001-05-08 02:13:38 +02:00
|
|
|
|
2019-04-03 09:50:31 +02:00
|
|
|
static BOOL init_done;
|
|
|
|
static struct debug_info initial_info; /* debug info for initial thread */
|
2019-04-03 19:25:28 +02:00
|
|
|
static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
|
|
|
|
static int nb_debug_options = -1;
|
|
|
|
static int options_size;
|
|
|
|
static struct __wine_debug_channel *debug_options;
|
|
|
|
|
|
|
|
static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
|
2000-11-05 05:51:34 +01:00
|
|
|
|
|
|
|
/* get the debug info pointer for the current thread */
|
|
|
|
static inline struct debug_info *get_info(void)
|
|
|
|
{
|
2019-04-03 14:16:29 +02:00
|
|
|
if (!init_done) return &initial_info;
|
2005-06-06 22:04:33 +02:00
|
|
|
return ntdll_get_thread_data()->debug_info;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
|
|
|
|
2019-04-03 19:24:39 +02:00
|
|
|
/* add a string to the output buffer */
|
|
|
|
static int append_output( struct debug_info *info, const char *str, size_t len )
|
|
|
|
{
|
2019-04-03 14:16:29 +02:00
|
|
|
if (len >= sizeof(info->output) - info->out_pos)
|
2019-04-03 19:24:39 +02:00
|
|
|
{
|
|
|
|
fprintf( stderr, "wine_dbg_output: debugstr buffer overflow (contents: '%s')\n", info->output );
|
2019-04-03 14:16:29 +02:00
|
|
|
info->out_pos = 0;
|
2019-04-03 19:24:39 +02:00
|
|
|
abort();
|
|
|
|
}
|
2019-04-03 14:16:29 +02:00
|
|
|
memcpy( info->output + info->out_pos, str, len );
|
2019-04-03 19:24:39 +02:00
|
|
|
info->out_pos += len;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:25:28 +02:00
|
|
|
/* add a new debug option at the end of the option list */
|
|
|
|
static void add_option( const char *name, unsigned char set, unsigned char clear )
|
|
|
|
{
|
|
|
|
int min = 0, max = nb_debug_options - 1, pos, res;
|
|
|
|
|
|
|
|
if (!name[0]) /* "all" option */
|
|
|
|
{
|
|
|
|
default_flags = (default_flags & ~clear) | set;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (strlen(name) >= sizeof(debug_options[0].name)) return;
|
|
|
|
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
pos = (min + max) / 2;
|
|
|
|
res = strcmp( name, debug_options[pos].name );
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (res < 0) max = pos - 1;
|
|
|
|
else min = pos + 1;
|
|
|
|
}
|
|
|
|
if (nb_debug_options >= options_size)
|
|
|
|
{
|
|
|
|
options_size = max( options_size * 2, 16 );
|
|
|
|
debug_options = realloc( debug_options, options_size * sizeof(debug_options[0]) );
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = min;
|
|
|
|
if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
|
|
|
|
(nb_debug_options - pos) * sizeof(debug_options[0]) );
|
|
|
|
strcpy( debug_options[pos].name, name );
|
|
|
|
debug_options[pos].flags = (default_flags & ~clear) | set;
|
|
|
|
nb_debug_options++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse a set of debugging option specifications and add them to the option list */
|
|
|
|
static void parse_options( const char *str )
|
|
|
|
{
|
|
|
|
char *opt, *next, *options;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
if (!(options = strdup(str))) return;
|
|
|
|
for (opt = options; opt; opt = next)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
unsigned char set = 0, clear = 0;
|
|
|
|
|
|
|
|
if ((next = strchr( opt, ',' ))) *next++ = 0;
|
|
|
|
|
|
|
|
p = opt + strcspn( opt, "+-" );
|
|
|
|
if (!p[0]) p = opt; /* assume it's a debug channel name */
|
|
|
|
|
|
|
|
if (p > opt)
|
|
|
|
{
|
|
|
|
for (i = 0; i < ARRAY_SIZE(debug_classes); 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 == ARRAY_SIZE(debug_classes)) /* bad class name, skip it */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*p == '-') clear = ~0;
|
|
|
|
else set = ~0;
|
|
|
|
}
|
|
|
|
if (*p == '+' || *p == '-') p++;
|
|
|
|
if (!p[0]) continue;
|
|
|
|
|
|
|
|
if (!strcmp( p, "all" ))
|
|
|
|
default_flags = (default_flags & ~clear) | set;
|
|
|
|
else
|
|
|
|
add_option( p, set, clear );
|
|
|
|
}
|
|
|
|
free( options );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the usage message */
|
|
|
|
static void debug_usage(void)
|
|
|
|
{
|
|
|
|
static const char usage[] =
|
|
|
|
"Syntax of the WINEDEBUG variable:\n"
|
|
|
|
" WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
|
|
|
|
"Example: WINEDEBUG=+relay,warn-heap\n"
|
|
|
|
" turns on relay traces, disable heap warnings\n"
|
|
|
|
"Available message classes: err, warn, fixme, trace\n";
|
|
|
|
write( 2, usage, sizeof(usage) - 1 );
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize all options at startup */
|
|
|
|
static void init_options(void)
|
|
|
|
{
|
|
|
|
char *wine_debug = getenv("WINEDEBUG");
|
|
|
|
struct stat st1, st2;
|
|
|
|
|
|
|
|
nb_debug_options = 0;
|
|
|
|
|
|
|
|
/* check for stderr pointing to /dev/null */
|
|
|
|
if (!fstat( 2, &st1 ) && S_ISCHR(st1.st_mode) &&
|
|
|
|
!stat( "/dev/null", &st2 ) && S_ISCHR(st2.st_mode) &&
|
|
|
|
st1.st_rdev == st2.st_rdev)
|
|
|
|
{
|
|
|
|
default_flags = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!wine_debug) return;
|
|
|
|
if (!strcmp( wine_debug, "help" )) debug_usage();
|
|
|
|
parse_options( wine_debug );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* __wine_dbg_get_channel_flags (NTDLL.@)
|
|
|
|
*
|
|
|
|
* Get the flags to use for a given channel, possibly setting them too in case of lazy init
|
|
|
|
*/
|
|
|
|
unsigned char __cdecl __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
|
|
|
|
{
|
|
|
|
int min, max, pos, res;
|
|
|
|
|
|
|
|
if (nb_debug_options == -1) init_options();
|
|
|
|
|
|
|
|
min = 0;
|
|
|
|
max = nb_debug_options - 1;
|
|
|
|
while (min <= max)
|
|
|
|
{
|
|
|
|
pos = (min + max) / 2;
|
|
|
|
res = strcmp( channel->name, debug_options[pos].name );
|
|
|
|
if (!res) return debug_options[pos].flags;
|
|
|
|
if (res < 0) max = pos - 1;
|
|
|
|
else min = pos + 1;
|
|
|
|
}
|
|
|
|
/* no option for this channel */
|
|
|
|
if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
|
|
|
|
return default_flags;
|
|
|
|
}
|
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
/***********************************************************************
|
2019-04-03 19:23:43 +02:00
|
|
|
* __wine_dbg_strdup (NTDLL.@)
|
2001-10-17 21:39:22 +02:00
|
|
|
*/
|
2019-04-03 19:23:43 +02:00
|
|
|
const char * __cdecl __wine_dbg_strdup( const char *str )
|
2001-10-17 21:39:22 +02:00
|
|
|
{
|
2001-10-21 17:04:04 +02:00
|
|
|
struct debug_info *info = get_info();
|
2019-04-03 14:16:29 +02:00
|
|
|
unsigned int pos = info->str_pos;
|
2019-04-03 19:23:43 +02:00
|
|
|
size_t n = strlen( str ) + 1;
|
2005-09-26 18:45:25 +02:00
|
|
|
|
2019-04-03 14:16:29 +02:00
|
|
|
assert( n <= sizeof(info->strings) );
|
|
|
|
if (pos + n > sizeof(info->strings)) pos = 0;
|
|
|
|
info->str_pos = pos + n;
|
|
|
|
return memcpy( info->strings + pos, str, n );
|
2001-10-17 21:39:22 +02:00
|
|
|
}
|
|
|
|
|
2019-04-03 19:24:39 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* __wine_dbg_output (NTDLL.@)
|
|
|
|
*/
|
|
|
|
int __cdecl __wine_dbg_output( const char *str )
|
|
|
|
{
|
|
|
|
struct debug_info *info = get_info();
|
|
|
|
const char *end = strrchr( str, '\n' );
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (end)
|
|
|
|
{
|
|
|
|
ret += append_output( info, str, end + 1 - str );
|
2019-04-03 14:16:29 +02:00
|
|
|
write( 2, info->output, info->out_pos );
|
|
|
|
info->out_pos = 0;
|
2019-04-03 19:24:39 +02:00
|
|
|
str = end + 1;
|
|
|
|
}
|
|
|
|
if (*str) ret += append_output( info, str, strlen( str ));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-04-03 19:27:13 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* __wine_dbg_header (NTDLL.@)
|
|
|
|
*/
|
|
|
|
int __cdecl __wine_dbg_header( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
|
|
|
|
const char *function )
|
|
|
|
{
|
|
|
|
static const char * const classes[] = { "fixme", "err", "warn", "trace" };
|
|
|
|
struct debug_info *info = get_info();
|
|
|
|
char buffer[200], *pos = buffer;
|
|
|
|
|
|
|
|
if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
|
|
|
|
|
|
|
|
/* only print header if we are at the beginning of the line */
|
2019-04-03 14:16:29 +02:00
|
|
|
if (info->out_pos) return 0;
|
2019-04-03 19:27:13 +02:00
|
|
|
|
|
|
|
if (init_done)
|
|
|
|
{
|
|
|
|
if (TRACE_ON(timestamp))
|
|
|
|
{
|
|
|
|
ULONG ticks = NtGetTickCount();
|
|
|
|
pos += sprintf( pos, "%3u.%03u:", ticks / 1000, ticks % 1000 );
|
|
|
|
}
|
|
|
|
if (TRACE_ON(pid)) pos += sprintf( pos, "%04x:", GetCurrentProcessId() );
|
|
|
|
pos += sprintf( pos, "%04x:", GetCurrentThreadId() );
|
|
|
|
}
|
|
|
|
if (function && cls < ARRAY_SIZE( classes ))
|
|
|
|
snprintf( pos, sizeof(buffer) - (pos - buffer), "%s:%s:%s ",
|
|
|
|
classes[cls], channel->name, function );
|
|
|
|
|
|
|
|
return append_output( info, buffer, strlen( buffer ));
|
|
|
|
}
|
|
|
|
|
2002-05-17 04:55:48 +02:00
|
|
|
/***********************************************************************
|
2020-05-17 11:07:02 +02:00
|
|
|
* dbg_init
|
2002-05-17 04:55:48 +02:00
|
|
|
*/
|
2020-05-17 11:07:02 +02:00
|
|
|
void CDECL dbg_init(void)
|
2002-05-17 04:55:48 +02:00
|
|
|
{
|
2019-12-03 19:54:56 +01:00
|
|
|
setbuf( stdout, NULL );
|
|
|
|
setbuf( stderr, NULL );
|
2019-04-03 09:50:31 +02:00
|
|
|
ntdll_get_thread_data()->debug_info = &initial_info;
|
|
|
|
init_done = TRUE;
|
2002-05-17 04:55:48 +02:00
|
|
|
}
|