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

📄 debug.c

📁 ucos操作系统下TCPIP实现代码
💻 C
📖 第 1 页 / 共 3 页
字号:
int getTraceLevel(TraceModule tMod)
{
#if DEBUG_SUPPORT > 0
	return traceLevel[tMod];
#else
	return 0;
#endif
}


/*	trace - a form of printf to write a line to the trace log.
 */
#pragma argsused
void trace(int level, const char *format,...) 
{
#if DEBUG_SUPPORT > 0
	char **arg = (char **)&format;				/* ptr to arg list on stack */
	int  logNdx;
	
	if (level <= traceLevel[TL_UNDEF]) {
		OS_ENTER_CRITICAL();
		logNdx = logHead++ % MAXLOGSZ;
		/* If the log is full, drop the oldest entry. */
		if (logHead % MAXLOGSZ == logTail % MAXLOGSZ)
			logTail++;
		OS_EXIT_CRITICAL();
		
		/* Note: mtime() exits with interrupts enabled. */
		debugLog[logNdx].logTime = mtime();
		
		debugLog[logNdx].OSTCBPrio = OSTCBCur->OSTCBPrio + 100;
		debugLog[logNdx].tCode = 0;
		arg++;							/* Point to first arguement if any */
		if (vsprintf(debugLog[logNdx].logMsg, format, (const void *)arg) >= LOGMSGLEN)
			panic("TRACE");
	}
#endif
}


/*	ntrace - a numeric trace function
 */
#pragma argsused
void ntrace(int level, NumTraceCodes tCode, ULONG arg1, ULONG arg2) 
{
#if DEBUG_SUPPORT > 0
	u_int  logNdx;
	
	if (level <= traceLevel[TL_UNDEF]) {
		OS_ENTER_CRITICAL();
		logNdx = logHead++ % MAXLOGSZ;
		/* If the log is full, drop the oldest entry. */
		if (logHead % MAXLOGSZ == logTail % MAXLOGSZ)
			logTail++;
		OS_EXIT_CRITICAL();
		
		/* Note: mtime() exits with interrupts enabled. */
		debugLog[logNdx].logTime = mtime();
		
		debugLog[logNdx].OSTCBPrio = OSTCBCur->OSTCBPrio + 100;
		debugLog[logNdx].tCode = tCode;
		debugLog[logNdx].arg1 = arg1;
		debugLog[logNdx].arg2 = arg2;
		debugLog[logNdx].logMsg[0] = '\0';
	}
#endif
}


/*
 * logTrace - A form of printf that writes a line to the trace log if
 *	the trace level for the specified module is high enough.
 */
#pragma argsused
void logTrace(int level, TraceModule tMod,  const char FAR *format,...)
{
#if DEBUG_SUPPORT > 0
	char **arg = (char **)&format;				/* ptr to arg list on stack */
	u_int  logNdx;
	
	if (level <= traceLevel[tMod]) {
		OS_ENTER_CRITICAL();
		logNdx = logHead++ % MAXLOGSZ;
		/* If the log is full, drop the oldest entry. */
		if (logHead % MAXLOGSZ == logTail % MAXLOGSZ)
			logTail++;
		OS_EXIT_CRITICAL();
		
		/* Note: mtime() exits with interrupts enabled. */
		debugLog[logNdx].logTime = mtime();
		
		debugLog[logNdx].OSTCBPrio = OSTCBCur->OSTCBPrio + 100;
		debugLog[logNdx].tCode = 0;
		arg++;							/* Point to first arguement if any */
		if (vsprintf(debugLog[logNdx].logMsg, format, (const void *)arg) >= LOGMSGLEN)
			panic("LOGTRACE");
	}
#endif
}


/*  Generate the a string in d containing the character c if printable,
 *  otherwise a bracketed mnemonic for it.  Return d.
 */
