brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · 5326d83 Raw
108 lines · cpp
1// Check that we can patch and un-patch DSOs loaded with dlopen.2//3 4// RUN: split-file %s %t5// RUN: %clangxx_xray -g -fPIC -fxray-instrument -fxray-shared -shared -std=c++11 %t/testlib.cpp -o %t/testlib.so6// RUN: %clangxx_xray -g -fPIC -rdynamic -fxray-instrument -fxray-shared -std=c++11 %t/main.cpp -o %t/main.o7//8// RUN: env XRAY_OPTIONS="patch_premain=true" %run %t/main.o %t/testlib.so 2>&1 | FileCheck %s9 10// REQUIRES: target={{(aarch64|x86_64)-.*}}11 12//--- main.cpp13 14#include "xray/xray_interface.h"15 16#include <cstdio>17#include <dlfcn.h>18 19void test_handler(int32_t fid, XRayEntryType type) {20  printf("called: %d, type=%d\n", fid, static_cast<int32_t>(type));21}22 23[[clang::xray_always_instrument]] void instrumented_in_executable() {24  printf("instrumented_in_executable called\n");25}26 27typedef void (*dso_func_type)();28 29int main(int argc, char **argv) {30  if (argc < 2) {31    printf("Shared library argument missing\n");32    // CHECK-NOT: Shared library argument missing33    return 1;34  }35 36  const char *dso_path = argv[1];37 38  void *dso_handle = dlopen(dso_path, RTLD_LAZY);39  if (!dso_handle) {40    printf("Failed to load shared library\n");41    char *error = dlerror();42    if (error) {43      fprintf(stderr, "%s\n", error);44      return 1;45    }46    return 1;47  }48 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();54    if (error) {55      fprintf(stderr, "%s\n", error);56      return 1;57    }58    return 1;59  }60 61  __xray_set_handler(test_handler);62 63  instrumented_in_executable();64  // CHECK: called: {{.*}}, type=065  // CHECK-NEXT: instrumented_in_executable called66  // CHECK-NEXT: called: {{.*}}, type=167  instrumented_in_dso();68  // CHECK-NEXT: called: {{.*}}, type=069  // CHECK-NEXT: instrumented_in_dso called70  // CHECK-NEXT: called: {{.*}}, type=171 72  auto status = __xray_unpatch();73  printf("unpatching status: %d\n", static_cast<int32_t>(status));74  // CHECK-NEXT: unpatching status: 175 76  instrumented_in_executable();77  // CHECK-NEXT: instrumented_in_executable called78  instrumented_in_dso();79  // CHECK-NEXT: instrumented_in_dso called80 81  status = __xray_patch();82  printf("patching status: %d\n", static_cast<int32_t>(status));83  // CHECK-NEXT: patching status: 184 85  instrumented_in_executable();86  // CHECK-NEXT: called: {{.*}}, type=087  // CHECK-NEXT: instrumented_in_executable called88  // CHECK-NEXT: called: {{.*}}, type=189  instrumented_in_dso();90  // CHECK-NEXT: called: {{.*}}, type=091  // CHECK-NEXT: instrumented_in_dso called92  // CHECK-NEXT: called: {{.*}}, type=193 94  dlclose(dso_handle);95 96  status = __xray_unpatch();97  printf("unpatching status: %d\n", static_cast<int32_t>(status));98  // CHECK-NEXT: unpatching status: 199}100 101//--- testlib.cpp102 103#include <cstdio>104 105[[clang::xray_always_instrument]] void instrumented_in_dso() {106  printf("instrumented_in_dso called\n");107}108