/* Copyright (c) 2024-present, Alessandro Gario All rights reserved. This source code is licensed in accordance with the terms specified in the LICENSE file found in the root directory of this source tree. */ use std::{env, fs::File, os::unix::fs::FileExt, path::Path}; use btfparse::{Readable, Result as BTFResult, TypeInformation}; struct ReadableFile { file: File, } impl ReadableFile { fn new(path: &Path) -> Self { ReadableFile { file: File::open(path).unwrap(), } } } impl Readable for ReadableFile { fn read(&self, offset: u64, buffer: &mut [u8]) -> BTFResult<()> { self.file .read_exact_at(buffer, offset) .map_err(|err| err.into()) } } fn main() { let argument_list: Vec = env::args().collect(); if argument_list.len() != 4 { println!("Usage:\n\tget-type-offset /path/to/btf/file \n"); return; } let btf_file_path = Path::new(&argument_list[1]); let btf_type_name = &argument_list[2]; let type_path = &argument_list[3]; println!("Opening BTF file: {:?}", btf_file_path); let vmlinux_btf_file = ReadableFile::new(btf_file_path); let type_information = TypeInformation::new(&vmlinux_btf_file).unwrap(); let offset = type_information .offset_of(type_information.id_of(btf_type_name).unwrap(), type_path) .unwrap(); println!("{} => {}: {:?}", btf_type_name, type_path, offset); }