ntdll/tests: Skip a couple of exception tests that crash on Wow64.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winnt.h"
38 #include "winternl.h"
39 #include "ntdll_misc.h"
40
41 WINE_DECLARE_DEBUG_CHANNEL(tid);
42
43 static struct __wine_debug_functions default_funcs;
44
45 /* ---------------------------------------------------------------------- */
46
47 /* get the debug info pointer for the current thread */
48 static inline struct debug_info *get_info(void)
49 {
50     return ntdll_get_thread_data()->debug_info;
51 }
52
53 /* allocate some tmp space for a string */
54 static char *get_temp_buffer( size_t n )
55 {
56     struct debug_info *info = get_info();
57     char *res = info->str_pos;
58
59     if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
60     info->str_pos = res + n;
61     return res;
62 }
63
64 /* release extra space that we requested in gimme1() */
65 static void release_temp_buffer( char *ptr, size_t size )
66 {
67     struct debug_info *info = get_info();
68     info->str_pos = ptr + size;
69 }
70
71 /***********************************************************************
72  *              NTDLL_dbgstr_an
73  */
74 static const char *NTDLL_dbgstr_an( const char *src, int n )
75 {
76     const char *res;
77     struct debug_info *info = get_info();
78     /* save current position to restore it on exception */
79     char *old_pos = info->str_pos;
80
81     __TRY
82     {
83         res = default_funcs.dbgstr_an( src, n );
84     }
85     __EXCEPT_PAGE_FAULT
86     {
87         release_temp_buffer( old_pos, 0 );
88         return "(invalid)";
89     }
90     __ENDTRY
91     return res;
92 }
93
94 /***********************************************************************
95  *              NTDLL_dbgstr_wn
96  */
97 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
98 {
99     const char *res;
100     struct debug_info *info = get_info();
101     /* save current position to restore it on exception */
102     char *old_pos = info->str_pos;
103
104     __TRY
105     {
106         res = default_funcs.dbgstr_wn( src, n );
107     }
108     __EXCEPT_PAGE_FAULT
109     {
110         release_temp_buffer( old_pos, 0 );
111         return "(invalid)";
112     }
113     __ENDTRY
114      return res;
115 }
116
117 /***********************************************************************
118  *              NTDLL_dbg_vprintf
119  */
120 static int NTDLL_dbg_vprintf( const char *format, va_list args )
121 {
122     struct debug_info *info = get_info();
123     int end;
124
125     int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
126                          format, args );
127
128     /* make sure we didn't exceed the buffer length
129      * the two checks are due to glibc changes in vsnprintfs return value
130      * the buffer size can be exceeded in case of a missing \n in
131      * debug output */
132     if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
133     {
134        fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
135                 info->output);
136        info->out_pos = info->output;
137        abort();
138     }
139
140     for (end = ret; end > 0; end--) if (info->out_pos[end - 1] == '\n') break;
141
142     if (!end) info->out_pos += ret;
143     else
144     {
145         char *pos = info->output;
146         write( 2, pos, info->out_pos + end - pos );
147         /* move beginning of next line to start of buffer */
148         memmove( pos, info->out_pos + end, ret - end );
149         info->out_pos = pos + ret - end;
150     }
151     return ret;
152 }
153
154 /***********************************************************************
155  *              NTDLL_dbg_vlog
156  */
157 static int NTDLL_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
158                            const char *function, const char *format, va_list args )
159 {
160     static const char * const classes[] = { "fixme", "err", "warn", "trace" };
161     struct debug_info *info = get_info();
162     int ret = 0;
163
164     /* only print header if we are at the beginning of the line */
165     if (info->out_pos == info->output || info->out_pos[-1] == '\n')
166     {
167         if (TRACE_ON(tid))
168             ret = wine_dbg_printf( "%04x:", GetCurrentThreadId() );
169         if (cls < sizeof(classes)/sizeof(classes[0]))
170             ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel->name, function );
171     }
172     if (format)
173         ret += NTDLL_dbg_vprintf( format, args );
174     return ret;
175 }
176
177
178 static const struct __wine_debug_functions funcs =
179 {
180     get_temp_buffer,
181     release_temp_buffer,
182     NTDLL_dbgstr_an,
183     NTDLL_dbgstr_wn,
184     NTDLL_dbg_vprintf,
185     NTDLL_dbg_vlog
186 };
187
188 /***********************************************************************
189  *              debug_init
190  */
191 void debug_init(void)
192 {
193     __wine_dbg_set_functions( &funcs, &default_funcs, sizeof(funcs) );
194 }