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