8 #include "debugtools.h"
12 /* ---------------------------------------------------------------------- */
14 #define SAVE_STRING_COUNT 50
15 static void *strings[SAVE_STRING_COUNT];
16 static int nextstring;
18 /* ---------------------------------------------------------------------- */
24 if (strings[nextstring]) free (strings[nextstring]);
25 res = strings[nextstring] = xmalloc (n);
26 if (++nextstring == SAVE_STRING_COUNT) nextstring = 0;
30 /* ---------------------------------------------------------------------- */
33 debugstr_an (LPCSTR src, int n)
37 if (!src) return "(null)";
39 dst = res = gimme1 (n * 4 + 6);
41 while (n-- > 0 && *src)
46 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
47 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
48 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
49 case '"': *dst++ = '\\'; *dst++ = '"'; break;
50 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
52 if (c >= ' ' && c <= 126)
57 *dst++ = '0' + ((c >> 6) & 7);
58 *dst++ = '0' + ((c >> 3) & 7);
59 *dst++ = '0' + ((c >> 0) & 7);
74 /* ---------------------------------------------------------------------- */
79 return debugstr_an (s, 80);
82 /* ---------------------------------------------------------------------- */
85 debugstr_wn (LPCWSTR src, int n)
89 if (!src) return "(null)";
91 dst = res = gimme1 (n * 5 + 7);
94 while (n-- > 0 && *src)
99 case '\n': *dst++ = '\\'; *dst++ = 'n'; break;
100 case '\r': *dst++ = '\\'; *dst++ = 'r'; break;
101 case '\t': *dst++ = '\\'; *dst++ = 't'; break;
102 case '"': *dst++ = '\\'; *dst++ = '"'; break;
103 case '\\': *dst++ = '\\'; *dst++ = '\\'; break;
105 if (c >= ' ' && c <= 126)
110 sprintf(dst,"%04x",c);
126 /* ---------------------------------------------------------------------- */
129 debugstr_w (LPCWSTR s)
131 return debugstr_wn (s, 80);
134 /* ---------------------------------------------------------------------- */
135 /* This routine returns a nicely formated name of the resource res
136 If the resource name is a string, it will return '<res-name>'
137 If it is a number, it will return #<4-digit-hex-number> */
138 LPSTR debugres_a( LPCSTR res )
141 if (HIWORD(res)) return debugstr_a(res);
142 sprintf(resname, "#%04x", LOWORD(res));
143 return debugstr_a (resname);
146 LPSTR debugres_w( LPCWSTR res )
149 if (HIWORD(res)) return debugstr_w(res);
150 sprintf(resname, "#%04x", LOWORD(res));
151 return debugstr_a (resname);
154 /* ---------------------------------------------------------------------- */
156 LPSTR debugstr_guid( const GUID *id )
160 if (!id) return "(null)";
164 sprintf( str, "<guid-0x%04x>", LOWORD(id) );
169 sprintf( str, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
170 id->Data1, id->Data2, id->Data3,
171 id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
172 id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7] );
177 /* ---------------------------------------------------------------------- */
179 void debug_dumpstr (LPCSTR s)
188 fputc ('\\', stderr);
192 fputc ('\\', stderr);
196 fputc ('\\', stderr);
200 fputc ('\\', stderr);
205 fprintf (stderr, "\\0x%02x", *s);
214 /* ---------------------------------------------------------------------- */
216 int dbg_printf(const char *format, ...)
221 va_start(valist, format);
222 ret = vfprintf(stderr, format, valist);
229 /*--< Function >---------------------------------------------------------
234 ** This function creates a hex dump, with a readable ascii
235 ** section, for displaying memory.
238 ** 1. ptr Pointer to memory
239 ** 2. len How much to dump.
242 ** Temporarily allocated buffer, with the hex dump in it.
243 ** Don't rely on this pointer being around for very long, just
244 ** long enough to use it in a TRACE statement; e.g.:
245 ** TRACE("struct dump is \n%s", debugstr_hex_dump(&x, sizeof(x)));
247 **-------------------------------------------------------------------------*/
249 debugstr_hex_dump (const void *ptr, int len)
261 /* Begin function dbg_hex_dump */
263 /*-----------------------------------------------------------------------
264 ** Allocate an output buffer
265 ** A reasonable value is one line overhand (80 chars), and
266 ** then one line (80) for every 16 bytes.
267 **---------------------------------------------------------------------*/
268 outptr = dst = gimme1 ((len * (80 / 16)) + 80);
270 /*-----------------------------------------------------------------------
271 ** Loop throught the input buffer, one character at a time
272 **---------------------------------------------------------------------*/
273 for (i = 0, p = ptr; (i < len); i++, p++)
276 /*-------------------------------------------------------------------
277 ** If we're just starting a line,
278 ** we need to possibly flush the old line, and then
279 ** intialize the line buffer.
280 **-----------------------------------------------------------------*/
285 sprintf(outptr, " %-43.43s %-16.16s\n", dumpbuf, charbuf);
286 outptr += strlen(outptr);
288 sprintf (dumpbuf, "%04x: ", i);
289 strcpy (charbuf, "");
292 /*-------------------------------------------------------------------
293 ** Add the current data byte to the dump section.
294 **-----------------------------------------------------------------*/
295 nosign = (unsigned char) *p;
296 sprintf (tempbuf, "%02X", nosign);
298 /*-------------------------------------------------------------------
299 ** If we're two DWORDS through, add a hyphen for readability,
300 ** if it's a DWORD boundary, add a space for more
302 **-----------------------------------------------------------------*/
304 strcat(tempbuf, " - ");
305 else if ( (i % 4) == 3)
306 strcat(tempbuf, " ");
307 strcat (dumpbuf, tempbuf);
309 /*-------------------------------------------------------------------
310 ** Add the current byte to the character display part of the
312 **-----------------------------------------------------------------*/
313 sprintf (tempbuf, "%c", isprint(*p) ? *p : '.');
314 strcat (charbuf, tempbuf);
317 /*-----------------------------------------------------------------------
318 ** Flush the last line, if any
319 **---------------------------------------------------------------------*/
322 sprintf(outptr, " %-43.43s %-16.16s\n", dumpbuf, charbuf);
323 outptr += strlen(outptr);
327 } /* End function dbg_hex_dump */