nvlist: Add nvlist_snprintf() and zfs_dbgmsg_nvlist()

Add nvlist_snprintf() to print a nvlist to a buffer.  This is basically
the snprintf() version of dump_nvlist().  Along with that, add a
zfs_dbgmsg_nvlist() to print out an nvlist to dbgmsg.  This will aid in
debugging.

Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Signed-off-by: Tony Hutter <hutter2@llnl.gov>
Closes #17215
This commit is contained in:
Tony Hutter
2025-04-18 06:22:16 -07:00
committed by GitHub
parent ba03054c83
commit 8d1489735b
12 changed files with 488 additions and 332 deletions
+1
View File
@@ -413,6 +413,7 @@ ZFS_OBJS := \
zfeature.o \
zfs_byteswap.o \
zfs_chksum.o \
zfs_debug_common.o \
zfs_fm.o \
zfs_fuid.o \
zfs_impl.o \
+236
View File
@@ -3681,6 +3681,240 @@ nvs_xdr(nvstream_t *nvs, nvlist_t *nvl, char *buf, size_t *buflen)
return (err);
}
#define NVP(buf, size, len, buf_end, elem, type, vtype, ptype, format) { \
vtype value; \
int rc; \
\
(void) nvpair_value_##type(elem, &value); \
rc = snprintf(buf, size, "%*s%s: " format "\n", indent, "", \
nvpair_name(elem), (ptype)value); \
if (rc < 0) \
return (rc); \
size = MAX((int)size - rc, 0); \
buf = size == 0 ? NULL : buf_end - size; \
len += rc; \
}
#define NVPA(buf, size, len, buf_end, elem, type, vtype, ptype, format) \
{ \
uint_t i, count; \
vtype *value; \
int rc; \
\
(void) nvpair_value_##type(elem, &value, &count); \
for (i = 0; i < count; i++) { \
rc = snprintf(buf, size, "%*s%s[%d]: " format "\n", indent, \
"", nvpair_name(elem), i, (ptype)value[i]); \
if (rc < 0) \
return (rc); \
size = MAX((int)size - rc, 0); \
buf = size == 0 ? NULL : buf_end - size; \
len += rc; \
} \
}
/*
* snprintf() version of dump_nvlist()
*
* Works just like snprintf(), but with an nvlist and indent count as args.
*
* Output is similar to nvlist_print() but handles arrays slightly differently.
*
* Return value matches C99 snprintf() return value conventions.
*/
int
nvlist_snprintf(char *buf, size_t size, nvlist_t *list, int indent)
{
nvpair_t *elem = NULL;
boolean_t bool_value;
nvlist_t *nvlist_value;
nvlist_t **nvlist_array_value;
uint_t i, count;
int len = 0;
int rc;
char *buf_end = &buf[size];
if (list == NULL)
return (0);
while ((elem = nvlist_next_nvpair(list, elem)) != NULL) {
switch (nvpair_type(elem)) {
case DATA_TYPE_BOOLEAN:
rc = snprintf(buf, size, "%*s%s\n", indent, "",
nvpair_name(elem));
if (rc < 0)
return (rc);
size = MAX((int)size - rc, 0);
buf = size == 0 ? NULL : buf_end - size;
len += rc;
break;
case DATA_TYPE_BOOLEAN_VALUE:
(void) nvpair_value_boolean_value(elem, &bool_value);
rc = snprintf(buf, size, "%*s%s: %s\n", indent, "",
nvpair_name(elem), bool_value ? "true" : "false");
if (rc < 0)
return (rc);
size = MAX((int)size - rc, 0);
buf = size == 0 ? NULL : buf_end - size;
len += rc;
break;
case DATA_TYPE_BYTE:
NVP(buf, size, len, buf_end, elem, byte, uchar_t, int,
"%u");
break;
case DATA_TYPE_INT8:
NVP(buf, size, len, buf_end, elem, int8, int8_t, int,
"%d");
break;
case DATA_TYPE_UINT8:
NVP(buf, size, len, buf_end, elem, uint8, uint8_t, int,
"%u");
break;
case DATA_TYPE_INT16:
NVP(buf, size, len, buf_end, elem, int16, int16_t, int,
"%d");
break;
case DATA_TYPE_UINT16:
NVP(buf, size, len, buf_end, elem, uint16, uint16_t,
int, "%u");
break;
case DATA_TYPE_INT32:
NVP(buf, size, len, buf_end, elem, int32, int32_t,
long, "%ld");
break;
case DATA_TYPE_UINT32:
NVP(buf, size, len, buf_end, elem, uint32, uint32_t,
ulong_t, "%lu");
break;
case DATA_TYPE_INT64:
NVP(buf, size, len, buf_end, elem, int64, int64_t,
longlong_t, "%lld");
break;
case DATA_TYPE_UINT64:
NVP(buf, size, len, buf_end, elem, uint64, uint64_t,
u_longlong_t, "%llu");
break;
case DATA_TYPE_STRING:
NVP(buf, size, len, buf_end, elem, string, const char *,
const char *, "'%s'");
break;
case DATA_TYPE_BYTE_ARRAY:
NVPA(buf, size, len, buf_end, elem, byte_array, uchar_t,
int, "%u");
break;
case DATA_TYPE_INT8_ARRAY:
NVPA(buf, size, len, buf_end, elem, int8_array, int8_t,
int, "%d");
break;
case DATA_TYPE_UINT8_ARRAY:
NVPA(buf, size, len, buf_end, elem, uint8_array,
uint8_t, int, "%u");
break;
case DATA_TYPE_INT16_ARRAY:
NVPA(buf, size, len, buf_end, elem, int16_array,
int16_t, int, "%d");
break;
case DATA_TYPE_UINT16_ARRAY:
NVPA(buf, size, len, buf_end, elem, uint16_array,
uint16_t, int, "%u");
break;
case DATA_TYPE_INT32_ARRAY:
NVPA(buf, size, len, buf_end, elem, int32_array,
int32_t, long, "%ld");
break;
case DATA_TYPE_UINT32_ARRAY:
NVPA(buf, size, len, buf_end, elem, uint32_array,
uint32_t, ulong_t, "%lu");
break;
case DATA_TYPE_INT64_ARRAY:
NVPA(buf, size, len, buf_end, elem, int64_array,
int64_t, longlong_t, "%lld");
break;
case DATA_TYPE_UINT64_ARRAY:
NVPA(buf, size, len, buf_end, elem, uint64_array,
uint64_t, u_longlong_t, "%llu");
break;
case DATA_TYPE_STRING_ARRAY:
NVPA(buf, size, len, buf_end, elem, string_array,
const char *, const char *, "'%s'");
break;
case DATA_TYPE_NVLIST:
(void) nvpair_value_nvlist(elem, &nvlist_value);
rc = snprintf(buf, size, "%*s%s:\n", indent, "",
nvpair_name(elem));
if (rc < 0)
return (rc);
size = MAX((int)size - rc, 0);
buf = size == 0 ? NULL : buf_end - size;
len += rc;
rc = nvlist_snprintf(buf, size, nvlist_value,
indent + 4);
if (rc < 0)
return (rc);
size = MAX((int)size - rc, 0);
buf = size == 0 ? NULL : buf_end - size;
len += rc;
break;
case DATA_TYPE_NVLIST_ARRAY:
(void) nvpair_value_nvlist_array(elem,
&nvlist_array_value, &count);
for (i = 0; i < count; i++) {
rc = snprintf(buf, size, "%*s%s[%u]:\n",
indent, "", nvpair_name(elem), i);
if (rc < 0)
return (rc);
size = MAX((int)size - rc, 0);
buf = size == 0 ? NULL : buf_end - size;
len += rc;
rc = nvlist_snprintf(buf, size,
nvlist_array_value[i], indent + 4);
if (rc < 0)
return (rc);
size = MAX((int)size - rc, 0);
buf = size == 0 ? NULL : buf_end - size;
len += rc;
}
break;
default:
rc = snprintf(buf, size, "bad config type %d for %s\n",
nvpair_type(elem), nvpair_name(elem));
if (rc < 0)
return (rc);
size = MAX((int)size - rc, 0);
buf = size == 0 ? NULL : buf_end - size;
len += rc;
}
}
return (len);
}
EXPORT_SYMBOL(nv_alloc_init);
EXPORT_SYMBOL(nv_alloc_reset);
EXPORT_SYMBOL(nv_alloc_fini);
@@ -3766,6 +4000,8 @@ EXPORT_SYMBOL(nvlist_lookup_pairs);
EXPORT_SYMBOL(nvlist_lookup_nvpair);
EXPORT_SYMBOL(nvlist_exists);
EXPORT_SYMBOL(nvlist_snprintf);
/* processing nvpair */
EXPORT_SYMBOL(nvpair_name);
EXPORT_SYMBOL(nvpair_type);
+97
View File
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: CDDL-1.0
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or https://opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2025 by Lawrence Livermore National Security, LLC.
*/
/*
* This file contains zfs_dbgmsg() specific functions that are not OS or
* userspace specific.
*/
#if !defined(_KERNEL)
#include <string.h>
#endif
#include <sys/zfs_context.h>
#include <sys/zfs_debug.h>
#include <sys/nvpair.h>
/*
* Given a multi-line string, print out one of the lines and return a pointer
* to the next line. Lines are demarcated by '\n'. Note: this modifies the
* input string (buf[]).
*
* This function is meant to be used in a loop like:
* while (buf != NULL)
* buf = kernel_print_one_line(buf);
*
* This function is useful for printing large, multi-line text buffers.
*
* Returns the pointer to the beginning of the next line in buf[], or NULL
* if it's the last line, or nothing more to print.
*/
static char *
zfs_dbgmsg_one_line(char *buf)
{
char *nl;
if (!buf)
return (NULL);
nl = strchr(buf, '\n');
if (nl == NULL) {
__zfs_dbgmsg(buf);
return (NULL); /* done */
}
*nl = '\0';
__zfs_dbgmsg(buf);
return (nl + 1);
}
/*
* Dump an nvlist tree to dbgmsg.
*
* This is the zfs_dbgmsg version of userspace's dump_nvlist() from libnvpair.
*/
void
__zfs_dbgmsg_nvlist(nvlist_t *nv)
{
int len;
char *buf;
len = nvlist_snprintf(NULL, 0, nv, 4);
len++; /* Add null terminator */
buf = vmem_alloc(len, KM_SLEEP);
if (buf == NULL)
return;
(void) nvlist_snprintf(buf, len, nv, 4);
while (buf != NULL)
buf = zfs_dbgmsg_one_line(buf);
vmem_free(buf, len);
}
#ifdef _KERNEL
EXPORT_SYMBOL(__zfs_dbgmsg_nvlist);
#endif