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
28 #include "wine/debug.h"
29 #include "wine/exception.h"
30 #include "wine/library.h"
31 #include "wine/unicode.h"
36 #include "msvcrt/excpt.h"
38 WINE_DECLARE_DEBUG_CHANNEL(tid);
40 /* ---------------------------------------------------------------------- */
44 char *str_pos; /* current position in strings buffer */
45 char *out_pos; /* current position in output buffer */
46 char strings[1024]; /* buffer for temporary strings */
47 char output[1024]; /* current output line */
50 static struct debug_info initial_thread_info; /* debug info for initial thread */
52 /* filter for page-fault exceptions */
53 static WINE_EXCEPTION_FILTER(page_fault)
55 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
56 return EXCEPTION_EXECUTE_HANDLER;
57 return EXCEPTION_CONTINUE_SEARCH;
60 /* get the debug info pointer for the current thread */
61 static inline struct debug_info *get_info(void)
63 struct debug_info *info = NtCurrentTeb()->debug_info;
65 if (!info) NtCurrentTeb()->debug_info = info = &initial_thread_info;
68 info->str_pos = info->strings;
69 info->out_pos = info->output;
74 /* allocate some tmp space for a string */
75 static void *gimme1(int n)
77 struct debug_info *info = get_info();
78 char *res = info->str_pos;
80 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
81 info->str_pos = res + n;
85 /* release extra space that we requested in gimme1() */
86 static inline void release( void *ptr )
88 struct debug_info *info = NtCurrentTeb()->debug_info;
92 /* put an ASCII string into the debug buffer */
93 inline static char *put_string_a( const char *src, int n )
97 if (n == -1) n = strlen(src);
99 else if (n > 200) n = 200;
100 dst = res = gimme1 (n * 4 + 6);
104 unsigned char c = *src++;
107 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
108 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
109 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
110 case '"': *dst++ = '\\'; *dst++ = '"'; break;
111 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
113 if (c >= ' ' && c <= 126)
118 *dst++ = '0' + ((c >> 6) & 7);
119 *dst++ = '0' + ((c >> 3) & 7);
120 *dst++ = '0' + ((c >> 0) & 7);
136 /* put a Unicode string into the debug buffer */
137 inline static char *put_string_w( const WCHAR *src, int n )
141 if (n == -1) n = strlenW(src);
143 else if (n > 200) n = 200;
144 dst = res = gimme1 (n * 5 + 7);
152 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
153 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
154 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
155 case '"': *dst++ = '\\'; *dst++ = '"'; break;
156 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
158 if (c >= ' ' && c <= 126)
163 sprintf(dst,"%04x",c);
180 /***********************************************************************
183 static const char *NTDLL_dbgstr_an( const char *src, int n )
186 struct debug_info *info = get_info();
190 if (!src) return "(null)";
192 sprintf(res, "#%04x", LOWORD(src) );
195 /* save current position to restore it on exception */
196 old_pos = info->str_pos;
199 res = put_string_a( src, n );
210 /***********************************************************************
213 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
216 struct debug_info *info = get_info();
220 if (!src) return "(null)";
222 sprintf(res, "#%04x", LOWORD(src) );
226 /* save current position to restore it on exception */
227 old_pos = info->str_pos;
230 res = put_string_w( src, n );
241 /***********************************************************************
244 static const char *NTDLL_dbgstr_guid( const GUID *id )
248 if (!id) return "(null)";
252 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
257 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
258 id->Data1, id->Data2, id->Data3,
259 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
260 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
265 /***********************************************************************
268 static int NTDLL_dbg_vprintf( const char *format, va_list args )
270 struct debug_info *info = get_info();
273 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
276 /* make sure we didn't exceed the buffer length
277 * the two checks are due to glibc changes in vsnprintfs return value
278 * the buffer size can be exceeded in case of a missing \n in
280 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
282 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
284 info->out_pos = info->output;
288 p = strrchr( info->out_pos, '\n' );
289 if (!p) info->out_pos += ret;
292 char *pos = info->output;
294 write( 2, pos, p - pos );
295 /* move beginning of next line to start of buffer */
296 while ((*pos = *p++)) pos++;
302 /***********************************************************************
305 static int NTDLL_dbg_vlog( int cls, const char *channel,
306 const char *function, const char *format, va_list args )
308 static const char *classes[] = { "fixme", "err", "warn", "trace" };
312 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
313 if (cls < sizeof(classes)/sizeof(classes[0]))
314 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
316 ret += NTDLL_dbg_vprintf( format, args );
320 /***********************************************************************
323 DECL_GLOBAL_CONSTRUCTOR(debug_init)
325 __wine_dbgstr_an = NTDLL_dbgstr_an;
326 __wine_dbgstr_wn = NTDLL_dbgstr_wn;
327 __wine_dbgstr_guid = NTDLL_dbgstr_guid;
328 __wine_dbg_vprintf = NTDLL_dbg_vprintf;
329 __wine_dbg_vlog = NTDLL_dbg_vlog;