// // this file defines some functions used to output debugging info to the // vc++ debugger. // #include "stdafx.h" #include #include "debug.h" // declarations for this file. void vtdebug(TCHAR *msg, va_list ap) { TCHAR *tbuf= new TCHAR[16384]; _vsntprintf(tbuf, 16384, msg, ap); OutputDebugString(tbuf); delete[] tbuf; } void vdebug(char *msg, va_list ap) { char *buf = new char[16384]; _vsnprintf(buf, 16384, msg, ap); TCHAR *tbuf = new TCHAR[16384]; _sntprintf(tbuf, 16384, L"%hs", buf); OutputDebugString(tbuf); delete[] tbuf; delete[] buf; } void tdebug(TCHAR *msg, ...) { va_list ap; va_start(ap, msg); vtdebug(msg, ap); va_end(ap); } void debug(char *msg, ...) { va_list ap; va_start(ap, msg); vdebug(msg, ap); va_end(ap); } void error(char *msg, ...) { DWORD dwErrorCode= GetLastError(); va_list ap; va_start(ap, msg); vdebug(msg, ap); va_end(ap); TCHAR *msgbuf; int rc= FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, 0, (LPTSTR) &msgbuf, 0, NULL); if (rc) debug("ERROR: %ls\n", msgbuf); else debug("UNKNOWNERROR: 0x%08lx\n", dwErrorCode); LocalFree(msgbuf); } int g_nDumpUnitSize=1; int g_nMaxWordsPerLine=-1; void hexdumpbytes(CString &str, BYTE *buf, int nLength) { while(nLength--) { CString hexbuf; hexbuf.Format(L" %02x", *buf++); str += hexbuf; } } void hexdumpwords(CString &str, WORD *buf, int nLength) { while(nLength--) { CString hexbuf; hexbuf.Format(L" %04x", *buf++); str += hexbuf; } } void hexdumpdwords(CString &str, DWORD *buf, int nLength) { while(nLength--) { CString hexbuf; hexbuf.Format(L" %08x", *buf++); str += hexbuf; } } void dumpascii(CString &str, BYTE *buf, int nLength) { while(nLength--) { BYTE c= *buf++; str += CString((c>=' ' && c<='~')?c:'.'); } } void writespaces(CString &str, int n) { while(n--) { str += ' '; } } void hexdump(DWORD dwOffset, BYTE *buf, int nLength, int nDumpUnitSize, int nMaxUnitsPerLine) { while(nLength) { CString line; // is rounding correct here? int nLineLength= min(nLength/nDumpUnitSize, nMaxUnitsPerLine); CString bufaddr; bufaddr.Format(L"%08lx: ", dwOffset); line += bufaddr; switch(nDumpUnitSize) { case 1: hexdumpbytes(line, buf, nLineLength); break; case 2: hexdumpwords(line, (WORD*)buf, nLineLength); break; case 4: hexdumpdwords(line, (DWORD*)buf, nLineLength); break; } if (nLineLength