Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2010-2014 Intel Corporation
3 : : */
4 : : #include <stdio.h>
5 : : #include <eal_export.h>
6 : : #include <rte_hexdump.h>
7 : : #include <rte_string_fns.h>
8 : :
9 : : #define LINE_LEN 128
10 : :
11 : : RTE_EXPORT_SYMBOL(rte_hexdump)
12 : : void
13 : 1047 : rte_hexdump(FILE *f, const char *title, const void *buf, unsigned int len)
14 : : {
15 : : unsigned int i, out, ofs;
16 : : const unsigned char *data = buf;
17 : : char line[LINE_LEN]; /* space needed 8+16*3+3+16 == 75 */
18 : :
19 [ + + ]: 1047 : fprintf(f, "%s at [%p], len=%u\n",
20 : : title != NULL ? title : " Dump data", data, len);
21 : : ofs = 0;
22 [ + + ]: 133547 : while (ofs < len) {
23 : : /* format the line in the buffer */
24 : 132500 : out = snprintf(line, LINE_LEN, "%08X:", ofs);
25 [ + + ]: 2252500 : for (i = 0; i < 16; i++) {
26 [ + + ]: 2120000 : if (ofs + i < len)
27 : 2117491 : snprintf(line + out, LINE_LEN - out,
28 : 2117491 : " %02X", (data[ofs + i] & 0xff));
29 : : else
30 : 2509 : strcpy(line + out, " ");
31 : 2120000 : out += 3;
32 : : }
33 : :
34 : :
35 [ + + ]: 265000 : for (; i <= 16; i++)
36 : 132500 : out += snprintf(line + out, LINE_LEN - out, " | ");
37 : :
38 [ + + ]: 2249991 : for (i = 0; ofs < len && i < 16; i++, ofs++) {
39 : 2117491 : unsigned char c = data[ofs];
40 : :
41 [ + + ]: 2117491 : if (c < ' ' || c > '~')
42 : : c = '.';
43 : 2117491 : out += snprintf(line + out, LINE_LEN - out, "%c", c);
44 : : }
45 : : fprintf(f, "%s\n", line);
46 : : }
47 : 1047 : fflush(f);
48 : 1047 : }
49 : :
50 : : RTE_EXPORT_SYMBOL(rte_memdump)
51 : : void
52 : 121 : rte_memdump(FILE *f, const char *title, const void *buf, unsigned int len)
53 : : {
54 : : unsigned int i, out;
55 : : const unsigned char *data = buf;
56 : : char line[LINE_LEN];
57 : :
58 [ + + ]: 121 : if (title)
59 : : fprintf(f, "%s: ", title);
60 : :
61 : 121 : line[0] = '\0';
62 [ + + ]: 1574 : for (i = 0, out = 0; i < len; i++) {
63 : : /* Make sure we do not overrun the line buffer length. */
64 [ - + ]: 1453 : if (out >= LINE_LEN - 4) {
65 : : fprintf(f, "%s", line);
66 : : out = 0;
67 : 0 : line[out] = '\0';
68 : : }
69 : 2906 : out += snprintf(line + out, LINE_LEN - out, "%02x%s",
70 [ + + ]: 1574 : (data[i] & 0xff), ((i + 1) < len) ? ":" : "");
71 : : }
72 [ + - ]: 121 : if (out > 0)
73 : : fprintf(f, "%s", line);
74 : : fprintf(f, "\n");
75 : :
76 : 121 : fflush(f);
77 : 121 : }
|