Fixed cut&paste problem in SETRTS.
[wine] / dlls / ntdll / debugtools.c
1 /*
2  * Debugging functions
3  */
4
5 #include <assert.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
10 #include <ctype.h>
11
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"
19 #include "msvcrt/excpt.h"
20
21 DECLARE_DEBUG_CHANNEL(tid);
22
23 /* ---------------------------------------------------------------------- */
24
25 struct debug_info
26 {
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 */
31 };
32
33 static struct debug_info initial_thread_info;  /* debug info for initial thread */
34
35 /* filter for page-fault exceptions */
36 static WINE_EXCEPTION_FILTER(page_fault)
37 {
38     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
39         return EXCEPTION_EXECUTE_HANDLER;
40     return EXCEPTION_CONTINUE_SEARCH;
41 }
42
43 /* get the debug info pointer for the current thread */
44 static inline struct debug_info *get_info(void)
45 {
46     struct debug_info *info = NtCurrentTeb()->debug_info;
47
48     if (!info) NtCurrentTeb()->debug_info = info = &initial_thread_info;
49     if (!info->str_pos)
50     {
51         info->str_pos = info->strings;
52         info->out_pos = info->output;
53     }
54     return info;
55 }
56
57 /* allocate some tmp space for a string */
58 static void *gimme1(int n)
59 {
60     struct debug_info *info = get_info();
61     char *res = info->str_pos;
62
63     if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
64     info->str_pos = res + n;
65     return res;
66 }
67
68 /* release extra space that we requested in gimme1() */
69 static inline void release( void *ptr )
70 {
71     struct debug_info *info = NtCurrentTeb()->debug_info;
72     info->str_pos = ptr;
73 }
74
75 /* put an ASCII string into the debug buffer */
76 inline static char *put_string_a( const char *src, int n )
77 {
78     char *dst, *res;
79
80     if (n < 0) n = 0;
81     else if (n > 200) n = 200;
82     dst = res = gimme1 (n * 4 + 6);
83     *dst++ = '"';
84     while (n-- > 0 && *src)
85     {
86         unsigned char c = *src++;
87         switch (c)
88         {
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;
94         default:
95             if (c >= ' ' && c <= 126)
96                 *dst++ = c;
97             else
98             {
99                 *dst++ = '\\';
100                 *dst++ = '0' + ((c >> 6) & 7);
101                 *dst++ = '0' + ((c >> 3) & 7);
102                 *dst++ = '0' + ((c >> 0) & 7);
103             }
104         }
105     }
106     *dst++ = '"';
107     if (*src)
108     {
109         *dst++ = '.';
110         *dst++ = '.';
111         *dst++ = '.';
112     }
113     *dst++ = '\0';
114     release( dst );
115     return res;
116 }
117
118 /* put a Unicode string into the debug buffer */
119 inline static char *put_string_w( const WCHAR *src, int n )
120 {
121     char *dst, *res;
122
123     if (n < 0) n = 0;
124     else if (n > 200) n = 200;
125     dst = res = gimme1 (n * 5 + 7);
126     *dst++ = 'L';
127     *dst++ = '"';
128     while (n-- > 0 && *src)
129     {
130         WCHAR c = *src++;
131         switch (c)
132         {
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;
138         default:
139             if (c >= ' ' && c <= 126)
140                 *dst++ = c;
141             else
142             {
143                 *dst++ = '\\';
144                 sprintf(dst,"%04x",c);
145                 dst+=4;
146             }
147         }
148     }
149     *dst++ = '"';
150     if (*src)
151     {
152         *dst++ = '.';
153         *dst++ = '.';
154         *dst++ = '.';
155     }
156     *dst++ = '\0';
157     release( dst );
158     return res;
159 }
160
161 /***********************************************************************
162  *              wine_dbgstr_an (NTDLL.@)
163  */
164 const char *wine_dbgstr_an( const char *src, int n )
165 {
166     char *res, *old_pos;
167     struct debug_info *info = get_info();
168
169     if (!HIWORD(src))
170     {
171         if (!src) return "(null)";
172         res = gimme1(6);
173         sprintf(res, "#%04x", LOWORD(src) );
174         return res;
175     }
176     /* save current position to restore it on exception */
177     old_pos = info->str_pos;
178     __TRY
179     {
180         res = put_string_a( src, n );
181     }
182     __EXCEPT(page_fault)
183     {
184         release( old_pos );
185         return "(invalid)";
186     }
187     __ENDTRY
188     return res;
189 }
190
191 /***********************************************************************
192  *              wine_dbgstr_wn (NTDLL.@)
193  */
194 const char *wine_dbgstr_wn( const WCHAR *src, int n )
195 {
196     char *res, *old_pos;
197     struct debug_info *info = get_info();
198
199     if (!HIWORD(src))
200     {
201         if (!src) return "(null)";
202         res = gimme1(6);
203         sprintf(res, "#%04x", LOWORD(src) );
204         return res;
205     }
206
207     /* save current position to restore it on exception */
208     old_pos = info->str_pos;
209     __TRY
210     {
211         res = put_string_w( src, n );
212     }
213     __EXCEPT(page_fault)
214     {
215         release( old_pos );
216         return "(invalid)";
217     }
218     __ENDTRY
219      return res;
220 }
221
222 /***********************************************************************
223  *              wine_dbgstr_guid (NTDLL.@)
224  */
225 const char *wine_dbgstr_guid( const GUID *id )
226 {
227     char *str;
228
229     if (!id) return "(null)";
230     if (!HIWORD(id))
231     {
232         str = gimme1(12);
233         sprintf( str, "<guid-0x%04x>", LOWORD(id) );
234     }
235     else
236     {
237         str = gimme1(40);
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] );
242     }
243     return str;
244 }
245
246 /***********************************************************************
247  *              wine_dbg_vprintf (NTDLL.@)
248  */
249 int wine_dbg_vprintf( const char *format, va_list args )
250 {
251     struct debug_info *info = get_info();
252     char *p;
253
254     int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
255                          format, args );
256
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
260      * debug output */
261     if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
262     {
263        fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
264                 info->output);
265        info->out_pos = info->output;
266        abort();
267     }
268
269     p = strrchr( info->out_pos, '\n' );
270     if (!p) info->out_pos += ret;
271     else
272     {
273         char *pos = info->output;
274         p++;
275         write( 2, pos, p - pos );
276         /* move beginning of next line to start of buffer */
277         while ((*pos = *p++)) pos++;
278         info->out_pos = pos;
279     }
280     return ret;
281 }
282
283 /***********************************************************************
284  *              wine_dbg_printf (NTDLL.@)
285  */
286 int wine_dbg_printf(const char *format, ...)
287 {
288     int ret;
289     va_list valist;
290
291     va_start(valist, format);
292     ret = wine_dbg_vprintf( format, valist );
293     va_end(valist);
294     return ret;
295 }
296
297 /***********************************************************************
298  *              wine_dbg_log (NTDLL.@)
299  */
300 int wine_dbg_log(enum __WINE_DEBUG_CLASS cls, const char *channel,
301                  const char *function, const char *format, ... )
302 {
303     static const char *classes[__WINE_DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
304     va_list valist;
305     int ret = 0;
306
307     va_start(valist, format);
308     if (TRACE_ON(tid))
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 );
312     if (format)
313         ret += wine_dbg_vprintf( format, valist );
314     va_end(valist);
315     return ret;
316 }