@@ -10,6 +10,28 @@ use strum_macros::Display;
10
10
11
11
use crate :: * ;
12
12
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
+
13
35
/// Options to optionally be provided when attaching to a USDT.
14
36
#[ derive( Clone , Debug , Default ) ]
15
37
pub struct UsdtOpts {
@@ -417,7 +439,7 @@ impl Program {
417
439
binary_path : T ,
418
440
func_offset : usize ,
419
441
) -> Result < Link > {
420
- let path = util:: path_to_cstring ( binary_path. as_ref ( ) ) ?;
442
+ let path = util:: path_to_cstring ( binary_path) ?;
421
443
let path_ptr = path. as_ptr ( ) ;
422
444
let ptr = unsafe {
423
445
libbpf_sys:: bpf_program__attach_uprobe (
@@ -436,6 +458,53 @@ impl Program {
436
458
}
437
459
}
438
460
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
+
439
508
/// Attach this program to a [kernel
440
509
/// probe](https://www.kernel.org/doc/html/latest/trace/kprobetrace.html).
441
510
pub fn attach_kprobe < T : AsRef < str > > ( & mut self , retprobe : bool , func_name : T ) -> Result < Link > {
0 commit comments