@@ -2544,16 +2544,39 @@ void cleanup_bpf_maps(void) {
25442544 }
25452545 case BPF_PROG_TYPE_KPROBE : {
25462546 // For kprobe programs, target should be the kernel function name (e.g., " sys_read" )
2547- // Use bpf_raw_tracepoint_open which works for kprobes
2547+ // Use libbpf high - level API for kprobe attachment
25482548
2549- int raw_tp_fd = bpf_raw_tracepoint_open(target, prog_fd);
2550- if (raw_tp_fd < 0 ) {
2549+ // Get the bpf_program struct from the object and file descriptor
2550+ struct bpf_program *prog = NULL ;
2551+ struct bpf_object * obj_iter;
2552+
2553+ // Find the program object corresponding to this fd
2554+ // We need to get the program from the skeleton object
2555+ if (! obj) {
2556+ fprintf(stderr, " eBPF skeleton not loaded for kprobe attachment\n " );
2557+ return - 1 ;
2558+ }
2559+
2560+ bpf_object__for_each_program(prog, obj->obj) {
2561+ if (bpf_program__fd(prog) == prog_fd) {
2562+ break;
2563+ }
2564+ }
2565+
2566+ if (! prog) {
2567+ fprintf(stderr, " Failed to find bpf_program for fd %d\n " , prog_fd);
2568+ return - 1 ;
2569+ }
2570+
2571+ // Use libbpf's high- level kprobe attachment API
2572+ struct bpf_link * link = bpf_program__attach_kprobe(prog, false , target);
2573+ if (! link) {
25512574 fprintf(stderr, " Failed to attach kprobe to function '%s': %s\n " , target, strerror(errno));
25522575 return - 1 ;
25532576 }
25542577
25552578 // For now, close immediately - in a production system you'd store this for cleanup
2556- close(raw_tp_fd );
2579+ bpf_link__destroy(link );
25572580 printf(" ✅ Kprobe attached to function: %s\n " , target);
25582581
25592582 return 0 ;
0 commit comments