summaryrefslogtreecommitdiff
path: root/test/xray/TestCases/Posix/logging-modes.cc
blob: 22f6942b759532c26beba7fc864ca2dcf15c2e49 (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
// Check that we can install an implementation associated with a mode.
//
// RUN: %clangxx_xray -std=c++11 %s -o %t
// RUN: %run %t | FileCheck %s
//
// UNSUPPORTED: target-is-mips64,target-is-mips64el

#include "xray/xray_interface.h"
#include "xray/xray_log_interface.h"
#include <cassert>
#include <cstdio>

[[clang::xray_never_instrument]] void printing_handler(int32_t fid,
                                                       XRayEntryType) {
  thread_local volatile bool printing = false;
  if (printing)
    return;
  printing = true;
  std::printf("printing %d\n", fid);
  printing = false;
}

[[clang::xray_never_instrument]] XRayLogInitStatus
printing_init(size_t, size_t, void *, size_t) {
  return XRayLogInitStatus::XRAY_LOG_INITIALIZED;
}

[[clang::xray_never_instrument]] XRayLogInitStatus printing_finalize() {
  return XRayLogInitStatus::XRAY_LOG_FINALIZED;
}

[[clang::xray_never_instrument]] XRayLogFlushStatus printing_flush_log() {
  return XRayLogFlushStatus::XRAY_LOG_FLUSHED;
}

[[clang::xray_always_instrument]] void callme() { std::printf("called me!\n"); }

static bool unused = [] {
  assert(__xray_log_register_mode("custom",
                                  {printing_init, printing_finalize,
                                   printing_handler, printing_flush_log}) ==
         XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
  return true;
}();

int main(int argc, char **argv) {
  assert(__xray_log_select_mode("custom") ==
         XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
  assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
  assert(__xray_log_init(0, 0, nullptr, 0) ==
         XRayLogInitStatus::XRAY_LOG_INITIALIZED);
  // CHECK: printing {{.*}}
  callme(); // CHECK: called me!
  // CHECK: printing {{.*}}
  assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED);
  assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
  assert(__xray_log_select_mode("not-found") ==
         XRayLogRegisterStatus::XRAY_MODE_NOT_FOUND);
}