Dean Michael Berris | 47e6b8c | 2018-05-04 06:27:53 +0000 | [diff] [blame] | 1 | //===-- xray_basic_logging.cc -----------------------------------*- C++ -*-===// |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file is a part of XRay, a dynamic runtime instrumentation system. |
| 11 | // |
| 12 | // Implementation of a simple in-memory log of XRay events. This defines a |
| 13 | // logging function that's compatible with the XRay handler interface, and |
| 14 | // routines for exporting data to files. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
| 18 | #include <cassert> |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 19 | #include <cstring> |
Dean Michael Berris | 9952d95 | 2017-08-02 04:51:40 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 21 | #include <fcntl.h> |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 22 | #include <pthread.h> |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 23 | #include <sys/stat.h> |
| 24 | #include <sys/syscall.h> |
| 25 | #include <sys/types.h> |
Dean Michael Berris | 9952d95 | 2017-08-02 04:51:40 +0000 | [diff] [blame] | 26 | #include <time.h> |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 27 | #include <unistd.h> |
Dean Michael Berris | d1617cd | 2016-09-20 14:35:57 +0000 | [diff] [blame] | 28 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 29 | #include "sanitizer_common/sanitizer_allocator_internal.h" |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 30 | #include "sanitizer_common/sanitizer_libc.h" |
| 31 | #include "xray/xray_records.h" |
Dean Michael Berris | c561970 | 2018-06-06 06:07:48 +0000 | [diff] [blame^] | 32 | #include "xray_recursion_guard.h" |
Dean Michael Berris | 47e6b8c | 2018-05-04 06:27:53 +0000 | [diff] [blame] | 33 | #include "xray_basic_flags.h" |
| 34 | #include "xray_basic_logging.h" |
Dean Michael Berris | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 35 | #include "xray_defs.h" |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 36 | #include "xray_flags.h" |
| 37 | #include "xray_interface_internal.h" |
Tim Shen | 7d0bffb | 2017-02-10 20:30:43 +0000 | [diff] [blame] | 38 | #include "xray_tsc.h" |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 39 | #include "xray_utils.h" |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 40 | |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 41 | namespace __xray { |
| 42 | |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 43 | SpinMutex LogMutex; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 44 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 45 | // We use elements of this type to record the entry TSC of every function ID we |
| 46 | // see as we're tracing a particular thread's execution. |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 47 | struct alignas(16) StackEntry { |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 48 | int32_t FuncId; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 49 | uint16_t Type; |
| 50 | uint8_t CPU; |
| 51 | uint8_t Padding; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 52 | uint64_t TSC; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 53 | }; |
| 54 | |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 55 | static_assert(sizeof(StackEntry) == 16, "Wrong size for StackEntry"); |
| 56 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 57 | struct alignas(64) ThreadLocalData { |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 58 | void *InMemoryBuffer = nullptr; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 59 | size_t BufferSize = 0; |
| 60 | size_t BufferOffset = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 61 | void *ShadowStack = nullptr; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 62 | size_t StackSize = 0; |
| 63 | size_t StackEntries = 0; |
| 64 | int Fd = -1; |
Kamil Rytarowski | 4d4ed0e | 2018-02-15 14:17:15 +0000 | [diff] [blame] | 65 | tid_t TID = 0; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 66 | }; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 67 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 68 | static pthread_key_t PThreadKey; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 69 | |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 70 | static atomic_uint8_t BasicInitialized{0}; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 71 | |
| 72 | BasicLoggingOptions GlobalOptions; |
| 73 | |
Dean Michael Berris | c561970 | 2018-06-06 06:07:48 +0000 | [diff] [blame^] | 74 | thread_local atomic_uint8_t Guard{0}; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 75 | |
| 76 | static uint64_t thresholdTicks() XRAY_NEVER_INSTRUMENT { |
| 77 | static uint64_t TicksPerSec = probeRequiredCPUFeatures() |
| 78 | ? getTSCFrequency() |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 79 | : NanosecondsPerSecond; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 80 | static const uint64_t ThresholdTicks = |
| 81 | TicksPerSec * GlobalOptions.DurationFilterMicros / 1000000; |
| 82 | return ThresholdTicks; |
| 83 | } |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 84 | |
| 85 | static int openLogFile() XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 86 | int F = getLogFD(); |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 87 | if (F == -1) |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 88 | return -1; |
Douglas Yung | 8439c8e | 2017-04-18 03:25:11 +0000 | [diff] [blame] | 89 | |
| 90 | // Test for required CPU features and cache the cycle frequency |
| 91 | static bool TSCSupported = probeRequiredCPUFeatures(); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 92 | static uint64_t CycleFrequency = |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 93 | TSCSupported ? getTSCFrequency() : NanosecondsPerSecond; |
Douglas Yung | 8439c8e | 2017-04-18 03:25:11 +0000 | [diff] [blame] | 94 | |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 95 | // Since we're here, we get to write the header. We set it up so that the |
| 96 | // header will only be written once, at the start, and let the threads |
| 97 | // logging do writes which just append. |
| 98 | XRayFileHeader Header; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 99 | Header.Version = 2; // Version 2 includes tail exit records. |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 100 | Header.Type = FileTypes::NAIVE_LOG; |
Douglas Yung | 8439c8e | 2017-04-18 03:25:11 +0000 | [diff] [blame] | 101 | Header.CycleFrequency = CycleFrequency; |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 102 | |
| 103 | // FIXME: Actually check whether we have 'constant_tsc' and 'nonstop_tsc' |
| 104 | // before setting the values in the header. |
| 105 | Header.ConstantTSC = 1; |
| 106 | Header.NonstopTSC = 1; |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 107 | retryingWriteAll(F, reinterpret_cast<char *>(&Header), |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 108 | reinterpret_cast<char *>(&Header) + sizeof(Header)); |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 109 | return F; |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 112 | int getGlobalFd() XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 113 | static int Fd = openLogFile(); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 114 | return Fd; |
| 115 | } |
| 116 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 117 | ThreadLocalData &getThreadLocalData() XRAY_NEVER_INSTRUMENT { |
| 118 | thread_local ThreadLocalData TLD; |
| 119 | thread_local bool UNUSED TOnce = [] { |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 120 | if (GlobalOptions.ThreadBufferSize == 0) { |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 121 | if (Verbosity()) |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 122 | Report("Not initializing TLD since ThreadBufferSize == 0.\n"); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 123 | return false; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 124 | } |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 125 | TLD.TID = GetTid(); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 126 | pthread_setspecific(PThreadKey, &TLD); |
| 127 | TLD.Fd = getGlobalFd(); |
| 128 | TLD.InMemoryBuffer = reinterpret_cast<XRayRecord *>( |
| 129 | InternalAlloc(sizeof(XRayRecord) * GlobalOptions.ThreadBufferSize, |
| 130 | nullptr, alignof(XRayRecord))); |
| 131 | TLD.BufferSize = GlobalOptions.ThreadBufferSize; |
| 132 | TLD.BufferOffset = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 133 | if (GlobalOptions.MaxStackDepth == 0) { |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 134 | if (Verbosity()) |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 135 | Report("Not initializing the ShadowStack since MaxStackDepth == 0.\n"); |
| 136 | TLD.StackSize = 0; |
| 137 | TLD.StackEntries = 0; |
| 138 | TLD.ShadowStack = nullptr; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 139 | return false; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 140 | } |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 141 | TLD.ShadowStack = reinterpret_cast<StackEntry *>( |
| 142 | InternalAlloc(sizeof(StackEntry) * GlobalOptions.MaxStackDepth, nullptr, |
| 143 | alignof(StackEntry))); |
| 144 | TLD.StackSize = GlobalOptions.MaxStackDepth; |
| 145 | TLD.StackEntries = 0; |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 146 | if (Verbosity() >= 2) { |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 147 | static auto UNUSED Once = [] { |
| 148 | auto ticks = thresholdTicks(); |
| 149 | Report("Ticks threshold: %d\n", ticks); |
| 150 | return false; |
| 151 | }(); |
| 152 | } |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 153 | return false; |
| 154 | }(); |
| 155 | return TLD; |
| 156 | } |
| 157 | |
Dean Michael Berris | 66443d8 | 2017-03-15 02:28:00 +0000 | [diff] [blame] | 158 | template <class RDTSC> |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 159 | void InMemoryRawLog(int32_t FuncId, XRayEntryType Type, |
| 160 | RDTSC ReadTSC) XRAY_NEVER_INSTRUMENT { |
| 161 | auto &TLD = getThreadLocalData(); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 162 | int Fd = getGlobalFd(); |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 163 | if (Fd == -1) |
| 164 | return; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 165 | |
Dean Michael Berris | 484fe0a | 2017-09-18 06:18:03 +0000 | [diff] [blame] | 166 | // Use a simple recursion guard, to handle cases where we're already logging |
| 167 | // and for one reason or another, this function gets called again in the same |
| 168 | // thread. |
Dean Michael Berris | c561970 | 2018-06-06 06:07:48 +0000 | [diff] [blame^] | 169 | RecursionGuard G(Guard); |
| 170 | if (!G) |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 171 | return; |
Dean Michael Berris | 484fe0a | 2017-09-18 06:18:03 +0000 | [diff] [blame] | 172 | |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 173 | uint8_t CPU = 0; |
| 174 | uint64_t TSC = ReadTSC(CPU); |
| 175 | |
| 176 | switch (Type) { |
| 177 | case XRayEntryType::ENTRY: |
| 178 | case XRayEntryType::LOG_ARGS_ENTRY: { |
| 179 | // Short circuit if we've reached the maximum depth of the stack. |
| 180 | if (TLD.StackEntries++ >= TLD.StackSize) |
| 181 | return; |
| 182 | |
| 183 | // When we encounter an entry event, we keep track of the TSC and the CPU, |
| 184 | // and put it in the stack. |
| 185 | StackEntry E; |
| 186 | E.FuncId = FuncId; |
| 187 | E.CPU = CPU; |
| 188 | E.Type = Type; |
| 189 | E.TSC = TSC; |
| 190 | auto StackEntryPtr = static_cast<char *>(TLD.ShadowStack) + |
| 191 | (sizeof(StackEntry) * (TLD.StackEntries - 1)); |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 192 | internal_memcpy(StackEntryPtr, &E, sizeof(StackEntry)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | case XRayEntryType::EXIT: |
| 196 | case XRayEntryType::TAIL: { |
| 197 | if (TLD.StackEntries == 0) |
| 198 | break; |
| 199 | |
| 200 | if (--TLD.StackEntries >= TLD.StackSize) |
| 201 | return; |
| 202 | |
| 203 | // When we encounter an exit event, we check whether all the following are |
| 204 | // true: |
| 205 | // |
| 206 | // - The Function ID is the same as the most recent entry in the stack. |
| 207 | // - The CPU is the same as the most recent entry in the stack. |
| 208 | // - The Delta of the TSCs is less than the threshold amount of time we're |
| 209 | // looking to record. |
| 210 | // |
| 211 | // If all of these conditions are true, we pop the stack and don't write a |
| 212 | // record and move the record offset back. |
| 213 | StackEntry StackTop; |
| 214 | auto StackEntryPtr = static_cast<char *>(TLD.ShadowStack) + |
| 215 | (sizeof(StackEntry) * TLD.StackEntries); |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 216 | internal_memcpy(&StackTop, StackEntryPtr, sizeof(StackEntry)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 217 | if (StackTop.FuncId == FuncId && StackTop.CPU == CPU && |
| 218 | StackTop.TSC < TSC) { |
| 219 | auto Delta = TSC - StackTop.TSC; |
| 220 | if (Delta < thresholdTicks()) { |
| 221 | assert(TLD.BufferOffset > 0); |
| 222 | TLD.BufferOffset -= StackTop.Type == XRayEntryType::ENTRY ? 1 : 2; |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | break; |
| 227 | } |
| 228 | default: |
| 229 | // Should be unreachable. |
| 230 | assert(false && "Unsupported XRayEntryType encountered."); |
| 231 | break; |
| 232 | } |
| 233 | |
| 234 | // First determine whether the delta between the function's enter record and |
| 235 | // the exit record is higher than the threshold. |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 236 | XRayRecord R; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 237 | R.RecordType = RecordTypes::NORMAL; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 238 | R.CPU = CPU; |
| 239 | R.TSC = TSC; |
| 240 | R.TId = TLD.TID; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 241 | R.Type = Type; |
| 242 | R.FuncId = FuncId; |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 243 | auto FirstEntry = reinterpret_cast<XRayRecord *>(TLD.InMemoryBuffer); |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 244 | internal_memcpy(FirstEntry + TLD.BufferOffset, &R, sizeof(R)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 245 | if (++TLD.BufferOffset == TLD.BufferSize) { |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 246 | SpinMutexLock L(&LogMutex); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame] | 247 | retryingWriteAll(Fd, reinterpret_cast<char *>(FirstEntry), |
| 248 | reinterpret_cast<char *>(FirstEntry + TLD.BufferOffset)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 249 | TLD.BufferOffset = 0; |
| 250 | TLD.StackEntries = 0; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 254 | template <class RDTSC> |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 255 | void InMemoryRawLogWithArg(int32_t FuncId, XRayEntryType Type, uint64_t Arg1, |
| 256 | RDTSC ReadTSC) XRAY_NEVER_INSTRUMENT { |
| 257 | auto &TLD = getThreadLocalData(); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame] | 258 | auto FirstEntry = |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 259 | reinterpret_cast<XRayArgPayload *>(TLD.InMemoryBuffer); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 260 | const auto &BuffLen = TLD.BufferSize; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 261 | int Fd = getGlobalFd(); |
| 262 | if (Fd == -1) |
| 263 | return; |
| 264 | |
| 265 | // First we check whether there's enough space to write the data consecutively |
| 266 | // in the thread-local buffer. If not, we first flush the buffer before |
| 267 | // attempting to write the two records that must be consecutive. |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame] | 268 | if (TLD.BufferOffset + 2 > BuffLen) { |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 269 | SpinMutexLock L(&LogMutex); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame] | 270 | retryingWriteAll(Fd, reinterpret_cast<char *>(FirstEntry), |
| 271 | reinterpret_cast<char *>(FirstEntry + TLD.BufferOffset)); |
| 272 | TLD.BufferOffset = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 273 | TLD.StackEntries = 0; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | // Then we write the "we have an argument" record. |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 277 | InMemoryRawLog(FuncId, Type, ReadTSC); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 278 | |
Dean Michael Berris | c561970 | 2018-06-06 06:07:48 +0000 | [diff] [blame^] | 279 | RecursionGuard G(Guard); |
| 280 | if (!G) |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 281 | return; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 282 | |
| 283 | // And from here on write the arg payload. |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 284 | XRayArgPayload R; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 285 | R.RecordType = RecordTypes::ARG_PAYLOAD; |
| 286 | R.FuncId = FuncId; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 287 | R.TId = TLD.TID; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 288 | R.Arg = Arg1; |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 289 | internal_memcpy(FirstEntry + TLD.BufferOffset, &R, sizeof(R)); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame] | 290 | if (++TLD.BufferOffset == BuffLen) { |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 291 | SpinMutexLock L(&LogMutex); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame] | 292 | retryingWriteAll(Fd, reinterpret_cast<char *>(FirstEntry), |
| 293 | reinterpret_cast<char *>(FirstEntry + TLD.BufferOffset)); |
| 294 | TLD.BufferOffset = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 295 | TLD.StackEntries = 0; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 296 | } |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 299 | void basicLoggingHandleArg0RealTSC(int32_t FuncId, |
| 300 | XRayEntryType Type) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 301 | InMemoryRawLog(FuncId, Type, readTSC); |
Dean Michael Berris | 66443d8 | 2017-03-15 02:28:00 +0000 | [diff] [blame] | 302 | } |
| 303 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 304 | void basicLoggingHandleArg0EmulateTSC(int32_t FuncId, XRayEntryType Type) |
| 305 | XRAY_NEVER_INSTRUMENT { |
| 306 | InMemoryRawLog(FuncId, Type, [](uint8_t &CPU) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | 66443d8 | 2017-03-15 02:28:00 +0000 | [diff] [blame] | 307 | timespec TS; |
| 308 | int result = clock_gettime(CLOCK_REALTIME, &TS); |
| 309 | if (result != 0) { |
| 310 | Report("clock_gettimg(2) return %d, errno=%d.", result, int(errno)); |
| 311 | TS = {0, 0}; |
| 312 | } |
| 313 | CPU = 0; |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 314 | return TS.tv_sec * NanosecondsPerSecond + TS.tv_nsec; |
Dean Michael Berris | 66443d8 | 2017-03-15 02:28:00 +0000 | [diff] [blame] | 315 | }); |
| 316 | } |
| 317 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 318 | void basicLoggingHandleArg1RealTSC(int32_t FuncId, XRayEntryType Type, |
| 319 | uint64_t Arg1) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 320 | InMemoryRawLogWithArg(FuncId, Type, Arg1, readTSC); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 323 | void basicLoggingHandleArg1EmulateTSC(int32_t FuncId, XRayEntryType Type, |
| 324 | uint64_t Arg1) XRAY_NEVER_INSTRUMENT { |
| 325 | InMemoryRawLogWithArg( |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 326 | FuncId, Type, Arg1, [](uint8_t &CPU) XRAY_NEVER_INSTRUMENT { |
| 327 | timespec TS; |
| 328 | int result = clock_gettime(CLOCK_REALTIME, &TS); |
| 329 | if (result != 0) { |
| 330 | Report("clock_gettimg(2) return %d, errno=%d.", result, int(errno)); |
| 331 | TS = {0, 0}; |
| 332 | } |
| 333 | CPU = 0; |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 334 | return TS.tv_sec * NanosecondsPerSecond + TS.tv_nsec; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 335 | }); |
| 336 | } |
| 337 | |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 338 | static void TLDDestructor(void *P) XRAY_NEVER_INSTRUMENT { |
| 339 | ThreadLocalData &TLD = *reinterpret_cast<ThreadLocalData *>(P); |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 340 | auto ExitGuard = at_scope_exit([&TLD] { |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 341 | // Clean up dynamic resources. |
| 342 | if (TLD.InMemoryBuffer) |
| 343 | InternalFree(TLD.InMemoryBuffer); |
| 344 | if (TLD.ShadowStack) |
| 345 | InternalFree(TLD.ShadowStack); |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 346 | if (Verbosity()) |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 347 | Report("Cleaned up log for TID: %d\n", TLD.TID); |
| 348 | }); |
| 349 | |
| 350 | if (TLD.Fd == -1 || TLD.BufferOffset == 0) { |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 351 | if (Verbosity()) |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 352 | Report("Skipping buffer for TID: %d; Fd = %d; Offset = %llu\n", TLD.TID, |
| 353 | TLD.Fd, TLD.BufferOffset); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | { |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 358 | SpinMutexLock L(&LogMutex); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 359 | retryingWriteAll(TLD.Fd, reinterpret_cast<char *>(TLD.InMemoryBuffer), |
| 360 | reinterpret_cast<char *>(TLD.InMemoryBuffer) + |
Dean Michael Berris | abbeb4c | 2018-06-05 10:12:58 +0000 | [diff] [blame] | 361 | (sizeof(XRayRecord) * TLD.BufferOffset)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // Because this thread's exit could be the last one trying to write to |
| 365 | // the file and that we're not able to close out the file properly, we |
| 366 | // sync instead and hope that the pending writes are flushed as the |
| 367 | // thread exits. |
| 368 | fsync(TLD.Fd); |
| 369 | } |
| 370 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 371 | XRayLogInitStatus basicLoggingInit(size_t BufferSize, size_t BufferMax, |
| 372 | void *Options, |
| 373 | size_t OptionsSize) XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 374 | uint8_t Expected = 0; |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 375 | if (!atomic_compare_exchange_strong( |
| 376 | &BasicInitialized, &Expected, 1, memory_order_acq_rel)) { |
| 377 | if (Verbosity()) |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 378 | Report("Basic logging already initialized.\n"); |
| 379 | return XRayLogInitStatus::XRAY_LOG_INITIALIZED; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Dean Michael Berris | 47e6b8c | 2018-05-04 06:27:53 +0000 | [diff] [blame] | 382 | static bool UNUSED Once = [] { |
| 383 | pthread_key_create(&PThreadKey, TLDDestructor); |
| 384 | return false; |
| 385 | }(); |
| 386 | |
| 387 | if (BufferSize == 0 && BufferMax == 0 && Options != nullptr) { |
| 388 | FlagParser P; |
| 389 | BasicFlags F; |
| 390 | F.setDefaults(); |
| 391 | registerXRayBasicFlags(&P, &F); |
| 392 | P.ParseString(useCompilerDefinedBasicFlags()); |
| 393 | auto *EnvOpts = GetEnv("XRAY_BASIC_OPTIONS"); |
| 394 | if (EnvOpts == nullptr) |
| 395 | EnvOpts = ""; |
| 396 | |
| 397 | P.ParseString(EnvOpts); |
| 398 | |
| 399 | // If XRAY_BASIC_OPTIONS was not defined, then we use the deprecated options |
| 400 | // set through XRAY_OPTIONS instead. |
| 401 | if (internal_strlen(EnvOpts) == 0) { |
| 402 | F.func_duration_threshold_us = |
| 403 | flags()->xray_naive_log_func_duration_threshold_us; |
| 404 | F.max_stack_depth = flags()->xray_naive_log_max_stack_depth; |
| 405 | F.thread_buffer_size = flags()->xray_naive_log_thread_buffer_size; |
| 406 | } |
| 407 | |
| 408 | P.ParseString(static_cast<const char *>(Options)); |
| 409 | GlobalOptions.ThreadBufferSize = F.thread_buffer_size; |
| 410 | GlobalOptions.DurationFilterMicros = F.func_duration_threshold_us; |
| 411 | GlobalOptions.MaxStackDepth = F.max_stack_depth; |
| 412 | } else if (OptionsSize != sizeof(BasicLoggingOptions)) { |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 413 | Report("Invalid options size, potential ABI mismatch; expected %d got %d", |
| 414 | sizeof(BasicLoggingOptions), OptionsSize); |
| 415 | return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED; |
Dean Michael Berris | 47e6b8c | 2018-05-04 06:27:53 +0000 | [diff] [blame] | 416 | } else { |
| 417 | if (Verbosity()) |
| 418 | Report("XRay Basic: struct-based init is deprecated, please use " |
| 419 | "string-based configuration instead.\n"); |
| 420 | GlobalOptions = *reinterpret_cast<BasicLoggingOptions *>(Options); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static auto UseRealTSC = probeRequiredCPUFeatures(); |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 424 | if (!UseRealTSC && Verbosity()) |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 425 | Report("WARNING: Required CPU features missing for XRay instrumentation, " |
| 426 | "using emulation instead.\n"); |
| 427 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 428 | __xray_set_handler_arg1(UseRealTSC ? basicLoggingHandleArg1RealTSC |
| 429 | : basicLoggingHandleArg1EmulateTSC); |
| 430 | __xray_set_handler(UseRealTSC ? basicLoggingHandleArg0RealTSC |
| 431 | : basicLoggingHandleArg0EmulateTSC); |
| 432 | __xray_remove_customevent_handler(); |
Keith Wyss | adb092e | 2018-04-17 21:28:53 +0000 | [diff] [blame] | 433 | __xray_remove_typedevent_handler(); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 434 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 435 | return XRayLogInitStatus::XRAY_LOG_INITIALIZED; |
| 436 | } |
| 437 | |
| 438 | XRayLogInitStatus basicLoggingFinalize() XRAY_NEVER_INSTRUMENT { |
| 439 | uint8_t Expected = 0; |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 440 | if (!atomic_compare_exchange_strong( |
| 441 | &BasicInitialized, &Expected, 0, memory_order_acq_rel) && |
| 442 | Verbosity()) |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 443 | Report("Basic logging already finalized.\n"); |
| 444 | |
| 445 | // Nothing really to do aside from marking state of the global to be |
| 446 | // uninitialized. |
| 447 | |
| 448 | return XRayLogInitStatus::XRAY_LOG_FINALIZED; |
| 449 | } |
| 450 | |
| 451 | XRayLogFlushStatus basicLoggingFlush() XRAY_NEVER_INSTRUMENT { |
| 452 | // This really does nothing, since flushing the logs happen at the end of a |
| 453 | // thread's lifetime, or when the buffers are full. |
| 454 | return XRayLogFlushStatus::XRAY_LOG_FLUSHED; |
| 455 | } |
| 456 | |
| 457 | // This is a handler that, effectively, does nothing. |
| 458 | void basicLoggingHandleArg0Empty(int32_t, XRayEntryType) XRAY_NEVER_INSTRUMENT { |
| 459 | } |
| 460 | |
| 461 | bool basicLogDynamicInitializer() XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | c360f41 | 2017-12-05 12:08:56 +0000 | [diff] [blame] | 462 | XRayLogImpl Impl{ |
| 463 | basicLoggingInit, |
| 464 | basicLoggingFinalize, |
| 465 | basicLoggingHandleArg0Empty, |
| 466 | basicLoggingFlush, |
| 467 | }; |
| 468 | auto RegistrationResult = __xray_log_register_mode("xray-basic", Impl); |
| 469 | if (RegistrationResult != XRayLogRegisterStatus::XRAY_REGISTRATION_OK && |
Dean Michael Berris | 47e6b8c | 2018-05-04 06:27:53 +0000 | [diff] [blame] | 470 | Verbosity()) |
Dean Michael Berris | c360f41 | 2017-12-05 12:08:56 +0000 | [diff] [blame] | 471 | Report("Cannot register XRay Basic Mode to 'xray-basic'; error = %d\n", |
| 472 | RegistrationResult); |
| 473 | if (flags()->xray_naive_log || |
Dean Michael Berris | 47e6b8c | 2018-05-04 06:27:53 +0000 | [diff] [blame] | 474 | !internal_strcmp(flags()->xray_mode, "xray-basic")) { |
| 475 | auto SelectResult = __xray_log_select_mode("xray-basic"); |
| 476 | if (SelectResult != XRayLogRegisterStatus::XRAY_REGISTRATION_OK) { |
| 477 | if (Verbosity()) |
| 478 | Report("Failed selecting XRay Basic Mode; error = %d\n", SelectResult); |
| 479 | return false; |
| 480 | } |
| 481 | |
| 482 | // We initialize the implementation using the data we get from the |
| 483 | // XRAY_BASIC_OPTIONS environment variable, at this point of the |
| 484 | // implementation. |
| 485 | auto *Env = GetEnv("XRAY_BASIC_OPTIONS"); |
| 486 | auto InitResult = |
| 487 | __xray_log_init_mode("xray-basic", Env == nullptr ? "" : Env); |
| 488 | if (InitResult != XRayLogInitStatus::XRAY_LOG_INITIALIZED) { |
| 489 | if (Verbosity()) |
| 490 | Report("Failed initializing XRay Basic Mode; error = %d\n", InitResult); |
| 491 | return false; |
| 492 | } |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 493 | static auto UNUSED Once = [] { |
| 494 | static auto UNUSED &TLD = getThreadLocalData(); |
Dean Michael Berris | 5eaaff6 | 2018-06-05 06:12:42 +0000 | [diff] [blame] | 495 | Atexit(+[] { TLDDestructor(&TLD); }); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 496 | return false; |
| 497 | }(); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 498 | } |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 499 | return true; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | } // namespace __xray |
| 503 | |
| 504 | static auto UNUSED Unused = __xray::basicLogDynamicInitializer(); |