1// Check that we can patch and un-patch on demand, and that logging gets invoked
2// appropriately.
3//
4// RUN: %clangxx_xray -fxray-instrument -std=c++11 %s -o %t
5// RUN: XRAY_OPTIONS="patch_premain=false" %run %t 2>&1 | FileCheck %s
6// RUN: %clangxx_xray -fxray-instrument -fno-xray-function-index -std=c++11 %s -o %t
7// RUN: XRAY_OPTIONS="patch_premain=false" %run %t 2>&1 | FileCheck %s
8
9// UNSUPPORTED: target-is-mips64,target-is-mips64el
10
11#include "xray/xray_interface.h"
12
13#include <cstdio>
14
15bool called = false;
16
17void test_handler(int32_t fid, XRayEntryType type) {
18 printf(format: "called: %d, type=%d\n", fid, static_cast<int32_t>(type));
19 called = true;
20}
21
22[[clang::xray_always_instrument]] void always_instrument() {
23 printf(format: "always instrumented called\n");
24}
25
26int main() {
27 __xray_set_handler(entry: test_handler);
28 always_instrument();
29 // CHECK: always instrumented called
30 auto status = __xray_patch();
31 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
32 // CHECK-NEXT: patching status: 1
33 always_instrument();
34 // CHECK-NEXT: called: {{.*}}, type=0
35 // CHECK-NEXT: always instrumented called
36 // CHECK-NEXT: called: {{.*}}, type=1
37 status = __xray_unpatch();
38 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
39 // CHECK-NEXT: patching status: 1
40 always_instrument();
41 // CHECK-NEXT: always instrumented called
42 status = __xray_patch();
43 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
44 // CHECK-NEXT: patching status: 1
45 __xray_remove_handler();
46 always_instrument();
47 // CHECK-NEXT: always instrumented called
48 status = __xray_unpatch();
49 printf(format: "patching status: %d\n", static_cast<int32_t>(status));
50 // CHECK-NEXT: patching status: 1
51}
52

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