|
5 | 5 | #define __LIBBPF_BTF_H
|
6 | 6 |
|
7 | 7 | #include <stdarg.h>
|
| 8 | +#include <linux/btf.h> |
8 | 9 | #include <linux/types.h>
|
9 | 10 |
|
10 | 11 | #ifdef __cplusplus
|
@@ -120,6 +121,183 @@ LIBBPF_API void btf_dump__free(struct btf_dump *d);
|
120 | 121 |
|
121 | 122 | LIBBPF_API int btf_dump__dump_type(struct btf_dump *d, __u32 id);
|
122 | 123 |
|
| 124 | +/* |
| 125 | + * A set of helpers for easier BTF types handling |
| 126 | + */ |
| 127 | +static inline __u16 btf_kind(const struct btf_type *t) |
| 128 | +{ |
| 129 | + return BTF_INFO_KIND(t->info); |
| 130 | +} |
| 131 | + |
| 132 | +static inline __u16 btf_vlen(const struct btf_type *t) |
| 133 | +{ |
| 134 | + return BTF_INFO_VLEN(t->info); |
| 135 | +} |
| 136 | + |
| 137 | +static inline bool btf_kflag(const struct btf_type *t) |
| 138 | +{ |
| 139 | + return BTF_INFO_KFLAG(t->info); |
| 140 | +} |
| 141 | + |
| 142 | +static inline bool btf_is_int(const struct btf_type *t) |
| 143 | +{ |
| 144 | + return btf_kind(t) == BTF_KIND_INT; |
| 145 | +} |
| 146 | + |
| 147 | +static inline bool btf_is_ptr(const struct btf_type *t) |
| 148 | +{ |
| 149 | + return btf_kind(t) == BTF_KIND_PTR; |
| 150 | +} |
| 151 | + |
| 152 | +static inline bool btf_is_array(const struct btf_type *t) |
| 153 | +{ |
| 154 | + return btf_kind(t) == BTF_KIND_ARRAY; |
| 155 | +} |
| 156 | + |
| 157 | +static inline bool btf_is_struct(const struct btf_type *t) |
| 158 | +{ |
| 159 | + return btf_kind(t) == BTF_KIND_STRUCT; |
| 160 | +} |
| 161 | + |
| 162 | +static inline bool btf_is_union(const struct btf_type *t) |
| 163 | +{ |
| 164 | + return btf_kind(t) == BTF_KIND_UNION; |
| 165 | +} |
| 166 | + |
| 167 | +static inline bool btf_is_composite(const struct btf_type *t) |
| 168 | +{ |
| 169 | + __u16 kind = btf_kind(t); |
| 170 | + |
| 171 | + return kind == BTF_KIND_STRUCT || kind == BTF_KIND_UNION; |
| 172 | +} |
| 173 | + |
| 174 | +static inline bool btf_is_enum(const struct btf_type *t) |
| 175 | +{ |
| 176 | + return btf_kind(t) == BTF_KIND_ENUM; |
| 177 | +} |
| 178 | + |
| 179 | +static inline bool btf_is_fwd(const struct btf_type *t) |
| 180 | +{ |
| 181 | + return btf_kind(t) == BTF_KIND_FWD; |
| 182 | +} |
| 183 | + |
| 184 | +static inline bool btf_is_typedef(const struct btf_type *t) |
| 185 | +{ |
| 186 | + return btf_kind(t) == BTF_KIND_TYPEDEF; |
| 187 | +} |
| 188 | + |
| 189 | +static inline bool btf_is_volatile(const struct btf_type *t) |
| 190 | +{ |
| 191 | + return btf_kind(t) == BTF_KIND_VOLATILE; |
| 192 | +} |
| 193 | + |
| 194 | +static inline bool btf_is_const(const struct btf_type *t) |
| 195 | +{ |
| 196 | + return btf_kind(t) == BTF_KIND_CONST; |
| 197 | +} |
| 198 | + |
| 199 | +static inline bool btf_is_restrict(const struct btf_type *t) |
| 200 | +{ |
| 201 | + return btf_kind(t) == BTF_KIND_RESTRICT; |
| 202 | +} |
| 203 | + |
| 204 | +static inline bool btf_is_mod(const struct btf_type *t) |
| 205 | +{ |
| 206 | + __u16 kind = btf_kind(t); |
| 207 | + |
| 208 | + return kind == BTF_KIND_VOLATILE || |
| 209 | + kind == BTF_KIND_CONST || |
| 210 | + kind == BTF_KIND_RESTRICT; |
| 211 | +} |
| 212 | + |
| 213 | +static inline bool btf_is_func(const struct btf_type *t) |
| 214 | +{ |
| 215 | + return btf_kind(t) == BTF_KIND_FUNC; |
| 216 | +} |
| 217 | + |
| 218 | +static inline bool btf_is_func_proto(const struct btf_type *t) |
| 219 | +{ |
| 220 | + return btf_kind(t) == BTF_KIND_FUNC_PROTO; |
| 221 | +} |
| 222 | + |
| 223 | +static inline bool btf_is_var(const struct btf_type *t) |
| 224 | +{ |
| 225 | + return btf_kind(t) == BTF_KIND_VAR; |
| 226 | +} |
| 227 | + |
| 228 | +static inline bool btf_is_datasec(const struct btf_type *t) |
| 229 | +{ |
| 230 | + return btf_kind(t) == BTF_KIND_DATASEC; |
| 231 | +} |
| 232 | + |
| 233 | +static inline __u8 btf_int_encoding(const struct btf_type *t) |
| 234 | +{ |
| 235 | + return BTF_INT_ENCODING(*(__u32 *)(t + 1)); |
| 236 | +} |
| 237 | + |
| 238 | +static inline __u8 btf_int_offset(const struct btf_type *t) |
| 239 | +{ |
| 240 | + return BTF_INT_OFFSET(*(__u32 *)(t + 1)); |
| 241 | +} |
| 242 | + |
| 243 | +static inline __u8 btf_int_bits(const struct btf_type *t) |
| 244 | +{ |
| 245 | + return BTF_INT_BITS(*(__u32 *)(t + 1)); |
| 246 | +} |
| 247 | + |
| 248 | +static inline struct btf_array *btf_array(const struct btf_type *t) |
| 249 | +{ |
| 250 | + return (struct btf_array *)(t + 1); |
| 251 | +} |
| 252 | + |
| 253 | +static inline struct btf_enum *btf_enum(const struct btf_type *t) |
| 254 | +{ |
| 255 | + return (struct btf_enum *)(t + 1); |
| 256 | +} |
| 257 | + |
| 258 | +static inline struct btf_member *btf_members(const struct btf_type *t) |
| 259 | +{ |
| 260 | + return (struct btf_member *)(t + 1); |
| 261 | +} |
| 262 | + |
| 263 | +/* Get bit offset of a member with specified index. */ |
| 264 | +static inline __u32 btf_member_bit_offset(const struct btf_type *t, |
| 265 | + __u32 member_idx) |
| 266 | +{ |
| 267 | + const struct btf_member *m = btf_members(t) + member_idx; |
| 268 | + bool kflag = btf_kflag(t); |
| 269 | + |
| 270 | + return kflag ? BTF_MEMBER_BIT_OFFSET(m->offset) : m->offset; |
| 271 | +} |
| 272 | +/* |
| 273 | + * Get bitfield size of a member, assuming t is BTF_KIND_STRUCT or |
| 274 | + * BTF_KIND_UNION. If member is not a bitfield, zero is returned. |
| 275 | + */ |
| 276 | +static inline __u32 btf_member_bitfield_size(const struct btf_type *t, |
| 277 | + __u32 member_idx) |
| 278 | +{ |
| 279 | + const struct btf_member *m = btf_members(t) + member_idx; |
| 280 | + bool kflag = btf_kflag(t); |
| 281 | + |
| 282 | + return kflag ? BTF_MEMBER_BITFIELD_SIZE(m->offset) : 0; |
| 283 | +} |
| 284 | + |
| 285 | +static inline struct btf_param *btf_params(const struct btf_type *t) |
| 286 | +{ |
| 287 | + return (struct btf_param *)(t + 1); |
| 288 | +} |
| 289 | + |
| 290 | +static inline struct btf_var *btf_var(const struct btf_type *t) |
| 291 | +{ |
| 292 | + return (struct btf_var *)(t + 1); |
| 293 | +} |
| 294 | + |
| 295 | +static inline struct btf_var_secinfo * |
| 296 | +btf_var_secinfos(const struct btf_type *t) |
| 297 | +{ |
| 298 | + return (struct btf_var_secinfo *)(t + 1); |
| 299 | +} |
| 300 | + |
123 | 301 | #ifdef __cplusplus
|
124 | 302 | } /* extern "C" */
|
125 | 303 | #endif
|
|
0 commit comments