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 // TraceFlag needs to be trivially destructible since it is used as global
58 ~TraceFlag() = default;
60 const char* name() const { return name_; }
62 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in
63 // opt builds (tracers are always on in dbg builds). The default in OSS is for
64 // tracers to be on since we support binary distributions of gRPC for the
65 // wrapped language (wr don't want to force recompilation to get tracing).
66 // Internally, however, for performance reasons, we compile them out by
67 // default, since internal build systems make recompiling trivial.
68 #define GRPC_USE_TRACERS // tracers on by default in OSS
69 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG)
71 #ifdef GRPC_THREADSAFE_TRACER
72 return gpr_atm_no_barrier_load(&value_) != 0;
75 #endif // GRPC_THREADSAFE_TRACER
78 bool enabled() { return false; }
79 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */
82 friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag);
83 friend class TraceFlagList;
85 void set_enabled(bool enabled) {
86 #ifdef GRPC_THREADSAFE_TRACER
87 gpr_atm_no_barrier_store(&value_, enabled);
93 TraceFlag* next_tracer_;
94 const char* const name_;
95 #ifdef GRPC_THREADSAFE_TRACER
103 typedef TraceFlag DebugOnlyTraceFlag;
105 class DebugOnlyTraceFlag {
107 constexpr DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
108 constexpr bool enabled() const { return false; }
109 constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
112 void set_enabled(bool enabled) {}
116 } // namespace grpc_core
118 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */