058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Management of the debugging channels
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Copyright 2000 Alexandre Julliard
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * This library is free software; you can redistribute it and/or
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * modify it under the terms of the GNU Lesser General Public
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * License as published by the Free Software Foundation; either
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * version 2.1 of the License, or (at your option) any later version.
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * This library is distributed in the hope that it will be useful,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * but WITHOUT ANY WARRANTY; without even the implied warranty of
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Lesser General Public License for more details.
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * You should have received a copy of the GNU Lesser General Public
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * License along with this library; if not, write to the Free Software
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * Oracle LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * a choice of LGPL license versions is made available with the language indicating
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * that LGPLv2 or any later version may be used, or where a choice of which version
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync * of the LGPL is applied is otherwise unspecified.
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsynctypedef DECLCALLBACK(void) FNVBOXWINELOGBACKDOOR(char* pcszStr);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsynctypedef FNVBOXWINELOGBACKDOOR *PFNVBOXWINELOGBACKDOOR;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic PFNVBOXWINELOGBACKDOOR vbox_log_backdoor = NULL;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME) | (1 << __WINE_DBCL_WARN);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic struct __wine_debug_channel debug_options[MAX_DEBUG_OPTIONS];
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic void debug_init(void);
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic int cmp_name( const void *p1, const void *p2 )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* get the flags to use for a given channel, possibly setting them too in case of lazy init */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncunsigned char __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* no option for this channel */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* set the flags to use for a given channel; return 0 if the channel is not available to set */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncint __wine_dbg_set_channel_flags( struct __wine_debug_channel *channel,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct __wine_debug_channel *opt = bsearch( channel->name, debug_options, nb_debug_options,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* add a new debug option at the end of the option list */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic void add_option( const char *name, unsigned char set, unsigned char clear )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (strlen(name) >= sizeof(debug_options[0].name)) return;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync (nb_debug_options - pos) * sizeof(debug_options[0]) );
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync debug_options[pos].flags = (default_flags & ~clear) | set;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* parse a set of debugging option specifications and add them to the option list */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync unsigned int i;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync const char *p;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (!p[0]) p = opt; /* assume it's a debug channel name */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync for (i = 0; i < sizeof(debug_classes)/sizeof(debug_classes[0]); i++)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (!memcmp( opt, debug_classes[i], len )) /* found it */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (i == sizeof(debug_classes)/sizeof(debug_classes[0])) /* bad class name, skip it */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (!p[0]) continue;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* print the usage message */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync "Syntax of the WINEDEBUG variable:\n"
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync " WINEDEBUG=[class]+xxx,[class]-yyy,...\n\n"
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync "Example: WINEDEBUG=+all,warn-heap\n"
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync " turns on all messages except warning heap messages\n"
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync "Available message classes: err, warn, fixme, trace\n";
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic DECLCALLBACK(void) vbox_log_backdoor_rt(char* pcszStr)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic DECLCALLBACK(void) vbox_log_backdoor_dispmp(char* pcszStr)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic void vbox_log_v(const char *pszFormat, va_list args)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync int offset = sprintf(buf, "[0x%lx.0x%lx] Wine Debug: ", GetCurrentProcessId(), GetCurrentThreadId());
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* initialize all options at startup */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (nb_debug_options != -1) return; /* already initialized */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* varargs wrapper for funcs.dbg_vprintf */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* printf with temp buffer allocation */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncconst char *wine_dbg_sprintf( const char *format, ... )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (len == -1 || len >= max_size) ret[max_size-1] = 0;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* varargs wrapper for funcs.dbg_vlog */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncint wine_dbg_log( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ret = funcs.dbg_vlog( cls, channel, func, format, valist );
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* allocate some tmp string space */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* FIXME: this is not 100% thread-safe */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync idx = interlocked_xchg_add( &pos, 1 ) % (sizeof(list)/sizeof(list[0]));
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if ((ret = realloc( list[idx], size ))) list[idx] = ret;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* release unused part of the buffer */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic void release_temp_buffer( char *buffer, size_t size )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync /* don't bother doing anything */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* default implementation of wine_dbgstr_an */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic const char *default_dbgstr_an( const char *str, int n )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (n < 0) n = 0;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync unsigned char c = *str++;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* default implementation of wine_dbgstr_wn */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic const char *default_dbgstr_wn( const WCHAR *str, int n )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (n == -1)
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (n < 0) n = 0;
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* default implementation of wine_dbg_vprintf */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic int default_dbg_vprintf( const char *format, va_list args )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* default implementation of wine_dbg_vlog */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncstatic int default_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync const char *func, const char *format, va_list args )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (cls < sizeof(debug_classes)/sizeof(debug_classes[0]))
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync ret += wine_dbg_printf( "%s:[%#x]:%s:%s ", debug_classes[cls], GetCurrentThreadId(), channel->name, func );
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync/* wrappers to use the function pointers */
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsyncvoid __wine_dbg_set_functions( const struct __wine_debug_functions *new_funcs,
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync struct __wine_debug_functions *old_funcs, size_t size )
058c0c53c37f5cb271aeb3c385c10766f84f4aefvboxsync if (old_funcs) memcpy( old_funcs, &funcs, min(sizeof(funcs),size) );