Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 1 | //===-- xray_inmemory_log.cc ------------------------------------*- C++ -*-===// |
| 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 | 4031e4b | 2016-11-16 01:01:13 +0000 | [diff] [blame] | 32 | #include "xray_defs.h" |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 33 | #include "xray_flags.h" |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 34 | #include "xray_inmemory_log.h" |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 35 | #include "xray_interface_internal.h" |
Tim Shen | 7d0bffb | 2017-02-10 20:30:43 +0000 | [diff] [blame] | 36 | #include "xray_tsc.h" |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 37 | #include "xray_utils.h" |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 38 | |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 39 | namespace __xray { |
| 40 | |
Dean Michael Berris | 9952d95 | 2017-08-02 04:51:40 +0000 | [diff] [blame] | 41 | __sanitizer::SpinMutex LogMutex; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 42 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 43 | // We use elements of this type to record the entry TSC of every function ID we |
| 44 | // see as we're tracing a particular thread's execution. |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 45 | struct alignas(16) StackEntry { |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 46 | int32_t FuncId; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 47 | uint16_t Type; |
| 48 | uint8_t CPU; |
| 49 | uint8_t Padding; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 50 | uint64_t TSC; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 53 | static_assert(sizeof(StackEntry) == 16, "Wrong size for StackEntry"); |
| 54 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 55 | struct alignas(64) ThreadLocalData { |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 56 | void *InMemoryBuffer = nullptr; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 57 | size_t BufferSize = 0; |
| 58 | size_t BufferOffset = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 59 | void *ShadowStack = nullptr; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 60 | size_t StackSize = 0; |
| 61 | size_t StackEntries = 0; |
| 62 | int Fd = -1; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 63 | pid_t TID = 0; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 64 | }; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 65 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 66 | static pthread_key_t PThreadKey; |
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 __sanitizer::atomic_uint8_t BasicInitialized{0}; |
| 69 | |
| 70 | BasicLoggingOptions GlobalOptions; |
| 71 | |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 72 | thread_local volatile bool RecursionGuard = false; |
| 73 | |
| 74 | static uint64_t thresholdTicks() XRAY_NEVER_INSTRUMENT { |
| 75 | static uint64_t TicksPerSec = probeRequiredCPUFeatures() |
| 76 | ? getTSCFrequency() |
| 77 | : __xray::NanosecondsPerSecond; |
| 78 | static const uint64_t ThresholdTicks = |
| 79 | TicksPerSec * GlobalOptions.DurationFilterMicros / 1000000; |
| 80 | return ThresholdTicks; |
| 81 | } |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 82 | |
| 83 | static int openLogFile() XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 84 | int F = getLogFD(); |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 85 | if (F == -1) |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 86 | return -1; |
Douglas Yung | 8439c8e | 2017-04-18 03:25:11 +0000 | [diff] [blame] | 87 | |
| 88 | // Test for required CPU features and cache the cycle frequency |
| 89 | static bool TSCSupported = probeRequiredCPUFeatures(); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 90 | static uint64_t CycleFrequency = |
| 91 | TSCSupported ? getTSCFrequency() : __xray::NanosecondsPerSecond; |
Douglas Yung | 8439c8e | 2017-04-18 03:25:11 +0000 | [diff] [blame] | 92 | |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 93 | // Since we're here, we get to write the header. We set it up so that the |
| 94 | // header will only be written once, at the start, and let the threads |
| 95 | // logging do writes which just append. |
| 96 | XRayFileHeader Header; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 97 | Header.Version = 2; // Version 2 includes tail exit records. |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 98 | Header.Type = FileTypes::NAIVE_LOG; |
Douglas Yung | 8439c8e | 2017-04-18 03:25:11 +0000 | [diff] [blame] | 99 | Header.CycleFrequency = CycleFrequency; |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 100 | |
| 101 | // FIXME: Actually check whether we have 'constant_tsc' and 'nonstop_tsc' |
| 102 | // before setting the values in the header. |
| 103 | Header.ConstantTSC = 1; |
| 104 | Header.NonstopTSC = 1; |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 105 | retryingWriteAll(F, reinterpret_cast<char *>(&Header), |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 106 | reinterpret_cast<char *>(&Header) + sizeof(Header)); |
Dean Michael Berris | e7dbebf | 2017-01-25 03:50:46 +0000 | [diff] [blame] | 107 | return F; |
Dean Michael Berris | 0aba357 | 2017-01-03 04:04:00 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 110 | int getGlobalFd() XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 111 | static int Fd = openLogFile(); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 112 | return Fd; |
| 113 | } |
| 114 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 115 | ThreadLocalData &getThreadLocalData() XRAY_NEVER_INSTRUMENT { |
| 116 | thread_local ThreadLocalData TLD; |
| 117 | thread_local bool UNUSED TOnce = [] { |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 118 | if (GlobalOptions.ThreadBufferSize == 0) { |
| 119 | if (__sanitizer::Verbosity()) |
| 120 | Report("Not initializing TLD since ThreadBufferSize == 0.\n"); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 121 | return false; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 122 | } |
| 123 | TLD.TID = __sanitizer::GetTid(); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 124 | pthread_setspecific(PThreadKey, &TLD); |
| 125 | TLD.Fd = getGlobalFd(); |
| 126 | TLD.InMemoryBuffer = reinterpret_cast<XRayRecord *>( |
| 127 | InternalAlloc(sizeof(XRayRecord) * GlobalOptions.ThreadBufferSize, |
| 128 | nullptr, alignof(XRayRecord))); |
| 129 | TLD.BufferSize = GlobalOptions.ThreadBufferSize; |
| 130 | TLD.BufferOffset = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 131 | if (GlobalOptions.MaxStackDepth == 0) { |
| 132 | if (__sanitizer::Verbosity()) |
| 133 | Report("Not initializing the ShadowStack since MaxStackDepth == 0.\n"); |
| 134 | TLD.StackSize = 0; |
| 135 | TLD.StackEntries = 0; |
| 136 | TLD.ShadowStack = nullptr; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 137 | return false; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 138 | } |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 139 | TLD.ShadowStack = reinterpret_cast<StackEntry *>( |
| 140 | InternalAlloc(sizeof(StackEntry) * GlobalOptions.MaxStackDepth, nullptr, |
| 141 | alignof(StackEntry))); |
| 142 | TLD.StackSize = GlobalOptions.MaxStackDepth; |
| 143 | TLD.StackEntries = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 144 | if (__sanitizer::Verbosity() >= 2) { |
| 145 | static auto UNUSED Once = [] { |
| 146 | auto ticks = thresholdTicks(); |
| 147 | Report("Ticks threshold: %d\n", ticks); |
| 148 | return false; |
| 149 | }(); |
| 150 | } |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 151 | return false; |
| 152 | }(); |
| 153 | return TLD; |
| 154 | } |
| 155 | |
Dean Michael Berris | 66443d8 | 2017-03-15 02:28:00 +0000 | [diff] [blame] | 156 | template <class RDTSC> |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 157 | void InMemoryRawLog(int32_t FuncId, XRayEntryType Type, |
| 158 | RDTSC ReadTSC) XRAY_NEVER_INSTRUMENT { |
| 159 | auto &TLD = getThreadLocalData(); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 160 | int Fd = getGlobalFd(); |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 161 | if (Fd == -1) |
| 162 | return; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 163 | |
Dean Michael Berris | 484fe0a | 2017-09-18 06:18:03 +0000 | [diff] [blame] | 164 | // Use a simple recursion guard, to handle cases where we're already logging |
| 165 | // and for one reason or another, this function gets called again in the same |
| 166 | // thread. |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 167 | if (RecursionGuard) |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 168 | return; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 169 | RecursionGuard = true; |
| 170 | auto ExitGuard = __sanitizer::at_scope_exit([] { RecursionGuard = false; }); |
Dean Michael Berris | 484fe0a | 2017-09-18 06:18:03 +0000 | [diff] [blame] | 171 | |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 172 | uint8_t CPU = 0; |
| 173 | uint64_t TSC = ReadTSC(CPU); |
| 174 | |
| 175 | switch (Type) { |
| 176 | case XRayEntryType::ENTRY: |
| 177 | case XRayEntryType::LOG_ARGS_ENTRY: { |
| 178 | // Short circuit if we've reached the maximum depth of the stack. |
| 179 | if (TLD.StackEntries++ >= TLD.StackSize) |
| 180 | return; |
| 181 | |
| 182 | // When we encounter an entry event, we keep track of the TSC and the CPU, |
| 183 | // and put it in the stack. |
| 184 | StackEntry E; |
| 185 | E.FuncId = FuncId; |
| 186 | E.CPU = CPU; |
| 187 | E.Type = Type; |
| 188 | E.TSC = TSC; |
| 189 | auto StackEntryPtr = static_cast<char *>(TLD.ShadowStack) + |
| 190 | (sizeof(StackEntry) * (TLD.StackEntries - 1)); |
| 191 | __sanitizer::internal_memcpy(StackEntryPtr, &E, sizeof(StackEntry)); |
| 192 | break; |
| 193 | } |
| 194 | case XRayEntryType::EXIT: |
| 195 | case XRayEntryType::TAIL: { |
| 196 | if (TLD.StackEntries == 0) |
| 197 | break; |
| 198 | |
| 199 | if (--TLD.StackEntries >= TLD.StackSize) |
| 200 | return; |
| 201 | |
| 202 | // When we encounter an exit event, we check whether all the following are |
| 203 | // true: |
| 204 | // |
| 205 | // - The Function ID is the same as the most recent entry in the stack. |
| 206 | // - The CPU is the same as the most recent entry in the stack. |
| 207 | // - The Delta of the TSCs is less than the threshold amount of time we're |
| 208 | // looking to record. |
| 209 | // |
| 210 | // If all of these conditions are true, we pop the stack and don't write a |
| 211 | // record and move the record offset back. |
| 212 | StackEntry StackTop; |
| 213 | auto StackEntryPtr = static_cast<char *>(TLD.ShadowStack) + |
| 214 | (sizeof(StackEntry) * TLD.StackEntries); |
| 215 | __sanitizer::internal_memcpy(&StackTop, StackEntryPtr, sizeof(StackEntry)); |
| 216 | if (StackTop.FuncId == FuncId && StackTop.CPU == CPU && |
| 217 | StackTop.TSC < TSC) { |
| 218 | auto Delta = TSC - StackTop.TSC; |
| 219 | if (Delta < thresholdTicks()) { |
| 220 | assert(TLD.BufferOffset > 0); |
| 221 | TLD.BufferOffset -= StackTop.Type == XRayEntryType::ENTRY ? 1 : 2; |
| 222 | return; |
| 223 | } |
| 224 | } |
| 225 | break; |
| 226 | } |
| 227 | default: |
| 228 | // Should be unreachable. |
| 229 | assert(false && "Unsupported XRayEntryType encountered."); |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | // First determine whether the delta between the function's enter record and |
| 234 | // the exit record is higher than the threshold. |
| 235 | __xray::XRayRecord R; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 236 | R.RecordType = RecordTypes::NORMAL; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 237 | R.CPU = CPU; |
| 238 | R.TSC = TSC; |
| 239 | R.TId = TLD.TID; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 240 | R.Type = Type; |
| 241 | R.FuncId = FuncId; |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame^] | 242 | auto FirstEntry = reinterpret_cast<__xray::XRayRecord *>(TLD.InMemoryBuffer); |
| 243 | __sanitizer::internal_memcpy(FirstEntry + TLD.BufferOffset, &R, sizeof(R)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 244 | if (++TLD.BufferOffset == TLD.BufferSize) { |
Dean Michael Berris | 9952d95 | 2017-08-02 04:51:40 +0000 | [diff] [blame] | 245 | __sanitizer::SpinMutexLock L(&LogMutex); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame^] | 246 | retryingWriteAll(Fd, reinterpret_cast<char *>(FirstEntry), |
| 247 | reinterpret_cast<char *>(FirstEntry + TLD.BufferOffset)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 248 | TLD.BufferOffset = 0; |
| 249 | TLD.StackEntries = 0; |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 253 | template <class RDTSC> |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 254 | void InMemoryRawLogWithArg(int32_t FuncId, XRayEntryType Type, uint64_t Arg1, |
| 255 | RDTSC ReadTSC) XRAY_NEVER_INSTRUMENT { |
| 256 | auto &TLD = getThreadLocalData(); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame^] | 257 | auto FirstEntry = |
| 258 | reinterpret_cast<__xray::XRayArgPayload *>(TLD.InMemoryBuffer); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 259 | const auto &BuffLen = TLD.BufferSize; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 260 | int Fd = getGlobalFd(); |
| 261 | if (Fd == -1) |
| 262 | return; |
| 263 | |
| 264 | // First we check whether there's enough space to write the data consecutively |
| 265 | // in the thread-local buffer. If not, we first flush the buffer before |
| 266 | // attempting to write the two records that must be consecutive. |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame^] | 267 | if (TLD.BufferOffset + 2 > BuffLen) { |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 268 | __sanitizer::SpinMutexLock L(&LogMutex); |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame^] | 269 | retryingWriteAll(Fd, reinterpret_cast<char *>(FirstEntry), |
| 270 | reinterpret_cast<char *>(FirstEntry + TLD.BufferOffset)); |
| 271 | TLD.BufferOffset = 0; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 272 | TLD.StackEntries = 0; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // Then we write the "we have an argument" record. |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 276 | InMemoryRawLog(FuncId, Type, ReadTSC); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 277 | |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 278 | if (RecursionGuard) |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 279 | return; |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 280 | RecursionGuard = true; |
| 281 | auto ExitGuard = __sanitizer::at_scope_exit([] { RecursionGuard = false; }); |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 282 | |
| 283 | // And from here on write the arg payload. |
| 284 | __xray::XRayArgPayload R; |
| 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; |
Martin Pelikan | 4834bca | 2018-01-19 13:18:40 +0000 | [diff] [blame^] | 289 | __sanitizer::internal_memcpy(FirstEntry + TLD.BufferOffset, &R, sizeof(R)); |
| 290 | if (++TLD.BufferOffset == BuffLen) { |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 291 | __sanitizer::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 { |
| 301 | InMemoryRawLog(FuncId, Type, __xray::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; |
| 314 | return TS.tv_sec * __xray::NanosecondsPerSecond + TS.tv_nsec; |
| 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 { |
| 320 | InMemoryRawLogWithArg(FuncId, Type, Arg1, __xray::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; |
| 334 | return TS.tv_sec * __xray::NanosecondsPerSecond + TS.tv_nsec; |
| 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); |
| 340 | auto ExitGuard = __sanitizer::at_scope_exit([&TLD] { |
| 341 | // Clean up dynamic resources. |
| 342 | if (TLD.InMemoryBuffer) |
| 343 | InternalFree(TLD.InMemoryBuffer); |
| 344 | if (TLD.ShadowStack) |
| 345 | InternalFree(TLD.ShadowStack); |
| 346 | if (__sanitizer::Verbosity()) |
| 347 | Report("Cleaned up log for TID: %d\n", TLD.TID); |
| 348 | }); |
| 349 | |
| 350 | if (TLD.Fd == -1 || TLD.BufferOffset == 0) { |
| 351 | if (__sanitizer::Verbosity()) |
| 352 | Report("Skipping buffer for TID: %d; Fd = %d; Offset = %llu\n", TLD.TID, |
| 353 | TLD.Fd, TLD.BufferOffset); |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | { |
| 358 | __sanitizer::SpinMutexLock L(&LogMutex); |
| 359 | retryingWriteAll(TLD.Fd, reinterpret_cast<char *>(TLD.InMemoryBuffer), |
| 360 | reinterpret_cast<char *>(TLD.InMemoryBuffer) + |
| 361 | (sizeof(__xray::XRayRecord) * TLD.BufferOffset)); |
| 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 { |
| 374 | static bool UNUSED Once = [] { |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 375 | pthread_key_create(&PThreadKey, TLDDestructor); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 376 | return false; |
| 377 | }(); |
| 378 | |
| 379 | uint8_t Expected = 0; |
| 380 | if (!__sanitizer::atomic_compare_exchange_strong( |
| 381 | &BasicInitialized, &Expected, 1, __sanitizer::memory_order_acq_rel)) { |
| 382 | if (__sanitizer::Verbosity()) |
| 383 | Report("Basic logging already initialized.\n"); |
| 384 | return XRayLogInitStatus::XRAY_LOG_INITIALIZED; |
Dean Michael Berris | 8dcba55 | 2017-10-05 05:45:51 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 387 | if (OptionsSize != sizeof(BasicLoggingOptions)) { |
| 388 | Report("Invalid options size, potential ABI mismatch; expected %d got %d", |
| 389 | sizeof(BasicLoggingOptions), OptionsSize); |
| 390 | return XRayLogInitStatus::XRAY_LOG_UNINITIALIZED; |
| 391 | } |
| 392 | |
| 393 | static auto UseRealTSC = probeRequiredCPUFeatures(); |
| 394 | if (!UseRealTSC && __sanitizer::Verbosity()) |
| 395 | Report("WARNING: Required CPU features missing for XRay instrumentation, " |
| 396 | "using emulation instead.\n"); |
| 397 | |
| 398 | GlobalOptions = *reinterpret_cast<BasicLoggingOptions *>(Options); |
| 399 | __xray_set_handler_arg1(UseRealTSC ? basicLoggingHandleArg1RealTSC |
| 400 | : basicLoggingHandleArg1EmulateTSC); |
| 401 | __xray_set_handler(UseRealTSC ? basicLoggingHandleArg0RealTSC |
| 402 | : basicLoggingHandleArg0EmulateTSC); |
| 403 | __xray_remove_customevent_handler(); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 404 | |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 405 | return XRayLogInitStatus::XRAY_LOG_INITIALIZED; |
| 406 | } |
| 407 | |
| 408 | XRayLogInitStatus basicLoggingFinalize() XRAY_NEVER_INSTRUMENT { |
| 409 | uint8_t Expected = 0; |
| 410 | if (!__sanitizer::atomic_compare_exchange_strong( |
| 411 | &BasicInitialized, &Expected, 0, __sanitizer::memory_order_acq_rel) && |
| 412 | __sanitizer::Verbosity()) |
| 413 | Report("Basic logging already finalized.\n"); |
| 414 | |
| 415 | // Nothing really to do aside from marking state of the global to be |
| 416 | // uninitialized. |
| 417 | |
| 418 | return XRayLogInitStatus::XRAY_LOG_FINALIZED; |
| 419 | } |
| 420 | |
| 421 | XRayLogFlushStatus basicLoggingFlush() XRAY_NEVER_INSTRUMENT { |
| 422 | // This really does nothing, since flushing the logs happen at the end of a |
| 423 | // thread's lifetime, or when the buffers are full. |
| 424 | return XRayLogFlushStatus::XRAY_LOG_FLUSHED; |
| 425 | } |
| 426 | |
| 427 | // This is a handler that, effectively, does nothing. |
| 428 | void basicLoggingHandleArg0Empty(int32_t, XRayEntryType) XRAY_NEVER_INSTRUMENT { |
| 429 | } |
| 430 | |
| 431 | bool basicLogDynamicInitializer() XRAY_NEVER_INSTRUMENT { |
Dean Michael Berris | c360f41 | 2017-12-05 12:08:56 +0000 | [diff] [blame] | 432 | XRayLogImpl Impl{ |
| 433 | basicLoggingInit, |
| 434 | basicLoggingFinalize, |
| 435 | basicLoggingHandleArg0Empty, |
| 436 | basicLoggingFlush, |
| 437 | }; |
| 438 | auto RegistrationResult = __xray_log_register_mode("xray-basic", Impl); |
| 439 | if (RegistrationResult != XRayLogRegisterStatus::XRAY_REGISTRATION_OK && |
| 440 | __sanitizer::Verbosity()) |
| 441 | Report("Cannot register XRay Basic Mode to 'xray-basic'; error = %d\n", |
| 442 | RegistrationResult); |
| 443 | if (flags()->xray_naive_log || |
| 444 | !__sanitizer::internal_strcmp(flags()->xray_mode, "xray-basic")) { |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 445 | __xray_set_log_impl(Impl); |
| 446 | BasicLoggingOptions Options; |
| 447 | Options.DurationFilterMicros = |
| 448 | flags()->xray_naive_log_func_duration_threshold_us; |
| 449 | Options.MaxStackDepth = flags()->xray_naive_log_max_stack_depth; |
| 450 | Options.ThreadBufferSize = flags()->xray_naive_log_thread_buffer_size; |
| 451 | __xray_log_init(flags()->xray_naive_log_thread_buffer_size, 0, &Options, |
| 452 | sizeof(BasicLoggingOptions)); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 453 | static auto UNUSED Once = [] { |
| 454 | static auto UNUSED &TLD = getThreadLocalData(); |
Dean Michael Berris | 21d0d53 | 2017-12-05 13:40:01 +0000 | [diff] [blame] | 455 | __sanitizer::Atexit(+[] { TLDDestructor(&TLD); }); |
Dean Michael Berris | 52517d7 | 2017-12-05 12:21:14 +0000 | [diff] [blame] | 456 | return false; |
| 457 | }(); |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 458 | } |
Dean Michael Berris | f50eb93 | 2016-08-26 06:39:33 +0000 | [diff] [blame] | 459 | return true; |
Dean Michael Berris | 364f11c | 2017-11-21 07:29:21 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | } // namespace __xray |
| 463 | |
| 464 | static auto UNUSED Unused = __xray::basicLogDynamicInitializer(); |