@@ -631,6 +631,64 @@ value btf_extract_kernel_struct_names_stub(value btf_handle) {
631631 CAMLreturn (result );
632632}
633633
634+ /* Extract kfuncs from BTF using DECL_TAG annotations */
635+ value btf_extract_kfuncs_stub (value btf_handle ) {
636+ CAMLparam1 (btf_handle );
637+ CAMLlocal3 (result_list , current , tuple );
638+
639+ struct btf * btf = btf_of_value (btf_handle );
640+ if (!btf ) {
641+ CAMLreturn (Val_emptylist );
642+ }
643+
644+ result_list = Val_emptylist ;
645+ int nr_types = btf__type_cnt (btf );
646+
647+ /* First pass: find all DECL_TAG types that reference "bpf_kfunc" */
648+ for (int i = 1 ; i < nr_types ; i ++ ) {
649+ const struct btf_type * t = btf__type_by_id (btf , i );
650+ if (!t ) continue ;
651+
652+ int kind = btf_kind (t );
653+
654+ if (kind == BTF_KIND_DECL_TAG ) {
655+ const char * tag_name = btf__name_by_offset (btf , t -> name_off );
656+ if (tag_name && strcmp (tag_name , "bpf_kfunc" ) == 0 ) {
657+ /* This is a bpf_kfunc tag, get the function it references */
658+ int target_id = t -> type ;
659+ const struct btf_type * target_func = btf__type_by_id (btf , target_id );
660+
661+ if (target_func && btf_kind (target_func ) == BTF_KIND_FUNC ) {
662+ const char * func_name = btf__name_by_offset (btf , target_func -> name_off );
663+ if (!func_name ) continue ;
664+
665+ /* Get the function prototype */
666+ const struct btf_type * func_proto = btf__type_by_id (btf , target_func -> type );
667+ if (func_proto && btf_kind (func_proto ) == BTF_KIND_FUNC_PROTO ) {
668+ /* Extract function signature */
669+ char * signature = format_function_prototype (btf , func_proto );
670+
671+ /* Create tuple (function_name, signature) */
672+ tuple = caml_alloc_tuple (2 );
673+ Store_field (tuple , 0 , caml_copy_string (func_name ));
674+ Store_field (tuple , 1 , caml_copy_string (signature ));
675+
676+ /* Add to result list */
677+ current = caml_alloc (2 , 0 );
678+ Store_field (current , 0 , tuple );
679+ Store_field (current , 1 , result_list );
680+ result_list = current ;
681+
682+ free (signature );
683+ }
684+ }
685+ }
686+ }
687+ }
688+
689+ CAMLreturn (result_list );
690+ }
691+
634692/* Free BTF handle */
635693value btf_free_stub (value btf_handle ) {
636694 CAMLparam1 (btf_handle );
0 commit comments