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 <grpc/support/atm.h> 25 #include <stdbool.h> 26 27 void grpc_tracer_init(const char* env_var_name); 28 void grpc_tracer_shutdown(void); 29 30 #if defined(__has_feature) 31 #if __has_feature(thread_sanitizer) 32 #define GRPC_THREADSAFE_TRACER 33 #endif 34 #endif 35 36 namespace grpc_core { 37 38 class TraceFlag; 39 class TraceFlagList { 40 public: 41 static bool Set(const char* name, bool enabled); 42 static void Add(TraceFlag* flag); 43 44 private: 45 static void LogAllTracers(); 46 static TraceFlag* root_tracer_; 47 }; 48 49 namespace testing { 50 void grpc_tracer_enable_flag(grpc_core::TraceFlag* flag); 51 } 52 53 class TraceFlag { 54 public: 55 TraceFlag(bool default_enabled, const char* name); ~TraceFlag()56 ~TraceFlag() {} 57 name()58 const char* name() const { return name_; } 59 60 // Use the symbol GRPC_USE_TRACERS to determine if tracers will be enabled in 61 // opt builds (tracers are always on in dbg builds). The default in OSS is for 62 // tracers to be on since we support binary distributions of gRPC for the 63 // wrapped language (wr don't want to force recompilation to get tracing). 64 // Internally, however, for performance reasons, we compile them out by 65 // default, since internal build systems make recompiling trivial. 66 #define GRPC_USE_TRACERS // tracers on by default in OSS 67 #if defined(GRPC_USE_TRACERS) || !defined(NDEBUG) enabled()68 bool enabled() { 69 #ifdef GRPC_THREADSAFE_TRACER 70 return gpr_atm_no_barrier_load(&value_) != 0; 71 #else 72 return value_; 73 #endif // GRPC_THREADSAFE_TRACER 74 } 75 #else enabled()76 bool enabled() { return false; } 77 #endif /* defined(GRPC_USE_TRACERS) || !defined(NDEBUG) */ 78 79 private: 80 friend void grpc_core::testing::grpc_tracer_enable_flag(TraceFlag* flag); 81 friend class TraceFlagList; 82 set_enabled(bool enabled)83 void set_enabled(bool enabled) { 84 #ifdef GRPC_THREADSAFE_TRACER 85 gpr_atm_no_barrier_store(&value_, enabled); 86 #else 87 value_ = enabled; 88 #endif 89 } 90 91 TraceFlag* next_tracer_; 92 const char* const name_; 93 #ifdef GRPC_THREADSAFE_TRACER 94 gpr_atm value_; 95 #else 96 bool value_; 97 #endif 98 }; 99 100 #ifndef NDEBUG 101 typedef TraceFlag DebugOnlyTraceFlag; 102 #else 103 class DebugOnlyTraceFlag { 104 public: DebugOnlyTraceFlag(bool default_enabled,const char * name)105 DebugOnlyTraceFlag(bool default_enabled, const char* name) {} enabled()106 bool enabled() { return false; } 107 108 private: set_enabled(bool enabled)109 void set_enabled(bool enabled) {} 110 }; 111 #endif 112 113 } // namespace grpc_core 114 115 #endif /* GRPC_CORE_LIB_DEBUG_TRACE_H */ 116