12 #include "debugtools.h"
13 #include "wine/exception.h"
20 DECLARE_DEBUG_CHANNEL(tid);
22 /* ---------------------------------------------------------------------- */
26 char *str_pos; /* current position in strings buffer */
27 char *out_pos; /* current position in output buffer */
28 char strings[1024]; /* buffer for temporary strings */
29 char output[1024]; /* current output line */
32 static struct debug_info initial_thread_info; /* debug info for initial thread */
34 /* filter for page-fault exceptions */
35 static WINE_EXCEPTION_FILTER(page_fault)
37 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
38 return EXCEPTION_EXECUTE_HANDLER;
39 return EXCEPTION_CONTINUE_SEARCH;
42 /* get the debug info pointer for the current thread */
43 static inline struct debug_info *get_info(void)
45 struct debug_info *info = NtCurrentTeb()->debug_info;
47 if (!info) NtCurrentTeb()->debug_info = info = &initial_thread_info;
50 info->str_pos = info->strings;
51 info->out_pos = info->output;
56 /* allocate some tmp space for a string */
57 static void *gimme1(int n)
59 struct debug_info *info = get_info();
60 char *res = info->str_pos;
62 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
63 info->str_pos = res + n;
67 /* release extra space that we requested in gimme1() */
68 static inline void release( void *ptr )
70 struct debug_info *info = NtCurrentTeb()->debug_info;
74 /* put an ASCII string into the debug buffer */
75 inline static char *put_string_a( const char *src, int n )
80 else if (n > 200) n = 200;
81 dst = res = gimme1 (n * 4 + 6);
83 while (n-- > 0 && *src)
85 unsigned char c = *src++;
88 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
89 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
90 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
91 case '"': *dst++ = '\\'; *dst++ = '"'; break;
92 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
94 if (c >= ' ' && c <= 126)
99 *dst++ = '0' + ((c >> 6) & 7);
100 *dst++ = '0' + ((c >> 3) & 7);
101 *dst++ = '0' + ((c >> 0) & 7);
117 /* put a Unicode string into the debug buffer */
118 inline static char *put_string_w( const WCHAR *src, int n )
123 else if (n > 200) n = 200;
124 dst = res = gimme1 (n * 5 + 7);
127 while (n-- > 0 && *src)
132 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
133 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
134 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
135 case '"': *dst++ = '\\'; *dst++ = '"'; break;
136 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
138 if (c >= ' ' && c <= 126)
143 sprintf(dst,"%04x",c);
160 /***********************************************************************
161 * wine_dbgstr_an (NTDLL.@)
163 const char *wine_dbgstr_an( const char *src, int n )
166 struct debug_info *info = get_info();
170 if (!src) return "(null)";
172 sprintf(res, "#%04x", LOWORD(src) );
175 /* save current position to restore it on exception */
176 old_pos = info->str_pos;
179 res = put_string_a( src, n );
190 /***********************************************************************
191 * wine_dbgstr_wn (NTDLL.@)
193 const char *wine_dbgstr_wn( const WCHAR *src, int n )
196 struct debug_info *info = get_info();
200 if (!src) return "(null)";
202 sprintf(res, "#%04x", LOWORD(src) );
206 /* save current position to restore it on exception */
207 old_pos = info->str_pos;
210 res = put_string_w( src, n );
221 /***********************************************************************
222 * wine_dbgstr_guid (NTDLL.@)
224 const char *wine_dbgstr_guid( const GUID *id )
228 if (!id) return "(null)";
232 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
237 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
238 id->Data1, id->Data2, id->Data3,
239 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
240 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
245 /***********************************************************************
246 * wine_dbg_vprintf (NTDLL.@)
248 int wine_dbg_vprintf( const char *format, va_list args )
250 struct debug_info *info = get_info();
253 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
256 /* make sure we didn't exceed the buffer length
257 * the two checks are due to glibc changes in vsnprintfs return value
258 * the buffer size can be exceeded in case of a missing \n in
260 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
262 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
264 info->out_pos = info->output;
268 p = strrchr( info->out_pos, '\n' );
269 if (!p) info->out_pos += ret;
272 char *pos = info->output;
274 write( 2, pos, p - pos );
275 /* move beginning of next line to start of buffer */
276 while ((*pos = *p++)) pos++;
282 /***********************************************************************
283 * wine_dbg_printf (NTDLL.@)
285 int wine_dbg_printf(const char *format, ...)
290 va_start(valist, format);
291 ret = wine_dbg_vprintf( format, valist );
296 /***********************************************************************
297 * wine_dbg_log (NTDLL.@)
299 int wine_dbg_log(enum __WINE_DEBUG_CLASS cls, const char *channel,
300 const char *function, const char *format, ... )
302 static const char *classes[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
306 va_start(valist, format);
308 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
309 if (cls < __WINE_DBCL_COUNT)
310 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
312 ret += wine_dbg_vprintf( format, valist );