Struct TypeDatabase

Source
pub struct TypeDatabase { /* private fields */ }
Expand description

Holds type information.

Implementations§

Source§

impl TypeDatabase

Source

pub fn add_btf_types(&mut self, btf: &Btf) -> Result<()>

Adds a parsed list of BTF types to this type database.

§Arguments
  • name - The optional name of the type.
  • btf - The BTF types.
§Example
use bpf_script::types::TypeDatabase;
use btf::Btf;

let btf = Btf::from_file("/sys/kernel/btf/vmlinux").expect("Failed to parse vmlinux btf");
let mut database = TypeDatabase::default();

database
    .add_btf_types(&btf)
    .expect("failed to add btf types");
database
    .get_type_by_name("task_struct")
    .expect("Couldn't find task_struct");
Source§

impl TypeDatabase

Source

pub fn add_type(&mut self, name: Option<&str>, ty: &Type) -> Result<usize>

Adds a type to the type database.

§Arguments
  • name - The name of the type.
  • ty - The type to add.
Source

pub fn get_type_by_name(&self, name: &str) -> Option<&Type>

Finds a type in the database by name.

§Arguments
  • name - The name of the type.
Source

pub fn get_type_by_id(&self, id: usize) -> Option<&Type>

Finds a type in the database by id.

§Arguments
  • id - The id of the type.
Source

pub fn get_type_id_by_name(&self, name: &str) -> Option<usize>

Gets a type’s id by its name.

§Arguments
  • name - The name of the type.
Source

pub fn add_integer( &mut self, name: Option<&str>, bytes: u32, is_signed: bool, ) -> Result<usize>

Convenience function for adding an integer type to the database.

§Arguments
  • name - The name of the type.
  • bytes - The number of bits
  • is_signed - If the integer is signed.
Examples found in repository?
examples/print-instructions.rs (line 16)
4fn main() {
5    let prog = r#"
6            fn(vec: &iovec)
7              vec_copy: iovec = 0
8              vec_copy.iov_base = vec.iov_base
9              vec_copy.iov_len = vec.iov_len
10              return 50
11        "#;
12
13    let mut database = TypeDatabase::default();
14
15    let u64id = database
16        .add_integer(Some("__u64"), 8, false)
17        .expect("Failed to add type.");
18
19    let iov_base = Field {
20        offset: 0,
21        type_id: u64id,
22    };
23
24    let iov_len = Field {
25        offset: 64,
26        type_id: u64id,
27    };
28
29    database
30        .add_struct(
31            Some("iovec"),
32            &[("iov_base", iov_base), ("iov_len", iov_len)],
33        )
34        .expect("Failed to add type.");
35    let mut compiler = Compiler::create(&database);
36    compiler.compile(prog).unwrap();
37
38    for ins in compiler.get_instructions() {
39        println!("{}", ins);
40    }
41}
Source

pub fn add_float(&mut self, name: Option<&str>, bits: u32) -> Result<usize>

Convenience function for adding a float type to the database.

§Arguments
  • name - The name of the type.
  • bits - The number of bits.
Source

pub fn add_array( &mut self, name: Option<&str>, element_type_id: usize, num_elements: u32, ) -> Result<usize>

Convenience function for adding an array to the database.

§Arguments
  • name - The name of the type.
  • element_type_id - The type id of the element.
  • num_elements - The number of elements in the array.
Source

pub fn add_struct( &mut self, name: Option<&str>, fields: &[(&str, Field)], ) -> Result<usize>

Convenience function for adding a struct to the database.

§Arguments
  • name - The name of the type.
  • fields - The fields to add.
Examples found in repository?
examples/print-instructions.rs (lines 30-33)
4fn main() {
5    let prog = r#"
6            fn(vec: &iovec)
7              vec_copy: iovec = 0
8              vec_copy.iov_base = vec.iov_base
9              vec_copy.iov_len = vec.iov_len
10              return 50
11        "#;
12
13    let mut database = TypeDatabase::default();
14
15    let u64id = database
16        .add_integer(Some("__u64"), 8, false)
17        .expect("Failed to add type.");
18
19    let iov_base = Field {
20        offset: 0,
21        type_id: u64id,
22    };
23
24    let iov_len = Field {
25        offset: 64,
26        type_id: u64id,
27    };
28
29    database
30        .add_struct(
31            Some("iovec"),
32            &[("iov_base", iov_base), ("iov_len", iov_len)],
33        )
34        .expect("Failed to add type.");
35    let mut compiler = Compiler::create(&database);
36    compiler.compile(prog).unwrap();
37
38    for ins in compiler.get_instructions() {
39        println!("{}", ins);
40    }
41}
Source

pub fn add_struct_by_ids( &mut self, name: Option<&str>, fields: &[(&str, usize)], ) -> Result<usize>

Convenience function for adding a struct to the database using a slice of (field_name, type_id). Types are added in order, and packed together contiguously.

§Arguments
  • name - The name of the type.
  • fields - The fields to add (by id).
Source

pub fn add_struct_by_names( &mut self, name: Option<&str>, fields: &[(&str, &str)], ) -> Result<usize>

Convenience function for adding a struct to the database using a slice of (field_name, type_name). Types are added in order, and packed together contiguously.

§Arguments
  • name - The name of the type.
  • fields - The fields to add (by name).

Trait Implementations§

Source§

impl Clone for TypeDatabase

Source§

fn clone(&self) -> TypeDatabase

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TypeDatabase

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TypeDatabase

Source§

fn default() -> TypeDatabase

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.