I'm learning eBPF and I'm playing with it in order to understand it better while following the docs but there's something I don't understand why it's not working...
I have this very simple code that stops the code and returns 5.
int main() {
exit(5);
return 0;
}
The exit function from the code above calls the exit_group syscall as can we can see by using strace (image below) yet within my Python code that's using eBPF through bcc the output I get for my bpf_trace_printk is the value 208682672 and not the value 5 that the exit_group syscall is called with as I was expecting...
from bcc import BPF
def main():
bpftext = """
#include <uapi/linux/ptrace.h>
void my_exit(struct pt_regs *ctx, int status){
bpf_trace_printk("%d", status);
}
"""
bpf = BPF(text=bpftext)
fname = bpf.get_syscall_fnname('exit_group')
bpf.attach_kprobe(event=fname, fn_name='my_exit')
while True:
print(bpf.trace_fields())
if __name__ == '__main__':
main()
I've looked into whatever I found online but I couldn't find a solution as I've been investigating this problem for a few days now...
I truly appreciate any help available and thank you!

Fix
You need to rename your function from
my_exittosyscall__exit_group.Why does this matter? BPF programs named in this way get special handling from BCC. Here's what the documentation says:
Source.
Corrected Code
Output from the sample program exiting:
How it Works
After BCC transforms your BPF program, this results in a slightly different interpretation of the arguments passed. You can use
bpf = BPF(text=bpftext, debug=bcc.DEBUG_PREPROCESSOR)to see how your code is transformed.Here's what happens without the
syscall__prefix:This reads in the RDI register and interprets it as the syscall argument.
On the other hand, here's what happens if it's named
syscall__exit_group:If the
CONFIG_ARCH_HAS_SYSCALL_WRAPPERis defined (it is on x86_64) then the RDI register is interpreted as a pointer to astruct pt_regs, which looks up the RDI register in that, which is the first argument toexit_group().On systems without syscall wrappers, this does the same thing as the previous example.