On Tue, 2025-06-10 at 20:08 +0100, Mykyta Yatsenko wrote: > From: Mykyta Yatsenko <yatsenko@xxxxxxxx> > > Implement support for presetting values for array elements in > veristat. > For example: > ``` > sudo ./veristat set_global_vars.bpf.o -G "arr[3] = 1" > ``` > Arrays of structures and structure of arrays work, but each > individual > scalar value has to be set separately: `foo[1].bar[2] = value`. > > Signed-off-by: Mykyta Yatsenko <yatsenko@xxxxxxxx> > --- A few nits regarding error reporting: ./veristat -q -G ptr_arr[10]=0 set_global_vars.bpf.o Unsupported array element type for variable ptr_arr. Only int, enum, struct, union are supported ^^^ missing dot ./veristat -G arr[[10]]=0 set_global_vars.bpf.o Could not parse 'arr[[10]]'Failed to parse global variable presets: arr[[10]]=0 ^^^^^^^^^ ^^^ ^^^ Can't ? dot or comma missing dot ./veristat -q -G "struct1[0] = 0" set_global_vars.bpf.o Setting value for type Struct is not supported ^^^^^^^^^ report full_name here? I applied a diff as in the attachment, to see what offsets are being assigned. Looks like this shows a bug: ./veristat -q -G "struct1[0].filler = 42" set_global_vars.bpf.o > /dev/null setting struct1[0].filler: offset 54, kind int, value 42 Shouldn't offset be 2 in this case? (maybe print such info in debug (-d) mode?) Unrelated to this patch, but still a bug: # Catches range error: ./veristat -q -G "struct1[0].filler2 = 100500" set_global_vars.bpf.o Variable unsigned short value 100500 is out of range [0; 65535] # Does not range error: ./veristat -q -G "struct1[0].filler2 = -1" set_global_vars.bpf.o ... success ... [...]
diff --git a/tools/testing/selftests/bpf/veristat.c b/tools/testing/selftests/bpf/veristat.c index bc9ebf5a2985..7f3e8ba75acc 100644 --- a/tools/testing/selftests/bpf/veristat.c +++ b/tools/testing/selftests/bpf/veristat.c @@ -1725,6 +1725,8 @@ static int adjust_var_secinfo(struct btf *btf, const struct btf_type *t, return 0; } +const char *btf_kind_str(const struct btf_type *t); + static int set_global_var(struct bpf_object *obj, struct btf *btf, struct bpf_map *map, struct btf_var_secinfo *sinfo, struct var_preset *preset) @@ -1761,6 +1763,9 @@ static int set_global_var(struct bpf_object *obj, struct btf *btf, } } + fprintf(stderr, "setting %s: offset %d, kind %s, value %lld\n", + preset->full_name, sinfo->offset, btf_kind_str(base_type), value); + /* Check if value fits into the target variable size */ if (sinfo->size < sizeof(value)) { bool is_signed = is_signed_type(base_type);