⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debuging.c

📁 wince host 和 target PCI驱动程序
💻 C
字号:
#include <ntddk.h>
//#include <stdlib.h>
//#include <windows.h>
//#include <excpt.h>
//#include <tchar.h>
#include "debuging.h"

/*** ATOI/ATOL Support ***/

int CE_atoi(const char *pzArg)
{
        char ch;
        int iValue = 0;

        if (pzArg != 0) {
                ch = 0;
                do {
                        iValue = 10*iValue + ch - '0';
                        ch = *pzArg++;
                } while (('0' <= ch)&&(ch <= '9'));
        }
        return (iValue);
}

long CE_atol(const char *pzArg)
{
        char ch;
        long lValue = 0;

        if (pzArg != 0) {
                ch = 0;
                do {
                        lValue = 10*lValue + ch - '0';
                        ch = *pzArg++;
                } while (('0' <= ch)&&(ch <= '9'));
        }
        return (lValue);
}

/*** PRINTF/SPRINTF/VSPRINTF Support ***/

#define FLAG_NONE               0x000   /* no flags */
#define FLAG_LEFTADJ    0x001   /* left adjust */
#define FLAG_ZEROPAD    0x002   /* zero pad */
#define FLAG_ALTFORM    0x004   /* alternate form */
#define FLAG_SHORTINT   0x008   /* short integer */
#define FLAG_LONGINT    0x010   /* long integer */
#define FLAG_SIGNED             0x020   /* signed integer */
#define FLAG_HEXPREFIX  0x040   /* hex prefix */
#define FLAG_STRING             0x080   /* string conversion */
#define FLAG_NEARPTR    0x100   /* near pointer */
#define FLAG_FARPTR             0x200   /* far pointer */

#define CONV_SIZE               32              /* binary conversion might require 32 chars */

/* This implementation does not support floating point but implements several */
/* extensions to the ANSI C specifiction.  The implementation supports the    */
/* following conversions:                                                     */

/*   %d, %i, %u, %o, %x, %X, %p, %c, %s, %n, and %% standard ANSI conversions */
/*   ' ', '0', '+', '-', '#' flags, 'h' , 'l', and 'L' size modifiers         */
/*   minimum_width and precision specifiers including '*' for runtime support */
/*   synonyms %Lx = %lx, %D = %Ld = %ld, %U = %Lu = %lu, and %O = %Lo = %lo   */
/*   extension for binary conversion %b and %B = %Lb = %lB                    */

/* if the NEARFAR_POINTERS constant is defined, the implementation supports:  */

/*   %lp = %P = %Fp far pointer conversions, %hp %Np near pointer conversions */
/*   %ls = %S = %Fs far string conversions, %hs %Ns near string conversions   */
/*   %Fn far count conversion, %Nn near count conversions                     */

#undef NEARFAR_POINTERS

