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 #include "src/core/lib/gprpp/global_config.h"
29 GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_trace);
31 // TODO(veblush): Remove this deprecated function once codes depending on this
32 // function are updated in the internal repo.
33 void grpc_tracer_init(const char* env_var_name);
35 void grpc_tracer_init();
36 void grpc_tracer_shutdown(void);
38 #if defined(__has_feature)
39 #if __has_feature(thread_sanitizer)
40 #define GRPC_THREADSAFE_TRACER
49 static bool Set(const char* name, bool enabled);
50 static void Add(TraceFlag* flag);
53 static void LogAllTracers();
54 static TraceFlag* root_tracer_;
58 void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag);
63 TraceFlag(bool default_enabled, const char* name);
64 // TraceFlag needs to be trivially destructible since it is used as global
66 ~TraceFlag() = default;
68 const char* name() const { return name_; }
70 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in
71 // opt builds (tracers are always on in dbg builds). The default in OSS is for
72 // tracers to be on since we support binary distributions of gRPC for the
73 // wrapped language (wr don't want to force recompilation to get tracing).
74 // Internally, however, for performance reasons, we compile them out by
75 // default, since internal build systems make recompiling trivial.
77 // Prefer GRPC_TRACE_FLAG_ENABLED() macro instead of using enabled() directly.
78 #define GRPC_USE_TRACERS // tracers on by default in OSS
79 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG)
81 #ifdef GRPC_THREADSAFE_TRACER
82 return gpr_atm_no_barrier_load(&value_) != 0;
85 #endif // GRPC_THREADSAFE_TRACER
88 bool enabled() { return false; }
89 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */
92 friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag);
93 friend class TraceFlagList;
95 void set_enabled(bool enabled) {
96 #ifdef GRPC_THREADSAFE_TRACER
97 gpr_atm_no_barrier_store(&value_, enabled);
103 TraceFlag* next_tracer_;
104 const char* const name_;
105 #ifdef GRPC_THREADSAFE_TRACER
112 #define GRPC_TRACE_FLAG_ENABLED(f) GPR_UNLIKELY((f).enabled())
115 typedef TraceFlag DebugOnlyTraceFlag;
117 class DebugOnlyTraceFlag {
119 constexpr DebugOnlyTraceFlag(bool default_enabled, const char* name) {}
120 constexpr bool enabled() const { return false; }
121 constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
124 void set_enabled(bool enabled) {}
128 } // namespace grpc_core
130 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */