2000-11-05 05:51:34 +01:00
|
|
|
/*
|
|
|
|
* Debugging functions
|
|
|
|
*/
|
|
|
|
|
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>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "debugtools.h"
|
2001-10-17 21:39:22 +02:00
|
|
|
#include "wine/exception.h"
|
2000-11-05 05:51:34 +01:00
|
|
|
#include "thread.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnt.h"
|
2001-12-19 20:16:27 +01:00
|
|
|
#include "ntddk.h"
|
2000-11-05 05:51:34 +01:00
|
|
|
#include "wtypes.h"
|
2002-02-25 21:10:35 +01:00
|
|
|
#include "msvcrt/excpt.h"
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2001-05-08 02:13:38 +02:00
|
|
|
DECLARE_DEBUG_CHANNEL(tid);
|
|
|
|
|
2000-11-05 05:51:34 +01:00
|
|
|
/* ---------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
struct debug_info
|
|
|
|
{
|
|
|
|
char *str_pos; /* current position in strings buffer */
|
|
|
|
char *out_pos; /* current position in output buffer */
|
2001-03-22 00:54:59 +01:00
|
|
|
char strings[1024]; /* buffer for temporary strings */
|
|
|
|
char output[1024]; /* current output line */
|
2000-11-05 05:51:34 +01:00
|
|
|
};
|
|
|
|
|
2002-01-07 19:03:14 +01:00
|
|
|
static struct debug_info initial_thread_info; /* debug info for initial thread */
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
/* filter for page-fault exceptions */
|
|
|
|
static WINE_EXCEPTION_FILTER(page_fault)
|
|
|
|
{
|
|
|
|
if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
|
|
|
|
return EXCEPTION_EXECUTE_HANDLER;
|
|
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct debug_info *info = NtCurrentTeb()->debug_info;
|
2002-01-07 19:03:14 +01:00
|
|
|
|
|
|
|
if (!info) NtCurrentTeb()->debug_info = info = &initial_thread_info;
|
|
|
|
if (!info->str_pos)
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
info->str_pos = info->strings;
|
|
|
|
info->out_pos = info->output;
|
|
|
|
}
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate some tmp space for a string */
|
|
|
|
static void *gimme1(int n)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* release extra space that we requested in gimme1() */
|
|
|
|
static inline void release( void *ptr )
|
|
|
|
{
|
|
|
|
struct debug_info *info = NtCurrentTeb()->debug_info;
|
|
|
|
info->str_pos = ptr;
|
|
|
|
}
|
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
/* put an ASCII string into the debug buffer */
|
|
|
|
inline static char *put_string_a( const char *src, int n )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
char *dst, *res;
|
|
|
|
|
|
|
|
if (n < 0) n = 0;
|
2001-06-08 22:19:28 +02:00
|
|
|
else if (n > 200) n = 200;
|
2000-11-05 05:51:34 +01:00
|
|
|
dst = res = gimme1 (n * 4 + 6);
|
|
|
|
*dst++ = '"';
|
|
|
|
while (n-- > 0 && *src)
|
|
|
|
{
|
|
|
|
unsigned char c = *src++;
|
|
|
|
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 (*src)
|
|
|
|
{
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
}
|
|
|
|
*dst++ = '\0';
|
|
|
|
release( dst );
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
/* put a Unicode string into the debug buffer */
|
|
|
|
inline static char *put_string_w( const WCHAR *src, int n )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
char *dst, *res;
|
|
|
|
|
|
|
|
if (n < 0) n = 0;
|
2001-06-08 22:19:28 +02:00
|
|
|
else if (n > 200) n = 200;
|
2000-11-05 05:51:34 +01:00
|
|
|
dst = res = gimme1 (n * 5 + 7);
|
|
|
|
*dst++ = 'L';
|
|
|
|
*dst++ = '"';
|
|
|
|
while (n-- > 0 && *src)
|
|
|
|
{
|
|
|
|
WCHAR c = *src++;
|
|
|
|
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 (*src)
|
|
|
|
{
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
*dst++ = '.';
|
|
|
|
}
|
|
|
|
*dst++ = '\0';
|
|
|
|
release( dst );
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-10-17 21:39:22 +02:00
|
|
|
/***********************************************************************
|
|
|
|
* wine_dbgstr_an (NTDLL.@)
|
|
|
|
*/
|
|
|
|
const char *wine_dbgstr_an( const char *src, int n )
|
|
|
|
{
|
|
|
|
char *res, *old_pos;
|
2001-10-21 17:04:04 +02:00
|
|
|
struct debug_info *info = get_info();
|
2001-10-17 21:39:22 +02:00
|
|
|
|
|
|
|
if (!HIWORD(src))
|
|
|
|
{
|
|
|
|
if (!src) return "(null)";
|
|
|
|
res = gimme1(6);
|
|
|
|
sprintf(res, "#%04x", LOWORD(src) );
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
/* save current position to restore it on exception */
|
|
|
|
old_pos = info->str_pos;
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
res = put_string_a( src, n );
|
|
|
|
}
|
|
|
|
__EXCEPT(page_fault)
|
|
|
|
{
|
|
|
|
release( old_pos );
|
|
|
|
return "(invalid)";
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* wine_dbgstr_wn (NTDLL.@)
|
|
|
|
*/
|
|
|
|
const char *wine_dbgstr_wn( const WCHAR *src, int n )
|
|
|
|
{
|
|
|
|
char *res, *old_pos;
|
2001-10-21 17:04:04 +02:00
|
|
|
struct debug_info *info = get_info();
|
2001-10-17 21:39:22 +02:00
|
|
|
|
|
|
|
if (!HIWORD(src))
|
|
|
|
{
|
|
|
|
if (!src) return "(null)";
|
|
|
|
res = gimme1(6);
|
|
|
|
sprintf(res, "#%04x", LOWORD(src) );
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* save current position to restore it on exception */
|
|
|
|
old_pos = info->str_pos;
|
|
|
|
__TRY
|
|
|
|
{
|
|
|
|
res = put_string_w( src, n );
|
|
|
|
}
|
|
|
|
__EXCEPT(page_fault)
|
|
|
|
{
|
|
|
|
release( old_pos );
|
|
|
|
return "(invalid)";
|
|
|
|
}
|
|
|
|
__ENDTRY
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-11-25 04:09:30 +01:00
|
|
|
/***********************************************************************
|
2001-06-28 20:04:41 +02:00
|
|
|
* wine_dbgstr_guid (NTDLL.@)
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2000-11-11 00:31:06 +01:00
|
|
|
const char *wine_dbgstr_guid( const GUID *id )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
if (!id) return "(null)";
|
|
|
|
if (!HIWORD(id))
|
|
|
|
{
|
|
|
|
str = gimme1(12);
|
|
|
|
sprintf( str, "<guid-0x%04x>", LOWORD(id) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
str = gimme1(40);
|
|
|
|
sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
|
|
|
|
id->Data1, id->Data2, id->Data3,
|
|
|
|
id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
|
|
|
|
id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2000-11-25 04:09:30 +01:00
|
|
|
/***********************************************************************
|
2001-06-28 20:04:41 +02:00
|
|
|
* wine_dbg_vprintf (NTDLL.@)
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2000-11-05 05:51:34 +01:00
|
|
|
int wine_dbg_vprintf( const char *format, va_list args )
|
|
|
|
{
|
|
|
|
struct debug_info *info = get_info();
|
2001-02-16 20:52:50 +01:00
|
|
|
char *p;
|
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
|
|
|
|
|
|
|
p = strrchr( info->out_pos, '\n' );
|
2000-11-05 05:51:34 +01:00
|
|
|
if (!p) info->out_pos += ret;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *pos = info->output;
|
|
|
|
p++;
|
|
|
|
write( 2, pos, p - pos );
|
|
|
|
/* move beginning of next line to start of buffer */
|
|
|
|
while ((*pos = *p++)) pos++;
|
|
|
|
info->out_pos = pos;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-11-25 04:09:30 +01:00
|
|
|
/***********************************************************************
|
2001-06-28 20:04:41 +02:00
|
|
|
* wine_dbg_printf (NTDLL.@)
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2000-11-05 05:51:34 +01:00
|
|
|
int wine_dbg_printf(const char *format, ...)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list valist;
|
|
|
|
|
|
|
|
va_start(valist, format);
|
2000-11-11 00:31:06 +01:00
|
|
|
ret = wine_dbg_vprintf( format, valist );
|
2000-11-05 05:51:34 +01:00
|
|
|
va_end(valist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-11-25 04:09:30 +01:00
|
|
|
/***********************************************************************
|
2001-06-28 20:04:41 +02:00
|
|
|
* wine_dbg_log (NTDLL.@)
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2002-01-22 00:39:36 +01:00
|
|
|
int wine_dbg_log(enum __WINE_DEBUG_CLASS cls, const char *channel,
|
2000-11-11 00:31:06 +01:00
|
|
|
const char *function, const char *format, ... )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
2002-01-22 00:39:36 +01:00
|
|
|
static const char *classes[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
|
2000-11-11 00:31:06 +01:00
|
|
|
va_list valist;
|
|
|
|
int ret = 0;
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2000-11-11 00:31:06 +01:00
|
|
|
va_start(valist, format);
|
2001-05-08 02:13:38 +02:00
|
|
|
if (TRACE_ON(tid))
|
|
|
|
ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
|
2002-01-22 00:39:36 +01:00
|
|
|
if (cls < __WINE_DBCL_COUNT)
|
2001-05-08 02:13:38 +02:00
|
|
|
ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
|
2000-11-11 00:31:06 +01:00
|
|
|
if (format)
|
|
|
|
ret += wine_dbg_vprintf( format, valist );
|
|
|
|
va_end(valist);
|
|
|
|
return ret;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|