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>
26 #include <grpc/support/atm.h>
28 #include "src/core/lib/gprpp/global_config.h"
30 GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_trace);
32 // TODO(veblush): Remove this deprecated function once codes depending on this
33 // function are updated in the internal repo.
34 void grpc_tracer_init(const char* env_var_name);
36 void grpc_tracer_init();
37 void grpc_tracer_shutdown(void);
39 #if defined(__has_feature)
40 #if __has_feature(thread_sanitizer)
41 #define GRPC_THREADSAFE_TRACER
50 static bool Set(const char* name, bool enabled);
51 static void Add(TraceFlag* flag);
54 static void LogAllTracers();
55 static TraceFlag* root_tracer_;
59 void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag);
64 TraceFlag(bool default_enabled, const char* name);
65 // TraceFlag needs to be trivially destructible since it is used as global
67 ~TraceFlag() = default;
69 const char* name() const { return name_; }
71 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in
72 // opt builds (tracers are always on in dbg builds). The default in OSS is for
73 // tracers to be on since we support binary distributions of gRPC for the
74 // wrapped language (wr don't want to force recompilation to get tracing).
75 // Internally, however, for performance reasons, we compile them out by
76 // default, since internal build systems make recompiling trivial.
78 // Prefer GRPC_TRACE_FLAG_ENABLED() macro instead of using enabled() directly.
79 #define GRPC_USE_TRACERS // tracers on by default in OSS
80 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG)
82 #ifdef GRPC_THREADSAFE_TRACER
83 return gpr_atm_no_barrier_load(&value_) != 0;
86 #endif // GRPC_THREADSAFE_TRACER
89 bool enabled() { return false; }
90 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */
93 friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag);
94 friend class TraceFlagList;
96 void set_enabled(bool enabled) {
97 #ifdef GRPC_THREADSAFE_TRACER
98 gpr_atm_no_barrier_store(&value_, enabled);
104 TraceFlag* next_tracer_;
105 const char* const name_;
106 #ifdef GRPC_THREADSAFE_TRACER
113 #define GRPC_TRACE_FLAG_ENABLED(f) GPR_UNLIKELY((f).enabled())
116 typedef TraceFlag DebugOnlyTraceFlag;
118 class DebugOnlyTraceFlag {
120 constexpr DebugOnlyTraceFlag(bool /*default_enabled*/, const char* /*name*/) {
122 constexpr bool enabled() const { return false; }
123 constexpr const char* name() const { return "DebugOnlyTraceFlag"; }
126 void set_enabled(bool /*enabled*/) {}
130 } // namespace grpc_core
132 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */