summaryrefslogtreecommitdiff
path: root/test/xray/TestCases/Posix/logging-modes.cc
blob: f839ba5e5f5034720b9ea9751fb7065a7492b5fa (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
92
93
94
95
// Check that we can install an implementation associated with a mode.
//
// RUN: rm -f xray-log.logging-modes*
// RUN: %clangxx_xray -std=c++11 %s -o %t -fxray-modes=none
// 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>
#include <string>

[[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]] XRayBuffer next_buffer(XRayBuffer buffer) {
  static const char data[10] = {};
  static const XRayBuffer first_and_last{data, 10};
  if (buffer.Data == nullptr)
    return first_and_last;
  if (buffer.Data == first_and_last.Data)
    return XRayBuffer{nullptr, 0};
  assert(false && "Invalid buffer provided.");
}

static constexpr char Options[] = "additional_flags";

[[clang::xray_never_instrument]] XRayLogInitStatus
printing_init(size_t BufferSize, size_t MaxBuffers, void *Config,
              size_t ArgsSize) {
  // We require that the printing init is called through the
  // __xray_log_init_mode(...) implementation, and that the promised contract is
  // enforced.
  assert(BufferSize == 0);
  assert(MaxBuffers == 0);
  assert(Config != nullptr);
  assert(ArgsSize == 0 || ArgsSize == sizeof(Options));
  __xray_log_set_buffer_iterator(next_buffer);
  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() {
  __xray_log_remove_buffer_iterator();
  return XRayLogFlushStatus::XRAY_LOG_FLUSHED;
}

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

static auto buffer_counter = 0;

void process_buffer(const char *, XRayBuffer) { ++buffer_counter; }

int main(int argc, char **argv) {
  assert(__xray_log_register_mode("custom",
                                  {printing_init, printing_finalize,
                                   printing_handler, printing_flush_log}) ==
         XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
  assert(__xray_log_select_mode("custom") ==
         XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
  assert(__xray_log_get_current_mode() != nullptr);
  std::string current_mode = __xray_log_get_current_mode();
  assert(current_mode == "custom");
  assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
  assert(__xray_log_init_mode("custom", "flags_config_here=true") ==
         XRayLogInitStatus::XRAY_LOG_INITIALIZED);

  // Also test that we can use the "binary" version of the
  // __xray_log_niit_mode(...) API.
  assert(__xray_log_init_mode_bin("custom", Options, sizeof(Options)) ==
         XRayLogInitStatus::XRAY_LOG_INITIALIZED);

  // CHECK: printing {{.*}}
  callme(); // CHECK: called me!
  // CHECK: printing {{.*}}
  assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED);
  assert(__xray_log_process_buffers(process_buffer) ==
         XRayLogFlushStatus::XRAY_LOG_FLUSHED);
  assert(buffer_counter == 1);
  assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
  assert(__xray_log_select_mode("not-found") ==
         XRayLogRegisterStatus::XRAY_MODE_NOT_FOUND);
}