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