1 // Check that we can patch and un-patch DSOs loaded with dlopen.
4 // RUN: split-file %s %t
5 // RUN: %clangxx_xray -g -fPIC -fxray-instrument -fxray-shared -shared -std=c++11 %t/testlib.cpp -o %t/testlib.so
6 // RUN: %clangxx_xray -g -fPIC -rdynamic -fxray-instrument -fxray-shared -std=c++11 %t/main.cpp -o %t/main.o
8 // RUN: XRAY_OPTIONS="patch_premain=true" %run %t/main.o %t/testlib.so 2>&1 | FileCheck %s
10 // REQUIRES: target={{(aarch64|x86_64)-.*}}
14 #include "xray/xray_interface.h"
19 void test_handler(int32_t fid
, XRayEntryType type
) {
20 printf("called: %d, type=%d\n", fid
, static_cast<int32_t>(type
));
23 [[clang::xray_always_instrument
]] void instrumented_in_executable() {
24 printf("instrumented_in_executable called\n");
27 typedef void (*dso_func_type
)();
29 int main(int argc
, char **argv
) {
31 printf("Shared library argument missing\n");
32 // CHECK-NOT: Shared library argument missing
36 const char *dso_path
= argv
[1];
38 void *dso_handle
= dlopen(dso_path
, RTLD_LAZY
);
40 printf("Failed to load shared library\n");
41 char *error
= dlerror();
43 fprintf(stderr
, "%s\n", error
);
49 dso_func_type instrumented_in_dso
=
50 (dso_func_type
)dlsym(dso_handle
, "_Z19instrumented_in_dsov");
51 if (!instrumented_in_dso
) {
52 printf("Failed to find symbol\n");
53 char *error
= dlerror();
55 fprintf(stderr
, "%s\n", error
);
61 __xray_set_handler(test_handler
);
63 instrumented_in_executable();
64 // CHECK: called: {{.*}}, type=0
65 // CHECK-NEXT: instrumented_in_executable called
66 // CHECK-NEXT: called: {{.*}}, type=1
67 instrumented_in_dso();
68 // CHECK-NEXT: called: {{.*}}, type=0
69 // CHECK-NEXT: instrumented_in_dso called
70 // CHECK-NEXT: called: {{.*}}, type=1
72 auto status
= __xray_unpatch();
73 printf("unpatching status: %d\n", static_cast<int32_t>(status
));
74 // CHECK-NEXT: unpatching status: 1
76 instrumented_in_executable();
77 // CHECK-NEXT: instrumented_in_executable called
78 instrumented_in_dso();
79 // CHECK-NEXT: instrumented_in_dso called
81 status
= __xray_patch();
82 printf("patching status: %d\n", static_cast<int32_t>(status
));
83 // CHECK-NEXT: patching status: 1
85 instrumented_in_executable();
86 // CHECK-NEXT: called: {{.*}}, type=0
87 // CHECK-NEXT: instrumented_in_executable called
88 // CHECK-NEXT: called: {{.*}}, type=1
89 instrumented_in_dso();
90 // CHECK-NEXT: called: {{.*}}, type=0
91 // CHECK-NEXT: instrumented_in_dso called
92 // CHECK-NEXT: called: {{.*}}, type=1
96 status
= __xray_unpatch();
97 printf("unpatching status: %d\n", static_cast<int32_t>(status
));
98 // CHECK-NEXT: unpatching status: 1
105 [[clang::xray_always_instrument
]] void instrumented_in_dso() {
106 printf("instrumented_in_dso called\n");