Commit 9cbe1f58 authored by Quentin Monnet's avatar Quentin Monnet Committed by David S. Miller

tools: bpftool: add pointer to file argument to print_hex()

Make print_hex() able to print to any file instead of standard output
only, and rename it to fprint_hex(). The function can now be called with
the info() macro, for example, without splitting the output between
standard and error outputs.
Signed-off-by: default avatarQuentin Monnet <quentin.monnet@netronome.com>
Signed-off-by: default avatarJakub Kicinski <jakub.kicinski@netronome.com>
Acked-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent f3ae608e
...@@ -100,7 +100,7 @@ bool is_prefix(const char *pfx, const char *str) ...@@ -100,7 +100,7 @@ bool is_prefix(const char *pfx, const char *str)
return !memcmp(str, pfx, strlen(pfx)); return !memcmp(str, pfx, strlen(pfx));
} }
void print_hex(void *arg, unsigned int n, const char *sep) void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep)
{ {
unsigned char *data = arg; unsigned char *data = arg;
unsigned int i; unsigned int i;
...@@ -111,13 +111,13 @@ void print_hex(void *arg, unsigned int n, const char *sep) ...@@ -111,13 +111,13 @@ void print_hex(void *arg, unsigned int n, const char *sep)
if (!i) if (!i)
/* nothing */; /* nothing */;
else if (!(i % 16)) else if (!(i % 16))
printf("\n"); fprintf(f, "\n");
else if (!(i % 8)) else if (!(i % 8))
printf(" "); fprintf(f, " ");
else else
pfx = sep; pfx = sep;
printf("%s%02hhx", i ? pfx : "", data[i]); fprintf(f, "%s%02hhx", i ? pfx : "", data[i]);
} }
} }
......
...@@ -67,7 +67,7 @@ enum bpf_obj_type { ...@@ -67,7 +67,7 @@ enum bpf_obj_type {
extern const char *bin_name; extern const char *bin_name;
bool is_prefix(const char *pfx, const char *str); bool is_prefix(const char *pfx, const char *str);
void print_hex(void *arg, unsigned int n, const char *sep); void fprint_hex(FILE *f, void *arg, unsigned int n, const char *sep);
void usage(void) __attribute__((noreturn)); void usage(void) __attribute__((noreturn));
struct cmd { struct cmd {
......
...@@ -216,12 +216,12 @@ static void print_entry(struct bpf_map_info *info, unsigned char *key, ...@@ -216,12 +216,12 @@ static void print_entry(struct bpf_map_info *info, unsigned char *key,
!break_names; !break_names;
printf("key:%c", break_names ? '\n' : ' '); printf("key:%c", break_names ? '\n' : ' ');
print_hex(key, info->key_size, " "); fprint_hex(stdout, key, info->key_size, " ");
printf(single_line ? " " : "\n"); printf(single_line ? " " : "\n");
printf("value:%c", break_names ? '\n' : ' '); printf("value:%c", break_names ? '\n' : ' ');
print_hex(value, info->value_size, " "); fprint_hex(stdout, value, info->value_size, " ");
printf("\n"); printf("\n");
} else { } else {
...@@ -230,13 +230,13 @@ static void print_entry(struct bpf_map_info *info, unsigned char *key, ...@@ -230,13 +230,13 @@ static void print_entry(struct bpf_map_info *info, unsigned char *key,
n = get_possible_cpus(); n = get_possible_cpus();
printf("key:\n"); printf("key:\n");
print_hex(key, info->key_size, " "); fprint_hex(stdout, key, info->key_size, " ");
printf("\n"); printf("\n");
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
printf("value (CPU %02d):%c", printf("value (CPU %02d):%c",
i, info->value_size > 16 ? '\n' : ' '); i, info->value_size > 16 ? '\n' : ' ');
print_hex(value + i * info->value_size, fprint_hex(stdout, value + i * info->value_size,
info->value_size, " "); info->value_size, " ");
printf("\n"); printf("\n");
} }
} }
...@@ -492,8 +492,8 @@ static int do_dump(int argc, char **argv) ...@@ -492,8 +492,8 @@ static int do_dump(int argc, char **argv)
print_entry(&info, key, value); print_entry(&info, key, value);
} else { } else {
info("can't lookup element with key: "); info("can't lookup element with key: ");
print_hex(key, info.key_size, " "); fprint_hex(stderr, key, info.key_size, " ");
printf("\n"); fprintf(stderr, "\n");
} }
prev_key = key; prev_key = key;
...@@ -587,7 +587,7 @@ static int do_lookup(int argc, char **argv) ...@@ -587,7 +587,7 @@ static int do_lookup(int argc, char **argv)
print_entry(&info, key, value); print_entry(&info, key, value);
} else if (errno == ENOENT) { } else if (errno == ENOENT) {
printf("key:\n"); printf("key:\n");
print_hex(key, info.key_size, " "); fprint_hex(stdout, key, info.key_size, " ");
printf("\n\nNot found\n"); printf("\n\nNot found\n");
} else { } else {
err("lookup failed: %s\n", strerror(errno)); err("lookup failed: %s\n", strerror(errno));
...@@ -642,14 +642,14 @@ static int do_getnext(int argc, char **argv) ...@@ -642,14 +642,14 @@ static int do_getnext(int argc, char **argv)
if (key) { if (key) {
printf("key:\n"); printf("key:\n");
print_hex(key, info.key_size, " "); fprint_hex(stdout, key, info.key_size, " ");
printf("\n"); printf("\n");
} else { } else {
printf("key: None\n"); printf("key: None\n");
} }
printf("next key:\n"); printf("next key:\n");
print_hex(nextkey, info.key_size, " "); fprint_hex(stdout, nextkey, info.key_size, " ");
printf("\n"); printf("\n");
exit_free: exit_free:
......
...@@ -224,7 +224,7 @@ static int show_prog(int fd) ...@@ -224,7 +224,7 @@ static int show_prog(int fd)
printf("name %s ", info.name); printf("name %s ", info.name);
printf("tag "); printf("tag ");
print_hex(info.tag, BPF_TAG_SIZE, ""); fprint_hex(stdout, info.tag, BPF_TAG_SIZE, "");
printf("\n"); printf("\n");
if (info.load_time) { if (info.load_time) {
...@@ -319,7 +319,7 @@ static void dump_xlated(void *buf, unsigned int len, bool opcodes) ...@@ -319,7 +319,7 @@ static void dump_xlated(void *buf, unsigned int len, bool opcodes)
if (opcodes) { if (opcodes) {
printf(" "); printf(" ");
print_hex(insn + i, 8, " "); fprint_hex(stdout, insn + i, 8, " ");
printf("\n"); printf("\n");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment