pub struct TypeDatabase { /* private fields */ }
Expand description
Holds type information.
Implementations§
Source§impl TypeDatabase
impl TypeDatabase
Sourcepub fn add_btf_types(&mut self, btf: &Btf) -> Result<()>
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
impl TypeDatabase
Sourcepub fn get_type_by_name(&self, name: &str) -> Option<&Type>
pub fn get_type_by_name(&self, name: &str) -> Option<&Type>
Sourcepub fn get_type_by_id(&self, id: usize) -> Option<&Type>
pub fn get_type_by_id(&self, id: usize) -> Option<&Type>
Sourcepub fn get_type_id_by_name(&self, name: &str) -> Option<usize>
pub fn get_type_id_by_name(&self, name: &str) -> Option<usize>
Sourcepub fn add_integer(
&mut self,
name: Option<&str>,
bytes: u32,
is_signed: bool,
) -> Result<usize>
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 bitsis_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}
Sourcepub fn add_float(&mut self, name: Option<&str>, bits: u32) -> Result<usize>
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.
Sourcepub fn add_array(
&mut self,
name: Option<&str>,
element_type_id: usize,
num_elements: u32,
) -> Result<usize>
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.
Sourcepub fn add_struct(
&mut self,
name: Option<&str>,
fields: &[(&str, Field)],
) -> Result<usize>
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}
Sourcepub fn add_struct_by_ids(
&mut self,
name: Option<&str>,
fields: &[(&str, usize)],
) -> Result<usize>
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).
Sourcepub fn add_struct_by_names(
&mut self,
name: Option<&str>,
fields: &[(&str, &str)],
) -> Result<usize>
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
impl Clone for TypeDatabase
Source§fn clone(&self) -> TypeDatabase
fn clone(&self) -> TypeDatabase
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for TypeDatabase
impl Debug for TypeDatabase
Source§impl Default for TypeDatabase
impl Default for TypeDatabase
Source§fn default() -> TypeDatabase
fn default() -> TypeDatabase
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for TypeDatabase
impl RefUnwindSafe for TypeDatabase
impl Send for TypeDatabase
impl Sync for TypeDatabase
impl Unpin for TypeDatabase
impl UnwindSafe for TypeDatabase
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more