12 #include "debugtools.h"
18 DECLARE_DEBUG_CHANNEL(tid);
20 /* ---------------------------------------------------------------------- */
24 char *str_pos; /* current position in strings buffer */
25 char *out_pos; /* current position in output buffer */
26 char strings[1024]; /* buffer for temporary strings */
27 char output[1024]; /* current output line */
30 static struct debug_info tmp;
32 /* get the debug info pointer for the current thread */
33 static inline struct debug_info *get_info(void)
35 struct debug_info *info = NtCurrentTeb()->debug_info;
40 tmp.str_pos = tmp.strings;
41 tmp.out_pos = tmp.output;
43 if (!GetProcessHeap()) return &tmp;
44 /* setup the temp structure in case HeapAlloc wants to print something */
45 NtCurrentTeb()->debug_info = &tmp;
46 info = HeapAlloc( GetProcessHeap(), 0, sizeof(*info) );
47 info->str_pos = info->strings;
48 info->out_pos = info->output;
49 NtCurrentTeb()->debug_info = info;
54 /* allocate some tmp space for a string */
55 static void *gimme1(int n)
57 struct debug_info *info = get_info();
58 char *res = info->str_pos;
60 if (res + n >= &info->strings[sizeof(info->strings)]) res = info->strings;
61 info->str_pos = res + n;
65 /* release extra space that we requested in gimme1() */
66 static inline void release( void *ptr )
68 struct debug_info *info = NtCurrentTeb()->debug_info;
72 /***********************************************************************
73 * wine_dbgstr_an (NTDLL.@)
75 const char *wine_dbgstr_an( const char *src, int n )
81 if (!src) return "(null)";
83 sprintf(res, "#%04x", LOWORD(src) );
87 else if (n > 200) n = 200;
88 dst = res = gimme1 (n * 4 + 6);
90 while (n-- > 0 && *src)
92 unsigned char c = *src++;
95 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
96 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
97 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
98 case '"': *dst++ = '\\'; *dst++ = '"'; break;
99 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
101 if (c >= ' ' && c <= 126)
106 *dst++ = '0' + ((c >> 6) & 7);
107 *dst++ = '0' + ((c >> 3) & 7);
108 *dst++ = '0' + ((c >> 0) & 7);
124 /***********************************************************************
125 * wine_dbgstr_wn (NTDLL.@)
127 const char *wine_dbgstr_wn( const WCHAR *src, int n )
133 if (!src) return "(null)";
135 sprintf(res, "#%04x", LOWORD(src) );
139 else if (n > 200) n = 200;
140 dst = res = gimme1 (n * 5 + 7);
143 while (n-- > 0 && *src)
148 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
149 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
150 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
151 case '"': *dst++ = '\\'; *dst++ = '"'; break;
152 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
154 if (c >= ' ' && c <= 126)
159 sprintf(dst,"%04x",c);
176 /***********************************************************************
177 * wine_dbgstr_guid (NTDLL.@)
179 const char *wine_dbgstr_guid( const GUID *id )
183 if (!id) return "(null)";
187 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
192 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
193 id->Data1, id->Data2, id->Data3,
194 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
195 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
200 /***********************************************************************
201 * wine_dbg_vprintf (NTDLL.@)
203 int wine_dbg_vprintf( const char *format, va_list args )
205 struct debug_info *info = get_info();
208 int ret = vsnprintf( info->out_pos, sizeof(info->output) - (info->out_pos - info->output),
211 /* make sure we didn't exceed the buffer length
212 * the two checks are due to glibc changes in vsnprintfs return value
213 * the buffer size can be exceeded in case of a missing \n in
215 if ((ret == -1) || (ret >= sizeof(info->output) - (info->out_pos - info->output)))
217 fprintf( stderr, "wine_dbg_vprintf: debugstr buffer overflow (contents: '%s')\n",
219 info->out_pos = info->output;
223 p = strrchr( info->out_pos, '\n' );
224 if (!p) info->out_pos += ret;
227 char *pos = info->output;
229 write( 2, pos, p - pos );
230 /* move beginning of next line to start of buffer */
231 while ((*pos = *p++)) pos++;
237 /***********************************************************************
238 * wine_dbg_printf (NTDLL.@)
240 int wine_dbg_printf(const char *format, ...)
245 va_start(valist, format);
246 ret = wine_dbg_vprintf( format, valist );
251 /***********************************************************************
252 * wine_dbg_log (NTDLL.@)
254 int wine_dbg_log(enum __DEBUG_CLASS cls, const char *channel,
255 const char *function, const char *format, ... )
257 static const char *classes[__DBCL_COUNT] = { "fixme", "err", "warn", "trace" };
261 va_start(valist, format);
263 ret = wine_dbg_printf( "%08lx:", (DWORD)NtCurrentTeb()->tid );
264 if (cls < __DBCL_COUNT)
265 ret += wine_dbg_printf( "%s:%s:%s ", classes[cls], channel + 1, function );
267 ret += wine_dbg_vprintf( format, valist );