Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 8a9fd52 | 2016-03-25 17:09:29 -0700 | [diff] [blame^] | 3 | * Copyright 2015-2016, Google Inc. |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 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 | |
Craig Tiller | af7abf9 | 2015-06-03 17:18:58 -0700 | [diff] [blame] | 38 | #include <grpc/grpc.h> |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 39 | #include <grpc/support/alloc.h> |
| 40 | #include <grpc/support/log.h> |
| 41 | #include "src/core/support/env.h" |
| 42 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 43 | typedef struct tracer { |
| 44 | const char *name; |
| 45 | int *flag; |
| 46 | struct tracer *next; |
| 47 | } tracer; |
| 48 | static tracer *tracers; |
| 49 | |
| 50 | void 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 Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 58 | |
| 59 | static 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 Tiller | 3121fd4 | 2015-09-10 09:56:20 -0700 | [diff] [blame] | 62 | 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 Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 69 | *ss = gpr_realloc(*ss, sizeof(char **) * np); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 70 | (*ss)[n] = s; |
| 71 | *ns = np; |
| 72 | } |
| 73 | |
| 74 | static 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 Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 80 | split(c + 1, ss, ns); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | static 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 Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 90 | for (i = 0; i < nstrings; i++) { |
Craig Tiller | af7abf9 | 2015-06-03 17:18:58 -0700 | [diff] [blame] | 91 | grpc_tracer_set_enabled(strings[i], 1); |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | for (i = 0; i < nstrings; i++) { |
| 95 | gpr_free(strings[i]); |
| 96 | } |
| 97 | gpr_free(strings); |
| 98 | } |
| 99 | |
Craig Tiller | faa8480 | 2015-03-01 21:56:38 -0800 | [diff] [blame] | 100 | void grpc_tracer_init(const char *env_var) { |
| 101 | char *e = gpr_getenv(env_var); |
| 102 | if (e != NULL) { |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 103 | parse(e); |
| 104 | gpr_free(e); |
| 105 | } |
Craig Tiller | 2f300e2 | 2015-06-04 08:28:43 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | void grpc_tracer_shutdown(void) { |
Craig Tiller | 077e75c | 2015-03-01 21:59:17 -0800 | [diff] [blame] | 109 | while (tracers) { |
| 110 | tracer *t = tracers; |
| 111 | tracers = t->next; |
| 112 | gpr_free(t); |
| 113 | } |
Craig Tiller | 6e7c622 | 2015-02-20 15:31:21 -0800 | [diff] [blame] | 114 | } |
Craig Tiller | af7abf9 | 2015-06-03 17:18:58 -0700 | [diff] [blame] | 115 | |
| 116 | int 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 Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 132 | return 0; /* early return */ |
Craig Tiller | af7abf9 | 2015-06-03 17:18:58 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | return 1; |
| 136 | } |