Convert MRULists to Unicode.
[wine/testsucceed.git] / dlls / ntdll / debugtools.c
blobfc222ab959210cfdbf056bd22659bc5f98ea89e0
1 /*
2 * Debugging functions
3 */
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <ctype.h>
12 #include "debugtools.h"
13 #include "wine/exception.h"
14 #include "thread.h"
15 #include "winbase.h"
16 #include "winnt.h"
17 #include "ntddk.h"
18 #include "wtypes.h"
20 DECLARE_DEBUG_CHANNEL(tid);
22 /* ---------------------------------------------------------------------- */
24 struct debug_info
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;
48 if (!info->str_pos)
50 info->str_pos = info->strings;
51 info->out_pos = info->output;
53 return info;
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;
64 return res;
67 /* release extra space that we requested in gimme1() */
68 static inline void release( void *ptr )
70 struct debug_info *info = NtCurrentTeb()->debug_info;
71 info->str_pos = ptr;
74 /* put an ASCII string into the debug buffer */
75 inline static char *put_string_a( const char *src, int n )
77 char *dst, *res;
79 if (n < 0) n = 0;
80 else if (n > 200) n = 200;
81 dst = res = gimme1 (n * 4 + 6);
82 *dst++ = '"';
83 while (n-- > 0 && *src)
85 unsigned char c = *src++;
86 switch (c)
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;
93 default:
94 if (c >= ' ' && c <= 126)
95 *dst++ = c;
96 else
98 *dst++ = '\\';
99 *dst++ = '0' + ((c >> 6) & 7);
100 *dst++ = '0' + ((c >> 3) & 7);
101 *dst++ = '0' + ((c >> 0) & 7);
105 *dst++ = '"';
106 if (*src)
108 *dst++ = '.';
109 *dst++ = '.';
110 *dst++ = '.';
112 *dst++ = '\0';
113 release( dst );
114 return res;
117 /* put a Unicode string into the debug buffer */
118 inline static char *put_string_w( const WCHAR *src, int n )
120 char *dst, *res;
122 if (n < 0) n = 0;
123 else if (n > 200) n = 200;
124 dst = res = gimme1 (n * 5 + 7);
125 *dst++ = 'L';
126 *dst++ = '"';
127 while (n-- > 0 && *src)
129 WCHAR c = *src++;
130 switch (c)
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;
137 default:
138 if (c >= ' ' && c <= 126)
139 *dst++ = c;
140 else
142 *dst++ = '\\';
143 sprintf(dst,"%04x",c);
144 dst+=4;
148 *dst++ = '"';
149 if (*src)
151 *dst++ = '.';
152 *dst++ = '.';
153 *dst++ = '.';
155 *dst++ = '\0';
156 release( dst );
157 return res;
160 /***********************************************************************
161 * wine_dbgstr_an (NTDLL.@)
163 const char *wine_dbgstr_an( const char *src, int n )
165 char *res, *old_pos;
166 struct debug_info *info = get_info();
168 if (!HIWORD(src))
170 if (!src) return "(null)";
171 res = gimme1(6);
172 sprintf(res, "#%04x", LOWORD(src) );
173 return res;
175 /* save current position to restore it on exception */
176 old_pos = info->str_pos;
177 __TRY
179 res = put_string_a( src, n );
181 __EXCEPT(page_fault)
183 release( old_pos );
184 return "(invalid)";
186 __ENDTRY
187 return res;
190 /***********************************************************************
191 * wine_dbgstr_wn (NTDLL.@)
193 const char *wine_dbgstr_wn( const WCHAR *src, int n )
195 char *res, *old_pos;
196 struct debug_info *info = get_info();
198 if (!HIWORD(src))
200 if (!src) return "(null)";
201 res = gimme1(6);
202 sprintf(res, "#%04x", LOWORD(src) );
203 return res;
206 /* save current position to restore it on exception */
207 old_pos = info->str_pos;
208 __TRY
210 res = put_string_w( src, n );
212 __EXCEPT(page_fault)
214 release( old_pos );
215 return "(invalid)";
217 __ENDTRY
218 return res;
221 /***********************************************************************
222 * wine_dbgstr_guid (NTDLL.@)
224 const char *wine_dbgstr_guid( const GUID *id )
226 char *str;
228 if (!id) return "(null)";
229 if (!HIWORD(id))
231 str = gimme1(12);
232 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
234 else
236 str = gimme1(40);
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] );
242 return str;
245 /***********************************************************************
246 * wine_dbg_vprintf (NTDLL.@)
248 int wine_dbg_vprintf( const char *format, va_list args )
250 struct debug_info *info = get_info();
251 char *p;
253 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
254 format, args );
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
259 * debug output */
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",
263 info->output);
264 info->out_pos = info->output;
265 abort();
268 p = strrchr( info->out_pos, '\n' );
269 if (!p) info->out_pos += ret;
270 else
272 char *pos = info->output;
273 p++;
274 write( 2, pos, p - pos );
275 /* move beginning of next line to start of buffer */
276 while ((*pos = *p++)) pos++;
277 info->out_pos = pos;
279 return ret;
282 /***********************************************************************
283 * wine_dbg_printf (NTDLL.@)
285 int wine_dbg_printf(const char *format, ...)
287 int ret;
288 va_list valist;
290 va_start(valist, format);
291 ret = wine_dbg_vprintf( format, valist );
292 va_end(valist);
293 return ret;
296 /***********************************************************************
297 * wine_dbg_log (NTDLL.@)
299 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
300 const char *function, const char *format, ... )
302 static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
303 va_list valist;
304 int ret = 0;
306 va_start(valist, format);
307 if (TRACE_ON(tid))
308 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
309 if (cls < __DBCL_COUNT)
310 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
311 if (format)
312 ret += wine_dbg_vprintf( format, valist );
313 va_end(valist);
314 return ret;