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