Fix the case of product and company names.
[wine] / dlls / ntdll / debugtools.c
1 /*
2  * Debugging functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <assert.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include <ctype.h>
35
36 #include "wine/debug.h"
37 #include "wine/exception.h"
38 #include "wine/library.h"
39 #include "wine/unicode.h"
40 #include "ntstatus.h"
41 #include "thread.h"
42 #include "winbase.h"
43 #include "winnt.h"
44 #include "winternl.h"
45 #include "excpt.h"
46
47 WINE_DECLARE_DEBUG_CHANNEL(tid);
48
49 /* ---------------------------------------------------------------------- */
50
51 /* filter for page-fault exceptions */
52 static WINE_EXCEPTION_FILTER(page_fault)
53 {
54     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
55         return EXCEPTION_EXECUTE_HANDLER;
56     return EXCEPTION_CONTINUE_SEARCH;
57 }
58
59 /* get the debug info pointer for the current thread */
60 static inline struct debug_info *get_info(void)
61 {
62     return NtCurrentTeb()->debug_info;
63 }
64
65 /* allocate some tmp space for a string */
66 static void *gimme1(int n)
67 {
68     struct debug_info *info = get_info();
69     char *res = info->str_pos;
70
71     if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
72     info->str_pos = res + n;
73     return res;
74 }
75
76 /* release extra space that we requested in gimme1() */
77 static inline void release( void *ptr )
78 {
79     struct debug_info *info = NtCurrentTeb()->debug_info;
80     info->str_pos = ptr;
81 }
82
83 /* put an ASCII string into the debug buffer */
84 inline static char *put_string_a( const char *src, int n )
85 {
86     static const char hex[16] = "0123456789abcdef";
87     char *dst, *res;
88
89     if (n == -1) n = strlen(src);
90     if (n < 0) n = 0;
91     else if (n > 80) n = 80;
92     dst = res = gimme1 (n * 4 + 6);
93     *dst++ = '"';
94     while (n-- > 0)
95     {
96         unsigned char c = *src++;
97         switch (c)
98         {
99         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
100         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
101         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
102         case '"': *dst++ = '\\'; *dst++ = '"'; break;
103         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
104         default:
105             if (c >= ' ' && c <= 126)
106                 *dst++ = c;
107             else
108             {
109                 *dst++ = '\\';
110                 *dst++ = 'x';
111                 *dst++ = hex[(c >> 4) & 0x0f];
112                 *dst++ = hex[c & 0x0f];
113             }
114         }
115     }
116     *dst++ = '"';
117     if (*src)
118     {
119         *dst++ = '.';
120         *dst++ = '.';
121         *dst++ = '.';
122     }
123     *dst++ = '\0';
124     release( dst );
125     return res;
126 }
127
128 /* put a Unicode string into the debug buffer */
129 inline static char *put_string_w( const WCHAR *src, int n )
130 {
131     char *dst, *res;
132
133     if (n == -1) n = strlenW(src);
134     if (n < 0) n = 0;
135     else if (n > 80) n = 80;
136     dst = res = gimme1 (n * 5 + 7);
137     *dst++ = 'L';
138     *dst++ = '"';
139     while (n-- > 0)
140     {
141         WCHAR c = *src++;
142         switch (c)
143         {
144         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
145         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
146         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
147         case '"': *dst++ = '\\'; *dst++ = '"'; break;
148         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
149         default:
150             if (c >= ' ' && c <= 126)
151                 *dst++ = c;
152             else
153             {
154                 *dst++ = '\\';
155                 sprintf(dst,"%04x",c);
156                 dst+=4;
157             }
158         }
159     }
160     *dst++ = '"';
161     if (*src)
162     {
163         *dst++ = '.';
164         *dst++ = '.';
165         *dst++ = '.';
166     }
167     *dst++ = '\0';
168     release( dst );
169     return res;
170 }
171
172 /***********************************************************************
173  *              NTDLL_dbgstr_an
174  */
175 static const char *NTDLL_dbgstr_an( const char *src, int n )
176 {
177     char *res, *old_pos;
178     struct debug_info *info = get_info();
179
180     if (!HIWORD(src))
181     {
182         if (!src) return "(null)";
183         res = gimme1(6);
184         sprintf(res, "#%04x", LOWORD(src) );
185         return res;
186     }
187     /* save current position to restore it on exception */
188     old_pos = info->str_pos;
189     __TRY
190     {
191         res = put_string_a( src, n );
192     }
193     __EXCEPT(page_fault)
194     {
195         release( old_pos );
196         return "(invalid)";
197     }
198     __ENDTRY
199     return res;
200 }
201
202 /***********************************************************************
203  *              NTDLL_dbgstr_wn
204  */
205 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
206 {
207     char *res, *old_pos;
208     struct debug_info *info = get_info();
209
210     if (!HIWORD(src))
211     {
212         if (!src) return "(null)";
213         res = gimme1(6);
214         sprintf(res, "#%04x", LOWORD(src) );
215         return res;
216     }
217
218     /* save current position to restore it on exception */
219     old_pos = info->str_pos;
220     __TRY
221     {
222         res = put_string_w( src, n );
223     }
224     __EXCEPT(page_fault)
225     {
226         release( old_pos );
227         return "(invalid)";
228     }
229     __ENDTRY
230      return res;
231 }
232
233 /***********************************************************************
234  *              NTDLL_dbg_vsprintf
235  */
236 static const char *NTDLL_dbg_vsprintf( const char *format, va_list args )
237 {
238     static const int max_size = 200;
239
240     char *res = gimme1( max_size );
241     int len = vsnprintf( res, max_size, format, args );
242     if (len == -1 || len >= max_size) res[max_size-1] = 0;
243     else release( res + len + 1 );
244     return res;
245 }
246
247 /***********************************************************************
248  *              NTDLL_dbg_vprintf
249  */
250 static int NTDLL_dbg_vprintf( const char *format, va_list args )
251 {
252     struct debug_info *info = get_info();
253     char *p;
254
255     int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
256                          format, args );
257
258     /* make sure we didn't exceed the buffer length
259      * the two checks are due to glibc changes in vsnprintfs return value
260      * the buffer size can be exceeded in case of a missing \n in
261      * debug output */
262     if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
263     {
264        fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
265                 info->output);
266        info->out_pos = info->output;
267        abort();
268     }
269
270     p = strrchr( info->out_pos, '\n' );
271     if (!p) info->out_pos += ret;
272     else
273     {
274         char *pos = info->output;
275         p++;
276         write( 2, pos, p - pos );
277         /* move beginning of next line to start of buffer */
278         while ((*pos = *p++)) pos++;
279         info->out_pos = pos;
280     }
281     return ret;
282 }
283
284 /***********************************************************************
285  *              NTDLL_dbg_vlog
286  */
287 static int NTDLL_dbg_vlog( unsigned int cls, const char *channel,
288                            const char *function, const char *format, va_list args )
289 {
290     static const char * const classes[] = { "fixme", "err", "warn", "trace" };
291     struct debug_info *info = get_info();
292     int ret = 0;
293
294     /* only print header if we are at the beginning of the line */
295     if (info->out_pos == info->output || info->out_pos[-1] == '\n')
296     {
297         if (TRACE_ON(tid))
298             ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
299         if (cls < sizeof(classes)/sizeof(classes[0]))
300             ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
301     }
302     if (format)
303         ret += NTDLL_dbg_vprintf( format, args );
304     return ret;
305 }
306
307 /***********************************************************************
308  *              debug_init
309  */
310 DECL_GLOBAL_CONSTRUCTOR(debug_init)
311 {
312     __wine_dbgstr_an    = NTDLL_dbgstr_an;
313     __wine_dbgstr_wn    = NTDLL_dbgstr_wn;
314     __wine_dbg_vsprintf = NTDLL_dbg_vsprintf;
315     __wine_dbg_vprintf  = NTDLL_dbg_vprintf;
316     __wine_dbg_vlog     = NTDLL_dbg_vlog;
317 }