lizrice / learning-ebpf

Learning eBPF, published by O'Reilly - out now! Here's where you'll find a VM config for the examples, and more
https://www.amazon.com/Learning-eBPF-Programming-Observability-Networking/dp/1098135121
Apache License 2.0
1.19k stars 255 forks source link

error: incomplete definition of type 'struct bpf_raw_tracepoint_args' | Chapter 3 #24

Closed ankitmnit closed 1 year ago

ankitmnit commented 1 year ago

It gives following error when compiling hello-func.bpf.c from chapter3

clang \
    -target bpf \
    -I/usr/include/x86_64-linux-gnu \
    -g \
    -O2 -o hello-func.bpf.o -c hello-func.bpf.c
\hello-func.bpf.c:4:54: warning: declaration of 'struct bpf_raw_tracepoint_args' will not be visible outside of this function [-Wvisibility]
static __attribute((noinline)) int get_opcode(struct bpf_raw_tracepoint_args *ctx) {
                                                     ^
hello-func.bpf.c:5:15: error: incomplete definition of type 'struct bpf_raw_tracepoint_args'
    return ctx->args[1];
           ~~~^
hello-func.bpf.c:4:54: note: forward declaration of 'struct bpf_raw_tracepoint_args'
static __attribute((noinline)) int get_opcode(struct bpf_raw_tracepoint_args *ctx) {
                                                     ^
hello-func.bpf.c:9:18: warning: declaration of 'struct bpf_raw_tracepoint_args' will not be visible outside of this function [-Wvisibility]
int hello(struct bpf_raw_tracepoint_args *ctx) {
                 ^
hello-func.bpf.c:10:29: warning: incompatible pointer types passing 'struct bpf_raw_tracepoint_args *' to parameter of type 'struct bpf_raw_tracepoint_args *' [-Wincompatible-pointer-types]
    int opcode = get_opcode(ctx);
                            ^~~
hello-func.bpf.c:4:79: note: passing argument to parameter 'ctx' here
static __attribute((noinline)) int get_opcode(struct bpf_raw_tracepoint_args *ctx) {
                                                                              ^
3 warnings and 1 error generated.
Makefile:2: recipe for target 'hello-func.bpf.o' failed
make: *** [hello-func.bpf.o] Error 1
64J0 commented 1 year ago

Hello. I'm getting a different error in this same example. The compilation goes well, but I can't load the program.

My error is this:

bpftool prog load hello-func.bpf.o /sys/fs/bpf/hello

# libbpf: failed to guess program type from ELF section 'raw_tp'
# libbpf: supported section(type) names are: socket sk_reuseport/migrate sk_reuseport kprobe/ 
# uprobe/ kretprobe/ uretprobe/ classifier action tracepoint/ tp/ raw_tracepoint/ raw_tp/ tp_btf/ 
# fentry/ fmod_ret/ fexit/ fentry.s/ fmod_ret.s/ fexit.s/ freplace/ lsm/ lsm.s/ iter/ syscall xdp_devmap/ 
# xdp_cpumap/ xdp perf_event lwt_in lwt_out lwt_xmit lwt_seg6local cgroup_skb/ingress cgroup_skb/egress 
# cgroup/skb cgroup/sock_create cgroup/sock_release cgroup/sock cgroup/post_bind4 cgroup/post_bind6 
# cgroup/dev sockops sk_skb/stream_parser sk_skb/stream_verdict sk_skb sk_msg lirc_mode2 
# flow_dissector cgroup/bind4 cgroup/bind6 cgroup/connect4 cgroup/connect6 cgroup/sendmsg4 
# cgroup/sendmsg6 cgroup/recvmsg4 cgroup/recvmsg6 cgroup/getpeername4 cgroup/getpeername6
# cgroup/getsockname4 cgroup/getsockname6 cgroup/sysctl cgroup/getsockopt cgroup/setsockopt 
# struct_ops sk_lookup/

Update:

I was able to fix my program by changing the SEC part:

SEC("raw_tp/")
qmonnet commented 1 year ago

I would recommend keeping each error to a different issue, or discussions will quickly turn into a mess. I've commented on the SEC("raw_tp/") issue here.

As for the compilation issue, I would suggest mentioning your kernel version (uname -r) and clang/LLVM version (clang --version). It looks like clang cannot find the definition for the struct bpf_raw_tracepoint_args, which is rather surprising because this struct has been in the UAPI header for a long time, since 4.17.

If your kernel is older than that, then you have your answer. Otherwise, clang probably fails to look at the right header file for some reason, and it would be helpful to add the -H flag to the clang invocation in the Makefile to figure out what headers it's using.

lizrice commented 1 year ago

@ankitmnit I was unable to reproduce the compilation issue. Did you follow all the steps in the README? Are you using the provided Lima config or something else for your virtual machine setup?

I'll close this for now, please feel free to re-open with more information if you can reproduce