Fixed typo.
[wine/gsoc_dplay.git] / dlls / ntdll / debugtools.c
blob17f73b196506ba082017eef938b59c12ffd41576
1 /*
2 * Debugging functions
4 * Copyright 2000 Alexandre Julliard
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.
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.
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include "config.h"
25 #include "wine/port.h"
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>
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 "winternl.h"
44 #include "excpt.h"
46 WINE_DECLARE_DEBUG_CHANNEL(tid);
48 /* ---------------------------------------------------------------------- */
50 /* filter for page-fault exceptions */
51 static WINE_EXCEPTION_FILTER(page_fault)
53 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
54 return EXCEPTION_EXECUTE_HANDLER;
55 return EXCEPTION_CONTINUE_SEARCH;
58 /* get the debug info pointer for the current thread */
59 static inline struct debug_info *get_info(void)
61 return NtCurrentTeb()->debug_info;
64 /* allocate some tmp space for a string */
65 static void *gimme1(int n)
67 struct debug_info *info = get_info();
68 char *res = info->str_pos;
70 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
71 info->str_pos = res + n;
72 return res;
75 /* release extra space that we requested in gimme1() */
76 static inline void release( void *ptr )
78 struct debug_info *info = NtCurrentTeb()->debug_info;
79 info->str_pos = ptr;
82 /* put an ASCII string into the debug buffer */
83 inline static char *put_string_a( const char *src, int n )
85 static const char hex[16] = "0123456789abcdef";
86 char *dst, *res;
88 if (n == -1) n = strlen(src);
89 if (n < 0) n = 0;
90 else if (n > 80) n = 80;
91 dst = res = gimme1 (n * 4 + 6);
92 *dst++ = '"';
93 while (n-- > 0)
95 unsigned char c = *src++;
96 switch (c)
98 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
99 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
100 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
101 case '"': *dst++ = '\\'; *dst++ = '"'; break;
102 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
103 default:
104 if (c >= ' ' && c <= 126)
105 *dst++ = c;
106 else
108 *dst++ = '\\';
109 *dst++ = 'x';
110 *dst++ = hex[(c >> 4) & 0x0f];
111 *dst++ = hex[c & 0x0f];
115 *dst++ = '"';
116 if (*src)
118 *dst++ = '.';
119 *dst++ = '.';
120 *dst++ = '.';
122 *dst++ = '\0';
123 release( dst );
124 return res;
127 /* put a Unicode string into the debug buffer */
128 inline static char *put_string_w( const WCHAR *src, int n )
130 char *dst, *res;
132 if (n == -1) n = strlenW(src);
133 if (n < 0) n = 0;
134 else if (n > 80) n = 80;
135 dst = res = gimme1 (n * 5 + 7);
136 *dst++ = 'L';
137 *dst++ = '"';
138 while (n-- > 0)
140 WCHAR c = *src++;
141 switch (c)
143 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
144 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
145 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
146 case '"': *dst++ = '\\'; *dst++ = '"'; break;
147 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
148 default:
149 if (c >= ' ' && c <= 126)
150 *dst++ = c;
151 else
153 *dst++ = '\\';
154 sprintf(dst,"%04x",c);
155 dst+=4;
159 *dst++ = '"';
160 if (*src)
162 *dst++ = '.';
163 *dst++ = '.';
164 *dst++ = '.';
166 *dst++ = '\0';
167 release( dst );
168 return res;
171 /***********************************************************************
172 * NTDLL_dbgstr_an
174 static const char *NTDLL_dbgstr_an( const char *src, int n )
176 char *res, *old_pos;
177 struct debug_info *info = get_info();
179 if (!HIWORD(src))
181 if (!src) return "(null)";
182 res = gimme1(6);
183 sprintf(res, "#%04x", LOWORD(src) );
184 return res;
186 /* save current position to restore it on exception */
187 old_pos = info->str_pos;
188 __TRY
190 res = put_string_a( src, n );
192 __EXCEPT(page_fault)
194 release( old_pos );
195 return "(invalid)";
197 __ENDTRY
198 return res;
201 /***********************************************************************
202 * NTDLL_dbgstr_wn
204 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
206 char *res, *old_pos;
207 struct debug_info *info = get_info();
209 if (!HIWORD(src))
211 if (!src) return "(null)";
212 res = gimme1(6);
213 sprintf(res, "#%04x", LOWORD(src) );
214 return res;
217 /* save current position to restore it on exception */
218 old_pos = info->str_pos;
219 __TRY
221 res = put_string_w( src, n );
223 __EXCEPT(page_fault)
225 release( old_pos );
226 return "(invalid)";
228 __ENDTRY
229 return res;
232 /***********************************************************************
233 * NTDLL_dbg_vsprintf
235 static const char *NTDLL_dbg_vsprintf( const char *format, va_list args )
237 static const int max_size = 200;
239 char *res = gimme1( max_size );
240 int len = vsnprintf( res, max_size, format, args );
241 if (len == -1 || len >= max_size) res[max_size-1] = 0;
242 else release( res + len + 1 );
243 return res;
246 /***********************************************************************
247 * NTDLL_dbg_vprintf
249 static int NTDLL_dbg_vprintf( const char *format, va_list args )
251 struct debug_info *info = get_info();
252 char *p;
254 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
255 format, args );
257 /* make sure we didn't exceed the buffer length
258 * the two checks are due to glibc changes in vsnprintfs return value
259 * the buffer size can be exceeded in case of a missing \n in
260 * debug output */
261 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
263 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
264 info->output);
265 info->out_pos = info->output;
266 abort();
269 p = strrchr( info->out_pos, '\n' );
270 if (!p) info->out_pos += ret;
271 else
273 char *pos = info->output;
274 p++;
275 write( 2, pos, p - pos );
276 /* move beginning of next line to start of buffer */
277 while ((*pos = *p++)) pos++;
278 info->out_pos = pos;
280 return ret;
283 /***********************************************************************
284 * NTDLL_dbg_vlog
286 static int NTDLL_dbg_vlog( unsigned int cls, const char *channel,
287 const char *function, const char *format, va_list args )
289 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
290 struct debug_info *info = get_info();
291 int ret = 0;
293 /* only print header if we are at the beginning of the line */
294 if (info->out_pos == info->output || info->out_pos[-1] == '\n')
296 if (TRACE_ON(tid))
297 ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
298 if (cls < sizeof(classes)/sizeof(classes[0]))
299 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
301 if (format)
302 ret += NTDLL_dbg_vprintf( format, args );
303 return ret;
306 /***********************************************************************
307 * debug_init
309 DECL_GLOBAL_CONSTRUCTOR(debug_init)
311 __wine_dbgstr_an = NTDLL_dbgstr_an;
312 __wine_dbgstr_wn = NTDLL_dbgstr_wn;
313 __wine_dbg_vsprintf = NTDLL_dbg_vsprintf;
314 __wine_dbg_vprintf = NTDLL_dbg_vprintf;
315 __wine_dbg_vlog = NTDLL_dbg_vlog;