1010
1111int lets_test_this (int );
1212
13+ #if defined(__x86_64__ ) || defined(__i386__ )
14+ /*
15+ * SIB (Scale-Index-Base) addressing format:
16+ * "size@(base_reg, index_reg, scale)"
17+ * - 'size' is the size in bytes of the array element, and its sign indicates
18+ * whether the type is signed (negative) or unsigned (positive).
19+ * - 'base_reg' is the register holding the base address, normally rdx or edx
20+ * - 'index_reg' is the register holding the index, normally rax or eax
21+ * - 'scale' is the scaling factor (typically 1, 2, 4, or 8), which matches the
22+ * size of the element type.
23+ *
24+ * For example, for an array of 'short' (signed 2-byte elements), the SIB spec would be:
25+ * - size: -2 (negative because 'short' is signed)
26+ * - scale: 2 (since sizeof(short) == 2)
27+ * The resulting SIB format: "-2@(%%rdx,%%rax,2)"
28+ */
29+ static volatile short array [] = {-1 , -2 , -3 , -4 };
30+ #define USDT_SIB_ARG_SPEC -2@(%%rdx,%%rax,2)
31+ #endif
32+
1333static volatile int idx = 2 ;
1434static volatile __u64 bla = 0xFEDCBA9876543210ULL ;
1535static volatile short nums [] = {-1 , -2 , -3 , -4 };
@@ -25,6 +45,10 @@ unsigned short test_usdt0_semaphore SEC(".probes");
2545unsigned short test_usdt3_semaphore SEC (".probes" );
2646unsigned short test_usdt12_semaphore SEC (".probes" );
2747
48+ #if defined(__x86_64__ ) || defined(__i386__ )
49+ unsigned short test_usdt_sib_semaphore SEC (".probes" );
50+ #endif
51+
2852static void __always_inline trigger_func (int x ) {
2953 long y = 42 ;
3054
@@ -40,12 +64,27 @@ static void __always_inline trigger_func(int x) {
4064 }
4165}
4266
67+ #if defined(__x86_64__ ) || defined(__i386__ )
68+ static void trigger_sib_spec (void )
69+ {
70+ /* Base address + offset + (index * scale) */
71+ /* Force SIB addressing with inline assembly */
72+ asm volatile (
73+ STAP_PROBE_ASM (test , usdt_sib , USDT_SIB_ARG_SPEC )
74+ :
75+ : "d" (array ), "a" (0 )
76+ : "memory "
77+ );
78+ }
79+ #endif
80+
4381static void subtest_basic_usdt (void )
4482{
4583 LIBBPF_OPTS (bpf_usdt_opts , opts );
4684 struct test_usdt * skel ;
4785 struct test_usdt__bss * bss ;
4886 int err , i ;
87+ const __u64 expected_cookie = 0xcafedeadbeeffeed ;
4988
5089 skel = test_usdt__open_and_load ();
5190 if (!ASSERT_OK_PTR (skel , "skel_open" ))
@@ -59,20 +98,29 @@ static void subtest_basic_usdt(void)
5998 goto cleanup ;
6099
61100 /* usdt0 won't be auto-attached */
62- opts .usdt_cookie = 0xcafedeadbeeffeed ;
101+ opts .usdt_cookie = expected_cookie ;
63102 skel -> links .usdt0 = bpf_program__attach_usdt (skel -> progs .usdt0 ,
64103 0 /*self*/ , "/proc/self/exe" ,
65104 "test" , "usdt0" , & opts );
66105 if (!ASSERT_OK_PTR (skel -> links .usdt0 , "usdt0_link" ))
67106 goto cleanup ;
68107
108+ #if defined(__x86_64__ ) || defined(__i386__ )
109+ opts .usdt_cookie = expected_cookie ;
110+ skel -> links .usdt_sib = bpf_program__attach_usdt (skel -> progs .usdt_sib ,
111+ 0 /*self*/ , "/proc/self/exe" ,
112+ "test" , "usdt_sib" , & opts );
113+ if (!ASSERT_OK_PTR (skel -> links .usdt_sib , "usdt_sib_link" ))
114+ goto cleanup ;
115+ #endif
116+
69117 trigger_func (1 );
70118
71119 ASSERT_EQ (bss -> usdt0_called , 1 , "usdt0_called" );
72120 ASSERT_EQ (bss -> usdt3_called , 1 , "usdt3_called" );
73121 ASSERT_EQ (bss -> usdt12_called , 1 , "usdt12_called" );
74122
75- ASSERT_EQ (bss -> usdt0_cookie , 0xcafedeadbeeffeed , "usdt0_cookie" );
123+ ASSERT_EQ (bss -> usdt0_cookie , expected_cookie , "usdt0_cookie" );
76124 ASSERT_EQ (bss -> usdt0_arg_cnt , 0 , "usdt0_arg_cnt" );
77125 ASSERT_EQ (bss -> usdt0_arg_ret , - ENOENT , "usdt0_arg_ret" );
78126 ASSERT_EQ (bss -> usdt0_arg_size , - ENOENT , "usdt0_arg_size" );
@@ -156,6 +204,16 @@ static void subtest_basic_usdt(void)
156204 ASSERT_EQ (bss -> usdt3_args [1 ], 42 , "usdt3_arg2" );
157205 ASSERT_EQ (bss -> usdt3_args [2 ], (uintptr_t )& bla , "usdt3_arg3" );
158206
207+ #if defined(__x86_64__ ) || defined(__i386__ )
208+ trigger_sib_spec ();
209+ ASSERT_EQ (bss -> usdt_sib_called , 1 , "usdt_sib_called" );
210+ ASSERT_EQ (bss -> usdt_sib_cookie , expected_cookie , "usdt_sib_cookie" );
211+ ASSERT_EQ (bss -> usdt_sib_arg_cnt , 1 , "usdt_sib_arg_cnt" );
212+ ASSERT_EQ (bss -> usdt_sib_arg , nums [0 ], "usdt_sib_arg" );
213+ ASSERT_EQ (bss -> usdt_sib_arg_ret , 0 , "usdt_sib_arg_ret" );
214+ ASSERT_EQ (bss -> usdt_sib_arg_size , sizeof (nums [0 ]), "usdt_sib_arg_size" );
215+ #endif
216+
159217cleanup :
160218 test_usdt__destroy (skel );
161219}
0 commit comments