1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
//! Wrappers representing concrete btf types.
use num_enum::IntoPrimitive;
use num_enum::TryFromPrimitive;
use super::BtfKind;
use super::BtfType;
use super::HasSize;
use super::ReferencesType;
use super::TypeId;
use std::ffi::CStr;
use std::fmt;
use std::fmt::Display;
use std::ops::Deref;
// Generate a btf type that doesn't have any fields, i.e. there is no data after the BtfType
// pointer.
macro_rules! gen_fieldless_concrete_type {
(
$(#[$docs:meta])*
$name:ident $(with $trait:ident)?
) => {
$(#[$docs])*
#[derive(Debug)]
pub struct $name<'btf> {
source: BtfType<'btf>,
}
impl<'btf> TryFrom<BtfType<'btf>> for $name<'btf> {
type Error = BtfType<'btf>;
fn try_from(t: BtfType<'btf>) -> ::core::result::Result<Self, Self::Error> {
if t.kind() == BtfKind::$name {
Ok($name { source: t })
} else {
Err(t)
}
}
}
impl<'btf> ::std::ops::Deref for $name<'btf> {
type Target = BtfType<'btf>;
fn deref(&self) -> &Self::Target {
&self.source
}
}
$(
impl super::sealed::Sealed for $name<'_> {}
unsafe impl<'btf> $trait<'btf> for $name<'btf> {}
)*
};
}
// Generate a btf type that has at least one field, and as such, there is data following the
// btf_type pointer.
macro_rules! gen_concrete_type {
(
$(#[$docs:meta])*
$libbpf_ty:ident as $name:ident $(with $trait:ident)?
) => {
$(#[$docs])*
#[derive(Debug)]
pub struct $name<'btf> {
source: BtfType<'btf>,
ptr: &'btf libbpf_sys::$libbpf_ty,
}
impl<'btf> TryFrom<BtfType<'btf>> for $name<'btf> {
type Error = BtfType<'btf>;
fn try_from(t: BtfType<'btf>) -> ::core::result::Result<Self, Self::Error> {
if t.kind() == BtfKind::$name {
let ptr = unsafe {
// SAFETY:
//
// It's in bounds to access the memory following this btf_type
// because we've checked the type
(t.ty as *const libbpf_sys::btf_type).offset(1)
};
let ptr = ptr.cast::<libbpf_sys::$libbpf_ty>();
Ok($name {
source: t,
// SAFETY:
//
// This pointer is aligned.
// all fields of all struct have size and
// alignment of u32, if t.ty was aligned, then this must be as well
//
// It's initialized
// libbpf guarantees this since we've checked the type
//
// The lifetime will match the lifetime of the original t.ty reference.
ptr: unsafe { &*ptr },
})
} else {
Err(t)
}
}
}
impl<'btf> ::std::ops::Deref for $name<'btf> {
type Target = BtfType<'btf>;
fn deref(&self) -> &Self::Target {
&self.source
}
}
$(
impl super::sealed::Sealed for $name<'_> {}
unsafe impl<'btf> $trait<'btf> for $name<'btf> {}
)*
};
}
macro_rules! gen_collection_members_concrete_type {
(
$libbpf_ty:ident as $name:ident $(with $trait:ident)?;
$(#[$docs:meta])*
struct $member_name:ident $(<$lt:lifetime>)? {
$(
$(#[$field_docs:meta])*
pub $field:ident : $type:ty
),* $(,)?
}
|$btf:ident, $member:ident $(, $kind_flag:ident)?| $convert:expr
) => {
impl<'btf> ::std::ops::Deref for $name<'btf> {
type Target = BtfType<'btf>;
fn deref(&self) -> &Self::Target {
&self.source
}
}
impl<'btf> $name<'btf> {
/// Whether this type has no members
#[inline]
pub fn is_empty(&self) -> bool {
self.members.is_empty()
}
#[doc = ::core::concat!("How many members this [`", ::core::stringify!($name), "`] has")]
#[inline]
pub fn len(&self) -> usize {
self.members.len()
}
#[doc = ::core::concat!("Get a [`", ::core::stringify!($member_name), "`] at a given index")]
/// # Errors
///
/// This function returns [`None`] when the index is out of bounds.
pub fn get(&self, index: usize) -> Option<$member_name$(<$lt>)*> {
self.members.get(index).map(|m| self.c_to_rust_member(m))
}
#[doc = ::core::concat!("Returns an iterator over the [`", ::core::stringify!($member_name), "`]'s of the [`", ::core::stringify!($name), "`]")]
pub fn iter(&'btf self) -> impl ExactSizeIterator<Item = $member_name$(<$lt>)*> + 'btf {
self.members.iter().map(|m| self.c_to_rust_member(m))
}
fn c_to_rust_member(&self, member: &libbpf_sys::$libbpf_ty) -> $member_name$(<$lt>)* {
let $btf = self.source.source;
let $member = member;
$(let $kind_flag = self.source.kind_flag();)*
$convert
}
}
$(#[$docs])*
#[derive(Debug)]
pub struct $member_name $(<$lt>)? {
$(
$(#[$field_docs])*
pub $field: $type
),*
}
$(
impl $crate::btf::sealed::Sealed for $name<'_> {}
unsafe impl<'btf> $trait<'btf> for $name<'btf> {}
)*
};
}
macro_rules! gen_collection_concrete_type {
(
$(#[$docs:meta])*
$libbpf_ty:ident as $name:ident $(with $trait:ident)?;
$($rest:tt)+
) => {
$(#[$docs])*
#[derive(Debug)]
pub struct $name<'btf> {
source: BtfType<'btf>,
members: &'btf [libbpf_sys::$libbpf_ty],
}
impl<'btf> TryFrom<BtfType<'btf>> for $name<'btf> {
type Error = BtfType<'btf>;
fn try_from(t: BtfType<'btf>) -> ::core::result::Result<Self, Self::Error> {
if t.kind() == BtfKind::$name {
let base_ptr = unsafe {
// SAFETY:
//
// It's in bounds to access the memory following this btf_type
// because we've checked the type
(t.ty as *const libbpf_sys::btf_type).offset(1)
};
let members = unsafe {
// SAFETY:
//
// This pointer is aligned.
// all fields of all struct have size and
// alignment of u32, if t.ty was aligned, then this must be as well
//
// It's initialized
// libbpf guarantees this since we've checked the type
//
// The lifetime will match the lifetime of the original t.ty reference.
//
// The docs specify the length of the array is stored in vlen.
std::slice::from_raw_parts(base_ptr.cast(), t.vlen() as usize)
};
Ok(Self { source: t, members })
} else {
Err(t)
}
}
}
gen_collection_members_concrete_type!{
$libbpf_ty as $name $(with $trait)?;
$($rest)*
}
};
}
/// The attributes of a member.
#[derive(Debug)]
pub enum MemberAttr {
/// Member is a normal field.
Normal {
/// The offset of this member in the struct/union.
offset: u32,
},
/// Member is a bitfield.
BitField {
/// The size of the bitfield.
size: u8,
/// The offset of the bitfield.
offset: u32,
},
}
impl MemberAttr {
#[inline]
fn normal(offset: u32) -> Self {
Self::Normal { offset }
}
#[inline]
fn bif_field(offset: u32) -> Self {
Self::BitField {
size: (offset >> 24) as u8,
offset: offset & 0x00_ff_ff_ff,
}
}
}
/// The kind of linkage a variable of function can have.
#[derive(TryFromPrimitive, IntoPrimitive, Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u32)]
pub enum Linkage {
/// Static linkage
Static = 0,
/// Global linkage
Global,
/// External linkage
Extern,
/// Unknown
Unknown,
}
impl Display for Linkage {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}",
match self {
Linkage::Static => "static",
Linkage::Global => "global",
Linkage::Extern => "extern",
Linkage::Unknown => "(unknown)",
}
)
}
}
// Void
gen_fieldless_concrete_type! {
/// The representation of the c_void type.
Void
}
// Int
/// An integer.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-int)
#[derive(Debug)]
pub struct Int<'btf> {
source: BtfType<'btf>,
/// The encoding of the number.
pub encoding: IntEncoding,
/// The offset in bits where the value of this integer starts. Mostly usefull for bitfields in
/// structs.
pub offset: u8,
/// The number of bits in the int. (For example, an u8 has 8 bits).
pub bits: u8,
}
/// The kinds of ways a btf [Int] can be encoded.
#[derive(Debug)]
pub enum IntEncoding {
/// No encoding.
None,
/// Signed.
Signed,
/// It's a c_char.
Char,
/// It's a bool.
Bool,
}
impl<'btf> TryFrom<BtfType<'btf>> for Int<'btf> {
type Error = BtfType<'btf>;
fn try_from(t: BtfType<'btf>) -> std::result::Result<Self, Self::Error> {
if t.kind() == BtfKind::Int {
let int = {
let base_ptr = t.ty as *const libbpf_sys::btf_type;
let u32_ptr = unsafe {
// SAFETY:
//
// It's in bounds to access the memory following this btf_type
// because we've checked the type
base_ptr.offset(1).cast::<u32>()
};
unsafe {
// SAFETY:
//
// This pointer is aligned.
// all fields of all struct have size and
// alignment of u32, if t.ty was aligned, then this must be as well
//
// It's initialized
// libbpf guarantees this since we've checked the type
//
// The lifetime will match the lifetime of the original t.ty reference.
*u32_ptr
}
};
let encoding = match (int & 0x0f_00_00_00) >> 24 {
0b1 => IntEncoding::Signed,
0b10 => IntEncoding::Char,
0b100 => IntEncoding::Bool,
_ => IntEncoding::None,
};
Ok(Self {
source: t,
encoding,
offset: ((int & 0x00_ff_00_00) >> 24) as u8,
bits: (int & 0x00_00_00_ff) as u8,
})
} else {
Err(t)
}
}
}
impl<'btf> Deref for Int<'btf> {
type Target = BtfType<'btf>;
fn deref(&self) -> &Self::Target {
&self.source
}
}
// SAFETY: Int has the .size field set.
impl super::sealed::Sealed for Int<'_> {}
unsafe impl<'btf> HasSize<'btf> for Int<'btf> {}
// Ptr
gen_fieldless_concrete_type! {
/// A pointer.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-ptr)
Ptr with ReferencesType
}
// Array
gen_concrete_type! {
/// An array.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-array)
btf_array as Array
}
impl<'s> Array<'s> {
/// The type id of the stored type.
#[inline]
pub fn ty(&self) -> TypeId {
self.ptr.type_.into()
}
/// The type of index used.
#[inline]
pub fn index_ty(&self) -> TypeId {
self.ptr.index_type.into()
}
/// The capacity of the array.
#[inline]
pub fn capacity(&self) -> usize {
self.ptr.nelems as usize
}
/// The type contained in this array.
#[inline]
pub fn contained_type(&self) -> BtfType<'s> {
self.source
.source
.type_by_id(self.ty())
.expect("arrays should always reference an existing type")
}
}
// Struct
gen_collection_concrete_type! {
/// A struct.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-struct)
btf_member as Struct with HasSize;
/// A member of a [Struct]
struct StructMember<'btf> {
/// The member's name
pub name: Option<&'btf CStr>,
/// The member's type
pub ty: TypeId,
/// The attributes of this member.
pub attr: MemberAttr,
}
|btf, member, kflag| StructMember {
name: btf.name_at(member.name_off),
ty: member.type_.into(),
attr: if kflag {
MemberAttr::bif_field(member.offset)
} else {
MemberAttr::normal(member.offset)
},
}
}
// Union
gen_collection_concrete_type! {
/// A Union.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-union)
btf_member as Union with HasSize;
/// A member of an [Union]
struct UnionMember<'btf> {
/// The member's name
pub name: Option<&'btf CStr>,
/// The member's type
pub ty: TypeId,
/// The attributes of this member.
pub attr: MemberAttr,
}
|btf, member, kflag| UnionMember {
name: btf.name_at(member.name_off),
ty: member.type_.into(),
attr: if kflag {
MemberAttr::bif_field(member.offset)
} else {
MemberAttr::normal(member.offset)
},
}
}
/// A Composite type, which can be one of a [`Struct`] or a [`Union`].
///
/// Sometimes it's not usefull to distinguish them, in that case, one can use this
/// type to inspect any of them.
#[derive(Debug)]
pub struct Composite<'btf> {
source: BtfType<'btf>,
/// Whether this type is a struct.
pub is_struct: bool,
members: &'btf [libbpf_sys::btf_member],
}
impl<'btf> From<Struct<'btf>> for Composite<'btf> {
fn from(s: Struct<'btf>) -> Self {
Self {
source: s.source,
is_struct: true,
members: s.members,
}
}
}
impl<'btf> From<Union<'btf>> for Composite<'btf> {
fn from(s: Union<'btf>) -> Self {
Self {
source: s.source,
is_struct: false,
members: s.members,
}
}
}
impl<'btf> TryFrom<BtfType<'btf>> for Composite<'btf> {
type Error = BtfType<'btf>;
fn try_from(t: BtfType<'btf>) -> Result<Self, Self::Error> {
Struct::try_from(t)
.map(Self::from)
.or_else(|_| Union::try_from(t).map(Self::from))
}
}
impl<'btf> TryFrom<Composite<'btf>> for Struct<'btf> {
type Error = Composite<'btf>;
fn try_from(value: Composite<'btf>) -> Result<Self, Self::Error> {
if value.is_struct {
Ok(Self {
source: value.source,
members: value.members,
})
} else {
Err(value)
}
}
}
impl<'btf> TryFrom<Composite<'btf>> for Union<'btf> {
type Error = Composite<'btf>;
fn try_from(value: Composite<'btf>) -> Result<Self, Self::Error> {
if !value.is_struct {
Ok(Self {
source: value.source,
members: value.members,
})
} else {
Err(value)
}
}
}
// Composite
gen_collection_members_concrete_type! {
btf_member as Composite with HasSize;
/// A member of a [Struct]
struct CompositeMember<'btf> {
/// The member's name
pub name: Option<&'btf CStr>,
/// The member's type
pub ty: TypeId,
/// If this member is a bifield, these are it's attributes.
pub attr: MemberAttr
}
|btf, member, kflag| CompositeMember {
name: btf.name_at(member.name_off),
ty: member.type_.into(),
attr: if kflag {
MemberAttr::bif_field(member.offset)
} else {
MemberAttr::normal(member.offset)
},
}
}
// Enum
gen_collection_concrete_type! {
/// An Enum of at most 32 bits.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-enum)
btf_enum as Enum with HasSize;
/// A member of an [Enum]
struct EnumMember<'btf> {
/// The name of this enum variant.
pub name: Option<&'btf CStr>,
/// The numeric value of this enum variant.
pub value: i32,
}
|btf, member| EnumMember {
name: btf.name_at(member.name_off),
value: member.val,
}
}
// Fwd
gen_fieldless_concrete_type! {
/// A forward declared C type.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-fwd)
Fwd
}
impl Fwd<'_> {
/// The kind of C type that is forwardly declared.
pub fn kind(&self) -> FwdKind {
if self.source.kind_flag() {
FwdKind::Union
} else {
FwdKind::Struct
}
}
}
/// The kinds of types that can be forward declared.
#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
pub enum FwdKind {
/// A struct.
Struct,
/// A union.
Union,
}
// Typedef
gen_fieldless_concrete_type! {
/// A C typedef.
///
/// References the original type.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-typedef)
Typedef with ReferencesType
}
// Volatile
gen_fieldless_concrete_type! {
/// The volatile modifier.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-volatile)
Volatile with ReferencesType
}
// Const
gen_fieldless_concrete_type! {
/// The const modifier.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-const)
Const with ReferencesType
}
// Restrict
gen_fieldless_concrete_type! {
/// The restrict modifier.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-restrict)
Restrict with ReferencesType
}
// Func
gen_fieldless_concrete_type! {
/// A function.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-func)
Func with ReferencesType
}
impl Func<'_> {
/// This function's linkage.
#[inline]
pub fn linkage(&self) -> Linkage {
self.source.vlen().try_into().unwrap_or(Linkage::Unknown)
}
}
// FuncProto
gen_collection_concrete_type! {
/// A function prototype.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-func-proto)
btf_param as FuncProto with ReferencesType;
/// A parameter of a [FuncProto].
struct FuncProtoParam<'btf> {
/// The parameter's name
pub name: Option<&'btf CStr>,
/// The parameter's type
pub ty: TypeId,
}
|btf, member| FuncProtoParam {
name: btf.name_at(member.name_off),
ty: member.type_.into()
}
}
// Var
gen_concrete_type! {
/// A global variable.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-var)
btf_var as Var with ReferencesType
}
impl Var<'_> {
/// The kind of linkage this variable has.
#[inline]
pub fn linkage(&self) -> Linkage {
self.ptr.linkage.try_into().unwrap_or(Linkage::Unknown)
}
}
// DataSec
gen_collection_concrete_type! {
/// An ELF's data section, such as `.data`, `.bss` or `.rodata`.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-datasec)
btf_var_secinfo as DataSec with HasSize;
/// Describes the btf var in a section.
///
/// See [`DataSec`].
struct VarSecInfo {
/// The type id of the var
pub ty: TypeId,
/// The offset in the section
pub offset: u32,
/// The size of the type.
pub size: usize,
}
|_btf, member| VarSecInfo {
ty: member.type_.into(),
offset: member.offset,
size: member.size as usize
}
}
// Float
gen_fieldless_concrete_type! {
/// A floating point number.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-float)
Float with HasSize
}
// DeclTag
gen_concrete_type! {
/// A declaration tag.
///
/// A custom tag the programmer can attach to a symbol.
///
/// See the [clang docs](https://clang.llvm.org/docs/AttributeReference.html#btf-decl-tag) on
/// it.
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-decl-tag)
btf_decl_tag as DeclTag with ReferencesType
}
impl DeclTag<'_> {
/// The component index is present only when the tag points to a struct/union member or a
/// function argument.
/// And component_idx indicates which member or argument, this decl tag refers to.
#[inline]
pub fn component_index(&self) -> Option<u32> {
self.ptr.component_idx.try_into().ok()
}
}
// TypeTag
gen_fieldless_concrete_type! {
/// A type tag.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-type-tag)
TypeTag with ReferencesType
}
// Enum64
gen_collection_concrete_type! {
/// An Enum of 64 bits.
///
/// See also [libbpf docs](https://www.kernel.org/doc/html/latest/bpf/btf.html#btf-kind-enum64)
btf_enum64 as Enum64 with HasSize;
/// A member of an [Enum64].
struct Enum64Member<'btf> {
/// The name of this enum variant.
pub name: Option<&'btf CStr>,
/// The numeric value of this enum variant.
pub value: u64,
}
|btf, member| Enum64Member {
name: btf.name_at(member.name_off),
value: {
let hi: u64 = member.val_hi32.into();
let lo: u64 = member.val_lo32.into();
hi << 32 | lo
},
}
}
/// A macro that allows matching on the type of a [`BtfType`] as if it was an enum.
///
/// Each pattern can be of two types.
///
/// ```no_run
/// use libbpf_rs::btf::BtfType;
/// use libbpf_rs::btf_type_match;
///
/// # fn do_something_with_an_int(i: libbpf_rs::btf::types::Int) -> &'static str { "" }
///
/// let ty: BtfType;
/// # ty = todo!();
/// btf_type_match!(match ty {
/// BtfKind::Int(i) => do_something_with_an_int(i),
/// BtfKind::Struct => "it's a struct",
/// // other fields
/// # // we have to include these here otherwise the tests fail saying this doesn't compile
/// # BtfKind::Void => "",
/// # BtfKind::Int => "",
/// # BtfKind::Ptr => "",
/// # BtfKind::Array => "",
/// # BtfKind::Struct => "",
/// # BtfKind::Union => "",
/// # BtfKind::Enum => "",
/// # BtfKind::Fwd => "",
/// # BtfKind::Typedef => "",
/// # BtfKind::Volatile => "",
/// # BtfKind::Const => "",
/// # BtfKind::Restrict => "",
/// # BtfKind::Func => "",
/// # BtfKind::FuncProto => "",
/// # BtfKind::Var => "",
/// # BtfKind::DataSec => "",
/// # BtfKind::Float => "",
/// # BtfKind::DeclTag => "",
/// # BtfKind::TypeTag => "",
/// # BtfKind::Enum64 => "",
/// });
/// ```
///
/// Variable Binding.
///
/// ```compile_fail
/// BtfKind::Int(i) => { /* we can use i here and it will be an `Int` */ }
/// ```
///
/// NonBinding.
/// ```compile_fail
/// BtfKind::Int => {
/// /* we don't have access to the variable, but we know the scrutinee is an Int */
/// }
/// ```
#[macro_export]
macro_rules! btf_type_match {
(
match $ty:ident {
$(BtfKind::$name:ident $(($var:ident))? => $action:expr $(,)?)+
}
) => {{
let ty: $crate::btf::BtfType = $ty;
match ty.kind() {
$(
$crate::btf::BtfKind::$name => {
$(let $var = $crate::btf::types::$name::try_from(ty).unwrap();)*
$action
}
)+
}
}}
}