debugtools.c 5.41 KB
Newer Older
1 2
/*
 * Debugging functions
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22
#include "config.h"
#include "wine/port.h"
23

24
#include <assert.h>
25 26 27
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
28 29 30
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
31 32
#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"
40

41
WINE_DECLARE_DEBUG_CHANNEL(tid);
42

43 44
static struct __wine_debug_functions default_funcs;

45 46 47 48 49
/* ---------------------------------------------------------------------- */

/* get the debug info pointer for the current thread */
static inline struct debug_info *get_info(void)
{
50
    return ntdll_get_thread_data()->debug_info;
51 52 53
}

/* allocate some tmp space for a string */
54
static char *get_temp_buffer( size_t n )
55 56 57 58 59 60 61 62 63 64
{
    struct debug_info *info = get_info();
    char *res = info->str_pos;

    if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
    info->str_pos = res + n;
    return res;
}

/* release extra space that we requested in gimme1() */
65
static void release_temp_buffer( char *ptr, size_t size )
66
{
67
    struct debug_info *info = get_info();
68
    info->str_pos = ptr + size;
69 70
}

71
/***********************************************************************
72
 *		NTDLL_dbgstr_an
73
 */
74
static const char *NTDLL_dbgstr_an( const char *src, int n )
75
{
76
    const char *res;
77
    struct debug_info *info = get_info();
78
    /* save current position to restore it on exception */
79 80
    char *old_pos = info->str_pos;

81 82
    __TRY
    {
83
        res = default_funcs.dbgstr_an( src, n );
84
    }
85
    __EXCEPT_PAGE_FAULT
86
    {
87
        release_temp_buffer( old_pos, 0 );
88 89 90 91 92 93 94
        return "(invalid)";
    }
    __ENDTRY
    return res;
}

/***********************************************************************
95
 *		NTDLL_dbgstr_wn
96
 */
97
static const char *NTDLL_dbgstr_wn( const WCHAR *src, int n )
98
{
99
    const char *res;
100
    struct debug_info *info = get_info();
101
    /* save current position to restore it on exception */
102 103
    char *old_pos = info->str_pos;

104 105
    __TRY
    {
106
        res = default_funcs.dbgstr_wn( src, n );
107
    }
108
    __EXCEPT_PAGE_FAULT
109
    {
110
        release_temp_buffer( old_pos, 0 );
111 112 113 114 115 116
        return "(invalid)";
    }
    __ENDTRY
     return res;
}

117
/***********************************************************************
118
 *		NTDLL_dbg_vprintf
119
 */
120
static int NTDLL_dbg_vprintf( const char *format, va_list args )
121 122
{
    struct debug_info *info = get_info();
123
    int end;
124

125 126 127 128
    int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
                         format, args );

    /* make sure we didn't exceed the buffer length
129 130 131 132 133 134 135 136 137 138
     * the two checks are due to glibc changes in vsnprintfs return value
     * the buffer size can be exceeded in case of a missing \n in
     * debug output */
    if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
    {
       fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
                info->output);
       info->out_pos = info->output;
       abort();
    }
139

140 141 142
    for (end = ret; end > 0; end--) if (info->out_pos[end - 1] == '\n') break;

    if (!end) info->out_pos += ret;
143 144 145
    else
    {
        char *pos = info->output;
146
        write( 2, pos, info->out_pos + end - pos );
147
        /* move beginning of next line to start of buffer */
148 149
        memmove( pos, info->out_pos + end, ret - end );
        info->out_pos = pos + ret - end;
150 151 152 153
    }
    return ret;
}

154
/***********************************************************************
155
 *		NTDLL_dbg_vlog
156
 */
157
static int NTDLL_dbg_vlog( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
158
                           const char *function, const char *format, va_list args )
159
{
160 161
    static const char * const classes[] = { "fixme", "err", "warn", "trace" };
    struct debug_info *info = get_info();
162
    int ret = 0;
163

164 165 166 167
    /* only print header if we are at the beginning of the line */
    if (info->out_pos == info->output || info->out_pos[-1] == '\n')
    {
        if (TRACE_ON(tid))
168
            ret = wine_dbg_printf( "%04x:", GetCurrentThreadId() );
169
        if (cls < sizeof(classes)/sizeof(classes[0]))
170
            ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel->name, function );
171
    }
172
    if (format)
173
        ret += NTDLL_dbg_vprintf( format, args );
174
    return ret;
175
}
176

177 178 179

static const struct __wine_debug_functions funcs =
{
180 181
    get_temp_buffer,
    release_temp_buffer,
182 183 184 185 186 187
    NTDLL_dbgstr_an,
    NTDLL_dbgstr_wn,
    NTDLL_dbg_vprintf,
    NTDLL_dbg_vlog
};

188 189 190
/***********************************************************************
 *		debug_init
 */
191
void debug_init(void)
192
{
193
    __wine_dbg_set_functions( &funcs, &default_funcs, sizeof(funcs) );
194
}