Allow adding error symbols after early initialization
This was acomplished by making errSymbolAdd add the error symbol directly into the global hash table and removing errnumlist which is not needed anymore. Unit tests were added for checking the following cases: - Adding a valid symbol and checking that it exists (fixed by this change) - Getting an existing error symbol - Getting a non existing error symbol - Adding an invalid error symbol (fixed by this change) - Adding an error symbol with a code that already exists (fixed by this change) Therefore, issue #268 was fixed error: statically allocate error symbol hash table This will allow calling errSymbolAdd before errSymBld, therefore, a function adding error symbols can now be run before iocInit or errlogInit error: add a constant for the minimum module number Make adding an identical error symbol not fail A test case was also added which test that adding an error symbol with same error code and message as one added before will not fail Add locking to error symbol table This protects the cases of: - simultaneously adding and requesting of an error symbol - simultaneously adding many error symbols Update release notes regarding adding error symbols
This commit is contained in:

committed by
Michael Davidsaver

parent
45b3bce515
commit
8c08c57247
@ -102,6 +102,10 @@ and string formats, some of which support full nanosecond precision.
|
||||
More information is included in the filters documentation, which can be found in
|
||||
the `html/filters.html` document that is generated during the build
|
||||
|
||||
### Allow adding new error symbols at any time
|
||||
|
||||
`errSymbolAdd` can now be called after early initialization.
|
||||
|
||||
### Add conditional output (OOPT) to the longout record
|
||||
|
||||
The longout record can now be configured using its new OOPT and OOCH fields
|
||||
|
@ -26,6 +26,7 @@ ERR_S_FILES += $(LIBCOM)/as/asLib.h
|
||||
ERR_S_FILES += $(LIBCOM)/misc/epicsStdlib.h
|
||||
ERR_S_FILES += $(LIBCOM)/pool/epicsThreadPool.h
|
||||
ERR_S_FILES += $(LIBCOM)/error/errMdef.h
|
||||
ERR_S_FILES += $(LIBCOM)/error/errSymTbl.h
|
||||
ERR_S_FILES += $(TOP)/modules/database/src/ioc/db/dbAccessDefs.h
|
||||
ERR_S_FILES += $(TOP)/modules/database/src/ioc/dbStatic/dbStaticLib.h
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#define RTN_SUCCESS(STATUS) ((STATUS)==0)
|
||||
|
||||
/* Module numbers start above 500 for compatibility with vxWorks errnoLib */
|
||||
#define MIN_MODULE_NUM 501
|
||||
|
||||
/* FIXME: M_xxx values could be declared as integer variables and set
|
||||
* at runtime from registration routines; the S_xxx definitions would
|
||||
@ -32,6 +33,7 @@
|
||||
#define M_stdlib (504 << 16) /* EPICS Standard library */
|
||||
#define M_pool (505 << 16) /* Thread pool */
|
||||
#define M_time (506 << 16) /* epicsTime */
|
||||
#define M_err (507 << 16) /* Error */
|
||||
|
||||
/* ioc */
|
||||
#define M_dbAccess (511 << 16) /* Database Access Routines */
|
||||
|
@ -23,6 +23,8 @@
|
||||
#include "cantProceed.h"
|
||||
#include "epicsAssert.h"
|
||||
#include "epicsStdio.h"
|
||||
#include "epicsMutex.h"
|
||||
#include "epicsThread.h"
|
||||
#include "errMdef.h"
|
||||
#include "errSymTbl.h"
|
||||
#include "ellLib.h"
|
||||
@ -33,69 +35,49 @@
|
||||
static epicsUInt16 errhash(long errNum);
|
||||
|
||||
typedef struct errnumnode {
|
||||
ELLNODE node;
|
||||
long errNum;
|
||||
struct errnumnode *hashnode;
|
||||
const char *message;
|
||||
long pad;
|
||||
} ERRNUMNODE;
|
||||
|
||||
static ELLLIST errnumlist = ELLLIST_INIT;
|
||||
static ERRNUMNODE **hashtable;
|
||||
static int initialized = 0;
|
||||
typedef struct {
|
||||
ERRNUMNODE *table[NHASH * sizeof(ERRNUMNODE *)];
|
||||
epicsMutexId tableMutexId;
|
||||
} errHashTable_t;
|
||||
|
||||
static errHashTable_t errHashTable;
|
||||
|
||||
extern ERRSYMTAB_ID errSymTbl;
|
||||
|
||||
/****************************************************************
|
||||
* ERRSYMBLD
|
||||
*
|
||||
* Create the normal ell LIST of sorted error messages nodes
|
||||
* Followed by linked hash lists - that link together those
|
||||
* ell nodes that have a common hash number.
|
||||
* Populate EPICS error symbols
|
||||
*
|
||||
***************************************************************/
|
||||
int errSymBld(void)
|
||||
{
|
||||
ERRSYMBOL *errArray = errSymTbl->symbols;
|
||||
ERRNUMNODE *perrNumNode = NULL;
|
||||
ERRNUMNODE *pNextNode = NULL;
|
||||
ERRNUMNODE **phashnode = NULL;
|
||||
int i;
|
||||
int modnum;
|
||||
|
||||
if (initialized)
|
||||
return(0);
|
||||
|
||||
hashtable = (ERRNUMNODE**)callocMustSucceed
|
||||
(NHASH, sizeof(ERRNUMNODE*),"errSymBld");
|
||||
ERRSYMBOL *errArray = errSymTbl->symbols;
|
||||
int i;
|
||||
for (i = 0; i < errSymTbl->nsymbols; i++, errArray++) {
|
||||
modnum = errArray->errNum >> 16;
|
||||
if (modnum < 501) {
|
||||
fprintf(stderr, "errSymBld: ERROR - Module number in errSymTbl < 501 was Module=%lx Name=%s\n",
|
||||
errArray->errNum, errArray->name);
|
||||
continue;
|
||||
}
|
||||
if ((errSymbolAdd(errArray->errNum, errArray->name)) < 0) {
|
||||
if (errSymbolAdd(errArray->errNum, errArray->name)) {
|
||||
fprintf(stderr, "errSymBld: ERROR - errSymbolAdd() failed \n");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
perrNumNode = (ERRNUMNODE *) ellFirst(&errnumlist);
|
||||
while (perrNumNode) {
|
||||
/* hash each perrNumNode->errNum */
|
||||
epicsUInt16 hashInd = errhash(perrNumNode->errNum);
|
||||
|
||||
phashnode = (ERRNUMNODE**)&hashtable[hashInd];
|
||||
pNextNode = (ERRNUMNODE*) *phashnode;
|
||||
/* search for last node (NULL) of hashnode linked list */
|
||||
while (pNextNode) {
|
||||
phashnode = &pNextNode->hashnode;
|
||||
pNextNode = *phashnode;
|
||||
}
|
||||
*phashnode = perrNumNode;
|
||||
perrNumNode = (ERRNUMNODE *) ellNext((ELLNODE *) perrNumNode);
|
||||
}
|
||||
initialized = 1;
|
||||
return(0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _initErrorHashTable(void *_unused)
|
||||
{
|
||||
errHashTable.tableMutexId = epicsMutexMustCreate();
|
||||
}
|
||||
|
||||
static void initErrorHashTable()
|
||||
{
|
||||
static epicsThreadOnceId initErrSymOnceFlag = EPICS_THREAD_ONCE_INIT;
|
||||
epicsThreadOnce(&initErrSymOnceFlag, _initErrorHashTable, NULL);
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
@ -109,21 +91,48 @@ static epicsUInt16 errhash(long errNum)
|
||||
|
||||
modnum = (unsigned short) (errNum >> 16);
|
||||
errnum = (unsigned short) (errNum & 0xffff);
|
||||
return (((modnum - 500) * 20) + errnum) % NHASH;
|
||||
return ((modnum - (MIN_MODULE_NUM - 1)) * 20 + errnum) % NHASH;
|
||||
}
|
||||
|
||||
/****************************************************************
|
||||
* ERRSYMBOLADD
|
||||
* adds symbols to the master errnumlist as compiled from errSymTbl.c
|
||||
* adds symbols to the global error symbol hash table
|
||||
***************************************************************/
|
||||
int errSymbolAdd(long errNum, const char *name)
|
||||
{
|
||||
ERRNUMNODE *pNew = (ERRNUMNODE*) callocMustSucceed(1,
|
||||
sizeof(ERRNUMNODE), "errSymbolAdd");
|
||||
ERRNUMNODE *pNextNode = NULL;
|
||||
ERRNUMNODE **phashnode = NULL;
|
||||
ERRNUMNODE *pNew = NULL;
|
||||
int modnum = (epicsUInt16) (errNum >> 16);
|
||||
|
||||
if (modnum < MIN_MODULE_NUM)
|
||||
return S_err_invCode;
|
||||
|
||||
initErrorHashTable();
|
||||
|
||||
epicsUInt16 hashInd = errhash(errNum);
|
||||
|
||||
epicsMutexLock(errHashTable.tableMutexId);
|
||||
phashnode = (ERRNUMNODE**)&errHashTable.table[hashInd];
|
||||
pNextNode = (ERRNUMNODE*) *phashnode;
|
||||
|
||||
/* search for last node (NULL) of hashnode linked list */
|
||||
while (pNextNode) {
|
||||
if (pNextNode->errNum == errNum) {
|
||||
int notIdentical = strcmp(name, pNextNode->message);
|
||||
epicsMutexUnlock(errHashTable.tableMutexId);
|
||||
return notIdentical ? S_err_codeExists : 0;
|
||||
}
|
||||
phashnode = &pNextNode->hashnode;
|
||||
pNextNode = *phashnode;
|
||||
}
|
||||
|
||||
pNew = (ERRNUMNODE*) callocMustSucceed(
|
||||
1, sizeof(ERRNUMNODE), "errSymbolAdd");
|
||||
pNew->errNum = errNum;
|
||||
pNew->message = name;
|
||||
ellAdd(&errnumlist, (ELLNODE*)pNew);
|
||||
*phashnode = pNew;
|
||||
epicsMutexUnlock(errHashTable.tableMutexId);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -155,31 +164,31 @@ const char* errSymLookupInternal(long status)
|
||||
if (!status)
|
||||
return "Ok";
|
||||
|
||||
if (!initialized)
|
||||
errSymBld();
|
||||
initErrorHashTable();
|
||||
|
||||
modNum = (unsigned) status;
|
||||
modNum >>= 16;
|
||||
modNum &= 0xffff;
|
||||
if (modNum <= 500) {
|
||||
const char * pStr = strerror ((int) status);
|
||||
if (pStr) {
|
||||
return pStr;
|
||||
}
|
||||
if (modNum < MIN_MODULE_NUM) {
|
||||
return strerror ((int) status);
|
||||
}
|
||||
else {
|
||||
unsigned hashInd = errhash(status);
|
||||
phashnode = (ERRNUMNODE**)&hashtable[hashInd];
|
||||
const char *result = NULL;
|
||||
epicsMutexLock(errHashTable.tableMutexId);
|
||||
phashnode = (ERRNUMNODE**)&errHashTable.table[hashInd];
|
||||
pNextNode = *phashnode;
|
||||
while (pNextNode) {
|
||||
if (pNextNode->errNum==status){
|
||||
return pNextNode->message;
|
||||
result = pNextNode->message;
|
||||
break;
|
||||
}
|
||||
phashnode = &pNextNode->hashnode;
|
||||
pNextNode = *phashnode;
|
||||
}
|
||||
epicsMutexUnlock(errHashTable.tableMutexId);
|
||||
return result;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
const char* errSymMsg(long status)
|
||||
@ -210,12 +219,12 @@ void errSymDump(void)
|
||||
int i;
|
||||
int msgcount = 0;
|
||||
|
||||
if (!initialized) errSymBld();
|
||||
initErrorHashTable();
|
||||
|
||||
msgcount = 0;
|
||||
printf("errSymDump: number of hash slots = %d\n", NHASH);
|
||||
for (i = 0; i < NHASH; i++) {
|
||||
ERRNUMNODE **phashnode = &hashtable[i];
|
||||
ERRNUMNODE **phashnode = &errHashTable.table[i];
|
||||
ERRNUMNODE *pNextNode = *phashnode;
|
||||
int count = 0;
|
||||
|
||||
@ -246,14 +255,15 @@ void errSymTestPrint(long errNum)
|
||||
epicsUInt16 modnum;
|
||||
epicsUInt16 errnum;
|
||||
|
||||
if (!initialized) errSymBld();
|
||||
initErrorHashTable();
|
||||
|
||||
message[0] = '\0';
|
||||
modnum = (epicsUInt16) (errNum >> 16);
|
||||
errnum = (epicsUInt16) (errNum & 0xffff);
|
||||
if (modnum < 501) {
|
||||
if (modnum < MIN_MODULE_NUM) {
|
||||
fprintf(stderr, "Usage: errSymTestPrint(long errNum) \n");
|
||||
fprintf(stderr, "errSymTestPrint: module number < 501 \n");
|
||||
fprintf(stderr,
|
||||
"errSymTestPrint: module number < %d\n", MIN_MODULE_NUM);
|
||||
return;
|
||||
}
|
||||
errSymLookup(errNum, message, sizeof(message));
|
||||
@ -271,10 +281,11 @@ void errSymTest(epicsUInt16 modnum, epicsUInt16 begErrNum,
|
||||
long errNum;
|
||||
epicsUInt16 errnum;
|
||||
|
||||
if (!initialized) errSymBld();
|
||||
if (modnum < 501)
|
||||
if (modnum < MIN_MODULE_NUM)
|
||||
return;
|
||||
|
||||
initErrorHashTable();
|
||||
|
||||
/* print range of error messages */
|
||||
for (errnum = begErrNum; errnum <= endErrNum; errnum++) {
|
||||
errNum = modnum << 16;
|
||||
|
@ -16,6 +16,9 @@
|
||||
#include "libComAPI.h"
|
||||
#include "epicsTypes.h"
|
||||
|
||||
#define S_err_invCode (M_err | 1) /* Invalid error symbol code */
|
||||
#define S_err_codeExists (M_err | 2) /* Error code already exists */
|
||||
|
||||
/* ERRSYMBOL - entry in symbol table */
|
||||
typedef struct {
|
||||
long errNum; /* errMessage symbol number */
|
||||
|
@ -29,6 +29,7 @@
|
||||
#include "osiSock.h"
|
||||
#include "fdmgr.h"
|
||||
#include "epicsString.h"
|
||||
#include "errSymTbl.h"
|
||||
|
||||
/* private between errlog.c and this test */
|
||||
LIBCOM_API
|
||||
@ -197,13 +198,61 @@ void testANSIStrip(void)
|
||||
#undef testEscape
|
||||
}
|
||||
|
||||
static void testErrorMessageMatches(long status, const char *expected)
|
||||
{
|
||||
const char *msg = errSymMsg(status);
|
||||
testOk(strcmp(msg, expected) == 0,
|
||||
"Error code %ld returns \"%s\", expected message \"%s\"", status,
|
||||
msg, expected
|
||||
);
|
||||
}
|
||||
|
||||
static void testGettingExistingErrorSymbol()
|
||||
{
|
||||
testErrorMessageMatches(S_err_invCode, "Invalid error symbol code");
|
||||
}
|
||||
|
||||
static void testGettingNonExistingErrorSymbol()
|
||||
{
|
||||
long invented_code = (0x7999 << 16) | 0x9998;
|
||||
testErrorMessageMatches(invented_code, "<Unknown code>");
|
||||
}
|
||||
|
||||
static void testAddingErrorSymbol()
|
||||
{
|
||||
long invented_code = (0x7999 << 16) | 0x9999;
|
||||
errSymbolAdd(invented_code, "Invented Error Message");
|
||||
testErrorMessageMatches(invented_code, "Invented Error Message");
|
||||
}
|
||||
|
||||
static void testAddingInvalidErrorSymbol()
|
||||
{
|
||||
long invented_code = (500 << 16) | 0x1;
|
||||
testOk(errSymbolAdd(invented_code, "No matter"),
|
||||
"Adding error symbol with module code < 501 should fail");
|
||||
}
|
||||
|
||||
static void testAddingExistingErrorSymbol()
|
||||
{
|
||||
testOk(errSymbolAdd(S_err_invCode, "Duplicate"),
|
||||
"Adding an error symbol with an existing error code should fail");
|
||||
}
|
||||
|
||||
static void testAddingExistingErrorSymbolWithSameMessage()
|
||||
{
|
||||
long invented_code = (0x7999 << 16) | 0x9997;
|
||||
errSymbolAdd(invented_code, "Invented Error Message");
|
||||
testOk(!errSymbolAdd(invented_code, "Invented Error Message"),
|
||||
"Adding identical error symbol shouldn't fail");
|
||||
}
|
||||
|
||||
MAIN(epicsErrlogTest)
|
||||
{
|
||||
size_t mlen, i, N;
|
||||
char msg[256];
|
||||
clientPvt pvt, pvt2;
|
||||
|
||||
testPlan(48);
|
||||
testPlan(54);
|
||||
|
||||
testANSIStrip();
|
||||
|
||||
@ -414,6 +463,13 @@ MAIN(epicsErrlogTest)
|
||||
testLogPrefix();
|
||||
osiSockRelease();
|
||||
|
||||
testGettingExistingErrorSymbol();
|
||||
testGettingNonExistingErrorSymbol();
|
||||
testAddingErrorSymbol();
|
||||
testAddingInvalidErrorSymbol();
|
||||
testAddingExistingErrorSymbol();
|
||||
testAddingExistingErrorSymbolWithSameMessage();
|
||||
|
||||
return testDone();
|
||||
}
|
||||
/*
|
||||
|
Reference in New Issue
Block a user