debug/trace.cc
Go to the documentation of this file.
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 
20 
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 
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 
74  flag->next_tracer_ = root_tracer_;
76 }
77 
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) {
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
144 }
145 
148  parse(value.get());
149 }
150 
151 void grpc_tracer_shutdown(void) {}
152 
153 int grpc_tracer_set_enabled(const char* name, int enabled) {
154  return grpc_core::TraceFlagList::Set(name, enabled != 0);
155 }
trace.h
flag
uint32_t flag
Definition: ssl_versions.cc:162
log.h
GPR_GLOBAL_CONFIG_GET
#define GPR_GLOBAL_CONFIG_GET(name)
Definition: global_config_generic.h:24
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::TraceFlag::TraceFlag
TraceFlag(bool default_enabled, const char *name)
Definition: debug/trace.cc:87
string.h
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
setup.name
name
Definition: setup.py:542
name_
const std::string name_
Definition: priority.cc:233
grpc_tracer_init
void grpc_tracer_init(const char *env_var_name)
Definition: debug/trace.cc:141
grpc_tracer_set_enabled
int grpc_tracer_set_enabled(const char *name, int enabled)
Definition: debug/trace.cc:153
grpc_core::TraceFlagList::LogAllTracers
static void LogAllTracers()
Definition: debug/trace.cc:78
GPR_GLOBAL_CONFIG_DEFINE_STRING
GPR_GLOBAL_CONFIG_DEFINE_STRING(grpc_trace, "", "A comma separated list of tracers that provide additional insight into " "how gRPC C core is processing requests via debug logs.")
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
gpr_realloc
GPRAPI void * gpr_realloc(void *p, size_t size)
Definition: alloc.cc:56
gen_stats_data.found
bool found
Definition: gen_stats_data.py:61
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc.h
grpc_core::TraceFlagList::root_tracer_
static TraceFlag * root_tracer_
Definition: debug/trace.h:56
grpc_core::TraceFlagList::Set
static bool Set(const char *name, bool enabled)
Definition: debug/trace.cc:42
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
grpc_core::UniquePtr
std::unique_ptr< T, DefaultDeleteChar > UniquePtr
Definition: src/core/lib/gprpp/memory.h:43
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
add
static void add(const char *beg, const char *end, char ***ss, size_t *ns)
Definition: debug/trace.cc:96
grpc_core::TraceFlag
Definition: debug/trace.h:63
value
const char * value
Definition: hpack_parser_table.cc:165
alloc.h
parse
static void parse(const char *s)
Definition: debug/trace.cc:121
ns
static int64_t ns
Definition: bloaty/third_party/re2/util/benchmark.cc:43
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc_core::TraceFlagList::Add
static void Add(TraceFlag *flag)
Definition: debug/trace.cc:73
grpc_core::TraceFlag::set_enabled
void set_enabled(bool enabled)
Definition: debug/trace.h:97
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
grpc_tracer_shutdown
void grpc_tracer_shutdown(void)
Definition: debug/trace.cc:151
split
static void split(const char *s, char ***ss, size_t *ns)
Definition: debug/trace.cc:111
grpc_trace
char * grpc_trace
Definition: php_grpc.h:80
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:40