1// Check that we can patch and un-patch on demand, and that logging gets invoked
2// appropriately.
3//
4
5// RUN: split-file %s %t
6// RUN: %clangxx_xray -g -fPIC -fxray-instrument -fxray-shared -shared -std=c++11 %t/testlib.cpp -o %t/testlib.so
7// RUN: %clangxx_xray -g -fPIC -fxray-instrument -fxray-shared -std=c++11 %t/main.cpp %t/testlib.so -Wl,-rpath,%t -o %t/main.o
8
9// RUN: XRAY_OPTIONS="patch_premain=false" %run %t/main.o 2>&1 | FileCheck %s
10
11// REQUIRES: target={{(aarch64|x86_64)-.*}}
12
13//--- main.cpp
14
15#include "xray/xray_interface.h"
16
17#include <cstdio>
18
19bool called = false;
20
21void test_handler(int32_t fid, XRayEntryType type) {
22 printf(format: "called: %d, type=%d\n", fid, static_cast<int32_t>(type));
23 called = true;
24}
25
26[[clang::xray_always_instrument]] void instrumented_in_executable() {
27 printf(format: "instrumented_in_executable called\n");
28}
29
30extern void instrumented_in_dso();
31
32int main() {
33 __xray_set_handler(entry: test_handler);
34 instrumented_in_executable();
35 // CHECK: instrumented_in_executable called
36 instrumented_in_dso();
37 // CHECK: instrumented_in_dso called
38 auto status = __xray_patch();
39 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
40 // CHECK-NEXT: patching status: 1
41 instrumented_in_executable();
42 // CHECK-NEXT: called: {{.*}}, type=0
43 // CHECK-NEXT: instrumented_in_executable called
44 // CHECK-NEXT: called: {{.*}}, type=1
45 instrumented_in_dso();
46 // CHECK-NEXT: called: {{.*}}, type=0
47 // CHECK-NEXT: instrumented_in_dso called
48 // CHECK-NEXT: called: {{.*}}, type=1
49 status = __xray_unpatch();
50 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
51 // CHECK-NEXT: patching status: 1
52 instrumented_in_executable();
53 // CHECK-NEXT: instrumented_in_executable called
54 instrumented_in_dso();
55 // CHECK-NEXT: instrumented_in_dso called
56 status = __xray_patch();
57 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
58 // CHECK-NEXT: patching status: 1
59 __xray_remove_handler();
60 instrumented_in_executable();
61 // CHECK-NEXT: instrumented_in_executable called
62 instrumented_in_dso();
63 // CHECK-NEXT: instrumented_in_dso called
64 status = __xray_unpatch();
65 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
66 // CHECK-NEXT: patching status: 1
67}
68
69//--- testlib.cpp
70
71#include <cstdio>
72
73[[clang::xray_always_instrument]] void instrumented_in_dso() {
74 printf(format: "instrumented_in_dso called\n");
75}
76

source code of compiler-rt/test/xray/TestCases/Posix/patching-unpatching-dso.cpp