1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 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 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 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. 16 * 17 */ 18 19 #ifndef GRPC_CORE_LIB_DEBUG_TRACE_H 20 #define GRPC_CORE_LIB_DEBUG_TRACE_H 21 22 #include <grpc/support/port_platform.h> 23 24 #include <stdbool.h> 25 26 #include <grpc/support/atm.h> 27 28 #include "src/core/lib/gprpp/global_config.h" 29 30 GPR_GLOBAL_CONFIG_DECLARE_STRING(grpc_trace); 31 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); 35 36 void grpc_tracer_init(); 37 void grpc_tracer_shutdown(void); 38 39 #if defined(__has_feature) 40 #if __has_feature(thread_sanitizer) 41 #define GRPC_THREADSAFE_TRACER 42 #endif 43 #endif 44 45 namespace grpc_core { 46 47 class TraceFlag; 48 class TraceFlagList { 49 public: 50 static bool Set(const char* name, bool enabled); 51 static void Add(TraceFlag* flag); 52 53 private: 54 static void LogAllTracers(); 55 static TraceFlag* root_tracer_; 56 }; 57 58 namespace testing { 59 void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag); 60 } 61 62 class TraceFlag { 63 public: 64 TraceFlag(bool default_enabled, const char* name); 65 // TraceFlag needs to be trivially destructible since it is used as global 66 // variable. 67 ~TraceFlag() = default; 68 name()69 const char* name() const { return name_; } 70 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. 77 // 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) enabled()81 bool enabled() { 82 #ifdef GRPC_THREADSAFE_TRACER 83 return gpr_atm_no_barrier_load(&value_) != 0; 84 #else 85 return value_; 86 #endif // GRPC_THREADSAFE_TRACER 87 } 88 #else enabled()89 bool enabled() { return false; } 90 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */ 91 92 private: 93 friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag); 94 friend class TraceFlagList; 95 set_enabled(bool enabled)96 void set_enabled(bool enabled) { 97 #ifdef GRPC_THREADSAFE_TRACER 98 gpr_atm_no_barrier_store(&value_, enabled); 99 #else 100 value_ = enabled; 101 #endif 102 } 103 104 TraceFlag* next_tracer_; 105 const char* const name_; 106 #ifdef GRPC_THREADSAFE_TRACER 107 gpr_atm value_; 108 #else 109 bool value_; 110 #endif 111 }; 112 113 #define GRPC_TRACE_FLAG_ENABLED(f) GPR_UNLIKELY((f).enabled()) 114 115 #ifndef NDEBUG 116 typedef TraceFlag DebugOnlyTraceFlag; 117 #else 118 class DebugOnlyTraceFlag { 119 public: DebugOnlyTraceFlag(bool,const char *)120 constexpr DebugOnlyTraceFlag(bool /*default_enabled*/, const char* /*name*/) { 121 } enabled()122 constexpr bool enabled() const { return false; } name()123 constexpr const char* name() const { return "DebugOnlyTraceFlag"; } 124 125 private: set_enabled(bool)126 void set_enabled(bool /*enabled*/) {} 127 }; 128 #endif 129 130 } // namespace grpc_core 131 132 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */ 133