Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
wine-mirror
GitHub Repository: wine-mirror/wine
Path: blob/master/libs/winecrt0/debug.c
12343 views
1
/*
2
* Fallbacks for debugging functions when running on Windows
3
*
4
* Copyright 2019 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
#ifdef __WINE_PE_BUILD
22
23
#include <stdio.h>
24
#include <stdarg.h>
25
#include <stdlib.h>
26
#include "windef.h"
27
#include "winbase.h"
28
#include "wine/debug.h"
29
30
WINE_DECLARE_DEBUG_CHANNEL(pid);
31
WINE_DECLARE_DEBUG_CHANNEL(timestamp);
32
33
static const char * (__cdecl *p__wine_dbg_strdup)( const char *str );
34
static int (__cdecl *p__wine_dbg_output)( const char *str );
35
static unsigned char (__cdecl *p__wine_dbg_get_channel_flags)( struct __wine_debug_channel *channel );
36
static int (__cdecl *p__wine_dbg_header)( enum __wine_debug_class cls,
37
struct __wine_debug_channel *channel,
38
const char *function );
39
40
static const char * const debug_classes[] = { "fixme", "err", "warn", "trace" };
41
42
static unsigned char default_flags = (1 << __WINE_DBCL_ERR) | (1 << __WINE_DBCL_FIXME);
43
static int nb_debug_options = -1;
44
static int options_size;
45
static struct __wine_debug_channel *debug_options;
46
static DWORD partial_line_tid; /* id of the last thread to output a partial line */
47
48
static void load_func( void **func, const char *name, void *def )
49
{
50
if (!*func)
51
{
52
DWORD err = GetLastError();
53
HMODULE module = GetModuleHandleW( L"ntdll.dll" );
54
void *proc = GetProcAddress( module, name );
55
InterlockedExchangePointer( func, proc ? proc : def );
56
SetLastError( err );
57
}
58
}
59
#define LOAD_FUNC(name) load_func( (void **)&p ## name, #name, fallback ## name )
60
61
62
/* add a new debug option at the end of the option list */
63
static void add_option( const char *name, unsigned char set, unsigned char clear )
64
{
65
int min = 0, max = nb_debug_options - 1, pos, res;
66
67
if (!name[0]) /* "all" option */
68
{
69
default_flags = (default_flags & ~clear) | set;
70
return;
71
}
72
if (strlen(name) >= sizeof(debug_options[0].name)) return;
73
74
while (min <= max)
75
{
76
pos = (min + max) / 2;
77
res = strcmp( name, debug_options[pos].name );
78
if (!res)
79
{
80
debug_options[pos].flags = (debug_options[pos].flags & ~clear) | set;
81
return;
82
}
83
if (res < 0) max = pos - 1;
84
else min = pos + 1;
85
}
86
if (nb_debug_options >= options_size)
87
{
88
options_size = max( options_size * 2, 16 );
89
if (!debug_options)
90
debug_options = HeapAlloc( GetProcessHeap(), 0, options_size * sizeof(debug_options[0]) );
91
else
92
debug_options = HeapReAlloc( GetProcessHeap(), 0, debug_options,
93
options_size * sizeof(debug_options[0]) );
94
}
95
96
pos = min;
97
if (pos < nb_debug_options) memmove( &debug_options[pos + 1], &debug_options[pos],
98
(nb_debug_options - pos) * sizeof(debug_options[0]) );
99
strcpy( debug_options[pos].name, name );
100
debug_options[pos].flags = (default_flags & ~clear) | set;
101
nb_debug_options++;
102
}
103
104
/* parse a set of debugging option specifications and add them to the option list */
105
static void parse_options( const char *str )
106
{
107
char *opt, *next, *options;
108
unsigned int i;
109
110
if (!(options = _strdup(str))) return;
111
for (opt = options; opt; opt = next)
112
{
113
const char *p;
114
unsigned char set = 0, clear = 0;
115
116
if ((next = strchr( opt, ',' ))) *next++ = 0;
117
118
p = opt + strcspn( opt, "+-" );
119
if (!p[0]) p = opt; /* assume it's a debug channel name */
120
121
if (p > opt)
122
{
123
for (i = 0; i < ARRAY_SIZE(debug_classes); i++)
124
{
125
int len = strlen(debug_classes[i]);
126
if (len != (p - opt)) continue;
127
if (!memcmp( opt, debug_classes[i], len )) /* found it */
128
{
129
if (*p == '+') set |= 1 << i;
130
else clear |= 1 << i;
131
break;
132
}
133
}
134
if (i == ARRAY_SIZE(debug_classes)) /* bad class name, skip it */
135
continue;
136
}
137
else
138
{
139
if (*p == '-') clear = ~0;
140
else set = ~0;
141
}
142
if (*p == '+' || *p == '-') p++;
143
if (!p[0]) continue;
144
145
if (!strcmp( p, "all" ))
146
default_flags = (default_flags & ~clear) | set;
147
else
148
add_option( p, set, clear );
149
}
150
free( options );
151
}
152
153
/* initialize all options at startup */
154
static void init_options(void)
155
{
156
char *wine_debug = getenv("WINEDEBUG");
157
158
nb_debug_options = 0;
159
if (wine_debug) parse_options( wine_debug );
160
}
161
162
/* FIXME: this is not 100% thread-safe */
163
static const char * __cdecl fallback__wine_dbg_strdup( const char *str )
164
{
165
static char *list[32];
166
static LONG pos;
167
char *ret = strdup( str );
168
int idx;
169
170
idx = InterlockedIncrement( &pos ) % ARRAY_SIZE(list);
171
free( InterlockedExchangePointer( (void **)&list[idx], ret ));
172
return ret;
173
}
174
175
static int __cdecl fallback__wine_dbg_output( const char *str )
176
{
177
size_t len = strlen( str );
178
179
if (!len) return 0;
180
InterlockedExchange( (LONG *)&partial_line_tid, str[len - 1] != '\n' ? GetCurrentThreadId() : 0 );
181
return fwrite( str, 1, len, stderr );
182
}
183
184
static int __cdecl fallback__wine_dbg_header( enum __wine_debug_class cls,
185
struct __wine_debug_channel *channel,
186
const char *function )
187
{
188
char buffer[200], *pos = buffer;
189
190
if (!(__wine_dbg_get_channel_flags( channel ) & (1 << cls))) return -1;
191
192
/* skip header if partial line and no other thread came in between */
193
if (partial_line_tid == GetCurrentThreadId()) return 0;
194
195
if (TRACE_ON(timestamp))
196
{
197
UINT ticks = GetTickCount();
198
pos += sprintf( pos, "%3u.%03u:", ticks / 1000, ticks % 1000 );
199
}
200
if (TRACE_ON(pid)) pos += sprintf( pos, "%04x:", (UINT)GetCurrentProcessId() );
201
pos += sprintf( pos, "%04x:", (UINT)GetCurrentThreadId() );
202
if (function && cls < ARRAY_SIZE( debug_classes ))
203
snprintf( pos, sizeof(buffer) - (pos - buffer), "%s:%s:%s ",
204
debug_classes[cls], channel->name, function );
205
206
return fwrite( buffer, 1, strlen(buffer), stderr );
207
}
208
209
static unsigned char __cdecl fallback__wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
210
{
211
int min, max, pos, res;
212
213
if (nb_debug_options == -1) init_options();
214
215
min = 0;
216
max = nb_debug_options - 1;
217
while (min <= max)
218
{
219
pos = (min + max) / 2;
220
res = strcmp( channel->name, debug_options[pos].name );
221
if (!res) return debug_options[pos].flags;
222
if (res < 0) max = pos - 1;
223
else min = pos + 1;
224
}
225
/* no option for this channel */
226
if (channel->flags & (1 << __WINE_DBCL_INIT)) channel->flags = default_flags;
227
return default_flags;
228
}
229
230
const char * __cdecl __wine_dbg_strdup( const char *str )
231
{
232
LOAD_FUNC( __wine_dbg_strdup );
233
return p__wine_dbg_strdup( str );
234
}
235
236
int __cdecl __wine_dbg_output( const char *str )
237
{
238
LOAD_FUNC( __wine_dbg_output );
239
return p__wine_dbg_output( str );
240
}
241
242
unsigned char __cdecl __wine_dbg_get_channel_flags( struct __wine_debug_channel *channel )
243
{
244
LOAD_FUNC( __wine_dbg_get_channel_flags );
245
return p__wine_dbg_get_channel_flags( channel );
246
}
247
248
int __cdecl __wine_dbg_header( enum __wine_debug_class cls, struct __wine_debug_channel *channel,
249
const char *function )
250
{
251
LOAD_FUNC( __wine_dbg_header );
252
return p__wine_dbg_header( cls, channel, function );
253
}
254
255
#endif /* __WINE_PE_BUILD */
256
257