Added an unknown VxD error code.
[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 "thread.h"
14 #include "winbase.h"
15 #include "winnt.h"
16 #include "wtypes.h"
17
18 DECLARE_DEBUG_CHANNEL(tid);
19
20 /* ---------------------------------------------------------------------- */
21
22 struct debug_info
23 {
24     char *str_pos;       /* current position in strings buffer */
25     char *out_pos;       /* current position in output buffer */
26     char  strings[1024]; /* buffer for temporary strings */
27     char  output[1024];  /* current output line */
28 };
29
30 static struct debug_info tmp;
31
32 /* get the debug info pointer for the current thread */
33 static inline struct debug_info *get_info(void)
34 {
35     struct debug_info *info = NtCurrentTeb()->debug_info;
36     if (!info)
37     {
38         if (!tmp.str_pos)
39         {
40             tmp.str_pos = tmp.strings;
41             tmp.out_pos = tmp.output;
42         }
43         if (!GetProcessHeap()) return &tmp;
44         /* setup the temp structure in case HeapAlloc wants to print something */
45         NtCurrentTeb()->debug_info = &tmp;
46         info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
47         info->str_pos = info->strings;
48         info->out_pos = info->output;
49         NtCurrentTeb()->debug_info = info;
50     }
51     return info;
52 }
53
54 /* allocate some tmp space for a string */
55 static void *gimme1(int n)
56 {
57     struct debug_info *info = get_info();
58     char *res = info->str_pos;
59
60     if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
61     info->str_pos = res + n;
62     return res;
63 }
64
65 /* release extra space that we requested in gimme1() */
66 static inline void release( void *ptr )
67 {
68     struct debug_info *info = NtCurrentTeb()->debug_info;
69     info->str_pos = ptr;
70 }
71
72 /***********************************************************************
73  *              wine_dbgstr_an
74  */
75 const char *wine_dbgstr_an( const char *src, int n )
76 {
77     char *dst, *res;
78
79     if (!HIWORD(src))
80     {
81         if (!src) return "(null)";
82         res = gimme1(6);
83         sprintf(res, "#%04x", LOWORD(src) );
84         return res;
85     }
86     if (n < 0) n = 0;
87     dst = res = gimme1 (n * 4 + 6);
88     *dst++ = '"';
89     while (n-- > 0 && *src)
90     {
91         unsigned char c = *src++;
92         switch (c)
93         {
94         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
95         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
96         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
97         case '"': *dst++ = '\\'; *dst++ = '"'; break;
98         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
99         default:
100             if (c >= ' ' && c <= 126)
101                 *dst++ = c;
102             else
103             {
104                 *dst++ = '\\';
105                 *dst++ = '0' + ((c >> 6) & 7);
106                 *dst++ = '0' + ((c >> 3) & 7);
107                 *dst++ = '0' + ((c >> 0) & 7);
108             }
109         }
110     }
111     *dst++ = '"';
112     if (*src)
113     {
114         *dst++ = '.';
115         *dst++ = '.';
116         *dst++ = '.';
117     }
118     *dst++ = '\0';
119     release( dst );
120     return res;
121 }
122
123 /***********************************************************************
124  *              wine_dbgstr_wn
125  */
126 const char *wine_dbgstr_wn( const WCHAR *src, int n )
127 {
128     char *dst, *res;
129
130     if (!HIWORD(src))
131     {
132         if (!src) return "(null)";
133         res = gimme1(6);
134         sprintf(res, "#%04x", LOWORD(src) );
135         return res;
136     }
137     if (n < 0) n = 0;
138     dst = res = gimme1 (n * 5 + 7);
139     *dst++ = 'L';
140     *dst++ = '"';
141     while (n-- > 0 && *src)
142     {
143         WCHAR c = *src++;
144         switch (c)
145         {
146         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
147         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
148         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
149         case '"': *dst++ = '\\'; *dst++ = '"'; break;
150         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
151         default:
152             if (c >= ' ' && c <= 126)
153                 *dst++ = c;
154             else
155             {
156                 *dst++ = '\\';
157                 sprintf(dst,"%04x",c);
158                 dst+=4;
159             }
160         }
161     }
162     *dst++ = '"';
163     if (*src)
164     {
165         *dst++ = '.';
166         *dst++ = '.';
167         *dst++ = '.';
168     }
169     *dst++ = '\0';
170     release( dst );
171     return res;
172 }
173
174 /***********************************************************************
175  *              wine_dbgstr_guid
176  */
177 const char *wine_dbgstr_guid( const GUID *id )
178 {
179     char *str;
180
181     if (!id) return "(null)";
182     if (!HIWORD(id))
183     {
184         str = gimme1(12);
185         sprintf( str, "<guid-0x%04x>", LOWORD(id) );
186     }
187     else
188     {
189         str = gimme1(40);
190         sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
191                  id->Data1, id->Data2, id->Data3,
192                  id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
193                  id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
194     }
195     return str;
196 }
197
198 /***********************************************************************
199  *              wine_dbg_vprintf
200  */
201 int wine_dbg_vprintf( const char *format, va_list args )
202 {
203     struct debug_info *info = get_info();
204     char *p;
205
206     int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
207                          format, args );
208
209     /* make sure we didn't exceed the buffer length
210      * the two asserts are due to glibc changes in vsnprintfs return value */
211     assert( ret != -1 );
212     assert( ret < sizeof(info->output) - (info->out_pos - info->output) );
213
214     p = strrchr( info->out_pos, '\n' );
215     if (!p) info->out_pos += ret;
216     else
217     {
218         char *pos = info->output;
219         p++;
220         write( 2, pos, p - pos );
221         /* move beginning of next line to start of buffer */
222         while ((*pos = *p++)) pos++;
223         info->out_pos = pos;
224     }
225     return ret;
226 }
227
228 /***********************************************************************
229  *              wine_dbg_printf
230  */
231 int wine_dbg_printf(const char *format, ...)
232 {
233     int ret;
234     va_list valist;
235
236     va_start(valist, format);
237     ret = wine_dbg_vprintf( format, valist );
238     va_end(valist);
239     return ret;
240 }
241
242 /***********************************************************************
243  *              wine_dbg_log
244  */
245 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
246                  const char *function, const char *format, ... )
247 {
248     static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
249     va_list valist;
250     int ret = 0;
251
252     va_start(valist, format);
253     if (TRACE_ON(tid))
254         ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
255     if (cls < __DBCL_COUNT)
256         ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
257     if (format)
258         ret += wine_dbg_vprintf( format, valist );
259     va_end(valist);
260     return ret;
261 }