0001
0002
0003
0004
0005 #ifndef __LIBBPF_BTF_H
0006 #define __LIBBPF_BTF_H
0007
0008 #include <stdarg.h>
0009 #include <stdbool.h>
0010 #include <linux/btf.h>
0011 #include <linux/types.h>
0012
0013 #include "libbpf_common.h"
0014
0015 #ifdef __cplusplus
0016 extern "C" {
0017 #endif
0018
0019 #define BTF_ELF_SEC ".BTF"
0020 #define BTF_EXT_ELF_SEC ".BTF.ext"
0021 #define MAPS_ELF_SEC ".maps"
0022
0023 struct btf;
0024 struct btf_ext;
0025 struct btf_type;
0026
0027 struct bpf_object;
0028
0029 enum btf_endianness {
0030 BTF_LITTLE_ENDIAN = 0,
0031 BTF_BIG_ENDIAN = 1,
0032 };
0033
0034
0035
0036
0037
0038 LIBBPF_API void btf__free(struct btf *btf);
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054 LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 LIBBPF_API struct btf *btf__new_split(const void *data, __u32 size, struct btf *base_btf);
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090 LIBBPF_API struct btf *btf__new_empty(void);
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108 LIBBPF_API struct btf *btf__new_empty_split(struct btf *base_btf);
0109
0110 LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
0111 LIBBPF_API struct btf *btf__parse_split(const char *path, struct btf *base_btf);
0112 LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
0113 LIBBPF_API struct btf *btf__parse_elf_split(const char *path, struct btf *base_btf);
0114 LIBBPF_API struct btf *btf__parse_raw(const char *path);
0115 LIBBPF_API struct btf *btf__parse_raw_split(const char *path, struct btf *base_btf);
0116
0117 LIBBPF_API struct btf *btf__load_vmlinux_btf(void);
0118 LIBBPF_API struct btf *btf__load_module_btf(const char *module_name, struct btf *vmlinux_btf);
0119 LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
0120
0121 LIBBPF_API struct btf *btf__load_from_kernel_by_id(__u32 id);
0122 LIBBPF_API struct btf *btf__load_from_kernel_by_id_split(__u32 id, struct btf *base_btf);
0123
0124 LIBBPF_API int btf__load_into_kernel(struct btf *btf);
0125 LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
0126 const char *type_name);
0127 LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
0128 const char *type_name, __u32 kind);
0129 LIBBPF_API __u32 btf__type_cnt(const struct btf *btf);
0130 LIBBPF_API const struct btf *btf__base_btf(const struct btf *btf);
0131 LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
0132 __u32 id);
0133 LIBBPF_API size_t btf__pointer_size(const struct btf *btf);
0134 LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz);
0135 LIBBPF_API enum btf_endianness btf__endianness(const struct btf *btf);
0136 LIBBPF_API int btf__set_endianness(struct btf *btf, enum btf_endianness endian);
0137 LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
0138 LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
0139 LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
0140 LIBBPF_API int btf__fd(const struct btf *btf);
0141 LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
0142 LIBBPF_API const void *btf__raw_data(const struct btf *btf, __u32 *size);
0143 LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
0144 LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
0145
0146 LIBBPF_API struct btf_ext *btf_ext__new(const __u8 *data, __u32 size);
0147 LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
0148 LIBBPF_API const void *btf_ext__raw_data(const struct btf_ext *btf_ext, __u32 *size);
0149
0150 LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
0151 LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
0152 LIBBPF_API int btf__add_type(struct btf *btf, const struct btf *src_btf,
0153 const struct btf_type *src_type);
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175 LIBBPF_API int btf__add_btf(struct btf *btf, const struct btf *src_btf);
0176
0177 LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding);
0178 LIBBPF_API int btf__add_float(struct btf *btf, const char *name, size_t byte_sz);
0179 LIBBPF_API int btf__add_ptr(struct btf *btf, int ref_type_id);
0180 LIBBPF_API int btf__add_array(struct btf *btf,
0181 int index_type_id, int elem_type_id, __u32 nr_elems);
0182
0183 LIBBPF_API int btf__add_struct(struct btf *btf, const char *name, __u32 sz);
0184 LIBBPF_API int btf__add_union(struct btf *btf, const char *name, __u32 sz);
0185 LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_id,
0186 __u32 bit_offset, __u32 bit_size);
0187
0188
0189 LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz);
0190 LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value);
0191 LIBBPF_API int btf__add_enum64(struct btf *btf, const char *name, __u32 bytes_sz, bool is_signed);
0192 LIBBPF_API int btf__add_enum64_value(struct btf *btf, const char *name, __u64 value);
0193
0194 enum btf_fwd_kind {
0195 BTF_FWD_STRUCT = 0,
0196 BTF_FWD_UNION = 1,
0197 BTF_FWD_ENUM = 2,
0198 };
0199
0200 LIBBPF_API int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind);
0201 LIBBPF_API int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id);
0202 LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id);
0203 LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id);
0204 LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id);
0205 LIBBPF_API int btf__add_type_tag(struct btf *btf, const char *value, int ref_type_id);
0206
0207
0208 LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
0209 enum btf_func_linkage linkage, int proto_type_id);
0210 LIBBPF_API int btf__add_func_proto(struct btf *btf, int ret_type_id);
0211 LIBBPF_API int btf__add_func_param(struct btf *btf, const char *name, int type_id);
0212
0213
0214 LIBBPF_API int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id);
0215 LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz);
0216 LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
0217 __u32 offset, __u32 byte_sz);
0218
0219
0220 LIBBPF_API int btf__add_decl_tag(struct btf *btf, const char *value, int ref_type_id,
0221 int component_idx);
0222
0223 struct btf_dedup_opts {
0224 size_t sz;
0225
0226 struct btf_ext *btf_ext;
0227
0228 bool force_collisions;
0229 size_t :0;
0230 };
0231 #define btf_dedup_opts__last_field force_collisions
0232
0233 LIBBPF_API int btf__dedup(struct btf *btf, const struct btf_dedup_opts *opts);
0234
0235 struct btf_dump;
0236
0237 struct btf_dump_opts {
0238 size_t sz;
0239 };
0240 #define btf_dump_opts__last_field sz
0241
0242 typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
0243
0244 LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
0245 btf_dump_printf_fn_t printf_fn,
0246 void *ctx,
0247 const struct btf_dump_opts *opts);
0248
0249 LIBBPF_API void btf_dump__free(struct btf_dump *d);
0250
0251 LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
0252
0253 struct btf_dump_emit_type_decl_opts {
0254
0255 size_t sz;
0256
0257
0258
0259
0260
0261 const char *field_name;
0262
0263
0264
0265
0266
0267 int indent_level;
0268
0269 bool strip_mods;
0270 size_t :0;
0271 };
0272 #define btf_dump_emit_type_decl_opts__last_field strip_mods
0273
0274 LIBBPF_API int
0275 btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
0276 const struct btf_dump_emit_type_decl_opts *opts);
0277
0278
0279 struct btf_dump_type_data_opts {
0280
0281 size_t sz;
0282 const char *indent_str;
0283 int indent_level;
0284
0285 bool compact;
0286 bool skip_names;
0287 bool emit_zeroes;
0288 size_t :0;
0289 };
0290 #define btf_dump_type_data_opts__last_field emit_zeroes
0291
0292 LIBBPF_API int
0293 btf_dump__dump_type_data(struct btf_dump *d, __u32 id,
0294 const void *data, size_t data_sz,
0295 const struct btf_dump_type_data_opts *opts);
0296
0297
0298
0299
0300
0301
0302
0303
0304
0305 #ifndef BTF_KIND_FUNC
0306 #define BTF_KIND_FUNC 12
0307 #define BTF_KIND_FUNC_PROTO 13
0308 #endif
0309 #ifndef BTF_KIND_VAR
0310 #define BTF_KIND_VAR 14
0311 #define BTF_KIND_DATASEC 15
0312 #endif
0313 #ifndef BTF_KIND_FLOAT
0314 #define BTF_KIND_FLOAT 16
0315 #endif
0316
0317 #define BTF_KIND_DECL_TAG 17
0318 #define BTF_KIND_TYPE_TAG 18
0319 #define BTF_KIND_ENUM64 19
0320
0321 static inline __u16 btf_kind(const struct btf_type *t)
0322 {
0323 return BTF_INFO_KIND(t->info);
0324 }
0325
0326 static inline __u16 btf_vlen(const struct btf_type *t)
0327 {
0328 return BTF_INFO_VLEN(t->info);
0329 }
0330
0331 static inline bool btf_kflag(const struct btf_type *t)
0332 {
0333 return BTF_INFO_KFLAG(t->info);
0334 }
0335
0336 static inline bool btf_is_void(const struct btf_type *t)
0337 {
0338 return btf_kind(t) == BTF_KIND_UNKN;
0339 }
0340
0341 static inline bool btf_is_int(const struct btf_type *t)
0342 {
0343 return btf_kind(t) == BTF_KIND_INT;
0344 }
0345
0346 static inline bool btf_is_ptr(const struct btf_type *t)
0347 {
0348 return btf_kind(t) == BTF_KIND_PTR;
0349 }
0350
0351 static inline bool btf_is_array(const struct btf_type *t)
0352 {
0353 return btf_kind(t) == BTF_KIND_ARRAY;
0354 }
0355
0356 static inline bool btf_is_struct(const struct btf_type *t)
0357 {
0358 return btf_kind(t) == BTF_KIND_STRUCT;
0359 }
0360
0361 static inline bool btf_is_union(const struct btf_type *t)
0362 {
0363 return btf_kind(t) == BTF_KIND_UNION;
0364 }
0365
0366 static inline bool btf_is_composite(const struct btf_type *t)
0367 {
0368 __u16 kind = btf_kind(t);
0369
0370 return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
0371 }
0372
0373 static inline bool btf_is_enum(const struct btf_type *t)
0374 {
0375 return btf_kind(t) == BTF_KIND_ENUM;
0376 }
0377
0378 static inline bool btf_is_enum64(const struct btf_type *t)
0379 {
0380 return btf_kind(t) == BTF_KIND_ENUM64;
0381 }
0382
0383 static inline bool btf_is_fwd(const struct btf_type *t)
0384 {
0385 return btf_kind(t) == BTF_KIND_FWD;
0386 }
0387
0388 static inline bool btf_is_typedef(const struct btf_type *t)
0389 {
0390 return btf_kind(t) == BTF_KIND_TYPEDEF;
0391 }
0392
0393 static inline bool btf_is_volatile(const struct btf_type *t)
0394 {
0395 return btf_kind(t) == BTF_KIND_VOLATILE;
0396 }
0397
0398 static inline bool btf_is_const(const struct btf_type *t)
0399 {
0400 return btf_kind(t) == BTF_KIND_CONST;
0401 }
0402
0403 static inline bool btf_is_restrict(const struct btf_type *t)
0404 {
0405 return btf_kind(t) == BTF_KIND_RESTRICT;
0406 }
0407
0408 static inline bool btf_is_mod(const struct btf_type *t)
0409 {
0410 __u16 kind = btf_kind(t);
0411
0412 return kind == BTF_KIND_VOLATILE ||
0413 kind == BTF_KIND_CONST ||
0414 kind == BTF_KIND_RESTRICT ||
0415 kind == BTF_KIND_TYPE_TAG;
0416 }
0417
0418 static inline bool btf_is_func(const struct btf_type *t)
0419 {
0420 return btf_kind(t) == BTF_KIND_FUNC;
0421 }
0422
0423 static inline bool btf_is_func_proto(const struct btf_type *t)
0424 {
0425 return btf_kind(t) == BTF_KIND_FUNC_PROTO;
0426 }
0427
0428 static inline bool btf_is_var(const struct btf_type *t)
0429 {
0430 return btf_kind(t) == BTF_KIND_VAR;
0431 }
0432
0433 static inline bool btf_is_datasec(const struct btf_type *t)
0434 {
0435 return btf_kind(t) == BTF_KIND_DATASEC;
0436 }
0437
0438 static inline bool btf_is_float(const struct btf_type *t)
0439 {
0440 return btf_kind(t) == BTF_KIND_FLOAT;
0441 }
0442
0443 static inline bool btf_is_decl_tag(const struct btf_type *t)
0444 {
0445 return btf_kind(t) == BTF_KIND_DECL_TAG;
0446 }
0447
0448 static inline bool btf_is_type_tag(const struct btf_type *t)
0449 {
0450 return btf_kind(t) == BTF_KIND_TYPE_TAG;
0451 }
0452
0453 static inline bool btf_is_any_enum(const struct btf_type *t)
0454 {
0455 return btf_is_enum(t) || btf_is_enum64(t);
0456 }
0457
0458 static inline bool btf_kind_core_compat(const struct btf_type *t1,
0459 const struct btf_type *t2)
0460 {
0461 return btf_kind(t1) == btf_kind(t2) ||
0462 (btf_is_any_enum(t1) && btf_is_any_enum(t2));
0463 }
0464
0465 static inline __u8 btf_int_encoding(const struct btf_type *t)
0466 {
0467 return BTF_INT_ENCODING(*(__u32 *)(t + 1));
0468 }
0469
0470 static inline __u8 btf_int_offset(const struct btf_type *t)
0471 {
0472 return BTF_INT_OFFSET(*(__u32 *)(t + 1));
0473 }
0474
0475 static inline __u8 btf_int_bits(const struct btf_type *t)
0476 {
0477 return BTF_INT_BITS(*(__u32 *)(t + 1));
0478 }
0479
0480 static inline struct btf_array *btf_array(const struct btf_type *t)
0481 {
0482 return (struct btf_array *)(t + 1);
0483 }
0484
0485 static inline struct btf_enum *btf_enum(const struct btf_type *t)
0486 {
0487 return (struct btf_enum *)(t + 1);
0488 }
0489
0490 static inline struct btf_enum64 *btf_enum64(const struct btf_type *t)
0491 {
0492 return (struct btf_enum64 *)(t + 1);
0493 }
0494
0495 static inline __u64 btf_enum64_value(const struct btf_enum64 *e)
0496 {
0497 return ((__u64)e->val_hi32 << 32) | e->val_lo32;
0498 }
0499
0500 static inline struct btf_member *btf_members(const struct btf_type *t)
0501 {
0502 return (struct btf_member *)(t + 1);
0503 }
0504
0505
0506 static inline __u32 btf_member_bit_offset(const struct btf_type *t,
0507 __u32 member_idx)
0508 {
0509 const struct btf_member *m = btf_members(t) + member_idx;
0510 bool kflag = btf_kflag(t);
0511
0512 return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
0513 }
0514
0515
0516
0517
0518 static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
0519 __u32 member_idx)
0520 {
0521 const struct btf_member *m = btf_members(t) + member_idx;
0522 bool kflag = btf_kflag(t);
0523
0524 return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
0525 }
0526
0527 static inline struct btf_param *btf_params(const struct btf_type *t)
0528 {
0529 return (struct btf_param *)(t + 1);
0530 }
0531
0532 static inline struct btf_var *btf_var(const struct btf_type *t)
0533 {
0534 return (struct btf_var *)(t + 1);
0535 }
0536
0537 static inline struct btf_var_secinfo *
0538 btf_var_secinfos(const struct btf_type *t)
0539 {
0540 return (struct btf_var_secinfo *)(t + 1);
0541 }
0542
0543 struct btf_decl_tag;
0544 static inline struct btf_decl_tag *btf_decl_tag(const struct btf_type *t)
0545 {
0546 return (struct btf_decl_tag *)(t + 1);
0547 }
0548
0549 #ifdef __cplusplus
0550 }
0551 #endif
0552
0553 #endif