int CE_vsprintf(char *pzOutBuf, const char *pzFmt, va_list ap)
{
	int		nChars;			/* number of characters printed */
	char		ch;				/* current format character */
	unsigned long	ulValue;	/* value for conversion */
	int		flags;			/* holds value for flags */
	char    	chSign;			/* hold the sign to be printed (' ', '+', '-', or \0) */
	int		min_width;		/* minimum width specifier */
	int		precision;		/* precision number */
	int		base;			/* base for integer conversion */
	char    *mchDigit;		/* map of digit set to use */
	int		size;			/* size of conversion text */
#ifdef NEARFAR_POINTERS
	char _far       *lpch;	/* pointer in temp holding buffer */
#else
	char    *lpch;			/* pointer in temp holding buffer */
#endif /* NEARFAR_POINTERS */
	char    chBufTemp[CONV_SIZE];	/* temp holding buffer for conversion */

	nChars = 0;
	for (;;) {
		ch = *pzFmt++;
		if (ch != '%') {
			*pzOutBuf++ = ch;
			if (ch == '\0') return (nChars);
			++nChars;
			continue;
		}

		/* parse flags */
		flags = FLAG_NONE;
		chSign = '\0';
		mchDigit = "0123456789abcdef";
		ch = *pzFmt++;
		for (;;) {
			if (ch == ' ') {
				if (chSign == '\0') chSign = ' ';
			} else if (ch == '+') {
				chSign = '+';
			} else if (ch == '-') {
				flags |= FLAG_LEFTADJ;
			} else if (ch == '0') {
				flags |= FLAG_ZEROPAD;
			} else if (ch == '#') {
				flags |= FLAG_ALTFORM;
			} else {
				break;
			}
			ch = *pzFmt++;
		}

		/* parse min_width */
		min_width = 0;
		if (ch == '*') {
			min_width = va_arg(ap, int);
			ch = *pzFmt++;
        } else {
			while (('0' <= ch)&&(ch <= '9')) {
				min_width = 10*min_width + ch - '0';
				ch = *pzFmt++;
			}
        }

		/* parse precision */
		precision = -1;
		if (ch == '.') {
			ch = *pzFmt++;
			if (ch == '*') {
				precision = va_arg(ap, int);
				ch = *pzFmt++;
			} else {
				precision = 0;
				while (('0' <= ch)&&(ch <= '9')) {
					precision = 10*precision + ch - '0';
					ch = *pzFmt++;
				}
			}
		}

		/* parse conversion size */
		for (;;) {
			if (ch == 'h') flags |= FLAG_SHORTINT;
			else if (ch == 'l') flags |= FLAG_LONGINT;
			else if (ch == 'L') flags |= FLAG_LONGINT;
#ifdef NEARFAR_POINTERS
			else if (ch == 'N') flags |= FLAG_NEARPTR;
			else if (ch == 'F') flags |= FLAG_FARPTR;
#endif /* NEARFAR_POINTERS */
			else break;
			ch = *pzFmt++;
        }

        /* parse conversion */
        switch (ch) {
        default: /* unknown conversion or NULL */
            *pzOutBuf++ = ch;
            if (ch == '\0') return (nChars);
            ++nChars;
            continue;
        case 'n': /* save characters output so far */
#ifdef NEARFAR_POINTERS
            if (flags & FLAG_FARPTR) {
                void _far *vlp = va_arg(ap, void _far *);
                if (flags & FLAG_LONGINT) *((long _far *)vlp) = nChars;
                else if (flags & FLAG_SHORTINT) *((short _far *)vlp) = (short)nChars;
                else *((int _far *)vlp) = nChars;
            } else if (flags & FLAG_NEARPTR) {
                void *vnp = va_arg(ap, void _near *);
                if (flags & FLAG_LONGINT) *((long _near *)vnp) = nChars;
                else if (flags & FLAG_SHORTINT) *((short _near *)vnp) = (short)nChars;
                else *((int _near *)vnp) = nChars;
            }
#endif /* NEARFAR_POINTERS */
            {
                    void *vp = va_arg(ap, void *);
                    if (flags & FLAG_LONGINT) *((long *)vp) = nChars;
                    else if (flags & FLAG_SHORTINT) *((short *)vp) = (short)nChars;
                    else *((int *)vp) = nChars;
            }
            continue;
		case '%': /* % conversion */
            lpch = "%";
            size = 1;
            chSign = '\0';
            break;
		case 'c': /* character conversion */
            lpch = chBufTemp;
            *lpch = (char)va_arg(ap, int);
            size = 1;
            chSign = '\0';
            break;
#ifdef NEARFAR_POINTERS
		case 'S': /* far string conversion */
			flags |= FLAG_FARPTR;
			/* FALLTHROUGH */
#endif /* NEARFAR_POINTERS */
		case 's': /* string conversion */
#ifdef NEARFAR_POINTERS
			if ((flags & FLAG_LONGINT)||(flags & FLAG_FARPTR)) {
				lpch = va_arg(ap, char _far *);
			} else if ((flags & FLAG_SHORTINT)||(flags & FLAG_NEARPTR)) {
				lpch = va_arg(ap, char _near *);
            } else
#endif /* NEARFAR_POINTERS */
            {
				lpch = va_arg(ap, char *);
            }
			if (lpch == 0) {
				lpch = "(null)";
				size = sizeof ("(null)");
				if (size > precision) size = precision;
			} else {
#ifdef NEARFAR_POINTERS
				char _far *lpchTemp = lpch;
#else
				char *lpchTemp = lpch;
#endif /* NEARFAR_POINTERS */

				size = 0;
				if (precision >= 0) {
					while (*lpchTemp++ != '\0') {
						if (++size >= precision) break;
					}
				} else {
					while (*lpchTemp++ != '\0') ++size;
				}
			}
			chSign = '\0';
			flags |= FLAG_STRING;
			break;
#ifdef NEARFAR_POINTERS
		case 'P': /* far pointer conversion */
			flags |= FLAG_LONGINT;
			/* FALLTHROUGH */
#endif /* NEARFAR_POINTERS */
		case 'p': /* pointer conversion */
#ifdef NEARFAR_POINTERS
			if ((flags & FLAG_LONGINT)||(flags & FLAG_FARPTR)) {
				ulValue = (unsigned long)va_arg(ap, void _far *);
			} else if ((flags & FLAG_SHORTINT)||(flags & FLAG_NEARPTR)) {
				ulValue = (unsigned long)va_arg(ap, void _near *);
			} else
#endif /* NEARFAR_POINTERS */
			{
				ulValue = (unsigned long)va_arg(ap, void *);
			}
			base = 16;
			chSign = '\0';
			goto L_number;
		case 'X': /* hexadecimal conversion (caps) */
			mchDigit = "0123456789ABCDEF";
			/* FALLTHROUGH */
		case 'x': /* hexadecimal conversion (lowercase) */
			base = 16;
			chSign = '\0';
			goto L_read_number;
		case 'O': /* EXTENSION: long octal conversion */
			flags |= FLAG_LONGINT;
			/* FALLTHROUGH */
		case 'o': /* octal conversion */
			base = 8;
			chSign = '\0';
			goto L_read_number;
		case 'B': /* EXTENSION: long binary conversion */
			flags |= FLAG_LONGINT;
			/* FALLTHROUGH */
		case 'b': /* EXTENSION: binary conversion */
			base = 2;
			chSign = '\0';
			goto L_read_number;
		case 'U': /* EXTENSION: unsigned long decimal conversion */
			flags |= FLAG_LONGINT;
			/* FALLTHROUGH */
		case 'u': /* unsigned decimal conversion */
			base = 10;
			chSign = '\0';
			goto L_read_number;
		case 'D': /* EXTENSION: long decimal conversion */
			flags |= FLAG_LONGINT;
			/* FALLTHROUGH */
		case 'd': /* decimal conversion */
		case 'i':
			flags |= FLAG_SIGNED;
			base = 10;
			/* FALLTHROUGH */
		L_read_number:
			/* read number */
			if (flags & FLAG_LONGINT) {
				if (flags & FLAG_SIGNED) {
					long lValue = va_arg(ap, long);
					if (lValue < 0) {
						ulValue = (unsigned long)(-lValue);
						chSign = '-';
					} else {
						ulValue = (unsigned long)lValue;
					}
				} else {
					ulValue = va_arg(ap, unsigned long);
				}
			} else {
				if (flags & FLAG_SIGNED) {
					int iValue = va_arg(ap, int);
					if (iValue < 0) {
						ulValue = (unsigned long)(-iValue);
						chSign = '-';
					} else {
						ulValue = (unsigned long)iValue;
					}
				} else {
					ulValue = (unsigned long)va_arg(ap, unsigned int);
				}
			}
			if ((base == 16)&&(flags & FLAG_ALTFORM)&&(ulValue != 0)) {
				flags |= FLAG_HEXPREFIX;
			}
			/*FALLTHROUGH*/
		L_number:
			/* don't zero pad if precision is specified */
			if (precision >= 0) flags &= ~FLAG_ZEROPAD;

			lpch = &chBufTemp[CONV_SIZE];
			if ((ulValue != 0) || (precision != 0)) {
				do {
					*--lpch = mchDigit[ulValue % base];
					ulValue /= base;
				} while (ulValue != 0);
				if ((flags & FLAG_ALTFORM)&&(base == 8)&&(*lpch != '0')) {
					*--lpch = '0';
				}
			}
#ifdef NEARFAR_POINTERS
			size = (int)((char _far *)(&chBufTemp[CONV_SIZE]) - lpch);
#else
			size = (int)(&chBufTemp[CONV_SIZE] - lpch);
#endif /* NEARFAR_POINTERS */
			break;
		}

		/* process conversion */
		{
			int     n;                      /* integer for loop counts */
			int     real_size = size;       /* actual size of conversion for alignment */
			char    *pzStart = pzOutBuf; /* save start position of conversion */

			/* adjust precision to equal size for strings */
			if (flags & FLAG_STRING) precision = size;
			else if (precision > real_size) real_size = precision;

			/* adjust real_size for prefix */
			if (chSign != '\0') ++real_size;
			if (flags & FLAG_HEXPREFIX) real_size += 2;

			/* put in blank padding for right adjust before prefixes */
			if ((flags & (FLAG_LEFTADJ|FLAG_ZEROPAD)) == 0) {
				for (n = real_size; n < min_width; ++n) *pzOutBuf++ = ' ';
			}

            /* put in conversion prefix(es) */
			if (chSign != '\0') *pzOutBuf++ = chSign;
			if (flags & FLAG_HEXPREFIX) {
				*pzOutBuf++ = '0';
				*pzOutBuf++ = ch;
			}

            /* put in zero padding for right adjust after prefixes*/
			if ((flags & (FLAG_LEFTADJ|FLAG_ZEROPAD)) == FLAG_ZEROPAD) {
				for (n = real_size; n < min_width; ++n) *pzOutBuf++ = '0';
			}

			/* put in leading zeroes to fill precision for numbers */
			for (n = size; n < precision; ++n) *pzOutBuf++ = '0';

			/* put in the conversion text */
			for (n = 0; n < size; ++n) *pzOutBuf++ = *lpch++;

			/* put in blank padding for left adjust */
			if (flags & FLAG_LEFTADJ) {
				for (n = real_size; n < min_width; ++n) *pzOutBuf++ = ' ';
			}

			/* compute number of characters in conversion */
			nChars += (int)(pzOutBuf - pzStart);
		}
	}
	/* NOTREACHED */
}

