Module Name:    src
Committed By:   christos
Date:           Fri Dec  8 21:51:07 UTC 2017

Modified Files:
        src/sys/dev/pcmcia: if_ray.c
        src/sys/dev/usb: xhci.c
        src/sys/lib/libkern: Makefile.libkern libkern.h
Added Files:
        src/sys/lib/libkern: hexdump.c

Log Message:
coalesce the two copies of hexdump into libkern


To generate a diff of this commit:
cvs rdiff -u -r1.86 -r1.87 src/sys/dev/pcmcia/if_ray.c
cvs rdiff -u -r1.78 -r1.79 src/sys/dev/usb/xhci.c
cvs rdiff -u -r1.40 -r1.41 src/sys/lib/libkern/Makefile.libkern
cvs rdiff -u -r0 -r1.1 src/sys/lib/libkern/hexdump.c
cvs rdiff -u -r1.124 -r1.125 src/sys/lib/libkern/libkern.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/dev/pcmcia/if_ray.c
diff -u src/sys/dev/pcmcia/if_ray.c:1.86 src/sys/dev/pcmcia/if_ray.c:1.87
--- src/sys/dev/pcmcia/if_ray.c:1.86	Fri Jan 13 09:48:18 2017
+++ src/sys/dev/pcmcia/if_ray.c	Fri Dec  8 16:51:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: if_ray.c,v 1.86 2017/01/13 14:48:18 maya Exp $	*/
+/*	$NetBSD: if_ray.c,v 1.87 2017/12/08 21:51:07 christos Exp $	*/
 
 /*
  * Copyright (c) 2000 Christian E. Hopps
@@ -57,7 +57,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: if_ray.c,v 1.86 2017/01/13 14:48:18 maya Exp $");
+__KERNEL_RCSID(0, "$NetBSD: if_ray.c,v 1.87 2017/12/08 21:51:07 christos Exp $");
 
 #include "opt_inet.h"
 
@@ -368,10 +368,6 @@ static struct timeval rtv, tv1, tv2, *tt
 	printf x ;				\
 	} } while (0)
 
-#define	HEXDF_NOCOMPRESS	0x1
-#define	HEXDF_NOOFFSET		0x2
-#define HEXDF_NOASCII		0x4
-void hexdump(const u_int8_t *, int, int, int, int);
 static void ray_dump_mbuf(struct ray_softc *, struct mbuf *);
 
 #else	/* !RAY_DEBUG */
