xref: /netbsd-src/usr.sbin/perfused/debug.c (revision 109f5fba24c31581d250ba89bf579014e97df43c)
1*109f5fbaSchristos /*  $NetBSD: debug.c,v 1.5 2012/01/30 22:49:03 christos Exp $ */
2a18d4c5aSmanu 
3a18d4c5aSmanu /*-
4a18d4c5aSmanu  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
5a18d4c5aSmanu  *
6a18d4c5aSmanu  *  Redistribution and use in source and binary forms, with or without
7a18d4c5aSmanu  *  modification, are permitted provided that the following conditions
8a18d4c5aSmanu  *  are met:
9a18d4c5aSmanu  *  1. Redistributions of source code must retain the above copyright
10a18d4c5aSmanu  *     notice, this list of conditions and the following disclaimer.
11a18d4c5aSmanu  *  2. Redistributions in binary form must reproduce the above copyright
12a18d4c5aSmanu  *     notice, this list of conditions and the following disclaimer in the
13a18d4c5aSmanu  *     documentation and/or other materials provided with the distribution.
14a18d4c5aSmanu  *
15a18d4c5aSmanu  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16a18d4c5aSmanu  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17a18d4c5aSmanu  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18a18d4c5aSmanu  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19a18d4c5aSmanu  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20a18d4c5aSmanu  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21a18d4c5aSmanu  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22a18d4c5aSmanu  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23a18d4c5aSmanu  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24a18d4c5aSmanu  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25a18d4c5aSmanu  *  POSSIBILITY OF SUCH DAMAGE.
26a18d4c5aSmanu  */
27a18d4c5aSmanu #include <stdio.h>
28a18d4c5aSmanu #include <errno.h>
29a18d4c5aSmanu #include <string.h>
30516b1c90Smanu #include <syslog.h>
31a18d4c5aSmanu #include <ctype.h>
32e9a8a6acSmanu #include <sys/socket.h>
33a18d4c5aSmanu 
34a18d4c5aSmanu #include "perfused.h"
35a18d4c5aSmanu 
36a18d4c5aSmanu #ifdef PERFUSE_DEBUG
37a18d4c5aSmanu void
perfused_hexdump(const char * addr,size_t len)38*109f5fbaSchristos perfused_hexdump(const char *addr, size_t len)
39a18d4c5aSmanu {
40a18d4c5aSmanu 	unsigned int i, j, k;
41a18d4c5aSmanu 
42a18d4c5aSmanu 	for (i = 0; i < len; i += 16) {
43a18d4c5aSmanu 		DPRINTF("%p  ", &addr[i]);
44a18d4c5aSmanu 		for (j = 0; j < 16; j += 4) {
45a18d4c5aSmanu 			for (k = 0; k < 4; k++) {
46a18d4c5aSmanu 				if (i + j + k < len) {
47a18d4c5aSmanu 					DPRINTF("%02x ",
48a18d4c5aSmanu 					       *(addr + i + j + k) & 0xff);
49a18d4c5aSmanu 				} else {
50a18d4c5aSmanu 					DPRINTF("   ");
51a18d4c5aSmanu 				}
52a18d4c5aSmanu 			}
53a18d4c5aSmanu 		}
54a18d4c5aSmanu 
55a18d4c5aSmanu 		DPRINTF("  ");
56a18d4c5aSmanu 		for (j = 0; j < 16; j++) {
57a18d4c5aSmanu 			char c;
58a18d4c5aSmanu 
59a18d4c5aSmanu 			if (i + j < len) {
60a18d4c5aSmanu 				c = *(addr + i + j);
61a18d4c5aSmanu 				DPRINTF("%c", isprint((int)c) ? c : '.');
62a18d4c5aSmanu 			} else {
63a18d4c5aSmanu 				DPRINTF(" ");
64a18d4c5aSmanu 			}
65a18d4c5aSmanu 		}
66a18d4c5aSmanu 		DPRINTF("\n");
67a18d4c5aSmanu 	}
68a18d4c5aSmanu 
69a18d4c5aSmanu 	return;
70a18d4c5aSmanu }
71a18d4c5aSmanu 
72a18d4c5aSmanu 
73a18d4c5aSmanu 
74a18d4c5aSmanu #endif /* PERFUSE_DEBUG */
75