Skip to content

Commit e8087b0

Browse files
committed
libbpf-rs: Make func_name optional for uprobe attach
Change UprobeOpts::func_name to Option<String> so that user can attach uprobe to specifc offset in binary. This would be useful for target that have symbol stripped. Signed-off-by: Hengqi Chen <hengqi.chen@gmail.com>
1 parent e8c360b commit e8087b0

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

libbpf-rs/src/program.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use std::ffi::c_void;
66
use std::ffi::CStr;
7+
use std::ffi::CString;
78
use std::ffi::OsStr;
89
use std::marker::PhantomData;
910
use std::mem;
@@ -52,7 +53,7 @@ pub struct UprobeOpts {
5253
/// `func_name` and use `func_offset` argument to specify offset within the
5354
/// function. Shared library functions must specify the shared library
5455
/// binary_path.
55-
pub func_name: String,
56+
pub func_name: Option<String>,
5657
#[doc(hidden)]
5758
pub _non_exhaustive: (),
5859
}
@@ -817,13 +818,20 @@ impl<'obj> ProgramMut<'obj> {
817818
_non_exhaustive,
818819
} = opts;
819820

820-
let func_name = util::str_to_cstring(&func_name)?;
821+
let func_name: Option<CString> = if let Some(func_name) = func_name {
822+
Some(util::str_to_cstring(&func_name)?)
823+
} else {
824+
None
825+
};
826+
let ptr = func_name
827+
.as_ref()
828+
.map_or(ptr::null(), |func_name| func_name.as_ptr());
821829
let opts = libbpf_sys::bpf_uprobe_opts {
822830
sz: size_of::<libbpf_sys::bpf_uprobe_opts>() as _,
823831
ref_ctr_offset: ref_ctr_offset as libbpf_sys::size_t,
824832
bpf_cookie: cookie,
825833
retprobe,
826-
func_name: func_name.as_ptr(),
834+
func_name: ptr,
827835
..Default::default()
828836
};
829837

libbpf-rs/tests/test.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1684,7 +1684,7 @@ fn test_object_uprobe_with_opts() {
16841684
let path = current_exe().expect("failed to find executable name");
16851685
let func_offset = 0;
16861686
let opts = UprobeOpts {
1687-
func_name: "uprobe_target".to_string(),
1687+
func_name: Some("uprobe_target".into()),
16881688
..Default::default()
16891689
};
16901690
let _link = prog
@@ -1713,7 +1713,7 @@ fn test_object_uprobe_with_cookie() {
17131713
let path = current_exe().expect("failed to find executable name");
17141714
let func_offset = 0;
17151715
let opts = UprobeOpts {
1716-
func_name: "uprobe_target".to_string(),
1716+
func_name: Some("uprobe_target".into()),
17171717
cookie: cookie_val.into(),
17181718
..Default::default()
17191719
};

0 commit comments

Comments
 (0)