summaryrefslogtreecommitdiff
path: root/lib/xray/xray_log_interface.cc
blob: c7cfe5080b2ec2d34782e9188f9eda84d5b24b89 (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
//===-- xray_log_interface.cc ---------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file is a part of XRay, a function call tracing system.
//
//===----------------------------------------------------------------------===//
#include "xray/xray_log_interface.h"

#include "xray/xray_interface.h"
#include "xray_defs.h"

#include <memory>
#include <mutex>

std::mutex XRayImplMutex;
std::unique_ptr<XRayLogImpl> GlobalXRayImpl;

void __xray_set_log_impl(XRayLogImpl Impl) XRAY_NEVER_INSTRUMENT {
  if (Impl.log_init == nullptr || Impl.log_finalize == nullptr ||
      Impl.handle_arg0 == nullptr || Impl.flush_log == nullptr) {
    std::lock_guard<std::mutex> Guard(XRayImplMutex);
    GlobalXRayImpl.reset();
    return;
  }

  std::lock_guard<std::mutex> Guard(XRayImplMutex);
  GlobalXRayImpl.reset(new XRayLogImpl);
  *GlobalXRayImpl = Impl;
}

XRayLogInitStatus __xray_init(size_t BufferSize, size_t MaxBuffers, void *Args,
                              size_t ArgsSize) XRAY_NEVER_INSTRUMENT {
  std::lock_guard<std::mutex> Guard(XRayImplMutex);
  if (!GlobalXRayImpl)
    return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
  return GlobalXRayImpl->log_init(BufferSize, MaxBuffers, Args, ArgsSize);
}

XRayLogInitStatus __xray_log_finalize() XRAY_NEVER_INSTRUMENT {
  std::lock_guard<std::mutex> Guard(XRayImplMutex);
  if (!GlobalXRayImpl)
    return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED;
  return GlobalXRayImpl->log_finalize();
}

XRayLogFlushStatus __xray_log_flushLog() XRAY_NEVER_INSTRUMENT {
  std::lock_guard<std::mutex> Guard(XRayImplMutex);
  if (!GlobalXRayImpl)
    return XRayLogFlushStatus::XRAY_LOG_NOT_FLUSHING;
  return GlobalXRayImpl->flush_log();
}