From nobody Tue Nov 26 11:34:06 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1579185317; cv=none; d=zohomail.com; s=zohoarc; b=KnGGxjDKsVHG0wE47UF8cZXcrr/rhiCS8V3uGUTibbyHofp2oEj551QZ4yR+T1WzO7MFhleCClvZuGQQ5fnMDekB/YH4xmwQ3gtbkfTD/KcM95HKgz1ktmqDNGvSHMC2IE9wPpY3r+m4CqZPElyVbGhLQbc11UT+i5bc/qPNsgo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579185317; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=brJg7KXTCDypnx+NYaUaTwuGIbDCtoHP/hH18TzeXFk=; b=iZAFUPIqQ4kacjMw59BVUO3hAyPTzy4tOOQsfnPv8EWnO9vU3wpdkNMNps93zHykhNJ8WOG3xPgG8SuBWd+BIymXj/G5IiAe3gaRZsJqRyVVy/XjnbsxkUDJndY6t/uLKdtkoLUIgsujpTFINO57cYtJtQlVQKmLQGDfkwoPlzM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1579185317170353.8192125184273; Thu, 16 Jan 2020 06:35:17 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-137-3upL9bCOMIy7IRiR-pOiVQ-1; Thu, 16 Jan 2020 09:35:14 -0500 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4F001106BC24; Thu, 16 Jan 2020 14:35:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D221660C85; Thu, 16 Jan 2020 14:35:07 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5BD4C87085; Thu, 16 Jan 2020 14:35:07 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 00GEZ6HV021946 for ; Thu, 16 Jan 2020 09:35:06 -0500 Received: by smtp.corp.redhat.com (Postfix) id 3CB65385; Thu, 16 Jan 2020 14:35:06 +0000 (UTC) Received: from angien.redhat.com (unknown [10.43.2.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id B6145386 for ; Thu, 16 Jan 2020 14:34:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579185316; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=brJg7KXTCDypnx+NYaUaTwuGIbDCtoHP/hH18TzeXFk=; b=LXdLn6aFxzMjh0EA39L98ybl+H0JPjOAehHNFMSJY2K1ZhO1T7DXhSIxlJ5vQhnm4srE84 Wn/xlvptz2FX59tE8fJ0DpdOcGVbQ1k1CHVnUvB+uJLfez3BO86WIgv5sOskfGLkOhD5lT NHdrA9nZfDaKU8a6jaggN206jMta61I= From: Peter Krempa To: libvir-list@redhat.com Subject: [PATCH 1/2] util: hash: Improve debugability of "Duplicate key" error message Date: Thu, 16 Jan 2020 15:34:30 +0100 Message-Id: In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-MC-Unique: 3upL9bCOMIy7IRiR-pOiVQ-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" If we get an 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 --- src/conf/domain_addr.c | 9 +++++++++ src/util/vircgroup.c | 8 ++++++++ src/util/virhash.c | 22 ++++++++++++++++++++-- src/util/virhash.h | 13 +++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/conf/domain_addr.c b/src/conf/domain_addr.c index ef7ee80e6a..607ba56efd 100644 --- a/src/conf/domain_addr.c +++ b/src/conf/domain_addr.c @@ -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(virDomainPCIAddr= essSetPtr addrs, virZPCIAddrKeyCode, virZPCIAddrKeyEqual, virZPCIAddrKeyCopy, + virZPCIAddrKeyPrint= Human, virZPCIAddrKeyFree)= )) goto error; @@ -1048,6 +1056,7 @@ virDomainPCIAddressSetExtensionAlloc(virDomainPCIAddr= essSetPtr addrs, virZPCIAddrKeyCode, virZPCIAddrKeyEqual, virZPCIAddrKeyCopy, + virZPCIAddrKeyPrint= Human, virZPCIAddrKeyFree)= )) goto error; } diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 87ed9f4565..dff2f6fd3a 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -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=3D%p path=3D%s signum=3D%d", group, group->path, sign= um); diff --git a/src/util/virhash.c b/src/util/virhash.c index 313ca57a54..edf11e8b7a 100644 --- a/src/util/virhash.c +++ b/src/util/virhash.c @@ -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 =3D NULL; @@ -153,6 +163,7 @@ virHashTablePtr virHashCreateFull(ssize_t size, table->keyCode =3D keyCode; table->keyEqual =3D keyEqual; table->keyCopy =3D keyCopy; + table->keyPrint =3D keyPrint; table->keyFree =3D 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, virHashData= Free dataFree) virHashStrCode, virHashStrEqual, virHashStrCopy, + virHashStrPrintHuman, virHashStrFree); } @@ -353,8 +366,13 @@ virHashAddOrUpdateEntry(virHashTablePtr table, const v= oid *name, entry->payload =3D userdata; return 0; } else { - virReportError(VIR_ERR_INTERNAL_ERROR, "%s", - _("Duplicate key")); + g_autofree char *keystr =3D NULL; + + if (table->keyPrint) + keystr =3D table->keyPrint(name); + + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Duplicate hash table key '%s'"), NULLSTR= (keystr)); return -1; } } diff --git a/src/util/virhash.h b/src/util/virhash.h index 6318c0b3cd..08f99d8a3d 100644 --- a/src/util/virhash.h +++ b/src/util/virhash.h @@ -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. C= aller + * 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); --=20 2.24.1