eb72f33e402cfde432adf37392c0480f44c20248
[dpdk.git] / lib / librte_eal / common / eal_common_hexdump.c
1 /*-
2  *   BSD LICENSE
3  * 
4  *   Copyright(c) 2010-2013 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  */
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <rte_hexdump.h>
39 #include <rte_string_fns.h>
40
41 #define LINE_LEN 128
42
43 /**************************************************************************//**
44 *
45 * rte_hexdump - Dump out memory in a special hex dump format.
46 *
47 * DESCRIPTION
48 * Dump out the message buffer in a special hex dump output format with characters
49 * printed for each line of 16 hex values.
50 *
51 * RETURNS: N/A
52 *
53 * SEE ALSO:
54 */
55
56 void
57 rte_hexdump(const char * title, const void * buf, unsigned int len)
58 {
59     unsigned int i, out, ofs;
60     const unsigned char *data = buf;
61     char line[LINE_LEN];    /* space needed 8+16*3+3+16 == 75 */
62
63     printf("%s at [%p], len=%u\n", (title)? title  : "  Dump data", data, len);
64     ofs = 0;
65     while (ofs < len) {
66         /* format the line in the buffer, then use printf to output to screen */
67         out = rte_snprintf(line, LINE_LEN, "%08X:", ofs);
68         for (i = 0; ((ofs + i) < len) && (i < 16); i++)
69             out += rte_snprintf(line+out, LINE_LEN - out, " %02X", (data[ofs+i] & 0xff));
70         for(; i <= 16; i++)
71             out += rte_snprintf(line+out, LINE_LEN - out, " | ");
72         for(i = 0; (ofs < len) && (i < 16); i++, ofs++) {
73             unsigned char c = data[ofs];
74             if ( (c < ' ') || (c > '~'))
75                 c = '.';
76             out += rte_snprintf(line+out, LINE_LEN - out, "%c", c);
77         }
78         printf("%s\n", line);
79     }
80     fflush(stdout);
81 }
82
83 /**************************************************************************//**
84 *
85 * rte_memdump - Dump out memory in hex bytes with colons.
86 *
87 * DESCRIPTION
88 * Dump out the message buffer in hex bytes with colons xx:xx:xx:xx:...
89 *
90 * RETURNS: N/A
91 *
92 * SEE ALSO:
93 */
94
95 void
96 rte_memdump(const char * title, const void * buf, unsigned int len)
97 {
98     unsigned int i, out;
99     const unsigned char *data = buf;
100     char line[LINE_LEN];
101
102     if ( title )
103         printf("%s: ", title);
104
105     line[0] = '\0';
106     for (i = 0, out = 0; i < len; i++) {
107         // Make sure we do not overrun the line buffer length.
108                 if ( out >= (LINE_LEN - 4) ) {
109                         printf("%s", line);
110                         out = 0;
111                         line[out] = '\0';
112                 }
113                 out += rte_snprintf(line+out, LINE_LEN - out, "%02x%s",
114                                 (data[i] & 0xff), ((i+1) < len)? ":" : "");
115     }
116     if ( out > 0 )
117         printf("%s", line);
118         printf("\n");
119
120     fflush(stdout);
121 }
122