git: 694714adcd19 - stable/13 - unr(9): add 'show unrhdr' ddb command

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 05 Jun 2023 08:36:06 UTC
The branch stable/13 has been updated by kib:

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

commit 694714adcd19b7463b2fe697f2d8301a13a25729
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2023-05-25 11:14:19 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2023-06-05 08:35:01 +0000

    unr(9): add 'show unrhdr' ddb command
    
    (cherry picked from commit f386b27736fe6dee535a530d5c7610d8a9827758)
---
 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 a78b6ddf4840..0f4d25257553 100644
--- a/sys/kern/subr_unit.c
+++ b/sys/kern/subr_unit.c
@@ -939,15 +939,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)
@@ -955,21 +958,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");
 	}
 }
 
@@ -979,12 +982,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;
@@ -993,6 +996,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[])
 {