int CE_sprintf(char *pzOutBuf, const char *pzFmt, ...)
{
	int		nChars;
	va_list ap;

	va_start(ap, pzFmt);
	nChars = CE_vsprintf(pzOutBuf, pzFmt, ap);
	va_end(ap);
	return (nChars);
}

#define HASH_LINE_SIZE  128
#define NUM_HASH_LINES  16      /* must be a power of 2 */
#define HASH_LINE_MASK  (NUM_HASH_LINES-1)
/* WARNING: the largest printf is 3*HASH_LINE_SIZE-1 characters */

int debugPrintf(ULONG iDebugLevel, CHAR* pzFmt, ...)
{
	int		nChars;
	int		i;
	char    *psz;
	va_list ap;
	static unsigned int uIndex = 0;
	static char chBuffer[(NUM_HASH_LINES+2)*HASH_LINE_SIZE];

   /* Hash the index number to a location in the buffer that changes with */
   /* every call to debugPrintf().  The hash function was chosen so that a */
   /* mixture of long and short lines can queued without over-writing the */
   /* buffer used by previous calls.  The function now pseudo re-entrant. */
   i = (int)(++uIndex);
   i = (3 * (i & HASH_LINE_MASK)) % HASH_LINE_MASK;

   psz = &chBuffer[i * HASH_LINE_SIZE]; /* save pointer to buffer location */
	va_start(ap, pzFmt);
	nChars = CE_vsprintf(psz, pzFmt, ap);
	va_end(ap);

	DbgPrint(psz);

	return (nChars);
}

