12 #include "debugtools.h"
13 #include "wine/exception.h"
19 #include "msvcrt/excpt.h"
21 DECLARE_DEBUG_CHANNEL(tid);
23 /* ---------------------------------------------------------------------- */
27 char *str_pos; /* current position in strings buffer */
28 char *out_pos; /* current position in output buffer */
29 char strings[1024]; /* buffer for temporary strings */
30 char output[1024]; /* current output line */
33 static struct debug_info initial_thread_info; /* debug info for initial thread */
35 /* filter for page-fault exceptions */
36 static WINE_EXCEPTION_FILTER(page_fault)
38 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
39 return EXCEPTION_EXECUTE_HANDLER;
40 return EXCEPTION_CONTINUE_SEARCH;
43 /* get the debug info pointer for the current thread */
44 static inline struct debug_info *get_info(void)
46 struct debug_info *info = NtCurrentTeb()->debug_info;
48 if (!info) NtCurrentTeb()->debug_info = info = &initial_thread_info;
51 info->str_pos = info->strings;
52 info->out_pos = info->output;
57 /* allocate some tmp space for a string */
58 static void *gimme1(int n)
60 struct debug_info *info = get_info();
61 char *res = info->str_pos;
63 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
64 info->str_pos = res + n;
68 /* release extra space that we requested in gimme1() */
69 static inline void release( void *ptr )
71 struct debug_info *info = NtCurrentTeb()->debug_info;
75 /* put an ASCII string into the debug buffer */
76 inline static char *put_string_a( const char *src, int n )
81 else if (n > 200) n = 200;
82 dst = res = gimme1 (n * 4 + 6);
84 while (n-- > 0 && *src)
86 unsigned char c = *src++;
89 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
90 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
91 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
92 case '"': *dst++ = '\\'; *dst++ = '"'; break;
93 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
95 if (c >= ' ' && c <= 126)
100 *dst++ = '0' + ((c >> 6) & 7);
101 *dst++ = '0' + ((c >> 3) & 7);
102 *dst++ = '0' + ((c >> 0) & 7);
118 /* put a Unicode string into the debug buffer */
119 inline static char *put_string_w( const WCHAR *src, int n )
124 else if (n > 200) n = 200;
125 dst = res = gimme1 (n * 5 + 7);
128 while (n-- > 0 && *src)
133 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
134 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
135 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
136 case '"': *dst++ = '\\'; *dst++ = '"'; break;
137 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
139 if (c >= ' ' && c <= 126)
144 sprintf(dst,"%04x",c);
161 /***********************************************************************
162 * wine_dbgstr_an (NTDLL.@)
164 const char *wine_dbgstr_an( const char *src, int n )
167 struct debug_info *info = get_info();
171 if (!src) return "(null)";
173 sprintf(res, "#%04x", LOWORD(src) );
176 /* save current position to restore it on exception */
177 old_pos = info->str_pos;
180 res = put_string_a( src, n );
191 /***********************************************************************
192 * wine_dbgstr_wn (NTDLL.@)
194 const char *wine_dbgstr_wn( const WCHAR *src, int n )
197 struct debug_info *info = get_info();
201 if (!src) return "(null)";
203 sprintf(res, "#%04x", LOWORD(src) );
207 /* save current position to restore it on exception */
208 old_pos = info->str_pos;
211 res = put_string_w( src, n );
222 /***********************************************************************
223 * wine_dbgstr_guid (NTDLL.@)
225 const char *wine_dbgstr_guid( const GUID *id )
229 if (!id) return "(null)";
233 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
238 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
239 id->Data1, id->Data2, id->Data3,
240 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
241 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
246 /***********************************************************************
247 * wine_dbg_vprintf (NTDLL.@)
249 int wine_dbg_vprintf( const char *format, va_list args )
251 struct debug_info *info = get_info();
254 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
257 /* make sure we didn't exceed the buffer length
258 * the two checks are due to glibc changes in vsnprintfs return value
259 * the buffer size can be exceeded in case of a missing \n in
261 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
263 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
265 info->out_pos = info->output;
269 p = strrchr( info->out_pos, '\n' );
270 if (!p) info->out_pos += ret;
273 char *pos = info->output;
275 write( 2, pos, p - pos );
276 /* move beginning of next line to start of buffer */
277 while ((*pos = *p++)) pos++;
283 /***********************************************************************
284 * wine_dbg_printf (NTDLL.@)
286 int wine_dbg_printf(const char *format, ...)
291 va_start(valist, format);
292 ret = wine_dbg_vprintf( format, valist );
297 /***********************************************************************
298 * wine_dbg_log (NTDLL.@)
300 int wine_dbg_log(enum __WINE_DEBUG_CLASS cls, const char *channel,
301 const char *function, const char *format, ... )
303 static const char *classes[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
307 va_start(valist, format);
309 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
310 if (cls < __WINE_DBCL_COUNT)
311 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
313 ret += wine_dbg_vprintf( format, valist );