Skip to content

Commit 2ceda05

Browse files
ver-nyand-e-s-o
authored andcommitted
query: add uprobe_multi second info call fields
1 parent 4281c45 commit 2ceda05

3 files changed

Lines changed: 93 additions & 10 deletions

File tree

libbpf-rs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Unreleased
22
----------
3-
- Added `KprobeMultiLinkInfo` fields that required a second info call.
3+
- Added `KprobeMultiLinkInfo` & `UprobeMultiLinkInfo` fields that required a
4+
second info call.
45

56

67
0.26.2

libbpf-rs/src/query.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use std::ffi::c_void;
1414
use std::ffi::CStr;
1515
use std::ffi::CString;
16+
use std::ffi::OsStr;
1617
use std::io;
1718
use std::mem::size_of_val;
1819
use std::mem::zeroed;
@@ -22,6 +23,8 @@ use std::os::fd::BorrowedFd;
2223
use std::os::fd::FromRawFd;
2324
use std::os::fd::OwnedFd;
2425
use std::os::raw::c_char;
26+
use std::os::unix::ffi::OsStrExt;
27+
use std::path::PathBuf;
2528
use std::ptr;
2629
use std::time::Duration;
2730

@@ -731,12 +734,20 @@ pub struct KprobeMultiLinkInfo {
731734
pub struct UprobeMultiLinkInfo {
732735
/// Size of the path.
733736
pub path_size: u32,
737+
/// The absolute file path of the binary being probed.
738+
pub path: Option<PathBuf>,
734739
/// Count of uprobe targets.
735740
pub count: u32,
736741
/// Flags for the link.
737742
pub flags: u32,
738743
/// PID to which the uprobe is attached.
739744
pub pid: u32,
745+
/// Offsets from the binary.
746+
pub offsets: Vec<u64>,
747+
/// Offsets of kernel reference counted USDT semaphore.
748+
pub ref_ctr_offsets: Vec<u64>,
749+
/// Cookies corresponding to the attach addresses.
750+
pub cookies: Vec<u64>,
740751
}
741752

742753
/// Information about a perf event link.
@@ -968,11 +979,45 @@ impl LinkInfo {
968979
})
969980
}
970981
libbpf_sys::BPF_LINK_TYPE_UPROBE_MULTI => {
982+
let mut buf = [0u8; libc::PATH_MAX as usize];
983+
let count = unsafe { s.__bindgen_anon_1.uprobe_multi.count } as usize;
984+
let mut offsets = vec![0; count];
985+
let mut ref_ctr_offsets = vec![0; count];
986+
let mut cookies = vec![0; count];
987+
988+
s.__bindgen_anon_1.uprobe_multi.path = buf.as_mut_ptr() as u64;
989+
s.__bindgen_anon_1.uprobe_multi.path_size = buf.len() as u32;
990+
s.__bindgen_anon_1.uprobe_multi.offsets = offsets.as_mut_ptr() as u64;
991+
s.__bindgen_anon_1.uprobe_multi.ref_ctr_offsets =
992+
ref_ctr_offsets.as_mut_ptr() as u64;
993+
s.__bindgen_anon_1.uprobe_multi.cookies = cookies.as_mut_ptr() as u64;
994+
let item_ptr: *mut libbpf_sys::bpf_link_info = &mut s;
995+
let mut len = size_of_val(&s) as u32;
996+
let ret = unsafe {
997+
libbpf_sys::bpf_obj_get_info_by_fd(fd.as_raw_fd(), item_ptr.cast(), &mut len)
998+
};
999+
if ret != 0 {
1000+
return None;
1001+
}
1002+
1003+
let path_size = unsafe { s.__bindgen_anon_1.uprobe_multi.path_size };
1004+
let path = if path_size != 0 {
1005+
let path_ptr = unsafe { s.__bindgen_anon_1.uprobe_multi.path } as *const c_char;
1006+
let c_str = unsafe { CStr::from_ptr(path_ptr) };
1007+
Some(PathBuf::from(OsStr::from_bytes(c_str.to_bytes())))
1008+
} else {
1009+
None
1010+
};
1011+
9711012
LinkTypeInfo::UprobeMulti(UprobeMultiLinkInfo {
972-
path_size: unsafe { s.__bindgen_anon_1.uprobe_multi.path_size },
1013+
path_size,
1014+
path,
9731015
count: unsafe { s.__bindgen_anon_1.uprobe_multi.count },
9741016
flags: unsafe { s.__bindgen_anon_1.uprobe_multi.flags },
9751017
pid: unsafe { s.__bindgen_anon_1.uprobe_multi.pid },
1018+
offsets,
1019+
ref_ctr_offsets,
1020+
cookies,
9761021
})
9771022
}
9781023
libbpf_sys::BPF_LINK_TYPE_SOCKMAP => LinkTypeInfo::SockMap(SockMapLinkInfo {

libbpf-rs/tests/test.rs

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ use libbpf_rs::query::KprobeMultiLinkInfo;
4242
use libbpf_rs::query::LinkTypeInfo;
4343
use libbpf_rs::query::PerfEventType;
4444
use libbpf_rs::query::ProgInfoIter;
45+
use libbpf_rs::query::UprobeMultiLinkInfo;
4546
use libbpf_rs::AsRawLibbpf;
4647
use libbpf_rs::Iter;
4748
use libbpf_rs::KprobeMultiOpts;
@@ -2088,18 +2089,23 @@ fn test_object_uprobe_multi_with_non_default_opts() {
20882089
let prog = get_prog_mut(&mut obj, "handle__uprobe_multi_with_non_default_opts");
20892090
let func_pattern = "";
20902091

2091-
let pid = unsafe { libc::getpid() };
2092-
let path = current_exe().expect("failed to find executable name");
2092+
let current_pid = unsafe { libc::getpid() };
2093+
let current_path = current_exe().expect("failed to find executable name");
2094+
let syms_opt = vec![
2095+
"non_default_opts_multi_uprobe_func_with_opts_func_1".to_string(),
2096+
"non_default_opts_multi_uprobe_func_with_opts_func_2".to_string(),
2097+
];
2098+
let ref_ctr_offsets_opt = vec![2, 4];
2099+
let cookies_opt = vec![5, 2];
20932100
let opts = UprobeMultiOpts {
2094-
syms: vec![
2095-
"non_default_opts_multi_uprobe_func_with_opts_func_1".to_string(),
2096-
"non_default_opts_multi_uprobe_func_with_opts_func_2".to_string(),
2097-
],
2101+
syms: syms_opt.clone(),
2102+
ref_ctr_offsets: ref_ctr_offsets_opt.clone(),
2103+
cookies: cookies_opt.clone(),
20982104
..Default::default()
20992105
};
21002106

2101-
let _link = prog
2102-
.attach_uprobe_multi_with_opts(pid, path, func_pattern, opts)
2107+
let link = prog
2108+
.attach_uprobe_multi_with_opts(current_pid, &current_path, func_pattern, opts)
21032109
.expect("failed to attach uprobe multi");
21042110

21052111
non_default_opts_multi_uprobe_func_with_opts_func_1();
@@ -2119,6 +2125,37 @@ fn test_object_uprobe_multi_with_non_default_opts() {
21192125
);
21202126

21212127
assert_eq!(result, 2);
2128+
2129+
let link_info = link.info().expect("failed to get uprobe_multi link info");
2130+
let LinkTypeInfo::UprobeMulti(uprobe_multi) = link_info.info else {
2131+
panic!(
2132+
"Expected LinkTypeInfo::UprobeMulti for uprobe_multi, got: {:?}",
2133+
link_info.info
2134+
);
2135+
};
2136+
2137+
let UprobeMultiLinkInfo {
2138+
path,
2139+
count,
2140+
pid,
2141+
offsets,
2142+
ref_ctr_offsets,
2143+
cookies,
2144+
..
2145+
} = uprobe_multi;
2146+
assert_eq!(path.as_ref(), Some(&current_path));
2147+
assert_eq!(pid, current_pid as u32);
2148+
assert_eq!(count, syms_opt.len() as u32);
2149+
for (actual, sym) in zip(offsets, syms_opt) {
2150+
let expected = get_symbol_offset(&current_path, &sym).unwrap();
2151+
assert_eq!(actual, expected as u64);
2152+
}
2153+
for (actual, expected) in zip(ref_ctr_offsets, ref_ctr_offsets_opt) {
2154+
assert_eq!(actual, expected as u64);
2155+
}
2156+
for (actual, expected) in zip(cookies, cookies_opt) {
2157+
assert_eq!(actual, expected);
2158+
}
21222159
}
21232160

21242161
#[tag(root)]

0 commit comments

Comments
 (0)