Skip to content

libbpf-rs: optionally return full vecs from query #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/bpf_query/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ edition = "2021"
libbpf-rs = { path = "../../libbpf-rs" }
nix = { version = "0.26", default-features = false, features = ["net", "user"] }
clap = { version = "4.0.32", default-features = false, features = ["std", "derive", "help", "usage"] }

[target.'cfg(target_arch = "x86_64")'.dependencies]
iced-x86 = "1.20.0"
38 changes: 33 additions & 5 deletions examples/bpf_query/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ use clap::Parser;
use libbpf_rs::query;
use nix::unistd::Uid;

#[derive(Debug, Parser)]
struct ProgArgs {
#[arg(short, long)]
disassemble: bool,
}

/// Query the system about BPF-related information
#[derive(Debug, Parser)]
enum Command {
/// Display information about progs
Prog,
Prog(ProgArgs),
/// Display information about maps
Map,
/// Display information about BTF
Expand All @@ -17,12 +23,34 @@ enum Command {
Link,
}

fn prog() {
for prog in query::ProgInfoIter::default() {
fn prog(args: ProgArgs) {
let opts = query::ProgInfoQueryOptions::default().include_all();
for prog in query::ProgInfoIter::with_query_opts(opts) {
println!(
"name={:<16} type={:<15} run_count={:<2} runtime_ns={}",
prog.name, prog.ty, prog.run_cnt, prog.run_time_ns
);
if args.disassemble {
#[cfg(target_arch = "x86_64")]
{
use iced_x86::Formatter;

let mut d = iced_x86::Decoder::new(32, &prog.jited_prog_insns, 0);
let mut f = iced_x86::GasFormatter::new();
while d.can_decode() {
let ip = d.ip();
let insn = d.decode();
let mut f_insn = String::new();
f.format(&insn, &mut f_insn);
println!(" {}: {}", ip, f_insn);
}
}

#[cfg(not(target_arch = "x86_64"))]
{
println!(" Unable to disassemble on non-x86_64");
}
}
}
}

Expand All @@ -34,7 +62,7 @@ fn map() {

fn btf() {
for btf in query::BtfInfoIter::default() {
println!("id={:4} size={}", btf.id, btf.btf_size);
println!("id={:4} name={} size={}", btf.id, btf.name, btf.btf.len());
}
}

Expand Down Expand Up @@ -65,7 +93,7 @@ fn main() {
let opts = Command::parse();

match opts {
Command::Prog => prog(),
Command::Prog(args) => prog(args),
Command::Map => map(),
Command::Btf => btf(),
Command::Link => link(),
Expand Down
Loading