Implemented signal handling for PPC.
[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 "thread.h"
41 #include "winbase.h"
42 #include "winnt.h"
43 #include "ntddk.h"
44 #include "msvcrt/excpt.h"
45
46 WINE_DECLARE_DEBUG_CHANNEL(tid);
47
48 /* ---------------------------------------------------------------------- */
49
50 struct debug_info
51 {
52     char *str_pos;       /* current position in strings buffer */
53     char *out_pos;       /* current position in output buffer */
54     char  strings[1024]; /* buffer for temporary strings */
55     char  output[1024];  /* current output line */
56 };
57
58 static struct debug_info initial_thread_info;  /* debug info for initial thread */
59
60 /* filter for page-fault exceptions */
61 static WINE_EXCEPTION_FILTER(page_fault)
62 {
63     if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
64         return EXCEPTION_EXECUTE_HANDLER;
65     return EXCEPTION_CONTINUE_SEARCH;
66 }
67
68 /* get the debug info pointer for the current thread */
69 static inline struct debug_info *get_info(void)
70 {
71     struct debug_info *info = NtCurrentTeb()->debug_info;
72
73     if (!info) NtCurrentTeb()->debug_info = info = &initial_thread_info;
74     if (!info->str_pos)
75     {
76         info->str_pos = info->strings;
77         info->out_pos = info->output;
78     }
79     return info;
80 }
81
82 /* allocate some tmp space for a string */
83 static void *gimme1(int n)
84 {
85     struct debug_info *info = get_info();
86     char *res = info->str_pos;
87
88     if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
89     info->str_pos = res + n;
90     return res;
91 }
92
93 /* release extra space that we requested in gimme1() */
94 static inline void release( void *ptr )
95 {
96     struct debug_info *info = NtCurrentTeb()->debug_info;
97     info->str_pos = ptr;
98 }
99
100 /* put an ASCII string into the debug buffer */
101 inline static char *put_string_a( const char *src, int n )
102 {
103     char *dst, *res;
104
105     if (n == -1) n = strlen(src);
106     if (n < 0) n = 0;
107     else if (n > 200) n = 200;
108     dst = res = gimme1 (n * 4 + 6);
109     *dst++ = '"';
110     while (n-- > 0)
111     {
112         unsigned char c = *src++;
113         switch (c)
114         {
115         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
116         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
117         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
118         case '"': *dst++ = '\\'; *dst++ = '"'; break;
119         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
120         default:
121             if (c >= ' ' && c <= 126)
122                 *dst++ = c;
123             else
124             {
125                 *dst++ = '\\';
126                 *dst++ = '0' + ((c >> 6) & 7);
127                 *dst++ = '0' + ((c >> 3) & 7);
128                 *dst++ = '0' + ((c >> 0) & 7);
129             }
130         }
131     }
132     *dst++ = '"';
133     if (*src)
134     {
135         *dst++ = '.';
136         *dst++ = '.';
137         *dst++ = '.';
138     }
139     *dst++ = '\0';
140     release( dst );
141     return res;
142 }
143
144 /* put a Unicode string into the debug buffer */
145 inline static char *put_string_w( const WCHAR *src, int n )
146 {
147     char *dst, *res;
148
149     if (n == -1) n = strlenW(src);
150     if (n < 0) n = 0;
151     else if (n > 200) n = 200;
152     dst = res = gimme1 (n * 5 + 7);
153     *dst++ = 'L';
154     *dst++ = '"';
155     while (n-- > 0)
156     {
157         WCHAR c = *src++;
158         switch (c)
159         {
160         case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
161         case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
162         case '\t': *dst++ = '\\'; *dst++ = 't'; break;
163         case '"': *dst++ = '\\'; *dst++ = '"'; break;
164         case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
165         default:
166             if (c >= ' ' && c <= 126)
167                 *dst++ = c;
168             else
169             {
170                 *dst++ = '\\';
171                 sprintf(dst,"%04x",c);
172                 dst+=4;
173             }
174         }
175     }
176     *dst++ = '"';
177     if (*src)
178     {
179         *dst++ = '.';
180         *dst++ = '.';
181         *dst++ = '.';
182     }
183     *dst++ = '\0';
184     release( dst );
185     return res;
186 }
187
188 /***********************************************************************
189  *              NTDLL_dbgstr_an
190  */
191 static const char *NTDLL_dbgstr_an( const char *src, int n )
192 {
193     char *res, *old_pos;
194     struct debug_info *info = get_info();
195
196     if (!HIWORD(src))
197     {
198         if (!src) return "(null)";
199         res = gimme1(6);
200         sprintf(res, "#%04x", LOWORD(src) );
201         return res;
202     }
203     /* save current position to restore it on exception */
204     old_pos = info->str_pos;
205     __TRY
206     {
207         res = put_string_a( src, n );
208     }
209     __EXCEPT(page_fault)
210     {
211         release( old_pos );
212         return "(invalid)";
213     }
214     __ENDTRY
215     return res;
216 }
217
218 /***********************************************************************
219  *              NTDLL_dbgstr_wn
220  */
221 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
222 {
223     char *res, *old_pos;
224     struct debug_info *info = get_info();
225
226     if (!HIWORD(src))
227     {
228         if (!src) return "(null)";
229         res = gimme1(6);
230         sprintf(res, "#%04x", LOWORD(src) );
231         return res;
232     }
233
234     /* save current position to restore it on exception */
235     old_pos = info->str_pos;
236     __TRY
237     {
238         res = put_string_w( src, n );
239     }
240     __EXCEPT(page_fault)
241     {
242         release( old_pos );
243         return "(invalid)";
244     }
245     __ENDTRY
246      return res;
247 }
248
249 /***********************************************************************
250  *              NTDLL_dbgstr_guid
251  */
252 static const char *NTDLL_dbgstr_guid( const GUID *id )
253 {
254     char *str;
255
256     if (!id) return "(null)";
257     if (!HIWORD(id))
258     {
259         str = gimme1(12);
260         sprintf( str, "<guid-0x%04x>", LOWORD(id) );
261     }
262     else
263     {
264         str = gimme1(40);
265         sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
266                  id->Data1, id->Data2, id->Data3,
267                  id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
268                  id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
269     }
270     return str;
271 }
272
273 /***********************************************************************
274  *              NTDLL_dbg_vprintf
275  */
276 static int NTDLL_dbg_vprintf( const char *format, va_list args )
277 {
278     struct debug_info *info = get_info();
279     char *p;
280
281     int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
282                          format, args );
283
284     /* make sure we didn't exceed the buffer length
285      * the two checks are due to glibc changes in vsnprintfs return value
286      * the buffer size can be exceeded in case of a missing \n in
287      * debug output */
288     if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
289     {
290        fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
291                 info->output);
292        info->out_pos = info->output;
293        abort();
294     }
295
296     p = strrchr( info->out_pos, '\n' );
297     if (!p) info->out_pos += ret;
298     else
299     {
300         char *pos = info->output;
301         p++;
302         write( 2, pos, p - pos );
303         /* move beginning of next line to start of buffer */
304         while ((*pos = *p++)) pos++;
305         info->out_pos = pos;
306     }
307     return ret;
308 }
309
310 /***********************************************************************
311  *              NTDLL_dbg_vlog
312  */
313 static int NTDLL_dbg_vlog( int cls, const char *channel,
314                            const char *function, const char *format, va_list args )
315 {
316     static const char *classes[] = { "fixme", "err", "warn", "trace" };
317     int ret = 0;
318
319     if (TRACE_ON(tid))
320         ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
321     if (cls < sizeof(classes)/sizeof(classes[0]))
322         ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
323     if (format)
324         ret += NTDLL_dbg_vprintf( format, args );
325     return ret;
326 }
327
328 /***********************************************************************
329  *              debug_init
330  */
331 DECL_GLOBAL_CONSTRUCTOR(debug_init)
332 {
333     __wine_dbgstr_an   = NTDLL_dbgstr_an;
334     __wine_dbgstr_wn   = NTDLL_dbgstr_wn;
335     __wine_dbgstr_guid = NTDLL_dbgstr_guid;
336     __wine_dbg_vprintf = NTDLL_dbg_vprintf;
337     __wine_dbg_vlog    = NTDLL_dbg_vlog;
338 }