git: f386b27736fe - main - unr(9): add 'show unrhdr' ddb command

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 29 May 2023 22:11:22 UTC
The branch main has been updated by kib:

URL: https://cgit.FreeBSD.org/src/commit/?id=f386b27736fe6dee535a530d5c7610d8a9827758

commit f386b27736fe6dee535a530d5c7610d8a9827758
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-05-25 11:14:19 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-05-29 22:10:36 +0000

    unr(9): add 'show unrhdr' ddb command
    
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D40089
---
 sys/kern/subr_unit.c | 59 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 43 insertions(+), 16 deletions(-)

diff --git a/sys/kern/subr_unit.c b/sys/kern/subr_unit.c
index b0977c3c01b9..0b8be66a8c4d 100644
--- a/sys/kern/subr_unit.c
+++ b/sys/kern/subr_unit.c
@@ -926,15 +926,18 @@ free_unr(struct unrhdr *uh, u_int item)
 		Free(p2);
 }
 
-#ifndef _KERNEL	/* USERLAND test driver */
+#ifdef _KERNEL
+#include "opt_ddb.h"
+#ifdef DDB
+#include <ddb/ddb.h>
+#endif
+#endif
 
-/*
- * Simple stochastic test driver for the above functions.  The code resides
- * here so that it can access static functions and structures.
- */
+#if (defined(_KERNEL) && defined(DDB)) || !defined(_KERNEL)
 
-static bool verbose;
-#define VPRINTF(...)	{if (verbose) printf(__VA_ARGS__);}
+#if !defined(_KERNEL)
+#define db_printf printf
+#endif
 
 static void
 print_unr(struct unrhdr *uh, struct unr *up)
@@ -942,21 +945,21 @@ print_unr(struct unrhdr *uh, struct unr *up)
 	u_int x;
 	struct unrb *ub;
 
-	printf("  %p len = %5u ", up, up->len);
+	db_printf("  %p len = %5u ", up, up->len);
 	if (up->ptr == NULL)
-		printf("free\n");
+		db_printf("free\n");
 	else if (up->ptr == uh)
-		printf("alloc\n");
+		db_printf("alloc\n");
 	else {
 		ub = up->ptr;
-		printf("bitmap [");
+		db_printf("bitmap [");
 		for (x = 0; x < up->len; x++) {
 			if (bit_test(ub->map, x))
-				printf("#");
+				db_printf("#");
 			else
-				printf(" ");
+				db_printf(" ");
 		}
-		printf("]\n");
+		db_printf("]\n");
 	}
 }
 
@@ -966,12 +969,12 @@ print_unrhdr(struct unrhdr *uh)
 	struct unr *up;
 	u_int x;
 
-	printf(
+	db_printf(
 	    "%p low = %u high = %u first = %u last = %u busy %u chunks = %u\n",
 	    uh, uh->low, uh->high, uh->first, uh->last, uh->busy, uh->alloc);
 	x = uh->low + uh->first;
 	TAILQ_FOREACH(up, &uh->head, list) {
-		printf("  from = %5u", x);
+		db_printf("  from = %5u", x);
 		print_unr(uh, up);
 		if (up->ptr == NULL || up->ptr == uh)
 			x += up->len;
@@ -980,6 +983,30 @@ print_unrhdr(struct unrhdr *uh)
 	}
 }
 
+#endif
+
+#if defined(_KERNEL) && defined(DDB)
+DB_SHOW_COMMAND(unrhdr, unrhdr_print_unrhdr)
+{
+	if (!have_addr) {
+		db_printf("show unrhdr addr\n");
+		return;
+	}
+
+	print_unrhdr((struct unrhdr *)addr);
+}
+#endif
+
+#ifndef _KERNEL	/* USERLAND test driver */
+
+/*
+ * Simple stochastic test driver for the above functions.  The code resides
+ * here so that it can access static functions and structures.
+ */
+
+static bool verbose;
+#define VPRINTF(...)	{if (verbose) printf(__VA_ARGS__);}
+
 static void
 test_alloc_unr(struct unrhdr *uh, u_int i, char a[])
 {