wined3d: Add AMD Radeon HD 6480G IGP.
[wine.git] / libs / wine / debug.c
blob8b04ef994cb8a0362202c5beb670f3fe5472f15c
1 /*
2 * Management of the debugging channels
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 <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <ctype.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
33 #include "wine/debug.h"
34 #include "wine/library.h"
36 #if defined(__MINGW32__) || defined(_MSC_VER)
37 WINE_DECLARE_DEBUG_CHANNEL(tid);
38 WINE_DECLARE_DEBUG_CHANNEL(pid);
39 #endif
41 static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
43 #define MAX_DEBUG_OPTIONS 256
45 static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
46 static int nb_debug_options = -1;
47 static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
49 static struct __wine_debug_functions funcs;
51 static void debug_init(void);
53 static int cmp_name( const void *p1, const void *p2 )
55 const char *name = p1;
56 const struct __wine_debug_channel *chan = p2;
57 return strcmp( name, chan->name );
60 /* get the flags to use for a given channel, possibly setting them too in case of lazy init */
61 unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
63 if (nb_debug_options == -1) debug_init();
65 if (nb_debug_options)
67 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
68 sizeof(debug_options[0]), cmp_name );
69 if (opt) return opt->flags;
71 /* no option for this channel */
72 if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
73 return default_flags;
76 /* set the flags to use for a given channel; return 0 if the channel is not available to set */
77 int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
78 unsigned char set, unsigned char clear )
80 if (nb_debug_options == -1) debug_init();
82 if (nb_debug_options)
84 struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
85 sizeof(debug_options[0]), cmp_name );
86 if (opt)
88 opt->flags = (opt->flags & ~clear) | set;
89 return 1;
92 return 0;
95 /* add a new debug option at the end of the option list */
96 static void add_option( const char *name, unsigned char set, unsigned char clear )
98 int min = 0, max = nb_debug_options - 1, pos, res;
100 if (!name[0]) /* "all" option */
102 default_flags = (default_flags & ~clear) | set;
103 return;
105 if (strlen(name) >= sizeof(debug_options[0].name)) return;
107 while (min <= max)
109 pos = (min + max) / 2;
110 res = strcmp( name, debug_options[pos].name );
111 if (!res)
113 debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
114 return;
116 if (res < 0) max = pos - 1;
117 else min = pos + 1;
119 if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
121 pos = min;
122 if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
123 (nb_debug_options - pos) * sizeof(debug_options[0]) );
124 strcpy( debug_options[pos].name, name );
125 debug_options[pos].flags = (default_flags & ~clear) | set;
126 nb_debug_options++;
129 /* parse a set of debugging option specifications and add them to the option list */
130 static void parse_options( const char *str )
132 char *opt, *next, *options;
133 unsigned int i;
135 if (!(options = strdup(str))) return;
136 for (opt = options; opt; opt = next)
138 const char *p;
139 unsigned char set = 0, clear = 0;
141 if ((next = strchr( opt, ',' ))) *next++ = 0;
143 p = opt + strcspn( opt, "+-" );
144 if (!p[0]) p = opt; /* assume it's a debug channel name */
146 if (p > opt)
148 for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
150 int len = strlen(debug_classes[i]);
151 if (len != (p - opt)) continue;
152 if (!memcmp( opt, debug_classes[i], len )) /* found it */
154 if (*p == '+') set |= 1 << i;
155 else clear |= 1 << i;
156 break;
159 if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
160 continue;
162 else
164 if (*p == '-') clear = ~0;
165 else set = ~0;
167 if (*p == '+' || *p == '-') p++;
168 if (!p[0]) continue;
170 if (!strcmp( p, "all" ))
171 default_flags = (default_flags & ~clear) | set;
172 else
173 add_option( p, set, clear );
175 free( options );
179 /* print the usage message */
180 static void debug_usage(void)
182 static const char usage[] =
183 "Syntax of the WINEDEBUG variable:\n"
184 " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
185 "Example: WINEDEBUG=+all,warn-heap\n"
186 " turns on all messages except warning heap messages\n"
187 "Available message classes: err, warn, fixme, trace\n";
188 write( 2, usage, sizeof(usage) - 1 );
189 exit(1);
193 /* initialize all options at startup */
194 static void debug_init(void)
196 char *wine_debug;
197 struct stat st1, st2;
199 if (nb_debug_options != -1) return; /* already initialized */
200 nb_debug_options = 0;
202 /* check for stderr pointing to /dev/null */
203 if (!fstat( 2, &st1 ) && S_ISCHR(st1.st_mode) &&
204 !stat( "/dev/null", &st2 ) && S_ISCHR(st2.st_mode) &&
205 st1.st_rdev == st2.st_rdev)
207 default_flags = 0;
208 return;
210 if ((wine_debug = getenv("WINEDEBUG")))
212 if (!strcmp( wine_debug, "help" )) debug_usage();
213 parse_options( wine_debug );
217 /* varargs wrapper for funcs.dbg_vprintf */
218 int wine_dbg_printf( const char *format, ... )
220 int ret;
221 va_list valist;
223 va_start(valist, format);
224 ret = funcs.dbg_vprintf( format, valist );
225 va_end(valist);
226 return ret;
229 /* printf with temp buffer allocation */
230 const char *wine_dbg_sprintf( const char *format, ... )
232 static const int max_size = 200;
233 char *ret;
234 int len;
235 va_list valist;
237 va_start(valist, format);
238 ret = funcs.get_temp_buffer( max_size );
239 len = vsnprintf( ret, max_size, format, valist );
240 if (len == -1 || len >= max_size) ret[max_size-1] = 0;
241 else funcs.release_temp_buffer( ret, len + 1 );
242 va_end(valist);
243 return ret;
247 /* varargs wrapper for funcs.dbg_vlog */
248 int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
249 const char *func, const char *format, ... )
251 int ret;
252 va_list valist;
254 if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
256 va_start(valist, format);
257 ret = funcs.dbg_vlog( cls, channel, func, format, valist );
258 va_end(valist);
259 return ret;
263 /* allocate some tmp string space */
264 /* FIXME: this is not 100% thread-safe */
265 static char *get_temp_buffer( size_t size )
267 static char *list[32];
268 static int pos;
269 char *ret;
270 int idx;
272 idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
273 if ((ret = realloc( list[idx], size ))) list[idx] = ret;
274 return ret;
278 /* release unused part of the buffer */
279 static void release_temp_buffer( char *buffer, size_t size )
281 /* don't bother doing anything */
285 /* default implementation of wine_dbgstr_an */
286 static const char *default_dbgstr_an( const char *str, int n )
288 static const char hex[16] = "0123456789abcdef";
289 char *dst, *res;
290 size_t size;
292 if (!((ULONG_PTR)str >> 16))
294 if (!str) return "(null)";
295 res = funcs.get_temp_buffer( 6 );
296 sprintf( res, "#%04x", LOWORD(str) );
297 return res;
299 if (n == -1) n = strlen(str);
300 if (n < 0) n = 0;
301 size = 10 + min( 300, n * 4 );
302 dst = res = funcs.get_temp_buffer( size );
303 *dst++ = '"';
304 while (n-- > 0 && dst <= res + size - 9)
306 unsigned char c = *str++;
307 switch (c)
309 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
310 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
311 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
312 case '"': *dst++ = '\\'; *dst++ = '"'; break;
313 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
314 default:
315 if (c >= ' ' && c <= 126)
316 *dst++ = c;
317 else
319 *dst++ = '\\';
320 *dst++ = 'x';
321 *dst++ = hex[(c >> 4) & 0x0f];
322 *dst++ = hex[c & 0x0f];
326 *dst++ = '"';
327 if (n > 0)
329 *dst++ = '.';
330 *dst++ = '.';
331 *dst++ = '.';
333 *dst++ = 0;
334 funcs.release_temp_buffer( res, dst - res );
335 return res;
339 /* default implementation of wine_dbgstr_wn */
340 static const char *default_dbgstr_wn( const WCHAR *str, int n )
342 char *dst, *res;
343 size_t size;
345 if (!((ULONG_PTR)str >> 16))
347 if (!str) return "(null)";
348 res = funcs.get_temp_buffer( 6 );
349 sprintf( res, "#%04x", LOWORD(str) );
350 return res;
352 if (n == -1)
354 const WCHAR *end = str;
355 while (*end) end++;
356 n = end - str;
358 if (n < 0) n = 0;
359 size = 12 + min( 300, n * 5 );
360 dst = res = funcs.get_temp_buffer( size );
361 *dst++ = 'L';
362 *dst++ = '"';
363 while (n-- > 0 && dst <= res + size - 10)
365 WCHAR c = *str++;
366 switch (c)
368 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
369 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
370 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
371 case '"': *dst++ = '\\'; *dst++ = '"'; break;
372 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
373 default:
374 if (c >= ' ' && c <= 126)
375 *dst++ = c;
376 else
378 *dst++ = '\\';
379 sprintf(dst,"%04x",c);
380 dst+=4;
384 *dst++ = '"';
385 if (n > 0)
387 *dst++ = '.';
388 *dst++ = '.';
389 *dst++ = '.';
391 *dst++ = 0;
392 funcs.release_temp_buffer( res, dst - res );
393 return res;
397 /* default implementation of wine_dbg_vprintf */
398 static int default_dbg_vprintf( const char *format, va_list args )
400 return vfprintf( stderr, format, args );
404 /* default implementation of wine_dbg_vlog */
405 static int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
406 const char *func, const char *format, va_list args )
408 int ret = 0;
410 #if defined(__MINGW32__) || defined(_MSC_VER)
411 if (TRACE_ON(pid))
412 ret += wine_dbg_printf( "%04x:", GetCurrentProcessId() );
413 if (TRACE_ON(tid))
414 ret += wine_dbg_printf( "%04x:", GetCurrentThreadId() );
415 #endif
416 if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
417 ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func );
418 if (format)
419 ret += funcs.dbg_vprintf( format, args );
420 return ret;
423 /* wrappers to use the function pointers */
425 const char *wine_dbgstr_an( const char * s, int n )
427 return funcs.dbgstr_an(s, n);
430 const char *wine_dbgstr_wn( const WCHAR *s, int n )
432 return funcs.dbgstr_wn(s, n);
435 void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
436 struct __wine_debug_functions *old_funcs, size_t size )
438 if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );
439 if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );
442 static struct __wine_debug_functions funcs =
444 get_temp_buffer,
445 release_temp_buffer,
446 default_dbgstr_an,
447 default_dbgstr_wn,
448 default_dbg_vprintf,
449 default_dbg_vlog