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 tmp;
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;
50 tmp.str_pos = tmp.strings;
51 tmp.out_pos = tmp.output;
53 if (!GetProcessHeap()) return &tmp;
54 /* setup the temp structure in case RtlAllocateHeap wants to print something */
55 NtCurrentTeb()->debug_info = &tmp;
56 info = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*info) );
57 info->str_pos = info->strings;
58 info->out_pos = info->output;
59 NtCurrentTeb()->debug_info = info;
64 /* allocate some tmp space for a string */
65 static void *gimme1(int n)
67 struct debug_info *info = get_info();
68 char *res = info->str_pos;
70 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
71 info->str_pos = res + n;
75 /* release extra space that we requested in gimme1() */
76 static inline void release( void *ptr )
78 struct debug_info *info = NtCurrentTeb()->debug_info;
82 /* put an ASCII string into the debug buffer */
83 inline static char *put_string_a( const char *src, int n )
88 else if (n > 200) n = 200;
89 dst = res = gimme1 (n * 4 + 6);
91 while (n-- > 0 && *src)
93 unsigned char c = *src++;
96 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
97 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
98 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
99 case '"': *dst++ = '\\'; *dst++ = '"'; break;
100 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
102 if (c >= ' ' && c <= 126)
107 *dst++ = '0' + ((c >> 6) & 7);
108 *dst++ = '0' + ((c >> 3) & 7);
109 *dst++ = '0' + ((c >> 0) & 7);
125 /* put a Unicode string into the debug buffer */
126 inline static char *put_string_w( const WCHAR *src, int n )
131 else if (n > 200) n = 200;
132 dst = res = gimme1 (n * 5 + 7);
135 while (n-- > 0 && *src)
140 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
141 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
142 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
143 case '"': *dst++ = '\\'; *dst++ = '"'; break;
144 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
146 if (c >= ' ' && c <= 126)
151 sprintf(dst,"%04x",c);
168 /***********************************************************************
169 * wine_dbgstr_an (NTDLL.@)
171 const char *wine_dbgstr_an( const char *src, int n )
174 struct debug_info *info = get_info();
178 if (!src) return "(null)";
180 sprintf(res, "#%04x", LOWORD(src) );
183 /* save current position to restore it on exception */
184 old_pos = info->str_pos;
187 res = put_string_a( src, n );
198 /***********************************************************************
199 * wine_dbgstr_wn (NTDLL.@)
201 const char *wine_dbgstr_wn( const WCHAR *src, int n )
204 struct debug_info *info = get_info();
208 if (!src) return "(null)";
210 sprintf(res, "#%04x", LOWORD(src) );
214 /* save current position to restore it on exception */
215 old_pos = info->str_pos;
218 res = put_string_w( src, n );
229 /***********************************************************************
230 * wine_dbgstr_guid (NTDLL.@)
232 const char *wine_dbgstr_guid( const GUID *id )
236 if (!id) return "(null)";
240 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
245 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
246 id->Data1, id->Data2, id->Data3,
247 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
248 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
253 /***********************************************************************
254 * wine_dbg_vprintf (NTDLL.@)
256 int wine_dbg_vprintf( const char *format, va_list args )
258 struct debug_info *info = get_info();
261 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
264 /* make sure we didn't exceed the buffer length
265 * the two checks are due to glibc changes in vsnprintfs return value
266 * the buffer size can be exceeded in case of a missing \n in
268 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
270 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
272 info->out_pos = info->output;
276 p = strrchr( info->out_pos, '\n' );
277 if (!p) info->out_pos += ret;
280 char *pos = info->output;
282 write( 2, pos, p - pos );
283 /* move beginning of next line to start of buffer */
284 while ((*pos = *p++)) pos++;
290 /***********************************************************************
291 * wine_dbg_printf (NTDLL.@)
293 int wine_dbg_printf(const char *format, ...)
298 va_start(valist, format);
299 ret = wine_dbg_vprintf( format, valist );
304 /***********************************************************************
305 * wine_dbg_log (NTDLL.@)
307 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
308 const char *function, const char *format, ... )
310 static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
314 va_start(valist, format);
316 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
317 if (cls < __DBCL_COUNT)
318 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
320 ret += wine_dbg_vprintf( format, valist );