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
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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"
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "ntstatus.h"
|
2000-11-05 05:51:34 +01:00
|
|
|
#include "thread.h"
|
|
|
|
#include "winbase.h"
|
|
|
|
#include "winnt.h"
|
2002-09-13 00:07:02 +02:00
|
|
|
#include "winternl.h"
|
2002-12-13 00:34:01 +01:00
|
|
|
#include "excpt.h"
|
2003-10-09 00:34:37 +02:00
|
|
|
#include "ntdll_misc.h"
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DECLARE_DEBUG_CHANNEL(tid);
|
2001-05-08 02:13:38 +02:00
|
|
|
|
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)
|
|
|
|
{
|
2003-08-21 23:34:33 +02:00
|
|
|
return NtCurrentTeb()->debug_info;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
{
|
2003-06-27 21:02:23 +02:00
|
|
|
static const char hex[16] = "0123456789abcdef";
|
2000-11-05 05:51:34 +01:00
|
|
|
char *dst, *res;
|
|
|
|
|
2002-05-17 04:55:48 +02:00
|
|
|
if (n == -1) n = strlen(src);
|
2000-11-05 05:51:34 +01:00
|
|
|
if (n < 0) n = 0;
|
2002-11-13 20:38:17 +01:00
|
|
|
else if (n > 80) n = 80;
|
2000-11-05 05:51:34 +01:00
|
|
|
dst = res = gimme1 (n * 4 + 6);
|
|
|
|
*dst++ = '"';
|
2002-05-17 04:55:48 +02:00
|
|
|
while (n-- > 0)
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
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++ = '\\';
|
2003-06-27 21:02:23 +02:00
|
|
|
*dst++ = 'x';
|
|
|
|
*dst++ = hex[(c >> 4) & 0x0f];
|
|
|
|
*dst++ = hex[c & 0x0f];
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*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;
|
|
|
|
|
2002-05-17 04:55:48 +02:00
|
|
|
if (n == -1) n = strlenW(src);
|
2000-11-05 05:51:34 +01:00
|
|
|
if (n < 0) n = 0;
|
2002-11-13 20:38:17 +01:00
|
|
|
else if (n > 80) n = 80;
|
2000-11-05 05:51:34 +01:00
|
|
|
dst = res = gimme1 (n * 5 + 7);
|
|
|
|
*dst++ = 'L';
|
|
|
|
*dst++ = '"';
|
2002-05-17 04:55:48 +02:00
|
|
|
while (n-- > 0)
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
|
|
|
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
|
|
|
/***********************************************************************
|
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
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
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
|
|
|
{
|
|
|
|
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
|
|
|
/***********************************************************************
|
2003-01-03 04:12:58 +01:00
|
|
|
* NTDLL_dbg_vsprintf
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2003-01-03 04:12:58 +01:00
|
|
|
static const char *NTDLL_dbg_vsprintf( const char *format, va_list args )
|
2000-11-05 05:51:34 +01:00
|
|
|
{
|
2003-01-03 04:12:58 +01:00
|
|
|
static const int max_size = 200;
|
2000-11-05 05:51:34 +01:00
|
|
|
|
2003-01-03 04:12:58 +01:00
|
|
|
char *res = gimme1( max_size );
|
|
|
|
int len = vsnprintf( res, max_size, format, args );
|
|
|
|
if (len == -1 || len >= max_size) res[max_size-1] = 0;
|
|
|
|
else release( res + len + 1 );
|
|
|
|
return res;
|
2000-11-05 05:51:34 +01:00
|
|
|
}
|
|
|
|
|
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();
|
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
|
|
|
/***********************************************************************
|
2002-05-17 04:55:48 +02:00
|
|
|
* NTDLL_dbg_vlog
|
2000-11-25 04:09:30 +01:00
|
|
|
*/
|
2003-01-03 04:12:58 +01:00
|
|
|
static int NTDLL_dbg_vlog( unsigned int cls, const char *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')
|
|
|
|
{
|
|
|
|
if (TRACE_ON(tid))
|
2003-08-28 01:14:29 +02:00
|
|
|
ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
|
2003-03-14 05:02:03 +01:00
|
|
|
if (cls < sizeof(classes)/sizeof(classes[0]))
|
|
|
|
ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
|
|
|
|
}
|
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
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* debug_init
|
|
|
|
*/
|
|
|
|
DECL_GLOBAL_CONSTRUCTOR(debug_init)
|
|
|
|
{
|
2003-01-03 04:12:58 +01:00
|
|
|
__wine_dbgstr_an = NTDLL_dbgstr_an;
|
|
|
|
__wine_dbgstr_wn = NTDLL_dbgstr_wn;
|
|
|
|
__wine_dbg_vsprintf = NTDLL_dbg_vsprintf;
|
|
|
|
__wine_dbg_vprintf = NTDLL_dbg_vprintf;
|
|
|
|
__wine_dbg_vlog = NTDLL_dbg_vlog;
|
2002-05-17 04:55:48 +02:00
|
|
|
}
|