Lines Matching +full:n +full:- +full:1

1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
51 if (str[n1 - i - 1] != suffix[n2 - i - 1]) in str_has_suffix()
67 t = skip_mods_and_typedefs(btf, t->type, res_id); in resolve_func_ptr()
77 strncpy(file_copy, file, PATH_MAX - 1)[PATH_MAX - 1] = '\0'; in get_obj_name()
78 strncpy(name, basename(file_copy), MAX_OBJ_NAME_LEN - 1)[MAX_OBJ_NAME_LEN - 1] = '\0'; in get_obj_name()
80 name[strlen(name) - 2] = '\0'; in get_obj_name()
97 int i, n; in get_map_ident() local
104 for (i = 0, n = ARRAY_SIZE(sfxs); i < n; i++) { in get_map_ident()
109 snprintf(buf, buf_sz, "%s", p + 1); in get_map_ident()
121 int i, n; in get_datasec_ident() local
124 if (strcmp(sec_name, ".addr_space.1") == 0) { in get_datasec_ident()
129 for (i = 0, n = ARRAY_SIZE(pfxs); i < n; i++) { in get_datasec_ident()
133 snprintf(buf, buf_sz, "%s", sec_name + 1); in get_datasec_ident()
153 const char *sec_name = btf__name_by_offset(btf, sec->name_off); in codegen_datasec_def()
165 printf(" struct %s__%s {\n", obj_name, sec_ident); in codegen_datasec_def()
167 const struct btf_type *var = btf__type_by_id(btf, sec_var->type); in codegen_datasec_def()
168 const char *var_name = btf__name_by_offset(btf, var->name_off); in codegen_datasec_def()
174 int need_off = sec_var->offset, align_off, align; in codegen_datasec_def()
175 __u32 var_type_id = var->type; in codegen_datasec_def()
178 if (btf_var(var)->linkage == BTF_VAR_STATIC) in codegen_datasec_def()
182 p_err("Something is wrong for %s's variable #%d: need offset %d, already at %d.\n", in codegen_datasec_def()
184 return -EINVAL; in codegen_datasec_def()
187 align = btf__align_of(btf, var->type); in codegen_datasec_def()
191 return -EINVAL; in codegen_datasec_def()
193 /* Assume 32-bit architectures when generating data section in codegen_datasec_def()
196 * conservative and assume 32-bit one to ensure enough padding in codegen_datasec_def()
198 * still work correctly for 64-bit architectures, because in in codegen_datasec_def()
200 * which on 64-bit architectures is not strictly necessary and in codegen_datasec_def()
201 * would be handled by natural 8-byte alignment. But it still in codegen_datasec_def()
208 align_off = (off + align - 1) / align * align; in codegen_datasec_def()
210 printf("\t\tchar __pad%d[%d];\n", in codegen_datasec_def()
211 pad_cnt, need_off - off); in codegen_datasec_def()
220 strncat(var_ident, var_name, sizeof(var_ident) - 1); in codegen_datasec_def()
227 printf(";\n"); in codegen_datasec_def()
229 off = sec_var->offset + sec_var->size; in codegen_datasec_def()
231 printf(" } *%s;\n", sec_ident); in codegen_datasec_def()
237 int n = btf__type_cnt(btf), i; in find_type_for_map() local
240 for (i = 1; i < n; i++) { in find_type_for_map()
247 name = btf__str_by_offset(btf, t->name_off); in find_type_for_map()
286 return -errno; in codegen_datasecs()
289 /* only generate definitions for memory-mapped internal maps */ in codegen_datasecs()
299 * map. It will still be memory-mapped and its contents in codegen_datasecs()
300 * accessible from user-space through BPF skeleton. in codegen_datasecs()
303 printf(" struct %s__%s {\n", obj_name, map_ident); in codegen_datasecs()
304 printf(" } *%s;\n", map_ident); in codegen_datasecs()
321 return btf_is_ptr(v) && btf_is_func_proto(btf__type_by_id(btf, v->type)); in btf_is_ptr_to_func_proto()
339 return -errno; in codegen_subskel_datasecs()
342 /* only generate definitions for memory-mapped internal maps */ in codegen_subskel_datasecs()
350 sec_name = btf__name_by_offset(btf, sec->name_off); in codegen_subskel_datasecs()
355 printf(" struct %s__%s {\n", obj_name, sec_ident); in codegen_subskel_datasecs()
367 var = btf__type_by_id(btf, sec_var->type); in codegen_subskel_datasecs()
368 var_name = btf__name_by_offset(btf, var->name_off); in codegen_subskel_datasecs()
369 var_type_id = var->type; in codegen_subskel_datasecs()
372 if (btf_var(var)->linkage == BTF_VAR_STATIC) in codegen_subskel_datasecs()
378 var = skip_mods_and_typedefs(btf, var->type, NULL); in codegen_subskel_datasecs()
398 printf(" *%s;\n", var_name); in codegen_subskel_datasecs()
400 printf(" } %s;\n", sec_ident); in codegen_subskel_datasecs()
411 int skip_tabs = 0, n; in codegen() local
416 n = strlen(template); in codegen()
417 s = malloc(n + 1); in codegen()
419 exit(-1); in codegen()
427 } else if (c == '\n') { in codegen()
431 src - template - 1, template, c); in codegen()
433 exit(-1); in codegen()
439 for (n = skip_tabs; n > 0; n--, src++) { in codegen()
442 src - template - 1, template); in codegen()
444 exit(-1); in codegen()
448 end = strchrnul(src, '\n'); in codegen()
449 for (n = end - src; n > 0 && isspace(src[n - 1]); n--) in codegen()
451 memcpy(dst, src, n); in codegen()
452 dst += n; in codegen()
454 *dst++ = '\n'; in codegen()
455 src = *end ? end + 1 : end; in codegen()
461 n = vprintf(s, args); in codegen()
476 printf("\\\n"); in print_hex()
496 /* Emit type size asserts for all top-level fields in memory-mapped internal maps. */
510 \n\ in codegen_asserts()
511 __attribute__((unused)) static void \n\ in codegen_asserts()
512 %1$s__assert(struct %1$s *s __attribute__((unused))) \n\ in codegen_asserts()
513 { \n\ in codegen_asserts()
514 #ifdef __cplusplus \n\ in codegen_asserts()
515 #define _Static_assert static_assert \n\ in codegen_asserts()
516 #endif \n\ in codegen_asserts()
533 const struct btf_type *var = btf__type_by_id(btf, sec_var->type); in codegen_asserts()
534 const char *var_name = btf__name_by_offset(btf, var->name_off); in codegen_asserts()
538 if (btf_var(var)->linkage == BTF_VAR_STATIC) in codegen_asserts()
541 var_size = btf__resolve_size(btf, var->type); in codegen_asserts()
546 strncat(var_ident, var_name, sizeof(var_ident) - 1); in codegen_asserts()
549 printf("\t_Static_assert(sizeof(s->%s->%s) == %ld, \"unexpected size of '%s'\");\n", in codegen_asserts()
554 \n\ in codegen_asserts()
555 #ifdef __cplusplus \n\ in codegen_asserts()
556 #undef _Static_assert \n\ in codegen_asserts()
557 #endif \n\ in codegen_asserts()
558 } \n\ in codegen_asserts()
570 \n\ in codegen_attach_detach()
571 \n\ in codegen_attach_detach()
572 static inline int \n\ in codegen_attach_detach()
573 %1$s__%2$s__attach(struct %1$s *skel) \n\ in codegen_attach_detach()
574 { \n\ in codegen_attach_detach()
575 int prog_fd = skel->progs.%2$s.prog_fd; \n\ in codegen_attach_detach()
580 tp_name = strchr(bpf_program__section_name(prog), '/') + 1; in codegen_attach_detach()
581 printf("\tint fd = skel_raw_tracepoint_open(\"%s\", prog_fd);\n", tp_name); in codegen_attach_detach()
586 printf("\tint fd = skel_link_create(prog_fd, 0, BPF_TRACE_ITER);\n"); in codegen_attach_detach()
588 printf("\tint fd = skel_raw_tracepoint_open(NULL, prog_fd);\n"); in codegen_attach_detach()
591 printf("\tint fd = ((void)prog_fd, 0); /* auto-attach not supported */\n"); in codegen_attach_detach()
595 \n\ in codegen_attach_detach()
596 \n\ in codegen_attach_detach()
597 if (fd > 0) \n\ in codegen_attach_detach()
598 skel->links.%1$s_fd = fd; \n\ in codegen_attach_detach()
599 return fd; \n\ in codegen_attach_detach()
600 } \n\ in codegen_attach_detach()
605 \n\ in codegen_attach_detach()
606 \n\ in codegen_attach_detach()
607 static inline int \n\ in codegen_attach_detach()
608 %1$s__attach(struct %1$s *skel) \n\ in codegen_attach_detach()
609 { \n\ in codegen_attach_detach()
610 int ret = 0; \n\ in codegen_attach_detach()
611 \n\ in codegen_attach_detach()
616 \n\ in codegen_attach_detach()
617 ret = ret < 0 ? ret : %1$s__%2$s__attach(skel); \n\ in codegen_attach_detach()
622 \n\ in codegen_attach_detach()
623 return ret < 0 ? ret : 0; \n\ in codegen_attach_detach()
624 } \n\ in codegen_attach_detach()
625 \n\ in codegen_attach_detach()
626 static inline void \n\ in codegen_attach_detach()
627 %1$s__detach(struct %1$s *skel) \n\ in codegen_attach_detach()
628 { \n\ in codegen_attach_detach()
633 \n\ in codegen_attach_detach()
634 skel_closenz(skel->links.%1$s_fd); \n\ in codegen_attach_detach()
639 \n\ in codegen_attach_detach()
640 } \n\ in codegen_attach_detach()
651 \n\ in codegen_destroy()
652 static void \n\ in codegen_destroy()
653 %1$s__destroy(struct %1$s *skel) \n\ in codegen_destroy()
654 { \n\ in codegen_destroy()
655 if (!skel) \n\ in codegen_destroy()
656 return; \n\ in codegen_destroy()
657 %1$s__detach(skel); \n\ in codegen_destroy()
663 \n\ in codegen_destroy()
664 skel_closenz(skel->progs.%1$s.prog_fd); \n\ in codegen_destroy()
673 printf("\tskel_free_map_data(skel->%1$s, skel->maps.%1$s.initial_value, %2$zd);\n", in codegen_destroy()
676 \n\ in codegen_destroy()
677 skel_closenz(skel->maps.%1$s.map_fd); \n\ in codegen_destroy()
681 \n\ in codegen_destroy()
682 skel_free(skel); \n\ in codegen_destroy()
683 } \n\ in codegen_destroy()
710 \n\ in gen_trace()
711 }; \n\ in gen_trace()
720 \n\ in gen_trace()
721 static inline struct %1$s * \n\ in gen_trace()
722 %1$s__open(void) \n\ in gen_trace()
723 { \n\ in gen_trace()
724 struct %1$s *skel; \n\ in gen_trace()
725 \n\ in gen_trace()
726 skel = skel_alloc(sizeof(*skel)); \n\ in gen_trace()
727 if (!skel) \n\ in gen_trace()
728 goto cleanup; \n\ in gen_trace()
729 skel->ctx.sz = (void *)&skel->links - (void *)skel; \n\ in gen_trace()
740 \n\ in gen_trace()
741 { \n\ in gen_trace()
742 static const char data[] __attribute__((__aligned__(8))) = \"\\\n\ in gen_trace()
747 \n\ in gen_trace()
748 \"; \n\ in gen_trace()
749 \n\ in gen_trace()
750 skel->%1$s = skel_prep_map_data((void *)data, %2$zd,\n\ in gen_trace()
751 sizeof(data) - 1);\n\ in gen_trace()
752 if (!skel->%1$s) \n\ in gen_trace()
753 goto cleanup; \n\ in gen_trace()
754 skel->maps.%1$s.initial_value = (__u64) (long) skel->%1$s;\n\ in gen_trace()
755 } \n\ in gen_trace()
759 \n\ in gen_trace()
760 return skel; \n\ in gen_trace()
761 cleanup: \n\ in gen_trace()
762 %1$s__destroy(skel); \n\ in gen_trace()
763 return NULL; \n\ in gen_trace()
764 } \n\ in gen_trace()
765 \n\ in gen_trace()
766 static inline int \n\ in gen_trace()
767 %1$s__load(struct %1$s *skel) \n\ in gen_trace()
768 { \n\ in gen_trace()
769 struct bpf_load_and_run_opts opts = {}; \n\ in gen_trace()
770 int err; \n\ in gen_trace()
771 static const char opts_data[] __attribute__((__aligned__(8))) = \"\\\n\ in gen_trace()
776 \n\ in gen_trace()
777 \"; \n\ in gen_trace()
778 static const char opts_insn[] __attribute__((__aligned__(8))) = \"\\\n\ in gen_trace()
782 \n\ in gen_trace()
783 \"; \n\ in gen_trace()
784 \n\ in gen_trace()
785 opts.ctx = (struct bpf_loader_ctx *)skel; \n\ in gen_trace()
786 opts.data_sz = sizeof(opts_data) - 1; \n\ in gen_trace()
787 opts.data = (void *)opts_data; \n\ in gen_trace()
788 opts.insns_sz = sizeof(opts_insn) - 1; \n\ in gen_trace()
789 opts.insns = (void *)opts_insn; \n\ in gen_trace()
790 \n\ in gen_trace()
791 err = bpf_load_and_run(&opts); \n\ in gen_trace()
792 if (err < 0) \n\ in gen_trace()
793 return err; \n\ in gen_trace()
807 \n\ in gen_trace()
808 skel->%1$s = skel_finalize_map_data(&skel->maps.%1$s.initial_value, \n\ in gen_trace()
809 %2$zd, %3$s, skel->maps.%1$s.map_fd);\n\ in gen_trace()
810 if (!skel->%1$s) \n\ in gen_trace()
811 return -ENOMEM; \n\ in gen_trace()
816 \n\ in gen_trace()
817 return 0; \n\ in gen_trace()
818 } \n\ in gen_trace()
819 \n\ in gen_trace()
820 static inline struct %1$s * \n\ in gen_trace()
821 %1$s__open_and_load(void) \n\ in gen_trace()
822 { \n\ in gen_trace()
823 struct %1$s *skel; \n\ in gen_trace()
824 \n\ in gen_trace()
825 skel = %1$s__open(); \n\ in gen_trace()
826 if (!skel) \n\ in gen_trace()
827 return NULL; \n\ in gen_trace()
828 if (%1$s__load(skel)) { \n\ in gen_trace()
829 %1$s__destroy(skel); \n\ in gen_trace()
830 return NULL; \n\ in gen_trace()
831 } \n\ in gen_trace()
832 return skel; \n\ in gen_trace()
833 } \n\ in gen_trace()
834 \n\ in gen_trace()
840 \n\ in gen_trace()
841 \n\ in gen_trace()
842 #endif /* %s */ \n\ in gen_trace()
877 \n\ in codegen_maps_skeleton()
878 \n\ in codegen_maps_skeleton()
879 /* maps */ \n\ in codegen_maps_skeleton()
880 s->map_cnt = %zu; \n\ in codegen_maps_skeleton()
881 s->map_skel_sz = %zu; \n\ in codegen_maps_skeleton()
882 s->maps = (struct bpf_map_skeleton *)calloc(s->map_cnt,\n\ in codegen_maps_skeleton()
883 sizeof(*s->maps) > %zu ? sizeof(*s->maps) : %zu);\n\ in codegen_maps_skeleton()
884 if (!s->maps) { \n\ in codegen_maps_skeleton()
885 err = -ENOMEM; \n\ in codegen_maps_skeleton()
886 goto err; \n\ in codegen_maps_skeleton()
887 } \n\ in codegen_maps_skeleton()
897 \n\ in codegen_maps_skeleton()
898 \n\ in codegen_maps_skeleton()
899 map = (struct bpf_map_skeleton *)((char *)s->maps + %zu * s->map_skel_sz);\n\ in codegen_maps_skeleton()
900 map->name = \"%s\"; \n\ in codegen_maps_skeleton()
901 map->map = &obj->maps.%s; \n\ in codegen_maps_skeleton()
904 /* memory-mapped internal maps */ in codegen_maps_skeleton()
906 printf("\tmap->mmaped = (void **)&obj->%s;\n", ident); in codegen_maps_skeleton()
911 \n\ in codegen_maps_skeleton()
912 map->link = &obj->links.%s; \n\ in codegen_maps_skeleton()
929 \n\ in codegen_progs_skeleton()
930 \n\ in codegen_progs_skeleton()
931 /* programs */ \n\ in codegen_progs_skeleton()
932 s->prog_cnt = %zu; \n\ in codegen_progs_skeleton()
933 s->prog_skel_sz = sizeof(*s->progs); \n\ in codegen_progs_skeleton()
934 s->progs = (struct bpf_prog_skeleton *)calloc(s->prog_cnt, s->prog_skel_sz);\n\ in codegen_progs_skeleton()
935 if (!s->progs) { \n\ in codegen_progs_skeleton()
936 err = -ENOMEM; \n\ in codegen_progs_skeleton()
937 goto err; \n\ in codegen_progs_skeleton()
938 } \n\ in codegen_progs_skeleton()
945 \n\ in codegen_progs_skeleton()
946 \n\ in codegen_progs_skeleton()
947 s->progs[%1$zu].name = \"%2$s\"; \n\ in codegen_progs_skeleton()
948 s->progs[%1$zu].prog = &obj->progs.%2$s;\n\ in codegen_progs_skeleton()
954 \n\ in codegen_progs_skeleton()
955 s->progs[%1$zu].link = &obj->links.%2$s;\n\ in codegen_progs_skeleton()
973 int i, err = 0, n; in walk_st_ops_shadow_vars() local
978 return -errno; in walk_st_ops_shadow_vars()
980 n = btf_vlen(map_type); in walk_st_ops_shadow_vars()
981 for (i = 0, m = btf_members(map_type); i < n; i++, m++) { in walk_st_ops_shadow_vars()
982 member_type = skip_mods_and_typedefs(btf, m->type, &member_type_id); in walk_st_ops_shadow_vars()
983 member_name = btf__name_by_offset(btf, m->name_off); in walk_st_ops_shadow_vars()
985 offset = m->offset / 8; in walk_st_ops_shadow_vars()
987 printf("\t\t\tchar __padding_%d[%d];\n", i, offset - next_offset); in walk_st_ops_shadow_vars()
1002 printf(";\n"); in walk_st_ops_shadow_vars()
1006 p_err("Failed to resolve size of %s: %d\n", member_name, size); in walk_st_ops_shadow_vars()
1015 if (resolve_func_ptr(btf, m->type, NULL)) { in walk_st_ops_shadow_vars()
1017 printf("\t\t\tstruct bpf_program *%s;\n", member_name); in walk_st_ops_shadow_vars()
1041 p_err("Failed to resolve size of %s: %d\n", member_name, size); in walk_st_ops_shadow_vars()
1045 printf("\t\t\tchar __unsupported_%d[%d];\n", i, size); in walk_st_ops_shadow_vars()
1055 printf("\t\t\tchar __padding_end[%d];\n", size - next_offset); in walk_st_ops_shadow_vars()
1088 return -EINVAL; in gen_st_ops_shadow_type()
1091 return -EINVAL; in gen_st_ops_shadow_type()
1093 type_name = btf__name_by_offset(btf, map_type->name_off); in gen_st_ops_shadow_type()
1095 printf("\t\tstruct %s__%s__%s {\n", obj_name, ident, type_name); in gen_st_ops_shadow_type()
1101 printf("\t\t} *%s;\n", ident); in gen_st_ops_shadow_type()
1125 printf("\tstruct {\n"); in gen_st_ops_shadow()
1134 printf("\t} struct_ops;\n"); in gen_st_ops_shadow()
1157 \n\ in gen_st_ops_shadow_init()
1158 obj->struct_ops.%1$s = (__typeof__(obj->struct_ops.%1$s))\n\ in gen_st_ops_shadow_init()
1159 bpf_map__initial_value(obj->maps.%1$s, NULL);\n\ in gen_st_ops_shadow_init()
1160 \n\ in gen_st_ops_shadow_init()
1175 int fd, err = -1; in do_skeleton()
1180 if (!REQ_ARGS(1)) { in do_skeleton()
1182 return -1; in do_skeleton()
1188 return -1; in do_skeleton()
1195 return -1; in do_skeleton()
1198 strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1); in do_skeleton()
1199 obj_name[MAX_OBJ_NAME_LEN - 1] = '\0'; in do_skeleton()
1202 return -1; in do_skeleton()
1210 return -1; in do_skeleton()
1215 return -1; in do_skeleton()
1222 return -1; in do_skeleton()
1235 opts.kernel_log_level = 1 + 2 + 4; in do_skeleton()
1240 err = -errno; in do_skeleton()
1265 \n\ in do_skeleton()
1266 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\ in do_skeleton()
1267 /* THIS FILE IS AUTOGENERATED BY BPFTOOL! */ \n\ in do_skeleton()
1268 #ifndef %2$s \n\ in do_skeleton()
1269 #define %2$s \n\ in do_skeleton()
1270 \n\ in do_skeleton()
1271 #include <bpf/skel_internal.h> \n\ in do_skeleton()
1272 \n\ in do_skeleton()
1273 struct %1$s { \n\ in do_skeleton()
1274 struct bpf_loader_ctx ctx; \n\ in do_skeleton()
1280 \n\ in do_skeleton()
1281 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\ in do_skeleton()
1282 \n\ in do_skeleton()
1283 /* THIS FILE IS AUTOGENERATED BY BPFTOOL! */ \n\ in do_skeleton()
1284 #ifndef %2$s \n\ in do_skeleton()
1285 #define %2$s \n\ in do_skeleton()
1286 \n\ in do_skeleton()
1287 #include <errno.h> \n\ in do_skeleton()
1288 #include <stdlib.h> \n\ in do_skeleton()
1289 #include <bpf/libbpf.h> \n\ in do_skeleton()
1290 \n\ in do_skeleton()
1291 #define BPF_SKEL_SUPPORTS_MAP_AUTO_ATTACH 1 \n\ in do_skeleton()
1292 \n\ in do_skeleton()
1293 struct %1$s { \n\ in do_skeleton()
1294 struct bpf_object_skeleton *skeleton; \n\ in do_skeleton()
1295 struct bpf_object *obj; \n\ in do_skeleton()
1302 printf("\tstruct {\n"); in do_skeleton()
1307 printf("\t\tstruct bpf_map_desc %s;\n", ident); in do_skeleton()
1309 printf("\t\tstruct bpf_map *%s;\n", ident); in do_skeleton()
1311 printf("\t} maps;\n"); in do_skeleton()
1320 printf("\tstruct {\n"); in do_skeleton()
1323 printf("\t\tstruct bpf_prog_desc %s;\n", in do_skeleton()
1326 printf("\t\tstruct bpf_program *%s;\n", in do_skeleton()
1329 printf("\t} progs;\n"); in do_skeleton()
1333 printf("\tstruct {\n"); in do_skeleton()
1336 printf("\t\tint %s_fd;\n", in do_skeleton()
1339 printf("\t\tstruct bpf_link *%s;\n", in do_skeleton()
1350 printf("t\tint %s_fd;\n", ident); in do_skeleton()
1352 printf("\t\tstruct bpf_link *%s;\n", ident); in do_skeleton()
1355 printf("\t} links;\n"); in do_skeleton()
1369 \n\ in do_skeleton()
1370 \n\ in do_skeleton()
1371 #ifdef __cplusplus \n\ in do_skeleton()
1372 static inline struct %1$s *open(const struct bpf_object_open_opts *opts = nullptr);\n\ in do_skeleton()
1373 static inline struct %1$s *open_and_load(); \n\ in do_skeleton()
1374 static inline int load(struct %1$s *skel); \n\ in do_skeleton()
1375 static inline int attach(struct %1$s *skel); \n\ in do_skeleton()
1376 static inline void detach(struct %1$s *skel); \n\ in do_skeleton()
1377 static inline void destroy(struct %1$s *skel); \n\ in do_skeleton()
1378 static inline const void *elf_bytes(size_t *sz); \n\ in do_skeleton()
1379 #endif /* __cplusplus */ \n\ in do_skeleton()
1380 }; \n\ in do_skeleton()
1381 \n\ in do_skeleton()
1382 static void \n\ in do_skeleton()
1383 %1$s__destroy(struct %1$s *obj) \n\ in do_skeleton()
1384 { \n\ in do_skeleton()
1385 if (!obj) \n\ in do_skeleton()
1386 return; \n\ in do_skeleton()
1387 if (obj->skeleton) \n\ in do_skeleton()
1388 bpf_object__destroy_skeleton(obj->skeleton);\n\ in do_skeleton()
1389 free(obj); \n\ in do_skeleton()
1390 } \n\ in do_skeleton()
1391 \n\ in do_skeleton()
1392 static inline int \n\ in do_skeleton()
1393 %1$s__create_skeleton(struct %1$s *obj); \n\ in do_skeleton()
1394 \n\ in do_skeleton()
1395 static inline struct %1$s * \n\ in do_skeleton()
1396 %1$s__open_opts(const struct bpf_object_open_opts *opts) \n\ in do_skeleton()
1397 { \n\ in do_skeleton()
1398 struct %1$s *obj; \n\ in do_skeleton()
1399 int err; \n\ in do_skeleton()
1400 \n\ in do_skeleton()
1401 obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\ in do_skeleton()
1402 if (!obj) { \n\ in do_skeleton()
1403 errno = ENOMEM; \n\ in do_skeleton()
1404 return NULL; \n\ in do_skeleton()
1405 } \n\ in do_skeleton()
1406 \n\ in do_skeleton()
1407 err = %1$s__create_skeleton(obj); \n\ in do_skeleton()
1408 if (err) \n\ in do_skeleton()
1409 goto err_out; \n\ in do_skeleton()
1410 \n\ in do_skeleton()
1411 err = bpf_object__open_skeleton(obj->skeleton, opts);\n\ in do_skeleton()
1412 if (err) \n\ in do_skeleton()
1413 goto err_out; \n\ in do_skeleton()
1414 \n\ in do_skeleton()
1420 \n\ in do_skeleton()
1421 return obj; \n\ in do_skeleton()
1422 err_out: \n\ in do_skeleton()
1423 %1$s__destroy(obj); \n\ in do_skeleton()
1424 errno = -err; \n\ in do_skeleton()
1425 return NULL; \n\ in do_skeleton()
1426 } \n\ in do_skeleton()
1427 \n\ in do_skeleton()
1428 static inline struct %1$s * \n\ in do_skeleton()
1429 %1$s__open(void) \n\ in do_skeleton()
1430 { \n\ in do_skeleton()
1431 return %1$s__open_opts(NULL); \n\ in do_skeleton()
1432 } \n\ in do_skeleton()
1433 \n\ in do_skeleton()
1434 static inline int \n\ in do_skeleton()
1435 %1$s__load(struct %1$s *obj) \n\ in do_skeleton()
1436 { \n\ in do_skeleton()
1437 return bpf_object__load_skeleton(obj->skeleton); \n\ in do_skeleton()
1438 } \n\ in do_skeleton()
1439 \n\ in do_skeleton()
1440 static inline struct %1$s * \n\ in do_skeleton()
1441 %1$s__open_and_load(void) \n\ in do_skeleton()
1442 { \n\ in do_skeleton()
1443 struct %1$s *obj; \n\ in do_skeleton()
1444 int err; \n\ in do_skeleton()
1445 \n\ in do_skeleton()
1446 obj = %1$s__open(); \n\ in do_skeleton()
1447 if (!obj) \n\ in do_skeleton()
1448 return NULL; \n\ in do_skeleton()
1449 err = %1$s__load(obj); \n\ in do_skeleton()
1450 if (err) { \n\ in do_skeleton()
1451 %1$s__destroy(obj); \n\ in do_skeleton()
1452 errno = -err; \n\ in do_skeleton()
1453 return NULL; \n\ in do_skeleton()
1454 } \n\ in do_skeleton()
1455 return obj; \n\ in do_skeleton()
1456 } \n\ in do_skeleton()
1457 \n\ in do_skeleton()
1458 static inline int \n\ in do_skeleton()
1459 %1$s__attach(struct %1$s *obj) \n\ in do_skeleton()
1460 { \n\ in do_skeleton()
1461 return bpf_object__attach_skeleton(obj->skeleton); \n\ in do_skeleton()
1462 } \n\ in do_skeleton()
1463 \n\ in do_skeleton()
1464 static inline void \n\ in do_skeleton()
1465 %1$s__detach(struct %1$s *obj) \n\ in do_skeleton()
1466 { \n\ in do_skeleton()
1467 bpf_object__detach_skeleton(obj->skeleton); \n\ in do_skeleton()
1468 } \n\ in do_skeleton()
1474 \n\ in do_skeleton()
1475 \n\ in do_skeleton()
1476 static inline const void *%1$s__elf_bytes(size_t *sz); \n\ in do_skeleton()
1477 \n\ in do_skeleton()
1478 static inline int \n\ in do_skeleton()
1479 %1$s__create_skeleton(struct %1$s *obj) \n\ in do_skeleton()
1480 { \n\ in do_skeleton()
1481 struct bpf_object_skeleton *s; \n\ in do_skeleton()
1482 struct bpf_map_skeleton *map __attribute__((unused));\n\ in do_skeleton()
1483 int err; \n\ in do_skeleton()
1484 \n\ in do_skeleton()
1485 s = (struct bpf_object_skeleton *)calloc(1, sizeof(*s));\n\ in do_skeleton()
1486 if (!s) { \n\ in do_skeleton()
1487 err = -ENOMEM; \n\ in do_skeleton()
1488 goto err; \n\ in do_skeleton()
1489 } \n\ in do_skeleton()
1490 \n\ in do_skeleton()
1491 s->sz = sizeof(*s); \n\ in do_skeleton()
1492 s->name = \"%1$s\"; \n\ in do_skeleton()
1493 s->obj = &obj->obj; \n\ in do_skeleton()
1502 \n\ in do_skeleton()
1503 \n\ in do_skeleton()
1504 s->data = %1$s__elf_bytes(&s->data_sz); \n\ in do_skeleton()
1505 \n\ in do_skeleton()
1506 obj->skeleton = s; \n\ in do_skeleton()
1507 return 0; \n\ in do_skeleton()
1508 err: \n\ in do_skeleton()
1509 bpf_object__destroy_skeleton(s); \n\ in do_skeleton()
1510 return err; \n\ in do_skeleton()
1511 } \n\ in do_skeleton()
1512 \n\ in do_skeleton()
1513 static inline const void *%1$s__elf_bytes(size_t *sz) \n\ in do_skeleton()
1514 { \n\ in do_skeleton()
1515 static const char data[] __attribute__((__aligned__(8))) = \"\\\n\ in do_skeleton()
1524 \n\ in do_skeleton()
1525 \"; \n\ in do_skeleton()
1526 \n\ in do_skeleton()
1527 *sz = sizeof(data) - 1; \n\ in do_skeleton()
1528 return (const void *)data; \n\ in do_skeleton()
1529 } \n\ in do_skeleton()
1530 \n\ in do_skeleton()
1531 #ifdef __cplusplus \n\ in do_skeleton()
1532 …struct %1$s *%1$s::open(const struct bpf_object_open_opts *opts) { return %1$s__open_opts(opts); }… in do_skeleton()
1533 struct %1$s *%1$s::open_and_load() { return %1$s__open_and_load(); } \n\ in do_skeleton()
1534 int %1$s::load(struct %1$s *skel) { return %1$s__load(skel); } \n\ in do_skeleton()
1535 int %1$s::attach(struct %1$s *skel) { return %1$s__attach(skel); } \n\ in do_skeleton()
1536 void %1$s::detach(struct %1$s *skel) { %1$s__detach(skel); } \n\ in do_skeleton()
1537 void %1$s::destroy(struct %1$s *skel) { %1$s__destroy(skel); } \n\ in do_skeleton()
1538 const void *%1$s::elf_bytes(size_t *sz) { return %1$s__elf_bytes(sz); } \n\ in do_skeleton()
1539 #endif /* __cplusplus */ \n\ in do_skeleton()
1540 \n\ in do_skeleton()
1547 \n\ in do_skeleton()
1548 \n\ in do_skeleton()
1549 #endif /* %1$s */ \n\ in do_skeleton()
1566 * This allows for library-like BPF objects to have userspace counterparts
1579 int fd, err = -1, map_type_id; in do_subskeleton()
1587 if (!REQ_ARGS(1)) { in do_subskeleton()
1589 return -1; in do_subskeleton()
1595 return -1; in do_subskeleton()
1602 return -1; in do_subskeleton()
1605 strncpy(obj_name, *argv, MAX_OBJ_NAME_LEN - 1); in do_subskeleton()
1606 obj_name[MAX_OBJ_NAME_LEN - 1] = '\0'; in do_subskeleton()
1609 return -1; in do_subskeleton()
1617 return -1; in do_subskeleton()
1622 return -1; in do_subskeleton()
1627 return -1; in do_subskeleton()
1634 return -1; in do_subskeleton()
1661 err = -1; in do_subskeleton()
1694 var_type = btf__type_by_id(btf, var->type); in do_subskeleton()
1696 if (btf_var(var_type)->linkage == BTF_VAR_STATIC) in do_subskeleton()
1705 \n\ in do_subskeleton()
1706 /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */ \n\ in do_subskeleton()
1707 \n\ in do_subskeleton()
1708 /* THIS FILE IS AUTOGENERATED! */ \n\ in do_subskeleton()
1709 #ifndef %2$s \n\ in do_subskeleton()
1710 #define %2$s \n\ in do_subskeleton()
1711 \n\ in do_subskeleton()
1712 #include <errno.h> \n\ in do_subskeleton()
1713 #include <stdlib.h> \n\ in do_subskeleton()
1714 #include <bpf/libbpf.h> \n\ in do_subskeleton()
1715 \n\ in do_subskeleton()
1716 struct %1$s { \n\ in do_subskeleton()
1717 struct bpf_object *obj; \n\ in do_subskeleton()
1718 struct bpf_object_subskeleton *subskel; \n\ in do_subskeleton()
1722 printf("\tstruct {\n"); in do_subskeleton()
1726 printf("\t\tstruct bpf_map *%s;\n", ident); in do_subskeleton()
1728 printf("\t} maps;\n"); in do_subskeleton()
1736 printf("\tstruct {\n"); in do_subskeleton()
1738 printf("\t\tstruct bpf_program *%s;\n", in do_subskeleton()
1741 printf("\t} progs;\n"); in do_subskeleton()
1750 \n\ in do_subskeleton()
1751 \n\ in do_subskeleton()
1752 #ifdef __cplusplus \n\ in do_subskeleton()
1753 static inline struct %1$s *open(const struct bpf_object *src);\n\ in do_subskeleton()
1754 static inline void destroy(struct %1$s *skel); \n\ in do_subskeleton()
1755 #endif /* __cplusplus */ \n\ in do_subskeleton()
1756 }; \n\ in do_subskeleton()
1757 \n\ in do_subskeleton()
1758 static inline void \n\ in do_subskeleton()
1759 %1$s__destroy(struct %1$s *skel) \n\ in do_subskeleton()
1760 { \n\ in do_subskeleton()
1761 if (!skel) \n\ in do_subskeleton()
1762 return; \n\ in do_subskeleton()
1763 if (skel->subskel) \n\ in do_subskeleton()
1764 bpf_object__destroy_subskeleton(skel->subskel);\n\ in do_subskeleton()
1765 free(skel); \n\ in do_subskeleton()
1766 } \n\ in do_subskeleton()
1767 \n\ in do_subskeleton()
1768 static inline struct %1$s * \n\ in do_subskeleton()
1769 %1$s__open(const struct bpf_object *src) \n\ in do_subskeleton()
1770 { \n\ in do_subskeleton()
1771 struct %1$s *obj; \n\ in do_subskeleton()
1772 struct bpf_object_subskeleton *s; \n\ in do_subskeleton()
1773 struct bpf_map_skeleton *map __attribute__((unused));\n\ in do_subskeleton()
1774 int err; \n\ in do_subskeleton()
1775 \n\ in do_subskeleton()
1776 obj = (struct %1$s *)calloc(1, sizeof(*obj)); \n\ in do_subskeleton()
1777 if (!obj) { \n\ in do_subskeleton()
1778 err = -ENOMEM; \n\ in do_subskeleton()
1779 goto err; \n\ in do_subskeleton()
1780 } \n\ in do_subskeleton()
1781 s = (struct bpf_object_subskeleton *)calloc(1, sizeof(*s));\n\ in do_subskeleton()
1782 if (!s) { \n\ in do_subskeleton()
1783 err = -ENOMEM; \n\ in do_subskeleton()
1784 goto err; \n\ in do_subskeleton()
1785 } \n\ in do_subskeleton()
1786 s->sz = sizeof(*s); \n\ in do_subskeleton()
1787 s->obj = src; \n\ in do_subskeleton()
1788 s->var_skel_sz = sizeof(*s->vars); \n\ in do_subskeleton()
1789 obj->subskel = s; \n\ in do_subskeleton()
1790 \n\ in do_subskeleton()
1791 /* vars */ \n\ in do_subskeleton()
1792 s->var_cnt = %2$d; \n\ in do_subskeleton()
1793 s->vars = (struct bpf_var_skeleton *)calloc(%2$d, sizeof(*s->vars));\n\ in do_subskeleton()
1794 if (!s->vars) { \n\ in do_subskeleton()
1795 err = -ENOMEM; \n\ in do_subskeleton()
1796 goto err; \n\ in do_subskeleton()
1797 } \n\ in do_subskeleton()
1816 var_type = btf__type_by_id(btf, var->type); in do_subskeleton()
1817 var_name = btf__name_by_offset(btf, var_type->name_off); in do_subskeleton()
1819 if (btf_var(var_type)->linkage == BTF_VAR_STATIC) in do_subskeleton()
1826 \n\ in do_subskeleton()
1827 \n\ in do_subskeleton()
1828 s->vars[%3$d].name = \"%1$s\"; \n\ in do_subskeleton()
1829 s->vars[%3$d].map = &obj->maps.%2$s; \n\ in do_subskeleton()
1830 s->vars[%3$d].addr = (void **) &obj->%2$s.%1$s;\n\ in do_subskeleton()
1841 \n\ in do_subskeleton()
1842 \n\ in do_subskeleton()
1843 err = bpf_object__open_subskeleton(s); \n\ in do_subskeleton()
1844 if (err) \n\ in do_subskeleton()
1845 goto err; \n\ in do_subskeleton()
1846 \n\ in do_subskeleton()
1852 \n\ in do_subskeleton()
1853 return obj; \n\ in do_subskeleton()
1854 err: \n\ in do_subskeleton()
1855 %1$s__destroy(obj); \n\ in do_subskeleton()
1856 errno = -err; \n\ in do_subskeleton()
1857 return NULL; \n\ in do_subskeleton()
1858 } \n\ in do_subskeleton()
1859 \n\ in do_subskeleton()
1860 #ifdef __cplusplus \n\ in do_subskeleton()
1861 struct %1$s *%1$s::open(const struct bpf_object *src) { return %1$s__open(src); }\n\ in do_subskeleton()
1862 void %1$s::destroy(struct %1$s *skel) { %1$s__destroy(skel); }\n\ in do_subskeleton()
1863 #endif /* __cplusplus */ \n\ in do_subskeleton()
1864 \n\ in do_subskeleton()
1865 #endif /* %2$s */ \n\ in do_subskeleton()
1885 return -1; in do_object()
1893 return -1; in do_object()
1926 "Usage: %1$s %2$s object OUTPUT_FILE INPUT_FILE [INPUT_FILE...]\n" in do_help()
1927 " %1$s %2$s skeleton FILE [name OBJECT_NAME]\n" in do_help()
1928 " %1$s %2$s subskeleton FILE [name OBJECT_NAME]\n" in do_help()
1929 " %1$s %2$s min_core_btf INPUT OUTPUT OBJECT [OBJECT...]\n" in do_help()
1930 " %1$s %2$s help\n" in do_help()
1931 "\n" in do_help()
1932 " " HELP_SPEC_OPTIONS " |\n" in do_help()
1933 " {-L|--use-loader} }\n" in do_help()
1949 return -ENOMEM; in btf_save_raw()
1953 return -errno; in btf_save_raw()
1955 if (fwrite(data, 1, data_sz, f) != data_sz) in btf_save_raw()
1956 err = -errno; in btf_save_raw()
1982 btf__free(info->src_btf); in btfgen_free_info()
1983 btf__free(info->marked_btf); in btfgen_free_info()
1994 info = calloc(1, sizeof(*info)); in btfgen_new_info()
1998 info->src_btf = btf__parse(targ_btf_path, NULL); in btfgen_new_info()
1999 if (!info->src_btf) { in btfgen_new_info()
2000 err = -errno; in btfgen_new_info()
2005 info->marked_btf = btf__parse(targ_btf_path, NULL); in btfgen_new_info()
2006 if (!info->marked_btf) { in btfgen_new_info()
2007 err = -errno; in btfgen_new_info()
2016 errno = -err; in btfgen_new_info()
2024 const struct btf_type *t = btf__type_by_id(info->marked_btf, type_id); in btfgen_mark_member()
2027 m->name_off = MARKED; in btfgen_mark_member()
2033 const struct btf_type *btf_type = btf__type_by_id(info->src_btf, type_id); in btfgen_mark_type()
2043 cloned_type = (struct btf_type *) btf__type_by_id(info->marked_btf, type_id); in btfgen_mark_type()
2044 cloned_type->name_off = MARKED; in btfgen_mark_type()
2058 err = btfgen_mark_type(info, btf_type->type, follow_pointers); in btfgen_mark_type()
2067 err = btfgen_mark_type(info, btf_type->type, follow_pointers); in btfgen_mark_type()
2075 err = btfgen_mark_type(info, array->type, follow_pointers); in btfgen_mark_type()
2077 err = err ? : btfgen_mark_type(info, array->index_type, follow_pointers); in btfgen_mark_type()
2083 err = btfgen_mark_type(info, btf_type->type, follow_pointers); in btfgen_mark_type()
2090 err = btfgen_mark_type(info, param->type, follow_pointers); in btfgen_mark_type()
2099 return -EINVAL; in btfgen_mark_type()
2107 struct btf *btf = info->src_btf; in btfgen_record_field_relo()
2111 unsigned int type_id = targ_spec->root_type_id; in btfgen_record_field_relo()
2121 for (int i = 1; i < targ_spec->raw_len; i++) { in btfgen_record_field_relo()
2124 type_id = btf_type->type; in btfgen_record_field_relo()
2131 idx = targ_spec->raw_spec[i]; in btfgen_record_field_relo()
2138 type_id = btf_member->type; in btfgen_record_field_relo()
2146 type_id = array->type; in btfgen_record_field_relo()
2151 btf_kind_str(btf_type), btf_type->type); in btfgen_record_field_relo()
2152 return -EINVAL; in btfgen_record_field_relo()
2170 struct btf *btf = info->src_btf; in btfgen_mark_type_match()
2179 cloned_type = (struct btf_type *)btf__type_by_id(info->marked_btf, type_id); in btfgen_mark_type_match()
2180 cloned_type->name_off = MARKED; in btfgen_mark_type_match()
2202 err = btfgen_mark_type_match(info, m->type, false); in btfgen_mark_type_match()
2213 return btfgen_mark_type_match(info, btf_type->type, behind_ptr); in btfgen_mark_type_match()
2215 return btfgen_mark_type_match(info, btf_type->type, true); in btfgen_mark_type_match()
2221 err = btfgen_mark_type_match(info, array->type, false); in btfgen_mark_type_match()
2223 err = err ? : btfgen_mark_type_match(info, array->index_type, false); in btfgen_mark_type_match()
2233 err = btfgen_mark_type_match(info, btf_type->type, false); in btfgen_mark_type_match()
2240 err = btfgen_mark_type_match(info, param->type, false); in btfgen_mark_type_match()
2250 return -EINVAL; in btfgen_mark_type_match()
2262 return btfgen_mark_type_match(info, targ_spec->root_type_id, false); in btfgen_record_type_match_relo()
2267 return btfgen_mark_type(info, targ_spec->root_type_id, true); in btfgen_record_type_relo()
2272 return btfgen_mark_type(info, targ_spec->root_type_id, false); in btfgen_record_enumval_relo()
2277 switch (res->relo_kind) { in btfgen_record_reloc()
2297 return -EINVAL; in btfgen_record_reloc()
2316 err = -EINVAL; in btfgen_find_cands()
2320 local_name = btf__name_by_offset(local_btf, local_type->name_off); in btfgen_find_cands()
2322 err = -EINVAL; in btfgen_find_cands()
2327 cands = calloc(1, sizeof(*cands)); in btfgen_find_cands()
2331 err = bpf_core_add_cands(&local_cand, local_essent_len, targ_btf, "vmlinux", 1, cands); in btfgen_find_cands()
2339 errno = -err; in btfgen_find_cands()
2359 err = -errno; in btfgen_record_obj()
2367 err = -EINVAL; in btfgen_record_obj()
2371 if (btf_ext->core_relo_info.len == 0) { in btfgen_record_obj()
2382 seg = &btf_ext->core_relo_info; in btfgen_record_obj()
2388 const char *sec_name = btf__name_by_offset(btf, sec->sec_name_off); in btfgen_record_obj()
2390 if (relo->kind != BPF_CORE_TYPE_ID_LOCAL && in btfgen_record_obj()
2391 !hashmap__find(cand_cache, relo->type_id, &cands)) { in btfgen_record_obj()
2392 cands = btfgen_find_cands(btf, info->src_btf, relo->type_id); in btfgen_record_obj()
2394 err = -errno; in btfgen_record_obj()
2398 err = hashmap__set(cand_cache, relo->type_id, cands, in btfgen_record_obj()
2422 bpf_core_free_cands(entry->pvalue); in btfgen_record_obj()
2435 unsigned int i, n = btf__type_cnt(info->marked_btf); in btfgen_get_btf() local
2440 err = -errno; in btfgen_get_btf()
2444 ids = calloc(n, sizeof(*ids)); in btfgen_get_btf()
2446 err = -errno; in btfgen_get_btf()
2451 for (i = 1; i < n; i++) { in btfgen_get_btf()
2456 cloned_type = btf__type_by_id(info->marked_btf, i); in btfgen_get_btf()
2458 if (cloned_type->name_off != MARKED) in btfgen_get_btf()
2461 type = btf__type_by_id(info->src_btf, i); in btfgen_get_btf()
2469 name = btf__str_by_offset(info->src_btf, type->name_off); in btfgen_get_btf()
2472 err = btf__add_struct(btf_new, name, type->size); in btfgen_get_btf()
2474 err = btf__add_union(btf_new, name, type->size); in btfgen_get_btf()
2485 if (cloned_m->name_off != MARKED) in btfgen_get_btf()
2488 name = btf__str_by_offset(info->src_btf, m->name_off); in btfgen_get_btf()
2489 err = btf__add_field(btf_new, name, m->type, in btfgen_get_btf()
2496 err = btf__add_type(btf_new, info->src_btf, type); in btfgen_get_btf()
2507 for (i = 1; i < btf__type_cnt(btf_new); i++) { in btfgen_get_btf()
2526 errno = -err; in btfgen_get_btf()
2532 * The BTFGen algorithm is divided in two main parts: (1) collect the
2542 * relocation. For field-based relocations only the members that are
2544 * BTF file. For type-based relocations empty struct / unions are
2545 * generated and for enum-based relocations the whole type is saved.
2561 err = -errno; in minimize_btf()
2577 err = -errno; in minimize_btf()
2602 return -1; in do_min_core_btf()
2608 objs = (const char **) calloc(argc + 1, sizeof(*objs)); in do_min_core_btf()
2611 return -ENOMEM; in do_min_core_btf()