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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/lib/debug/trace.h"
22
23 #include <string.h>
24
25 #include <grpc/grpc.h>
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28 #include "src/core/lib/gpr/env.h"
29
30 int grpc_tracer_set_enabled(const char* name, int enabled);
31
32 namespace grpc_core {
33
34 TraceFlag* TraceFlagList::root_tracer_ = nullptr;
35
Set(const char * name,bool enabled)36 bool TraceFlagList::Set(const char* name, bool enabled) {
37 TraceFlag* t;
38 if (0 == strcmp(name, "all")) {
39 for (t = root_tracer_; t; t = t->next_tracer_) {
40 t->set_enabled(enabled);
41 }
42 } else if (0 == strcmp(name, "list_tracers")) {
43 LogAllTracers();
44 } else if (0 == strcmp(name, "refcount")) {
45 for (t = root_tracer_; t; t = t->next_tracer_) {
46 if (strstr(t->name_, "refcount") != nullptr) {
47 t->set_enabled(enabled);
48 }
49 }
50 } else {
51 bool found = false;
52 for (t = root_tracer_; t; t = t->next_tracer_) {
53 if (0 == strcmp(name, t->name_)) {
54 t->set_enabled(enabled);
55 found = true;
56 }
57 }
58 // check for unknowns, but ignore "", to allow to GRPC_TRACE=
59 if (!found && 0 != strcmp(name, "")) {
60 gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name);
61 return false; /* early return */
62 }
63 }
64 return true;
65 }
66
Add(TraceFlag * flag)67 void TraceFlagList::Add(TraceFlag* flag) {
68 flag->next_tracer_ = root_tracer_;
69 root_tracer_ = flag;
70 }
71
LogAllTracers()72 void TraceFlagList::LogAllTracers() {
73 gpr_log(GPR_DEBUG, "available tracers:");
74 TraceFlag* t;
75 for (t = root_tracer_; t != nullptr; t = t->next_tracer_) {
76 gpr_log(GPR_DEBUG, "\t%s", t->name_);
77 }
78 }
79
80 // Flags register themselves on the list during construction
TraceFlag(bool default_enabled,const char * name)81 TraceFlag::TraceFlag(bool default_enabled, const char* name) : name_(name) {
82 set_enabled(default_enabled);
83 TraceFlagList::Add(this);
84 }
85
86 } // namespace grpc_core
87
add(const char * beg,const char * end,char *** ss,size_t * ns)88 static void add(const char* beg, const char* end, char*** ss, size_t* ns) {
89 size_t n = *ns;
90 size_t np = n + 1;
91 char* s;
92 size_t len;
93 GPR_ASSERT(end >= beg);
94 len = static_cast<size_t>(end - beg);
95 s = static_cast<char*>(gpr_malloc(len + 1));
96 memcpy(s, beg, len);
97 s[len] = 0;
98 *ss = static_cast<char**>(gpr_realloc(*ss, sizeof(char**) * np));
99 (*ss)[n] = s;
100 *ns = np;
101 }
102
split(const char * s,char *** ss,size_t * ns)103 static void split(const char* s, char*** ss, size_t* ns) {
104 const char* c = strchr(s, ',');
105 if (c == nullptr) {
106 add(s, s + strlen(s), ss, ns);
107 } else {
108 add(s, c, ss, ns);
109 split(c + 1, ss, ns);
110 }
111 }
112
parse(const char * s)113 static void parse(const char* s) {
114 char** strings = nullptr;
115 size_t nstrings = 0;
116 size_t i;
117 split(s, &strings, &nstrings);
118
119 for (i = 0; i < nstrings; i++) {
120 if (strings[i][0] == '-') {
121 grpc_core::TraceFlagList::Set(strings[i] + 1, false);
122 } else {
123 grpc_core::TraceFlagList::Set(strings[i], true);
124 }
125 }
126
127 for (i = 0; i < nstrings; i++) {
128 gpr_free(strings[i]);
129 }
130 gpr_free(strings);
131 }
132
grpc_tracer_init(const char * env_var)133 void grpc_tracer_init(const char* env_var) {
134 char* e = gpr_getenv(env_var);
135 if (e != nullptr) {
136 parse(e);
137 gpr_free(e);
138 }
139 }
140
grpc_tracer_shutdown(void)141 void grpc_tracer_shutdown(void) {}
142
grpc_tracer_set_enabled(const char * name,int enabled)143 int grpc_tracer_set_enabled(const char* name, int enabled) {
144 return grpc_core::TraceFlagList::Set(name, enabled != 0);
145 }
146