xref: /llvm-project/compiler-rt/test/xray/TestCases/Posix/patching-unpatching.cpp (revision 643a2080ec028bd7674206e0f97cbc7d0f132f99)
16db8c59fSFangrui Song // Check that we can patch and un-patch on demand, and that logging gets invoked
26db8c59fSFangrui Song // appropriately.
36db8c59fSFangrui Song //
46db8c59fSFangrui Song // RUN: %clangxx_xray -fxray-instrument -std=c++11 %s -o %t
5*643a2080SHarini0924 // RUN: env XRAY_OPTIONS="patch_premain=false" %run %t 2>&1 | FileCheck %s
67c7c8e0dSIan Levesque // RUN: %clangxx_xray -fxray-instrument -fno-xray-function-index -std=c++11 %s -o %t
7*643a2080SHarini0924 // RUN: env XRAY_OPTIONS="patch_premain=false" %run %t 2>&1 | FileCheck %s
86db8c59fSFangrui Song 
96db8c59fSFangrui Song // UNSUPPORTED: target-is-mips64,target-is-mips64el
106db8c59fSFangrui Song 
116db8c59fSFangrui Song #include "xray/xray_interface.h"
126db8c59fSFangrui Song 
136db8c59fSFangrui Song #include <cstdio>
146db8c59fSFangrui Song 
156db8c59fSFangrui Song bool called = false;
166db8c59fSFangrui Song 
176db8c59fSFangrui Song void test_handler(int32_t fid, XRayEntryType type) {
186db8c59fSFangrui Song   printf("called: %d, type=%d\n", fid, static_cast<int32_t>(type));
196db8c59fSFangrui Song   called = true;
206db8c59fSFangrui Song }
216db8c59fSFangrui Song 
226db8c59fSFangrui Song [[clang::xray_always_instrument]] void always_instrument() {
236db8c59fSFangrui Song   printf("always instrumented called\n");
246db8c59fSFangrui Song }
256db8c59fSFangrui Song 
266db8c59fSFangrui Song int main() {
276db8c59fSFangrui Song   __xray_set_handler(test_handler);
286db8c59fSFangrui Song   always_instrument();
296db8c59fSFangrui Song   // CHECK: always instrumented called
306db8c59fSFangrui Song   auto status = __xray_patch();
316db8c59fSFangrui Song   printf("patching status: %d\n", static_cast<int32_t>(status));
326db8c59fSFangrui Song   // CHECK-NEXT: patching status: 1
336db8c59fSFangrui Song   always_instrument();
346db8c59fSFangrui Song   // CHECK-NEXT: called: {{.*}}, type=0
356db8c59fSFangrui Song   // CHECK-NEXT: always instrumented called
366db8c59fSFangrui Song   // CHECK-NEXT: called: {{.*}}, type=1
376db8c59fSFangrui Song   status = __xray_unpatch();
386db8c59fSFangrui Song   printf("patching status: %d\n", static_cast<int32_t>(status));
396db8c59fSFangrui Song   // CHECK-NEXT: patching status: 1
406db8c59fSFangrui Song   always_instrument();
416db8c59fSFangrui Song   // CHECK-NEXT: always instrumented called
426db8c59fSFangrui Song   status = __xray_patch();
436db8c59fSFangrui Song   printf("patching status: %d\n", static_cast<int32_t>(status));
446db8c59fSFangrui Song   // CHECK-NEXT: patching status: 1
456db8c59fSFangrui Song   __xray_remove_handler();
466db8c59fSFangrui Song   always_instrument();
476db8c59fSFangrui Song   // CHECK-NEXT: always instrumented called
486db8c59fSFangrui Song   status = __xray_unpatch();
496db8c59fSFangrui Song   printf("patching status: %d\n", static_cast<int32_t>(status));
506db8c59fSFangrui Song   // CHECK-NEXT: patching status: 1
516db8c59fSFangrui Song }
52