| 1 | /* |
| 2 | * lttng-context-provider.c |
| 3 | * |
| 4 | * LTTng UST application context provider. |
| 5 | * |
| 6 | * Copyright (C) 2016 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation; only |
| 11 | * version 2.1 of the License. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #define _LGPL_SOURCE |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | #include <lttng/ust-context-provider.h> |
| 27 | #include "lttng-tracer-core.h" |
| 28 | #include "jhash.h" |
| 29 | #include <helper.h> |
| 30 | |
| 31 | #define CONTEXT_PROVIDER_HT_BITS 12 |
| 32 | #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS) |
| 33 | struct context_provider_ht { |
| 34 | struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE]; |
| 35 | }; |
| 36 | |
| 37 | static struct context_provider_ht context_provider_ht; |
| 38 | |
| 39 | static struct lttng_ust_context_provider * |
| 40 | lookup_provider_by_name(const char *name) |
| 41 | { |
| 42 | struct cds_hlist_head *head; |
| 43 | struct cds_hlist_node *node; |
| 44 | struct lttng_ust_context_provider *provider; |
| 45 | uint32_t hash; |
| 46 | const char *end; |
| 47 | size_t len; |
| 48 | |
| 49 | /* Lookup using everything before first ':' as key. */ |
| 50 | end = strchr(name, ':'); |
| 51 | if (end) |
| 52 | len = end - name; |
| 53 | else |
| 54 | len = strlen(name); |
| 55 | hash = jhash(name, len, 0); |
| 56 | head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)]; |
| 57 | cds_hlist_for_each_entry(provider, node, head, node) { |
| 58 | if (!strncmp(provider->name, name, len)) |
| 59 | return provider; |
| 60 | } |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | int lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider) |
| 65 | { |
| 66 | struct cds_hlist_head *head; |
| 67 | size_t name_len = strlen(provider->name); |
| 68 | uint32_t hash; |
| 69 | int ret = 0; |
| 70 | |
| 71 | lttng_ust_fixup_tls(); |
| 72 | |
| 73 | /* Provider name starts with "$app.". */ |
| 74 | if (strncmp("$app.", provider->name, strlen("$app.")) != 0) |
| 75 | return -EINVAL; |
| 76 | /* Provider name cannot contain a colon character. */ |
| 77 | if (strchr(provider->name, ':')) |
| 78 | return -EINVAL; |
| 79 | if (ust_lock()) { |
| 80 | ret = -EBUSY; |
| 81 | goto end; |
| 82 | } |
| 83 | if (lookup_provider_by_name(provider->name)) { |
| 84 | ret = -EBUSY; |
| 85 | goto end; |
| 86 | } |
| 87 | hash = jhash(provider->name, name_len, 0); |
| 88 | head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)]; |
| 89 | cds_hlist_add_head(&provider->node, head); |
| 90 | lttng_ust_context_set_session_provider(provider->name, |
| 91 | provider->get_size, provider->record, |
| 92 | provider->get_value); |
| 93 | end: |
| 94 | ust_unlock(); |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider *provider) |
| 99 | { |
| 100 | lttng_ust_fixup_tls(); |
| 101 | |
| 102 | if (ust_lock()) |
| 103 | goto end; |
| 104 | lttng_ust_context_set_session_provider(provider->name, |
| 105 | lttng_ust_dummy_get_size, lttng_ust_dummy_record, |
| 106 | lttng_ust_dummy_get_value); |
| 107 | cds_hlist_del(&provider->node); |
| 108 | end: |
| 109 | ust_unlock(); |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Called with ust mutex held. |
| 114 | * Add application context to array of context, even if the application |
| 115 | * context is not currently loaded by application. It will then use the |
| 116 | * dummy callbacks in that case. |
| 117 | * Always performed before tracing is started, since it modifies |
| 118 | * metadata describing the context. |
| 119 | */ |
| 120 | int lttng_ust_add_app_context_to_ctx_rcu(const char *name, |
| 121 | struct lttng_ctx **ctx) |
| 122 | { |
| 123 | struct lttng_ust_context_provider *provider; |
| 124 | struct lttng_ctx_field new_field; |
| 125 | int ret; |
| 126 | |
| 127 | if (*ctx && lttng_find_context(*ctx, name)) |
| 128 | return -EEXIST; |
| 129 | /* |
| 130 | * For application context, add it by expanding |
| 131 | * ctx array. |
| 132 | */ |
| 133 | memset(&new_field, 0, sizeof(new_field)); |
| 134 | new_field.field_name = strdup(name); |
| 135 | if (!new_field.field_name) |
| 136 | return -ENOMEM; |
| 137 | new_field.event_field.name = new_field.field_name; |
| 138 | new_field.event_field.type.atype = atype_dynamic; |
| 139 | /* |
| 140 | * If provider is not found, we add the context anyway, but |
| 141 | * it will provide a dummy context. |
| 142 | */ |
| 143 | provider = lookup_provider_by_name(name); |
| 144 | if (provider) { |
| 145 | new_field.get_size = provider->get_size; |
| 146 | new_field.record = provider->record; |
| 147 | new_field.get_value = provider->get_value; |
| 148 | } else { |
| 149 | new_field.get_size = lttng_ust_dummy_get_size; |
| 150 | new_field.record = lttng_ust_dummy_record; |
| 151 | new_field.get_value = lttng_ust_dummy_get_value; |
| 152 | } |
| 153 | ret = lttng_context_add_rcu(ctx, &new_field); |
| 154 | if (ret) { |
| 155 | free(new_field.field_name); |
| 156 | return ret; |
| 157 | } |
| 158 | return 0; |
| 159 | } |