2 * Fallbacks for debugging functions when running on Windows
4 * Copyright 2019 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 #ifdef __WINE_PE_BUILD
28 #include "wine/debug.h"
29 #include "wine/heap.h"
31 WINE_DECLARE_DEBUG_CHANNEL(pid
);
32 WINE_DECLARE_DEBUG_CHANNEL(timestamp
);
34 static const char * (__cdecl
*p__wine_dbg_strdup
)( const char *str
);
35 static int (__cdecl
*p__wine_dbg_output
)( const char *str
);
36 static unsigned char (__cdecl
*p__wine_dbg_get_channel_flags
)( struct __wine_debug_channel
*channel
);
37 static int (__cdecl
*p__wine_dbg_header
)( enum __wine_debug_class cls
,
38 struct __wine_debug_channel
*channel
,
39 const char *function
);
41 static const char * const debug_classes
[] = { "fixme", "err", "warn", "trace" };
43 static unsigned char default_flags
= (1 << __WINE_DBCL_ERR
) | (1 << __WINE_DBCL_FIXME
);
44 static int nb_debug_options
= -1;
45 static int options_size
;
46 static struct __wine_debug_channel
*debug_options
;
47 static DWORD partial_line_tid
; /* id of the last thread to output a partial line */
49 static void load_func( void **func
, const char *name
, void *def
)
53 DWORD err
= GetLastError();
54 HMODULE module
= GetModuleHandleA( "ntdll.dll" );
55 void *proc
= GetProcAddress( module
, name
);
56 InterlockedExchangePointer( func
, proc
? proc
: def
);
60 #define LOAD_FUNC(name) load_func( (void **)&p ## name, #name, fallback ## name )
63 /* add a new debug option at the end of the option list */
64 static void add_option( const char *name
, unsigned char set
, unsigned char clear
)
66 int min
= 0, max
= nb_debug_options
- 1, pos
, res
;
68 if (!name
[0]) /* "all" option */
70 default_flags
= (default_flags
& ~clear
) | set
;
73 if (strlen(name
) >= sizeof(debug_options
[0].name
)) return;
77 pos
= (min
+ max
) / 2;
78 res
= strcmp( name
, debug_options
[pos
].name
);
81 debug_options
[pos
].flags
= (debug_options
[pos
].flags
& ~clear
) | set
;
84 if (res
< 0) max
= pos
- 1;
87 if (nb_debug_options
>= options_size
)
89 options_size
= max( options_size
* 2, 16 );
90 debug_options
= heap_realloc( debug_options
, options_size
* sizeof(debug_options
[0]) );
94 if (pos
< nb_debug_options
) memmove( &debug_options
[pos
+ 1], &debug_options
[pos
],
95 (nb_debug_options
- pos
) * sizeof(debug_options
[0]) );
96 strcpy( debug_options
[pos
].name
, name
);
97 debug_options
[pos
].flags
= (default_flags
& ~clear
) | set
;
101 /* parse a set of debugging option specifications and add them to the option list */
102 static void parse_options( const char *str
)
104 char *opt
, *next
, *options
;
107 if (!(options
= _strdup(str
))) return;
108 for (opt
= options
; opt
; opt
= next
)
111 unsigned char set
= 0, clear
= 0;
113 if ((next
= strchr( opt
, ',' ))) *next
++ = 0;
115 p
= opt
+ strcspn( opt
, "+-" );
116 if (!p
[0]) p
= opt
; /* assume it's a debug channel name */
120 for (i
= 0; i
< ARRAY_SIZE(debug_classes
); i
++)
122 int len
= strlen(debug_classes
[i
]);
123 if (len
!= (p
- opt
)) continue;
124 if (!memcmp( opt
, debug_classes
[i
], len
)) /* found it */
126 if (*p
== '+') set
|= 1 << i
;
127 else clear
|= 1 << i
;
131 if (i
== ARRAY_SIZE(debug_classes
)) /* bad class name, skip it */
136 if (*p
== '-') clear
= ~0;
139 if (*p
== '+' || *p
== '-') p
++;
142 if (!strcmp( p
, "all" ))
143 default_flags
= (default_flags
& ~clear
) | set
;
145 add_option( p
, set
, clear
);
150 /* initialize all options at startup */
151 static void init_options(void)
153 char *wine_debug
= getenv("WINEDEBUG");
155 nb_debug_options
= 0;
156 if (wine_debug
) parse_options( wine_debug
);
159 /* FIXME: this is not 100% thread-safe */
160 static const char * __cdecl
fallback__wine_dbg_strdup( const char *str
)
162 static char *list
[32];
164 char *ret
= strdup( str
);
167 idx
= InterlockedIncrement( &pos
) % ARRAY_SIZE(list
);
168 free( InterlockedExchangePointer( (void **)&list
[idx
], ret
));
172 static int __cdecl
fallback__wine_dbg_output( const char *str
)
174 size_t len
= strlen( str
);
177 InterlockedExchange( (LONG
*)&partial_line_tid
, str
[len
- 1] != '\n' ? GetCurrentThreadId() : 0 );
178 return fwrite( str
, 1, len
, stderr
);
181 static int __cdecl
fallback__wine_dbg_header( enum __wine_debug_class cls
,
182 struct __wine_debug_channel
*channel
,
183 const char *function
)
185 char buffer
[200], *pos
= buffer
;
187 if (!(__wine_dbg_get_channel_flags( channel
) & (1 << cls
))) return -1;
189 /* skip header if partial line and no other thread came in between */
190 if (partial_line_tid
== GetCurrentThreadId()) return 0;
192 if (TRACE_ON(timestamp
))
194 ULONG ticks
= GetTickCount();
195 pos
+= sprintf( pos
, "%3u.%03u:", ticks
/ 1000, ticks
% 1000 );
197 if (TRACE_ON(pid
)) pos
+= sprintf( pos
, "%04x:", GetCurrentProcessId() );
198 pos
+= sprintf( pos
, "%04x:", GetCurrentThreadId() );
199 if (function
&& cls
< ARRAY_SIZE( debug_classes
))
200 snprintf( pos
, sizeof(buffer
) - (pos
- buffer
), "%s:%s:%s ",
201 debug_classes
[cls
], channel
->name
, function
);
203 return fwrite( buffer
, 1, strlen(buffer
), stderr
);
206 static unsigned char __cdecl
fallback__wine_dbg_get_channel_flags( struct __wine_debug_channel
*channel
)
208 int min
, max
, pos
, res
;
210 if (nb_debug_options
== -1) init_options();
213 max
= nb_debug_options
- 1;
216 pos
= (min
+ max
) / 2;
217 res
= strcmp( channel
->name
, debug_options
[pos
].name
);
218 if (!res
) return debug_options
[pos
].flags
;
219 if (res
< 0) max
= pos
- 1;
222 /* no option for this channel */
223 if (channel
->flags
& (1 << __WINE_DBCL_INIT
)) channel
->flags
= default_flags
;
224 return default_flags
;
227 const char * __cdecl
__wine_dbg_strdup( const char *str
)
229 LOAD_FUNC( __wine_dbg_strdup
);
230 return p__wine_dbg_strdup( str
);
233 int __cdecl
__wine_dbg_output( const char *str
)
235 LOAD_FUNC( __wine_dbg_output
);
236 return p__wine_dbg_output( str
);
239 unsigned char __cdecl
__wine_dbg_get_channel_flags( struct __wine_debug_channel
*channel
)
241 LOAD_FUNC( __wine_dbg_get_channel_flags
);
242 return p__wine_dbg_get_channel_flags( channel
);
245 int __cdecl
__wine_dbg_header( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
246 const char *function
)
248 LOAD_FUNC( __wine_dbg_header
);
249 return p__wine_dbg_header( cls
, channel
, function
);
252 #endif /* __WINE_PE_BUILD */