📄 netdebug.c
字号:
/*****************************************************************************
* debug.c - Developer debugging utilities - not used in release versions.
*
* Copyright (c) 1998 by Global Election Systems Inc.
*
* The authors hereby grant permission to use, copy, modify, distribute,
* and license this software and its documentation for any purpose, provided
* that existing copyright notices are retained in all copies and that this
* notice and the following disclaimer are included verbatim in any
* distributions. No written agreement, license, or royalty fee is required
* for any of the authorized uses.
*
* THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS *AS IS* AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
* REVISION HISTORY
*
* 98-07-29 Guy Lancaster <lancasterg@acm.org>, Global Election Systems Inc.
* Original.
*****************************************************************************/
#include "netconf.h"
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include "net.h"
#include "netbuf.h"
#include "netppp.h"
#include "netip.h"
#include "nettcp.h"
#include "netdebug.h"
/***************************/
/*** PRIVATE DEFINITIONS ***/
/***************************/
#define TCPPORT_ACCUVOTE 3031 /* Monitor port number. */
#define MAXLOGSZ 128 /* Number of messages in debug log. */
#define MAXSENDLINES 20 /* Number of trace lines to send. */
#define CMDLINESZ 200 /* Max length of a command line. */
#define MAXCMDARGS 5 /* Max arguements on a command. */
#define SENDLINESZ LOGMSGLEN+50 /* Max length of a line to send. */
#define NUM_MON 1 /* Number of monitor sessions. */
#define STACK_SIZE OSMINSTACK+800 /* Monitor stack size. */
#define TOKENLEN 20 /* Max length of a token string. */
#define MONPROMPT "> " /* The monitor prompt string. */
#define MONPROMPTLEN 2 /* Length of the monitor prompt. */
#define TERMWIDTH 80 /* Length of a terminal line. */
#define MONTCP_KEEPALIVE 60 /* Keep alive probe delay in seconds. */
/*
* WARNING: Make sure that there is an entry in the cmdFcn table for each
* entry here.
* Note: The SENDSTR command is not actually a user command but is used
* to display syntax and error messages if command parsing fails.
*/
typedef enum {
MONCMD_SENDSTR = 0, /* Send the send string. */
MONCMD_DUMP, /* Display a page of a trace or log. */
MONCMD_SET, /* Set parameters. */
MONCMD_DISPLAY /* Display tables. */
} MonitorCommands;
typedef enum {
MONDUMP_TRACE = 0, /* Set the trace levels. */
MONDUMP_SCAN /* Display raw scan data. */
} DumpOptions;
typedef enum {
MONSET_TRACE = 0 /* Set the trace levels. */
} SetCommandOptions;
typedef enum {
MONDISP_MCARD = 0, /* Display memory card status. */
MONDISP_BUFFER, /* Display the buffer statistics. */
MONDISP_TCP, /* Display TCP session statistics. */
MONDISP_IP, /* Display IP statistics. */
MONDISP_PPP, /* Display PPP session statistics. */
MONDISP_SERIAL /* Display serial driver statistics. */
} DisplayOptions;
#define MONCMDLIST "\t\tAccu-Vote Monitor Commands\r\n\
\t DUMP - display a page information\r\n\
\t DISPLAY - display a table\r\n\
\t SET - set parameters\r\n"
#define MONDUMPOPTIONS "\t\tAccu-Vote Monitor Dump Options\r\n\
\t TRACE <start> <lines> - Display trace lines from start to end\r\n\
\t SCAN <start> <lines> - Display raw scan data lines from start to end\r\n"
#define MONSETOPTIONS "\t\tAccu-Vote Monitor Set Options\r\n\
\t TRACE <module> <level> - set a module's trace level\r\n"
#define MONDISPOPTIONS "\t\tAccu-Vote Monitor Display Options\r\n\
\t MEMCARD - Display the memory card status\r\n\
\t BUFFERS - Display network buffer statistics\r\n\
\t TCP - Display TCP session statistics\r\n\
\t IP - Display IP statistics\r\n\
\t PPP - Display PPP session statistics\r\n\
\t SERIAL - Display serial I/O statistics\r\n"
#define TRACEMASKOPTIONS "\t\tAccu-Vote Monitor Trace Mask Options (when enabled)\r\n\
\tUNDEF - Set trace mask for undefined modules\r\n\
\tPPP - Set trace mask for PPP driver\r\n\
\tIP - Set trace mask for IP router\r\n\
\tTCP - Set trace mask for TCP operations\r\n\
\tCHAT - Set trace mask for CHAT modem dialer\r\n\
\tECHO - Set trace level for ECHO service\r\n\
\tFEEDER - Set trace level for Accu-Feed control\r\n\
\tSCAN - Set trace level for Accu-Vote's scanner\r\n"
#define TRACELEVELOPTIONS "\t\tAccu-Vote Monitor Trace Mask Values\r\n\
\tERR,1 - Log critical errors\r\n\
\tNOTICE,2 - Log non-critical errors\r\n\
\tWARNING,3 - Log warnings\r\n\
\tINFO,5 - Log informative messages\r\n\
\tDETAIL,6 - Log detail messages\r\n\
\tDEBUG,7 - Log debugging trace messages\r\n"
#define DUMPSTARTVALUES "\t\tAccu-Vote Monitor Dump Start Values\r\n\
\t<Start> - Optional number to start the dump at\r\n"
#define DUMPLINESVALUES "\t\tAccu-Vote Monitor Dump Lines Values\r\n\
\t<Lines> - Optional number of lines to dump (default 20)\r\n"
/**************************/
/*** PRIVATE DATA TYPES ***/
/**************************/
struct MonitorControl_s;
struct TokenTable_s;
typedef int ParseFcn(struct MonitorControl_s *mc, const char *tokenPtr, int tokenLen);
typedef int CmdFcn(struct MonitorControl_s *mc);
/*
* Monitor control block.
*/
typedef struct MonitorControl_s {
FILE *fp; /* File device pointer. */
char cmdBuf[CMDLINESZ + 1]; /* Current command line. */
int cmdLen; /* Current length of command. */
ParseFcn *parseFcn; /* Current parsing function. */
CmdFcn *commandFcn; /* Current monitor command. */
int curCmdArgs[MAXCMDARGS]; /* Arguments for the current command. */
int curCmdArgQty; /* Number of arguements for command. */
const struct TokenTable_s *curTokenTbl; /* Current token table. */
char *sendStr; /* String to be sent. */
char monitorStack[STACK_SIZE]; /* The monitor task stack. */
} MonitorControl;
/*
* Token table structure used for parsing.
*/
typedef struct TokenTable_s {
char *tokenLabel; /* Token literal. */
int tokenValue; /* Value for this token. */
ParseFcn *parseFcn; /* Current parsing function. */
const struct TokenTable_s *nextTbl; /* Table for next token. */
char *errStr; /* Error string if next token fails. */
} TokenTable;
typedef struct DebugLog_t {
ULONG logTime; /* Time of message. */
UINT OSTCBPrio; /* Current Task priority */
// NumTraceCodes tCode; /* Numeric trace code */
int tCode; /* Numeric trace code */
ULONG arg1, arg2; /* Numeric trace arguements */
char logMsg[LOGMSGLEN + 1]; /* Trace message. */
} DebugLog;
/***********************************/
/*** LOCAL FUNCTION DECLARATIONS ***/
/***********************************/
#if DEBUG_SUPPORT > 0
static void monitorMain0(void *arg);
#if DEBUGMONPORT > 0
static void monitorMain1(void *arg);
#endif
static int monProcInput(MonitorControl *mc, char *rBuf, int inCnt);
static int monParseCmd(MonitorControl *mc);
static int parseCmdToken(MonitorControl *mc, const char *tokenPtr, int tokenLen);
static int parseCmdArg(MonitorControl *mc, const char *tokenPtr, int tokenLen);
static int parseEOL(MonitorControl *mc, const char *tokenPtr, int tokenLen);
static int monDump(MonitorControl *mc);
static int monSendStr(MonitorControl *mc);
static int monSet(MonitorControl *mc);
static int monDisplay(MonitorControl *mc);
static int monSendMask(MonitorControl *mc);
static int monSendStats(MonitorControl *mc, DiagStat ds[]);
static const TokenTable *findToken(const char *tokenPtr, int tokenLen, const TokenTable *tt);
#endif
/*******************************/
/*** PRIVATE DATA STRUCTURES ***/
/*******************************/
#if DEBUG_SUPPORT > 0
DebugLog debugLog[MAXLOGSZ]; /* Debug trace log. */
u_int logTail = 0, logHead = 0; /* Indexes to top and bottom of log. */
MonitorControl monitorControl[2]; /* The monitor control blocks. */
/*
* The command execution function vectors.
*/
CmdFcn * const cmdFcn[] = {
monSendStr, /* MONCMD_SENDSTR - Used for error messages. */
monDump, /* MONCMD_DUMP */
monSet, /* MONCMD_SET */
monDisplay /* MONCMD_DISPLAY */
};
/*
* Set mask command module option lookup table.
*/
const TokenTable traceLevelToken[] = {
{"ERROR", LOG_ERR, parseEOL, NULL},
{"NOTICE", LOG_NOTICE, parseEOL, NULL},
{"WARNING", LOG_WARNING, parseEOL, NULL},
{"INFO", LOG_INFO, parseEOL, NULL},
{"DETAIL", LOG_DETAIL, parseEOL, NULL},
{"DEBUG", LOG_DEBUG, parseEOL, NULL},
{"%d", 0, parseEOL, NULL},
{"", 0}
};
/*
* Set mask command module option lookup table.
*/
const TokenTable maskModuleToken[] = {
{"UNDEF", TL_UNDEF, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
{"PPP", TL_PPP, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
{"IP", TL_IP, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
{"TCP", TL_TCP, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
{"CHAT", TL_CHAT, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
{"ECHO", TL_ECHO, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
#if TRACEFEEDER > 0
{"FEEDER", TL_FEEDER, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
#endif
#if TRACESCAN > 0
{"SCAN", TL_SCAN, parseCmdArg, traceLevelToken, TRACELEVELOPTIONS},
#endif
{"", 0}
};
const TokenTable dumpLinesToken[] = {
{"%d", 0, parseEOL, NULL, NULL},
{"", 0}
};
const TokenTable dumpStartToken[] = {
{"%d", 0, parseCmdArg, dumpLinesToken, DUMPLINESVALUES},
{"", 0}
};
/*
* Dump command option lookup table.
*/
const TokenTable dumpOptToken[] = {
{"TRACE", MONDUMP_TRACE, parseCmdArg, dumpStartToken, DUMPSTARTVALUES},
{"SCAN", MONDUMP_SCAN, parseCmdArg, dumpStartToken, DUMPSTARTVALUES},
{"", 0}
};
/*
* Set command option lookup table.
*/
const TokenTable setOptToken[] = {
{"TRACE", MONSET_TRACE, parseCmdArg, maskModuleToken, TRACEMASKOPTIONS},
{"", 0}
};
/*
* Display command option lookup table.
*/
const TokenTable displayOptToken[] = {
{"MEMCARD", MONDISP_MCARD, parseEOL, NULL},
{"BUFFERS", MONDISP_BUFFER, parseEOL, NULL},
{"TCP", MONDISP_TCP, parseEOL, NULL},
{"IP", MONDISP_IP, parseEOL, NULL},
{"PPP", MONDISP_PPP, parseEOL, NULL},
{"SERIAL", MONDISP_SERIAL, parseEOL, NULL},
{"", 0}
};
/*
* Command token lookup table. Note that the order is important since we'll
* match on the first string that matches the user's entered characters.
*/
const TokenTable cmdToken[] = {
{"DUMP", MONCMD_DUMP, parseCmdArg, dumpOptToken, MONDUMPOPTIONS},
{"SET", MONCMD_SET, parseCmdArg, setOptToken, MONSETOPTIONS},
{"DISPLAY", MONCMD_DISPLAY, parseCmdArg, displayOptToken, MONDISPOPTIONS},
{"\r\n", MONCMD_DUMP, parseCmdArg, dumpOptToken, MONDUMPOPTIONS},
{"", 0}
};
#endif
/******************************/
/*** PUBLIC DATA STRUCTURES ***/
/******************************/
#if DEBUG_SUPPORT > 0
int traceLevel[TL_MAX]; /* Module trace levels. */
#endif
/************************/
/*** PUBLIC FUNCTIONS ***/
/************************/
void debugInit(void)
{
#if DEBUG_SUPPORT > 0
int i;
memset(debugLog, 0, sizeof(debugLog));
for (i = 0; i < TL_MAX; i++)
traceLevel[i] = LOG_INFO;
#endif
}
void monStart(void)
{
#if DEBUG_SUPPORT > 0
/* Start the monitor tasks. */
OSTaskCreate(monitorMain0, (void *)0,
monitorControl[0].monitorStack + STACK_SIZE, PRI_MON0);
#if DEBUGMONPORT > 0
/* Start the monitor tasks. */
OSTaskCreate(monitorMain1, (void *)1,
monitorControl[1].monitorStack + STACK_SIZE, PRI_MON1);
#endif
#endif
}
#pragma argsused
void setTraceLevel(INT level, TraceModule tMod)
{
#if DEBUG_SUPPORT > 0
traceLevel[tMod] = level;
#endif
}
#pragma argsused
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,...)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -