Module Name:    src
Committed By:   riastradh
Date:           Wed Jul 24 03:33:48 UTC 2013

Modified Files:
        src/sys/external/bsd/drm2/include/linux [riastradh-drm2]: printk.h

Log Message:
Add hex dumping utilities to <linux/printk.h>.


To generate a diff of this commit:
cvs rdiff -u -r1.1.2.1 -r1.1.2.2 \
    src/sys/external/bsd/drm2/include/linux/printk.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/external/bsd/drm2/include/linux/printk.h
diff -u src/sys/external/bsd/drm2/include/linux/printk.h:1.1.2.1 src/sys/external/bsd/drm2/include/linux/printk.h:1.1.2.2
--- src/sys/external/bsd/drm2/include/linux/printk.h:1.1.2.1	Wed Jul 24 02:37:36 2013
+++ src/sys/external/bsd/drm2/include/linux/printk.h	Wed Jul 24 03:33:48 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: printk.h,v 1.1.2.1 2013/07/24 02:37:36 riastradh Exp $	*/
+/*	$NetBSD: printk.h,v 1.1.2.2 2013/07/24 03:33:48 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2013 The NetBSD Foundation, Inc.
@@ -32,6 +32,7 @@
 #ifndef _LINUX_PRINTK_H_
 #define _LINUX_PRINTK_H_
 
+#include <sys/param.h>
 #include <sys/systm.h>
 
 #define	printk		printf
@@ -40,4 +41,81 @@
 #define	KERN_WARNING	"drm kern warning: "
 #define	KERN_ERR	"drm kern error: "
 
+#define	DUMP_PREFIX_NONE	0
+#define	DUMP_PREFIX_OFFSET	1
+#define	DUMP_PREFIX_ADDRESS	2
+
+static inline void
+hex_dump_to_buffer(const void *buf, size_t buf_size, int bytes_per_line,
+    int bytes_per_group, char *output, size_t output_size, bool ascii __unused)
+{
+	const uint8_t *bytes = buf;
+	size_t i = 0, n;
+
+	KASSERT(output_size >= 1);
+	KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
+	KASSERT(powerof2(bytes_per_group));
+	KASSERT(0 < bytes_per_group);
+	KASSERT(bytes_per_group <= 8);
+
+	output[output_size - 1] = '\0';
+	while (i < buf_size) {
+		n = snprintf(output, output_size, "%02x", bytes[i++]);
+		if (n >= output_size)
+			break;
+		output += n; output_size -= n;
+		if ((i == buf_size) || (0 == (i % bytes_per_line)))
+			n = snprintf(output, output_size, "\n");
+		else if ((0 < i) && (0 == (i % bytes_per_group)))
+			n = snprintf(output, output_size, " ");
+		else
+			n = 0;
+		if (n >= output_size)
+			break;
+		output += n; output_size -= n;
+	}
+}
+
+static inline void
+print_hex_dump(const char *level, const char *prefix, int prefix_type,
+    int bytes_per_line, int bytes_per_group, const void *buf, size_t buf_size,
+    bool ascii)
+{
+	const uint8_t *bytes = buf;
+	/* Two digits and one space/newline per byte, plus a null.  */
+	char line[32*3 + 1];
+
+	KASSERT((bytes_per_line == 16) || (bytes_per_line == 32));
+	KASSERT(powerof2(bytes_per_group));
+	KASSERT(0 < bytes_per_group);
+	KASSERT(bytes_per_group <= 8);
+
+	while (0 < buf_size) {
+		const size_t n = MIN(buf_size, 32);
+
+		switch (prefix_type) {
+		case DUMP_PREFIX_OFFSET:
+			printf("%08zu: ", (bytes - (const uint8_t *)buf));
+			break;
+
+		case DUMP_PREFIX_ADDRESS:
+			printf("%p: ", bytes);
+			break;
+
+		case DUMP_PREFIX_NONE:
+		default:
+			break;
+		}
+
+		hex_dump_to_buffer(bytes, n, bytes_per_line, bytes_per_group,
+		    line, sizeof(line), ascii);
+		KASSERT(0 < strnlen(line, sizeof(line)));
+		KASSERT(line[strnlen(line, sizeof(line)) - 1] == '\n');
+		printf("%s", line);
+
+		bytes += n;
+		buf_size -= n;
+	}
+}
+
 #endif  /* _LINUX_LIST_H_ */

Reply via email to