// PUR: display the content of a sequence of memory locations
// inp: msg
//      Type : 1 (byte), 2 (short), 4 (long)
//      Format:
//      addr = starting address
//      no_data = no of data
// ret: no of lines outputted
//#define dump_bytes1( msg, addr, no_bytes ) \
//        dump_memory0( msg, 1, " %02x", addr, no_bytes, 24 )
//#define dump_shorts2( msg, addr, no_shorts ) \
//        dump_memory0( msg, 2, " %04x", addr, no_shorts, 12 )
//#define dump_longs4( msg, addr, no_longs ) \
//        dump_memory0( msg, 4, " %08x", addr, no_longs, 8 )
//
int dump_memory0( char *msg, int Type, char *Format,
                  void *addr, unsigned int no_data, int rowsiz )
{
  register unsigned int i;
  unsigned char  *data8;
  unsigned short *data16;
  unsigned long  *data32;
  int nolines;

     data8  = (unsigned char*)  addr;
     data16 = (unsigned short*) addr;
     data32 = (unsigned long*)  addr;

     debugPrintf(0, "\n --MEMORY CONTENT at %p-- %s (%d)", addr, msg, no_data );
     nolines = 1;
     if ( rowsiz < 1 )
        rowsiz = 1;
     for ( i= 0; i < no_data; i++ )
     {
        if ( i%rowsiz==0 )
        {
           debugPrintf(0, "\n%3d:", i );
           nolines ++;
        }

        if ( Type == 1 )
           debugPrintf(0, Format, (unsigned char) *data8 );
        else if ( Type == 2 )
           debugPrintf(0, Format, (unsigned short) *data16 );
        else if ( Type == 4 )
           debugPrintf(0, Format, (unsigned long) *data32 );

        data8 ++;
        data16++;
        data32++;
     }
     debugPrintf(0, "\n" );
     nolines++;
     return nolines;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -