2019-07-01 16:59:02 -07:00
// SPDX-License-Identifier: GPL-2.0
# include <test_progs.h>
2019-12-13 17:43:38 -08:00
# include "test_attach_probe.skel.h"
2019-07-01 16:59:02 -07:00
ssize_t get_base_addr ( ) {
2019-12-18 18:04:42 -08:00
size_t start , offset ;
2019-07-01 16:59:02 -07:00
char buf [ 256 ] ;
FILE * f ;
f = fopen ( " /proc/self/maps " , " r " ) ;
if ( ! f )
return - errno ;
2019-12-18 18:04:42 -08:00
while ( fscanf ( f , " %zx-%*x %s %zx %*[^ \n ] \n " ,
& start , buf , & offset ) = = 3 ) {
2019-07-01 16:59:02 -07:00
if ( strcmp ( buf , " r-xp " ) = = 0 ) {
fclose ( f ) ;
2019-12-18 18:04:42 -08:00
return start - offset ;
2019-07-01 16:59:02 -07:00
}
}
fclose ( f ) ;
return - EINVAL ;
}
void test_attach_probe ( void )
{
2019-12-13 17:43:38 -08:00
int duration = 0 ;
struct bpf_link * kprobe_link , * kretprobe_link ;
struct bpf_link * uprobe_link , * uretprobe_link ;
struct test_attach_probe * skel ;
2019-07-01 16:59:02 -07:00
size_t uprobe_offset ;
ssize_t base_addr ;
base_addr = get_base_addr ( ) ;
if ( CHECK ( base_addr < 0 , " get_base_addr " ,
" failed to find base addr: %zd " , base_addr ) )
return ;
uprobe_offset = ( size_t ) & get_base_addr - base_addr ;
2019-12-17 21:25:50 -08:00
skel = test_attach_probe__open_and_load ( ) ;
2019-12-13 17:43:38 -08:00
if ( CHECK ( ! skel , " skel_open " , " failed to open skeleton \n " ) )
2019-07-01 16:59:02 -07:00
return ;
2019-12-13 17:43:38 -08:00
if ( CHECK ( ! skel - > bss , " check_bss " , " .bss wasn't mmap()-ed \n " ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
2019-12-13 17:43:38 -08:00
kprobe_link = bpf_program__attach_kprobe ( skel - > progs . handle_kprobe ,
2019-07-01 16:59:02 -07:00
false /* retprobe */ ,
2019-07-16 14:58:27 +02:00
SYS_NANOSLEEP_KPROBE_NAME ) ;
2019-07-01 16:59:02 -07:00
if ( CHECK ( IS_ERR ( kprobe_link ) , " attach_kprobe " ,
2019-12-13 17:43:38 -08:00
" err %ld \n " , PTR_ERR ( kprobe_link ) ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
2019-12-13 17:43:38 -08:00
skel - > links . handle_kprobe = kprobe_link ;
kretprobe_link = bpf_program__attach_kprobe ( skel - > progs . handle_kretprobe ,
2019-07-01 16:59:02 -07:00
true /* retprobe */ ,
2019-07-16 14:58:27 +02:00
SYS_NANOSLEEP_KPROBE_NAME ) ;
2019-07-01 16:59:02 -07:00
if ( CHECK ( IS_ERR ( kretprobe_link ) , " attach_kretprobe " ,
2019-12-13 17:43:38 -08:00
" err %ld \n " , PTR_ERR ( kretprobe_link ) ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
2019-12-13 17:43:38 -08:00
skel - > links . handle_kretprobe = kretprobe_link ;
uprobe_link = bpf_program__attach_uprobe ( skel - > progs . handle_uprobe ,
2019-07-01 16:59:02 -07:00
false /* retprobe */ ,
0 /* self pid */ ,
" /proc/self/exe " ,
uprobe_offset ) ;
if ( CHECK ( IS_ERR ( uprobe_link ) , " attach_uprobe " ,
2019-12-13 17:43:38 -08:00
" err %ld \n " , PTR_ERR ( uprobe_link ) ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
2019-12-13 17:43:38 -08:00
skel - > links . handle_uprobe = uprobe_link ;
uretprobe_link = bpf_program__attach_uprobe ( skel - > progs . handle_uretprobe ,
2019-07-01 16:59:02 -07:00
true /* retprobe */ ,
- 1 /* any pid */ ,
" /proc/self/exe " ,
uprobe_offset ) ;
if ( CHECK ( IS_ERR ( uretprobe_link ) , " attach_uretprobe " ,
2019-12-13 17:43:38 -08:00
" err %ld \n " , PTR_ERR ( uretprobe_link ) ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
2019-12-13 17:43:38 -08:00
skel - > links . handle_uretprobe = uretprobe_link ;
2019-07-01 16:59:02 -07:00
/* trigger & validate kprobe && kretprobe */
usleep ( 1 ) ;
2019-12-13 17:43:38 -08:00
if ( CHECK ( skel - > bss - > kprobe_res ! = 1 , " check_kprobe_res " ,
" wrong kprobe res: %d \n " , skel - > bss - > kprobe_res ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
2019-12-13 17:43:38 -08:00
if ( CHECK ( skel - > bss - > kretprobe_res ! = 2 , " check_kretprobe_res " ,
" wrong kretprobe res: %d \n " , skel - > bss - > kretprobe_res ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
/* trigger & validate uprobe & uretprobe */
get_base_addr ( ) ;
2019-12-13 17:43:38 -08:00
if ( CHECK ( skel - > bss - > uprobe_res ! = 3 , " check_uprobe_res " ,
" wrong uprobe res: %d \n " , skel - > bss - > uprobe_res ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
2019-12-13 17:43:38 -08:00
if ( CHECK ( skel - > bss - > uretprobe_res ! = 4 , " check_uretprobe_res " ,
" wrong uretprobe res: %d \n " , skel - > bss - > uretprobe_res ) )
2019-07-01 16:59:02 -07:00
goto cleanup ;
cleanup :
2019-12-13 17:43:38 -08:00
test_attach_probe__destroy ( skel ) ;
2019-07-01 16:59:02 -07:00
}