^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* Copyright (c) 2018 Facebook */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #ifndef __LIBBPF_BTF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #define __LIBBPF_BTF_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <stdarg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/btf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "libbpf_common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #ifdef __cplusplus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) extern "C" {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define BTF_ELF_SEC ".BTF"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define BTF_EXT_ELF_SEC ".BTF.ext"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define MAPS_ELF_SEC ".maps"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct btf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct btf_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct btf_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) struct bpf_object;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) enum btf_endianness {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) BTF_LITTLE_ENDIAN = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) BTF_BIG_ENDIAN = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) LIBBPF_API void btf__free(struct btf *btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) LIBBPF_API struct btf *btf__new(const void *data, __u32 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) LIBBPF_API struct btf *btf__new_empty(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) LIBBPF_API struct btf *btf__parse(const char *path, struct btf_ext **btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) LIBBPF_API struct btf *btf__parse_elf(const char *path, struct btf_ext **btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) LIBBPF_API struct btf *btf__parse_raw(const char *path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) LIBBPF_API int btf__finalize_data(struct bpf_object *obj, struct btf *btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) LIBBPF_API int btf__load(struct btf *btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) LIBBPF_API __s32 btf__find_by_name(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) const char *type_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) LIBBPF_API __s32 btf__find_by_name_kind(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) const char *type_name, __u32 kind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) LIBBPF_API __u32 btf__get_nr_types(const struct btf *btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) LIBBPF_API const struct btf_type *btf__type_by_id(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) __u32 id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) LIBBPF_API size_t btf__pointer_size(const struct btf *btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) LIBBPF_API int btf__set_pointer_size(struct btf *btf, size_t ptr_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) LIBBPF_API enum btf_endianness btf__endianness(const struct btf *btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) LIBBPF_API int btf__set_endianness(struct btf *btf, enum btf_endianness endian);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) LIBBPF_API __s64 btf__resolve_size(const struct btf *btf, __u32 type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) LIBBPF_API int btf__align_of(const struct btf *btf, __u32 id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) LIBBPF_API int btf__fd(const struct btf *btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) LIBBPF_API void btf__set_fd(struct btf *btf, int fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) LIBBPF_API const void *btf__get_raw_data(const struct btf *btf, __u32 *size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) LIBBPF_API const char *btf__str_by_offset(const struct btf *btf, __u32 offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) LIBBPF_API int btf__get_from_id(__u32 id, struct btf **btf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) __u32 expected_key_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) __u32 expected_value_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) __u32 *key_type_id, __u32 *value_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) LIBBPF_API const void *btf_ext__get_raw_data(const struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) __u32 *size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) LIBBPF_API LIBBPF_DEPRECATED("btf_ext__reloc_func_info was never meant as a public API and has wrong assumptions embedded in it; it will be removed in the future libbpf versions")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int btf_ext__reloc_func_info(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) const struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) const char *sec_name, __u32 insns_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) void **func_info, __u32 *cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) LIBBPF_API LIBBPF_DEPRECATED("btf_ext__reloc_line_info was never meant as a public API and has wrong assumptions embedded in it; it will be removed in the future libbpf versions")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) int btf_ext__reloc_line_info(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) const struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) const char *sec_name, __u32 insns_cnt,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) void **line_info, __u32 *cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) LIBBPF_API __u32 btf_ext__func_info_rec_size(const struct btf_ext *btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) LIBBPF_API __u32 btf_ext__line_info_rec_size(const struct btf_ext *btf_ext);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) LIBBPF_API struct btf *libbpf_find_kernel_btf(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) LIBBPF_API int btf__find_str(struct btf *btf, const char *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) LIBBPF_API int btf__add_str(struct btf *btf, const char *s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) LIBBPF_API int btf__add_int(struct btf *btf, const char *name, size_t byte_sz, int encoding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) LIBBPF_API int btf__add_ptr(struct btf *btf, int ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) LIBBPF_API int btf__add_array(struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) int index_type_id, int elem_type_id, __u32 nr_elems);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) /* struct/union construction APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) LIBBPF_API int btf__add_struct(struct btf *btf, const char *name, __u32 sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) LIBBPF_API int btf__add_union(struct btf *btf, const char *name, __u32 sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) LIBBPF_API int btf__add_field(struct btf *btf, const char *name, int field_type_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) __u32 bit_offset, __u32 bit_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) /* enum construction APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) LIBBPF_API int btf__add_enum(struct btf *btf, const char *name, __u32 bytes_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) LIBBPF_API int btf__add_enum_value(struct btf *btf, const char *name, __s64 value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) enum btf_fwd_kind {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) BTF_FWD_STRUCT = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) BTF_FWD_UNION = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) BTF_FWD_ENUM = 2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) LIBBPF_API int btf__add_fwd(struct btf *btf, const char *name, enum btf_fwd_kind fwd_kind);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) LIBBPF_API int btf__add_typedef(struct btf *btf, const char *name, int ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) LIBBPF_API int btf__add_volatile(struct btf *btf, int ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) LIBBPF_API int btf__add_const(struct btf *btf, int ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) LIBBPF_API int btf__add_restrict(struct btf *btf, int ref_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /* func and func_proto construction APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) LIBBPF_API int btf__add_func(struct btf *btf, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) enum btf_func_linkage linkage, int proto_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) LIBBPF_API int btf__add_func_proto(struct btf *btf, int ret_type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) LIBBPF_API int btf__add_func_param(struct btf *btf, const char *name, int type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) /* var & datasec construction APIs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) LIBBPF_API int btf__add_var(struct btf *btf, const char *name, int linkage, int type_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) LIBBPF_API int btf__add_datasec(struct btf *btf, const char *name, __u32 byte_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) LIBBPF_API int btf__add_datasec_var_info(struct btf *btf, int var_type_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) __u32 offset, __u32 byte_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct btf_dedup_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unsigned int dedup_table_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bool dont_resolve_fwds;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) LIBBPF_API int btf__dedup(struct btf *btf, struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) const struct btf_dedup_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct btf_dump;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct btf_dump_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) void *ctx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) typedef void (*btf_dump_printf_fn_t)(void *ctx, const char *fmt, va_list args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) LIBBPF_API struct btf_dump *btf_dump__new(const struct btf *btf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) const struct btf_ext *btf_ext,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) const struct btf_dump_opts *opts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) btf_dump_printf_fn_t printf_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) LIBBPF_API void btf_dump__free(struct btf_dump *d);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) struct btf_dump_emit_type_decl_opts {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) /* size of this struct, for forward/backward compatiblity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) size_t sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* optional field name for type declaration, e.g.:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * - struct my_struct <FNAME>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * - void (*<FNAME>)(int)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * - char (*<FNAME>)[123]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) const char *field_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* extra indentation level (in number of tabs) to emit for multi-line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) * type declarations (e.g., anonymous struct); applies for lines
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) * starting from the second one (first line is assumed to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) * necessary indentation already
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int indent_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) /* strip all the const/volatile/restrict mods */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) bool strip_mods;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) size_t :0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #define btf_dump_emit_type_decl_opts__last_field strip_mods
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) LIBBPF_API int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) btf_dump__emit_type_decl(struct btf_dump *d, __u32 id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) const struct btf_dump_emit_type_decl_opts *opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * A set of helpers for easier BTF types handling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) static inline __u16 btf_kind(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return BTF_INFO_KIND(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) static inline __u16 btf_vlen(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return BTF_INFO_VLEN(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static inline bool btf_kflag(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return BTF_INFO_KFLAG(t->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static inline bool btf_is_void(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return btf_kind(t) == BTF_KIND_UNKN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static inline bool btf_is_int(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) return btf_kind(t) == BTF_KIND_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static inline bool btf_is_ptr(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) return btf_kind(t) == BTF_KIND_PTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) static inline bool btf_is_array(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return btf_kind(t) == BTF_KIND_ARRAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static inline bool btf_is_struct(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return btf_kind(t) == BTF_KIND_STRUCT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static inline bool btf_is_union(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) return btf_kind(t) == BTF_KIND_UNION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static inline bool btf_is_composite(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) __u16 kind = btf_kind(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static inline bool btf_is_enum(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return btf_kind(t) == BTF_KIND_ENUM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) static inline bool btf_is_fwd(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return btf_kind(t) == BTF_KIND_FWD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static inline bool btf_is_typedef(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return btf_kind(t) == BTF_KIND_TYPEDEF;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) static inline bool btf_is_volatile(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return btf_kind(t) == BTF_KIND_VOLATILE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) static inline bool btf_is_const(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return btf_kind(t) == BTF_KIND_CONST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) static inline bool btf_is_restrict(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return btf_kind(t) == BTF_KIND_RESTRICT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) static inline bool btf_is_mod(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) __u16 kind = btf_kind(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return kind == BTF_KIND_VOLATILE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) kind == BTF_KIND_CONST ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) kind == BTF_KIND_RESTRICT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) static inline bool btf_is_func(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) return btf_kind(t) == BTF_KIND_FUNC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) static inline bool btf_is_func_proto(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return btf_kind(t) == BTF_KIND_FUNC_PROTO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static inline bool btf_is_var(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) return btf_kind(t) == BTF_KIND_VAR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static inline bool btf_is_datasec(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) return btf_kind(t) == BTF_KIND_DATASEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static inline __u8 btf_int_encoding(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) return BTF_INT_ENCODING(*(__u32 *)(t + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static inline __u8 btf_int_offset(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) return BTF_INT_OFFSET(*(__u32 *)(t + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) static inline __u8 btf_int_bits(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return BTF_INT_BITS(*(__u32 *)(t + 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static inline struct btf_array *btf_array(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) return (struct btf_array *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) static inline struct btf_enum *btf_enum(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return (struct btf_enum *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static inline struct btf_member *btf_members(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) return (struct btf_member *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) /* Get bit offset of a member with specified index. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) static inline __u32 btf_member_bit_offset(const struct btf_type *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) __u32 member_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) const struct btf_member *m = btf_members(t) + member_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) bool kflag = btf_kflag(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) * BTF_KIND_UNION. If member is not a bitfield, zero is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) static inline __u32 btf_member_bitfield_size(const struct btf_type *t,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) __u32 member_idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) const struct btf_member *m = btf_members(t) + member_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) bool kflag = btf_kflag(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static inline struct btf_param *btf_params(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) return (struct btf_param *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) static inline struct btf_var *btf_var(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return (struct btf_var *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static inline struct btf_var_secinfo *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) btf_var_secinfos(const struct btf_type *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return (struct btf_var_secinfo *)(t + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) #ifdef __cplusplus
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) } /* extern "C" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) #endif /* __LIBBPF_BTF_H */