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 18659 : void grpc_register_tracer(const char *name, int *flag) {
51 18659 : tracer *t = gpr_malloc(sizeof(*t));
52 18659 : t->name = name;
53 18659 : t->flag = flag;
54 18659 : t->next = tracers;
55 18659 : *flag = 0;
56 18659 : tracers = t;
57 18659 : }
58 :
59 171 : static void add(const char *beg, const char *end, char ***ss, size_t *ns) {
60 171 : size_t n = *ns;
61 171 : size_t np = n + 1;
62 : char *s;
63 : size_t len;
64 171 : GPR_ASSERT(end >= beg);
65 171 : len = (size_t)(end - beg);
66 171 : s = gpr_malloc(len + 1);
67 171 : memcpy(s, beg, len);
68 171 : s[len] = 0;
69 171 : *ss = gpr_realloc(*ss, sizeof(char **) * np);
70 171 : (*ss)[n] = s;
71 171 : *ns = np;
72 171 : }
73 :
74 171 : static void split(const char *s, char ***ss, size_t *ns) {
75 171 : const char *c = strchr(s, ',');
76 171 : if (c == NULL) {
77 57 : add(s, s + strlen(s), ss, ns);
78 : } else {
79 114 : add(s, c, ss, ns);
80 114 : split(c + 1, ss, ns);
81 : }
82 171 : }
83 :
84 57 : static void parse(const char *s) {
85 57 : char **strings = NULL;
86 57 : size_t nstrings = 0;
87 : size_t i;
88 57 : split(s, &strings, &nstrings);
89 :
90 228 : for (i = 0; i < nstrings; i++) {
91 171 : grpc_tracer_set_enabled(strings[i], 1);
92 : }
93 :
94 228 : for (i = 0; i < nstrings; i++) {
95 171 : gpr_free(strings[i]);
96 : }
97 57 : gpr_free(strings);
98 57 : }
99 :
100 2501 : void grpc_tracer_init(const char *env_var) {
101 2501 : char *e = gpr_getenv(env_var);
102 2501 : if (e != NULL) {
103 57 : parse(e);
104 57 : gpr_free(e);
105 : }
106 2501 : }
107 :
108 2501 : void grpc_tracer_shutdown(void) {
109 23656 : while (tracers) {
110 18654 : tracer *t = tracers;
111 18654 : tracers = t->next;
112 18654 : gpr_free(t);
113 : }
114 2501 : }
115 :
116 342 : int grpc_tracer_set_enabled(const char *name, int enabled) {
117 : tracer *t;
118 342 : if (0 == strcmp(name, "all")) {
119 914 : for (t = tracers; t; t = t->next) {
120 800 : *t->flag = 1;
121 : }
122 : } else {
123 228 : int found = 0;
124 1828 : for (t = tracers; t; t = t->next) {
125 1600 : if (0 == strcmp(name, t->name)) {
126 114 : *t->flag = enabled;
127 114 : found = 1;
128 : }
129 : }
130 228 : if (!found) {
131 114 : gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name);
132 114 : return 0; /* early return */
133 : }
134 : }
135 228 : return 1;
136 : }
|