@@ -14,7 +14,7 @@ use scopeguard::defer;
14
14
15
15
use libbpf_rs:: {
16
16
num_possible_cpus, Iter , Map , MapFlags , MapType , Object , ObjectBuilder , OpenObject ,
17
- ProgramType , TracepointOpts , UsdtOpts ,
17
+ ProgramType , TracepointOpts , UprobeOpts , UsdtOpts ,
18
18
} ;
19
19
20
20
fn get_test_object_path ( filename : & str ) -> PathBuf {
@@ -937,3 +937,72 @@ fn test_object_tracepoint_with_opts() {
937
937
938
938
assert_eq ! ( result, cookie_val. into( ) ) ;
939
939
}
940
+
941
+ #[ inline( never) ]
942
+ #[ no_mangle]
943
+ extern "C" fn uprobe_target ( ) -> usize {
944
+ 42
945
+ }
946
+
947
+ /// Check that we can attach a BPF program to a uprobe.
948
+ #[ test]
949
+ fn test_object_uprobe_with_opts ( ) {
950
+ bump_rlimit_mlock ( ) ;
951
+
952
+ let mut obj = get_test_object ( "uprobe.bpf.o" ) ;
953
+ let prog = obj
954
+ . prog_mut ( "handle__uprobe" )
955
+ . expect ( "Failed to find program" ) ;
956
+
957
+ let pid = unsafe { libc:: getpid ( ) } ;
958
+ let path = std:: env:: current_exe ( ) . expect ( "Failed to find executable name" ) ;
959
+ let func_offset = 0 ;
960
+ let opts = UprobeOpts {
961
+ func_name : "uprobe_target" . to_string ( ) ,
962
+ ..Default :: default ( )
963
+ } ;
964
+ let _link = prog
965
+ . attach_uprobe_with_opts ( pid, path, func_offset, opts)
966
+ . expect ( "Failed to attach prog" ) ;
967
+
968
+ let map = obj. map ( "ringbuf" ) . expect ( "Failed to get ringbuf map" ) ;
969
+ let action = || {
970
+ let _ = uprobe_target ( ) ;
971
+ } ;
972
+ let result = with_ringbuffer ( map, action) ;
973
+
974
+ assert_eq ! ( result, 1 ) ;
975
+ }
976
+
977
+ /// Check that we can attach a BPF program to a uprobe and access the cookie
978
+ /// provided during attach.
979
+ #[ test]
980
+ fn test_object_uprobe_with_cookie ( ) {
981
+ bump_rlimit_mlock ( ) ;
982
+
983
+ let cookie_val = 5u16 ;
984
+ let mut obj = get_test_object ( "uprobe.bpf.o" ) ;
985
+ let prog = obj
986
+ . prog_mut ( "handle__uprobe_with_cookie" )
987
+ . expect ( "Failed to find program" ) ;
988
+
989
+ let pid = unsafe { libc:: getpid ( ) } ;
990
+ let path = std:: env:: current_exe ( ) . expect ( "Failed to find executable name" ) ;
991
+ let func_offset = 0 ;
992
+ let opts = UprobeOpts {
993
+ func_name : "uprobe_target" . to_string ( ) ,
994
+ cookie : cookie_val. into ( ) ,
995
+ ..Default :: default ( )
996
+ } ;
997
+ let _link = prog
998
+ . attach_uprobe_with_opts ( pid, path, func_offset, opts)
999
+ . expect ( "Failed to attach prog" ) ;
1000
+
1001
+ let map = obj. map ( "ringbuf" ) . expect ( "Failed to get ringbuf map" ) ;
1002
+ let action = || {
1003
+ let _ = uprobe_target ( ) ;
1004
+ } ;
1005
+ let result = with_ringbuffer ( map, action) ;
1006
+
1007
+ assert_eq ! ( result, cookie_val. into( ) ) ;
1008
+ }
0 commit comments