summaryrefslogtreecommitdiff
path: root/test/xray/TestCases/Posix/coverage-sample.cpp
blob: 62c13ba3d42a96ef3ee6c44edb5da56c0bb79a94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Check that we can patch and unpatch specific function ids.
//
// RUN: %clangxx_xray -std=c++11 %s -o %t
// RUN: XRAY_OPTIONS="patch_premain=false xray_naive_log=false" %run %t | FileCheck %s

// UNSUPPORTED: target-is-mips64,target-is-mips64el

#include "xray/xray_interface.h"

#include <set>
#include <cstdio>
#include <cassert>

std::set<int32_t> function_ids;

[[clang::xray_never_instrument]] void coverage_handler(int32_t fid,
                                                       XRayEntryType) {
  thread_local bool patching = false;
  if (patching) return;
  patching = true;
  function_ids.insert(fid);
  __xray_unpatch_function(fid);
  patching = false;
}

[[clang::xray_always_instrument]] void baz() {
  // do nothing!
}

[[clang::xray_always_instrument]] void bar() {
  baz();
}

[[clang::xray_always_instrument]] void foo() {
  bar();
}

[[clang::xray_always_instrument]] int main(int argc, char *argv[]) {
  __xray_set_handler(coverage_handler);
  assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
  foo();
  assert(__xray_unpatch() == XRayPatchingStatus::SUCCESS);

  // print out the function_ids.
  printf("first pass.\n");
  for (const auto id : function_ids)
    printf("patched: %d\n", id);

  // CHECK-LABEL: first pass.
  // CHECK-DAG: patched: [[F1:.*]]
  // CHECK-DAG: patched: [[F2:.*]]
  // CHECK-DAG: patched: [[F3:.*]]

  // make a copy of the function_ids, then patch them later.
  auto called_fns = function_ids;

  // clear the function_ids.
  function_ids.clear();

  // patch the functions we've called before.
  for (const auto id : called_fns)
    assert(__xray_patch_function(id) == XRayPatchingStatus::SUCCESS);

  // then call them again.
  foo();
  assert(__xray_unpatch() == XRayPatchingStatus::SUCCESS);

  // confirm that we've seen the same functions again.
  printf("second pass.\n");
  for (const auto id : function_ids)
    printf("patched: %d\n", id);
  // CHECK-LABEL: second pass.
  // CHECK-DAG: patched: [[F1]]
  // CHECK-DAG: patched: [[F2]]
  // CHECK-DAG: patched: [[F3]]

  // Now we want to make sure that if we unpatch one, that we're only going to
  // see two calls of the coverage_handler.
  function_ids.clear();
  assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
  assert(__xray_unpatch_function(1) == XRayPatchingStatus::SUCCESS);
  foo();
  assert(__xray_unpatch() == XRayPatchingStatus::SUCCESS);

  // confirm that we don't see function id one called anymore.
  printf("missing 1.\n");
  for (const auto id : function_ids)
    printf("patched: %d\n", id);
  // CHECK-LABEL: missing 1.
  // CHECK-NOT: patched: 1
}