iovisor / ply

Dynamic Tracing in Linux
GNU General Public License v2.0
972 stars 92 forks source link

Support for riscv64 architecture #81

Closed jsmolic closed 1 year ago

jsmolic commented 2 years ago

It would be great to have riscv64 support in ply as this is a very lightweight BPF implementation. I'll try to get initial support working and open a PR.

cc @dlan17

wkz commented 2 years ago

Sounds awesome!

Adding full support should hopefully be quite easy, have a look at the existing ones in src/libply/arch/.

Ideally we would also extend the test suite to run on a RISC-V QEMU machine, but I realize that that is very hard to extend at the moment (as the root filesystems are pulled in from a different repo).

jsmolic commented 2 years ago

Hi @wkz we have initial RISC-V support added in the https://github.com/xmzzz/ply/tree/Initial-support-for-riscv64, but there are some failures with the self-test

$ sudo ply -T
Verifying kernel config (/proc/config.gz)... OK
Ensuring that debugfs is mounted... OK
Verifying kprobe... OK
Verifying tracepoint... OK
Verifying special... ERR:-22
ERROR
Verifying interval... OK

Do you have any idea on where the problem might be?

wkz commented 2 years ago

It seems like the BEGIN probe isn't working: https://github.com/wkz/ply/blob/master/src/ply/self-test.sh#L87-L93

It is implemented using Uprobes: https://github.com/wkz/ply/blob/master/src/libply/provider/special.c#L110-L113

is that enabled on your system?

jsmolic commented 2 years ago

Yes, I have all kernel options that self test checks for enabled

namhyung commented 2 years ago

Please check if the offset calculation in register_special_probes() is correct. If not, it won't trigger the uprobe when it calls begin_fn() and then BPF cannot run then the test would fail.

wkz commented 2 years ago

@namhyung: I've been working with some other BPF related stuff recently, and I've been using libbpf's bpf_prog_test_run_opts for testing. I think we should look into using that facility to implement BEGIN/END in ply. It would remove the dependency on uprobes and avoid the tricky offset calculations.

namhyung commented 2 years ago

Sounds like a good idea. I'll try to find some time.. :)

xmzzz commented 2 years ago

I run the following command on the patch[1] version and get the error message:

$ sudo ply 'BEGIN { printf("Hello ply!"); } kretprobe:vfs_read { @["size"] = quantize(retval); }' -d
debug: ir_bpf_generate: v0 -> r2
debug: ir_bpf_generate: v1 -> r6
debug: ir_bpf_generate: v0 -> r6
debug: unlimited memlock
debug: writing xprobe: p:ply6285/p2abf555f40_ply_6534 /usr/local/sbin/ply:6534
error: BEGIN: Unable to attach xprobe: Invalid argument
ERR:-22

When I debug it, I found that the error point is fd = perf_event_open(&attr, pid, cpu, group_fd, 0); in src/libply/aux/perf_event.c

https://github.com/wkz/ply/blob/master/src/libply/aux/perf_event.c#L56-#L58

The return value fd is -1.

perf_event_open() calls syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags);

https://github.com/wkz/ply/blob/master/src/libply/aux/syscall.c#L96-L104

The same error occurs in both QEMU and the following real hardware environment tests. The relevant kernel options are already turned on.

$ uname -a
Linux unmatch5 6.0.0-rc3-riscv64 #15 SMP Fri Sep 2 13:08:30 CST 2022 riscv64 GNU/Linux

Still looking for the cause of the error. Glad to get any help from you.

patch[1]: https://github.com/xmzzz/ply/tree/Initial-support-for-riscv64