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"
25 #include "wine/port.h"
36 #include "wine/debug.h"
37 #include "wine/exception.h"
38 #include "wine/library.h"
39 #include "wine/unicode.h"
46 WINE_DECLARE_DEBUG_CHANNEL(tid);
48 /* ---------------------------------------------------------------------- */
52 char *str_pos; /* current position in strings buffer */
53 char *out_pos; /* current position in output buffer */
54 char strings[1024]; /* buffer for temporary strings */
55 char output[1024]; /* current output line */
58 static struct debug_info initial_thread_info; /* debug info for initial thread */
60 /* filter for page-fault exceptions */
61 static WINE_EXCEPTION_FILTER(page_fault)
63 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
64 return EXCEPTION_EXECUTE_HANDLER;
65 return EXCEPTION_CONTINUE_SEARCH;
68 /* get the debug info pointer for the current thread */
69 static inline struct debug_info *get_info(void)
71 struct debug_info *info = NtCurrentTeb()->debug_info;
73 if (!info) NtCurrentTeb()->debug_info = info = &initial_thread_info;
76 info->str_pos = info->strings;
77 info->out_pos = info->output;
82 /* allocate some tmp space for a string */
83 static void *gimme1(int n)
85 struct debug_info *info = get_info();
86 char *res = info->str_pos;
88 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
89 info->str_pos = res + n;
93 /* release extra space that we requested in gimme1() */
94 static inline void release( void *ptr )
96 struct debug_info *info = NtCurrentTeb()->debug_info;
100 /* put an ASCII string into the debug buffer */
101 inline static char *put_string_a( const char *src, int n )
105 if (n == -1) n = strlen(src);
107 else if (n > 80) n = 80;
108 dst = res = gimme1 (n * 4 + 6);
112 unsigned char c = *src++;
115 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
116 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
117 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
118 case '"': *dst++ = '\\'; *dst++ = '"'; break;
119 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
121 if (c >= ' ' && c <= 126)
126 *dst++ = '0' + ((c >> 6) & 7);
127 *dst++ = '0' + ((c >> 3) & 7);
128 *dst++ = '0' + ((c >> 0) & 7);
144 /* put a Unicode string into the debug buffer */
145 inline static char *put_string_w( const WCHAR *src, int n )
149 if (n == -1) n = strlenW(src);
151 else if (n > 80) n = 80;
152 dst = res = gimme1 (n * 5 + 7);
160 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
161 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
162 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
163 case '"': *dst++ = '\\'; *dst++ = '"'; break;
164 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
166 if (c >= ' ' && c <= 126)
171 sprintf(dst,"%04x",c);
188 /***********************************************************************
191 static const char *NTDLL_dbgstr_an( const char *src, int n )
194 struct debug_info *info = get_info();
198 if (!src) return "(null)";
200 sprintf(res, "#%04x", LOWORD(src) );
203 /* save current position to restore it on exception */
204 old_pos = info->str_pos;
207 res = put_string_a( src, n );
218 /***********************************************************************
221 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
224 struct debug_info *info = get_info();
228 if (!src) return "(null)";
230 sprintf(res, "#%04x", LOWORD(src) );
234 /* save current position to restore it on exception */
235 old_pos = info->str_pos;
238 res = put_string_w( src, n );
249 /***********************************************************************
252 static const char *NTDLL_dbg_vsprintf( const char *format, va_list args )
254 static const int max_size = 200;
256 char *res = gimme1( max_size );
257 int len = vsnprintf( res, max_size, format, args );
258 if (len == -1 || len >= max_size) res[max_size-1] = 0;
259 else release( res + len + 1 );
263 /***********************************************************************
266 static int NTDLL_dbg_vprintf( const char *format, va_list args )
268 struct debug_info *info = get_info();
271 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
274 /* make sure we didn't exceed the buffer length
275 * the two checks are due to glibc changes in vsnprintfs return value
276 * the buffer size can be exceeded in case of a missing \n in
278 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
280 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
282 info->out_pos = info->output;
286 p = strrchr( info->out_pos, '\n' );
287 if (!p) info->out_pos += ret;
290 char *pos = info->output;
292 write( 2, pos, p - pos );
293 /* move beginning of next line to start of buffer */
294 while ((*pos = *p++)) pos++;
300 /***********************************************************************
303 static int NTDLL_dbg_vlog( unsigned int cls, const char *channel,
304 const char *function, const char *format, va_list args )
306 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
307 struct debug_info *info = get_info();
310 /* only print header if we are at the beginning of the line */
311 if (info->out_pos == info->output || info->out_pos[-1] == '\n')
314 ret = wine_dbg_printf( "%04lx:", NtCurrentTeb()->tid );
315 if (cls < sizeof(classes)/sizeof(classes[0]))
316 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
319 ret += NTDLL_dbg_vprintf( format, args );
323 /***********************************************************************
326 DECL_GLOBAL_CONSTRUCTOR(debug_init)
328 __wine_dbgstr_an = NTDLL_dbgstr_an;
329 __wine_dbgstr_wn = NTDLL_dbgstr_wn;
330 __wine_dbg_vsprintf = NTDLL_dbg_vsprintf;
331 __wine_dbg_vprintf = NTDLL_dbg_vprintf;
332 __wine_dbg_vlog = NTDLL_dbg_vlog;