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__)) && 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 if (IsBadStringPtrA( str
, n
)) return "(invalid)";
229 if (n
== -1) for (n
= 0; str
[n
]; n
++) ;
231 while (n
-- > 0 && dst
<= buffer
+ sizeof(buffer
) - 9)
233 unsigned char c
= *str
++;
236 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
237 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
238 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
239 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
240 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
242 if (c
< ' ' || c
>= 127)
246 *dst
++ = hex
[(c
>> 4) & 0x0f];
247 *dst
++ = hex
[c
& 0x0f];
260 return __wine_dbg_strdup( buffer
);
263 static inline const char *wine_dbgstr_wn( const WCHAR
*str
, int n
)
265 static const char hex
[16] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
266 char buffer
[300], *dst
= buffer
;
268 if (!str
) return "(null)";
269 if (!((ULONG_PTR
)str
>> 16)) return wine_dbg_sprintf( "#%04x", LOWORD(str
) );
270 if (IsBadStringPtrW( str
, n
)) return "(invalid)";
271 if (n
== -1) for (n
= 0; str
[n
]; n
++) ;
274 while (n
-- > 0 && dst
<= buffer
+ sizeof(buffer
) - 10)
279 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
280 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
281 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
282 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
283 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
285 if (c
< ' ' || c
>= 127)
288 *dst
++ = hex
[(c
>> 12) & 0x0f];
289 *dst
++ = hex
[(c
>> 8) & 0x0f];
290 *dst
++ = hex
[(c
>> 4) & 0x0f];
291 *dst
++ = hex
[c
& 0x0f];
293 else *dst
++ = (char)c
;
304 return __wine_dbg_strdup( buffer
);
307 static inline const char *wine_dbgstr_a( const char *s
)
309 return wine_dbgstr_an( s
, -1 );
312 static inline const char *wine_dbgstr_w( const WCHAR
*s
)
314 return wine_dbgstr_wn( s
, -1 );
317 static inline const char *wine_dbgstr_guid( const GUID
*id
)
319 if (!id
) return "(null)";
320 if (!((ULONG_PTR
)id
>> 16)) return wine_dbg_sprintf( "<guid-0x%04hx>", (WORD
)(ULONG_PTR
)id
);
321 return wine_dbg_sprintf( "{%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
322 id
->Data1
, id
->Data2
, id
->Data3
,
323 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
324 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
327 static inline const char *wine_dbgstr_point( const POINT
*pt
)
329 if (!pt
) return "(null)";
330 return wine_dbg_sprintf( "(%d,%d)", pt
->x
, pt
->y
);
333 static inline const char *wine_dbgstr_rect( const RECT
*rect
)
335 if (!rect
) return "(null)";
336 return wine_dbg_sprintf( "(%d,%d)-(%d,%d)", rect
->left
, rect
->top
,
337 rect
->right
, rect
->bottom
);
340 static inline const char *wine_dbgstr_longlong( ULONGLONG ll
)
342 if (sizeof(ll
) > sizeof(unsigned long) && ll
>> 32)
343 return wine_dbg_sprintf( "%lx%08lx", (unsigned long)(ll
>> 32), (unsigned long)ll
);
344 else return wine_dbg_sprintf( "%lx", (unsigned long)ll
);
347 #if defined(__oaidl_h__) && defined(V_VT)
349 static inline const char *wine_dbgstr_vt( VARTYPE vt
)
351 static const char *const variant_types
[] =
353 "VT_EMPTY","VT_NULL","VT_I2","VT_I4","VT_R4","VT_R8","VT_CY","VT_DATE",
354 "VT_BSTR","VT_DISPATCH","VT_ERROR","VT_BOOL","VT_VARIANT","VT_UNKNOWN",
355 "VT_DECIMAL","15","VT_I1","VT_UI1","VT_UI2","VT_UI4","VT_I8","VT_UI8",
356 "VT_INT","VT_UINT","VT_VOID","VT_HRESULT","VT_PTR","VT_SAFEARRAY",
357 "VT_CARRAY","VT_USERDEFINED","VT_LPSTR","VT_LPWSTR","32","33","34","35",
358 "VT_RECORD","VT_INT_PTR","VT_UINT_PTR","39","40","41","42","43","44","45",
359 "46","47","48","49","50","51","52","53","54","55","56","57","58","59","60",
360 "61","62","63","VT_FILETIME","VT_BLOB","VT_STREAM","VT_STORAGE",
361 "VT_STREAMED_OBJECT","VT_STORED_OBJECT","VT_BLOB_OBJECT","VT_CF","VT_CLSID",
362 "VT_VERSIONED_STREAM"
365 static const char *const variant_flags
[16] =
370 "|VT_VECTOR|VT_ARRAY",
372 "|VT_VECTOR|VT_BYREF",
373 "|VT_ARRAY|VT_BYREF",
374 "|VT_VECTOR|VT_ARRAY|VT_BYREF",
376 "|VT_VECTOR|VT_RESERVED",
377 "|VT_ARRAY|VT_RESERVED",
378 "|VT_VECTOR|VT_ARRAY|VT_RESERVED",
379 "|VT_BYREF|VT_RESERVED",
380 "|VT_VECTOR|VT_BYREF|VT_RESERVED",
381 "|VT_ARRAY|VT_BYREF|VT_RESERVED",
382 "|VT_VECTOR|VT_ARRAY|VT_BYREF|VT_RESERVED",
385 if (vt
& ~VT_TYPEMASK
)
386 return wine_dbg_sprintf( "%s%s", wine_dbgstr_vt(vt
&VT_TYPEMASK
), variant_flags
[vt
>>12] );
388 if (vt
< sizeof(variant_types
)/sizeof(*variant_types
))
389 return variant_types
[vt
];
391 if (vt
== VT_BSTR_BLOB
)
392 return "VT_BSTR_BLOB";
394 return wine_dbg_sprintf( "vt(invalid %x)", vt
);
397 static inline const char *wine_dbgstr_variant( const VARIANT
*v
)
402 if (V_VT(v
) & VT_BYREF
) {
403 if (V_VT(v
) == (VT_VARIANT
|VT_BYREF
))
404 return wine_dbg_sprintf( "%p {VT_VARIANT|VT_BYREF: %s}", v
, wine_dbgstr_variant(V_VARIANTREF(v
)) );
405 if (V_VT(v
) == (VT_BSTR
|VT_BYREF
))
406 return wine_dbg_sprintf( "%p {VT_BSTR|VT_BYREF: %s}", v
, V_BSTRREF(v
) ? wine_dbgstr_w(*V_BSTRREF(v
)) : "(none)" );
407 return wine_dbg_sprintf( "%p {%s %p}", v
, wine_dbgstr_vt(V_VT(v
)), V_BYREF(v
) );
410 if (V_ISARRAY(v
) || V_ISVECTOR(v
))
411 return wine_dbg_sprintf( "%p {%s %p}", v
, wine_dbgstr_vt(V_VT(v
)), V_ARRAY(v
) );
415 return wine_dbg_sprintf( "%p {VT_EMPTY}", v
);
417 return wine_dbg_sprintf( "%p {VT_NULL}", v
);
419 return wine_dbg_sprintf( "%p {VT_I2: %d}", v
, V_I2(v
) );
421 return wine_dbg_sprintf( "%p {VT_I4: %d}", v
, V_I4(v
) );
423 return wine_dbg_sprintf( "%p {VT_R4: %f}", v
, V_R4(v
) );
425 return wine_dbg_sprintf( "%p {VT_R8: %lf}", v
, V_R8(v
) );
427 return wine_dbg_sprintf( "%p {VT_CY: %s}", v
, wine_dbgstr_longlong(V_CY(v
).int64
) );
429 return wine_dbg_sprintf( "%p {VT_DATE: %lf}", v
, V_DATE(v
) );
431 return wine_dbg_sprintf( "%p {VT_LPSTR: %s}", v
, wine_dbgstr_a((const char *)V_BSTR(v
)) );
433 return wine_dbg_sprintf( "%p {VT_LPWSTR: %s}", v
, wine_dbgstr_w(V_BSTR(v
)) );
435 return wine_dbg_sprintf( "%p {VT_BSTR: %s}", v
, wine_dbgstr_w(V_BSTR(v
)) );
437 return wine_dbg_sprintf( "%p {VT_DISPATCH: %p}", v
, V_DISPATCH(v
) );
439 return wine_dbg_sprintf( "%p {VT_ERROR: %08x}", v
, V_ERROR(v
) );
441 return wine_dbg_sprintf( "%p {VT_BOOL: %x}", v
, V_BOOL(v
) );
443 return wine_dbg_sprintf( "%p {VT_UNKNOWN: %p}", v
, V_UNKNOWN(v
) );
445 return wine_dbg_sprintf( "%p {VT_I1: %d}", v
, V_I1(v
) );
447 return wine_dbg_sprintf( "%p {VT_UI1: %u}", v
, V_UI1(v
) );
449 return wine_dbg_sprintf( "%p {VT_UI2: %d}", v
, V_UI2(v
) );
451 return wine_dbg_sprintf( "%p {VT_UI4: %d}", v
, V_UI4(v
) );
453 return wine_dbg_sprintf( "%p {VT_I8: %s}", v
, wine_dbgstr_longlong(V_I8(v
)) );
455 return wine_dbg_sprintf( "%p {VT_UI8: %s}", v
, wine_dbgstr_longlong(V_UI8(v
)) );
457 return wine_dbg_sprintf( "%p {VT_INT: %d}", v
, V_INT(v
) );
459 return wine_dbg_sprintf( "%p {VT_UINT: %u}", v
, V_UINT(v
) );
461 return wine_dbg_sprintf( "%p {VT_VOID}", v
);
463 return wine_dbg_sprintf( "%p {VT_RECORD: %p %p}", v
, V_RECORD(v
), V_RECORDINFO(v
) );
465 return wine_dbg_sprintf( "%p {vt %s}", v
, wine_dbgstr_vt(V_VT(v
)) );
469 #endif /* defined(__oaidl_h__) && defined(V_VT) */
472 #define WINE_TRACE __WINE_DPRINTF(_TRACE,__wine_dbch___default)
473 #define WINE_TRACE_(ch) __WINE_DPRINTF(_TRACE,&__wine_dbch_##ch)
475 #define WINE_TRACE_ON(ch) __WINE_IS_DEBUG_ON(_TRACE,&__wine_dbch_##ch)
478 #define WINE_WARN __WINE_DPRINTF(_WARN,__wine_dbch___default)
479 #define WINE_WARN_(ch) __WINE_DPRINTF(_WARN,&__wine_dbch_##ch)
481 #define WINE_WARN_ON(ch) __WINE_IS_DEBUG_ON(_WARN,&__wine_dbch_##ch)
484 #define WINE_FIXME __WINE_DPRINTF(_FIXME,__wine_dbch___default)
485 #define WINE_FIXME_(ch) __WINE_DPRINTF(_FIXME,&__wine_dbch_##ch)
487 #define WINE_FIXME_ON(ch) __WINE_IS_DEBUG_ON(_FIXME,&__wine_dbch_##ch)
489 #define WINE_ERR __WINE_DPRINTF(_ERR,__wine_dbch___default)
490 #define WINE_ERR_(ch) __WINE_DPRINTF(_ERR,&__wine_dbch_##ch)
491 #define WINE_ERR_ON(ch) __WINE_IS_DEBUG_ON(_ERR,&__wine_dbch_##ch)
493 #define WINE_DECLARE_DEBUG_CHANNEL(ch) \
494 static struct __wine_debug_channel __wine_dbch_##ch = { 0xff, #ch }
495 #define WINE_DEFAULT_DEBUG_CHANNEL(ch) \
496 static struct __wine_debug_channel __wine_dbch_##ch = { 0xff, #ch }; \
497 static struct __wine_debug_channel * const __wine_dbch___default = &__wine_dbch_##ch
499 #define WINE_MESSAGE wine_dbg_printf
502 /* Wine uses shorter names that are very likely to conflict with other software */
504 static inline const char *debugstr_an( const char * s
, int n
) { return wine_dbgstr_an( s
, n
); }
505 static inline const char *debugstr_wn( const WCHAR
*s
, int n
) { return wine_dbgstr_wn( s
, n
); }
506 static inline const char *debugstr_guid( const struct _GUID
*id
) { return wine_dbgstr_guid(id
); }
507 static inline const char *debugstr_a( const char *s
) { return wine_dbgstr_an( s
, -1 ); }
508 static inline const char *debugstr_w( const WCHAR
*s
) { return wine_dbgstr_wn( s
, -1 ); }
510 #if defined(__oaidl_h__) && defined(V_VT)
511 static inline const char *debugstr_vt( VARTYPE vt
) { return wine_dbgstr_vt( vt
); }
512 static inline const char *debugstr_variant( const VARIANT
*v
) { return wine_dbgstr_variant( v
); }
515 #define TRACE WINE_TRACE
516 #define TRACE_(ch) WINE_TRACE_(ch)
517 #define TRACE_ON(ch) WINE_TRACE_ON(ch)
519 #define WARN WINE_WARN
520 #define WARN_(ch) WINE_WARN_(ch)
521 #define WARN_ON(ch) WINE_WARN_ON(ch)
523 #define FIXME WINE_FIXME
524 #define FIXME_(ch) WINE_FIXME_(ch)
525 #define FIXME_ON(ch) WINE_FIXME_ON(ch)
527 #undef ERR /* Solaris got an 'ERR' define in <sys/reg.h> */
529 #define ERR_(ch) WINE_ERR_(ch)
530 #define ERR_ON(ch) WINE_ERR_ON(ch)
532 #define MESSAGE WINE_MESSAGE
534 #endif /* __WINESRC__ */
540 #endif /* __WINE_WINE_DEBUG_H */