Line data Source code
1 : /*
2 : *
3 : * Copyright 2015, Google Inc.
4 : * All rights reserved.
5 : *
6 : * Redistribution and use in source and binary forms, with or without
7 : * modification, are permitted provided that the following conditions are
8 : * met:
9 : *
10 : * * Redistributions of source code must retain the above copyright
11 : * notice, this list of conditions and the following disclaimer.
12 : * * Redistributions in binary form must reproduce the above
13 : * copyright notice, this list of conditions and the following disclaimer
14 : * in the documentation and/or other materials provided with the
15 : * distribution.
16 : * * Neither the name of Google Inc. nor the names of its
17 : * contributors may be used to endorse or promote products derived from
18 : * this software without specific prior written permission.
19 : *
20 : * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 : * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 : * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 : * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 : * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 : * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 : * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 : * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 : *
32 : */
33 :
34 : #include "src/core/debug/trace.h"
35 :
36 : #include <string.h>
37 :
38 : #include <grpc/grpc.h>
39 : #include <grpc/support/alloc.h>
40 : #include <grpc/support/log.h>
41 : #include "src/core/support/env.h"
42 :
43 : typedef struct tracer {
44 : const char *name;
45 : int *flag;
46 : struct tracer *next;
47 : } tracer;
48 : static tracer *tracers;
49 :
50 25465 : void grpc_register_tracer(const char *name, int *flag) {
51 25465 : tracer *t = gpr_malloc(sizeof(*t));
52 25465 : t->name = name;
53 25465 : t->flag = flag;
54 25465 : t->next = tracers;
55 25465 : *flag = 0;
56 25465 : tracers = t;
57 25465 : }
58 :
59 198 : static void add(const char *beg, const char *end, char ***ss, size_t *ns) {
60 198 : size_t n = *ns;
61 198 : size_t np = n + 1;
62 : char *s;
63 : size_t len;
64 198 : GPR_ASSERT(end >= beg);
65 198 : len = (size_t)(end - beg);
66 198 : s = gpr_malloc(len + 1);
67 198 : memcpy(s, beg, len);
68 198 : s[len] = 0;
69 198 : *ss = gpr_realloc(*ss, sizeof(char **) * np);
70 198 : (*ss)[n] = s;
71 198 : *ns = np;
72 198 : }
73 :
74 198 : static void split(const char *s, char ***ss, size_t *ns) {
75 198 : const char *c = strchr(s, ',');
76 198 : if (c == NULL) {
77 76 : add(s, s + strlen(s), ss, ns);
78 : } else {
79 122 : add(s, c, ss, ns);
80 122 : split(c + 1, ss, ns);
81 : }
82 198 : }
83 :
84 76 : static void parse(const char *s) {
85 76 : char **strings = NULL;
86 76 : size_t nstrings = 0;
87 : size_t i;
88 76 : split(s, &strings, &nstrings);
89 :
90 274 : for (i = 0; i < nstrings; i++) {
91 198 : grpc_tracer_set_enabled(strings[i], 1);
92 : }
93 :
94 273 : for (i = 0; i < nstrings; i++) {
95 198 : gpr_free(strings[i]);
96 : }
97 76 : gpr_free(strings);
98 76 : }
99 :
100 3452 : void grpc_tracer_init(const char *env_var) {
101 3452 : char *e = gpr_getenv(env_var);
102 3452 : if (e != NULL) {
103 76 : parse(e);
104 76 : gpr_free(e);
105 : }
106 3452 : }
107 :
108 3450 : void grpc_tracer_shutdown(void) {
109 32344 : while (tracers) {
110 25444 : tracer *t = tracers;
111 25444 : tracers = t->next;
112 25444 : gpr_free(t);
113 : }
114 3450 : }
115 :
116 381 : int grpc_tracer_set_enabled(const char *name, int enabled) {
117 : tracer *t;
118 381 : if (0 == strcmp(name, "all")) {
119 978 : for (t = tracers; t; t = t->next) {
120 856 : *t->flag = 1;
121 : }
122 : } else {
123 258 : int found = 0;
124 2091 : for (t = tracers; t; t = t->next) {
125 1832 : if (0 == strcmp(name, t->name)) {
126 137 : *t->flag = enabled;
127 136 : found = 1;
128 : }
129 : }
130 259 : if (!found) {
131 122 : gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name);
132 122 : return 0; /* early return */
133 : }
134 : }
135 258 : return 1;
136 : }
|