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 #include <grpc/support/port_platform.h>
21 #include "src/core/lib/debug/trace.h"
24 #include <type_traits>
26 #include <grpc/grpc.h>
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 GPR_GLOBAL_CONFIG_DEFINE_STRING(
32 "A comma separated list of tracers that provide additional insight into "
33 "how gRPC C core is processing requests via debug logs.");
35 int grpc_tracer_set_enabled(const char* name, int enabled);
39 TraceFlag* TraceFlagList::root_tracer_ = nullptr;
41 bool TraceFlagList::Set(const char* name, bool enabled) {
43 if (0 == strcmp(name, "all")) {
44 for (t = root_tracer_; t; t = t->next_tracer_) {
45 t->set_enabled(enabled);
47 } else if (0 == strcmp(name, "list_tracers")) {
49 } else if (0 == strcmp(name, "refcount")) {
50 for (t = root_tracer_; t; t = t->next_tracer_) {
51 if (strstr(t->name_, "refcount") != nullptr) {
52 t->set_enabled(enabled);
57 for (t = root_tracer_; t; t = t->next_tracer_) {
58 if (0 == strcmp(name, t->name_)) {
59 t->set_enabled(enabled);
63 // check for unknowns, but ignore "", to allow to GRPC_TRACE=
64 if (!found && 0 != strcmp(name, "")) {
65 gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name);
66 return false; /* early return */
72 void TraceFlagList::Add(TraceFlag* flag) {
73 flag->next_tracer_ = root_tracer_;
77 void TraceFlagList::LogAllTracers() {
78 gpr_log(GPR_DEBUG, "available tracers:");
80 for (t = root_tracer_; t != nullptr; t = t->next_tracer_) {
81 gpr_log(GPR_DEBUG, "\t%s", t->name_);
85 // Flags register themselves on the list during construction
86 TraceFlag::TraceFlag(bool default_enabled, const char* name) : name_(name) {
87 static_assert(std::is_trivially_destructible<TraceFlag>::value,
88 "TraceFlag needs to be trivially destructible.");
89 set_enabled(default_enabled);
90 TraceFlagList::Add(this);
93 } // namespace grpc_core
95 static void add(const char* beg, const char* end, char*** ss, size_t* ns) {
100 GPR_ASSERT(end >= beg);
101 len = static_cast<size_t>(end - beg);
102 s = static_cast<char*>(gpr_malloc(len + 1));
105 *ss = static_cast<char**>(gpr_realloc(*ss, sizeof(char**) * np));
110 static void split(const char* s, char*** ss, size_t* ns) {
111 const char* c = strchr(s, ',');
113 add(s, s + strlen(s), ss, ns);
116 split(c + 1, ss, ns);
120 static void parse(const char* s) {
121 char** strings = nullptr;
124 split(s, &strings, &nstrings);
126 for (i = 0; i < nstrings; i++) {
127 if (strings[i][0] == '-') {
128 grpc_core::TraceFlagList::Set(strings[i] + 1, false);
130 grpc_core::TraceFlagList::Set(strings[i], true);
134 for (i = 0; i < nstrings; i++) {
135 gpr_free(strings[i]);
140 void grpc_tracer_init(const char* env_var_name) {
141 (void)env_var_name; // suppress unused variable error
145 void grpc_tracer_init() {
146 grpc_core::UniquePtr<char> value = GPR_GLOBAL_CONFIG_GET(grpc_trace);
150 void grpc_tracer_shutdown(void) {}
152 int grpc_tracer_set_enabled(const char* name, int enabled) {
153 return grpc_core::TraceFlagList::Set(name, enabled != 0);