e8348a00501607442b881ba3b670447730c051a1
[dpdk.git] / lib / librte_eal / common / eal_common_hexdump.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  * 
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  * 
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  * 
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <errno.h>
36 #include <stdint.h>
37 #include <rte_hexdump.h>
38 #include <rte_string_fns.h>
39
40 #define LINE_LEN 128
41
42 /**************************************************************************//**
43 *
44 * rte_hexdump - Dump out memory in a special hex dump format.
45 *
46 * DESCRIPTION
47 * Dump out the message buffer in a special hex dump output format with characters
48 * printed for each line of 16 hex values.
49 *
50 * RETURNS: N/A
51 *
52 * SEE ALSO:
53 */
54
55 void
56 rte_hexdump(const char * title, const void * buf, unsigned int len)
57 {
58     unsigned int i, out, ofs;
59     const unsigned char *data = buf;
60     char line[LINE_LEN];    /* space needed 8+16*3+3+16 == 75 */
61
62     printf("%s at [%p], len=%u\n", (title)? title  : "  Dump data", data, len);
63     ofs = 0;
64     while (ofs < len) {
65         /* format the line in the buffer, then use printf to output to screen */
66         out = rte_snprintf(line, LINE_LEN, "%08X:", ofs);
67         for (i = 0; ((ofs + i) < len) && (i < 16); i++)
68             out += rte_snprintf(line+out, LINE_LEN - out, " %02X", (data[ofs+i] & 0xff));
69         for(; i <= 16; i++)
70             out += rte_snprintf(line+out, LINE_LEN - out, " | ");
71         for(i = 0; (ofs < len) && (i < 16); i++, ofs++) {
72             unsigned char c = data[ofs];
73             if ( (c < ' ') || (c > '~'))
74                 c = '.';
75             out += rte_snprintf(line+out, LINE_LEN - out, "%c", c);
76         }
77         printf("%s\n", line);
78     }
79     fflush(stdout);
80 }
81
82 /**************************************************************************//**
83 *
84 * rte_memdump - Dump out memory in hex bytes with colons.
85 *
86 * DESCRIPTION
87 * Dump out the message buffer in hex bytes with colons xx:xx:xx:xx:...
88 *
89 * RETURNS: N/A
90 *
91 * SEE ALSO:
92 */
93
94 void
95 rte_memdump(const char * title, const void * buf, unsigned int len)
96 {
97     unsigned int i, out;
98     const unsigned char *data = buf;
99     char line[LINE_LEN];
100
101     if ( title )
102         printf("%s: ", title);
103
104     line[0] = '\0';
105     for (i = 0, out = 0; i < len; i++) {
106         // Make sure we do not overrun the line buffer length.
107                 if ( out >= (LINE_LEN - 4) ) {
108                         printf("%s", line);
109                         out = 0;
110                         line[out] = '\0';
111                 }
112                 out += rte_snprintf(line+out, LINE_LEN - out, "%02x%s",
113                                 (data[i] & 0xff), ((i+1) < len)? ":" : "");
114     }
115     if ( out > 0 )
116         printf("%s", line);
117         printf("\n");
118
119     fflush(stdout);
120 }
121