TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / ntdll / debugtools.c
blobeb43224967da434cbd2142cc177d31ec0eb9e0c1
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winnt.h"
38 #include "winternl.h"
39 #include "ntdll_misc.h"
41 WINE_DECLARE_DEBUG_CHANNEL(tid);
42 WINE_DECLARE_DEBUG_CHANNEL(pid);
43 WINE_DECLARE_DEBUG_CHANNEL(timestamp);
45 static struct __wine_debug_functions default_funcs;
47 /* ---------------------------------------------------------------------- */
49 /* get the debug info pointer for the current thread */
50 static inline struct debug_info *get_info(void)
52 return ntdll_get_thread_data()->debug_info;
55 /* allocate some tmp space for a string */
56 static char *get_temp_buffer( size_t n )
58 struct debug_info *info = get_info();
59 char *res = info->str_pos;
61 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
62 info->str_pos = res + n;
63 return res;
66 /* release extra space that we requested in get_temp_buffer() */
67 static void release_temp_buffer( char *ptr, size_t size )
69 struct debug_info *info = get_info();
70 info->str_pos = ptr + size;
73 /***********************************************************************
74 * NTDLL_dbgstr_an
76 static const char *NTDLL_dbgstr_an( const char *src, int n )
78 const char *res;
79 struct debug_info *info = get_info();
80 /* save current position to restore it on exception */
81 char *old_pos = info->str_pos;
83 __TRY
85 res = default_funcs.dbgstr_an( src, n );
87 __EXCEPT_PAGE_FAULT
89 release_temp_buffer( old_pos, 0 );
90 return "(invalid)";
92 __ENDTRY
93 return res;
96 /***********************************************************************
97 * NTDLL_dbgstr_wn
99 static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
101 const char *res;
102 struct debug_info *info = get_info();
103 /* save current position to restore it on exception */
104 char *old_pos = info->str_pos;
106 __TRY
108 res = default_funcs.dbgstr_wn( src, n );
110 __EXCEPT_PAGE_FAULT
112 release_temp_buffer( old_pos, 0 );
113 return "(invalid)";
115 __ENDTRY
116 return res;
119 /***********************************************************************
120 * NTDLL_dbg_vprintf
122 static int NTDLL_dbg_vprintf( const char *format, va_list args )
124 struct debug_info *info = get_info();
125 int end;
127 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
128 format, args );
130 /* make sure we didn't exceed the buffer length
131 * the two checks are due to glibc changes in vsnprintfs return value
132 * the buffer size can be exceeded in case of a missing \n in
133 * debug output */
134 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
136 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
137 info->output);
138 info->out_pos = info->output;
139 abort();
142 for (end = ret; end > 0; end--) if (info->out_pos[end - 1] == '\n') break;
144 if (!end) info->out_pos += ret;
145 else
147 char *pos = info->output;
148 write( 2, pos, info->out_pos + end - pos );
149 /* move beginning of next line to start of buffer */
150 memmove( pos, info->out_pos + end, ret - end );
151 info->out_pos = pos + ret - end;
153 return ret;
156 /***********************************************************************
157 * NTDLL_dbg_vlog
159 static int NTDLL_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
160 const char *function, const char *format, va_list args )
162 static const char * const classes[] = { "fixme", "err", "warn", "trace" };
163 struct debug_info *info = get_info();
164 int ret = 0;
166 /* only print header if we are at the beginning of the line */
167 if (info->out_pos == info->output || info->out_pos[-1] == '\n')
169 if (TRACE_ON(timestamp))
171 ULONG ticks = NtGetTickCount();
172 ret = wine_dbg_printf( "%3u.%03u:", ticks / 1000, ticks % 1000 );
174 if (TRACE_ON(pid))
175 ret += wine_dbg_printf( "%04x:", GetCurrentProcessId() );
176 if (TRACE_ON(tid))
177 ret += wine_dbg_printf( "%04x:", GetCurrentThreadId() );
178 if (cls < sizeof(classes)/sizeof(classes[0]))
179 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel->name, function );
181 if (format)
182 ret += NTDLL_dbg_vprintf( format, args );
183 return ret;
187 static const struct __wine_debug_functions funcs =
189 get_temp_buffer,
190 release_temp_buffer,
191 NTDLL_dbgstr_an,
192 NTDLL_dbgstr_wn,
193 NTDLL_dbg_vprintf,
194 NTDLL_dbg_vlog
197 /***********************************************************************
198 * debug_init
200 void debug_init(void)
202 __wine_dbg_set_functions( &funcs, &default_funcs, sizeof(funcs) );