@@ -1295,7 +1291,7 @@ ray_recv(struct ray_softc *sc, bus_size_
 #ifdef RAY_DEBUG
 	/* have a look if you want to see how the card rx works :) */
 	if (ray_debug && ray_debug_dump_desc)
-		hexdump((char *)sc->sc_memh + RAY_RCS_BASE, 0x400, 16, 4, 0);
+		hexdump(__func__, (char *)sc->sc_memh + RAY_RCS_BASE, 0x400);
 #endif
 
 	m = 0;
@@ -1448,7 +1444,7 @@ done:
 			RAY_DPRINTF(("%s: mgt packet not supported\n",
 			    device_xname(sc->sc_dev)));
 #ifdef RAY_DEBUG
-			hexdump((const u_int8_t*)frame, pktlen, 16, 4, 0);
+			hexdump(__func__, (const u_int8_t*)frame, pktlen);
 #endif
 			RAY_DPRINTF(("\n"));
 			break;
@@ -1537,7 +1533,7 @@ ray_recv_auth(struct ray_softc *sc, stru
 		RAY_DPRINTF(("%s: recv auth packet:\n",
 		    device_xname(sc->sc_dev)));
 #ifdef RAY_DEBUG
-		hexdump((const u_int8_t *)frame, sizeof(*frame) + 6, 16, 4, 0);
+		hexdump(__func__, (const u_int8_t *)frame, sizeof(*frame) + 6);
 #endif
 		RAY_DPRINTF(("\n"));
 
@@ -3067,108 +3063,13 @@ ray_write_region(struct ray_softc *sc, b
 
 #ifdef RAY_DEBUG
 
-#define PRINTABLE(c) ((c) >= 0x20 && (c) <= 0x7f)
-
-void
-hexdump(const u_int8_t *d, int len, int br, int div, int fl)
-{
-	int i, j, offw, first, tlen, ni, nj, sp;
-
-	sp = br / div;
-	offw = 0;
-	if (len && (fl & HEXDF_NOOFFSET) == 0) {
-		tlen = len;
-		do {
-			offw++;
-		} while (tlen /= br);
-	}
-	if (offw)
-		printf("%0*x: ", offw, 0);
-	for (i = 0; i < len; i++, d++) {
-		if (i && (i % br) == 0) {
-			if ((fl & HEXDF_NOASCII) == 0) {
-				printf("   ");
-				d -= br;
-				for (j = 0; j < br; d++, j++) {
-					if (j && (j % sp) == 0)
-						printf(" ");
-					if (PRINTABLE(*d))
-						printf("%c", (int)*d);
-					else
-						printf(".");
-				}
-			}
-			if (offw)
-				printf("\n%0*x: ", offw, i);
-			else
-				printf("\n");
-			if ((fl & HEXDF_NOCOMPRESS) == 0) {
-				first = 1;
-				while (len - i >= br) {
-					if (memcmp(d, d - br, br))
-						break;
-					d += br;
-					i += br;
-					if (first) {
-						printf("*");
-						first = 0;
-					}
-				}
-				if (len == i) {
-					printf("\n%0*x", offw, i);
-					return;
-				}
-			}
-		} else if (i && (i % sp) == 0)
-			printf(" ");
-		printf("%02x ", *d);
-	}
-	if (len && (((i - 1) % br) || i == 1)) {
-		if ((fl & HEXDF_NOASCII) == 0) {
-			i = i % br ? i % br : br;
-			ni = (br - i) % br;
-			j = (i - 1) / sp;
-			nj = (div - j - 1) % div;
-			j = 3 * ni + nj + 3;
-			printf("%*s", j, "");
-			d -= i;
-			for (j = 0; j < i; d++, j++) {
-				if (j && (j % sp) == 0)
-					printf(" ");
-				if (PRINTABLE(*d))
-					printf("%c", (int)*d);
-				else
-					printf(".");
-			}
-		}
-		printf("\n");
-	}
-}
-
-
-
 static void
 ray_dump_mbuf(struct ray_softc *sc, struct mbuf *m)
 {
-	u_int8_t *d, *ed;
-	u_int i;
-
 	printf("%s: pkt dump:", device_xname(sc->sc_dev));
-	i = 0;
 	for (; m; m = m->m_next) {
-		d = mtod(m, u_int8_t *);
-		ed = d + m->m_len;
-
-		for (; d < ed; i++, d++) {
-			if ((i % 16) == 0)
-				printf("\n\t");
-			else if ((i % 8) == 0)
-				printf("  ");
-			printf(" %02x", *d);
-		}
+		hexdump(NULL, mtod(m, void *), m->m_len);
 	}
-	if ((i - 1) % 16)
-		printf("\n");
 }
 #endif	/* RAY_DEBUG */
 

Index: src/sys/dev/usb/xhci.c
diff -u src/sys/dev/usb/xhci.c:1.78 src/sys/dev/usb/xhci.c:1.79
--- src/sys/dev/usb/xhci.c:1.78	Thu Dec  7 17:56:23 2017
+++ src/sys/dev/usb/xhci.c	Fri Dec  8 16:51:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: xhci.c,v 1.78 2017/12/07 22:56:23 christos Exp $	*/
+/*	$NetBSD: xhci.c,v 1.79 2017/12/08 21:51:07 christos Exp $	*/
 
 /*
  * Copyright (c) 2013 Jonathan A. Kollasch
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.78 2017/12/07 22:56:23 christos Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.79 2017/12/08 21:51:07 christos Exp $");
 
 #ifdef _KERNEL_OPT
 #include "opt_usb.h"
@@ -73,6 +73,11 @@ __KERNEL_RCSID(0, "$NetBSD: xhci.c,v 1.7
 #ifndef XHCI_DEBUG
 #define xhcidebug 0
 #else /* !XHCI_DEBUG */
+#define HEXDUMP(a, b, c) \
+    do { \
+	    if (xhcidebug > 0) \
+		    hexdump(a, b, c); \
+    } while (/*CONSTCOND*/0)
 static int xhcidebug = 0;
 
 SYSCTL_SETUP(sysctl_hw_xhci_setup, "sysctl hw.xhci setup")
@@ -105,6 +110,10 @@ fail:
 #endif /* !XHCI_DEBUG */
 #endif /* USB_DEBUG */
 
+#ifndef HEXDUMP
+#define HEXDUMP(a, b, c)
+#endif
+
 #define DPRINTFN(N,FMT,A,B,C,D) USBHIST_LOGN(xhcidebug,N,FMT,A,B,C,D)
 #define XHCIHIST_FUNC() USBHIST_FUNC()
 #define XHCIHIST_CALLED(name) USBHIST_CALLED(xhcidebug)
@@ -720,35 +729,6 @@ xhci_hc_reset(struct xhci_softc * const 
 }
 
 
-static void
-hexdump(const char *msg, const void *base, size_t len)
-{
-#if 0
-	size_t cnt;
-	const uint32_t *p;
-	extern paddr_t vtophys(vaddr_t);
-
-	p = base;
-	cnt = 0;
-
-	printf("*** %s (%zu bytes @ %p %p)\n", msg, len, base,
-	    (void *)vtophys((vaddr_t)base));
-
-	while (cnt < len) {
-		if (cnt % 16 == 0)
-			printf("%p: ", p);
-		else if (cnt % 8 == 0)
-			printf(" |");
-		printf(" %08x", *p++);
-		cnt += 4;
-		if (cnt % 16 == 0)
-			printf("\n");
-	}
-	if (cnt % 16 != 0)
-		printf("\n");
-#endif
-}
-
 /* 7.2 xHCI Support Protocol Capability */
 static void
 xhci_id_protocols(struct xhci_softc *sc, bus_size_t ecp)
@@ -1184,10 +1164,8 @@ xhci_init(struct xhci_softc *sc)
 	xhci_op_write_8(sc, XHCI_CRCR, xhci_ring_trbp(&sc->sc_cr, 0) |
 	    sc->sc_cr.xr_cs);
 
-#if 0
-	hexdump("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
+	HEXDUMP("eventst", KERNADDR(&sc->sc_eventst_dma, 0),
 	    XHCI_ERSTE_SIZE * XHCI_EVENT_RING_SEGMENTS);
-#endif
 
 	if ((sc->sc_quirks & XHCI_DEFERRED_START) == 0)
 		xhci_start(sc);
@@ -1363,7 +1341,9 @@ xhci_configure_endpoint(struct usbd_pipe
 {
 	struct xhci_softc * const sc = XHCI_PIPE2SC(pipe);
 	struct xhci_slot * const xs = pipe->up_dev->ud_hcpriv;
+#ifdef XHCI_DEBUG
 	const u_int dci = xhci_ep_get_dci(pipe->up_endpoint->ue_edesc);
+#endif
 	struct xhci_trb trb;
 	usbd_status err;
 
@@ -1379,9 +1359,9 @@ xhci_configure_endpoint(struct usbd_pipe
 	/* set up context */
 	xhci_setup_ctx(pipe);
 
-	hexdump("input control context", xhci_slot_get_icv(sc, xs, 0),
+	HEXDUMP("input control context", xhci_slot_get_icv(sc, xs, 0),
 	    sc->sc_ctxsz * 1);
-	hexdump("input endpoint context", xhci_slot_get_icv(sc, xs,
+	HEXDUMP("input endpoint context", xhci_slot_get_icv(sc, xs,
 	    xhci_dci_to_ici(dci)), sc->sc_ctxsz * 1);
 
 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
@@ -1392,7 +1372,7 @@ xhci_configure_endpoint(struct usbd_pipe
 	err = xhci_do_command(sc, &trb, USBD_DEFAULT_TIMEOUT);
 
 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
-	hexdump("output context", xhci_slot_get_dcv(sc, xs, dci),
+	HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, dci),
 	    sc->sc_ctxsz * 1);
 
 	return err;
@@ -2363,7 +2343,7 @@ xhci_new_device(device_t parent, struct 
 		usbd_delay_ms(dev, USB_SET_ADDRESS_SETTLE);
 
 		cp = xhci_slot_get_dcv(sc, xs, XHCI_DCI_SLOT);
-		//hexdump("slot context", cp, sc->sc_ctxsz);
+		HEXDUMP("slot context", cp, sc->sc_ctxsz);
 		uint8_t addr = XHCI_SCTX_3_DEV_ADDR_GET(le32toh(cp[3]));
 		DPRINTFN(4, "device address %ju", addr, 0, 0, 0);
 		/*
@@ -2821,7 +2801,7 @@ xhci_update_ep0_mps(struct xhci_softc * 
 
 	/* sync input contexts before they are read from memory */
 	usb_syncmem(&xs->xs_ic_dma, 0, sc->sc_pgsz, BUS_DMASYNC_PREWRITE);
-	hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
+	HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
 	    sc->sc_ctxsz * 4);
 
 	trb.trb_0 = xhci_slot_get_icp(sc, xs, 0);
@@ -2942,7 +2922,7 @@ xhci_set_address(struct usbd_device *dev
 
 	xhci_setup_ctx(dev->ud_pipe0);
 
-	hexdump("input context", xhci_slot_get_icv(sc, xs, 0),
+	HEXDUMP("input context", xhci_slot_get_icv(sc, xs, 0),
 	    sc->sc_ctxsz * 3);
 
 	xhci_set_dcba(sc, DMAADDR(&xs->xs_dc_dma, 0), slot);
@@ -2950,7 +2930,7 @@ xhci_set_address(struct usbd_device *dev
 	err = xhci_address_device(sc, xhci_slot_get_icp(sc, xs, 0), slot, bsr);
 
 	usb_syncmem(&xs->xs_dc_dma, 0, sc->sc_pgsz, BUS_DMASYNC_POSTREAD);
-	hexdump("output context", xhci_slot_get_dcv(sc, xs, 0),
+	HEXDUMP("output context", xhci_slot_get_dcv(sc, xs, 0),
 	    sc->sc_ctxsz * 2);
 
 	return err;

Index: src/sys/lib/libkern/Makefile.libkern
diff -u src/sys/lib/libkern/Makefile.libkern:1.40 src/sys/lib/libkern/Makefile.libkern:1.41
--- src/sys/lib/libkern/Makefile.libkern:1.40	Thu Nov 30 00:47:24 2017
+++ src/sys/lib/libkern/Makefile.libkern	Fri Dec  8 16:51:07 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile.libkern,v 1.40 2017/11/30 05:47:24 riastradh Exp $
+#	$NetBSD: Makefile.libkern,v 1.41 2017/12/08 21:51:07 christos Exp $
 
 # 
 # Variable definitions for libkern.  
@@ -89,6 +89,8 @@ SRCS+=	xlat_mbr_fstype.c
 
 SRCS+=	heapsort.c ptree.c rb.c
 
+SRCS+=	hexdump.c
+
 # for crypto
 SRCS+=	explicit_memset.c consttime_memequal.c
 

Index: src/sys/lib/libkern/libkern.h
diff -u src/sys/lib/libkern/libkern.h:1.124 src/sys/lib/libkern/libkern.h:1.125
--- src/sys/lib/libkern/libkern.h:1.124	Thu Jul  7 02:55:43 2016
+++ src/sys/lib/libkern/libkern.h	Fri Dec  8 16:51:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: libkern.h,v 1.124 2016/07/07 06:55:43 msaitoh Exp $	*/
+/*	$NetBSD: libkern.h,v 1.125 2017/12/08 21:51:07 christos Exp $	*/
 
 /*-
  * Copyright (c) 1992, 1993
@@ -448,6 +448,7 @@ intmax_t strtoi(const char * __restrict,
     intmax_t, int *);
 uintmax_t strtou(const char * __restrict, char ** __restrict, int, uintmax_t,
     uintmax_t, int *);
+void	 hexdump(const char *, const void *, size_t);
 
 int	 snprintb(char *, size_t, const char *, uint64_t);
 int	 snprintb_m(char *, size_t, const char *, uint64_t, size_t);

Added files:

Index: src/sys/lib/libkern/hexdump.c
diff -u /dev/null src/sys/lib/libkern/hexdump.c:1.1
--- /dev/null	Fri Dec  8 16:51:07 2017
+++ src/sys/lib/libkern/hexdump.c	Fri Dec  8 16:51:07 2017
@@ -0,0 +1,69 @@
+/*-
+ * Copyright (c) 2017 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Christos Zoulas.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: hexdump.c,v 1.1 2017/12/08 21:51:07 christos Exp $");
+
+#include <lib/libkern/libkern.h>
+#include <sys/systm.h>
+
+void
+hexdump(const char *msg, const void *ptr, size_t len)
+{
+	size_t i;
+	const unsigned char *u = ptr;
+
+	if (msg)
+		printf("%s: %zu bytes @ %p\n", msg, len, ptr);
+        for (i = 0; i < len; ) {
+                printf("%02x ", u[i++]);
+                if (!(i & 0x7))
+                        printf(" ");
+                if (!(i & 0xf)) {
+                        printf("| ");
+                        for (size_t j = i - 16; j < i; j++) {
+				unsigned char c = u[j];
+                                printf("%c", isprint(c) ? c : '.');
+			}
+                        printf("\n");
+                }
+        }
+	if ((i = (len & 0xf)) != 0) {
+                for (size_t j = 16 - i; j > 0; j--) {
+                        printf("   ");
+                        if (!(j & 0x7) && i != 8)
+                                printf(" ");
+                }
+                printf(" | ");
+                for (size_t j = len - i; j < len; j++) {
+			unsigned char c = u[j];
+			printf("%c", isprint(c) ? c : '.');
+		}
+		printf("\n");
+        }
+}

Reply via email to