xref: /llvm-project/compiler-rt/test/xray/TestCases/Posix/logging-modes.cpp (revision 6db8c59f210af60d882f7aedc7c9dc95d41d9696)
1*6db8c59fSFangrui Song // Check that we can install an implementation associated with a mode.
2*6db8c59fSFangrui Song //
3*6db8c59fSFangrui Song // RUN: rm -f xray-log.logging-modes*
4*6db8c59fSFangrui Song // RUN: %clangxx_xray -std=c++11 %s -o %t -fxray-modes=none
5*6db8c59fSFangrui Song // RUN: %run %t | FileCheck %s
6*6db8c59fSFangrui Song //
7*6db8c59fSFangrui Song // UNSUPPORTED: target-is-mips64,target-is-mips64el
8*6db8c59fSFangrui Song 
9*6db8c59fSFangrui Song #include "xray/xray_interface.h"
10*6db8c59fSFangrui Song #include "xray/xray_log_interface.h"
11*6db8c59fSFangrui Song #include <cassert>
12*6db8c59fSFangrui Song #include <cstdio>
13*6db8c59fSFangrui Song #include <string>
14*6db8c59fSFangrui Song 
printing_handler(int32_t fid,XRayEntryType)15*6db8c59fSFangrui Song [[clang::xray_never_instrument]] void printing_handler(int32_t fid,
16*6db8c59fSFangrui Song                                                        XRayEntryType) {
17*6db8c59fSFangrui Song   thread_local volatile bool printing = false;
18*6db8c59fSFangrui Song   if (printing)
19*6db8c59fSFangrui Song     return;
20*6db8c59fSFangrui Song   printing = true;
21*6db8c59fSFangrui Song   std::printf("printing %d\n", fid);
22*6db8c59fSFangrui Song   printing = false;
23*6db8c59fSFangrui Song }
24*6db8c59fSFangrui Song 
next_buffer(XRayBuffer buffer)25*6db8c59fSFangrui Song [[clang::xray_never_instrument]] XRayBuffer next_buffer(XRayBuffer buffer) {
26*6db8c59fSFangrui Song   static const char data[10] = {};
27*6db8c59fSFangrui Song   static const XRayBuffer first_and_last{data, 10};
28*6db8c59fSFangrui Song   if (buffer.Data == nullptr)
29*6db8c59fSFangrui Song     return first_and_last;
30*6db8c59fSFangrui Song   if (buffer.Data == first_and_last.Data)
31*6db8c59fSFangrui Song     return XRayBuffer{nullptr, 0};
32*6db8c59fSFangrui Song   assert(false && "Invalid buffer provided.");
33*6db8c59fSFangrui Song }
34*6db8c59fSFangrui Song 
35*6db8c59fSFangrui Song static constexpr char Options[] = "additional_flags";
36*6db8c59fSFangrui Song 
37*6db8c59fSFangrui Song [[clang::xray_never_instrument]] XRayLogInitStatus
printing_init(size_t BufferSize,size_t MaxBuffers,void * Config,size_t ArgsSize)38*6db8c59fSFangrui Song printing_init(size_t BufferSize, size_t MaxBuffers, void *Config,
39*6db8c59fSFangrui Song               size_t ArgsSize) {
40*6db8c59fSFangrui Song   // We require that the printing init is called through the
41*6db8c59fSFangrui Song   // __xray_log_init_mode(...) implementation, and that the promised contract is
42*6db8c59fSFangrui Song   // enforced.
43*6db8c59fSFangrui Song   assert(BufferSize == 0);
44*6db8c59fSFangrui Song   assert(MaxBuffers == 0);
45*6db8c59fSFangrui Song   assert(Config != nullptr);
46*6db8c59fSFangrui Song   assert(ArgsSize == 0 || ArgsSize == sizeof(Options));
47*6db8c59fSFangrui Song   __xray_log_set_buffer_iterator(next_buffer);
48*6db8c59fSFangrui Song   return XRayLogInitStatus::XRAY_LOG_INITIALIZED;
49*6db8c59fSFangrui Song }
50*6db8c59fSFangrui Song 
printing_finalize()51*6db8c59fSFangrui Song [[clang::xray_never_instrument]] XRayLogInitStatus printing_finalize() {
52*6db8c59fSFangrui Song   return XRayLogInitStatus::XRAY_LOG_FINALIZED;
53*6db8c59fSFangrui Song }
54*6db8c59fSFangrui Song 
printing_flush_log()55*6db8c59fSFangrui Song [[clang::xray_never_instrument]] XRayLogFlushStatus printing_flush_log() {
56*6db8c59fSFangrui Song   __xray_log_remove_buffer_iterator();
57*6db8c59fSFangrui Song   return XRayLogFlushStatus::XRAY_LOG_FLUSHED;
58*6db8c59fSFangrui Song }
59*6db8c59fSFangrui Song 
callme()60*6db8c59fSFangrui Song [[clang::xray_always_instrument]] void callme() { std::printf("called me!\n"); }
61*6db8c59fSFangrui Song 
62*6db8c59fSFangrui Song static auto buffer_counter = 0;
63*6db8c59fSFangrui Song 
process_buffer(const char *,XRayBuffer)64*6db8c59fSFangrui Song void process_buffer(const char *, XRayBuffer) { ++buffer_counter; }
65*6db8c59fSFangrui Song 
main(int argc,char ** argv)66*6db8c59fSFangrui Song int main(int argc, char **argv) {
67*6db8c59fSFangrui Song   assert(__xray_log_register_mode("custom",
68*6db8c59fSFangrui Song                                   {printing_init, printing_finalize,
69*6db8c59fSFangrui Song                                    printing_handler, printing_flush_log}) ==
70*6db8c59fSFangrui Song          XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
71*6db8c59fSFangrui Song   assert(__xray_log_select_mode("custom") ==
72*6db8c59fSFangrui Song          XRayLogRegisterStatus::XRAY_REGISTRATION_OK);
73*6db8c59fSFangrui Song   assert(__xray_log_get_current_mode() != nullptr);
74*6db8c59fSFangrui Song   std::string current_mode = __xray_log_get_current_mode();
75*6db8c59fSFangrui Song   assert(current_mode == "custom");
76*6db8c59fSFangrui Song   assert(__xray_patch() == XRayPatchingStatus::SUCCESS);
77*6db8c59fSFangrui Song   assert(__xray_log_init_mode("custom", "flags_config_here=true") ==
78*6db8c59fSFangrui Song          XRayLogInitStatus::XRAY_LOG_INITIALIZED);
79*6db8c59fSFangrui Song 
80*6db8c59fSFangrui Song   // Also test that we can use the "binary" version of the
81*6db8c59fSFangrui Song   // __xray_log_niit_mode(...) API.
82*6db8c59fSFangrui Song   assert(__xray_log_init_mode_bin("custom", Options, sizeof(Options)) ==
83*6db8c59fSFangrui Song          XRayLogInitStatus::XRAY_LOG_INITIALIZED);
84*6db8c59fSFangrui Song 
85*6db8c59fSFangrui Song   // CHECK: printing {{.*}}
86*6db8c59fSFangrui Song   callme(); // CHECK: called me!
87*6db8c59fSFangrui Song   // CHECK: printing {{.*}}
88*6db8c59fSFangrui Song   assert(__xray_log_finalize() == XRayLogInitStatus::XRAY_LOG_FINALIZED);
89*6db8c59fSFangrui Song   assert(__xray_log_process_buffers(process_buffer) ==
90*6db8c59fSFangrui Song          XRayLogFlushStatus::XRAY_LOG_FLUSHED);
91*6db8c59fSFangrui Song   assert(buffer_counter == 1);
92*6db8c59fSFangrui Song   assert(__xray_log_flushLog() == XRayLogFlushStatus::XRAY_LOG_FLUSHED);
93*6db8c59fSFangrui Song   assert(__xray_log_select_mode("not-found") ==
94*6db8c59fSFangrui Song          XRayLogRegisterStatus::XRAY_MODE_NOT_FOUND);
95*6db8c59fSFangrui Song }
96