util: hash: Improve debugability of "Duplicate key" error message

If we get a user reporting this error message being shown it's pretty
useless in terms of actually debugging it since we don't know which hash
and which key are actually subject to the error.

This patch adds a new hash table callback which formats the
user-readable version of the hash key and reports it in the new message
which will look like:

"Duplicate hash table key 'blah'"

That way we will at least have an anchor point where to start the
search.

There are two special implementations of keys which are numeric so we
add specific printer functions for them.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Peter Krempa 2020-01-16 15:13:06 +01:00
parent 0c3792a155
commit ae60e05817
4 changed files with 50 additions and 2 deletions

View File

@ -1007,6 +1007,13 @@ virZPCIAddrKeyCopy(const void *name)
}
static char *
virZPCIAddrKeyPrintHuman(const void *name)
{
return g_strdup_printf("%u", *((unsigned int *)name));
}
static void
virZPCIAddrKeyFree(void *name)
{
@ -1041,6 +1048,7 @@ virDomainPCIAddressSetExtensionAlloc(virDomainPCIAddressSetPtr addrs,
virZPCIAddrKeyCode,
virZPCIAddrKeyEqual,
virZPCIAddrKeyCopy,
virZPCIAddrKeyPrintHuman,
virZPCIAddrKeyFree)))
goto error;
@ -1048,6 +1056,7 @@ virDomainPCIAddressSetExtensionAlloc(virDomainPCIAddressSetPtr addrs,
virZPCIAddrKeyCode,
virZPCIAddrKeyEqual,
virZPCIAddrKeyCopy,
virZPCIAddrKeyPrintHuman,
virZPCIAddrKeyFree)))
goto error;
}

View File

@ -2502,6 +2502,13 @@ virCgroupPidCopy(const void *name)
}
static char *
virCgroupPidPrintHuman(const void *name)
{
return g_strdup_printf("%ld", (const long)name);
}
int
virCgroupKillRecursiveInternal(virCgroupPtr group,
int signum,
@ -2587,6 +2594,7 @@ virCgroupKillRecursive(virCgroupPtr group, int signum)
virCgroupPidCode,
virCgroupPidEqual,
virCgroupPidCopy,
virCgroupPidPrintHuman,
NULL);
VIR_DEBUG("group=%p path=%s signum=%d", group, group->path, signum);

View File

@ -59,6 +59,7 @@ struct _virHashTable {
virHashKeyCode keyCode;
virHashKeyEqual keyEqual;
virHashKeyCopy keyCopy;
virHashKeyPrintHuman keyPrint;
virHashKeyFree keyFree;
};
@ -98,6 +99,14 @@ static void *virHashStrCopy(const void *name)
return ret;
}
static char *
virHashStrPrintHuman(const void *name)
{
return g_strdup(name);
}
static void virHashStrFree(void *name)
{
VIR_FREE(name);
@ -136,6 +145,7 @@ virHashTablePtr virHashCreateFull(ssize_t size,
virHashKeyCode keyCode,
virHashKeyEqual keyEqual,
virHashKeyCopy keyCopy,
virHashKeyPrintHuman keyPrint,
virHashKeyFree keyFree)
{
virHashTablePtr table = NULL;
@ -153,6 +163,7 @@ virHashTablePtr virHashCreateFull(ssize_t size,
table->keyCode = keyCode;
table->keyEqual = keyEqual;
table->keyCopy = keyCopy;
table->keyPrint = keyPrint;
table->keyFree = keyFree;
if (VIR_ALLOC_N(table->table, size) < 0) {
@ -180,6 +191,7 @@ virHashNew(virHashDataFree dataFree)
virHashStrCode,
virHashStrEqual,
virHashStrCopy,
virHashStrPrintHuman,
virHashStrFree);
}
@ -200,6 +212,7 @@ virHashTablePtr virHashCreate(ssize_t size, virHashDataFree dataFree)
virHashStrCode,
virHashStrEqual,
virHashStrCopy,
virHashStrPrintHuman,
virHashStrFree);
}
@ -353,8 +366,13 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const void *name,
entry->payload = userdata;
return 0;
} else {
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
_("Duplicate key"));
g_autofree char *keystr = NULL;
if (table->keyPrint)
keystr = table->keyPrint(name);
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Duplicate hash table key '%s'"), NULLSTR(keystr));
return -1;
}
}

View File

@ -86,6 +86,18 @@ typedef bool (*virHashKeyEqual)(const void *namea, const void *nameb);
* Returns a newly allocated copy of @name
*/
typedef void *(*virHashKeyCopy)(const void *name);
/**
* virHashKeyPrintHuman:
* @name: the hash key
*
* Get a human readable version of the key for error messages. Caller
* will free the returned string.
*
* Returns a string representation of the key for use in error messages. Caller
* promises to always free the returned string.
*/
typedef char *(*virHashKeyPrintHuman) (const void *name);
/**
* virHashKeyFree:
* @name: the hash key
@ -108,6 +120,7 @@ virHashTablePtr virHashCreateFull(ssize_t size,
virHashKeyCode keyCode,
virHashKeyEqual keyEqual,
virHashKeyCopy keyCopy,
virHashKeyPrintHuman keyPrint,
virHashKeyFree keyFree);
void virHashFree(virHashTablePtr table);
ssize_t virHashSize(const virHashTable *table);