blob: 42150b1bc3e25158533b135d8eefd6b9b1772820 [file] [log] [blame]
Craig Tiller6e7c6222015-02-20 15:31:21 -08001/*
2 *
Craig Tiller8a9fd522016-03-25 17:09:29 -07003 * Copyright 2015-2016, Google Inc.
Craig Tiller6e7c6222015-02-20 15:31:21 -08004 * 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
Craig Tilleraf7abf92015-06-03 17:18:58 -070038#include <grpc/grpc.h>
Craig Tiller6e7c6222015-02-20 15:31:21 -080039#include <grpc/support/alloc.h>
40#include <grpc/support/log.h>
41#include "src/core/support/env.h"
42
Craig Tillerfaa84802015-03-01 21:56:38 -080043typedef struct tracer {
44 const char *name;
45 int *flag;
46 struct tracer *next;
47} tracer;
48static tracer *tracers;
49
50void grpc_register_tracer(const char *name, int *flag) {
51 tracer *t = gpr_malloc(sizeof(*t));
52 t->name = name;
53 t->flag = flag;
54 t->next = tracers;
55 *flag = 0;
56 tracers = t;
57}
Craig Tiller6e7c6222015-02-20 15:31:21 -080058
59static void add(const char *beg, const char *end, char ***ss, size_t *ns) {
60 size_t n = *ns;
61 size_t np = n + 1;
Craig Tiller3121fd42015-09-10 09:56:20 -070062 char *s;
63 size_t len;
64 GPR_ASSERT(end >= beg);
65 len = (size_t)(end - beg);
66 s = gpr_malloc(len + 1);
67 memcpy(s, beg, len);
68 s[len] = 0;
Craig Tillerd6c98df2015-08-18 09:33:44 -070069 *ss = gpr_realloc(*ss, sizeof(char **) * np);
Craig Tiller6e7c6222015-02-20 15:31:21 -080070 (*ss)[n] = s;
71 *ns = np;
72}
73
74static void split(const char *s, char ***ss, size_t *ns) {
75 const char *c = strchr(s, ',');
76 if (c == NULL) {
77 add(s, s + strlen(s), ss, ns);
78 } else {
79 add(s, c, ss, ns);
Craig Tillerd6c98df2015-08-18 09:33:44 -070080 split(c + 1, ss, ns);
Craig Tiller6e7c6222015-02-20 15:31:21 -080081 }
82}
83
84static void parse(const char *s) {
85 char **strings = NULL;
86 size_t nstrings = 0;
87 size_t i;
88 split(s, &strings, &nstrings);
89
Craig Tiller6e7c6222015-02-20 15:31:21 -080090 for (i = 0; i < nstrings; i++) {
Craig Tilleraf7abf92015-06-03 17:18:58 -070091 grpc_tracer_set_enabled(strings[i], 1);
Craig Tiller6e7c6222015-02-20 15:31:21 -080092 }
93
94 for (i = 0; i < nstrings; i++) {
95 gpr_free(strings[i]);
96 }
97 gpr_free(strings);
98}
99
Craig Tillerfaa84802015-03-01 21:56:38 -0800100void grpc_tracer_init(const char *env_var) {
101 char *e = gpr_getenv(env_var);
102 if (e != NULL) {
Craig Tiller6e7c6222015-02-20 15:31:21 -0800103 parse(e);
104 gpr_free(e);
105 }
Craig Tiller2f300e22015-06-04 08:28:43 -0700106}
107
108void grpc_tracer_shutdown(void) {
Craig Tiller077e75c2015-03-01 21:59:17 -0800109 while (tracers) {
110 tracer *t = tracers;
111 tracers = t->next;
112 gpr_free(t);
113 }
Craig Tiller6e7c6222015-02-20 15:31:21 -0800114}
Craig Tilleraf7abf92015-06-03 17:18:58 -0700115
116int grpc_tracer_set_enabled(const char *name, int enabled) {
117 tracer *t;
118 if (0 == strcmp(name, "all")) {
119 for (t = tracers; t; t = t->next) {
120 *t->flag = 1;
121 }
122 } else {
123 int found = 0;
124 for (t = tracers; t; t = t->next) {
125 if (0 == strcmp(name, t->name)) {
126 *t->flag = enabled;
127 found = 1;
128 }
129 }
130 if (!found) {
131 gpr_log(GPR_ERROR, "Unknown trace var: '%s'", name);
Craig Tillerd6c98df2015-08-18 09:33:44 -0700132 return 0; /* early return */
Craig Tilleraf7abf92015-06-03 17:18:58 -0700133 }
134 }
135 return 1;
136}