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
|
|
|
*/
|
|
|
|
|
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
|
|
|
#include <ctype.h>
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
2001-10-17 21:39:22 +02:00
|
|
|
#include "wine/exception.h"
|
2002-05-20 00:29:01 +02:00
|
|
|
#include "wine/library.h"
|
2002-05-17 04:55:48 +02:00
|
|
|
#include "wine/unicode.h"
|
2000-11-05 05:51:34 +01:00
|
|
|
#include "winnt.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.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
|
|
|
|
2005-09-26 18:45:25 +02:00
|
|
|
static struct __wine_debug_functions default_funcs;
|
|
|
|
|
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)
|
|
|
|
{
|
2005-06-06 22:04:33 +02:00
|
|
|
return ntdll_get_thread_data()->debug_info;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate some tmp space for a string */
|
2005-09-26 18:45:25 +02:00
|
|
|
static char *get_temp_buffer( size_t n )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
struct debug_info *info = get_info();
|
|
|
|
char *res = info->str_pos;
|
|
|
|
|
|
|
|
if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
|
|
|
|
info->str_pos = res + n;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-01-17 02:05:37 +01:00
|
|
|
/* release extra space that we requested in get_temp_buffer() */
|
2005-09-26 18:45:25 +02:00
|
|
|
static void release_temp_buffer( char *ptr, size_t size )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
2005-06-06 22:04:33 +02:00
|
|
|
struct debug_info *info = get_info();
|
2005-09-26 18:45:25 +02:00
|
|
|
info->str_pos = ptr + size;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
/***********************************************************************
|
2002-05-17 04:55:48 +02:00
|
|
|
* NTDLL_dbgstr_an
|
2001-10-17 21:39:22 +02:00
|
|
|
*/
|
2002-05-17 04:55:48 +02:00
|
|
|
static const char *NTDLL_dbgstr_an( const char *src, int n )
|
2001-10-17 21:39:22 +02:00
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
const char *res;
|
2001-10-21 17:04:04 +02:00
|
|
|
struct debug_info *info = get_info();
|
2001-10-17 21:39:22 +02:00
|
|
|
/* save current position to restore it on exception */
|
2005-09-26 18:45:25 +02:00
|
|
|
char *old_pos = info->str_pos;
|
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
__TRY
|
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
res = default_funcs.dbgstr_an( src, n );
|
2001-10-17 21:39:22 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2001-10-17 21:39:22 +02:00
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
release_temp_buffer( old_pos, 0 );
|
2001-10-17 21:39:22 +02:00
|
|
|
return "(invalid)";
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
2002-05-17 04:55:48 +02:00
|
|
|
* NTDLL_dbgstr_wn
|
2001-10-17 21:39:22 +02:00
|
|
|
*/
|
2002-05-17 04:55:48 +02:00
|
|
|
static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
|
2001-10-17 21:39:22 +02:00
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
const char *res;
|
2001-10-21 17:04:04 +02:00
|
|
|
struct debug_info *info = get_info();
|
2001-10-17 21:39:22 +02:00
|
|
|
/* save current position to restore it on exception */
|
2005-09-26 18:45:25 +02:00
|
|
|
char *old_pos = info->str_pos;
|
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
__TRY
|
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
res = default_funcs.dbgstr_wn( src, n );
|
2001-10-17 21:39:22 +02:00
|
|
|
}
|
2005-12-16 17:17:57 +01:00
|
|
|
__EXCEPT_PAGE_FAULT
|
2001-10-17 21:39:22 +02:00
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
release_temp_buffer( old_pos, 0 );
|
2001-10-17 21:39:22 +02:00
|
|
|
return "(invalid)";
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-11-25 04:09:30 +01:00
|
|
|
/***********************************************************************
|
2002-05-17 04:55:48 +02:00
|
|
|
* NTDLL_dbg_vprintf
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2002-05-17 04:55:48 +02:00
|
|
|
static int NTDLL_dbg_vprintf( const char *format, va_list args )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
struct debug_info *info = get_info();
|
2007-01-24 12:46:17 +01:00
|
|
|
int end;
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2001-02-16 20:52:50 +01:00
|
|
|
int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
|
|
|
|
format, args );
|
|
|
|
|
|
|
|
/* make sure we didn't exceed the buffer length
|
2001-07-17 02:56:37 +02:00
|
|
|
* the two checks are due to glibc changes in vsnprintfs return value
|
|
|
|
* the buffer size can be exceeded in case of a missing \n in
|
|
|
|
* debug output */
|
|
|
|
if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
|
|
|
|
{
|
|
|
|
fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
|
|
|
|
info->output);
|
|
|
|
info->out_pos = info->output;
|
|
|
|
abort();
|
|
|
|
}
|
2001-02-16 20:52:50 +01:00
|
|
|
|
2007-01-24 12:46:17 +01:00
|
|
|
for (end = ret; end > 0; end--) if (info->out_pos[end - 1] == '\n') break;
|
|
|
|
|
|
|
|
if (!end) info->out_pos += ret;
|
2000-11-05 05:51:34 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *pos = info->output;
|
2007-01-24 12:46:17 +01:00
|
|
|
write( 2, pos, info->out_pos + end - pos );
|
2000-11-05 05:51:34 +01:00
|
|
|
/* move beginning of next line to start of buffer */
|
2007-01-24 12:46:17 +01:00
|
|
|
memmove( pos, info->out_pos + end, ret - end );
|
|
|
|
info->out_pos = pos + ret - end;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-11-25 04:09:30 +01:00
|
|
|
/***********************************************************************
|
2002-05-17 04:55:48 +02:00
|
|
|
* NTDLL_dbg_vlog
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2005-09-23 12:52:07 +02:00
|
|
|
static int NTDLL_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
|
2002-05-17 04:55:48 +02:00
|
|
|
const char *function, const char *format, va_list args )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
2003-03-14 05:02:03 +01:00
|
|
|
static const char * const classes[] = { "fixme", "err", "warn", "trace" };
|
|
|
|
struct debug_info *info = get_info();
|
2000-11-11 00:31:06 +01:00
|
|
|
int ret = 0;
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2003-03-14 05:02:03 +01:00
|
|
|
/* only print header if we are at the beginning of the line */
|
|
|
|
if (info->out_pos == info->output || info->out_pos[-1] == '\n')
|
|
|
|
{
|
2011-09-01 09:21:06 +02:00
|
|
|
if (TRACE_ON(timestamp))
|
|
|
|
{
|
|
|
|
ULONG ticks = NtGetTickCount();
|
|
|
|
ret = wine_dbg_printf( "%3u.%03u:", ticks / 1000, ticks % 1000 );
|
|
|
|
}
|
2015-08-25 12:31:21 +02:00
|
|
|
if (TRACE_ON(pid))
|
|
|
|
ret += wine_dbg_printf( "%04x:", GetCurrentProcessId() );
|
2017-11-25 11:42:28 +01:00
|
|
|
ret += wine_dbg_printf( "%04x:", GetCurrentThreadId() );
|
2017-11-25 11:45:20 +01:00
|
|
|
if (*format == '\1') /* special magic to avoid standard prefix */
|
|
|
|
format++;
|
|
|
|
else if (cls < sizeof(classes)/sizeof(classes[0]))
|
2005-09-23 12:52:07 +02:00
|
|
|
ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel->name, function );
|
2003-03-14 05:02:03 +01:00
|
|
|
}
|
2000-11-11 00:31:06 +01:00
|
|
|
if (format)
|
2002-05-17 04:55:48 +02:00
|
|
|
ret += NTDLL_dbg_vprintf( format, args );
|
2000-11-11 00:31:06 +01:00
|
|
|
return ret;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
2002-05-17 04:55:48 +02:00
|
|
|
|
2005-09-23 12:52:07 +02:00
|
|
|
|
|
|
|
static const struct __wine_debug_functions funcs =
|
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
get_temp_buffer,
|
|
|
|
release_temp_buffer,
|
2005-09-23 12:52:07 +02:00
|
|
|
NTDLL_dbgstr_an,
|
|
|
|
NTDLL_dbgstr_wn,
|
|
|
|
NTDLL_dbg_vprintf,
|
|
|
|
NTDLL_dbg_vlog
|
|
|
|
};
|
|
|
|
|
2002-05-17 04:55:48 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* debug_init
|
|
|
|
*/
|
2004-01-07 05:50:11 +01:00
|
|
|
void debug_init(void)
|
2002-05-17 04:55:48 +02:00
|
|
|
{
|
2005-09-26 18:45:25 +02:00
|
|
|
__wine_dbg_set_functions( &funcs, &default_funcs, sizeof(funcs) );
|
2002-05-17 04:55:48 +02:00
|
|
|
}
|