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"
40 #include "ntdll_misc.h"
42 WINE_DECLARE_DEBUG_CHANNEL(tid);
44 static struct __wine_debug_functions default_funcs;
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 ntdll_get_thread_data()->debug_info;
62 /* allocate some tmp space for a string */
63 static char *get_temp_buffer( size_t 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 void release_temp_buffer( char *ptr, size_t size )
76 struct debug_info *info = get_info();
77 info->str_pos = ptr + size;
80 /***********************************************************************
83 static const char *NTDLL_dbgstr_an( const char *src, int n )
86 struct debug_info *info = get_info();
87 /* save current position to restore it on exception */
88 char *old_pos = info->str_pos;
92 res = default_funcs.dbgstr_an( src, n );
96 release_temp_buffer( old_pos, 0 );
103 /***********************************************************************
106 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
109 struct debug_info *info = get_info();
110 /* save current position to restore it on exception */
111 char *old_pos = info->str_pos;
115 res = default_funcs.dbgstr_wn( src, n );
119 release_temp_buffer( old_pos, 0 );
126 /***********************************************************************
129 static int NTDLL_dbg_vprintf( const char *format, va_list args )
131 struct debug_info *info = get_info();
134 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
137 /* make sure we didn't exceed the buffer length
138 * the two checks are due to glibc changes in vsnprintfs return value
139 * the buffer size can be exceeded in case of a missing \n in
141 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
143 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
145 info->out_pos = info->output;
149 p = strrchr( info->out_pos, '\n' );
150 if (!p) info->out_pos += ret;
153 char *pos = info->output;
155 write( 2, pos, p - pos );
156 /* move beginning of next line to start of buffer */
157 while ((*pos = *p++)) pos++;
163 /***********************************************************************
166 static int NTDLL_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
167 const char *function, const char *format, va_list args )
169 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
170 struct debug_info *info = get_info();
173 /* only print header if we are at the beginning of the line */
174 if (info->out_pos == info->output || info->out_pos[-1] == '\n')
177 ret = wine_dbg_printf( "%04lx:", GetCurrentThreadId() );
178 if (cls < sizeof(classes)/sizeof(classes[0]))
179 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel->name, function );
182 ret += NTDLL_dbg_vprintf( format, args );
187 static const struct __wine_debug_functions funcs =
197 /***********************************************************************
200 void debug_init(void)
202 __wine_dbg_set_functions( &funcs, &default_funcs, sizeof(funcs) );