2 * Wine debugging interface
4 * Copyright 1999 Patrik Stridvall
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 #ifndef __WINE_WINE_DEBUG_H
22 #define __WINE_WINE_DEBUG_H
39 * Internal definitions (do not use these directly)
42 enum __wine_debug_class
49 __WINE_DBCL_INIT
= 7 /* lazy init flag */
52 struct __wine_debug_channel
58 #ifndef WINE_NO_TRACE_MSGS
59 # define __WINE_GET_DEBUGGING_TRACE(dbch) ((dbch)->flags & (1 << __WINE_DBCL_TRACE))
61 # define __WINE_GET_DEBUGGING_TRACE(dbch) 0
64 #ifndef WINE_NO_DEBUG_MSGS
65 # define __WINE_GET_DEBUGGING_WARN(dbch) ((dbch)->flags & (1 << __WINE_DBCL_WARN))
66 # define __WINE_GET_DEBUGGING_FIXME(dbch) ((dbch)->flags & (1 << __WINE_DBCL_FIXME))
68 # define __WINE_GET_DEBUGGING_WARN(dbch) 0
69 # define __WINE_GET_DEBUGGING_FIXME(dbch) 0
72 /* define error macro regardless of what is configured */
73 #define __WINE_GET_DEBUGGING_ERR(dbch) ((dbch)->flags & (1 << __WINE_DBCL_ERR))
75 #define __WINE_GET_DEBUGGING(dbcl,dbch) __WINE_GET_DEBUGGING##dbcl(dbch)
77 #define __WINE_IS_DEBUG_ON(dbcl,dbch) \
78 (__WINE_GET_DEBUGGING##dbcl(dbch) && (__wine_dbg_get_channel_flags(dbch) & (1 << __WINE_DBCL##dbcl)))
80 #if defined(__GNUC__) || defined(__clang__)
82 #define __WINE_DPRINTF(dbcl,dbch) \
83 do { if(__WINE_GET_DEBUGGING(dbcl,(dbch))) { \
84 struct __wine_debug_channel * const __dbch = (dbch); \
85 const enum __wine_debug_class __dbcl = __WINE_DBCL##dbcl; \
88 #define __WINE_DBG_LOG(args...) \
89 wine_dbg_log( __dbcl, __dbch, __FUNCTION__, args); } } while(0)
91 #if !defined(__WINE_USE_MSVCRT) || defined(__MINGW32__)
92 #define __WINE_PRINTF_ATTR(fmt,args) __attribute__((format (printf,fmt,args)))
94 #define __WINE_PRINTF_ATTR(fmt,args)
98 #ifdef WINE_NO_TRACE_MSGS
99 #define WINE_TRACE(args...) do { } while(0)
100 #define WINE_TRACE_(ch) WINE_TRACE
103 #ifdef WINE_NO_DEBUG_MSGS
104 #define WINE_WARN(args...) do { } while(0)
105 #define WINE_WARN_(ch) WINE_WARN
106 #define WINE_FIXME(args...) do { } while(0)
107 #define WINE_FIXME_(ch) WINE_FIXME
110 #elif defined(__SUNPRO_C)
112 #define __WINE_DPRINTF(dbcl,dbch) \
113 do { if(__WINE_GET_DEBUGGING(dbcl,(dbch))) { \
114 struct __wine_debug_channel * const __dbch = (dbch); \
115 const enum __WINE_DEBUG_CLASS __dbcl = __WINE_DBCL##dbcl; \
118 #define __WINE_DBG_LOG(...) \
119 wine_dbg_log( __dbcl, __dbch, __func__, __VA_ARGS__); } } while(0)
121 #define __WINE_PRINTF_ATTR(fmt,args)
123 #ifdef WINE_NO_TRACE_MSGS
124 #define WINE_TRACE(...) do { } while(0)
125 #define WINE_TRACE_(ch) WINE_TRACE
128 #ifdef WINE_NO_DEBUG_MSGS
129 #define WINE_WARN(...) do { } while(0)
130 #define WINE_WARN_(ch) WINE_WARN
131 #define WINE_FIXME(...) do { } while(0)
132 #define WINE_FIXME_(ch) WINE_FIXME
135 #else /* !__GNUC__ && !__SUNPRO_C */
137 #define __WINE_DPRINTF(dbcl,dbch) \
138 (!__WINE_GET_DEBUGGING(dbcl,(dbch)) || \
139 (wine_dbg_log(__WINE_DBCL##dbcl,(dbch),__FILE__,"%d: ",__LINE__) == -1)) ? \
140 (void)0 : (void)wine_dbg_printf
142 #define __WINE_PRINTF_ATTR(fmt, args)
144 #endif /* !__GNUC__ && !__SUNPRO_C */
146 extern unsigned char __cdecl
__wine_dbg_get_channel_flags( struct __wine_debug_channel
*channel
);
147 extern const char * __cdecl
__wine_dbg_strdup( const char *str
);
148 extern int __cdecl
__wine_dbg_output( const char *str
);
149 extern int __cdecl
__wine_dbg_header( enum __wine_debug_class cls
, struct __wine_debug_channel
*channel
,
150 const char *function
);
153 * Exported definitions and macros
156 /* These functions return a printable version of a string, including
157 quotes. The string will be valid for some time, but not indefinitely
158 as strings are re-used. */
160 #if (defined(__x86_64__) || (defined(__aarch64__) && __has_attribute(ms_abi))) && defined(__GNUC__) && defined(__WINE_USE_MSVCRT)
161 # define __wine_dbg_cdecl __cdecl
162 # define __wine_dbg_va_list __builtin_ms_va_list
163 # define __wine_dbg_va_start(list,arg) __builtin_ms_va_start(list,arg)
164 # define __wine_dbg_va_end(list) __builtin_ms_va_end(list)
166 # define __wine_dbg_cdecl
167 # define __wine_dbg_va_list va_list
168 # define __wine_dbg_va_start(list,arg) va_start(list,arg)
169 # define __wine_dbg_va_end(list) va_end(list)
172 static const char * __wine_dbg_cdecl
wine_dbg_sprintf( const char *format
, ... ) __WINE_PRINTF_ATTR(1,2);
173 static inline const char * __wine_dbg_cdecl
wine_dbg_sprintf( const char *format
, ... )
176 __wine_dbg_va_list args
;
178 __wine_dbg_va_start( args
, format
);
179 vsnprintf( buffer
, sizeof(buffer
), format
, args
);
180 __wine_dbg_va_end( args
);
181 return __wine_dbg_strdup( buffer
);
184 static int __wine_dbg_cdecl
wine_dbg_printf( const char *format
, ... ) __WINE_PRINTF_ATTR(1,2);
185 static inline int __wine_dbg_cdecl
wine_dbg_printf( const char *format
, ... )
188 __wine_dbg_va_list args
;
190 __wine_dbg_va_start( args
, format
);
191 vsnprintf( buffer
, sizeof(buffer
), format
, args
);
192 __wine_dbg_va_end( args
);
193 return __wine_dbg_output( buffer
);
196 static int __wine_dbg_cdecl
wine_dbg_log( enum __wine_debug_class cls
,
197 struct __wine_debug_channel
*channel
, const char *func
,
198 const char *format
, ... ) __WINE_PRINTF_ATTR(4,5);
199 static inline int __wine_dbg_cdecl
wine_dbg_log( enum __wine_debug_class cls
,
200 struct __wine_debug_channel
*channel
,
201 const char *function
, const char *format
, ... )
204 __wine_dbg_va_list args
;
207 if (*format
== '\1') /* special magic to avoid standard prefix */
212 if ((ret
= __wine_dbg_header( cls
, channel
, function
)) == -1) return ret
;
214 __wine_dbg_va_start( args
, format
);
215 vsnprintf( buffer
, sizeof(buffer
), format
, args
);
216 __wine_dbg_va_end( args
);
217 ret
+= __wine_dbg_output( buffer
);
221 static inline const char *wine_dbgstr_an( const char *str
, int n
)
223 static const char hex
[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
224 char buffer
[300], *dst
= buffer
;
226 if (!str
) return "(null)";
227 if (!((ULONG_PTR
)str
>> 16)) return wine_dbg_sprintf( "#%04x", LOWORD(str
) );
228 #ifndef WINE_UNIX_LIB
229 if (IsBadStringPtrA( str
, n
)) return "(invalid)";
231 if (n
== -1) for (n
= 0; str
[n
]; n
++) ;
233 while (n
-- > 0 && dst
<= buffer
+ sizeof(buffer
) - 9)
235 unsigned char c
= *str
++;
238 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
239 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
240 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
241 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
242 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
244 if (c
< ' ' || c
>= 127)
248 *dst
++ = hex
[(c
>> 4) & 0x0f];
249 *dst
++ = hex
[c
& 0x0f];
262 return __wine_dbg_strdup( buffer
);
265 static inline const char *wine_dbgstr_wn( const WCHAR
*str
, int n
)
267 static const char hex
[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
268 char buffer
[300], *dst
= buffer
;
270 if (!str
) return "(null)";
271 if (!((ULONG_PTR
)str
>> 16)) return wine_dbg_sprintf( "#%04x", LOWORD(str
) );
272 #ifndef WINE_UNIX_LIB
273 if (IsBadStringPtrW( str
, n
)) return "(invalid)";
275 if (n
== -1) for (n
= 0; str
[n
]; n
++) ;
278 while (n
-- > 0 && dst
<= buffer
+ sizeof(buffer
) - 10)
283 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
284 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
285 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
286 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
287 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
289 if (c
< ' ' || c
>= 127)
292 *dst
++ = hex
[(c
>> 12) & 0x0f];
293 *dst
++ = hex
[(c
>> 8) & 0x0f];
294 *dst
++ = hex
[(c
>> 4) & 0x0f];
295 *dst
++ = hex
[c
& 0x0f];
297 else *dst
++ = (char)c
;
308 return __wine_dbg_strdup( buffer
);
311 static inline const char *wine_dbgstr_a( const char *s
)
313 return wine_dbgstr_an( s
, -1 );
316 static inline const char *wine_dbgstr_w( const WCHAR
*s
)
318 return wine_dbgstr_wn( s
, -1 );
321 static inline const char *wine_dbgstr_guid( const GUID
*id
)
323 if (!id
) return "(null)";
324 if (!((ULONG_PTR
)id
>> 16)) return wine_dbg_sprintf( "<guid-0x%04hx>", (WORD
)(ULONG_PTR
)id
);
325 return wine_dbg_sprintf( "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
326 id
->Data1
, id
->Data2
, id
->Data3
,
327 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
328 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
331 static inline const char *wine_dbgstr_point( const POINT
*pt
)
333 if (!pt
) return "(null)";
334 return wine_dbg_sprintf( "(%d,%d)", pt
->x
, pt
->y
);
337 static inline const char *wine_dbgstr_rect( const RECT
*rect
)
339 if (!rect
) return "(null)";
340 return wine_dbg_sprintf( "(%d,%d)-(%d,%d)", rect
->left
, rect
->top
,
341 rect
->right
, rect
->bottom
);
344 static inline const char *wine_dbgstr_longlong( ULONGLONG ll
)
346 if (sizeof(ll
) > sizeof(unsigned long) && ll
>> 32)
347 return wine_dbg_sprintf( "%lx%08lx", (unsigned long)(ll
>> 32), (unsigned long)ll
);
348 else return wine_dbg_sprintf( "%lx", (unsigned long)ll
);
351 #if defined(__oaidl_h__) && defined(V_VT)
353 static inline const char *wine_dbgstr_vt( VARTYPE vt
)
355 static const char *const variant_types
[] =
357 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
358 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
359 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
360 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
361 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
362 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
363 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
364 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
365 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID",
366 "VT_VERSIONED_STREAM"
369 static const char *const variant_flags
[16] =
374 "|VT_VECTOR|VT_ARRAY",
376 "|VT_VECTOR|VT_BYREF",
377 "|VT_ARRAY|VT_BYREF",
378 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
380 "|VT_VECTOR|VT_RESERVED",
381 "|VT_ARRAY|VT_RESERVED",
382 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
383 "|VT_BYREF|VT_RESERVED",
384 "|VT_VECTOR|VT_BYREF|VT_RESERVED",
385 "|VT_ARRAY|VT_BYREF|VT_RESERVED",
386 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_RESERVED",
389 if (vt
& ~VT_TYPEMASK
)
390 return wine_dbg_sprintf( "%s%s", wine_dbgstr_vt(vt
&VT_TYPEMASK
), variant_flags
[vt
>>12] );
392 if (vt
< sizeof(variant_types
)/sizeof(*variant_types
))
393 return variant_types
[vt
];
395 if (vt
== VT_BSTR_BLOB
)
396 return "VT_BSTR_BLOB";
398 return wine_dbg_sprintf( "vt(invalid %x)", vt
);
401 static inline const char *wine_dbgstr_variant( const VARIANT
*v
)
406 if (V_VT(v
) & VT_BYREF
) {
407 if (V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
408 return wine_dbg_sprintf( "%p {VT_VARIANT|VT_BYREF: %s}", v
, wine_dbgstr_variant(V_VARIANTREF(v
)) );
409 if (V_VT(v
) == (VT_BSTR
|VT_BYREF
))
410 return wine_dbg_sprintf( "%p {VT_BSTR|VT_BYREF: %s}", v
, V_BSTRREF(v
) ? wine_dbgstr_w(*V_BSTRREF(v
)) : "(none)" );
411 return wine_dbg_sprintf( "%p {%s %p}", v
, wine_dbgstr_vt(V_VT(v
)), V_BYREF(v
) );
414 if (V_ISARRAY(v
) || V_ISVECTOR(v
))
415 return wine_dbg_sprintf( "%p {%s %p}", v
, wine_dbgstr_vt(V_VT(v
)), V_ARRAY(v
) );
419 return wine_dbg_sprintf( "%p {VT_EMPTY}", v
);
421 return wine_dbg_sprintf( "%p {VT_NULL}", v
);
423 return wine_dbg_sprintf( "%p {VT_I2: %d}", v
, V_I2(v
) );
425 return wine_dbg_sprintf( "%p {VT_I4: %d}", v
, V_I4(v
) );
427 return wine_dbg_sprintf( "%p {VT_R4: %f}", v
, V_R4(v
) );
429 return wine_dbg_sprintf( "%p {VT_R8: %lf}", v
, V_R8(v
) );
431 return wine_dbg_sprintf( "%p {VT_CY: %s}", v
, wine_dbgstr_longlong(V_CY(v
).int64
) );
433 return wine_dbg_sprintf( "%p {VT_DATE: %lf}", v
, V_DATE(v
) );
435 return wine_dbg_sprintf( "%p {VT_LPSTR: %s}", v
, wine_dbgstr_a((const char *)V_BSTR(v
)) );
437 return wine_dbg_sprintf( "%p {VT_LPWSTR: %s}", v
, wine_dbgstr_w(V_BSTR(v
)) );
439 return wine_dbg_sprintf( "%p {VT_BSTR: %s}", v
, wine_dbgstr_w(V_BSTR(v
)) );
441 return wine_dbg_sprintf( "%p {VT_DISPATCH: %p}", v
, V_DISPATCH(v
) );
443 return wine_dbg_sprintf( "%p {VT_ERROR: %08x}", v
, V_ERROR(v
) );
445 return wine_dbg_sprintf( "%p {VT_BOOL: %x}", v
, V_BOOL(v
) );
447 return wine_dbg_sprintf( "%p {VT_UNKNOWN: %p}", v
, V_UNKNOWN(v
) );
449 return wine_dbg_sprintf( "%p {VT_I1: %d}", v
, V_I1(v
) );
451 return wine_dbg_sprintf( "%p {VT_UI1: %u}", v
, V_UI1(v
) );
453 return wine_dbg_sprintf( "%p {VT_UI2: %d}", v
, V_UI2(v
) );
455 return wine_dbg_sprintf( "%p {VT_UI4: %d}", v
, V_UI4(v
) );
457 return wine_dbg_sprintf( "%p {VT_I8: %s}", v
, wine_dbgstr_longlong(V_I8(v
)) );
459 return wine_dbg_sprintf( "%p {VT_UI8: %s}", v
, wine_dbgstr_longlong(V_UI8(v
)) );
461 return wine_dbg_sprintf( "%p {VT_INT: %d}", v
, V_INT(v
) );
463 return wine_dbg_sprintf( "%p {VT_UINT: %u}", v
, V_UINT(v
) );
465 return wine_dbg_sprintf( "%p {VT_VOID}", v
);
467 return wine_dbg_sprintf( "%p {VT_RECORD: %p %p}", v
, V_RECORD(v
), V_RECORDINFO(v
) );
469 return wine_dbg_sprintf( "%p {vt %s}", v
, wine_dbgstr_vt(V_VT(v
)) );
473 #endif /* defined(__oaidl_h__) && defined(V_VT) */
476 #define WINE_TRACE __WINE_DPRINTF(_TRACE,__wine_dbch___default)
477 #define WINE_TRACE_(ch) __WINE_DPRINTF(_TRACE,&__wine_dbch_##ch)
479 #define WINE_TRACE_ON(ch) __WINE_IS_DEBUG_ON(_TRACE,&__wine_dbch_##ch)
482 #define WINE_WARN __WINE_DPRINTF(_WARN,__wine_dbch___default)
483 #define WINE_WARN_(ch) __WINE_DPRINTF(_WARN,&__wine_dbch_##ch)
485 #define WINE_WARN_ON(ch) __WINE_IS_DEBUG_ON(_WARN,&__wine_dbch_##ch)
488 #define WINE_FIXME __WINE_DPRINTF(_FIXME,__wine_dbch___default)
489 #define WINE_FIXME_(ch) __WINE_DPRINTF(_FIXME,&__wine_dbch_##ch)
491 #define WINE_FIXME_ON(ch) __WINE_IS_DEBUG_ON(_FIXME,&__wine_dbch_##ch)
493 #define WINE_ERR __WINE_DPRINTF(_ERR,__wine_dbch___default)
494 #define WINE_ERR_(ch) __WINE_DPRINTF(_ERR,&__wine_dbch_##ch)
495 #define WINE_ERR_ON(ch) __WINE_IS_DEBUG_ON(_ERR,&__wine_dbch_##ch)
497 #define WINE_DECLARE_DEBUG_CHANNEL(ch) \
498 static struct __wine_debug_channel __wine_dbch_##ch = { 0xff, #ch }
499 #define WINE_DEFAULT_DEBUG_CHANNEL(ch) \
500 static struct __wine_debug_channel __wine_dbch_##ch = { 0xff, #ch }; \
501 static struct __wine_debug_channel * const __wine_dbch___default = &__wine_dbch_##ch
503 #define WINE_MESSAGE wine_dbg_printf
506 /* Wine uses shorter names that are very likely to conflict with other software */
508 static inline const char *debugstr_an( const char * s
, int n
) { return wine_dbgstr_an( s
, n
); }
509 static inline const char *debugstr_wn( const WCHAR
*s
, int n
) { return wine_dbgstr_wn( s
, n
); }
510 static inline const char *debugstr_guid( const struct _GUID
*id
) { return wine_dbgstr_guid(id
); }
511 static inline const char *debugstr_a( const char *s
) { return wine_dbgstr_an( s
, -1 ); }
512 static inline const char *debugstr_w( const WCHAR
*s
) { return wine_dbgstr_wn( s
, -1 ); }
514 #if defined(__oaidl_h__) && defined(V_VT)
515 static inline const char *debugstr_vt( VARTYPE vt
) { return wine_dbgstr_vt( vt
); }
516 static inline const char *debugstr_variant( const VARIANT
*v
) { return wine_dbgstr_variant( v
); }
519 #define TRACE WINE_TRACE
520 #define TRACE_(ch) WINE_TRACE_(ch)
521 #define TRACE_ON(ch) WINE_TRACE_ON(ch)
523 #define WARN WINE_WARN
524 #define WARN_(ch) WINE_WARN_(ch)
525 #define WARN_ON(ch) WINE_WARN_ON(ch)
527 #define FIXME WINE_FIXME
528 #define FIXME_(ch) WINE_FIXME_(ch)
529 #define FIXME_ON(ch) WINE_FIXME_ON(ch)
531 #undef ERR /* Solaris got an 'ERR' define in <sys/reg.h> */
533 #define ERR_(ch) WINE_ERR_(ch)
534 #define ERR_ON(ch) WINE_ERR_ON(ch)
536 #define MESSAGE WINE_MESSAGE
538 #endif /* __WINESRC__ */
544 #endif /* __WINE_WINE_DEBUG_H */