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"
33 #include "wine/debug.h"
34 #include "wine/exception.h"
35 #include "wine/library.h"
36 #include "wine/unicode.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
;
73 /* release extra space that we requested in gimme1() */
74 static inline void release( void *ptr
)
76 struct debug_info
*info
= NtCurrentTeb()->debug_info
;
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";
86 if (n
== -1) n
= strlen(src
);
88 else if (n
> 80) n
= 80;
89 dst
= res
= gimme1 (n
* 4 + 6);
93 unsigned char c
= *src
++;
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;
102 if (c
>= ' ' && c
<= 126)
108 *dst
++ = hex
[(c
>> 4) & 0x0f];
109 *dst
++ = hex
[c
& 0x0f];
125 /* put a Unicode string into the debug buffer */
126 inline static char *put_string_w( const WCHAR
*src
, int n
)
130 if (n
== -1) n
= strlenW(src
);
132 else if (n
> 80) n
= 80;
133 dst
= res
= gimme1 (n
* 5 + 7);
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;
147 if (c
>= ' ' && c
<= 126)
152 sprintf(dst
,"%04x",c
);
169 /***********************************************************************
172 static const char *NTDLL_dbgstr_an( const char *src
, int n
)
175 struct debug_info
*info
= get_info();
179 if (!src
) return "(null)";
181 sprintf(res
, "#%04x", LOWORD(src
) );
184 /* save current position to restore it on exception */
185 old_pos
= info
->str_pos
;
188 res
= put_string_a( src
, n
);
199 /***********************************************************************
202 static const char *NTDLL_dbgstr_wn( const WCHAR
*src
, int n
)
205 struct debug_info
*info
= get_info();
209 if (!src
) return "(null)";
211 sprintf(res
, "#%04x", LOWORD(src
) );
215 /* save current position to restore it on exception */
216 old_pos
= info
->str_pos
;
219 res
= put_string_w( src
, n
);
230 /***********************************************************************
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 );
244 /***********************************************************************
247 static int NTDLL_dbg_vprintf( const char *format
, va_list args
)
249 struct debug_info
*info
= get_info();
252 int ret
= vsnprintf( info
->out_pos
, sizeof(info
->output
) - (info
->out_pos
- info
->output
),
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
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",
263 info
->out_pos
= info
->output
;
267 p
= strrchr( info
->out_pos
, '\n' );
268 if (!p
) info
->out_pos
+= ret
;
271 char *pos
= info
->output
;
273 write( 2, pos
, p
- pos
);
274 /* move beginning of next line to start of buffer */
275 while ((*pos
= *p
++)) pos
++;
281 /***********************************************************************
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();
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')
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
);
300 ret
+= NTDLL_dbg_vprintf( format
, args
);
304 /***********************************************************************
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
;