Remove redundant includes.
[wine/wine64.git] / dlls / ntdll / debugtools.c
blobb8a870689a09c6ddb694f4b50504fd5b23681aa3
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 <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>
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"
44 WINE_DECLARE_DEBUG_CHANNEL(tid);
46 /* ---------------------------------------------------------------------- */
48 /* filter for page-fault exceptions */
49 static WINE_EXCEPTION_FILTER(page_fault)
51 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
52 return EXCEPTION_EXECUTE_HANDLER;
53 return EXCEPTION_CONTINUE_SEARCH;
56 /* get the debug info pointer for the current thread */
57 static inline struct debug_info *get_info(void)
59 return NtCurrentTeb()->debug_info;
62 /* allocate some tmp space for a string */
63 static void *gimme1(int n)
65 struct debug_info *info = get_info();
66 char *res = info->str_pos;
68 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
69 info->str_pos = res + n;
70 return res;
73 /* release extra space that we requested in gimme1() */
74 static inline void release( void *ptr )
76 struct debug_info *info = NtCurrentTeb()->debug_info;
77 info->str_pos = ptr;
80 /* put an ASCII string into the debug buffer */
81 inline static char *put_string_a( const char *src, int n )
83 static const char hex[16] = "0123456789abcdef";
84 char *dst, *res;
86 if (n == -1) n = strlen(src);
87 if (n < 0) n = 0;
88 else if (n > 80) n = 80;
89 dst = res = gimme1 (n * 4 + 6);
90 *dst++ = '"';
91 while (n-- > 0)
93 unsigned char c = *src++;
94 switch (c)
96 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
97 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
98 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
99 case '"': *dst++ = '\\'; *dst++ = '"'; break;
100 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
101 default:
102 if (c >= ' ' && c <= 126)
103 *dst++ = c;
104 else
106 *dst++ = '\\';
107 *dst++ = 'x';
108 *dst++ = hex[(c >> 4) & 0x0f];
109 *dst++ = hex[c & 0x0f];
113 *dst++ = '"';
114 if (*src)
116 *dst++ = '.';
117 *dst++ = '.';
118 *dst++ = '.';
120 *dst++ = '\0';
121 release( dst );
122 return res;
125 /* put a Unicode string into the debug buffer */
126 inline static char *put_string_w( const WCHAR *src, int n )
128 char *dst, *res;
130 if (n == -1) n = strlenW(src);
131 if (n < 0) n = 0;
132 else if (n > 80) n = 80;
133 dst = res = gimme1 (n * 5 + 7);
134 *dst++ = 'L';
135 *dst++ = '"';
136 while (n-- > 0)
138 WCHAR c = *src++;
139 switch (c)
141 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
142 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
143 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
144 case '"': *dst++ = '\\'; *dst++ = '"'; break;
145 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
146 default:
147 if (c >= ' ' && c <= 126)
148 *dst++ = c;
149 else
151 *dst++ = '\\';
152 sprintf(dst,"%04x",c);
153 dst+=4;
157 *dst++ = '"';
158 if (*src)
160 *dst++ = '.';
161 *dst++ = '.';
162 *dst++ = '.';
164 *dst++ = '\0';
165 release( dst );
166 return res;
169 /***********************************************************************
170 * NTDLL_dbgstr_an
172 static const char *NTDLL_dbgstr_an( const char *src, int n )
174 char *res, *old_pos;
175 struct debug_info *info = get_info();
177 if (!HIWORD(src))
179 if (!src) return "(null)";
180 res = gimme1(6);
181 sprintf(res, "#%04x", LOWORD(src) );
182 return res;
184 /* save current position to restore it on exception */
185 old_pos = info->str_pos;
186 __TRY
188 res = put_string_a( src, n );
190 __EXCEPT(page_fault)
192 release( old_pos );
193 return "(invalid)";
195 __ENDTRY
196 return res;
199 /***********************************************************************
200 * NTDLL_dbgstr_wn
202 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
204 char *res, *old_pos;
205 struct debug_info *info = get_info();
207 if (!HIWORD(src))
209 if (!src) return "(null)";
210 res = gimme1(6);
211 sprintf(res, "#%04x", LOWORD(src) );
212 return res;
215 /* save current position to restore it on exception */
216 old_pos = info->str_pos;
217 __TRY
219 res = put_string_w( src, n );
221 __EXCEPT(page_fault)
223 release( old_pos );
224 return "(invalid)";
226 __ENDTRY
227 return res;
230 /***********************************************************************
231 * NTDLL_dbg_vsprintf
233 static const char *NTDLL_dbg_vsprintf( const char *format, va_list args )
235 static const int max_size = 200;
237 char *res = gimme1( max_size );
238 int len = vsnprintf( res, max_size, format, args );
239 if (len == -1 || len >= max_size) res[max_size-1] = 0;
240 else release( res + len + 1 );
241 return res;
244 /***********************************************************************
245 * NTDLL_dbg_vprintf
247 static int NTDLL_dbg_vprintf( const char *format, va_list args )
249 struct debug_info *info = get_info();
250 char *p;
252 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
253 format, args );
255 /* make sure we didn't exceed the buffer length
256 * the two checks are due to glibc changes in vsnprintfs return value
257 * the buffer size can be exceeded in case of a missing \n in
258 * debug output */
259 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
261 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
262 info->output);
263 info->out_pos = info->output;
264 abort();
267 p = strrchr( info->out_pos, '\n' );
268 if (!p) info->out_pos += ret;
269 else
271 char *pos = info->output;
272 p++;
273 write( 2, pos, p - pos );
274 /* move beginning of next line to start of buffer */
275 while ((*pos = *p++)) pos++;
276 info->out_pos = pos;
278 return ret;
281 /***********************************************************************
282 * NTDLL_dbg_vlog
284 static int NTDLL_dbg_vlog( unsigned int cls, const char *channel,
285 const char *function, const char *format, va_list args )
287 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
288 struct debug_info *info = get_info();
289 int ret = 0;
291 /* only print header if we are at the beginning of the line */
292 if (info->out_pos == info->output || info->out_pos[-1] == '\n')
294 if (TRACE_ON(tid))
295 ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
296 if (cls < sizeof(classes)/sizeof(classes[0]))
297 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
299 if (format)
300 ret += NTDLL_dbg_vprintf( format, args );
301 return ret;
304 /***********************************************************************
305 * debug_init
307 DECL_GLOBAL_CONSTRUCTOR(debug_init)
309 __wine_dbgstr_an = NTDLL_dbgstr_an;
310 __wine_dbgstr_wn = NTDLL_dbgstr_wn;
311 __wine_dbg_vsprintf = NTDLL_dbg_vsprintf;
312 __wine_dbg_vprintf = NTDLL_dbg_vprintf;
313 __wine_dbg_vlog = NTDLL_dbg_vlog;