Skip to content

Commit de00fbe

Browse files
d-e-s-oinsearchoflosttime
authored andcommitted
libbpf-rs: Add support for uprobe opts
This change adds support for providing additional options when attaching a program to a uprobe. Among other things, doing so can relieve users from having to manually parse ELF sections to look up function offsets, because using the bpf_program__attach_uprobe_opts function this is now natively supported by libbpf. Closes: #235 Signed-off-by: Daniel Müller <deso@posteo.net>
1 parent cc678e4 commit de00fbe

File tree

2 files changed

+71
-2
lines changed

2 files changed

+71
-2
lines changed

libbpf-rs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub use crate::object::{Object, ObjectBuilder, OpenObject};
9898
pub use crate::perf_buffer::{PerfBuffer, PerfBufferBuilder};
9999
pub use crate::print::{get_print, set_print, PrintCallback, PrintLevel};
100100
pub use crate::program::{
101-
OpenProgram, Program, ProgramAttachType, ProgramType, TracepointOpts, UsdtOpts,
101+
OpenProgram, Program, ProgramAttachType, ProgramType, TracepointOpts, UprobeOpts, UsdtOpts,
102102
};
103103
pub use crate::ringbuf::{RingBuffer, RingBufferBuilder};
104104
pub use crate::tc::{

libbpf-rs/src/program.rs

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,28 @@ use strum_macros::Display;
1010

1111
use crate::*;
1212

13+
/// Options to optionally be provided when attaching to a uprobe.
14+
#[derive(Clone, Debug, Default)]
15+
pub struct UprobeOpts {
16+
/// Offset of kernel reference counted USDT semaphore.
17+
pub ref_ctr_offset: usize,
18+
/// Custom user-provided value accessible through `bpf_get_attach_cookie`.
19+
pub cookie: u64,
20+
/// uprobe is return probe, invoked at function return time.
21+
pub retprobe: bool,
22+
/// Function name to attach to.
23+
///
24+
/// Could be an unqualified ("abc") or library-qualified "abc@LIBXYZ" name.
25+
/// To specify function entry, `func_name` should be set while `func_offset`
26+
/// argument to should be 0. To trace an offset within a function, specify
27+
/// `func_name` and use `func_offset` argument to specify offset within the
28+
/// function. Shared library functions must specify the shared library
29+
/// binary_path.
30+
pub func_name: String,
31+
#[doc(hidden)]
32+
pub _non_exhaustive: (),
33+
}
34+
1335
/// Options to optionally be provided when attaching to a USDT.
1436
#[derive(Clone, Debug, Default)]
1537
pub struct UsdtOpts {
@@ -417,7 +439,7 @@ impl Program {
417439
binary_path: T,
418440
func_offset: usize,
419441
) -> Result<Link> {
420-
let path = util::path_to_cstring(binary_path.as_ref())?;
442+
let path = util::path_to_cstring(binary_path)?;
421443
let path_ptr = path.as_ptr();
422444
let ptr = unsafe {
423445
libbpf_sys::bpf_program__attach_uprobe(
@@ -436,6 +458,53 @@ impl Program {
436458
}
437459
}
438460

461+
/// Attach this program to a [userspace
462+
/// probe](https://www.kernel.org/doc/html/latest/trace/uprobetracer.html),
463+
/// providing additional options.
464+
pub fn attach_uprobe_with_opts(
465+
&mut self,
466+
pid: i32,
467+
binary_path: impl AsRef<Path>,
468+
func_offset: usize,
469+
opts: UprobeOpts,
470+
) -> Result<Link> {
471+
let path = util::path_to_cstring(binary_path)?;
472+
let path_ptr = path.as_ptr();
473+
let UprobeOpts {
474+
ref_ctr_offset,
475+
cookie,
476+
retprobe,
477+
func_name,
478+
_non_exhaustive,
479+
} = opts;
480+
481+
let func_name = util::str_to_cstring(&func_name)?;
482+
let opts = libbpf_sys::bpf_uprobe_opts {
483+
sz: mem::size_of::<Self>() as u64,
484+
ref_ctr_offset: ref_ctr_offset as libbpf_sys::size_t,
485+
bpf_cookie: cookie,
486+
retprobe,
487+
func_name: func_name.as_ptr(),
488+
..Default::default()
489+
};
490+
491+
let ptr = unsafe {
492+
libbpf_sys::bpf_program__attach_uprobe_opts(
493+
self.ptr,
494+
pid,
495+
path_ptr,
496+
func_offset as libbpf_sys::size_t,
497+
&opts as *const _,
498+
)
499+
};
500+
let err = unsafe { libbpf_sys::libbpf_get_error(ptr as *const _) };
501+
if err != 0 {
502+
Err(Error::System(err as i32))
503+
} else {
504+
Ok(Link::new(ptr))
505+
}
506+
}
507+
439508
/// Attach this program to a [kernel
440509
/// probe](https://www.kernel.org/doc/html/latest/trace/kprobetrace.html).
441510
pub fn attach_kprobe<T: AsRef<str>>(&mut self, retprobe: bool, func_name: T) -> Result<Link> {

0 commit comments

Comments
 (0)