#pragma argsused
char *debugchr(char *d, unsigned char c)
{
#if DEBUG_SUPPORT > 0
	INT  fndx;
	char fstr[12], *dptr = d;

	switch (c) {
	case (NUL): 
		strcpy(d, "<NUL>"); 
		break;
	case (ACK): 
		strcpy(d, "<ACK>"); 
		break;
	case (NAK): 
		strcpy(d, "<NAK>"); 
		break;
	case (LF):  
		strcpy(d, "<LF>"); 
		break;
	case (FF):  
		strcpy(d, "<FF>"); 
		break;
	case (CR):  
		strcpy(d, "<CR>"); 
		break;
	case (RTS): 
		strcpy(d, "<RTS>"); 
		break;
	case (EOD): 
		strcpy(d, "<EOD>"); 
		break;
	case (EOF): 
		strcpy(d, "<EOF>"); 
		break;
	default:
		if (c < 32 || c >= 127) {
			fndx = 0;
			*dptr++ = '<';
			/* Convert number */
			if (c == 0) {
				fstr[fndx++] = '0';
			} else {
				while (c > 0 && fndx < 10) {
					fstr[fndx++] = (c % 10) + '0';
					c = c / 10;
				}
			}
			for (fndx--; fndx >= 0; fndx--) {
				*dptr++ = fstr[fndx];
			}
			*dptr++ = '>';
		} else
			*dptr++ = c;
		*dptr = '\0';
		break;
	}
	return(d);
#endif
}

/*
 * Print the given lines from the trace log in readable form to the
 *	specified file stream.  The previous start and max lines are saved so
 *	that if they are passed as -1, the dump displays the next set of lines.
 */
#pragma argsused
void traceDump(FILE *fptr, int startLine, int maxLines)
{
#if DEBUG_SUPPORT > 0
	int rc;
	static UINT curLine = 0;
	static int curMax = MAXSENDLINES;
	UINT endLine, logNdx;
	
	// Load parameters
	if (startLine >= 0)
		curLine = startLine;
	if (maxLines > 0)
		curMax = maxLines;
	if (curLine < logTail)
		curLine = logTail;
	else if (curLine > logHead)
		curLine = logHead;
	endLine = curLine + curMax;
	if (endLine > logHead)
		endLine = logHead;
		
	for (; curLine < endLine; curLine++) {
		logNdx = curLine % MAXLOGSZ; 
		
		if (debugLog[logNdx].tCode == tUndef) {
			rc = fprintf(fptr, "%4u>%9lu: %.*s\r\n", 
					curLine,
					debugLog[logNdx].logTime,
					LOGMSGLEN,
					debugLog[logNdx].logMsg);
		} else {
			rc = fprintf(fptr, "%4u>%9lu: %u %lu %lu %.*s\r\n", 
					curLine,
					debugLog[logNdx].logTime,
					debugLog[logNdx].tCode,
					debugLog[logNdx].arg1,
					debugLog[logNdx].arg2,
					LOGMSGLEN,
					debugLog[logNdx].logMsg);
		}
		/* 
		 * If we wrote more characters than fit on a terminal line, count it
		 * as 2 lines.  We don't count the leading CR/LF.
		 */
		if (rc > TERMWIDTH + 2)
			endLine--;
	}
	fflush(fptr);
#endif
}


/**********************************/
/*** LOCAL FUNCTION DEFINITIONS ***/
/**********************************/
/*
 * monitorMain0 - an interactive diagnostics monitor designed to be run as a
 * task connected via a telnet connection.
 */
