4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "wine/library.h"
36 #include "wine/unicode.h"
43 #include "ntdll_misc.h"
45 WINE_DECLARE_DEBUG_CHANNEL(tid);
47 /* ---------------------------------------------------------------------- */
49 /* filter for page-fault exceptions */
50 static WINE_EXCEPTION_FILTER(page_fault)
52 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
53 return EXCEPTION_EXECUTE_HANDLER;
54 return EXCEPTION_CONTINUE_SEARCH;
57 /* get the debug info pointer for the current thread */
58 static inline struct debug_info *get_info(void)
60 return NtCurrentTeb()->debug_info;
63 /* allocate some tmp space for a string */
64 static void *gimme1(int n)
66 struct debug_info *info = get_info();
67 char *res = info->str_pos;
69 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
70 info->str_pos = res + n;
74 /* release extra space that we requested in gimme1() */
75 static inline void release( void *ptr )
77 struct debug_info *info = NtCurrentTeb()->debug_info;
81 /* put an ASCII string into the debug buffer */
82 inline static char *put_string_a( const char *src, int n )
84 static const char hex[16] = "0123456789abcdef";
88 if (n == -1) n = strlen(src);
90 size = 10 + min( 300, n * 4 );
91 dst = res = gimme1( size );
93 while (n-- > 0 && dst <= res + size - 9)
95 unsigned char c = *src++;
98 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
99 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
100 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
101 case '"': *dst++ = '\\'; *dst++ = '"'; break;
102 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
104 if (c >= ' ' && c <= 126)
110 *dst++ = hex[(c >> 4) & 0x0f];
111 *dst++ = hex[c & 0x0f];
127 /* put a Unicode string into the debug buffer */
128 inline static char *put_string_w( const WCHAR *src, int n )
133 if (n == -1) n = strlenW(src);
135 size = 12 + min( 300, n * 5 );
136 dst = res = gimme1( size );
139 while (n-- > 0 && dst <= res + size - 10)
144 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
145 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
146 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
147 case '"': *dst++ = '\\'; *dst++ = '"'; break;
148 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
150 if (c >= ' ' && c <= 126)
155 sprintf(dst,"%04x",c);
172 /***********************************************************************
175 static const char *NTDLL_dbgstr_an( const char *src, int n )
178 struct debug_info *info = get_info();
182 if (!src) return "(null)";
184 sprintf(res, "#%04x", LOWORD(src) );
187 /* save current position to restore it on exception */
188 old_pos = info->str_pos;
191 res = put_string_a( src, n );
202 /***********************************************************************
205 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
208 struct debug_info *info = get_info();
212 if (!src) return "(null)";
214 sprintf(res, "#%04x", LOWORD(src) );
218 /* save current position to restore it on exception */
219 old_pos = info->str_pos;
222 res = put_string_w( src, n );
233 /***********************************************************************
236 static const char *NTDLL_dbg_vsprintf( const char *format, va_list args )
238 static const int max_size = 200;
240 char *res = gimme1( max_size );
241 int len = vsnprintf( res, max_size, format, args );
242 if (len == -1 || len >= max_size) res[max_size-1] = 0;
243 else release( res + len + 1 );
247 /***********************************************************************
250 static int NTDLL_dbg_vprintf( const char *format, va_list args )
252 struct debug_info *info = get_info();
255 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
258 /* make sure we didn't exceed the buffer length
259 * the two checks are due to glibc changes in vsnprintfs return value
260 * the buffer size can be exceeded in case of a missing \n in
262 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
264 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
266 info->out_pos = info->output;
270 p = strrchr( info->out_pos, '\n' );
271 if (!p) info->out_pos += ret;
274 char *pos = info->output;
276 write( 2, pos, p - pos );
277 /* move beginning of next line to start of buffer */
278 while ((*pos = *p++)) pos++;
284 /***********************************************************************
287 static int NTDLL_dbg_vlog( unsigned int cls, const char *channel,
288 const char *function, const char *format, va_list args )
290 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
291 struct debug_info *info = get_info();
294 /* only print header if we are at the beginning of the line */
295 if (info->out_pos == info->output || info->out_pos[-1] == '\n')
298 ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
299 if (cls < sizeof(classes)/sizeof(classes[0]))
300 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
303 ret += NTDLL_dbg_vprintf( format, args );
307 /***********************************************************************
310 void debug_init(void)
312 extern void __wine_dbg_ntdll_init(void);
314 __wine_dbgstr_an = NTDLL_dbgstr_an;
315 __wine_dbgstr_wn = NTDLL_dbgstr_wn;
316 __wine_dbg_vsprintf = NTDLL_dbg_vsprintf;
317 __wine_dbg_vprintf = NTDLL_dbg_vprintf;
318 __wine_dbg_vlog = NTDLL_dbg_vlog;
319 __wine_dbg_ntdll_init(); /* hack: register debug channels early */