SetMenu should always call SetWindowPos whether the window is visible
[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 <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <ctype.h>
32
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "wine/library.h"
36 #include "wine/unicode.h"
37 #include "ntstatus.h"
38 #include "thread.h"
39 #include "winbase.h"
40 #include "winnt.h"
41 #include "winternl.h"
42 #include "excpt.h"
43 #include "ntdll_misc.h"
44
45 WINE_DECLARE_DEBUG_CHANNEL(tid);
46
47 /* ---------------------------------------------------------------------- */
48
49 /* filter for page-fault exceptions */
50 static WINE_EXCEPTION_FILTER(page_fault)
51 {
52     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
53         return EXCEPTION_EXECUTE_HANDLER;
54     return EXCEPTION_CONTINUE_SEARCH;
55 }
56
57 /* get the debug info pointer for the current thread */
58 static inline struct debug_info *get_info(void)
59 {
60     return NtCurrentTeb()->debug_info;
61 }
62
63 /* allocate some tmp space for a string */
64 static void *gimme1(int n)
65 {
66     struct debug_info *info = get_info();
67     char *res = info->str_pos;
68
69     if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
70     info->str_pos = res + n;
71     return res;
72 }
73
74 /* release extra space that we requested in gimme1() */
75 static inline void release( void *ptr )
76 {
77     struct debug_info *info = NtCurrentTeb()->debug_info;
78     info->str_pos = ptr;
79 }
80
81 /* put an ASCII string into the debug buffer */
82 inline static char *put_string_a( const char *src, int n )
83 {
84     static const char hex[16] = "0123456789abcdef";
85     char *dst, *res;
86
87     if (n == -1) n = strlen(src);
88     if (n < 0) n = 0;
89     else if (n > 80) n = 80;
90     dst = res = gimme1 (n * 4 + 6);
91     *dst++ = '"';
92     while (n-- > 0)
93     {
94         unsigned char c = *src++;
95         switch (c)
96         {
97         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
98         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
99         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
100         case '"': *dst++ = '\\'; *dst++ = '"'; break;
101         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
102         default:
103             if (c >= ' ' && c <= 126)
104                 *dst++ = c;
105             else
106             {
107                 *dst++ = '\\';
108                 *dst++ = 'x';
109                 *dst++ = hex[(c >> 4) & 0x0f];
110                 *dst++ = hex[c & 0x0f];
111             }
112         }
113     }
114     *dst++ = '"';
115     if (*src)
116     {
117         *dst++ = '.';
118         *dst++ = '.';
119         *dst++ = '.';
120     }
121     *dst++ = '\0';
122     release( dst );
123     return res;
124 }
125
126 /* put a Unicode string into the debug buffer */
127 inline static char *put_string_w( const WCHAR *src, int n )
128 {
129     char *dst, *res;
130
131     if (n == -1) n = strlenW(src);
132     if (n < 0) n = 0;
133     else if (n > 80) n = 80;
134     dst = res = gimme1 (n * 5 + 7);
135     *dst++ = 'L';
136     *dst++ = '"';
137     while (n-- > 0)
138     {
139         WCHAR c = *src++;
140         switch (c)
141         {
142         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
143         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
144         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
145         case '"': *dst++ = '\\'; *dst++ = '"'; break;
146         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
147         default:
148             if (c >= ' ' && c <= 126)
149                 *dst++ = c;
150             else
151             {
152                 *dst++ = '\\';
153                 sprintf(dst,"%04x",c);
154                 dst+=4;
155             }
156         }
157     }
158     *dst++ = '"';
159     if (*src)
160     {
161         *dst++ = '.';
162         *dst++ = '.';
163         *dst++ = '.';
164     }
165     *dst++ = '\0';
166     release( dst );
167     return res;
168 }
169
170 /***********************************************************************
171  *              NTDLL_dbgstr_an
172  */
173 static const char *NTDLL_dbgstr_an( const char *src, int n )
174 {
175     char *res, *old_pos;
176     struct debug_info *info = get_info();
177
178     if (!HIWORD(src))
179     {
180         if (!src) return "(null)";
181         res = gimme1(6);
182         sprintf(res, "#%04x", LOWORD(src) );
183         return res;
184     }
185     /* save current position to restore it on exception */
186     old_pos = info->str_pos;
187     __TRY
188     {
189         res = put_string_a( src, n );
190     }
191     __EXCEPT(page_fault)
192     {
193         release( old_pos );
194         return "(invalid)";
195     }
196     __ENDTRY
197     return res;
198 }
199
200 /***********************************************************************
201  *              NTDLL_dbgstr_wn
202  */
203 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
204 {
205     char *res, *old_pos;
206     struct debug_info *info = get_info();
207
208     if (!HIWORD(src))
209     {
210         if (!src) return "(null)";
211         res = gimme1(6);
212         sprintf(res, "#%04x", LOWORD(src) );
213         return res;
214     }
215
216     /* save current position to restore it on exception */
217     old_pos = info->str_pos;
218     __TRY
219     {
220         res = put_string_w( src, n );
221     }
222     __EXCEPT(page_fault)
223     {
224         release( old_pos );
225         return "(invalid)";
226     }
227     __ENDTRY
228      return res;
229 }
230
231 /***********************************************************************
232  *              NTDLL_dbg_vsprintf
233  */
234 static const char *NTDLL_dbg_vsprintf( const char *format, va_list args )
235 {
236     static const int max_size = 200;
237
238     char *res = gimme1( max_size );
239     int len = vsnprintf( res, max_size, format, args );
240     if (len == -1 || len >= max_size) res[max_size-1] = 0;
241     else release( res + len + 1 );
242     return res;
243 }
244
245 /***********************************************************************
246  *              NTDLL_dbg_vprintf
247  */
248 static int NTDLL_dbg_vprintf( const char *format, va_list args )
249 {
250     struct debug_info *info = get_info();
251     char *p;
252
253     int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
254                          format, args );
255
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)))
261     {
262        fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
263                 info->output);
264        info->out_pos = info->output;
265        abort();
266     }
267
268     p = strrchr( info->out_pos, '\n' );
269     if (!p) info->out_pos += ret;
270     else
271     {
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;
278     }
279     return ret;
280 }
281
282 /***********************************************************************
283  *              NTDLL_dbg_vlog
284  */
285 static int NTDLL_dbg_vlog( unsigned int cls, const char *channel,
286                            const char *function, const char *format, va_list args )
287 {
288     static const char * const classes[] = { "fixme", "err", "warn", "trace" };
289     struct debug_info *info = get_info();
290     int ret = 0;
291
292     /* only print header if we are at the beginning of the line */
293     if (info->out_pos == info->output || info->out_pos[-1] == '\n')
294     {
295         if (TRACE_ON(tid))
296             ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
297         if (cls < sizeof(classes)/sizeof(classes[0]))
298             ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
299     }
300     if (format)
301         ret += NTDLL_dbg_vprintf( format, args );
302     return ret;
303 }
304
305 /***********************************************************************
306  *              debug_init
307  */
308 void debug_init(void)
309 {
310     __wine_dbgstr_an    = NTDLL_dbgstr_an;
311     __wine_dbgstr_wn    = NTDLL_dbgstr_wn;
312     __wine_dbg_vsprintf = NTDLL_dbg_vsprintf;
313     __wine_dbg_vprintf  = NTDLL_dbg_vprintf;
314     __wine_dbg_vlog     = NTDLL_dbg_vlog;
315 }