#if DEBUG_SUPPORT > 0
static void monitorMain0(void *md)
{
	MonitorControl *mc = &monitorControl[(int)md];
	#define READBUFSZ 50
	struct sockaddr_in localAddr, peerAddr;
	int tdListen, tcpd, inCnt, st;
	char rBuf[READBUFSZ];
	char devName[] = "TCP0";
	
	localAddr.ipAddr = 0;
	localAddr.sin_port = TCPPORT_ACCUVOTE;
	mc->cmdLen = 0;
	mc->commandFcn = monDump;
	tcpd = -1;
	
	if ((tdListen = tcpOpen()) < 0) {
		DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Unable to open TCP (%d)", 
					(int)md, tdListen));
	} else if ((inCnt = MONTCP_KEEPALIVE) != 0 
			&& (st = tcpIOCtl(tdListen, TCPCTLS_KEEPALIVE, &inCnt)) < 0) {
		DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d setting keep alive", 
					(int)md, st));
	} else for (;;) {
		if ((st = tcpBind(tdListen, &localAddr)) < 0) {
			DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d on bind", (int)md, st));
			break;
		} else if ((tcpd = tcpAccept(tdListen, &peerAddr)) < 0) {
			DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d on accept", (int)md, tcpd));
			break;
		} else if (devName[3] = '0' + tcpd, (mc->fp = fopen(devName, "w+")) == NULL) {
			DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d on fopen", (int)md, mc->fp));
			break;
		} else if ((st = fputs(MONPROMPT, mc->fp)) < 0 || fflush(mc->fp) < 0) {
			DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d on write", (int)md, st));
			break;
		} else {
			DIAGMONTRACE((LOG_INFO, "monitorMain[%d]: connect %s:%u", (int)md,
						ip_ntoa(htonl(peerAddr.sin_addr.s_addr)),
						peerAddr.sin_port));
			inCnt = LOG_DETAIL;
			tcpIOCtl(tdListen, TCPCTLS_TRACELEVEL, &inCnt);
			while ((inCnt = tcpRead(tcpd, rBuf, READBUFSZ)) >= 0) {
				if ((st = tcpWrite(tcpd, rBuf, inCnt)) < 0) {
					DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d on write", 
								(int)md, st));
					break;
				} else {
					(void)monProcInput(mc, rBuf, inCnt);
				}
			}
			DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Read=>%d st=%d", 
						(int)md, inCnt, st));
			inCnt = LOG_INFO;
			tcpIOCtl(tdListen, TCPCTLS_TRACELEVEL, &inCnt);
		}
		if (tcpd >= 0) {
			DIAGMONTRACE((LOG_INFO, "monitorMain[%d]: disconnect %d %s:%u", (int)md,
						tcpd,
						ip_ntoa(htonl(peerAddr.sin_addr.s_addr)),
						peerAddr.sin_port));
			if ((st = tcpDisconnect(tcpd)) < 0) {
				DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Disconnect err %d", 
							(int)md, st));
				break;
				
			} else if ((st = tcpWait(tcpd)) < 0) {
				DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Close wait err %d", 
							(int)md, st));
				break;
			}
		}
	}
	if (mc->fp != NULL) {
		DIAGMONTRACE((LOG_INFO, "monitorMain[%d]: closing", (int)md));
		fclose(mc->fp);
	}
	if (tdListen >= 0 && tdListen != tcpd) {
		DIAGMONTRACE((LOG_INFO, "monitorMain[%d]: disconnect %d %s:%u", (int)md, tdListen,
					ip_ntoa(htonl(peerAddr.sin_addr.s_addr)),
					peerAddr.sin_port));
		if ((st = tcpDisconnect(tdListen)) < 0) {
			DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Disconnect err %d", 
						(int)md, st));
		}
	}
	OSTaskDel(OS_PRIO_SELF);
}

#if DEBUGMONPORT > 0
/*
 * monitorMain1 - an interactive diagnostics monitor designed to be run as a
 * task connected via a serial port.
 */
static void monitorMain1(void *md)
{
	MonitorControl *mc = &monitorControl[(int)md];
	#define READBUFSZ 50
	FILE *fptr;
	int inCnt, st;
	char rBuf[READBUFSZ];
	
	mc->cmdLen = 0;
	mc->commandFcn = monDump;
	
	if ((mc->fp = fopen("COM1", "rw")) == NULL) {
		DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Unable to open sio (%d)", 
					(int)md, fptr));
	} else {
		inCnt = B9600;
		ioctl(fileno(mc->fp), SETBAUD, &inCnt);
		
		if ((st = write(fileno(mc->fp), MONPROMPT, MONPROMPTLEN)) < 0) {
			DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d on write", (int)md, st));
		} else {
			while ((inCnt = read(fileno(mc->fp), rBuf, READBUFSZ)) >= 0) {
				if ((st = write(fileno(mc->fp), rBuf, inCnt)) < 0) {
					DIAGMONTRACE((LOG_ERR, "monitorMain[%d]: Error %d on write", 
								(int)md, st));
					break;
				} else {
					(void)monProcInput(mc, rBuf, inCnt);
				}
			}
		}
	}
	if (mc->fp != NULL) {
		DIAGMONTRACE((LOG_INFO, "monitorMain[%d]: closing", (int)md));
		fclose(mc->fp);
	}
	OSTaskDel(OS_PRIO_SELF);
}
#endif

/*
 * monProcInput - process incoming monitor data.  This function takes user
 * input to build the command line which when complete, is parsed and
 * executed.  Because this may be called with any fragment or even character
 * by character, it must maintain state in the monitor control block.
 * Return 0 on success, an error code on failure.
 */
static int monProcInput(MonitorControl *mc, char *rBuf, int inCnt)

⌨️ 快捷键说明

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