Something went wrong. Try again.
Reactos
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556/* * Management of the debugging channels * * Copyright 2000 Alexandre Julliard * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */
#include "wine/config.h"#include "wine/port.h"
#include <stdlib.h>#include <stdio.h>#include <stdarg.h>#include <string.h>#include <ctype.h>#include <excpt.h>
#define WIN32_NO_STATUS#include "wine/debug.h"#include "wine/library.h"
#include "winternl.h"
WINE_DECLARE_DEBUG_CHANNEL(pid);WINE_DECLARE_DEBUG_CHANNEL(tid);
static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
#define MAX_DEBUG_OPTIONS 256
static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);static int nb_debug_options = -1;static struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
static struct __wine_debug_functions funcs;
static void debug_init(void);
/* Wine format */static int winefmt_default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *file, const char *func, const int line, const char *format, va_list args );/* ReactOS format (default) */static int rosfmt_default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *file, const char *func, const int line, const char *format, va_list args );/* Extended format */static int extfmt_default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *file, const char *func, const int line, const char *format, va_list args );
static int __cdecl cmp_name( const void *p1, const void *p2 ){ const char *name = p1; const struct __wine_debug_channel *chan = p2; return strcmp( name, chan->name );}
/* get the flags to use for a given channel, possibly setting them too in case of lazy init */unsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel ){ if (nb_debug_options == -1) debug_init();
if (nb_debug_options) { struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options, sizeof(debug_options[0]), cmp_name ); if (opt) return opt->flags; } /* no option for this channel */ if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags; return default_flags;}
/* set the flags to use for a given channel; return 0 if the channel is not available to set */int __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel, unsigned char set, unsigned char clear ){ if (nb_debug_options == -1) debug_init();
if (nb_debug_options) { struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options, sizeof(debug_options[0]), cmp_name ); if (opt) { opt->flags = (opt->flags & ~clear) | set; return 1; } } return 0;}
/* add a new debug option at the end of the option list */static void add_option( const char *name, unsigned char set, unsigned char clear ){ int min = 0, max = nb_debug_options - 1, pos, res;
if (!name[0]) /* "all" option */ { default_flags = (default_flags & ~clear) | set; return; } if (strlen(name) >= sizeof(debug_options[0].name)) return;
while (min <= max) { pos = (min + max) / 2; res = strcmp( name, debug_options[pos].name ); if (!res) { debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set; return; } if (res < 0) max = pos - 1; else min = pos + 1; } if (nb_debug_options >= MAX_DEBUG_OPTIONS) return;
pos = min; if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos], (nb_debug_options - pos) * sizeof(debug_options[0]) ); strcpy( debug_options[pos].name, name ); debug_options[pos].flags = (default_flags & ~clear) | set; nb_debug_options++;}
/* parse a set of debugging option specifications and add them to the option list */static void parse_options( const char *str ){ char *opt, *next, *options; unsigned int i;
if (!(options = _strdup(str))) return; for (opt = options; opt; opt = next) { const char *p; unsigned char set = 0, clear = 0;
if ((next = strchr( opt, ',' ))) *next++ = 0;
p = opt + strcspn( opt, "+-" ); if (!p[0]) p = opt; /* assume it's a debug channel name */
if (p > opt) { for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++) { int len = strlen(debug_classes[i]); if (len != (p - opt)) continue; if (!memcmp( opt, debug_classes[i], len )) /* found it */ { if (*p == '+') set |= 1 << i; else clear |= 1 << i; break; } } if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */ continue; } else { if (*p == '-') clear = ~0; else set = ~0; } if (*p == '+' || *p == '-') p++; if (!p[0]) continue;
if (!strcmp( p, "all" )) default_flags = (default_flags & ~clear) | set; else add_option( p, set, clear ); } free( options );}
/* * The syntax of the DEBUGCHANNEL environment variable is: * DEBUGCHANNEL=[class]+xxx,[class]-yyy,... * * For example: DEBUGCHANNEL=+all,warn-heap * turns on all messages except warning heap messages. * * The available message classes are: err, warn, fixme, trace. * * In order to select a different debug trace format, the * DEBUGFORMAT environment variable should be used: * * DEBUGFORMAT=fmt * * where fmt is the format name: 'wine', or 'extended' (abbreviation: 'ext'). * If no format or an invalid one is specified, the fall-back default format * is used instead. */
/* initialize all options at startup */static void debug_init(void){ char *wine_debug; DWORD dwLength; /* GetEnvironmentVariableA will change LastError! */ DWORD LastError = GetLastError();
if (nb_debug_options != -1) return; /* already initialized */ nb_debug_options = 0;
dwLength = GetEnvironmentVariableA("DEBUGCHANNEL", NULL, 0); if (dwLength) { wine_debug = malloc(dwLength); if (wine_debug) { if (GetEnvironmentVariableA("DEBUGCHANNEL", wine_debug, dwLength) < dwLength) parse_options(wine_debug); free(wine_debug); } }
dwLength = GetEnvironmentVariableA("DEBUGFORMAT", NULL, 0); if (dwLength) { wine_debug = malloc(dwLength); if (wine_debug) { if (GetEnvironmentVariableA("DEBUGFORMAT", wine_debug, dwLength) < dwLength) { if (strcmp(wine_debug, "wine") == 0) { funcs.dbg_vlog = winefmt_default_dbg_vlog; } else if (strcmp(wine_debug, "extended") == 0 || strcmp(wine_debug, "ext") == 0) { funcs.dbg_vlog = extfmt_default_dbg_vlog; } else { funcs.dbg_vlog = rosfmt_default_dbg_vlog; } } free(wine_debug); } }
SetLastError(LastError);}
/* varargs wrapper for funcs.dbg_vprintf */int wine_dbg_printf( const char *format, ... ){ int ret; va_list valist;
va_start(valist, format); ret = funcs.dbg_vprintf( format, valist ); va_end(valist); return ret;}
/* printf with temp buffer allocation */const char *wine_dbg_sprintf( const char *format, ... ){ static const int max_size = 200; char *ret; int len; va_list valist;
va_start(valist, format); ret = funcs.get_temp_buffer( max_size ); len = vsnprintf( ret, max_size, format, valist ); if (len == -1 || len >= max_size) ret[max_size-1] = 0; else funcs.release_temp_buffer( ret, len + 1 ); va_end(valist); return ret;}
/* varargs wrapper for funcs.dbg_vlog */int wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *func, const char *format, ... ){ int ret; va_list valist;
if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
va_start(valist, format); ret = funcs.dbg_vlog( cls, channel, NULL, func, 0, format, valist ); va_end(valist); return ret;}
/* ReactOS compliant debug format wrapper for funcs.dbg_vlog */int ros_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *file, const char *func, const int line, const char *format, ... ){ int ret; va_list valist;
if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
va_start(valist, format); ret = funcs.dbg_vlog( cls, channel, file, func, line, format, valist ); va_end(valist); return ret;}
/* allocate some tmp string space *//* FIXME: this is not 100% thread-safe */static char *get_temp_buffer( size_t size ){ static char *list[32]; static long pos = 0; char *ret; long idx;
idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0])); if ((ret = realloc( list[idx], size ))) list[idx] = ret; return ret;}
/* release unused part of the buffer */static void release_temp_buffer( char *buffer, size_t size ){ /* don't bother doing anything */}
/* default implementation of wine_dbgstr_an */static const char *default_dbgstr_an( const char *str, int n ){ static const char hex[16] = "0123456789abcdef"; char *dst, *res; size_t size;
if (!((ULONG_PTR)str >> 16)) { if (!str) return "(null)"; res = funcs.get_temp_buffer( 6 ); sprintf( res, "#%04x", LOWORD(str) ); return res; } if (n == -1) n = strlen(str); if (n < 0) n = 0; size = 10 + min( 300, n * 4 ); dst = res = funcs.get_temp_buffer( size ); *dst++ = '"'; while (n-- > 0 && dst <= res + size - 9) { unsigned char c = *str++; switch (c) { case '\n': *dst++ = '\\'; *dst++ = 'n'; break; case '\r': *dst++ = '\\'; *dst++ = 'r'; break; case '\t': *dst++ = '\\'; *dst++ = 't'; break; case '"': *dst++ = '\\'; *dst++ = '"'; break; case '\\': *dst++ = '\\'; *dst++ = '\\'; break; default: if (c >= ' ' && c <= 126) *dst++ = c; else { *dst++ = '\\'; *dst++ = 'x'; *dst++ = hex[(c >> 4) & 0x0f]; *dst++ = hex[c & 0x0f]; } } } *dst++ = '"'; if (n > 0) { *dst++ = '.'; *dst++ = '.'; *dst++ = '.'; } *dst++ = 0; funcs.release_temp_buffer( res, dst - res ); return res;}
/* default implementation of wine_dbgstr_wn */static const char *default_dbgstr_wn( const WCHAR *str, int n ){ char *dst, *res; size_t size;
if (!((ULONG_PTR)str >> 16)) { if (!str) return "(null)"; res = funcs.get_temp_buffer( 6 ); sprintf( res, "#%04x", LOWORD(str) ); return res; } if (n == -1) { const WCHAR *end = str; while (*end) end++; n = end - str; } if (n < 0) n = 0; size = 12 + min( 300, n * 5 ); dst = res = funcs.get_temp_buffer( size ); *dst++ = 'L'; *dst++ = '"'; while (n-- > 0 && dst <= res + size - 10) { WCHAR c = *str++; switch (c) { case '\n': *dst++ = '\\'; *dst++ = 'n'; break; case '\r': *dst++ = '\\'; *dst++ = 'r'; break; case '\t': *dst++ = '\\'; *dst++ = 't'; break; case '"': *dst++ = '\\'; *dst++ = '"'; break; case '\\': *dst++ = '\\'; *dst++ = '\\'; break; default: if (c >= ' ' && c <= 126) *dst++ = c; else { *dst++ = '\\'; sprintf(dst,"%04x",c); dst+=4; } } } *dst++ = '"'; if (n > 0) { *dst++ = '.'; *dst++ = '.'; *dst++ = '.'; } *dst++ = 0; funcs.release_temp_buffer( res, dst - res ); return res;}
/* default implementation of wine_dbg_vprintf */static int default_dbg_vprintf( const char *format, va_list args ){ return vDbgPrintExWithPrefix("", -1, 0, format, args);}
/* default implementation of wine_dbg_vlog *//* Wine format */static int winefmt_default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *file, const char *func, const int line, const char *format, va_list args ){ int ret = 0;
if (TRACE_ON(pid)) ret += wine_dbg_printf( "%04x:", HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess) ); ret += wine_dbg_printf( "%04x:", HandleToULong(NtCurrentTeb()->ClientId.UniqueThread) );
if (cls < sizeof(debug_classes)/sizeof(debug_classes[0])) ret += wine_dbg_printf( "%s:%s:%s ", debug_classes[cls], channel->name, func ); if (format) ret += funcs.dbg_vprintf( format, args ); return ret;}/* ReactOS format (default) */static int rosfmt_default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *file, const char *func, const int line, const char *format, va_list args ){ int ret = 0;
if (TRACE_ON(tid)) ret += wine_dbg_printf( "%04x:", HandleToULong(NtCurrentTeb()->ClientId.UniqueThread) );
if (cls < sizeof(debug_classes)/sizeof(debug_classes[0])) ret += wine_dbg_printf( "%s:", debug_classes[cls] );
if (file && line) ret += wine_dbg_printf( "(%s:%d) ", file, line ); else ret += wine_dbg_printf( "%s:%s: ", channel->name, func );
if (format) ret += funcs.dbg_vprintf( format, args ); return ret;}/* Extended format */static int extfmt_default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel, const char *file, const char *func, const int line, const char *format, va_list args ){ int ret = 0;
if (TRACE_ON(pid) || TRACE_ON(tid)) { ret += wine_dbg_printf( "[%04x:%04x]:", (TRACE_ON(pid) ? HandleToULong(NtCurrentTeb()->ClientId.UniqueProcess) : 0), (TRACE_ON(tid) ? HandleToULong(NtCurrentTeb()->ClientId.UniqueThread) : 0) ); }
if (cls < sizeof(debug_classes)/sizeof(debug_classes[0])) ret += wine_dbg_printf( "%s:", debug_classes[cls] );
if (file && line) ret += wine_dbg_printf( "(%s:%d):", file, line );
ret += wine_dbg_printf( "%s:%s ", channel->name, func );
if (format) ret += funcs.dbg_vprintf( format, args ); return ret;}
/* wrappers to use the function pointers */
const char *wine_dbgstr_an( const char * s, int n ){ return funcs.dbgstr_an(s, n);}
const char *wine_dbgstr_wn( const WCHAR *s, int n ){ return funcs.dbgstr_wn(s, n);}
void __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs, struct __wine_debug_functions *old_funcs, size_t size ){ if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) ); if (new_funcs) memcpy( &funcs, new_funcs, min(sizeof(funcs),size) );}
static struct __wine_debug_functions funcs ={ get_temp_buffer, release_temp_buffer, default_dbgstr_an, default_dbgstr_wn, default_dbg_vprintf, rosfmt_default_dbg_vlog};