Sync to trunk head(r38096)
[reactos.git] / reactos / lib / 3rdparty / libwine / debug.c
1 /*
2 * Management of the debugging channels
3 *
4 * Copyright 2000 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "wine/config.h"
22 #include "wine/port.h"
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include <excpt.h>
30
31 #define WIN32_NO_STATUS
32 #include "wine/debug.h"
33 #include "wine/library.h"
34
35 #include <rtlfuncs.h>
36 #include <cmfuncs.h>
37
38 ULONG
39 NTAPI
40 vDbgPrintExWithPrefix(
41 IN LPCSTR Prefix,
42 IN ULONG ComponentId,
43 IN ULONG Level,
44 IN LPCSTR Format,
45 IN va_list ap);
46
47
48 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
49
50 #define MAX_DEBUG_OPTIONS 256
51
52 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
53 static int nb_debug_options = -1;
54 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
55
56 static struct __wine_debug_functions funcs;
57
58 static void debug_init(void);
59
60 static int __cdecl cmp_name( const void *p1, const void *p2 )
61 {
62 const char *name = p1;
63 const struct __wine_debug_channel *chan = p2;
64 return strcmp( name, chan->name );
65 }
66
67 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
68 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
69 {
70 if (nb_debug_options == -1) debug_init();
71
72 if (nb_debug_options)
73 {
74 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
75 sizeof(debug_options[0]), cmp_name );
76 if (opt) return opt->flags;
77 }
78 /* no option for this channel */
79 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
80 return default_flags;
81 }
82
83 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
84 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
85 unsigned char set, unsigned char clear )
86 {
87 if (nb_debug_options == -1) debug_init();
88
89 if (nb_debug_options)
90 {
91 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
92 sizeof(debug_options[0]), cmp_name );
93 if (opt)
94 {
95 opt->flags = (opt->flags & ~clear) | set;
96 return 1;
97 }
98 }
99 return 0;
100 }
101
102 /* add a new debug option at the end of the option list */
103 static void add_option( const char *name, unsigned char set, unsigned char clear )
104 {
105 int min = 0, max = nb_debug_options - 1, pos, res;
106
107 if (!name[0]) /* "all" option */
108 {
109 default_flags = (default_flags & ~clear) | set;
110 return;
111 }
112 if (strlen(name) >= sizeof(debug_options[0].name)) return;
113
114 while (min <= max)
115 {
116 pos = (min + max) / 2;
117 res = strcmp( name, debug_options[pos].name );
118 if (!res)
119 {
120 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
121 return;
122 }
123 if (res < 0) max = pos - 1;
124 else min = pos + 1;
125 }
126 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
127
128 pos = min;
129 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
130 (nb_debug_options - pos) * sizeof(debug_options[0]) );
131 strcpy( debug_options[pos].name, name );
132 debug_options[pos].flags = (default_flags & ~clear) | set;
133 nb_debug_options++;
134 }
135
136 /* parse a set of debugging option specifications and add them to the option list */
137 static void parse_options( const char *str )
138 {
139 char *opt, *next, *options;
140 unsigned int i;
141
142 if (!(options = _strdup(str))) return;
143 for (opt = options; opt; opt = next)
144 {
145 const char *p;
146 unsigned char set = 0, clear = 0;
147
148 if ((next = strchr( opt, ',' ))) *next++ = 0;
149
150 p = opt + strcspn( opt, "+-" );
151 if (!p[0]) p = opt; /* assume it's a debug channel name */
152
153 if (p > opt)
154 {
155 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
156 {
157 int len = strlen(debug_classes[i]);
158 if (len != (p - opt)) continue;
159 if (!memcmp( opt, debug_classes[i], len )) /* found it */
160 {
161 if (*p == '+') set |= 1 << i;
162 else clear |= 1 << i;
163 break;
164 }
165 }
166 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
167 continue;
168 }
169 else
170 {
171 if (*p == '-') clear = ~0;
172 else set = ~0;
173 }
174 if (*p == '+' || *p == '-') p++;
175 if (!p[0]) continue;
176
177 if (!strcmp( p, "all" ))
178 default_flags = (default_flags & ~clear) | set;
179 else
180 add_option( p, set, clear );
181 }
182 free( options );
183 }
184
185 /* initialize all options at startup */
186 static void debug_init(void)
187 {
188 char *wine_debug;
189 DWORD dwLength;
190 /* GetEnvironmentVariableA will change LastError! */
191 DWORD LastError = GetLastError();
192
193 if (nb_debug_options != -1) return; /* already initialized */
194 nb_debug_options = 0;
195
196 dwLength = GetEnvironmentVariableA("DEBUGCHANNEL", NULL, 0);
197 if (dwLength)
198 {
199 wine_debug = malloc(dwLength);
200 if (wine_debug)
201 {
202 if (GetEnvironmentVariableA("DEBUGCHANNEL", wine_debug, dwLength) < dwLength)
203 parse_options(wine_debug);
204 free(wine_debug);
205 }
206 }
207 SetLastError(LastError);
208 }
209
210 /* varargs wrapper for funcs.dbg_vprintf */
211 int wine_dbg_printf( const char *format, ... )
212 {
213 int ret;
214 va_list valist;
215
216 va_start(valist, format);
217 ret = funcs.dbg_vprintf( format, valist );
218 va_end(valist);
219 return ret;
220 }
221
222 /* printf with temp buffer allocation */
223 const char *wine_dbg_sprintf( const char *format, ... )
224 {
225 static const int max_size = 200;
226 char *ret;
227 int len;
228 va_list valist;
229
230 va_start(valist, format);
231 ret = funcs.get_temp_buffer( max_size );
232 len = vsnprintf( ret, max_size, format, valist );
233 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
234 else funcs.release_temp_buffer( ret, len + 1 );
235 va_end(valist);
236 return ret;
237 }
238
239
240 /* varargs wrapper for funcs.dbg_vlog */
241 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
242 const char *file, const char *func, const int line, const char *format, ... )
243 {
244 int ret;
245 va_list valist;
246
247 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
248
249 va_start(valist, format);
250 ret = funcs.dbg_vlog( cls, channel, file, func, line, format, valist );
251 va_end(valist);
252 return ret;
253 }
254
255
256 /* allocate some tmp string space */
257 /* FIXME: this is not 100% thread-safe */
258 static char *get_temp_buffer( size_t size )
259 {
260 static char *list[32];
261 static long pos;
262 char *ret;
263 int idx;
264
265 idx = InterlockedExchangeAdd( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
266 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
267 return ret;
268 }
269
270
271 /* release unused part of the buffer */
272 static void release_temp_buffer( char *buffer, size_t size )
273 {
274 /* don't bother doing anything */
275 }
276
277
278 /* default implementation of wine_dbgstr_an */
279 static const char *default_dbgstr_an( const char *str, int n )
280 {
281 static const char hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
282 char *dst, *res;
283 size_t size;
284
285 if (!((ULONG_PTR)str >> 16))
286 {
287 if (!str) return "(null)";
288 res = funcs.get_temp_buffer( 6 );
289 sprintf( res, "#%04x", LOWORD(str) );
290 return res;
291 }
292 if (n == -1) n = strlen(str);
293 if (n < 0) n = 0;
294 size = 10 + min( 300, n * 4 );
295 dst = res = funcs.get_temp_buffer( size );
296 *dst++ = '"';
297 while (n-- > 0 && dst <= res + size - 9)
298 {
299 unsigned char c = *str++;
300 switch (c)
301 {
302 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
303 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
304 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
305 case '"': *dst++ = '\\'; *dst++ = '"'; break;
306 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
307 default:
308 if (c >= ' ' && c <= 126)
309 *dst++ = c;
310 else
311 {
312 *dst++ = '\\';
313 *dst++ = 'x';
314 *dst++ = hex[(c >> 4) & 0x0f];
315 *dst++ = hex[c & 0x0f];
316 }
317 }
318 }
319 *dst++ = '"';
320 if (n > 0)
321 {
322 *dst++ = '.';
323 *dst++ = '.';
324 *dst++ = '.';
325 }
326 *dst++ = 0;
327 funcs.release_temp_buffer( res, dst - res );
328 return res;
329 }
330
331
332 /* default implementation of wine_dbgstr_wn */
333 static const char *default_dbgstr_wn( const WCHAR *str, int n )
334 {
335 char *dst, *res;
336 size_t size;
337
338 if (!((ULONG_PTR)str >> 16))
339 {
340 if (!str) return "(null)";
341 res = funcs.get_temp_buffer( 6 );
342 sprintf( res, "#%04x", LOWORD(str) );
343 return res;
344 }
345 if (n == -1)
346 {
347 const WCHAR *end = str;
348 while (*end) end++;
349 n = end - str;
350 }
351 if (n < 0) n = 0;
352 size = 12 + min( 300, n * 5 );
353 dst = res = funcs.get_temp_buffer( n * 5 + 7 );
354 *dst++ = 'L';
355 *dst++ = '"';
356 while (n-- > 0 && dst <= res + size - 10)
357 {
358 WCHAR c = *str++;
359 switch (c)
360 {
361 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
362 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
363 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
364 case '"': *dst++ = '\\'; *dst++ = '"'; break;
365 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
366 default:
367 if (c >= ' ' && c <= 126)
368 *dst++ = c;
369 else
370 {
371 *dst++ = '\\';
372 sprintf(dst,"%04x",c);
373 dst+=4;
374 }
375 }
376 }
377 *dst++ = '"';
378 if (n > 0)
379 {
380 *dst++ = '.';
381 *dst++ = '.';
382 *dst++ = '.';
383 }
384 *dst++ = 0;
385 funcs.release_temp_buffer( res, dst - res );
386 return res;
387 }
388
389
390 /* default implementation of wine_dbg_vprintf */
391 static int default_dbg_vprintf( const char *format, va_list args )
392 {
393 return vDbgPrintExWithPrefix("", -1, 0, format, args);
394 }
395
396
397 /* default implementation of wine_dbg_vlog */
398 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
399 const char *file, const char *func, const int line, const char *format, va_list args )
400 {
401 int ret = 0;
402
403 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
404 ret += wine_dbg_printf( "%s:", debug_classes[cls] );
405 ret += wine_dbg_printf ( "(%s:%d) ", file, line );
406 if (format)
407 ret += funcs.dbg_vprintf( format, args );
408 return ret;
409 }
410
411 /* wrappers to use the function pointers */
412
413 const char *wine_dbgstr_an( const char * s, int n )
414 {
415 return funcs.dbgstr_an(s, n);
416 }
417
418 const char *wine_dbgstr_wn( const WCHAR *s, int n )
419 {
420 return funcs.dbgstr_wn(s, n);
421 }
422
423 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
424 struct __wine_debug_functions *old_funcs, size_t size )
425 {
426 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
427 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
428 }
429
430 static struct __wine_debug_functions funcs =
431 {
432 get_temp_buffer,
433 release_temp_buffer,
434 default_dbgstr_an,
435 default_dbgstr_wn,
436 default_dbg_vprintf,
437 default_dbg_vlog
438 };