12 #include "debugtools.h"
13 #include "wine/exception.h"
19 DECLARE_DEBUG_CHANNEL(tid);
21 /* ---------------------------------------------------------------------- */
25 char *str_pos; /* current position in strings buffer */
26 char *out_pos; /* current position in output buffer */
27 char strings[1024]; /* buffer for temporary strings */
28 char output[1024]; /* current output line */
31 static struct debug_info tmp;
33 /* filter for page-fault exceptions */
34 static WINE_EXCEPTION_FILTER(page_fault)
36 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
37 return EXCEPTION_EXECUTE_HANDLER;
38 return EXCEPTION_CONTINUE_SEARCH;
41 /* get the debug info pointer for the current thread */
42 static inline struct debug_info *get_info(void)
44 struct debug_info *info = NtCurrentTeb()->debug_info;
49 tmp.str_pos = tmp.strings;
50 tmp.out_pos = tmp.output;
52 if (!GetProcessHeap()) return &tmp;
53 /* setup the temp structure in case HeapAlloc wants to print something */
54 NtCurrentTeb()->debug_info = &tmp;
55 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
56 info->str_pos = info->strings;
57 info->out_pos = info->output;
58 NtCurrentTeb()->debug_info = info;
63 /* allocate some tmp space for a string */
64 static void *gimme1(int n)
66 struct debug_info *info = get_info();
67 char *res = info->str_pos;
69 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
70 info->str_pos = res + n;
74 /* release extra space that we requested in gimme1() */
75 static inline void release( void *ptr )
77 struct debug_info *info = NtCurrentTeb()->debug_info;
81 /* put an ASCII string into the debug buffer */
82 inline static char *put_string_a( const char *src, int n )
87 else if (n > 200) n = 200;
88 dst = res = gimme1 (n * 4 + 6);
90 while (n-- > 0 && *src)
92 unsigned char c = *src++;
95 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
96 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
97 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
98 case '"': *dst++ = '\\'; *dst++ = '"'; break;
99 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
101 if (c >= ' ' && c <= 126)
106 *dst++ = '0' + ((c >> 6) & 7);
107 *dst++ = '0' + ((c >> 3) & 7);
108 *dst++ = '0' + ((c >> 0) & 7);
124 /* put a Unicode string into the debug buffer */
125 inline static char *put_string_w( const WCHAR *src, int n )
130 else if (n > 200) n = 200;
131 dst = res = gimme1 (n * 5 + 7);
134 while (n-- > 0 && *src)
139 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
140 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
141 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
142 case '"': *dst++ = '\\'; *dst++ = '"'; break;
143 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
145 if (c >= ' ' && c <= 126)
150 sprintf(dst,"%04x",c);
167 /***********************************************************************
168 * wine_dbgstr_an (NTDLL.@)
170 const char *wine_dbgstr_an( const char *src, int n )
173 struct debug_info *info;
177 if (!src) return "(null)";
179 sprintf(res, "#%04x", LOWORD(src) );
182 /* save current position to restore it on exception */
183 info = NtCurrentTeb()->debug_info;
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;
208 if (!src) return "(null)";
210 sprintf(res, "#%04x", LOWORD(src) );
214 /* save current position to restore it on exception */
215 info = NtCurrentTeb()->debug_info;
216 old_pos = info->str_pos;
219 res = put_string_w( src, n );
230 /***********************************************************************
231 * wine_dbgstr_guid (NTDLL.@)
233 const char *wine_dbgstr_guid( const GUID *id )
237 if (!id) return "(null)";
241 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
246 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
247 id->Data1, id->Data2, id->Data3,
248 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
249 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
254 /***********************************************************************
255 * wine_dbg_vprintf (NTDLL.@)
257 int wine_dbg_vprintf( const char *format, va_list args )
259 struct debug_info *info = get_info();
262 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
265 /* make sure we didn't exceed the buffer length
266 * the two checks are due to glibc changes in vsnprintfs return value
267 * the buffer size can be exceeded in case of a missing \n in
269 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
271 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
273 info->out_pos = info->output;
277 p = strrchr( info->out_pos, '\n' );
278 if (!p) info->out_pos += ret;
281 char *pos = info->output;
283 write( 2, pos, p - pos );
284 /* move beginning of next line to start of buffer */
285 while ((*pos = *p++)) pos++;
291 /***********************************************************************
292 * wine_dbg_printf (NTDLL.@)
294 int wine_dbg_printf(const char *format, ...)
299 va_start(valist, format);
300 ret = wine_dbg_vprintf( format, valist );
305 /***********************************************************************
306 * wine_dbg_log (NTDLL.@)
308 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
309 const char *function, const char *format, ... )
311 static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
315 va_start(valist, format);
317 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
318 if (cls < __DBCL_COUNT)
319 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
321 ret += wine_dbg_vprintf( format, valist );