Skip to content

Commit aa1c7ac

Browse files
committed
Replaced results with anyhow errors
1 parent 93d0f94 commit aa1c7ac

File tree

13 files changed

+112
-119
lines changed

13 files changed

+112
-119
lines changed

src/core/kprobe/v0_4_0.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use anyhow::{self, bail, Result};
12
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_ENTRY as BPF_PROBE_ENTRY;
23
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_RETURN as BPF_PROBE_RETURN;
34
use bcc_sys::bccapi::*;
4-
use failure::*;
55

66
use crate::core::make_alphanumeric;
77
use crate::types::MutPointer;
@@ -23,9 +23,9 @@ pub struct Kprobe {
2323
}
2424

2525
impl Kprobe {
26-
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self, Error> {
26+
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self> {
2727
let cname =
28-
CString::new(name).map_err(|_| format_err!("Nul byte in Kprobe name: {}", name))?;
28+
CString::new(name).map_err(|_| anyhow::anyhow!("Nul byte in Kprobe name: {}", name))?;
2929
let cfunction = CString::new(function)
3030
.map_err(|_| format_err!("Nul byte in Kprobe function: {}", function))?;
3131
let (pid, cpu, group_fd) = (-1, 0, -1);
@@ -43,7 +43,7 @@ impl Kprobe {
4343
)
4444
};
4545
if ptr.is_null() {
46-
Err(format_err!("Failed to attach Kprobe: {}", name))
46+
bail!(anyhow::anyhow!("Failed to attach Kprobe: {}", name))
4747
} else {
4848
Ok(Self {
4949
p: ptr,
@@ -53,19 +53,19 @@ impl Kprobe {
5353
}
5454
}
5555

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

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

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

7171
enum Section {

src/core/kprobe/v0_6_0.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use anyhow::{self, bail, Result};
12
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_ENTRY as BPF_PROBE_ENTRY;
23
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_RETURN as BPF_PROBE_RETURN;
34
use bcc_sys::bccapi::*;
4-
use failure::*;
55

66
use crate::core::make_alphanumeric;
77

@@ -21,11 +21,11 @@ pub struct Kprobe {
2121
}
2222

2323
impl Kprobe {
24-
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self, Error> {
24+
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self> {
2525
let cname =
2626
CString::new(name).map_err(|_| format_err!("Nul byte in Kprobe name: {}", name))?;
2727
let cfunction = CString::new(function)
28-
.map_err(|_| format_err!("Nul byte in Kprobe function: {}", function))?;
28+
.map_err(|_| anyhow::anyhow!("Nul byte in Kprobe function: {}", function))?;
2929
let ptr = unsafe {
3030
bpf_attach_kprobe(
3131
code.as_raw_fd(),
@@ -36,7 +36,7 @@ impl Kprobe {
3636
)
3737
};
3838
if ptr < 0 {
39-
Err(format_err!("Failed to attach Kprobe: {}", name))
39+
Err(anyhow::anyhow!("Failed to attach Kprobe: {}", name))
4040
} else {
4141
Ok(Self {
4242
p: ptr,
@@ -46,19 +46,19 @@ impl Kprobe {
4646
}
4747
}
4848

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

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

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

6464
enum Section {

src/core/kprobe/v0_9_0.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
use anyhow::{self, Result};
12
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_ENTRY as BPF_PROBE_ENTRY;
23
use bcc_sys::bccapi::bpf_probe_attach_type_BPF_PROBE_RETURN as BPF_PROBE_RETURN;
34
use bcc_sys::bccapi::*;
4-
use failure::*;
55

66
use crate::core::make_alphanumeric;
77

@@ -21,11 +21,11 @@ pub struct Kprobe {
2121
}
2222

2323
impl Kprobe {
24-
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self, Error> {
24+
fn new(name: &str, attach_type: u32, function: &str, code: File) -> Result<Self> {
2525
let cname =
26-
CString::new(name).map_err(|_| format_err!("Nul byte in Kprobe name: {}", name))?;
26+
CString::new(name).map_err(|_| anyhow::anyhow!("Nul byte in Kprobe name: {}", name))?;
2727
let cfunction = CString::new(function)
28-
.map_err(|_| format_err!("Nul byte in Kprobe function: {}", function))?;
28+
.map_err(|_| anyhow::anyhow!("Nul byte in Kprobe function: {}", function))?;
2929
let ptr = unsafe {
3030
bpf_attach_kprobe(
3131
code.as_raw_fd(),
@@ -37,7 +37,7 @@ impl Kprobe {
3737
)
3838
};
3939
if ptr < 0 {
40-
Err(format_err!("Failed to attach Kprobe: {}", name))
40+
Err(anyhow::anyhow!("Failed to attach Kprobe: {}", name))
4141
} else {
4242
Ok(Self {
4343
p: ptr,
@@ -47,19 +47,19 @@ impl Kprobe {
4747
}
4848
}
4949

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

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

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

6565
enum Section {

src/core/mod.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ mod raw_tracepoint;
33
mod tracepoint;
44
mod uprobe;
55

6+
use anyhow::{self, bail, Result};
67
use bcc_sys::bccapi::*;
7-
use failure::*;
88

99
use self::kprobe::Kprobe;
1010
use self::raw_tracepoint::RawTracepoint;
@@ -59,7 +59,7 @@ impl BPF {
5959
feature = "v0_7_0",
6060
feature = "v0_8_0",
6161
))]
62-
pub fn new(code: &str) -> Result<BPF, Error> {
62+
pub fn new(code: &str) -> Result<BPF> {
6363
let cs = CString::new(code)?;
6464
let ptr = unsafe { bpf_module_create_c_from_string(cs.as_ptr(), 2, ptr::null_mut(), 0) };
6565
if ptr.is_null() {
@@ -79,7 +79,7 @@ impl BPF {
7979

8080
// 0.9.0 changes the API for bpf_module_create_c_from_string()
8181
#[cfg(any(feature = "v0_9_0", feature = "v0_10_0",))]
82-
pub fn new(code: &str) -> Result<BPF, Error> {
82+
pub fn new(code: &str) -> Result<BPF> {
8383
let cs = CString::new(code)?;
8484
let ptr =
8585
unsafe { bpf_module_create_c_from_string(cs.as_ptr(), 2, ptr::null_mut(), 0, true) };
@@ -105,7 +105,7 @@ impl BPF {
105105
feature = "v0_13_0",
106106
not(feature = "specific"),
107107
))]
108-
pub fn new(code: &str) -> Result<BPF, Error> {
108+
pub fn new(code: &str) -> Result<BPF> {
109109
let cs = CString::new(code)?;
110110
let ptr = unsafe {
111111
bpf_module_create_c_from_string(
@@ -118,7 +118,7 @@ impl BPF {
118118
)
119119
};
120120
if ptr.is_null() {
121-
return Err(format_err!("couldn't create BPF program"));
121+
bail!(anyhow::anyhow!("couldn't create BPF program"));
122122
}
123123

124124
Ok(BPF {
@@ -143,20 +143,20 @@ impl BPF {
143143
Table::new(id, self.ptr())
144144
}
145145

146-
pub fn load_net(&mut self, name: &str) -> Result<File, Error> {
146+
pub fn load_net(&mut self, name: &str) -> Result<File> {
147147
self.load(name, bpf_prog_type_BPF_PROG_TYPE_SCHED_ACT, 0, 0)
148148
}
149149

150-
pub fn load_kprobe(&mut self, name: &str) -> Result<File, Error> {
150+
pub fn load_kprobe(&mut self, name: &str) -> Result<File> {
151151
self.load(name, bpf_prog_type_BPF_PROG_TYPE_KPROBE, 0, 0)
152152
}
153153

154-
pub fn load_uprobe(&mut self, name: &str) -> Result<File, Error> {
154+
pub fn load_uprobe(&mut self, name: &str) -> Result<File> {
155155
// it's BPF_PROG_TYPE_KPROBE even though it's a uprobe, it's weird
156156
self.load(name, bpf_prog_type_BPF_PROG_TYPE_KPROBE, 0, 0)
157157
}
158158

159-
pub fn load_tracepoint(&mut self, name: &str) -> Result<File, Error> {
159+
pub fn load_tracepoint(&mut self, name: &str) -> Result<File> {
160160
self.load(name, bpf_prog_type_BPF_PROG_TYPE_TRACEPOINT, 0, 0)
161161
}
162162

@@ -172,7 +172,7 @@ impl BPF {
172172
feature = "v0_13_0",
173173
not(feature = "specific"),
174174
))]
175-
pub fn load_raw_tracepoint(&mut self, name: &str) -> Result<File, Error> {
175+
pub fn load_raw_tracepoint(&mut self, name: &str) -> Result<File> {
176176
self.load(name, bpf_prog_type_BPF_PROG_TYPE_RAW_TRACEPOINT, 0, 0)
177177
}
178178

@@ -183,7 +183,7 @@ impl BPF {
183183
prog_type: u32,
184184
_log_level: i32,
185185
log_size: u32,
186-
) -> Result<File, Error> {
186+
) -> Result<File> {
187187
let cname = CString::new(name).unwrap();
188188
unsafe {
189189
let start: *mut bpf_insn =
@@ -227,7 +227,7 @@ impl BPF {
227227
prog_type: u32,
228228
log_level: i32,
229229
log_size: u32,
230-
) -> Result<File, Error> {
230+
) -> Result<File> {
231231
let cname = CString::new(name).unwrap();
232232
unsafe {
233233
let start: *mut bpf_insn =
@@ -236,7 +236,7 @@ impl BPF {
236236
let license = bpf_module_license(self.ptr());
237237
let version = bpf_module_kern_version(self.ptr());
238238
if start.is_null() {
239-
return Err(format_err!("Error in bpf_function_start for {}", name));
239+
bail!(anyhow::anyhow!("Error in bpf_function_start for {}", name));
240240
}
241241
let mut log_buf: Vec<u8> = Vec::with_capacity(log_size as usize);
242242
// TODO: we're ignoring any changes bpf_prog_load made to log_buf right now
@@ -274,7 +274,7 @@ impl BPF {
274274
prog_type: u32,
275275
log_level: i32,
276276
log_size: u32,
277-
) -> Result<File, Error> {
277+
) -> Result<File> {
278278
let cname = CString::new(name).unwrap();
279279
unsafe {
280280
let start: *mut bpf_insn =
@@ -283,7 +283,7 @@ impl BPF {
283283
let license = bpf_module_license(self.ptr());
284284
let version = bpf_module_kern_version(self.ptr());
285285
if start.is_null() {
286-
return Err(format_err!("Error in bpf_function_start for {}", name));
286+
bail!(anyhow::anyhow!("Error in bpf_function_start for {}", name));
287287
}
288288
let mut log_buf: Vec<u8> = Vec::with_capacity(log_size as usize);
289289
// TODO: we're ignoring any changes bpf_prog_load made to log_buf right now
@@ -301,7 +301,7 @@ impl BPF {
301301
log_buf.capacity() as u32,
302302
);
303303
if fd < 0 {
304-
return Err(format_err!("error loading BPF program: {}", name));
304+
bail!(anyhow::anyhow!("error loading BPF program: {}", name));
305305
}
306306
Ok(File::from_raw_fd(fd))
307307
}
@@ -313,25 +313,25 @@ impl BPF {
313313
symbol: &str,
314314
file: File,
315315
pid: pid_t,
316-
) -> Result<(), Error> {
316+
) -> Result<()> {
317317
let uprobe = Uprobe::attach_uretprobe(binary_path, symbol, file, pid)?;
318318
self.uprobes.insert(uprobe);
319319
Ok(())
320320
}
321321

322-
pub fn attach_kprobe(&mut self, function: &str, file: File) -> Result<(), Error> {
322+
pub fn attach_kprobe(&mut self, function: &str, file: File) -> Result<()> {
323323
let kprobe = Kprobe::attach_kprobe(function, file)?;
324324
self.kprobes.insert(kprobe);
325325
Ok(())
326326
}
327327

328-
pub fn attach_kretprobe(&mut self, function: &str, file: File) -> Result<(), Error> {
328+
pub fn attach_kretprobe(&mut self, function: &str, file: File) -> Result<()> {
329329
let kretprobe = Kprobe::attach_kretprobe(function, file)?;
330330
self.kprobes.insert(kretprobe);
331331
Ok(())
332332
}
333333

334-
pub fn get_kprobe_functions(&mut self, event_re: &str) -> Result<Vec<String>, Error> {
334+
pub fn get_kprobe_functions(&mut self, event_re: &str) -> Result<Vec<String>> {
335335
Kprobe::get_kprobe_functions(event_re)
336336
}
337337

@@ -341,13 +341,13 @@ impl BPF {
341341
symbol: &str,
342342
file: File,
343343
pid: pid_t,
344-
) -> Result<(), Error> {
344+
) -> Result<()> {
345345
let uprobe = Uprobe::attach_uprobe(binary_path, symbol, file, pid)?;
346346
self.uprobes.insert(uprobe);
347347
Ok(())
348348
}
349349

350-
pub fn attach_tracepoint(&mut self, subsys: &str, name: &str, file: File) -> Result<(), Error> {
350+
pub fn attach_tracepoint(&mut self, subsys: &str, name: &str, file: File) -> Result<()> {
351351
let tracepoint = Tracepoint::attach_tracepoint(subsys, name, file)?;
352352
self.tracepoints.insert(tracepoint);
353353
Ok(())
@@ -365,13 +365,13 @@ impl BPF {
365365
feature = "v0_13_0",
366366
not(feature = "specific"),
367367
))]
368-
pub fn attach_raw_tracepoint(&mut self, name: &str, file: File) -> Result<(), Error> {
368+
pub fn attach_raw_tracepoint(&mut self, name: &str, file: File) -> Result<()> {
369369
let raw_tracepoint = RawTracepoint::attach_raw_tracepoint(name, file)?;
370370
self.raw_tracepoints.insert(raw_tracepoint);
371371
Ok(())
372372
}
373373

374-
pub fn ksymname(&mut self, name: &str) -> Result<u64, Error> {
374+
pub fn ksymname(&mut self, name: &str) -> Result<u64> {
375375
self.sym_caches
376376
.entry(-1)
377377
.or_insert_with(|| SymbolCache::new(-1));
@@ -396,7 +396,7 @@ impl BPF {
396396
|| self.ksymname("bpf_get_raw_tracepoint").is_ok()
397397
}
398398

399-
pub fn init_perf_map<F>(&mut self, table: Table, cb: F) -> Result<(), Error>
399+
pub fn init_perf_map<F>(&mut self, table: Table, cb: F) -> Result<()>
400400
where
401401
F: Fn() -> Box<dyn FnMut(&[u8]) + Send>,
402402
{

src/core/raw_tracepoint/v0_6_0.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use anyhow::{self, Result};
12
use bcc_sys::bccapi::*;
2-
use failure::*;
33

44
use std::ffi::CString;
55
use std::fs::File;
@@ -14,12 +14,12 @@ pub struct RawTracepoint {
1414
}
1515

1616
impl RawTracepoint {
17-
pub fn attach_raw_tracepoint(name: &str, file: File) -> Result<Self, Error> {
18-
let cname =
19-
CString::new(name).map_err(|_| format_err!("Nul byte in Tracepoint name: {}", name))?;
17+
pub fn attach_raw_tracepoint(name: &str, file: File) -> Result<Self> {
18+
let cname = CString::new(name)
19+
.map_err(|_| anyhow::anyhow!("Nul byte in Tracepoint name: {}", name))?;
2020
let ptr = unsafe { bpf_attach_raw_tracepoint(file.as_raw_fd(), cname.as_ptr() as *mut _) };
2121
if ptr < 0 {
22-
Err(format_err!("Failed to attach raw tracepoint: {}", name))
22+
Err(anyhow::anyhow!("Failed to attach raw tracepoint: {}", name))
2323
} else {
2424
Ok(Self {
2525
name: cname,

0 commit comments

Comments
 (0)