Skip to content

Commit

Permalink
libbpf: Fix BTF-defined map's __type macro handling of arrays
Browse files Browse the repository at this point in the history
Due to a quirky C syntax of declaring pointers to array or function
prototype, existing __type() macro doesn't work with map key/value types
that are array or function prototype. One has to create a typedef first
and use it to specify key/value type for a BPF map.  By using typeof(),
pointer to type is now handled uniformly for all kinds of types. Convert
one of self-tests as a demonstration.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Link: https://lore.kernel.org/bpf/20191004040211.2434033-1-andriin@fb.com
  • Loading branch information
Andrii Nakryiko authored and Alexei Starovoitov committed Oct 6, 2019
1 parent 4bbbf16 commit a53ba15
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 3 deletions.
2 changes: 1 addition & 1 deletion tools/testing/selftests/bpf/bpf_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define __BPF_HELPERS__

#define __uint(name, val) int (*name)[val]
#define __type(name, val) val *name
#define __type(name, val) typeof(val) *name

/* helper macro to print out debug messages */
#define bpf_printk(fmt, ...) \
Expand Down
3 changes: 1 addition & 2 deletions tools/testing/selftests/bpf/progs/test_get_stack_rawtp.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,11 @@ struct {
* issue and avoid complicated C programming massaging.
* This is an acceptable workaround since there is one entry here.
*/
typedef __u64 raw_stack_trace_t[2 * MAX_STACK_RAWTP];
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, raw_stack_trace_t);
__type(value, __u64[2 * MAX_STACK_RAWTP]);
} rawdata_map SEC(".maps");

SEC("raw_tracepoint/sys_enter")
Expand Down

0 comments on commit a53ba15

Please sign in to comment.