4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
37 #include <rte_hexdump.h>
38 #include <rte_string_fns.h>
42 /**************************************************************************//**
44 * rte_hexdump - Dump out memory in a special hex dump format.
47 * Dump out the message buffer in a special hex dump output format with characters
48 * printed for each line of 16 hex values.
56 rte_hexdump(FILE *f, const char * title, const void * buf, unsigned int len)
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 */
62 fprintf(f, "%s at [%p], len=%u\n", (title)? title : " Dump data", data, len);
65 /* format the line in the buffer, then use printf to output to screen */
66 out = snprintf(line, LINE_LEN, "%08X:", ofs);
67 for (i = 0; ((ofs + i) < len) && (i < 16); i++)
68 out += snprintf(line+out, LINE_LEN - out, " %02X", (data[ofs+i] & 0xff));
70 out += 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 > '~'))
75 out += snprintf(line+out, LINE_LEN - out, "%c", c);
77 fprintf(f, "%s\n", line);
82 /**************************************************************************//**
84 * rte_memdump - Dump out memory in hex bytes with colons.
87 * Dump out the message buffer in hex bytes with colons xx:xx:xx:xx:...
95 rte_memdump(FILE *f, const char * title, const void * buf, unsigned int len)
98 const unsigned char *data = buf;
102 fprintf(f, "%s: ", title);
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 fprintf(f, "%s", line);
112 out += snprintf(line+out, LINE_LEN - out, "%02x%s",
113 (data[i] & 0xff), ((i+1) < len)? ":" : "");
116 fprintf(f, "%s", line);