Skip to content
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
6 changes: 5 additions & 1 deletion libbpf-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,11 @@ impl<'obj> ProgramMut<'obj> {
} = opts;

let pattern = util::str_to_cstring(func_pattern.as_ref())?;
let pattern_ptr = pattern.as_ptr();
let pattern_ptr = if pattern.is_empty() {
ptr::null()
} else {
pattern.as_ptr()
};

let syms_cstrings = syms
.iter()
Expand Down
17 changes: 17 additions & 0 deletions libbpf-rs/tests/bin/src/uprobe_multi.bpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,21 @@ int handle__uprobe_multi_with_opts(void *ctx)
return 0;
}

SEC("uprobe.multi")
int handle__uprobe_multi_with_non_default_opts(void *ctx)
{
const int key = 1, init_val = 1;

int *val = bpf_map_lookup_elem(&hash_map, &key);
if (val) {
__sync_fetch_and_add(val, 1);
bpf_printk("handle__uprobe_multi_with_non_default_opts: val=%d\n",
*val);
} else {
bpf_map_update_elem(&hash_map, &key, &init_val, BPF_ANY);
}

return 0;
}

char LICENSE[] SEC("license") = "GPL";
54 changes: 54 additions & 0 deletions libbpf-rs/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1987,6 +1987,20 @@ extern "C" fn multi_uprobe_func_with_opts_func_2() -> usize {
hint::black_box(45)
}

#[inline(never)]
#[no_mangle]
extern "C" fn non_default_opts_multi_uprobe_func_with_opts_func_1() -> usize {
// Use `black_box` here as an additional barrier to inlining.
hint::black_box(46)
}

#[inline(never)]
#[no_mangle]
extern "C" fn non_default_opts_multi_uprobe_func_with_opts_func_2() -> usize {
// Use `black_box` here as an additional barrier to inlining.
hint::black_box(47)
}

#[tag(root)]
#[test]
fn test_object_uprobe_multi_with_opts() {
Expand Down Expand Up @@ -2021,6 +2035,46 @@ fn test_object_uprobe_multi_with_opts() {
assert_eq!(result, 2);
}

#[tag(root)]
#[test]
fn test_object_uprobe_multi_with_non_default_opts() {
let mut obj = get_test_object("uprobe_multi.bpf.o");
let prog = get_prog_mut(&mut obj, "handle__uprobe_multi_with_non_default_opts");
let func_pattern = "";

let pid = unsafe { libc::getpid() };
let path = current_exe().expect("failed to find executable name");
let opts = UprobeMultiOpts {
syms: vec![
"non_default_opts_multi_uprobe_func_with_opts_func_1".to_string(),
"non_default_opts_multi_uprobe_func_with_opts_func_2".to_string(),
],
..Default::default()
};

let _link = prog
.attach_uprobe_multi_with_opts(pid, path, func_pattern, opts)
.expect("failed to attach uprobe multi");

non_default_opts_multi_uprobe_func_with_opts_func_1();
non_default_opts_multi_uprobe_func_with_opts_func_2();

let map = get_map_mut(&mut obj, "hash_map");
let result_bytes = map
.lookup(&(1_u32).to_ne_bytes(), MapFlags::ANY)
.expect("failed to lookup")
.expect("failed to find value for key");

let result = i32::from_ne_bytes(
result_bytes
.as_slice()
.try_into()
.expect("invalid value size"),
);

assert_eq!(result, 2);
}

#[tag(root)]
#[test]
fn test_object_uprobe_multi() {
Expand Down