|
| 1 | +// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) |
| 2 | + |
| 3 | +#include "main.h" |
| 4 | + |
| 5 | +struct tnum { |
| 6 | + __u64 value; |
| 7 | + __u64 mask; |
| 8 | +}; |
| 9 | + |
| 10 | +struct bpf_reg_oracle_state { |
| 11 | + bool scalar; |
| 12 | + bool ptr_not_null; |
| 13 | + |
| 14 | + struct tnum var_off; |
| 15 | + __s64 smin_value; |
| 16 | + __s64 smax_value; |
| 17 | + __u64 umin_value; |
| 18 | + __u64 umax_value; |
| 19 | + __s32 s32_min_value; |
| 20 | + __s32 s32_max_value; |
| 21 | + __u32 u32_min_value; |
| 22 | + __u32 u32_max_value; |
| 23 | +}; |
| 24 | + |
| 25 | +struct bpf_oracle_state { |
| 26 | + struct bpf_reg_oracle_state regs[MAX_BPF_REG - 1]; |
| 27 | +}; |
| 28 | + |
| 29 | +static void print_register_state(int i, struct bpf_reg_oracle_state *reg) |
| 30 | +{ |
| 31 | + if (!reg->scalar && !reg->ptr_not_null) |
| 32 | + return; |
| 33 | + |
| 34 | + printf("R%d=", i); |
| 35 | + if (reg->scalar) { |
| 36 | + printf("scalar(u64=[%llu; %llu], s64=[%lld; %lld], u32=[%u; %u], s32=[%d; %d]", |
| 37 | + reg->umin_value, reg->umax_value, reg->smin_value, reg->smax_value, |
| 38 | + reg->u32_min_value, reg->u32_max_value, reg->s32_min_value, |
| 39 | + reg->s32_max_value); |
| 40 | + printf(", var_off=(%#llx; %#llx)", reg->var_off.value, reg->var_off.mask); |
| 41 | + } else if (reg->ptr_not_null) { |
| 42 | + printf("ptr"); |
| 43 | + } else { |
| 44 | + printf("unknown"); |
| 45 | + } |
| 46 | + printf("\n"); |
| 47 | +} |
| 48 | + |
| 49 | +static int |
| 50 | +oracle_map_dump(int fd, struct bpf_map_info *info, bool show_header) |
| 51 | +{ |
| 52 | + struct bpf_oracle_state value = {}; |
| 53 | + unsigned int num_elems = 0; |
| 54 | + __u32 key, *prev_key = NULL; |
| 55 | + int err, i; |
| 56 | + |
| 57 | + while (true) { |
| 58 | + err = bpf_map_get_next_key(fd, prev_key, &key); |
| 59 | + if (err) { |
| 60 | + if (errno == ENOENT) |
| 61 | + err = 0; |
| 62 | + break; |
| 63 | + } |
| 64 | + if (bpf_map_lookup_elem(fd, &key, &value)) { |
| 65 | + printf("<no entry>"); |
| 66 | + continue; |
| 67 | + } |
| 68 | + printf("State %u:\n", key); |
| 69 | + for (i = 0; i < MAX_BPF_REG - 1; i++) |
| 70 | + print_register_state(i, &value.regs[i]); |
| 71 | + printf("\n"); |
| 72 | + num_elems++; |
| 73 | + prev_key = &key; |
| 74 | + } |
| 75 | + |
| 76 | + printf("Found %u state%s\n", num_elems, |
| 77 | + num_elems != 1 ? "s" : ""); |
| 78 | + |
| 79 | + close(fd); |
| 80 | + return err; |
| 81 | +} |
| 82 | + |
| 83 | +static int do_dump(int argc, char **argv) |
| 84 | +{ |
| 85 | + struct bpf_map_info info = {}; |
| 86 | + __u32 len = sizeof(info); |
| 87 | + int nb_fds, i, err; |
| 88 | + int *fds = NULL; |
| 89 | + |
| 90 | + fds = malloc(sizeof(int)); |
| 91 | + if (!fds) { |
| 92 | + p_err("mem alloc failed"); |
| 93 | + return -1; |
| 94 | + } |
| 95 | + nb_fds = map_parse_fds(&argc, &argv, &fds, BPF_F_RDONLY); |
| 96 | + if (nb_fds < 1) |
| 97 | + goto exit_free; |
| 98 | + |
| 99 | + for (i = 0; i < nb_fds; i++) { |
| 100 | + if (bpf_map_get_info_by_fd(fds[i], &info, &len)) { |
| 101 | + p_err("can't get map info: %s", strerror(errno)); |
| 102 | + break; |
| 103 | + } |
| 104 | + if (info.type != BPF_MAP_TYPE_ARRAY || info.key_size != sizeof(__u32) || |
| 105 | + info.value_size != sizeof(struct bpf_oracle_state)) { |
| 106 | + p_err("not an oracle map"); |
| 107 | + break; |
| 108 | + } |
| 109 | + err = oracle_map_dump(fds[i], &info, nb_fds > 1); |
| 110 | + if (i != nb_fds - 1) |
| 111 | + printf("\n"); |
| 112 | + |
| 113 | + if (err) |
| 114 | + break; |
| 115 | + close(fds[i]); |
| 116 | + } |
| 117 | + |
| 118 | + for (; i < nb_fds; i++) |
| 119 | + close(fds[i]); |
| 120 | +exit_free: |
| 121 | + free(fds); |
| 122 | + return 0; |
| 123 | +} |
| 124 | + |
| 125 | +static int do_help(int argc, char **argv) |
| 126 | +{ |
| 127 | + if (json_output) { |
| 128 | + jsonw_null(json_wtr); |
| 129 | + return 0; |
| 130 | + } |
| 131 | + |
| 132 | + fprintf(stderr, |
| 133 | + "Usage: %1$s %2$s dump MAP\n" |
| 134 | + " %1$s %2$s help\n" |
| 135 | + "\n" |
| 136 | + " " HELP_SPEC_MAP "\n" |
| 137 | + " " HELP_SPEC_OPTIONS " |\n" |
| 138 | + " {-f|--bpffs} | {-n|--nomount} }\n" |
| 139 | + "", |
| 140 | + bin_name, argv[-2]); |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +static const struct cmd cmds[] = { |
| 146 | + { "help", do_help }, |
| 147 | + { "dump", do_dump }, |
| 148 | + { 0 } |
| 149 | +}; |
| 150 | + |
| 151 | +int do_oracle(int argc, char **argv) |
| 152 | +{ |
| 153 | + return cmd_select(cmds, argc, argv, do_help); |
| 154 | +} |
0 commit comments