3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef GRPC_CORE_LIB_DEBUG_TRACE_H
20 #define GRPC_CORE_LIB_DEBUG_TRACE_H
22 #include <grpc/support/port_platform.h>
24 #include <grpc/support/atm.h>
27 void grpc_tracer_init(const char* env_var_name);
28 void grpc_tracer_shutdown(void);
30 #if defined(__has_feature)
31 #if __has_feature(thread_sanitizer)
32 #define GRPC_THREADSAFE_TRACER
41 static bool Set(const char* name, bool enabled);
42 static void Add(TraceFlag* flag);
45 static void LogAllTracers();
46 static TraceFlag* root_tracer_;
50 void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag);
55 TraceFlag(bool default_enabled, const char* name);
56 // This needs to be trivially destructible as it is used as global variable.
57 ~TraceFlag() = default;
59 const char* name() const { return name_; }
61 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in
62 // opt builds (tracers are always on in dbg builds). The default in OSS is for
63 // tracers to be on since we support binary distributions of gRPC for the
64 // wrapped language (wr don't want to force recompilation to get tracing).
65 // Internally, however, for performance reasons, we compile them out by
66 // default, since internal build systems make recompiling trivial.
67 #define GRPC_USE_TRACERS // tracers on by default in OSS
68 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG)
70 #ifdef GRPC_THREADSAFE_TRACER
71 return gpr_atm_no_barrier_load(&value_) != 0;
74 #endif // GRPC_THREADSAFE_TRACER
77 bool enabled() { return false; }
78 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */
81 friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag);
82 friend class TraceFlagList;
84 void set_enabled(bool enabled) {
85 #ifdef GRPC_THREADSAFE_TRACER
86 gpr_atm_no_barrier_store(&value_, enabled);
92 TraceFlag* next_tracer_;
93 const char* const name_;
94 #ifdef GRPC_THREADSAFE_TRACER
102 typedef TraceFlag DebugOnlyTraceFlag;
104 class DebugOnlyTraceFlag {
106 constexpr DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
107 constexpr bool enabled() const { return false; }
108 constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
111 void set_enabled(bool enabled) {}
115 } // namespace grpc_core
117 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */