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
22 #include "wine/port.h"
31 #include "wine/debug.h"
32 #include "wine/exception.h"
33 #include "wine/library.h"
34 #include "wine/unicode.h"
39 #include "msvcrt/excpt.h"
41 WINE_DECLARE_DEBUG_CHANNEL(tid
);
43 /* ---------------------------------------------------------------------- */
47 char *str_pos
; /* current position in strings buffer */
48 char *out_pos
; /* current position in output buffer */
49 char strings
[1024]; /* buffer for temporary strings */
50 char output
[1024]; /* current output line */
53 static struct debug_info initial_thread_info
; /* debug info for initial thread */
55 /* filter for page-fault exceptions */
56 static WINE_EXCEPTION_FILTER(page_fault
)
58 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
59 return EXCEPTION_EXECUTE_HANDLER
;
60 return EXCEPTION_CONTINUE_SEARCH
;
63 /* get the debug info pointer for the current thread */
64 static inline struct debug_info
*get_info(void)
66 struct debug_info
*info
= NtCurrentTeb()->debug_info
;
68 if (!info
) NtCurrentTeb()->debug_info
= info
= &initial_thread_info
;
71 info
->str_pos
= info
->strings
;
72 info
->out_pos
= info
->output
;
77 /* allocate some tmp space for a string */
78 static void *gimme1(int n
)
80 struct debug_info
*info
= get_info();
81 char *res
= info
->str_pos
;
83 if (res
+ n
>= &info
->strings
[sizeof(info
->strings
)]) res
= info
->strings
;
84 info
->str_pos
= res
+ n
;
88 /* release extra space that we requested in gimme1() */
89 static inline void release( void *ptr
)
91 struct debug_info
*info
= NtCurrentTeb()->debug_info
;
95 /* put an ASCII string into the debug buffer */
96 inline static char *put_string_a( const char *src
, int n
)
100 if (n
== -1) n
= strlen(src
);
102 else if (n
> 200) n
= 200;
103 dst
= res
= gimme1 (n
* 4 + 6);
107 unsigned char c
= *src
++;
110 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
111 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
112 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
113 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
114 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
116 if (c
>= ' ' && c
<= 126)
121 *dst
++ = '0' + ((c
>> 6) & 7);
122 *dst
++ = '0' + ((c
>> 3) & 7);
123 *dst
++ = '0' + ((c
>> 0) & 7);
139 /* put a Unicode string into the debug buffer */
140 inline static char *put_string_w( const WCHAR
*src
, int n
)
144 if (n
== -1) n
= strlenW(src
);
146 else if (n
> 200) n
= 200;
147 dst
= res
= gimme1 (n
* 5 + 7);
155 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
156 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
157 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
158 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
159 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
161 if (c
>= ' ' && c
<= 126)
166 sprintf(dst
,"%04x",c
);
183 /***********************************************************************
186 static const char *NTDLL_dbgstr_an( const char *src
, int n
)
189 struct debug_info
*info
= get_info();
193 if (!src
) return "(null)";
195 sprintf(res
, "#%04x", LOWORD(src
) );
198 /* save current position to restore it on exception */
199 old_pos
= info
->str_pos
;
202 res
= put_string_a( src
, n
);
213 /***********************************************************************
216 static const char *NTDLL_dbgstr_wn( const WCHAR
*src
, int n
)
219 struct debug_info
*info
= get_info();
223 if (!src
) return "(null)";
225 sprintf(res
, "#%04x", LOWORD(src
) );
229 /* save current position to restore it on exception */
230 old_pos
= info
->str_pos
;
233 res
= put_string_w( src
, n
);
244 /***********************************************************************
247 static const char *NTDLL_dbgstr_guid( const GUID
*id
)
251 if (!id
) return "(null)";
255 sprintf( str
, "<guid-0x%04x>", LOWORD(id
) );
260 sprintf( str
, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
261 id
->Data1
, id
->Data2
, id
->Data3
,
262 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
263 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
268 /***********************************************************************
271 static int NTDLL_dbg_vprintf( const char *format
, va_list args
)
273 struct debug_info
*info
= get_info();
276 int ret
= vsnprintf( info
->out_pos
, sizeof(info
->output
) - (info
->out_pos
- info
->output
),
279 /* make sure we didn't exceed the buffer length
280 * the two checks are due to glibc changes in vsnprintfs return value
281 * the buffer size can be exceeded in case of a missing \n in
283 if ((ret
== -1) || (ret
>= sizeof(info
->output
) - (info
->out_pos
- info
->output
)))
285 fprintf( stderr
, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
287 info
->out_pos
= info
->output
;
291 p
= strrchr( info
->out_pos
, '\n' );
292 if (!p
) info
->out_pos
+= ret
;
295 char *pos
= info
->output
;
297 write( 2, pos
, p
- pos
);
298 /* move beginning of next line to start of buffer */
299 while ((*pos
= *p
++)) pos
++;
305 /***********************************************************************
308 static int NTDLL_dbg_vlog( int cls
, const char *channel
,
309 const char *function
, const char *format
, va_list args
)
311 static const char *classes
[] = { "fixme", "err", "warn", "trace" };
315 ret
= wine_dbg_printf( "%08lx:", (DWORD
)NtCurrentTeb()->tid
);
316 if (cls
< sizeof(classes
)/sizeof(classes
[0]))
317 ret
+= wine_dbg_printf( "%s:%s:%s ", classes
[cls
], channel
+ 1, function
);
319 ret
+= NTDLL_dbg_vprintf( format
, args
);
323 /***********************************************************************
326 DECL_GLOBAL_CONSTRUCTOR(debug_init
)
328 __wine_dbgstr_an
= NTDLL_dbgstr_an
;
329 __wine_dbgstr_wn
= NTDLL_dbgstr_wn
;
330 __wine_dbgstr_guid
= NTDLL_dbgstr_guid
;
331 __wine_dbg_vprintf
= NTDLL_dbg_vprintf
;
332 __wine_dbg_vlog
= NTDLL_dbg_vlog
;