2 * Debugging functions for WINE
6 #include <wine/debugtools.h>
8 DECLARE_DEBUG_CHANNEL(tid
);
10 /* ---------------------------------------------------------------------- */
14 char *str_pos
; /* current position in strings buffer */
15 char *out_pos
; /* current position in output buffer */
16 char strings
[1024]; /* buffer for temporary strings */
17 char output
[1024]; /* current output line */
20 static struct debug_info tmp
;
22 /* get the debug info pointer for the current thread */
23 static inline struct debug_info
*get_info(void)
25 struct debug_info
*info
= NtCurrentTeb()->WineDebugInfo
;
30 tmp
.str_pos
= tmp
.strings
;
31 tmp
.out_pos
= tmp
.output
;
33 if (!RtlGetProcessHeap()) return &tmp
;
34 /* setup the temp structure in case HeapAlloc wants to print something */
35 NtCurrentTeb()->WineDebugInfo
= &tmp
;
36 info
= RtlAllocateHeap( RtlGetProcessHeap(), 0, sizeof(*info
) );
37 info
->str_pos
= info
->strings
;
38 info
->out_pos
= info
->output
;
39 NtCurrentTeb()->WineDebugInfo
= info
;
44 /* allocate some tmp space for a string */
45 static void *gimme1(int n
)
47 struct debug_info
*info
= get_info();
48 char *res
= info
->str_pos
;
50 if (res
+ n
>= &info
->strings
[sizeof(info
->strings
)]) res
= info
->strings
;
51 info
->str_pos
= res
+ n
;
55 /* release extra space that we requested in gimme1() */
56 static inline void release( void *ptr
)
58 struct debug_info
*info
= NtCurrentTeb()->WineDebugInfo
;
62 /***********************************************************************
63 * wine_dbgstr_an (NTDLL.@)
65 const char *wine_dbgstr_an( const char *src
, int n
)
69 if (!((WORD
)(DWORD
)(src
) >> 16))
71 if (!src
) return "(null)";
73 sprintf(res
, "#%04x", (WORD
)(DWORD
)(src
) );
77 else if (n
> 200) n
= 200;
78 dst
= res
= gimme1 (n
* 4 + 6);
80 while (n
-- > 0 && *src
)
82 unsigned char c
= *src
++;
85 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
86 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
87 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
88 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
89 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
91 if (c
>= ' ' && c
<= 126)
96 *dst
++ = '0' + ((c
>> 6) & 7);
97 *dst
++ = '0' + ((c
>> 3) & 7);
98 *dst
++ = '0' + ((c
>> 0) & 7);
114 /***********************************************************************
115 * wine_dbgstr_wn (NTDLL.@)
117 const char *wine_dbgstr_wn( const WCHAR
*src
, int n
)
121 if (!((WORD
)(DWORD
)(src
) >> 16))
123 if (!src
) return "(null)";
125 sprintf(res
, "#%04x", (WORD
)(DWORD
)(src
) );
129 else if (n
> 200) n
= 200;
130 dst
= res
= gimme1 (n
* 5 + 7);
133 while (n
-- > 0 && *src
)
138 case '\n': *dst
++ = '\\'; *dst
++ = 'n'; break;
139 case '\r': *dst
++ = '\\'; *dst
++ = 'r'; break;
140 case '\t': *dst
++ = '\\'; *dst
++ = 't'; break;
141 case '"': *dst
++ = '\\'; *dst
++ = '"'; break;
142 case '\\': *dst
++ = '\\'; *dst
++ = '\\'; break;
144 if (c
>= ' ' && c
<= 126)
149 sprintf(dst
,"%04x",c
);
166 /***********************************************************************
167 * wine_dbgstr_guid (NTDLL.@)
169 const char *wine_dbgstr_guid( const GUID
*id
)
173 if (!id
) return "(null)";
174 if (!((WORD
)(DWORD
)(id
) >> 16))
177 sprintf( str
, "<guid-0x%04x>", (WORD
)(DWORD
)(id
) );
182 sprintf( str
, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
183 id
->Data1
, id
->Data2
, id
->Data3
,
184 id
->Data4
[0], id
->Data4
[1], id
->Data4
[2], id
->Data4
[3],
185 id
->Data4
[4], id
->Data4
[5], id
->Data4
[6], id
->Data4
[7] );
190 /***********************************************************************
191 * wine_dbg_vprintf (NTDLL.@)
193 int wine_dbg_vprintf( const char *format
, va_list args
)
195 struct debug_info
*info
= get_info();
198 int ret
= _vsnprintf( info
->out_pos
, sizeof(info
->output
) - (info
->out_pos
- info
->output
),
201 p
= strrchr( info
->out_pos
, '\n' );
202 if (!p
) info
->out_pos
+= ret
;
205 char *pos
= info
->output
;
212 /* move beginning of next line to start of buffer */
213 while ((*pos
= *p
++)) pos
++;
219 /***********************************************************************
220 * wine_dbg_printf (NTDLL.@)
222 int wine_dbg_printf(const char *format
, ...)
227 va_start(valist
, format
);
228 ret
= wine_dbg_vprintf( format
, valist
);
233 /***********************************************************************
234 * wine_dbg_log (NTDLL.@)
236 int wine_dbg_log(enum __DEBUG_CLASS cls
, const char *channel
,
237 const char *function
, const char *format
, ... )
239 static const char *classes
[__DBCL_COUNT
] = { "fixme", "err", "warn", "trace" };
243 va_start(valist
, format
);
245 ret
= wine_dbg_printf( "%08lx:", NtCurrentTeb()->Cid
.UniqueThread
);
246 if (cls
< __DBCL_COUNT
)
247 ret
+= wine_dbg_printf( "%s:%s:%s ", classes
[cls
], channel
+ 1, function
);
249 ret
+= wine_dbg_vprintf( format
, valist
);