1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
8 #include <rte_hexdump.h>
9 #include <rte_string_fns.h>
13 /**************************************************************************//**
15 * rte_hexdump - Dump out memory in a special hex dump format.
18 * Dump out the message buffer in a special hex dump output format with characters
19 * printed for each line of 16 hex values.
27 rte_hexdump(FILE *f, const char * title, const void * buf, unsigned int len)
29 unsigned int i, out, ofs;
30 const unsigned char *data = buf;
31 char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
33 fprintf(f, "%s at [%p], len=%u\n", (title)? title : " Dump data", data, len);
36 /* format the line in the buffer, then use printf to output to screen */
37 out = snprintf(line, LINE_LEN, "%08X:", ofs);
38 for (i = 0; ((ofs + i) < len) && (i < 16); i++)
39 out += snprintf(line+out, LINE_LEN - out, " %02X", (data[ofs+i] & 0xff));
41 out += snprintf(line+out, LINE_LEN - out, " | ");
42 for(i = 0; (ofs < len) && (i < 16); i++, ofs++) {
43 unsigned char c = data[ofs];
44 if ( (c < ' ') || (c > '~'))
46 out += snprintf(line+out, LINE_LEN - out, "%c", c);
48 fprintf(f, "%s\n", line);
53 /**************************************************************************//**
55 * rte_memdump - Dump out memory in hex bytes with colons.
58 * Dump out the message buffer in hex bytes with colons xx:xx:xx:xx:...
66 rte_memdump(FILE *f, const char * title, const void * buf, unsigned int len)
69 const unsigned char *data = buf;
73 fprintf(f, "%s: ", title);
76 for (i = 0, out = 0; i < len; i++) {
77 // Make sure we do not overrun the line buffer length.
78 if ( out >= (LINE_LEN - 4) ) {
79 fprintf(f, "%s", line);
83 out += snprintf(line+out, LINE_LEN - out, "%02x%s",
84 (data[i] & 0xff), ((i+1) < len)? ":" : "");
87 fprintf(f, "%s", line);