Skip to content

[RFC] Switched error handling to anyhow #81

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

Closed
wants to merge 4 commits into from
Closed
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ homepage = "https://github.com/rust-bpf/rust-bcc"
edition = '2018'

[dependencies]
anyhow = "1.0.28"
bcc-sys = "0.13.0"
byteorder = "1.3.4"
failure = "0.1.7"
Expand Down
5 changes: 2 additions & 3 deletions examples/biosnoop.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::Result;
use bcc::core::BPF;
use bcc::perf::init_perf_map;
use clap::{App, Arg};
use failure::Error;

use core::sync::atomic::{AtomicBool, Ordering};
use std::ptr;
Expand All @@ -24,7 +24,7 @@ struct data_t {
name: [u8; 16],
}

fn do_main(runnable: Arc<AtomicBool>) -> Result<(), Error> {
fn do_main(runnable: Arc<AtomicBool>) -> Result<()> {
let matches = App::new("biosnoop")
.about("Trace block I/O")
.arg(
Expand Down Expand Up @@ -134,7 +134,6 @@ fn main() {

if let Err(x) = do_main(runnable) {
eprintln!("Error: {}", x);
eprintln!("{}", x.backtrace());
std::process::exit(1);
}
}
5 changes: 2 additions & 3 deletions examples/opensnoop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ extern crate byteorder;
extern crate failure;
extern crate libc;

use anyhow::Result;
use bcc::core::BPF;
use bcc::perf::init_perf_map;
use clap::{App, Arg};
use failure::Error;

use core::sync::atomic::{AtomicBool, Ordering};
use std::ptr;
Expand Down Expand Up @@ -34,7 +34,7 @@ struct data_t {
fname: [u8; 255], // NAME_MAX
}

fn do_main(runnable: Arc<AtomicBool>) -> Result<(), Error> {
fn do_main(runnable: Arc<AtomicBool>) -> Result<()> {
let matches = App::new("opensnoop")
.about("Prints out filename + PID every time a file is opened")
.arg(
Expand Down Expand Up @@ -112,7 +112,6 @@ fn main() {
match do_main(runnable) {
Err(x) => {
eprintln!("Error: {}", x);
eprintln!("{}", x.backtrace());
std::process::exit(1);
}
_ => {}
Expand Down
5 changes: 2 additions & 3 deletions examples/runqlat.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use bcc::core::BPF;
use clap::{App, Arg};
use failure::Error;

use core::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -50,7 +50,7 @@ fn attach_events(bpf: &mut BPF) {
}
}

fn do_main(runnable: Arc<AtomicBool>) -> Result<(), Error> {
fn do_main(runnable: Arc<AtomicBool>) -> Result<()> {
let matches = App::new("runqlat")
.about("Reports distribution of scheduler latency")
.arg(
Expand Down Expand Up @@ -131,7 +131,6 @@ fn main() {
match do_main(runnable) {
Err(x) => {
eprintln!("Error: {}", x);
eprintln!("{}", x.backtrace());
std::process::exit(1);
}
_ => {}
Expand Down
5 changes: 2 additions & 3 deletions examples/softirqs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use bcc::core::BPF;
use clap::{App, Arg};
use failure::Error;

use core::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -73,7 +73,7 @@ impl fmt::Display for SoftIRQ {
}
}

fn do_main(runnable: Arc<AtomicBool>) -> Result<(), Error> {
fn do_main(runnable: Arc<AtomicBool>) -> Result<()> {
let matches = App::new("softirqs")
.about("Reports time spent in IRQ Handlers")
.arg(
Expand Down Expand Up @@ -169,7 +169,6 @@ fn main() {
match do_main(runnable) {
Err(x) => {
eprintln!("Error: {}", x);
eprintln!("{}", x.backtrace());
std::process::exit(1);
}
_ => {}
Expand Down
5 changes: 2 additions & 3 deletions examples/strlen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ extern crate byteorder;
extern crate failure;
extern crate libc;

use anyhow::Result;
use bcc::core::BPF;
use byteorder::{NativeEndian, ReadBytesExt};
use failure::Error;

use core::sync::atomic::{AtomicBool, Ordering};
use std::io::Cursor;
use std::sync::Arc;

fn do_main(runnable: Arc<AtomicBool>) -> Result<(), Error> {
fn do_main(runnable: Arc<AtomicBool>) -> Result<()> {
let code = "
#include <uapi/linux/ptrace.h>

Expand Down Expand Up @@ -75,7 +75,6 @@ fn main() {
match do_main(runnable) {
Err(x) => {
eprintln!("Error: {}", x);
eprintln!("{}", x.backtrace());
std::process::exit(1);
}
_ => {}
Expand Down
5 changes: 2 additions & 3 deletions examples/tcpretrans.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bcc::core::BPF;
extern crate chrono;
use anyhow::Result;
use chrono::Utc;
use clap::{App, Arg};
use failure::Error;

use core::sync::atomic::{AtomicBool, Ordering};
use std::net::Ipv4Addr;
Expand Down Expand Up @@ -38,7 +38,7 @@ struct ipv6_data_t {
type_: u64,
}

fn do_main(runnable: Arc<AtomicBool>) -> Result<(), Error> {
fn do_main(runnable: Arc<AtomicBool>) -> Result<()> {
let matches = App::new("biosnoop")
.arg(
Arg::with_name("duration")
Expand Down Expand Up @@ -131,7 +131,6 @@ fn main() {

if let Err(x) = do_main(runnable) {
eprintln!("Error: {}", x);
eprintln!("{}", x.backtrace());
std::process::exit(1);
}
}
18 changes: 9 additions & 9 deletions src/core/kprobe/v0_4_0.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{self, bail, Result};
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_ENTRY as BPF_PROBE_ENTRY;
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_RETURN as BPF_PROBE_RETURN;
use bcc_sys::bccapi::*;
use failure::*;

use crate::core::make_alphanumeric;
use crate::types::MutPointer;
Expand All @@ -23,9 +23,9 @@ pub struct Kprobe {
}

impl Kprobe {
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self, Error> {
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self> {
let cname =
CString::new(name).map_err(|_| format_err!("Nul byte in Kprobe name: {}", name))?;
CString::new(name).map_err(|_| anyhow::anyhow!("Nul byte in Kprobe name: {}", name))?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it looks like anyhow exports format_err!? If so, that could save a lot of lines in this diff by doing use anyhow::*;

let cfunction = CString::new(function)
.map_err(|_| format_err!("Nul byte in Kprobe function: {}", function))?;
let (pid, cpu, group_fd) = (-1, 0, -1);
Expand All @@ -43,7 +43,7 @@ impl Kprobe {
)
};
if ptr.is_null() {
Err(format_err!("Failed to attach Kprobe: {}", name))
bail!(anyhow::anyhow!("Failed to attach Kprobe: {}", name))
} else {
Ok(Self {
p: ptr,
Expand All @@ -53,19 +53,19 @@ impl Kprobe {
}
}

pub fn attach_kprobe(function: &str, code: File) -> Result<Self, Error> {
pub fn attach_kprobe(function: &str, code: File) -> Result<Self> {
let name = format!("p_{}", &make_alphanumeric(function));
Kprobe::new(&name, BPF_PROBE_ENTRY, function, code)
.map_err(|_| format_err!("Failed to attach Kprobe: {}", name))
.map_err(|_| anyhow::anyhow!("Failed to attach Kprobe: {}", name))
}

pub fn attach_kretprobe(function: &str, code: File) -> Result<Self, Error> {
pub fn attach_kretprobe(function: &str, code: File) -> Result<Self> {
let name = format!("r_{}", &make_alphanumeric(function));
Kprobe::new(&name, BPF_PROBE_RETURN, function, code)
.map_err(|_| format_err!("Failed to attach Kretprobe: {}", name))
.map_err(|_| anyhow::anyhow!("Failed to attach Kretprobe: {}", name))
}

pub fn get_kprobe_functions(event_re: &str) -> Result<Vec<String>, Error> {
pub fn get_kprobe_functions(event_re: &str) -> Result<Vec<String>> {
let mut fns: Vec<String> = vec![];

enum Section {
Expand Down
18 changes: 9 additions & 9 deletions src/core/kprobe/v0_6_0.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{self, bail, Result};
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_ENTRY as BPF_PROBE_ENTRY;
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_RETURN as BPF_PROBE_RETURN;
use bcc_sys::bccapi::*;
use failure::*;

use crate::core::make_alphanumeric;

Expand All @@ -21,11 +21,11 @@ pub struct Kprobe {
}

impl Kprobe {
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self, Error> {
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self> {
let cname =
CString::new(name).map_err(|_| format_err!("Nul byte in Kprobe name: {}", name))?;
let cfunction = CString::new(function)
.map_err(|_| format_err!("Nul byte in Kprobe function: {}", function))?;
.map_err(|_| anyhow::anyhow!("Nul byte in Kprobe function: {}", function))?;
let ptr = unsafe {
bpf_attach_kprobe(
code.as_raw_fd(),
Expand All @@ -36,7 +36,7 @@ impl Kprobe {
)
};
if ptr < 0 {
Err(format_err!("Failed to attach Kprobe: {}", name))
Err(anyhow::anyhow!("Failed to attach Kprobe: {}", name))
} else {
Ok(Self {
p: ptr,
Expand All @@ -46,19 +46,19 @@ impl Kprobe {
}
}

pub fn attach_kprobe(function: &str, code: File) -> Result<Self, Error> {
pub fn attach_kprobe(function: &str, code: File) -> Result<Self> {
let name = format!("p_{}", &make_alphanumeric(function));
Kprobe::new(&name, BPF_PROBE_ENTRY, function, code)
.map_err(|_| format_err!("Failed to attach Kprobe: {}", name))
.map_err(|_| anyhow::anyhow!("Failed to attach Kprobe: {}", name))
}

pub fn attach_kretprobe(function: &str, code: File) -> Result<Self, Error> {
pub fn attach_kretprobe(function: &str, code: File) -> Result<Self> {
let name = format!("r_{}", &make_alphanumeric(function));
Kprobe::new(&name, BPF_PROBE_RETURN, function, code)
.map_err(|_| format_err!("Failed to attach Kretprobe: {}", name))
.map_err(|_| anyhow::anyhow!("Failed to attach Kretprobe: {}", name))
}

pub fn get_kprobe_functions(event_re: &str) -> Result<Vec<String>, Error> {
pub fn get_kprobe_functions(event_re: &str) -> Result<Vec<String>> {
let mut fns: Vec<String> = vec![];

enum Section {
Expand Down
20 changes: 10 additions & 10 deletions src/core/kprobe/v0_9_0.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{self, Result};
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_ENTRY as BPF_PROBE_ENTRY;
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_RETURN as BPF_PROBE_RETURN;
use bcc_sys::bccapi::*;
use failure::*;

use crate::core::make_alphanumeric;

Expand All @@ -21,11 +21,11 @@ pub struct Kprobe {
}

impl Kprobe {
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self, Error> {
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self> {
let cname =
CString::new(name).map_err(|_| format_err!("Nul byte in Kprobe name: {}", name))?;
CString::new(name).map_err(|_| anyhow::anyhow!("Nul byte in Kprobe name: {}", name))?;
let cfunction = CString::new(function)
.map_err(|_| format_err!("Nul byte in Kprobe function: {}", function))?;
.map_err(|_| anyhow::anyhow!("Nul byte in Kprobe function: {}", function))?;
let ptr = unsafe {
bpf_attach_kprobe(
code.as_raw_fd(),
Expand All @@ -37,7 +37,7 @@ impl Kprobe {
)
};
if ptr < 0 {
Err(format_err!("Failed to attach Kprobe: {}", name))
Err(anyhow::anyhow!("Failed to attach Kprobe: {}", name))
} else {
Ok(Self {
p: ptr,
Expand All @@ -47,19 +47,19 @@ impl Kprobe {
}
}

pub fn attach_kprobe(function: &str, code: File) -> Result<Self, Error> {
pub fn attach_kprobe(function: &str, code: File) -> Result<Self> {
let name = format!("p_{}", &make_alphanumeric(function));
Kprobe::new(&name, BPF_PROBE_ENTRY, function, code)
.map_err(|_| format_err!("Failed to attach Kprobe: {}", name))
.map_err(|_| anyhow::anyhow!("Failed to attach Kprobe: {}", name))
}

pub fn attach_kretprobe(function: &str, code: File) -> Result<Self, Error> {
pub fn attach_kretprobe(function: &str, code: File) -> Result<Self> {
let name = format!("r_{}", &make_alphanumeric(function));
Kprobe::new(&name, BPF_PROBE_RETURN, function, code)
.map_err(|_| format_err!("Failed to attach Kretprobe: {}", name))
.map_err(|_| anyhow::anyhow!("Failed to attach Kretprobe: {}", name))
}

pub fn get_kprobe_functions(event_re: &str) -> Result<Vec<String>, Error> {
pub fn get_kprobe_functions(event_re: &str) -> Result<Vec<String>> {
let mut fns: Vec<String> = vec![];

enum Section {
Expand Down
Loading