fe831366779a0332531fcfad63769742481549a0
[lttng-ust.git] / liblttng-ust / lttng-context-provider.c
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 <stddef.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28
29 #include <lttng/ust-context-provider.h>
30
31 #include "lttng-tracer-core.h"
32 #include "jhash.h"
33 #include "context-provider-internal.h"
34 #include <helper.h>
35
36 #define CONTEXT_PROVIDER_HT_BITS 12
37 #define CONTEXT_PROVIDER_HT_SIZE (1U << CONTEXT_PROVIDER_HT_BITS)
38 struct context_provider_ht {
39 struct cds_hlist_head table[CONTEXT_PROVIDER_HT_SIZE];
40 };
41
42 static struct context_provider_ht context_provider_ht;
43
44 static struct lttng_ust_context_provider *
45 lookup_provider_by_name(const char *name)
46 {
47 struct cds_hlist_head *head;
48 struct cds_hlist_node *node;
49 struct lttng_ust_context_provider *provider;
50 uint32_t hash;
51 const char *end;
52 size_t len;
53
54 /* Lookup using everything before first ':' as key. */
55 end = strchr(name, ':');
56 if (end)
57 len = end - name;
58 else
59 len = strlen(name);
60 hash = jhash(name, len, 0);
61 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
62 cds_hlist_for_each_entry(provider, node, head, node) {
63 if (!strncmp(provider->name, name, len))
64 return provider;
65 }
66 return NULL;
67 }
68
69 int lttng_ust_context_provider_register(struct lttng_ust_context_provider *provider)
70 {
71 struct cds_hlist_head *head;
72 size_t name_len = strlen(provider->name);
73 uint32_t hash;
74 int ret = 0;
75
76 lttng_ust_fixup_tls();
77
78 /* Provider name starts with "$app.". */
79 if (strncmp("$app.", provider->name, strlen("$app.")) != 0)
80 return -EINVAL;
81 /* Provider name cannot contain a colon character. */
82 if (strchr(provider->name, ':'))
83 return -EINVAL;
84 if (ust_lock()) {
85 ret = -EBUSY;
86 goto end;
87 }
88 if (lookup_provider_by_name(provider->name)) {
89 ret = -EBUSY;
90 goto end;
91 }
92 hash = jhash(provider->name, name_len, 0);
93 head = &context_provider_ht.table[hash & (CONTEXT_PROVIDER_HT_SIZE - 1)];
94 cds_hlist_add_head(&provider->node, head);
95
96 lttng_ust_context_set_session_provider(provider->name,
97 provider->get_size, provider->record,
98 provider->get_value);
99
100 lttng_ust_context_set_event_notifier_group_provider(provider->name,
101 provider->get_size, provider->record,
102 provider->get_value);
103 end:
104 ust_unlock();
105 return ret;
106 }
107
108 void lttng_ust_context_provider_unregister(struct lttng_ust_context_provider *provider)
109 {
110 lttng_ust_fixup_tls();
111
112 if (ust_lock())
113 goto end;
114 lttng_ust_context_set_session_provider(provider->name,
115 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
116 lttng_ust_dummy_get_value);
117
118 lttng_ust_context_set_event_notifier_group_provider(provider->name,
119 lttng_ust_dummy_get_size, lttng_ust_dummy_record,
120 lttng_ust_dummy_get_value);
121
122 cds_hlist_del(&provider->node);
123 end:
124 ust_unlock();
125 }
126
127 /*
128 * Called with ust mutex held.
129 * Add application context to array of context, even if the application
130 * context is not currently loaded by application. It will then use the
131 * dummy callbacks in that case.
132 * Always performed before tracing is started, since it modifies
133 * metadata describing the context.
134 */
135 int lttng_ust_add_app_context_to_ctx_rcu(const char *name,
136 struct lttng_ctx **ctx)
137 {
138 struct lttng_ust_context_provider *provider;
139 struct lttng_ctx_field new_field;
140 int ret;
141
142 if (*ctx && lttng_find_context(*ctx, name))
143 return -EEXIST;
144 /*
145 * For application context, add it by expanding
146 * ctx array.
147 */
148 memset(&new_field, 0, sizeof(new_field));
149 new_field.field_name = strdup(name);
150 if (!new_field.field_name)
151 return -ENOMEM;
152 new_field.event_field.name = new_field.field_name;
153 new_field.event_field.type.atype = atype_dynamic;
154 /*
155 * If provider is not found, we add the context anyway, but
156 * it will provide a dummy context.
157 */
158 provider = lookup_provider_by_name(name);
159 if (provider) {
160 new_field.get_size = provider->get_size;
161 new_field.record = provider->record;
162 new_field.get_value = provider->get_value;
163 } else {
164 new_field.get_size = lttng_ust_dummy_get_size;
165 new_field.record = lttng_ust_dummy_record;
166 new_field.get_value = lttng_ust_dummy_get_value;
167 }
168 ret = lttng_context_add_rcu(ctx, &new_field);
169 if (ret) {
170 free(new_field.field_name);
171 return ret;
172 }
173 return 0;
174 }
This page took 0.032304 seconds and 3 git commands to generate.