New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / common / eal_common_hexdump.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <errno.h>
7 #include <stdint.h>
8 #include <rte_hexdump.h>
9 #include <rte_string_fns.h>
10
11 #define LINE_LEN 128
12
13 /**************************************************************************//**
14 *
15 * rte_hexdump - Dump out memory in a special hex dump format.
16 *
17 * DESCRIPTION
18 * Dump out the message buffer in a special hex dump output format with characters
19 * printed for each line of 16 hex values.
20 *
21 * RETURNS: N/A
22 *
23 * SEE ALSO:
24 */
25
26 void
27 rte_hexdump(FILE *f, const char * title, const void * buf, unsigned int len)
28 {
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 */
32
33     fprintf(f, "%s at [%p], len=%u\n", (title)? title  : "  Dump data", data, len);
34     ofs = 0;
35     while (ofs < 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));
40         for(; i <= 16; i++)
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 > '~'))
45                 c = '.';
46             out += snprintf(line+out, LINE_LEN - out, "%c", c);
47         }
48         fprintf(f, "%s\n", line);
49     }
50     fflush(f);
51 }
52
53 /**************************************************************************//**
54 *
55 * rte_memdump - Dump out memory in hex bytes with colons.
56 *
57 * DESCRIPTION
58 * Dump out the message buffer in hex bytes with colons xx:xx:xx:xx:...
59 *
60 * RETURNS: N/A
61 *
62 * SEE ALSO:
63 */
64
65 void
66 rte_memdump(FILE *f, const char * title, const void * buf, unsigned int len)
67 {
68     unsigned int i, out;
69     const unsigned char *data = buf;
70     char line[LINE_LEN];
71
72     if ( title )
73         fprintf(f, "%s: ", title);
74
75     line[0] = '\0';
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);
80                         out = 0;
81                         line[out] = '\0';
82                 }
83                 out += snprintf(line+out, LINE_LEN - out, "%02x%s",
84                                 (data[i] & 0xff), ((i+1) < len)? ":" : "");
85     }
86     if ( out > 0 )
87         fprintf(f, "%s", line);
88     fprintf(f, "\n");
89
90     fflush(f);
91 }