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
28 #include "wine/debug.h"
29 #include "wine/exception.h"
35 #include "msvcrt/excpt.h"
37 WINE_DECLARE_DEBUG_CHANNEL(tid
);
39 /* ---------------------------------------------------------------------- */
43 char *str_pos
; /* current position in strings buffer */
44 char *out_pos
; /* current position in output buffer */
45 char strings
[1024]; /* buffer for temporary strings */
46 char output
[1024]; /* current output line */
49 static struct debug_info initial_thread_info
; /* debug info for initial thread */
51 /* filter for page-fault exceptions */
52 static WINE_EXCEPTION_FILTER(page_fault
)
54 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
55 return EXCEPTION_EXECUTE_HANDLER
;
56 return EXCEPTION_CONTINUE_SEARCH
;
59 /* get the debug info pointer for the current thread */
60 static inline struct debug_info
*get_info(void)
62 struct debug_info
*info
= NtCurrentTeb()->debug_info
;
64 if (!info
) NtCurrentTeb()->debug_info
= info
= &initial_thread_info
;
67 info
->str_pos
= info
->strings
;
68 info
->out_pos
= info
->output
;
73 /* allocate some tmp space for a string */
74 static void *gimme1(int n
)
76 struct debug_info
*info
= get_info();
77 char *res
= info
->str_pos
;
79 if (res
+ n
>= &info
->strings
[sizeof(info
->strings
)]) res
= info
->strings
;
80 info
->str_pos
= res
+ n
;
84 /* release extra space that we requested in gimme1() */
85 static inline void release( void *ptr
)
87 struct debug_info
*info
= NtCurrentTeb()->debug_info
;
91 /* put an ASCII string into the debug buffer */
92 inline static char *put_string_a( const char *src
, int n
)
97 else if (n
> 200) n
= 200;
98 dst
= res
= gimme1 (n
* 4 + 6);
100 while (n
-- > 0 && *src
)
102 unsigned char c
= *src
++;
105 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
106 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
107 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
108 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
109 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
111 if (c
>= ' ' && c
<= 126)
116 *dst
++ = '0' + ((c
>> 6) & 7);
117 *dst
++ = '0' + ((c
>> 3) & 7);
118 *dst
++ = '0' + ((c
>> 0) & 7);
134 /* put a Unicode string into the debug buffer */
135 inline static char *put_string_w( const WCHAR
*src
, int n
)
140 else if (n
> 200) n
= 200;
141 dst
= res
= gimme1 (n
* 5 + 7);
144 while (n
-- > 0 && *src
)
149 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
150 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
151 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
152 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
153 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
155 if (c
>= ' ' && c
<= 126)
160 sprintf(dst
,"%04x",c
);
177 /***********************************************************************
178 * wine_dbgstr_an (NTDLL.@)
180 const char *wine_dbgstr_an( const char *src
, int n
)
183 struct debug_info
*info
= get_info();
187 if (!src
) return "(null)";
189 sprintf(res
, "#%04x", LOWORD(src
) );
192 /* save current position to restore it on exception */
193 old_pos
= info
->str_pos
;
196 res
= put_string_a( src
, n
);
207 /***********************************************************************
208 * wine_dbgstr_wn (NTDLL.@)
210 const char *wine_dbgstr_wn( const WCHAR
*src
, int n
)
213 struct debug_info
*info
= get_info();
217 if (!src
) return "(null)";
219 sprintf(res
, "#%04x", LOWORD(src
) );
223 /* save current position to restore it on exception */
224 old_pos
= info
->str_pos
;
227 res
= put_string_w( src
, n
);
238 /***********************************************************************
239 * wine_dbgstr_guid (NTDLL.@)
241 const char *wine_dbgstr_guid( const GUID
*id
)
245 if (!id
) return "(null)";
249 sprintf( str
, "<guid-0x%04x>", LOWORD(id
) );
254 sprintf( str
, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
255 id
->Data1
, id
->Data2
, id
->Data3
,
256 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
257 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
262 /***********************************************************************
263 * wine_dbg_vprintf (NTDLL.@)
265 int wine_dbg_vprintf( const char *format
, va_list args
)
267 struct debug_info
*info
= get_info();
270 int ret
= vsnprintf( info
->out_pos
, sizeof(info
->output
) - (info
->out_pos
- info
->output
),
273 /* make sure we didn't exceed the buffer length
274 * the two checks are due to glibc changes in vsnprintfs return value
275 * the buffer size can be exceeded in case of a missing \n in
277 if ((ret
== -1) || (ret
>= sizeof(info
->output
) - (info
->out_pos
- info
->output
)))
279 fprintf( stderr
, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
281 info
->out_pos
= info
->output
;
285 p
= strrchr( info
->out_pos
, '\n' );
286 if (!p
) info
->out_pos
+= ret
;
289 char *pos
= info
->output
;
291 write( 2, pos
, p
- pos
);
292 /* move beginning of next line to start of buffer */
293 while ((*pos
= *p
++)) pos
++;
299 /***********************************************************************
300 * wine_dbg_printf (NTDLL.@)
302 int wine_dbg_printf(const char *format
, ...)
307 va_start(valist
, format
);
308 ret
= wine_dbg_vprintf( format
, valist
);
313 /***********************************************************************
314 * wine_dbg_log (NTDLL.@)
316 int wine_dbg_log(enum __WINE_DEBUG_CLASS cls
, const char *channel
,
317 const char *function
, const char *format
, ... )
319 static const char *classes
[__WINE_DBCL_COUNT
] = { "fixme", "err", "warn", "trace" };
323 va_start(valist
, format
);
325 ret
= wine_dbg_printf( "%08lx:", (DWORD
)NtCurrentTeb()->tid
);
326 if (cls
< __WINE_DBCL_COUNT
)
327 ret
+= wine_dbg_printf( "%s:%s:%s ", classes
[cls
], channel
+ 1, function
);
329 ret
+= wine_dbg_vprintf( format
, valist
);